annotate gcc/tree-ssa-threadbackward.c @ 141:ce508c72660f

copy cbc flang in cfgexpand
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 22 Nov 2018 19:44:39 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* SSA Jump Threading
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "backend.h"
kono
parents:
diff changeset
24 #include "predict.h"
kono
parents:
diff changeset
25 #include "tree.h"
kono
parents:
diff changeset
26 #include "gimple.h"
kono
parents:
diff changeset
27 #include "fold-const.h"
kono
parents:
diff changeset
28 #include "cfgloop.h"
kono
parents:
diff changeset
29 #include "gimple-iterator.h"
kono
parents:
diff changeset
30 #include "tree-cfg.h"
kono
parents:
diff changeset
31 #include "tree-ssa-threadupdate.h"
kono
parents:
diff changeset
32 #include "params.h"
kono
parents:
diff changeset
33 #include "tree-ssa-loop.h"
kono
parents:
diff changeset
34 #include "cfganal.h"
kono
parents:
diff changeset
35 #include "tree-pass.h"
kono
parents:
diff changeset
36 #include "gimple-ssa.h"
kono
parents:
diff changeset
37 #include "tree-phinodes.h"
kono
parents:
diff changeset
38 #include "tree-inline.h"
kono
parents:
diff changeset
39 #include "tree-vectorizer.h"
kono
parents:
diff changeset
40
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
41 class thread_jumps
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
42 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
43 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
44 void find_jump_threads_backwards (basic_block bb, bool speed_p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
45 private:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
46 edge profitable_jump_thread_path (basic_block bbi, tree name, tree arg,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
47 bool *creates_irreducible_loop);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
48 void convert_and_register_current_path (edge taken_edge);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
49 void register_jump_thread_path_if_profitable (tree name, tree arg,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
50 basic_block def_bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
51 void handle_assignment (gimple *stmt, tree name, basic_block def_bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
52 void handle_phi (gphi *phi, tree name, basic_block def_bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
53 void fsm_find_control_statement_thread_paths (tree name);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
54 bool check_subpath_and_update_thread_path (basic_block last_bb,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
55 basic_block new_bb,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
56 int *next_path_length);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
57
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
58 /* Maximum number of BBs we are allowed to thread. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
59 int m_max_threaded_paths;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
60 /* Hash to keep track of seen bbs. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
61 hash_set<basic_block> m_visited_bbs;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
62 /* Current path we're analyzing. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
63 auto_vec<basic_block> m_path;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
64 /* Tracks if we have recursed through a loop PHI node. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
65 bool m_seen_loop_phi;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
66 /* Indicate that we could increase code size to improve the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
67 code path. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
68 bool m_speed_p;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
69 };
111
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 /* Simple helper to get the last statement from BB, which is assumed
kono
parents:
diff changeset
72 to be a control statement. Return NULL if the last statement is
kono
parents:
diff changeset
73 not a control statement. */
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 static gimple *
kono
parents:
diff changeset
76 get_gimple_control_stmt (basic_block bb)
kono
parents:
diff changeset
77 {
kono
parents:
diff changeset
78 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 if (gsi_end_p (gsi))
kono
parents:
diff changeset
81 return NULL;
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
84 enum gimple_code code = gimple_code (stmt);
kono
parents:
diff changeset
85 if (code == GIMPLE_COND || code == GIMPLE_SWITCH || code == GIMPLE_GOTO)
kono
parents:
diff changeset
86 return stmt;
kono
parents:
diff changeset
87 return NULL;
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 /* Return true if the CFG contains at least one path from START_BB to
kono
parents:
diff changeset
91 END_BB. When a path is found, record in PATH the blocks from
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
92 END_BB to START_BB. LOCAL_VISITED_BBS is used to make sure we
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
93 don't fall into an infinite loop. Bound the recursion to basic
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
94 blocks belonging to LOOP. */
111
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 static bool
kono
parents:
diff changeset
97 fsm_find_thread_path (basic_block start_bb, basic_block end_bb,
kono
parents:
diff changeset
98 vec<basic_block> &path,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
99 hash_set<basic_block> &local_visited_bbs,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
100 loop_p loop)
111
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 if (loop != start_bb->loop_father)
kono
parents:
diff changeset
103 return false;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 if (start_bb == end_bb)
kono
parents:
diff changeset
106 {
kono
parents:
diff changeset
107 path.safe_push (start_bb);
kono
parents:
diff changeset
108 return true;
kono
parents:
diff changeset
109 }
kono
parents:
diff changeset
110
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
111 if (!local_visited_bbs.add (start_bb))
111
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 edge e;
kono
parents:
diff changeset
114 edge_iterator ei;
kono
parents:
diff changeset
115 FOR_EACH_EDGE (e, ei, start_bb->succs)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
116 if (fsm_find_thread_path (e->dest, end_bb, path, local_visited_bbs,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
117 loop))
111
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 path.safe_push (start_bb);
kono
parents:
diff changeset
120 return true;
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 return false;
kono
parents:
diff changeset
125 }
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 /* Examine jump threading path PATH to which we want to add BBI.
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 If the resulting path is profitable to thread, then return the
kono
parents:
diff changeset
130 final taken edge from the path, NULL otherwise.
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 NAME is the SSA_NAME of the variable we found to have a constant
kono
parents:
diff changeset
133 value on PATH. ARG is the constant value of NAME on that path.
kono
parents:
diff changeset
134
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
135 BBI will be appended to PATH when we have a profitable jump
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
136 threading path. Callers are responsible for removing BBI from PATH
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
137 in that case. */
111
kono
parents:
diff changeset
138
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139 edge
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
140 thread_jumps::profitable_jump_thread_path (basic_block bbi, tree name,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
141 tree arg,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
142 bool *creates_irreducible_loop)
111
kono
parents:
diff changeset
143 {
kono
parents:
diff changeset
144 /* Note BBI is not in the path yet, hence the +1 in the test below
kono
parents:
diff changeset
145 to make sure BBI is accounted for in the path length test. */
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 /* We can get a length of 0 here when the statement that
kono
parents:
diff changeset
148 makes a conditional generate a compile-time constant
kono
parents:
diff changeset
149 result is in the same block as the conditional.
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 That's not really a jump threading opportunity, but instead is
kono
parents:
diff changeset
152 simple cprop & simplification. We could handle it here if we
kono
parents:
diff changeset
153 wanted by wiring up all the incoming edges. If we run this
kono
parents:
diff changeset
154 early in IPA, that might be worth doing. For now we just
kono
parents:
diff changeset
155 reject that case. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
156 if (m_path.is_empty ())
111
kono
parents:
diff changeset
157 return NULL;
kono
parents:
diff changeset
158
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159 if (m_path.length () + 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 > (unsigned) PARAM_VALUE (PARAM_MAX_FSM_THREAD_LENGTH))
111
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
163 fprintf (dump_file, "FSM jump-thread path not considered: "
kono
parents:
diff changeset
164 "the number of basic blocks on the path "
kono
parents:
diff changeset
165 "exceeds PARAM_MAX_FSM_THREAD_LENGTH.\n");
kono
parents:
diff changeset
166 return NULL;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
169 if (m_max_threaded_paths <= 0)
111
kono
parents:
diff changeset
170 {
kono
parents:
diff changeset
171 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
172 fprintf (dump_file, "FSM jump-thread path not considered: "
kono
parents:
diff changeset
173 "the number of previously recorded FSM paths to "
kono
parents:
diff changeset
174 "thread exceeds PARAM_MAX_FSM_THREAD_PATHS.\n");
kono
parents:
diff changeset
175 return NULL;
kono
parents:
diff changeset
176 }
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 /* Add BBI to the path.
kono
parents:
diff changeset
179 From this point onward, if we decide we the path is not profitable
kono
parents:
diff changeset
180 to thread, we must remove BBI from the path. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 m_path.safe_push (bbi);
111
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 int n_insns = 0;
kono
parents:
diff changeset
184 gimple_stmt_iterator gsi;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185 loop_p loop = m_path[0]->loop_father;
111
kono
parents:
diff changeset
186 bool path_crosses_loops = false;
kono
parents:
diff changeset
187 bool threaded_through_latch = false;
kono
parents:
diff changeset
188 bool multiway_branch_in_path = false;
kono
parents:
diff changeset
189 bool threaded_multiway_branch = false;
kono
parents:
diff changeset
190 bool contains_hot_bb = false;
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
193 fprintf (dump_file, "Checking profitability of path (backwards): ");
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 /* Count the number of instructions on the path: as these instructions
kono
parents:
diff changeset
196 will have to be duplicated, we will not record the path if there
kono
parents:
diff changeset
197 are too many instructions on the path. Also check that all the
kono
parents:
diff changeset
198 blocks in the path belong to a single loop. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
199 for (unsigned j = 0; j < m_path.length (); j++)
111
kono
parents:
diff changeset
200 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
201 basic_block bb = m_path[j];
111
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
204 fprintf (dump_file, " bb:%i", bb->index);
kono
parents:
diff changeset
205 /* Remember, blocks in the path are stored in opposite order
kono
parents:
diff changeset
206 in the PATH array. The last entry in the array represents
kono
parents:
diff changeset
207 the block with an outgoing edge that we will redirect to the
kono
parents:
diff changeset
208 jump threading path. Thus we don't care about that block's
kono
parents:
diff changeset
209 loop father, nor how many statements are in that block because
kono
parents:
diff changeset
210 it will not be copied or whether or not it ends in a multiway
kono
parents:
diff changeset
211 branch. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
212 if (j < m_path.length () - 1)
111
kono
parents:
diff changeset
213 {
kono
parents:
diff changeset
214 int orig_n_insns = n_insns;
kono
parents:
diff changeset
215 if (bb->loop_father != loop)
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 path_crosses_loops = true;
kono
parents:
diff changeset
218 break;
kono
parents:
diff changeset
219 }
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 /* PHIs in the path will create degenerate PHIS in the
kono
parents:
diff changeset
222 copied path which will then get propagated away, so
kono
parents:
diff changeset
223 looking at just the duplicate path the PHIs would
kono
parents:
diff changeset
224 seem unimportant.
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 But those PHIs, because they're assignments to objects
kono
parents:
diff changeset
227 typically with lives that exist outside the thread path,
kono
parents:
diff changeset
228 will tend to generate PHIs (or at least new PHI arguments)
kono
parents:
diff changeset
229 at points where we leave the thread path and rejoin
kono
parents:
diff changeset
230 the original blocks. So we do want to account for them.
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 We ignore virtual PHIs. We also ignore cases where BB
kono
parents:
diff changeset
233 has a single incoming edge. That's the most common
kono
parents:
diff changeset
234 degenerate PHI we'll see here. Finally we ignore PHIs
kono
parents:
diff changeset
235 that are associated with the value we're tracking as
kono
parents:
diff changeset
236 that object likely dies. */
kono
parents:
diff changeset
237 if (EDGE_COUNT (bb->succs) > 1 && EDGE_COUNT (bb->preds) > 1)
kono
parents:
diff changeset
238 {
kono
parents:
diff changeset
239 for (gphi_iterator gsip = gsi_start_phis (bb);
kono
parents:
diff changeset
240 !gsi_end_p (gsip);
kono
parents:
diff changeset
241 gsi_next (&gsip))
kono
parents:
diff changeset
242 {
kono
parents:
diff changeset
243 gphi *phi = gsip.phi ();
kono
parents:
diff changeset
244 tree dst = gimple_phi_result (phi);
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* Note that if both NAME and DST are anonymous
kono
parents:
diff changeset
247 SSA_NAMEs, then we do not have enough information
kono
parents:
diff changeset
248 to consider them associated. */
kono
parents:
diff changeset
249 if (dst != name
kono
parents:
diff changeset
250 && (SSA_NAME_VAR (dst) != SSA_NAME_VAR (name)
kono
parents:
diff changeset
251 || !SSA_NAME_VAR (dst))
kono
parents:
diff changeset
252 && !virtual_operand_p (dst))
kono
parents:
diff changeset
253 ++n_insns;
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
257 if (!contains_hot_bb && m_speed_p)
111
kono
parents:
diff changeset
258 contains_hot_bb |= optimize_bb_for_speed_p (bb);
kono
parents:
diff changeset
259 for (gsi = gsi_after_labels (bb);
kono
parents:
diff changeset
260 !gsi_end_p (gsi);
kono
parents:
diff changeset
261 gsi_next_nondebug (&gsi))
kono
parents:
diff changeset
262 {
kono
parents:
diff changeset
263 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
264 /* Do not count empty statements and labels. */
kono
parents:
diff changeset
265 if (gimple_code (stmt) != GIMPLE_NOP
kono
parents:
diff changeset
266 && !(gimple_code (stmt) == GIMPLE_ASSIGN
kono
parents:
diff changeset
267 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
kono
parents:
diff changeset
268 && !is_gimple_debug (stmt))
kono
parents:
diff changeset
269 n_insns += estimate_num_insns (stmt, &eni_size_weights);
kono
parents:
diff changeset
270 }
kono
parents:
diff changeset
271 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
272 fprintf (dump_file, " (%i insns)", n_insns-orig_n_insns);
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 /* We do not look at the block with the threaded branch
kono
parents:
diff changeset
275 in this loop. So if any block with a last statement that
kono
parents:
diff changeset
276 is a GIMPLE_SWITCH or GIMPLE_GOTO is seen, then we have a
kono
parents:
diff changeset
277 multiway branch on our path.
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 The block in PATH[0] is special, it's the block were we're
kono
parents:
diff changeset
280 going to be able to eliminate its branch. */
kono
parents:
diff changeset
281 gimple *last = last_stmt (bb);
kono
parents:
diff changeset
282 if (last && (gimple_code (last) == GIMPLE_SWITCH
kono
parents:
diff changeset
283 || gimple_code (last) == GIMPLE_GOTO))
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 if (j == 0)
kono
parents:
diff changeset
286 threaded_multiway_branch = true;
kono
parents:
diff changeset
287 else
kono
parents:
diff changeset
288 multiway_branch_in_path = true;
kono
parents:
diff changeset
289 }
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 /* Note if we thread through the latch, we will want to include
kono
parents:
diff changeset
293 the last entry in the array when determining if we thread
kono
parents:
diff changeset
294 through the loop latch. */
kono
parents:
diff changeset
295 if (loop->latch == bb)
kono
parents:
diff changeset
296 threaded_through_latch = true;
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
299 gimple *stmt = get_gimple_control_stmt (m_path[0]);
111
kono
parents:
diff changeset
300 gcc_assert (stmt);
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 /* We are going to remove the control statement at the end of the
kono
parents:
diff changeset
303 last block in the threading path. So don't count it against our
kono
parents:
diff changeset
304 statement count. */
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 int stmt_insns = estimate_num_insns (stmt, &eni_size_weights);
kono
parents:
diff changeset
307 n_insns-= stmt_insns;
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
310 fprintf (dump_file, "\n Control statement insns: %i\n"
kono
parents:
diff changeset
311 " Overall: %i insns\n",
kono
parents:
diff changeset
312 stmt_insns, n_insns);
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 /* We have found a constant value for ARG. For GIMPLE_SWITCH
kono
parents:
diff changeset
315 and GIMPLE_GOTO, we use it as-is. However, for a GIMPLE_COND
kono
parents:
diff changeset
316 we need to substitute, fold and simplify so we can determine
kono
parents:
diff changeset
317 the edge taken out of the last block. */
kono
parents:
diff changeset
318 if (gimple_code (stmt) == GIMPLE_COND)
kono
parents:
diff changeset
319 {
kono
parents:
diff changeset
320 enum tree_code cond_code = gimple_cond_code (stmt);
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 /* We know the underyling format of the condition. */
kono
parents:
diff changeset
323 arg = fold_binary (cond_code, boolean_type_node,
kono
parents:
diff changeset
324 arg, gimple_cond_rhs (stmt));
kono
parents:
diff changeset
325 }
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 /* If this path threaded through the loop latch back into the
kono
parents:
diff changeset
328 same loop and the destination does not dominate the loop
kono
parents:
diff changeset
329 latch, then this thread would create an irreducible loop.
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 We have to know the outgoing edge to figure this out. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
332 edge taken_edge = find_taken_edge (m_path[0], arg);
111
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 /* There are cases where we may not be able to extract the
kono
parents:
diff changeset
335 taken edge. For example, a computed goto to an absolute
kono
parents:
diff changeset
336 address. Handle those cases gracefully. */
kono
parents:
diff changeset
337 if (taken_edge == NULL)
kono
parents:
diff changeset
338 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
339 m_path.pop ();
111
kono
parents:
diff changeset
340 return NULL;
kono
parents:
diff changeset
341 }
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 *creates_irreducible_loop = false;
kono
parents:
diff changeset
344 if (threaded_through_latch
kono
parents:
diff changeset
345 && loop == taken_edge->dest->loop_father
kono
parents:
diff changeset
346 && (determine_bb_domination_status (loop, taken_edge->dest)
kono
parents:
diff changeset
347 == DOMST_NONDOMINATING))
kono
parents:
diff changeset
348 *creates_irreducible_loop = true;
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 if (path_crosses_loops)
kono
parents:
diff changeset
351 {
kono
parents:
diff changeset
352 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
353 fprintf (dump_file, "FSM jump-thread path not considered: "
kono
parents:
diff changeset
354 "the path crosses loops.\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
355 m_path.pop ();
111
kono
parents:
diff changeset
356 return NULL;
kono
parents:
diff changeset
357 }
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 /* Threading is profitable if the path duplicated is hot but also
kono
parents:
diff changeset
360 in a case we separate cold path from hot path and permit optimization
kono
parents:
diff changeset
361 of the hot path later. Be on the agressive side here. In some testcases,
kono
parents:
diff changeset
362 as in PR 78407 this leads to noticeable improvements. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
363 if (m_speed_p && (optimize_edge_for_speed_p (taken_edge) || contains_hot_bb))
111
kono
parents:
diff changeset
364 {
kono
parents:
diff changeset
365 if (n_insns >= PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATH_INSNS))
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
368 fprintf (dump_file, "FSM jump-thread path not considered: "
kono
parents:
diff changeset
369 "the number of instructions on the path "
kono
parents:
diff changeset
370 "exceeds PARAM_MAX_FSM_THREAD_PATH_INSNS.\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
371 m_path.pop ();
111
kono
parents:
diff changeset
372 return NULL;
kono
parents:
diff changeset
373 }
kono
parents:
diff changeset
374 }
kono
parents:
diff changeset
375 else if (n_insns > 1)
kono
parents:
diff changeset
376 {
kono
parents:
diff changeset
377 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
378 fprintf (dump_file, "FSM jump-thread path not considered: "
kono
parents:
diff changeset
379 "duplication of %i insns is needed and optimizing for size.\n",
kono
parents:
diff changeset
380 n_insns);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
381 m_path.pop ();
111
kono
parents:
diff changeset
382 return NULL;
kono
parents:
diff changeset
383 }
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 /* We avoid creating irreducible inner loops unless we thread through
kono
parents:
diff changeset
386 a multiway branch, in which case we have deemed it worth losing
kono
parents:
diff changeset
387 other loop optimizations later.
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 We also consider it worth creating an irreducible inner loop if
kono
parents:
diff changeset
390 the number of copied statement is low relative to the length of
kono
parents:
diff changeset
391 the path -- in that case there's little the traditional loop
kono
parents:
diff changeset
392 optimizer would have done anyway, so an irreducible loop is not
kono
parents:
diff changeset
393 so bad. */
kono
parents:
diff changeset
394 if (!threaded_multiway_branch && *creates_irreducible_loop
kono
parents:
diff changeset
395 && (n_insns * (unsigned) PARAM_VALUE (PARAM_FSM_SCALE_PATH_STMTS)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
396 > (m_path.length () *
111
kono
parents:
diff changeset
397 (unsigned) PARAM_VALUE (PARAM_FSM_SCALE_PATH_BLOCKS))))
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 {
kono
parents:
diff changeset
400 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
401 fprintf (dump_file,
kono
parents:
diff changeset
402 "FSM would create irreducible loop without threading "
kono
parents:
diff changeset
403 "multiway branch.\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
404 m_path.pop ();
111
kono
parents:
diff changeset
405 return NULL;
kono
parents:
diff changeset
406 }
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 /* If this path does not thread through the loop latch, then we are
kono
parents:
diff changeset
410 using the FSM threader to find old style jump threads. This
kono
parents:
diff changeset
411 is good, except the FSM threader does not re-use an existing
kono
parents:
diff changeset
412 threading path to reduce code duplication.
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 So for that case, drastically reduce the number of statements
kono
parents:
diff changeset
415 we are allowed to copy. */
kono
parents:
diff changeset
416 if (!(threaded_through_latch && threaded_multiway_branch)
kono
parents:
diff changeset
417 && (n_insns * PARAM_VALUE (PARAM_FSM_SCALE_PATH_STMTS)
kono
parents:
diff changeset
418 >= PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS)))
kono
parents:
diff changeset
419 {
kono
parents:
diff changeset
420 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
421 fprintf (dump_file,
kono
parents:
diff changeset
422 "FSM did not thread around loop and would copy too "
kono
parents:
diff changeset
423 "many statements.\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
424 m_path.pop ();
111
kono
parents:
diff changeset
425 return NULL;
kono
parents:
diff changeset
426 }
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 /* When there is a multi-way branch on the path, then threading can
kono
parents:
diff changeset
429 explode the CFG due to duplicating the edges for that multi-way
kono
parents:
diff changeset
430 branch. So like above, only allow a multi-way branch on the path
kono
parents:
diff changeset
431 if we actually thread a multi-way branch. */
kono
parents:
diff changeset
432 if (!threaded_multiway_branch && multiway_branch_in_path)
kono
parents:
diff changeset
433 {
kono
parents:
diff changeset
434 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
435 fprintf (dump_file,
kono
parents:
diff changeset
436 "FSM Thread through multiway branch without threading "
kono
parents:
diff changeset
437 "a multiway branch.\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
438 m_path.pop ();
111
kono
parents:
diff changeset
439 return NULL;
kono
parents:
diff changeset
440 }
kono
parents:
diff changeset
441 return taken_edge;
kono
parents:
diff changeset
442 }
kono
parents:
diff changeset
443
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
444 /* The current path PATH is a vector of blocks forming a jump threading
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
445 path in reverse order. TAKEN_EDGE is the edge taken from path[0].
111
kono
parents:
diff changeset
446
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
447 Convert the current path into the form used by register_jump_thread and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
448 register it. */
111
kono
parents:
diff changeset
449
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
450 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
451 thread_jumps::convert_and_register_current_path (edge taken_edge)
111
kono
parents:
diff changeset
452 {
kono
parents:
diff changeset
453 vec<jump_thread_edge *> *jump_thread_path = new vec<jump_thread_edge *> ();
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 /* Record the edges between the blocks in PATH. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
456 for (unsigned int j = 0; j + 1 < m_path.length (); j++)
111
kono
parents:
diff changeset
457 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 basic_block bb1 = m_path[m_path.length () - j - 1];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
459 basic_block bb2 = m_path[m_path.length () - j - 2];
111
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 edge e = find_edge (bb1, bb2);
kono
parents:
diff changeset
462 gcc_assert (e);
kono
parents:
diff changeset
463 jump_thread_edge *x = new jump_thread_edge (e, EDGE_FSM_THREAD);
kono
parents:
diff changeset
464 jump_thread_path->safe_push (x);
kono
parents:
diff changeset
465 }
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 /* Add the edge taken when the control variable has value ARG. */
kono
parents:
diff changeset
468 jump_thread_edge *x
kono
parents:
diff changeset
469 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
kono
parents:
diff changeset
470 jump_thread_path->safe_push (x);
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 register_jump_thread (jump_thread_path);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 --m_max_threaded_paths;
111
kono
parents:
diff changeset
474 }
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 /* While following a chain of SSA_NAME definitions, we jumped from a
kono
parents:
diff changeset
477 definition in LAST_BB to a definition in NEW_BB (walking
kono
parents:
diff changeset
478 backwards).
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 Verify there is a single path between the blocks and none of the
kono
parents:
diff changeset
481 blocks in the path is already in VISITED_BBS. If so, then update
kono
parents:
diff changeset
482 VISISTED_BBS, add the new blocks to PATH and return TRUE.
kono
parents:
diff changeset
483 Otherwise return FALSE.
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 Store the length of the subpath in NEXT_PATH_LENGTH. */
kono
parents:
diff changeset
486
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
487 bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488 thread_jumps::check_subpath_and_update_thread_path (basic_block last_bb,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
489 basic_block new_bb,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
490 int *next_path_length)
111
kono
parents:
diff changeset
491 {
kono
parents:
diff changeset
492 edge e;
kono
parents:
diff changeset
493 int e_count = 0;
kono
parents:
diff changeset
494 edge_iterator ei;
kono
parents:
diff changeset
495 auto_vec<basic_block> next_path;
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 FOR_EACH_EDGE (e, ei, last_bb->preds)
kono
parents:
diff changeset
498 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
499 hash_set<basic_block> local_visited_bbs;
111
kono
parents:
diff changeset
500
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 if (fsm_find_thread_path (new_bb, e->src, next_path,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 local_visited_bbs, e->src->loop_father))
111
kono
parents:
diff changeset
503 ++e_count;
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 /* If there is more than one path, stop. */
kono
parents:
diff changeset
506 if (e_count > 1)
kono
parents:
diff changeset
507 return false;
kono
parents:
diff changeset
508 }
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 /* Stop if we have not found a path: this could occur when the recursion
kono
parents:
diff changeset
511 is stopped by one of the bounds. */
kono
parents:
diff changeset
512 if (e_count == 0)
kono
parents:
diff changeset
513 return false;
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 /* Make sure we haven't already visited any of the nodes in
kono
parents:
diff changeset
516 NEXT_PATH. Don't add them here to avoid pollution. */
kono
parents:
diff changeset
517 for (unsigned int i = 0; i + 1 < next_path.length (); i++)
kono
parents:
diff changeset
518 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
519 if (m_visited_bbs.contains (next_path[i]))
111
kono
parents:
diff changeset
520 return false;
kono
parents:
diff changeset
521 }
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 /* Now add the nodes to VISISTED_BBS. */
kono
parents:
diff changeset
524 for (unsigned int i = 0; i + 1 < next_path.length (); i++)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
525 m_visited_bbs.add (next_path[i]);
111
kono
parents:
diff changeset
526
kono
parents:
diff changeset
527 /* Append all the nodes from NEXT_PATH to PATH. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
528 m_path.safe_splice (next_path);
111
kono
parents:
diff changeset
529 *next_path_length = next_path.length ();
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 return true;
kono
parents:
diff changeset
532 }
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 /* If this is a profitable jump thread path, register it.
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 NAME is an SSA NAME with a possible constant value of ARG on PATH.
kono
parents:
diff changeset
537
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
538 DEF_BB is the basic block that ultimately defines the constant. */
111
kono
parents:
diff changeset
539
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
540 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
541 thread_jumps::register_jump_thread_path_if_profitable (tree name, tree arg,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
542 basic_block def_bb)
111
kono
parents:
diff changeset
543 {
kono
parents:
diff changeset
544 if (TREE_CODE_CLASS (TREE_CODE (arg)) != tcc_constant)
kono
parents:
diff changeset
545 return;
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 bool irreducible = false;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
548 edge taken_edge = profitable_jump_thread_path (def_bb, name, arg,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
549 &irreducible);
111
kono
parents:
diff changeset
550 if (taken_edge)
kono
parents:
diff changeset
551 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
552 convert_and_register_current_path (taken_edge);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
553 m_path.pop ();
111
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 if (irreducible)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
556 vect_free_loop_info_assumptions (m_path[0]->loop_father);
111
kono
parents:
diff changeset
557 }
kono
parents:
diff changeset
558 }
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 /* Given PHI which defines NAME in block DEF_BB, recurse through the
kono
parents:
diff changeset
561 PHI's arguments searching for paths where NAME will ultimately have
kono
parents:
diff changeset
562 a constant value.
kono
parents:
diff changeset
563
kono
parents:
diff changeset
564 PATH contains the series of blocks to traverse that will result in
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
565 NAME having a constant value. */
111
kono
parents:
diff changeset
566
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
567 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
568 thread_jumps::handle_phi (gphi *phi, tree name, basic_block def_bb)
111
kono
parents:
diff changeset
569 {
kono
parents:
diff changeset
570 /* Iterate over the arguments of PHI. */
kono
parents:
diff changeset
571 for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
kono
parents:
diff changeset
572 {
kono
parents:
diff changeset
573 tree arg = gimple_phi_arg_def (phi, i);
kono
parents:
diff changeset
574 basic_block bbi = gimple_phi_arg_edge (phi, i)->src;
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 /* Skip edges pointing outside the current loop. */
kono
parents:
diff changeset
577 if (!arg || def_bb->loop_father != bbi->loop_father)
kono
parents:
diff changeset
578 continue;
kono
parents:
diff changeset
579
kono
parents:
diff changeset
580 if (TREE_CODE (arg) == SSA_NAME)
kono
parents:
diff changeset
581 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
582 m_path.safe_push (bbi);
111
kono
parents:
diff changeset
583 /* Recursively follow SSA_NAMEs looking for a constant
kono
parents:
diff changeset
584 definition. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
585 fsm_find_control_statement_thread_paths (arg);
111
kono
parents:
diff changeset
586
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
587 m_path.pop ();
111
kono
parents:
diff changeset
588 continue;
kono
parents:
diff changeset
589 }
kono
parents:
diff changeset
590
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
591 register_jump_thread_path_if_profitable (name, arg, bbi);
111
kono
parents:
diff changeset
592 }
kono
parents:
diff changeset
593 }
kono
parents:
diff changeset
594
kono
parents:
diff changeset
595 /* Return TRUE if STMT is a gimple assignment we want to either directly
kono
parents:
diff changeset
596 handle or recurse through. Return FALSE otherwise.
kono
parents:
diff changeset
597
kono
parents:
diff changeset
598 Note that adding more cases here requires adding cases to handle_assignment
kono
parents:
diff changeset
599 below. */
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 static bool
kono
parents:
diff changeset
602 handle_assignment_p (gimple *stmt)
kono
parents:
diff changeset
603 {
kono
parents:
diff changeset
604 if (is_gimple_assign (stmt))
kono
parents:
diff changeset
605 {
kono
parents:
diff changeset
606 enum tree_code def_code = gimple_assign_rhs_code (stmt);
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 /* If the RHS is an SSA_NAME, then we will recurse through it.
kono
parents:
diff changeset
609 Go ahead and filter out cases where the SSA_NAME is a default
kono
parents:
diff changeset
610 definition. There's little to be gained by trying to handle that. */
kono
parents:
diff changeset
611 if (def_code == SSA_NAME
kono
parents:
diff changeset
612 && !SSA_NAME_IS_DEFAULT_DEF (gimple_assign_rhs1 (stmt)))
kono
parents:
diff changeset
613 return true;
kono
parents:
diff changeset
614
kono
parents:
diff changeset
615 /* If the RHS is a constant, then it's a terminal that we'll want
kono
parents:
diff changeset
616 to handle as well. */
kono
parents:
diff changeset
617 if (TREE_CODE_CLASS (def_code) == tcc_constant)
kono
parents:
diff changeset
618 return true;
kono
parents:
diff changeset
619 }
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 /* Anything not explicitly allowed is not handled. */
kono
parents:
diff changeset
622 return false;
kono
parents:
diff changeset
623 }
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 /* Given STMT which defines NAME in block DEF_BB, recurse through the
kono
parents:
diff changeset
626 PHI's arguments searching for paths where NAME will ultimately have
kono
parents:
diff changeset
627 a constant value.
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 PATH contains the series of blocks to traverse that will result in
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
630 NAME having a constant value. */
111
kono
parents:
diff changeset
631
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
632 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
633 thread_jumps::handle_assignment (gimple *stmt, tree name, basic_block def_bb)
111
kono
parents:
diff changeset
634 {
kono
parents:
diff changeset
635 tree arg = gimple_assign_rhs1 (stmt);
kono
parents:
diff changeset
636
kono
parents:
diff changeset
637 if (TREE_CODE (arg) == SSA_NAME)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
638 fsm_find_control_statement_thread_paths (arg);
111
kono
parents:
diff changeset
639
kono
parents:
diff changeset
640 else
kono
parents:
diff changeset
641 {
kono
parents:
diff changeset
642 /* register_jump_thread_path_if_profitable will push the current
kono
parents:
diff changeset
643 block onto the path. But the path will always have the current
kono
parents:
diff changeset
644 block at this point. So we can just pop it. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
645 m_path.pop ();
111
kono
parents:
diff changeset
646
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
647 register_jump_thread_path_if_profitable (name, arg, def_bb);
111
kono
parents:
diff changeset
648
kono
parents:
diff changeset
649 /* And put the current block back onto the path so that the
kono
parents:
diff changeset
650 state of the stack is unchanged when we leave. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
651 m_path.safe_push (def_bb);
111
kono
parents:
diff changeset
652 }
kono
parents:
diff changeset
653 }
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 /* We trace the value of the SSA_NAME NAME back through any phi nodes
kono
parents:
diff changeset
656 looking for places where it gets a constant value and save the
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
657 path. */
111
kono
parents:
diff changeset
658
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
659 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
660 thread_jumps::fsm_find_control_statement_thread_paths (tree name)
111
kono
parents:
diff changeset
661 {
kono
parents:
diff changeset
662 /* If NAME appears in an abnormal PHI, then don't try to trace its
kono
parents:
diff changeset
663 value back through PHI nodes. */
kono
parents:
diff changeset
664 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
kono
parents:
diff changeset
665 return;
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
kono
parents:
diff changeset
668 basic_block def_bb = gimple_bb (def_stmt);
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 if (def_bb == NULL)
kono
parents:
diff changeset
671 return;
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 /* We allow the SSA chain to contains PHIs and simple copies and constant
kono
parents:
diff changeset
674 initializations. */
kono
parents:
diff changeset
675 if (gimple_code (def_stmt) != GIMPLE_PHI
kono
parents:
diff changeset
676 && gimple_code (def_stmt) != GIMPLE_ASSIGN)
kono
parents:
diff changeset
677 return;
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 if (gimple_code (def_stmt) == GIMPLE_PHI
kono
parents:
diff changeset
680 && (gimple_phi_num_args (def_stmt)
kono
parents:
diff changeset
681 >= (unsigned) PARAM_VALUE (PARAM_FSM_MAXIMUM_PHI_ARGUMENTS)))
kono
parents:
diff changeset
682 return;
kono
parents:
diff changeset
683
kono
parents:
diff changeset
684 if (is_gimple_assign (def_stmt)
kono
parents:
diff changeset
685 && ! handle_assignment_p (def_stmt))
kono
parents:
diff changeset
686 return;
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 /* Avoid infinite recursion. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
689 if (m_visited_bbs.add (def_bb))
111
kono
parents:
diff changeset
690 return;
kono
parents:
diff changeset
691
kono
parents:
diff changeset
692 int next_path_length = 0;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
693 basic_block last_bb_in_path = m_path.last ();
111
kono
parents:
diff changeset
694
kono
parents:
diff changeset
695 if (loop_containing_stmt (def_stmt)->header == gimple_bb (def_stmt))
kono
parents:
diff changeset
696 {
kono
parents:
diff changeset
697 /* Do not walk through more than one loop PHI node. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
698 if (m_seen_loop_phi)
111
kono
parents:
diff changeset
699 return;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
700 m_seen_loop_phi = true;
111
kono
parents:
diff changeset
701 }
kono
parents:
diff changeset
702
kono
parents:
diff changeset
703 /* Following the chain of SSA_NAME definitions, we jumped from a definition in
kono
parents:
diff changeset
704 LAST_BB_IN_PATH to a definition in DEF_BB. When these basic blocks are
kono
parents:
diff changeset
705 different, append to PATH the blocks from LAST_BB_IN_PATH to DEF_BB. */
kono
parents:
diff changeset
706 if (def_bb != last_bb_in_path)
kono
parents:
diff changeset
707 {
kono
parents:
diff changeset
708 /* When DEF_BB == LAST_BB_IN_PATH, then the first block in the path
kono
parents:
diff changeset
709 will already be in VISITED_BBS. When they are not equal, then we
kono
parents:
diff changeset
710 must ensure that first block is accounted for to ensure we do not
kono
parents:
diff changeset
711 create bogus jump threading paths. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
712 m_visited_bbs.add (m_path[0]);
111
kono
parents:
diff changeset
713 if (!check_subpath_and_update_thread_path (last_bb_in_path, def_bb,
kono
parents:
diff changeset
714 &next_path_length))
kono
parents:
diff changeset
715 return;
kono
parents:
diff changeset
716 }
kono
parents:
diff changeset
717
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
718 gcc_assert (m_path.last () == def_bb);
111
kono
parents:
diff changeset
719
kono
parents:
diff changeset
720 if (gimple_code (def_stmt) == GIMPLE_PHI)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
721 handle_phi (as_a <gphi *> (def_stmt), name, def_bb);
111
kono
parents:
diff changeset
722 else if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
723 handle_assignment (def_stmt, name, def_bb);
111
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 /* Remove all the nodes that we added from NEXT_PATH. */
kono
parents:
diff changeset
726 if (next_path_length)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
727 m_path.truncate (m_path.length () - next_path_length);
111
kono
parents:
diff changeset
728 }
kono
parents:
diff changeset
729
kono
parents:
diff changeset
730 /* Search backwards from BB looking for paths where NAME (an SSA_NAME)
kono
parents:
diff changeset
731 is a constant. Record such paths for jump threading.
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 It is assumed that BB ends with a control statement and that by
kono
parents:
diff changeset
734 finding a path where NAME is a constant, we can thread the path.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
735 SPEED_P indicates that we could increase code size to improve the
111
kono
parents:
diff changeset
736 code path. */
kono
parents:
diff changeset
737
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
738 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
739 thread_jumps::find_jump_threads_backwards (basic_block bb, bool speed_p)
111
kono
parents:
diff changeset
740 {
kono
parents:
diff changeset
741 gimple *stmt = get_gimple_control_stmt (bb);
kono
parents:
diff changeset
742 if (!stmt)
kono
parents:
diff changeset
743 return;
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 enum gimple_code code = gimple_code (stmt);
kono
parents:
diff changeset
746 tree name = NULL;
kono
parents:
diff changeset
747 if (code == GIMPLE_SWITCH)
kono
parents:
diff changeset
748 name = gimple_switch_index (as_a <gswitch *> (stmt));
kono
parents:
diff changeset
749 else if (code == GIMPLE_GOTO)
kono
parents:
diff changeset
750 name = gimple_goto_dest (stmt);
kono
parents:
diff changeset
751 else if (code == GIMPLE_COND)
kono
parents:
diff changeset
752 {
kono
parents:
diff changeset
753 if (TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
kono
parents:
diff changeset
754 && TREE_CODE_CLASS (TREE_CODE (gimple_cond_rhs (stmt))) == tcc_constant
kono
parents:
diff changeset
755 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
kono
parents:
diff changeset
756 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
kono
parents:
diff changeset
757 name = gimple_cond_lhs (stmt);
kono
parents:
diff changeset
758 }
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 if (!name || TREE_CODE (name) != SSA_NAME)
kono
parents:
diff changeset
761 return;
kono
parents:
diff changeset
762
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
763 /* Initialize pass local data that's different for each BB. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
764 m_path.truncate (0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
765 m_path.safe_push (bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
766 m_visited_bbs.empty ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
767 m_seen_loop_phi = false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
768 m_speed_p = speed_p;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
769 m_max_threaded_paths = PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATHS);
111
kono
parents:
diff changeset
770
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
771 fsm_find_control_statement_thread_paths (name);
111
kono
parents:
diff changeset
772 }
kono
parents:
diff changeset
773
kono
parents:
diff changeset
774 namespace {
kono
parents:
diff changeset
775
kono
parents:
diff changeset
776 const pass_data pass_data_thread_jumps =
kono
parents:
diff changeset
777 {
kono
parents:
diff changeset
778 GIMPLE_PASS,
kono
parents:
diff changeset
779 "thread",
kono
parents:
diff changeset
780 OPTGROUP_NONE,
kono
parents:
diff changeset
781 TV_TREE_SSA_THREAD_JUMPS,
kono
parents:
diff changeset
782 ( PROP_cfg | PROP_ssa ),
kono
parents:
diff changeset
783 0,
kono
parents:
diff changeset
784 0,
kono
parents:
diff changeset
785 0,
kono
parents:
diff changeset
786 TODO_update_ssa,
kono
parents:
diff changeset
787 };
kono
parents:
diff changeset
788
kono
parents:
diff changeset
789 class pass_thread_jumps : public gimple_opt_pass
kono
parents:
diff changeset
790 {
kono
parents:
diff changeset
791 public:
kono
parents:
diff changeset
792 pass_thread_jumps (gcc::context *ctxt)
kono
parents:
diff changeset
793 : gimple_opt_pass (pass_data_thread_jumps, ctxt)
kono
parents:
diff changeset
794 {}
kono
parents:
diff changeset
795
kono
parents:
diff changeset
796 opt_pass * clone (void) { return new pass_thread_jumps (m_ctxt); }
kono
parents:
diff changeset
797 virtual bool gate (function *);
kono
parents:
diff changeset
798 virtual unsigned int execute (function *);
kono
parents:
diff changeset
799 };
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 bool
kono
parents:
diff changeset
802 pass_thread_jumps::gate (function *fun ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
803 {
kono
parents:
diff changeset
804 return flag_expensive_optimizations;
kono
parents:
diff changeset
805 }
kono
parents:
diff changeset
806
kono
parents:
diff changeset
807
kono
parents:
diff changeset
808 unsigned int
kono
parents:
diff changeset
809 pass_thread_jumps::execute (function *fun)
kono
parents:
diff changeset
810 {
kono
parents:
diff changeset
811 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 /* Try to thread each block with more than one successor. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
814 thread_jumps threader;
111
kono
parents:
diff changeset
815 basic_block bb;
kono
parents:
diff changeset
816 FOR_EACH_BB_FN (bb, fun)
kono
parents:
diff changeset
817 {
kono
parents:
diff changeset
818 if (EDGE_COUNT (bb->succs) > 1)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
819 threader.find_jump_threads_backwards (bb, true);
111
kono
parents:
diff changeset
820 }
kono
parents:
diff changeset
821 bool changed = thread_through_all_blocks (true);
kono
parents:
diff changeset
822
kono
parents:
diff changeset
823 loop_optimizer_finalize ();
kono
parents:
diff changeset
824 return changed ? TODO_cleanup_cfg : 0;
kono
parents:
diff changeset
825 }
kono
parents:
diff changeset
826
kono
parents:
diff changeset
827 }
kono
parents:
diff changeset
828
kono
parents:
diff changeset
829 gimple_opt_pass *
kono
parents:
diff changeset
830 make_pass_thread_jumps (gcc::context *ctxt)
kono
parents:
diff changeset
831 {
kono
parents:
diff changeset
832 return new pass_thread_jumps (ctxt);
kono
parents:
diff changeset
833 }
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 namespace {
kono
parents:
diff changeset
836
kono
parents:
diff changeset
837 const pass_data pass_data_early_thread_jumps =
kono
parents:
diff changeset
838 {
kono
parents:
diff changeset
839 GIMPLE_PASS,
kono
parents:
diff changeset
840 "ethread",
kono
parents:
diff changeset
841 OPTGROUP_NONE,
kono
parents:
diff changeset
842 TV_TREE_SSA_THREAD_JUMPS,
kono
parents:
diff changeset
843 ( PROP_cfg | PROP_ssa ),
kono
parents:
diff changeset
844 0,
kono
parents:
diff changeset
845 0,
kono
parents:
diff changeset
846 0,
kono
parents:
diff changeset
847 ( TODO_cleanup_cfg | TODO_update_ssa ),
kono
parents:
diff changeset
848 };
kono
parents:
diff changeset
849
kono
parents:
diff changeset
850 class pass_early_thread_jumps : public gimple_opt_pass
kono
parents:
diff changeset
851 {
kono
parents:
diff changeset
852 public:
kono
parents:
diff changeset
853 pass_early_thread_jumps (gcc::context *ctxt)
kono
parents:
diff changeset
854 : gimple_opt_pass (pass_data_early_thread_jumps, ctxt)
kono
parents:
diff changeset
855 {}
kono
parents:
diff changeset
856
kono
parents:
diff changeset
857 opt_pass * clone (void) { return new pass_early_thread_jumps (m_ctxt); }
kono
parents:
diff changeset
858 virtual bool gate (function *);
kono
parents:
diff changeset
859 virtual unsigned int execute (function *);
kono
parents:
diff changeset
860 };
kono
parents:
diff changeset
861
kono
parents:
diff changeset
862 bool
kono
parents:
diff changeset
863 pass_early_thread_jumps::gate (function *fun ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
864 {
kono
parents:
diff changeset
865 return true;
kono
parents:
diff changeset
866 }
kono
parents:
diff changeset
867
kono
parents:
diff changeset
868
kono
parents:
diff changeset
869 unsigned int
kono
parents:
diff changeset
870 pass_early_thread_jumps::execute (function *fun)
kono
parents:
diff changeset
871 {
kono
parents:
diff changeset
872 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
kono
parents:
diff changeset
873
kono
parents:
diff changeset
874 /* Try to thread each block with more than one successor. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
875 thread_jumps threader;
111
kono
parents:
diff changeset
876 basic_block bb;
kono
parents:
diff changeset
877 FOR_EACH_BB_FN (bb, fun)
kono
parents:
diff changeset
878 {
kono
parents:
diff changeset
879 if (EDGE_COUNT (bb->succs) > 1)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
880 threader.find_jump_threads_backwards (bb, false);
111
kono
parents:
diff changeset
881 }
kono
parents:
diff changeset
882 thread_through_all_blocks (true);
kono
parents:
diff changeset
883
kono
parents:
diff changeset
884 loop_optimizer_finalize ();
kono
parents:
diff changeset
885 return 0;
kono
parents:
diff changeset
886 }
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 }
kono
parents:
diff changeset
889
kono
parents:
diff changeset
890 gimple_opt_pass *
kono
parents:
diff changeset
891 make_pass_early_thread_jumps (gcc::context *ctxt)
kono
parents:
diff changeset
892 {
kono
parents:
diff changeset
893 return new pass_early_thread_jumps (ctxt);
kono
parents:
diff changeset
894 }