annotate gcc/tree-ssa-phionlycprop.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Const/Copy propagation originating from degenerate PHIs
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "backend.h"
kono
parents:
diff changeset
24 #include "cfghooks.h"
kono
parents:
diff changeset
25 #include "tree.h"
kono
parents:
diff changeset
26 #include "gimple.h"
kono
parents:
diff changeset
27 #include "ssa.h"
kono
parents:
diff changeset
28 #include "fold-const.h"
kono
parents:
diff changeset
29 #include "cfgloop.h"
kono
parents:
diff changeset
30 #include "gimple-pretty-print.h"
kono
parents:
diff changeset
31 #include "gimple-fold.h"
kono
parents:
diff changeset
32 #include "tree-eh.h"
kono
parents:
diff changeset
33 #include "gimple-iterator.h"
kono
parents:
diff changeset
34 #include "tree-cfg.h"
kono
parents:
diff changeset
35 #include "tree-pass.h"
kono
parents:
diff changeset
36 #include "tree-ssa-propagate.h"
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
kono
parents:
diff changeset
40 up degenerate PHIs created by or exposed by jump threading. */
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 /* Given a statement STMT, which is either a PHI node or an assignment,
kono
parents:
diff changeset
43 remove it from the IL. */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 static void
kono
parents:
diff changeset
46 remove_stmt_or_phi (gimple *stmt)
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 if (gimple_code (stmt) == GIMPLE_PHI)
kono
parents:
diff changeset
51 remove_phi_node (&gsi, true);
kono
parents:
diff changeset
52 else
kono
parents:
diff changeset
53 {
kono
parents:
diff changeset
54 gsi_remove (&gsi, true);
kono
parents:
diff changeset
55 release_defs (stmt);
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Given a statement STMT, which is either a PHI node or an assignment,
kono
parents:
diff changeset
60 return the "rhs" of the node, in the case of a non-degenerate
kono
parents:
diff changeset
61 phi, NULL is returned. */
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 static tree
kono
parents:
diff changeset
64 get_rhs_or_phi_arg (gimple *stmt)
kono
parents:
diff changeset
65 {
kono
parents:
diff changeset
66 if (gimple_code (stmt) == GIMPLE_PHI)
kono
parents:
diff changeset
67 return degenerate_phi_result (as_a <gphi *> (stmt));
kono
parents:
diff changeset
68 else if (gimple_assign_single_p (stmt))
kono
parents:
diff changeset
69 return gimple_assign_rhs1 (stmt);
kono
parents:
diff changeset
70 else
kono
parents:
diff changeset
71 gcc_unreachable ();
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 /* Given a statement STMT, which is either a PHI node or an assignment,
kono
parents:
diff changeset
76 return the "lhs" of the node. */
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 static tree
kono
parents:
diff changeset
79 get_lhs_or_phi_result (gimple *stmt)
kono
parents:
diff changeset
80 {
kono
parents:
diff changeset
81 if (gimple_code (stmt) == GIMPLE_PHI)
kono
parents:
diff changeset
82 return gimple_phi_result (stmt);
kono
parents:
diff changeset
83 else if (is_gimple_assign (stmt))
kono
parents:
diff changeset
84 return gimple_assign_lhs (stmt);
kono
parents:
diff changeset
85 else
kono
parents:
diff changeset
86 gcc_unreachable ();
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 /* Propagate RHS into all uses of LHS (when possible).
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 RHS and LHS are derived from STMT, which is passed in solely so
kono
parents:
diff changeset
92 that we can remove it if propagation is successful.
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 When propagating into a PHI node or into a statement which turns
kono
parents:
diff changeset
95 into a trivial copy or constant initialization, set the
kono
parents:
diff changeset
96 appropriate bit in INTERESTING_NAMEs so that we will visit those
kono
parents:
diff changeset
97 nodes as well in an effort to pick up secondary optimization
kono
parents:
diff changeset
98 opportunities.
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 NEED_EH_CLEANUP tracks blocks that need their EH information
kono
parents:
diff changeset
101 cleaned up after changing EH information on a statement. */
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 static bool
kono
parents:
diff changeset
104 propagate_rhs_into_lhs (gimple *stmt, tree lhs, tree rhs,
kono
parents:
diff changeset
105 bitmap interesting_names, bitmap need_eh_cleanup)
kono
parents:
diff changeset
106 {
kono
parents:
diff changeset
107 bool cfg_altered = false;
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 /* First verify that propagation is valid. */
kono
parents:
diff changeset
110 if (may_propagate_copy (lhs, rhs))
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 use_operand_p use_p;
kono
parents:
diff changeset
113 imm_use_iterator iter;
kono
parents:
diff changeset
114 gimple *use_stmt;
kono
parents:
diff changeset
115 bool all = true;
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 /* Dump details. */
kono
parents:
diff changeset
118 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
119 {
kono
parents:
diff changeset
120 fprintf (dump_file, " Replacing '");
kono
parents:
diff changeset
121 print_generic_expr (dump_file, lhs, dump_flags);
kono
parents:
diff changeset
122 fprintf (dump_file, "' with %s '",
kono
parents:
diff changeset
123 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
kono
parents:
diff changeset
124 print_generic_expr (dump_file, rhs, dump_flags);
kono
parents:
diff changeset
125 fprintf (dump_file, "'\n");
kono
parents:
diff changeset
126 }
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 /* Walk over every use of LHS and try to replace the use with RHS.
kono
parents:
diff changeset
129 At this point the only reason why such a propagation would not
kono
parents:
diff changeset
130 be successful would be if the use occurs in an ASM_EXPR. */
kono
parents:
diff changeset
131 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
kono
parents:
diff changeset
132 {
kono
parents:
diff changeset
133 /* Leave debug stmts alone. If we succeed in propagating
kono
parents:
diff changeset
134 all non-debug uses, we'll drop the DEF, and propagation
kono
parents:
diff changeset
135 into debug stmts will occur then. */
kono
parents:
diff changeset
136 if (gimple_debug_bind_p (use_stmt))
kono
parents:
diff changeset
137 continue;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 /* It's not always safe to propagate into an ASM_EXPR. */
kono
parents:
diff changeset
140 if (gimple_code (use_stmt) == GIMPLE_ASM
kono
parents:
diff changeset
141 && ! may_propagate_copy_into_asm (lhs))
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 all = false;
kono
parents:
diff changeset
144 continue;
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 /* It's not ok to propagate into the definition stmt of RHS.
kono
parents:
diff changeset
148 <bb 9>:
kono
parents:
diff changeset
149 # prephitmp.12_36 = PHI <g_67.1_6(9)>
kono
parents:
diff changeset
150 g_67.1_6 = prephitmp.12_36;
kono
parents:
diff changeset
151 goto <bb 9>;
kono
parents:
diff changeset
152 While this is strictly all dead code we do not want to
kono
parents:
diff changeset
153 deal with this here. */
kono
parents:
diff changeset
154 if (TREE_CODE (rhs) == SSA_NAME
kono
parents:
diff changeset
155 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 all = false;
kono
parents:
diff changeset
158 continue;
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 /* Dump details. */
kono
parents:
diff changeset
162 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
163 {
kono
parents:
diff changeset
164 fprintf (dump_file, " Original statement:");
kono
parents:
diff changeset
165 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
kono
parents:
diff changeset
166 }
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 /* Propagate the RHS into this use of the LHS. */
kono
parents:
diff changeset
169 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
kono
parents:
diff changeset
170 propagate_value (use_p, rhs);
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /* Special cases to avoid useless calls into the folding
kono
parents:
diff changeset
173 routines, operand scanning, etc.
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 Propagation into a PHI may cause the PHI to become
kono
parents:
diff changeset
176 a degenerate, so mark the PHI as interesting. No other
kono
parents:
diff changeset
177 actions are necessary. */
kono
parents:
diff changeset
178 if (gimple_code (use_stmt) == GIMPLE_PHI)
kono
parents:
diff changeset
179 {
kono
parents:
diff changeset
180 tree result;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 /* Dump details. */
kono
parents:
diff changeset
183 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
184 {
kono
parents:
diff changeset
185 fprintf (dump_file, " Updated statement:");
kono
parents:
diff changeset
186 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 result = get_lhs_or_phi_result (use_stmt);
kono
parents:
diff changeset
190 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
kono
parents:
diff changeset
191 continue;
kono
parents:
diff changeset
192 }
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 /* From this point onward we are propagating into a
kono
parents:
diff changeset
195 real statement. Folding may (or may not) be possible,
kono
parents:
diff changeset
196 we may expose new operands, expose dead EH edges,
kono
parents:
diff changeset
197 etc. */
kono
parents:
diff changeset
198 /* NOTE tuples. In the tuples world, fold_stmt_inplace
kono
parents:
diff changeset
199 cannot fold a call that simplifies to a constant,
kono
parents:
diff changeset
200 because the GIMPLE_CALL must be replaced by a
kono
parents:
diff changeset
201 GIMPLE_ASSIGN, and there is no way to effect such a
kono
parents:
diff changeset
202 transformation in-place. We might want to consider
kono
parents:
diff changeset
203 using the more general fold_stmt here. */
kono
parents:
diff changeset
204 {
kono
parents:
diff changeset
205 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
kono
parents:
diff changeset
206 fold_stmt_inplace (&gsi);
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* Sometimes propagation can expose new operands to the
kono
parents:
diff changeset
210 renamer. */
kono
parents:
diff changeset
211 update_stmt (use_stmt);
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 /* Dump details. */
kono
parents:
diff changeset
214 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
215 {
kono
parents:
diff changeset
216 fprintf (dump_file, " Updated statement:");
kono
parents:
diff changeset
217 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
kono
parents:
diff changeset
218 }
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 /* If we replaced a variable index with a constant, then
kono
parents:
diff changeset
221 we would need to update the invariant flag for ADDR_EXPRs. */
kono
parents:
diff changeset
222 if (gimple_assign_single_p (use_stmt)
kono
parents:
diff changeset
223 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
kono
parents:
diff changeset
224 recompute_tree_invariant_for_addr_expr
kono
parents:
diff changeset
225 (gimple_assign_rhs1 (use_stmt));
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 /* If we cleaned up EH information from the statement,
kono
parents:
diff changeset
228 mark its containing block as needing EH cleanups. */
kono
parents:
diff changeset
229 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
kono
parents:
diff changeset
230 {
kono
parents:
diff changeset
231 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
kono
parents:
diff changeset
232 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
233 fprintf (dump_file, " Flagged to clear EH edges.\n");
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 /* Propagation may expose new trivial copy/constant propagation
kono
parents:
diff changeset
237 opportunities. */
kono
parents:
diff changeset
238 if (gimple_assign_single_p (use_stmt)
kono
parents:
diff changeset
239 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
kono
parents:
diff changeset
240 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
kono
parents:
diff changeset
241 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
kono
parents:
diff changeset
242 {
kono
parents:
diff changeset
243 tree result = get_lhs_or_phi_result (use_stmt);
kono
parents:
diff changeset
244 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
kono
parents:
diff changeset
245 }
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 /* Propagation into these nodes may make certain edges in
kono
parents:
diff changeset
248 the CFG unexecutable. We want to identify them as PHI nodes
kono
parents:
diff changeset
249 at the destination of those unexecutable edges may become
kono
parents:
diff changeset
250 degenerates. */
kono
parents:
diff changeset
251 else if (gimple_code (use_stmt) == GIMPLE_COND
kono
parents:
diff changeset
252 || gimple_code (use_stmt) == GIMPLE_SWITCH
kono
parents:
diff changeset
253 || gimple_code (use_stmt) == GIMPLE_GOTO)
kono
parents:
diff changeset
254 {
kono
parents:
diff changeset
255 tree val;
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 if (gimple_code (use_stmt) == GIMPLE_COND)
kono
parents:
diff changeset
258 val = fold_binary_loc (gimple_location (use_stmt),
kono
parents:
diff changeset
259 gimple_cond_code (use_stmt),
kono
parents:
diff changeset
260 boolean_type_node,
kono
parents:
diff changeset
261 gimple_cond_lhs (use_stmt),
kono
parents:
diff changeset
262 gimple_cond_rhs (use_stmt));
kono
parents:
diff changeset
263 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
kono
parents:
diff changeset
264 val = gimple_switch_index (as_a <gswitch *> (use_stmt));
kono
parents:
diff changeset
265 else
kono
parents:
diff changeset
266 val = gimple_goto_dest (use_stmt);
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 if (val && is_gimple_min_invariant (val))
kono
parents:
diff changeset
269 {
kono
parents:
diff changeset
270 basic_block bb = gimple_bb (use_stmt);
kono
parents:
diff changeset
271 edge te = find_taken_edge (bb, val);
kono
parents:
diff changeset
272 if (!te)
kono
parents:
diff changeset
273 continue;
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 edge_iterator ei;
kono
parents:
diff changeset
276 edge e;
kono
parents:
diff changeset
277 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
278 gphi_iterator psi;
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 /* Remove all outgoing edges except TE. */
kono
parents:
diff changeset
281 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
kono
parents:
diff changeset
282 {
kono
parents:
diff changeset
283 if (e != te)
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 /* Mark all the PHI nodes at the destination of
kono
parents:
diff changeset
286 the unexecutable edge as interesting. */
kono
parents:
diff changeset
287 for (psi = gsi_start_phis (e->dest);
kono
parents:
diff changeset
288 !gsi_end_p (psi);
kono
parents:
diff changeset
289 gsi_next (&psi))
kono
parents:
diff changeset
290 {
kono
parents:
diff changeset
291 gphi *phi = psi.phi ();
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 tree result = gimple_phi_result (phi);
kono
parents:
diff changeset
294 int version = SSA_NAME_VERSION (result);
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 bitmap_set_bit (interesting_names, version);
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 te->probability += e->probability;
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 remove_edge (e);
kono
parents:
diff changeset
302 cfg_altered = true;
kono
parents:
diff changeset
303 }
kono
parents:
diff changeset
304 else
kono
parents:
diff changeset
305 ei_next (&ei);
kono
parents:
diff changeset
306 }
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 gsi = gsi_last_bb (gimple_bb (use_stmt));
kono
parents:
diff changeset
309 gsi_remove (&gsi, true);
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 /* And fixup the flags on the single remaining edge. */
kono
parents:
diff changeset
312 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
kono
parents:
diff changeset
313 te->flags &= ~EDGE_ABNORMAL;
kono
parents:
diff changeset
314 te->flags |= EDGE_FALLTHRU;
kono
parents:
diff changeset
315 }
kono
parents:
diff changeset
316 }
kono
parents:
diff changeset
317 }
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 /* Ensure there is nothing else to do. */
kono
parents:
diff changeset
320 gcc_assert (!all || has_zero_uses (lhs));
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 /* If we were able to propagate away all uses of LHS, then
kono
parents:
diff changeset
323 we can remove STMT. */
kono
parents:
diff changeset
324 if (all)
kono
parents:
diff changeset
325 remove_stmt_or_phi (stmt);
kono
parents:
diff changeset
326 }
kono
parents:
diff changeset
327 return cfg_altered;
kono
parents:
diff changeset
328 }
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 /* STMT is either a PHI node (potentially a degenerate PHI node) or
kono
parents:
diff changeset
331 a statement that is a trivial copy or constant initialization.
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 Attempt to eliminate STMT by propagating its RHS into all uses of
kono
parents:
diff changeset
334 its LHS. This may in turn set new bits in INTERESTING_NAMES
kono
parents:
diff changeset
335 for nodes we want to revisit later.
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 All exit paths should clear INTERESTING_NAMES for the result
kono
parents:
diff changeset
338 of STMT.
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 NEED_EH_CLEANUP tracks blocks that need their EH information
kono
parents:
diff changeset
341 cleaned up after changing EH information on a statement. It is
kono
parents:
diff changeset
342 not set or queried here, but passed along to children. */
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 static bool
kono
parents:
diff changeset
345 eliminate_const_or_copy (gimple *stmt, bitmap interesting_names,
kono
parents:
diff changeset
346 bitmap need_eh_cleanup)
kono
parents:
diff changeset
347 {
kono
parents:
diff changeset
348 tree lhs = get_lhs_or_phi_result (stmt);
kono
parents:
diff changeset
349 tree rhs;
kono
parents:
diff changeset
350 int version = SSA_NAME_VERSION (lhs);
kono
parents:
diff changeset
351 bool cfg_altered = false;
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 /* If the LHS of this statement or PHI has no uses, then we can
kono
parents:
diff changeset
354 just eliminate it. This can occur if, for example, the PHI
kono
parents:
diff changeset
355 was created by block duplication due to threading and its only
kono
parents:
diff changeset
356 use was in the conditional at the end of the block which was
kono
parents:
diff changeset
357 deleted. */
kono
parents:
diff changeset
358 if (has_zero_uses (lhs))
kono
parents:
diff changeset
359 {
kono
parents:
diff changeset
360 bitmap_clear_bit (interesting_names, version);
kono
parents:
diff changeset
361 remove_stmt_or_phi (stmt);
kono
parents:
diff changeset
362 return cfg_altered;
kono
parents:
diff changeset
363 }
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 /* Get the RHS of the assignment or PHI node if the PHI is a
kono
parents:
diff changeset
366 degenerate. */
kono
parents:
diff changeset
367 rhs = get_rhs_or_phi_arg (stmt);
kono
parents:
diff changeset
368 if (!rhs)
kono
parents:
diff changeset
369 {
kono
parents:
diff changeset
370 bitmap_clear_bit (interesting_names, version);
kono
parents:
diff changeset
371 return cfg_altered;
kono
parents:
diff changeset
372 }
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 if (!virtual_operand_p (lhs))
kono
parents:
diff changeset
375 cfg_altered = propagate_rhs_into_lhs (stmt, lhs, rhs,
kono
parents:
diff changeset
376 interesting_names, need_eh_cleanup);
kono
parents:
diff changeset
377 else
kono
parents:
diff changeset
378 {
kono
parents:
diff changeset
379 gimple *use_stmt;
kono
parents:
diff changeset
380 imm_use_iterator iter;
kono
parents:
diff changeset
381 use_operand_p use_p;
kono
parents:
diff changeset
382 /* For virtual operands we have to propagate into all uses as
kono
parents:
diff changeset
383 otherwise we will create overlapping life-ranges. */
kono
parents:
diff changeset
384 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
kono
parents:
diff changeset
385 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
kono
parents:
diff changeset
386 SET_USE (use_p, rhs);
kono
parents:
diff changeset
387 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
kono
parents:
diff changeset
388 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
kono
parents:
diff changeset
389 remove_stmt_or_phi (stmt);
kono
parents:
diff changeset
390 }
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 /* Note that STMT may well have been deleted by now, so do
kono
parents:
diff changeset
393 not access it, instead use the saved version # to clear
kono
parents:
diff changeset
394 T's entry in the worklist. */
kono
parents:
diff changeset
395 bitmap_clear_bit (interesting_names, version);
kono
parents:
diff changeset
396 return cfg_altered;
kono
parents:
diff changeset
397 }
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 /* The first phase in degenerate PHI elimination.
kono
parents:
diff changeset
400
kono
parents:
diff changeset
401 Eliminate the degenerate PHIs in BB, then recurse on the
kono
parents:
diff changeset
402 dominator children of BB.
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 INTERESTING_NAMES tracks SSA_NAMEs that we may want to revisit
kono
parents:
diff changeset
405 in the future. It is not set or queried here, but passed along
kono
parents:
diff changeset
406 to children.
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 NEED_EH_CLEANUP tracks blocks that need their EH information
kono
parents:
diff changeset
409 cleaned up after changing EH information on a statement. It is
kono
parents:
diff changeset
410 not set or queried here, but passed along to children. */
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 static bool
kono
parents:
diff changeset
413 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names,
kono
parents:
diff changeset
414 bitmap need_eh_cleanup)
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 gphi_iterator gsi;
kono
parents:
diff changeset
417 basic_block son;
kono
parents:
diff changeset
418 bool cfg_altered = false;
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);)
kono
parents:
diff changeset
421 {
kono
parents:
diff changeset
422 gphi *phi = gsi.phi ();
kono
parents:
diff changeset
423 /* We might end up removing PHI so advance the iterator now. */
kono
parents:
diff changeset
424 gsi_next (&gsi);
kono
parents:
diff changeset
425 cfg_altered |= eliminate_const_or_copy (phi, interesting_names,
kono
parents:
diff changeset
426 need_eh_cleanup);
kono
parents:
diff changeset
427 }
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 /* Recurse into the dominator children of BB. */
kono
parents:
diff changeset
430 for (son = first_dom_son (CDI_DOMINATORS, bb);
kono
parents:
diff changeset
431 son;
kono
parents:
diff changeset
432 son = next_dom_son (CDI_DOMINATORS, son))
kono
parents:
diff changeset
433 cfg_altered |= eliminate_degenerate_phis_1 (son, interesting_names,
kono
parents:
diff changeset
434 need_eh_cleanup);
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 return cfg_altered;
kono
parents:
diff changeset
437 }
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439
kono
parents:
diff changeset
440 /* A very simple pass to eliminate degenerate PHI nodes from the
kono
parents:
diff changeset
441 IL. This is meant to be fast enough to be able to be run several
kono
parents:
diff changeset
442 times in the optimization pipeline.
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 Certain optimizations, particularly those which duplicate blocks
kono
parents:
diff changeset
445 or remove edges from the CFG can create or expose PHIs which are
kono
parents:
diff changeset
446 trivial copies or constant initializations.
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 While we could pick up these optimizations in DOM or with the
kono
parents:
diff changeset
449 combination of copy-prop and CCP, those solutions are far too
kono
parents:
diff changeset
450 heavy-weight for our needs.
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 This implementation has two phases so that we can efficiently
kono
parents:
diff changeset
453 eliminate the first order degenerate PHIs and second order
kono
parents:
diff changeset
454 degenerate PHIs.
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 The first phase performs a dominator walk to identify and eliminate
kono
parents:
diff changeset
457 the vast majority of the degenerate PHIs. When a degenerate PHI
kono
parents:
diff changeset
458 is identified and eliminated any affected statements or PHIs
kono
parents:
diff changeset
459 are put on a worklist.
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 The second phase eliminates degenerate PHIs and trivial copies
kono
parents:
diff changeset
462 or constant initializations using the worklist. This is how we
kono
parents:
diff changeset
463 pick up the secondary optimization opportunities with minimal
kono
parents:
diff changeset
464 cost. */
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 namespace {
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 const pass_data pass_data_phi_only_cprop =
kono
parents:
diff changeset
469 {
kono
parents:
diff changeset
470 GIMPLE_PASS, /* type */
kono
parents:
diff changeset
471 "phicprop", /* name */
kono
parents:
diff changeset
472 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
473 TV_TREE_PHI_CPROP, /* tv_id */
kono
parents:
diff changeset
474 ( PROP_cfg | PROP_ssa ), /* properties_required */
kono
parents:
diff changeset
475 0, /* properties_provided */
kono
parents:
diff changeset
476 0, /* properties_destroyed */
kono
parents:
diff changeset
477 0, /* todo_flags_start */
kono
parents:
diff changeset
478 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
kono
parents:
diff changeset
479 };
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 class pass_phi_only_cprop : public gimple_opt_pass
kono
parents:
diff changeset
482 {
kono
parents:
diff changeset
483 public:
kono
parents:
diff changeset
484 pass_phi_only_cprop (gcc::context *ctxt)
kono
parents:
diff changeset
485 : gimple_opt_pass (pass_data_phi_only_cprop, ctxt)
kono
parents:
diff changeset
486 {}
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 /* opt_pass methods: */
kono
parents:
diff changeset
489 opt_pass * clone () { return new pass_phi_only_cprop (m_ctxt); }
kono
parents:
diff changeset
490 virtual bool gate (function *) { return flag_tree_dom != 0; }
kono
parents:
diff changeset
491 virtual unsigned int execute (function *);
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 }; // class pass_phi_only_cprop
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 unsigned int
kono
parents:
diff changeset
496 pass_phi_only_cprop::execute (function *fun)
kono
parents:
diff changeset
497 {
kono
parents:
diff changeset
498 bool cfg_altered = false;
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 /* Bitmap of blocks which need EH information updated. We can not
kono
parents:
diff changeset
501 update it on-the-fly as doing so invalidates the dominator tree. */
kono
parents:
diff changeset
502 auto_bitmap need_eh_cleanup;
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 /* INTERESTING_NAMES is effectively our worklist, indexed by
kono
parents:
diff changeset
505 SSA_NAME_VERSION.
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 A set bit indicates that the statement or PHI node which
kono
parents:
diff changeset
508 defines the SSA_NAME should be (re)examined to determine if
kono
parents:
diff changeset
509 it has become a degenerate PHI or trivial const/copy propagation
kono
parents:
diff changeset
510 opportunity.
kono
parents:
diff changeset
511
kono
parents:
diff changeset
512 Experiments have show we generally get better compilation
kono
parents:
diff changeset
513 time behavior with bitmaps rather than sbitmaps. */
kono
parents:
diff changeset
514 auto_bitmap interesting_names;
kono
parents:
diff changeset
515 auto_bitmap interesting_names1;
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 calculate_dominance_info (CDI_DOMINATORS);
kono
parents:
diff changeset
518 cfg_altered = false;
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 /* First phase. Eliminate degenerate PHIs via a dominator
kono
parents:
diff changeset
521 walk of the CFG.
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 Experiments have indicated that we generally get better
kono
parents:
diff changeset
524 compile-time behavior by visiting blocks in the first
kono
parents:
diff changeset
525 phase in dominator order. Presumably this is because walking
kono
parents:
diff changeset
526 in dominator order leaves fewer PHIs for later examination
kono
parents:
diff changeset
527 by the worklist phase. */
kono
parents:
diff changeset
528 cfg_altered = eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR_FOR_FN (fun),
kono
parents:
diff changeset
529 interesting_names,
kono
parents:
diff changeset
530 need_eh_cleanup);
kono
parents:
diff changeset
531
kono
parents:
diff changeset
532 /* Second phase. Eliminate second order degenerate PHIs as well
kono
parents:
diff changeset
533 as trivial copies or constant initializations identified by
kono
parents:
diff changeset
534 the first phase or this phase. Basically we keep iterating
kono
parents:
diff changeset
535 until our set of INTERESTING_NAMEs is empty. */
kono
parents:
diff changeset
536 while (!bitmap_empty_p (interesting_names))
kono
parents:
diff changeset
537 {
kono
parents:
diff changeset
538 unsigned int i;
kono
parents:
diff changeset
539 bitmap_iterator bi;
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
kono
parents:
diff changeset
542 changed during the loop. Copy it to another bitmap and
kono
parents:
diff changeset
543 use that. */
kono
parents:
diff changeset
544 bitmap_copy (interesting_names1, interesting_names);
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
kono
parents:
diff changeset
547 {
kono
parents:
diff changeset
548 tree name = ssa_name (i);
kono
parents:
diff changeset
549
kono
parents:
diff changeset
550 /* Ignore SSA_NAMEs that have been released because
kono
parents:
diff changeset
551 their defining statement was deleted (unreachable). */
kono
parents:
diff changeset
552 if (name)
kono
parents:
diff changeset
553 cfg_altered
kono
parents:
diff changeset
554 |= eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
kono
parents:
diff changeset
555 interesting_names, need_eh_cleanup);
kono
parents:
diff changeset
556 }
kono
parents:
diff changeset
557 }
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 if (cfg_altered)
kono
parents:
diff changeset
560 {
kono
parents:
diff changeset
561 free_dominance_info (CDI_DOMINATORS);
kono
parents:
diff changeset
562 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
kono
parents:
diff changeset
563 loops_state_set (LOOPS_NEED_FIXUP);
kono
parents:
diff changeset
564 }
kono
parents:
diff changeset
565
kono
parents:
diff changeset
566 /* Propagation of const and copies may make some EH edges dead. Purge
kono
parents:
diff changeset
567 such edges from the CFG as needed. */
kono
parents:
diff changeset
568 if (!bitmap_empty_p (need_eh_cleanup))
kono
parents:
diff changeset
569 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 return 0;
kono
parents:
diff changeset
572 }
kono
parents:
diff changeset
573
kono
parents:
diff changeset
574 } // anon namespace
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 gimple_opt_pass *
kono
parents:
diff changeset
577 make_pass_phi_only_cprop (gcc::context *ctxt)
kono
parents:
diff changeset
578 {
kono
parents:
diff changeset
579 return new pass_phi_only_cprop (ctxt);
kono
parents:
diff changeset
580 }