annotate gcc/gimple-ssa-split-paths.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Support routines for Splitting Paths to loop backedges
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Ajit Kumar Agarwal <ajitkum@xilinx.com>.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
10 any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
15 GNU General Public License for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #include "config.h"
kono
parents:
diff changeset
22 #include "system.h"
kono
parents:
diff changeset
23 #include "coretypes.h"
kono
parents:
diff changeset
24 #include "backend.h"
kono
parents:
diff changeset
25 #include "tree.h"
kono
parents:
diff changeset
26 #include "gimple.h"
kono
parents:
diff changeset
27 #include "tree-pass.h"
kono
parents:
diff changeset
28 #include "tree-cfg.h"
kono
parents:
diff changeset
29 #include "cfganal.h"
kono
parents:
diff changeset
30 #include "cfgloop.h"
kono
parents:
diff changeset
31 #include "gimple-iterator.h"
kono
parents:
diff changeset
32 #include "tracer.h"
kono
parents:
diff changeset
33 #include "predict.h"
kono
parents:
diff changeset
34 #include "gimple-ssa.h"
kono
parents:
diff changeset
35 #include "tree-phinodes.h"
kono
parents:
diff changeset
36 #include "ssa-iterators.h"
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 /* Given LATCH, the latch block in a loop, see if the shape of the
kono
parents:
diff changeset
39 path reaching LATCH is suitable for being split by duplication.
kono
parents:
diff changeset
40 If so, return the block that will be duplicated into its predecessor
kono
parents:
diff changeset
41 paths. Else return NULL. */
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 static basic_block
kono
parents:
diff changeset
44 find_block_to_duplicate_for_splitting_paths (basic_block latch)
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 /* We should have simple latches at this point. So the latch should
kono
parents:
diff changeset
47 have a single successor. This implies the predecessor of the latch
kono
parents:
diff changeset
48 likely has the loop exit. And it's that predecessor we're most
kono
parents:
diff changeset
49 interested in. To keep things simple, we're going to require that
kono
parents:
diff changeset
50 the latch have a single predecessor too. */
kono
parents:
diff changeset
51 if (single_succ_p (latch) && single_pred_p (latch))
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, latch);
kono
parents:
diff changeset
54 gcc_assert (single_pred_edge (latch)->src == bb);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 /* If BB has been marked as not to be duplicated, then honor that
kono
parents:
diff changeset
57 request. */
kono
parents:
diff changeset
58 if (ignore_bb_p (bb))
kono
parents:
diff changeset
59 return NULL;
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 gimple *last = gsi_stmt (gsi_last_nondebug_bb (bb));
kono
parents:
diff changeset
62 /* The immediate dominator of the latch must end in a conditional. */
kono
parents:
diff changeset
63 if (!last || gimple_code (last) != GIMPLE_COND)
kono
parents:
diff changeset
64 return NULL;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* We're hoping that BB is a join point for an IF-THEN-ELSE diamond
kono
parents:
diff changeset
67 region. Verify that it is.
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 First, verify that BB has two predecessors (each arm of the
kono
parents:
diff changeset
70 IF-THEN-ELSE) and two successors (the latch and exit). */
kono
parents:
diff changeset
71 if (EDGE_COUNT (bb->preds) == 2 && EDGE_COUNT (bb->succs) == 2)
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 /* Now verify that BB's immediate dominator ends in a
kono
parents:
diff changeset
74 conditional as well. */
kono
parents:
diff changeset
75 basic_block bb_idom = get_immediate_dominator (CDI_DOMINATORS, bb);
kono
parents:
diff changeset
76 gimple *last = gsi_stmt (gsi_last_nondebug_bb (bb_idom));
kono
parents:
diff changeset
77 if (!last || gimple_code (last) != GIMPLE_COND)
kono
parents:
diff changeset
78 return NULL;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 /* And that BB's immediate dominator's successors are the
kono
parents:
diff changeset
81 predecessors of BB or BB itself. */
kono
parents:
diff changeset
82 if (!(EDGE_PRED (bb, 0)->src == bb_idom
kono
parents:
diff changeset
83 || find_edge (bb_idom, EDGE_PRED (bb, 0)->src))
kono
parents:
diff changeset
84 || !(EDGE_PRED (bb, 1)->src == bb_idom
kono
parents:
diff changeset
85 || find_edge (bb_idom, EDGE_PRED (bb, 1)->src)))
kono
parents:
diff changeset
86 return NULL;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 /* And that the predecessors of BB each have a single successor
kono
parents:
diff changeset
89 or are BB's immediate domiator itself. */
kono
parents:
diff changeset
90 if (!(EDGE_PRED (bb, 0)->src == bb_idom
kono
parents:
diff changeset
91 || single_succ_p (EDGE_PRED (bb, 0)->src))
kono
parents:
diff changeset
92 || !(EDGE_PRED (bb, 1)->src == bb_idom
kono
parents:
diff changeset
93 || single_succ_p (EDGE_PRED (bb, 1)->src)))
kono
parents:
diff changeset
94 return NULL;
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 /* So at this point we have a simple diamond for an IF-THEN-ELSE
kono
parents:
diff changeset
97 construct starting at BB_IDOM, with a join point at BB. BB
kono
parents:
diff changeset
98 pass control outside the loop or to the loop latch.
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 We're going to want to create two duplicates of BB, one for
kono
parents:
diff changeset
101 each successor of BB_IDOM. */
kono
parents:
diff changeset
102 return bb;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105 return NULL;
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 /* Return the number of non-debug statements in a block. */
kono
parents:
diff changeset
109 static unsigned int
kono
parents:
diff changeset
110 count_stmts_in_block (basic_block bb)
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
113 unsigned int num_stmts = 0;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
118 if (!is_gimple_debug (stmt))
kono
parents:
diff changeset
119 num_stmts++;
kono
parents:
diff changeset
120 }
kono
parents:
diff changeset
121 return num_stmts;
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 /* Return TRUE if CODE represents a tree code that is not likely to
kono
parents:
diff changeset
125 be easily if-convertable because it likely expands into multiple
kono
parents:
diff changeset
126 insns, FALSE otherwise. */
kono
parents:
diff changeset
127 static bool
kono
parents:
diff changeset
128 poor_ifcvt_candidate_code (enum tree_code code)
kono
parents:
diff changeset
129 {
kono
parents:
diff changeset
130 return (code == MIN_EXPR
kono
parents:
diff changeset
131 || code == MAX_EXPR
kono
parents:
diff changeset
132 || code == ABS_EXPR
kono
parents:
diff changeset
133 || code == COND_EXPR
kono
parents:
diff changeset
134 || code == CALL_EXPR);
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 /* Return TRUE if BB is a reasonable block to duplicate by examining
kono
parents:
diff changeset
138 its size, false otherwise. BB will always be a loop latch block.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 Things to consider:
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 We do not want to spoil if-conversion if at all possible.
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 Most of the benefit seems to be from eliminating the unconditional
kono
parents:
diff changeset
145 jump rather than CSE/DCE opportunities. So favor duplicating
kono
parents:
diff changeset
146 small latches. A latch with just a conditional branch is ideal.
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 CSE/DCE opportunties crop up when statements from the predecessors
kono
parents:
diff changeset
149 feed statements in the latch and allow statements in the latch to
kono
parents:
diff changeset
150 simplify. */
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 static bool
kono
parents:
diff changeset
153 is_feasible_trace (basic_block bb)
kono
parents:
diff changeset
154 {
kono
parents:
diff changeset
155 basic_block pred1 = EDGE_PRED (bb, 0)->src;
kono
parents:
diff changeset
156 basic_block pred2 = EDGE_PRED (bb, 1)->src;
kono
parents:
diff changeset
157 int num_stmts_in_join = count_stmts_in_block (bb);
kono
parents:
diff changeset
158 int num_stmts_in_pred1
kono
parents:
diff changeset
159 = EDGE_COUNT (pred1->succs) == 1 ? count_stmts_in_block (pred1) : 0;
kono
parents:
diff changeset
160 int num_stmts_in_pred2
kono
parents:
diff changeset
161 = EDGE_COUNT (pred2->succs) == 1 ? count_stmts_in_block (pred2) : 0;
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 /* This is meant to catch cases that are likely opportunities for
kono
parents:
diff changeset
164 if-conversion. Essentially we look for the case where
kono
parents:
diff changeset
165 BB's predecessors are both single statement blocks where
kono
parents:
diff changeset
166 the output of that statement feed the same PHI in BB. */
kono
parents:
diff changeset
167 if (num_stmts_in_pred1 == 1 && num_stmts_in_pred2 == 1)
kono
parents:
diff changeset
168 {
kono
parents:
diff changeset
169 gimple *stmt1 = last_and_only_stmt (pred1);
kono
parents:
diff changeset
170 gimple *stmt2 = last_and_only_stmt (pred2);
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 if (stmt1 && stmt2
kono
parents:
diff changeset
173 && gimple_code (stmt1) == GIMPLE_ASSIGN
kono
parents:
diff changeset
174 && gimple_code (stmt2) == GIMPLE_ASSIGN)
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 enum tree_code code1 = gimple_assign_rhs_code (stmt1);
kono
parents:
diff changeset
177 enum tree_code code2 = gimple_assign_rhs_code (stmt2);
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 if (!poor_ifcvt_candidate_code (code1)
kono
parents:
diff changeset
180 && !poor_ifcvt_candidate_code (code2))
kono
parents:
diff changeset
181 {
kono
parents:
diff changeset
182 tree lhs1 = gimple_assign_lhs (stmt1);
kono
parents:
diff changeset
183 tree lhs2 = gimple_assign_lhs (stmt2);
kono
parents:
diff changeset
184 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
185 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
186 {
kono
parents:
diff changeset
187 gimple *phi = gsi_stmt (gsi);
kono
parents:
diff changeset
188 if ((gimple_phi_arg_def (phi, 0) == lhs1
kono
parents:
diff changeset
189 && gimple_phi_arg_def (phi, 1) == lhs2)
kono
parents:
diff changeset
190 || (gimple_phi_arg_def (phi, 1) == lhs1
kono
parents:
diff changeset
191 && gimple_phi_arg_def (phi, 0) == lhs2))
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
194 fprintf (dump_file,
kono
parents:
diff changeset
195 "Block %d appears to be a join point for "
kono
parents:
diff changeset
196 "if-convertable diamond.\n",
kono
parents:
diff changeset
197 bb->index);
kono
parents:
diff changeset
198 return false;
kono
parents:
diff changeset
199 }
kono
parents:
diff changeset
200 }
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203 }
kono
parents:
diff changeset
204
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
205 /* Canonicalize the form. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
206 if (num_stmts_in_pred1 == 0 && num_stmts_in_pred2 == 1)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
207 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
208 std::swap (pred1, pred2);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
209 std::swap (num_stmts_in_pred1, num_stmts_in_pred2);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
210 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
211
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
212 /* Another variant. This one is half-diamond. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
213 if (num_stmts_in_pred1 == 1 && num_stmts_in_pred2 == 0
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
214 && dominated_by_p (CDI_DOMINATORS, pred1, pred2))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
215 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
216 gimple *stmt1 = last_and_only_stmt (pred1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
217
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
218 /* The only statement in PRED1 must be an assignment that is
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
219 not a good candidate for if-conversion. This may need some
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
220 generalization. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
221 if (stmt1 && gimple_code (stmt1) == GIMPLE_ASSIGN)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
222 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
223 enum tree_code code1 = gimple_assign_rhs_code (stmt1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
224
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
225 if (!poor_ifcvt_candidate_code (code1))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
226 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
227 tree lhs1 = gimple_assign_lhs (stmt1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
228 tree rhs1 = gimple_assign_rhs1 (stmt1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
229
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
230 gimple_stmt_iterator gsi;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
231 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
232 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
233 gimple *phi = gsi_stmt (gsi);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
234 if ((gimple_phi_arg_def (phi, 0) == lhs1
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
235 && gimple_phi_arg_def (phi, 1) == rhs1)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
236 || (gimple_phi_arg_def (phi, 1) == lhs1
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
237 && gimple_phi_arg_def (phi, 0) == rhs1))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
238 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
239 if (dump_file && (dump_flags & TDF_DETAILS))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
240 fprintf (dump_file,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
241 "Block %d appears to be a join point for "
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
242 "if-convertable half-diamond.\n",
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
243 bb->index);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
244 return false;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
245 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
246 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
247 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
248 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
249 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
250
111
kono
parents:
diff changeset
251 /* If the joiner has no PHIs with useful uses there is zero chance
kono
parents:
diff changeset
252 of CSE/DCE/jump-threading possibilities exposed by duplicating it. */
kono
parents:
diff changeset
253 bool found_useful_phi = false;
kono
parents:
diff changeset
254 for (gphi_iterator si = gsi_start_phis (bb); ! gsi_end_p (si);
kono
parents:
diff changeset
255 gsi_next (&si))
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 gphi *phi = si.phi ();
kono
parents:
diff changeset
258 use_operand_p use_p;
kono
parents:
diff changeset
259 imm_use_iterator iter;
kono
parents:
diff changeset
260 FOR_EACH_IMM_USE_FAST (use_p, iter, gimple_phi_result (phi))
kono
parents:
diff changeset
261 {
kono
parents:
diff changeset
262 gimple *stmt = USE_STMT (use_p);
kono
parents:
diff changeset
263 if (is_gimple_debug (stmt))
kono
parents:
diff changeset
264 continue;
kono
parents:
diff changeset
265 /* If there's a use in the joiner this might be a CSE/DCE
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
266 opportunity, but not if the use is in a conditional
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
267 which makes this a likely if-conversion candidate. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
268 if (gimple_bb (stmt) == bb
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
269 && (!is_gimple_assign (stmt)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
270 || (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
271 != tcc_comparison)))
111
kono
parents:
diff changeset
272 {
kono
parents:
diff changeset
273 found_useful_phi = true;
kono
parents:
diff changeset
274 break;
kono
parents:
diff changeset
275 }
kono
parents:
diff changeset
276 /* If the use is on a loop header PHI and on one path the
kono
parents:
diff changeset
277 value is unchanged this might expose a jump threading
kono
parents:
diff changeset
278 opportunity. */
kono
parents:
diff changeset
279 if (gimple_code (stmt) == GIMPLE_PHI
kono
parents:
diff changeset
280 && gimple_bb (stmt) == bb->loop_father->header
kono
parents:
diff changeset
281 /* But for memory the PHI alone isn't good enough. */
kono
parents:
diff changeset
282 && ! virtual_operand_p (gimple_phi_result (stmt)))
kono
parents:
diff changeset
283 {
kono
parents:
diff changeset
284 bool found_unchanged_path = false;
kono
parents:
diff changeset
285 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
kono
parents:
diff changeset
286 if (gimple_phi_arg_def (phi, i) == gimple_phi_result (stmt))
kono
parents:
diff changeset
287 {
kono
parents:
diff changeset
288 found_unchanged_path = true;
kono
parents:
diff changeset
289 break;
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291 /* If we found an unchanged path this can only be a threading
kono
parents:
diff changeset
292 opportunity if we have uses of the loop header PHI result
kono
parents:
diff changeset
293 in a stmt dominating the merge block. Otherwise the
kono
parents:
diff changeset
294 splitting may prevent if-conversion. */
kono
parents:
diff changeset
295 if (found_unchanged_path)
kono
parents:
diff changeset
296 {
kono
parents:
diff changeset
297 use_operand_p use2_p;
kono
parents:
diff changeset
298 imm_use_iterator iter2;
kono
parents:
diff changeset
299 FOR_EACH_IMM_USE_FAST (use2_p, iter2, gimple_phi_result (stmt))
kono
parents:
diff changeset
300 {
kono
parents:
diff changeset
301 gimple *use_stmt = USE_STMT (use2_p);
kono
parents:
diff changeset
302 if (is_gimple_debug (use_stmt))
kono
parents:
diff changeset
303 continue;
kono
parents:
diff changeset
304 basic_block use_bb = gimple_bb (use_stmt);
kono
parents:
diff changeset
305 if (use_bb != bb
kono
parents:
diff changeset
306 && dominated_by_p (CDI_DOMINATORS, bb, use_bb))
kono
parents:
diff changeset
307 {
kono
parents:
diff changeset
308 if (gcond *cond = dyn_cast <gcond *> (use_stmt))
kono
parents:
diff changeset
309 if (gimple_cond_code (cond) == EQ_EXPR
kono
parents:
diff changeset
310 || gimple_cond_code (cond) == NE_EXPR)
kono
parents:
diff changeset
311 found_useful_phi = true;
kono
parents:
diff changeset
312 break;
kono
parents:
diff changeset
313 }
kono
parents:
diff changeset
314 }
kono
parents:
diff changeset
315 }
kono
parents:
diff changeset
316 if (found_useful_phi)
kono
parents:
diff changeset
317 break;
kono
parents:
diff changeset
318 }
kono
parents:
diff changeset
319 }
kono
parents:
diff changeset
320 if (found_useful_phi)
kono
parents:
diff changeset
321 break;
kono
parents:
diff changeset
322 }
kono
parents:
diff changeset
323 /* There is one exception namely a controlling condition we can propagate
kono
parents:
diff changeset
324 an equivalence from to the joiner. */
kono
parents:
diff changeset
325 bool found_cprop_opportunity = false;
kono
parents:
diff changeset
326 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
kono
parents:
diff changeset
327 gcond *cond = as_a <gcond *> (last_stmt (dom));
kono
parents:
diff changeset
328 if (gimple_cond_code (cond) == EQ_EXPR
kono
parents:
diff changeset
329 || gimple_cond_code (cond) == NE_EXPR)
kono
parents:
diff changeset
330 for (unsigned i = 0; i < 2; ++i)
kono
parents:
diff changeset
331 {
kono
parents:
diff changeset
332 tree op = gimple_op (cond, i);
kono
parents:
diff changeset
333 if (TREE_CODE (op) == SSA_NAME)
kono
parents:
diff changeset
334 {
kono
parents:
diff changeset
335 use_operand_p use_p;
kono
parents:
diff changeset
336 imm_use_iterator iter;
kono
parents:
diff changeset
337 FOR_EACH_IMM_USE_FAST (use_p, iter, op)
kono
parents:
diff changeset
338 {
kono
parents:
diff changeset
339 if (is_gimple_debug (USE_STMT (use_p)))
kono
parents:
diff changeset
340 continue;
kono
parents:
diff changeset
341 if (gimple_bb (USE_STMT (use_p)) == bb)
kono
parents:
diff changeset
342 {
kono
parents:
diff changeset
343 found_cprop_opportunity = true;
kono
parents:
diff changeset
344 break;
kono
parents:
diff changeset
345 }
kono
parents:
diff changeset
346 }
kono
parents:
diff changeset
347 }
kono
parents:
diff changeset
348 if (found_cprop_opportunity)
kono
parents:
diff changeset
349 break;
kono
parents:
diff changeset
350 }
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 if (! found_useful_phi && ! found_cprop_opportunity)
kono
parents:
diff changeset
353 {
kono
parents:
diff changeset
354 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
355 fprintf (dump_file,
kono
parents:
diff changeset
356 "Block %d is a join that does not expose CSE/DCE/jump-thread "
kono
parents:
diff changeset
357 "opportunities when duplicated.\n",
kono
parents:
diff changeset
358 bb->index);
kono
parents:
diff changeset
359 return false;
kono
parents:
diff changeset
360 }
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 /* We may want something here which looks at dataflow and tries
kono
parents:
diff changeset
363 to guess if duplication of BB is likely to result in simplification
kono
parents:
diff changeset
364 of instructions in BB in either the original or the duplicate. */
kono
parents:
diff changeset
365
kono
parents:
diff changeset
366 /* Upper Hard limit on the number statements to copy. */
kono
parents:
diff changeset
367 if (num_stmts_in_join
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
368 >= param_max_jump_thread_duplication_stmts)
111
kono
parents:
diff changeset
369 return false;
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 return true;
kono
parents:
diff changeset
372 }
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 /* If the immediate dominator of the latch of the loop is
kono
parents:
diff changeset
375 block with conditional branch, then the loop latch is
kono
parents:
diff changeset
376 duplicated to its predecessors path preserving the SSA
kono
parents:
diff changeset
377 semantics.
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 CFG before transformation.
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 2
kono
parents:
diff changeset
382 |
kono
parents:
diff changeset
383 |
kono
parents:
diff changeset
384 +---->3
kono
parents:
diff changeset
385 | / \
kono
parents:
diff changeset
386 | / \
kono
parents:
diff changeset
387 | 4 5
kono
parents:
diff changeset
388 | \ /
kono
parents:
diff changeset
389 | \ /
kono
parents:
diff changeset
390 | 6
kono
parents:
diff changeset
391 | / \
kono
parents:
diff changeset
392 | / \
kono
parents:
diff changeset
393 | 8 7
kono
parents:
diff changeset
394 | | |
kono
parents:
diff changeset
395 ---+ E
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 Block 8 is the latch. We're going to make copies of block 6 (9 & 10)
kono
parents:
diff changeset
400 and wire things up so they look like this:
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 2
kono
parents:
diff changeset
403 |
kono
parents:
diff changeset
404 |
kono
parents:
diff changeset
405 +---->3
kono
parents:
diff changeset
406 | / \
kono
parents:
diff changeset
407 | / \
kono
parents:
diff changeset
408 | 4 5
kono
parents:
diff changeset
409 | | |
kono
parents:
diff changeset
410 | | |
kono
parents:
diff changeset
411 | 9 10
kono
parents:
diff changeset
412 | |\ /|
kono
parents:
diff changeset
413 | | \ / |
kono
parents:
diff changeset
414 | | 7 |
kono
parents:
diff changeset
415 | | | |
kono
parents:
diff changeset
416 | | E |
kono
parents:
diff changeset
417 | | |
kono
parents:
diff changeset
418 | \ /
kono
parents:
diff changeset
419 | \ /
kono
parents:
diff changeset
420 +-----8
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 Blocks 9 and 10 will get merged into blocks 4 & 5 respectively which
kono
parents:
diff changeset
424 enables CSE, DCE and other optimizations to occur on a larger block
kono
parents:
diff changeset
425 of code. */
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 static bool
kono
parents:
diff changeset
428 split_paths ()
kono
parents:
diff changeset
429 {
kono
parents:
diff changeset
430 bool changed = false;
kono
parents:
diff changeset
431 loop_p loop;
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
kono
parents:
diff changeset
434 initialize_original_copy_tables ();
kono
parents:
diff changeset
435 calculate_dominance_info (CDI_DOMINATORS);
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
kono
parents:
diff changeset
438 {
kono
parents:
diff changeset
439 /* Only split paths if we are optimizing this loop for speed. */
kono
parents:
diff changeset
440 if (!optimize_loop_for_speed_p (loop))
kono
parents:
diff changeset
441 continue;
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 /* See if there is a block that we can duplicate to split the
kono
parents:
diff changeset
444 path to the loop latch. */
kono
parents:
diff changeset
445 basic_block bb
kono
parents:
diff changeset
446 = find_block_to_duplicate_for_splitting_paths (loop->latch);
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 /* BB is the merge point for an IF-THEN-ELSE we want to transform.
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 Essentially we want to create a duplicate of bb and redirect the
kono
parents:
diff changeset
451 first predecessor of BB to the duplicate (leaving the second
kono
parents:
diff changeset
452 predecessor as is. This will split the path leading to the latch
kono
parents:
diff changeset
453 re-using BB to avoid useless copying. */
kono
parents:
diff changeset
454 if (bb && is_feasible_trace (bb))
kono
parents:
diff changeset
455 {
kono
parents:
diff changeset
456 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
457 fprintf (dump_file,
kono
parents:
diff changeset
458 "Duplicating join block %d into predecessor paths\n",
kono
parents:
diff changeset
459 bb->index);
kono
parents:
diff changeset
460 basic_block pred0 = EDGE_PRED (bb, 0)->src;
kono
parents:
diff changeset
461 if (EDGE_COUNT (pred0->succs) != 1)
kono
parents:
diff changeset
462 pred0 = EDGE_PRED (bb, 1)->src;
kono
parents:
diff changeset
463 transform_duplicate (pred0, bb);
kono
parents:
diff changeset
464 changed = true;
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 /* If BB has an outgoing edge marked as IRREDUCIBLE, then
kono
parents:
diff changeset
467 duplicating BB may result in an irreducible region turning
kono
parents:
diff changeset
468 into a natural loop.
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 Long term we might want to hook this into the block
kono
parents:
diff changeset
471 duplication code, but as we've seen with similar changes
kono
parents:
diff changeset
472 for edge removal, that can be somewhat risky. */
kono
parents:
diff changeset
473 if (EDGE_SUCC (bb, 0)->flags & EDGE_IRREDUCIBLE_LOOP
kono
parents:
diff changeset
474 || EDGE_SUCC (bb, 1)->flags & EDGE_IRREDUCIBLE_LOOP)
kono
parents:
diff changeset
475 {
kono
parents:
diff changeset
476 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
477 fprintf (dump_file,
kono
parents:
diff changeset
478 "Join block %d has EDGE_IRREDUCIBLE_LOOP set. "
kono
parents:
diff changeset
479 "Scheduling loop fixups.\n",
kono
parents:
diff changeset
480 bb->index);
kono
parents:
diff changeset
481 loops_state_set (LOOPS_NEED_FIXUP);
kono
parents:
diff changeset
482 }
kono
parents:
diff changeset
483 }
kono
parents:
diff changeset
484 }
kono
parents:
diff changeset
485
kono
parents:
diff changeset
486 loop_optimizer_finalize ();
kono
parents:
diff changeset
487 free_original_copy_tables ();
kono
parents:
diff changeset
488 return changed;
kono
parents:
diff changeset
489 }
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 /* Main entry point for splitting paths. Returns TODO_cleanup_cfg if any
kono
parents:
diff changeset
492 paths where split, otherwise return zero. */
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 static unsigned int
kono
parents:
diff changeset
495 execute_split_paths ()
kono
parents:
diff changeset
496 {
kono
parents:
diff changeset
497 /* If we don't have at least 2 real blocks and backedges in the
kono
parents:
diff changeset
498 CFG, then there's no point in trying to perform path splitting. */
kono
parents:
diff changeset
499 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
kono
parents:
diff changeset
500 || !mark_dfs_back_edges ())
kono
parents:
diff changeset
501 return 0;
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 bool changed = split_paths();
kono
parents:
diff changeset
504 if (changed)
kono
parents:
diff changeset
505 free_dominance_info (CDI_DOMINATORS);
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 return changed ? TODO_cleanup_cfg : 0;
kono
parents:
diff changeset
508 }
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 static bool
kono
parents:
diff changeset
511 gate_split_paths ()
kono
parents:
diff changeset
512 {
kono
parents:
diff changeset
513 return flag_split_paths;
kono
parents:
diff changeset
514 }
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 namespace {
kono
parents:
diff changeset
517
kono
parents:
diff changeset
518 const pass_data pass_data_split_paths =
kono
parents:
diff changeset
519 {
kono
parents:
diff changeset
520 GIMPLE_PASS, /* type */
kono
parents:
diff changeset
521 "split-paths", /* name */
kono
parents:
diff changeset
522 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
523 TV_SPLIT_PATHS, /* tv_id */
kono
parents:
diff changeset
524 PROP_ssa, /* properties_required */
kono
parents:
diff changeset
525 0, /* properties_provided */
kono
parents:
diff changeset
526 0, /* properties_destroyed */
kono
parents:
diff changeset
527 0, /* todo_flags_start */
kono
parents:
diff changeset
528 TODO_update_ssa, /* todo_flags_finish */
kono
parents:
diff changeset
529 };
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 class pass_split_paths : public gimple_opt_pass
kono
parents:
diff changeset
532 {
kono
parents:
diff changeset
533 public:
kono
parents:
diff changeset
534 pass_split_paths (gcc::context *ctxt)
kono
parents:
diff changeset
535 : gimple_opt_pass (pass_data_split_paths, ctxt)
kono
parents:
diff changeset
536 {}
kono
parents:
diff changeset
537 /* opt_pass methods: */
kono
parents:
diff changeset
538 opt_pass * clone () { return new pass_split_paths (m_ctxt); }
kono
parents:
diff changeset
539 virtual bool gate (function *) { return gate_split_paths (); }
kono
parents:
diff changeset
540 virtual unsigned int execute (function *) { return execute_split_paths (); }
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 }; // class pass_split_paths
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 } // anon namespace
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 gimple_opt_pass *
kono
parents:
diff changeset
547 make_pass_split_paths (gcc::context *ctxt)
kono
parents:
diff changeset
548 {
kono
parents:
diff changeset
549 return new pass_split_paths (ctxt);
kono
parents:
diff changeset
550 }