annotate gcc/tree-ssa-threadbackward.c @ 158:494b0b89df80 default tip

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