Branch data Line data Source code
1 : : /* Generic SSA value propagation engine.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 : : Contributed by Diego Novillo <dnovillo@redhat.com>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by the
9 : : Free Software Foundation; either version 3, or (at your option) any
10 : : later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT
13 : : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "ssa.h"
28 : : #include "gimple-pretty-print.h"
29 : : #include "dumpfile.h"
30 : : #include "gimple-iterator.h"
31 : : #include "gimple-fold.h"
32 : : #include "tree-eh.h"
33 : : #include "gimplify.h"
34 : : #include "tree-cfg.h"
35 : : #include "tree-ssa.h"
36 : : #include "tree-ssa-propagate.h"
37 : : #include "domwalk.h"
38 : : #include "cfgloop.h"
39 : : #include "tree-cfgcleanup.h"
40 : : #include "cfganal.h"
41 : : #include "tree-ssa-dce.h"
42 : :
43 : : /* This file implements a generic value propagation engine based on
44 : : the same propagation used by the SSA-CCP algorithm [1].
45 : :
46 : : Propagation is performed by simulating the execution of every
47 : : statement that produces the value being propagated. Simulation
48 : : proceeds as follows:
49 : :
50 : : 1- Initially, all edges of the CFG are marked not executable and
51 : : the CFG worklist is seeded with all the statements in the entry
52 : : basic block (block 0).
53 : :
54 : : 2- Every statement S is simulated with a call to the call-back
55 : : function SSA_PROP_VISIT_STMT. This evaluation may produce 3
56 : : results:
57 : :
58 : : SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
59 : : interest and does not affect any of the work lists.
60 : : The statement may be simulated again if any of its input
61 : : operands change in future iterations of the simulator.
62 : :
63 : : SSA_PROP_VARYING: The value produced by S cannot be determined
64 : : at compile time. Further simulation of S is not required.
65 : : If S is a conditional jump, all the outgoing edges for the
66 : : block are considered executable and added to the work
67 : : list.
68 : :
69 : : SSA_PROP_INTERESTING: S produces a value that can be computed
70 : : at compile time. Its result can be propagated into the
71 : : statements that feed from S. Furthermore, if S is a
72 : : conditional jump, only the edge known to be taken is added
73 : : to the work list. Edges that are known not to execute are
74 : : never simulated.
75 : :
76 : : 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
77 : : return value from SSA_PROP_VISIT_PHI has the same semantics as
78 : : described in #2.
79 : :
80 : : 4- Three work lists are kept. Statements are only added to these
81 : : lists if they produce one of SSA_PROP_INTERESTING or
82 : : SSA_PROP_VARYING.
83 : :
84 : : CFG_BLOCKS contains the list of blocks to be simulated.
85 : : Blocks are added to this list if their incoming edges are
86 : : found executable.
87 : :
88 : : SSA_EDGE_WORKLIST contains the list of statements that we
89 : : need to revisit.
90 : :
91 : : 5- Simulation terminates when all three work lists are drained.
92 : :
93 : : Before calling ssa_propagate, it is important to clear
94 : : prop_simulate_again_p for all the statements in the program that
95 : : should be simulated. This initialization allows an implementation
96 : : to specify which statements should never be simulated.
97 : :
98 : : It is also important to compute def-use information before calling
99 : : ssa_propagate.
100 : :
101 : : References:
102 : :
103 : : [1] Constant propagation with conditional branches,
104 : : Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
105 : :
106 : : [2] Building an Optimizing Compiler,
107 : : Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
108 : :
109 : : [3] Advanced Compiler Design and Implementation,
110 : : Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
111 : :
112 : : /* Worklists of control flow edge destinations. This contains
113 : : the CFG order number of the blocks so we can iterate in CFG
114 : : order by visiting in bit-order. We use two worklists to
115 : : first make forward progress before iterating. */
116 : : static bitmap cfg_blocks;
117 : : static int *bb_to_cfg_order;
118 : : static int *cfg_order_to_bb;
119 : :
120 : : /* Worklists of SSA edges which will need reexamination as their
121 : : definition has changed. SSA edges are def-use edges in the SSA
122 : : web. For each D-U edge, we store the target statement or PHI node
123 : : UID in a bitmap. UIDs order stmts in execution order. We use
124 : : two worklists to first make forward progress before iterating. */
125 : : static bitmap ssa_edge_worklist;
126 : : static vec<gimple *> uid_to_stmt;
127 : :
128 : : /* Current RPO index in the iteration. */
129 : : static int curr_order;
130 : :
131 : :
132 : : /* We have just defined a new value for VAR. If IS_VARYING is true,
133 : : add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
134 : : them to INTERESTING_SSA_EDGES. */
135 : :
136 : : static void
137 : 261084267 : add_ssa_edge (tree var)
138 : : {
139 : 261084267 : imm_use_iterator iter;
140 : 261084267 : use_operand_p use_p;
141 : :
142 : 792661910 : FOR_EACH_IMM_USE_FAST (use_p, iter, var)
143 : : {
144 : 531577643 : gimple *use_stmt = USE_STMT (use_p);
145 : 531577643 : if (!prop_simulate_again_p (use_stmt))
146 : 222906726 : continue;
147 : :
148 : : /* If we did not yet simulate the block wait for this to happen
149 : : and do not add the stmt to the SSA edge worklist. */
150 : 308670917 : basic_block use_bb = gimple_bb (use_stmt);
151 : 308670917 : if (! (use_bb->flags & BB_VISITED))
152 : 126841700 : continue;
153 : :
154 : : /* If this is a use on a not yet executable edge do not bother to
155 : : queue it. */
156 : 181829217 : if (gimple_code (use_stmt) == GIMPLE_PHI
157 : 181829217 : && !(EDGE_PRED (use_bb, PHI_ARG_INDEX_FROM_USE (use_p))->flags
158 : 58809543 : & EDGE_EXECUTABLE))
159 : 5408923 : continue;
160 : :
161 : 176420294 : if (bitmap_set_bit (ssa_edge_worklist, gimple_uid (use_stmt)))
162 : : {
163 : 163288910 : uid_to_stmt[gimple_uid (use_stmt)] = use_stmt;
164 : 163288910 : if (dump_file && (dump_flags & TDF_DETAILS))
165 : : {
166 : 0 : fprintf (dump_file, "ssa_edge_worklist: adding SSA use in ");
167 : 0 : print_gimple_stmt (dump_file, use_stmt, 0, TDF_SLIM);
168 : : }
169 : : }
170 : : }
171 : 261084267 : }
172 : :
173 : :
174 : : /* Add edge E to the control flow worklist. */
175 : :
176 : : static void
177 : 128544094 : add_control_edge (edge e)
178 : : {
179 : 128544094 : basic_block bb = e->dest;
180 : 128544094 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
181 : : return;
182 : :
183 : : /* If the edge had already been executed, skip it. */
184 : 113832132 : if (e->flags & EDGE_EXECUTABLE)
185 : : return;
186 : :
187 : 97824092 : e->flags |= EDGE_EXECUTABLE;
188 : :
189 : 97824092 : int bb_order = bb_to_cfg_order[bb->index];
190 : 97824092 : bitmap_set_bit (cfg_blocks, bb_order);
191 : :
192 : 97824092 : if (dump_file && (dump_flags & TDF_DETAILS))
193 : 53 : fprintf (dump_file, "Adding destination of edge (%d -> %d) to worklist\n",
194 : 53 : e->src->index, e->dest->index);
195 : : }
196 : :
197 : :
198 : : /* Simulate the execution of STMT and update the work lists accordingly. */
199 : :
200 : : void
201 : 703376139 : ssa_propagation_engine::simulate_stmt (gimple *stmt)
202 : : {
203 : 703376139 : enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
204 : 703376139 : edge taken_edge = NULL;
205 : 703376139 : tree output_name = NULL_TREE;
206 : :
207 : : /* Pull the stmt off the SSA edge worklist. */
208 : 703376139 : bitmap_clear_bit (ssa_edge_worklist, gimple_uid (stmt));
209 : :
210 : : /* Don't bother visiting statements that are already
211 : : considered varying by the propagator. */
212 : 703376139 : if (!prop_simulate_again_p (stmt))
213 : 489032934 : return;
214 : :
215 : 327226336 : if (gimple_code (stmt) == GIMPLE_PHI)
216 : : {
217 : 72675615 : val = visit_phi (as_a <gphi *> (stmt));
218 : 72675615 : output_name = gimple_phi_result (stmt);
219 : : }
220 : : else
221 : 254550721 : val = visit_stmt (stmt, &taken_edge, &output_name);
222 : :
223 : 327226336 : if (val == SSA_PROP_VARYING)
224 : : {
225 : 112883131 : prop_set_simulate_again (stmt, false);
226 : :
227 : : /* If the statement produced a new varying value, add the SSA
228 : : edges coming out of OUTPUT_NAME. */
229 : 112883131 : if (output_name)
230 : 67924359 : add_ssa_edge (output_name);
231 : :
232 : : /* If STMT transfers control out of its basic block, add
233 : : all outgoing edges to the work list. */
234 : 112883131 : if (stmt_ends_bb_p (stmt))
235 : : {
236 : 46404871 : edge e;
237 : 46404871 : edge_iterator ei;
238 : 46404871 : basic_block bb = gimple_bb (stmt);
239 : 119740167 : FOR_EACH_EDGE (e, ei, bb->succs)
240 : 73335296 : add_control_edge (e);
241 : : }
242 : 112883131 : return;
243 : : }
244 : 214343205 : else if (val == SSA_PROP_INTERESTING)
245 : : {
246 : : /* If the statement produced new value, add the SSA edges coming
247 : : out of OUTPUT_NAME. */
248 : 199277407 : if (output_name)
249 : 193159908 : add_ssa_edge (output_name);
250 : :
251 : : /* If we know which edge is going to be taken out of this block,
252 : : add it to the CFG work list. */
253 : 199277407 : if (taken_edge)
254 : 6117499 : add_control_edge (taken_edge);
255 : : }
256 : :
257 : : /* If there are no SSA uses on the stmt whose defs are simulated
258 : : again then this stmt will be never visited again. */
259 : 214343205 : bool has_simulate_again_uses = false;
260 : 214343205 : use_operand_p use_p;
261 : 214343205 : ssa_op_iter iter;
262 : 214343205 : if (gimple_code (stmt) == GIMPLE_PHI)
263 : : {
264 : 60329393 : edge_iterator ei;
265 : 60329393 : edge e;
266 : 60329393 : tree arg;
267 : 94380907 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
268 : 92437081 : if (!(e->flags & EDGE_EXECUTABLE)
269 : 92437081 : || ((arg = PHI_ARG_DEF_FROM_EDGE (stmt, e))
270 : 85079543 : && TREE_CODE (arg) == SSA_NAME
271 : 70024671 : && !SSA_NAME_IS_DEFAULT_DEF (arg)
272 : 69823634 : && prop_simulate_again_p (SSA_NAME_DEF_STMT (arg))))
273 : : {
274 : : has_simulate_again_uses = true;
275 : : break;
276 : : }
277 : : }
278 : : else
279 : 189726420 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
280 : : {
281 : 153124006 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
282 : 153124006 : if (!gimple_nop_p (def_stmt)
283 : 153124006 : && prop_simulate_again_p (def_stmt))
284 : : {
285 : : has_simulate_again_uses = true;
286 : : break;
287 : : }
288 : : }
289 : 214343205 : if (!has_simulate_again_uses)
290 : : {
291 : 38546240 : if (dump_file && (dump_flags & TDF_DETAILS))
292 : 31 : fprintf (dump_file, "marking stmt to be not simulated again\n");
293 : 38546240 : prop_set_simulate_again (stmt, false);
294 : : }
295 : : }
296 : :
297 : :
298 : : /* Simulate the execution of BLOCK. Evaluate the statement associated
299 : : with each variable reference inside the block. */
300 : :
301 : : void
302 : 74444337 : ssa_propagation_engine::simulate_block (basic_block block)
303 : : {
304 : 74444337 : gimple_stmt_iterator gsi;
305 : :
306 : : /* There is nothing to do for the exit block. */
307 : 74444337 : if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
308 : 74444337 : return;
309 : :
310 : 74444337 : if (dump_file && (dump_flags & TDF_DETAILS))
311 : 51 : fprintf (dump_file, "\nSimulating block %d\n", block->index);
312 : :
313 : : /* Always simulate PHI nodes, even if we have simulated this block
314 : : before. */
315 : 113865014 : for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
316 : 39420677 : simulate_stmt (gsi_stmt (gsi));
317 : :
318 : : /* If this is the first time we've simulated this block, then we
319 : : must simulate each of its statements. */
320 : 74444337 : if (! (block->flags & BB_VISITED))
321 : : {
322 : 69632188 : gimple_stmt_iterator j;
323 : 69632188 : unsigned int normal_edge_count;
324 : 69632188 : edge e, normal_edge;
325 : 69632188 : edge_iterator ei;
326 : :
327 : 640030515 : for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
328 : 500766139 : simulate_stmt (gsi_stmt (j));
329 : :
330 : : /* Note that we have simulated this block. */
331 : 69632188 : block->flags |= BB_VISITED;
332 : :
333 : : /* We cannot predict when abnormal and EH edges will be executed, so
334 : : once a block is considered executable, we consider any
335 : : outgoing abnormal edges as executable.
336 : :
337 : : TODO: This is not exactly true. Simplifying statement might
338 : : prove it non-throwing and also computed goto can be handled
339 : : when destination is known.
340 : :
341 : : At the same time, if this block has only one successor that is
342 : : reached by non-abnormal edges, then add that successor to the
343 : : worklist. */
344 : 69632188 : normal_edge_count = 0;
345 : 69632188 : normal_edge = NULL;
346 : 167648845 : FOR_EACH_EDGE (e, ei, block->succs)
347 : : {
348 : 98016657 : if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
349 : 6860595 : add_control_edge (e);
350 : : else
351 : : {
352 : 91156062 : normal_edge_count++;
353 : 91156062 : normal_edge = e;
354 : : }
355 : : }
356 : :
357 : 69632188 : if (normal_edge_count == 1)
358 : 34720037 : add_control_edge (normal_edge);
359 : : }
360 : : }
361 : :
362 : :
363 : : /* Initialize local data structures and work lists. */
364 : :
365 : : static void
366 : 7510667 : ssa_prop_init (void)
367 : : {
368 : 7510667 : edge e;
369 : 7510667 : edge_iterator ei;
370 : 7510667 : basic_block bb;
371 : :
372 : : /* Worklists of SSA edges. */
373 : 7510667 : ssa_edge_worklist = BITMAP_ALLOC (NULL);
374 : 7510667 : bitmap_tree_view (ssa_edge_worklist);
375 : :
376 : : /* Worklist of basic-blocks. */
377 : 7510667 : bb_to_cfg_order = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
378 : 7510667 : cfg_order_to_bb = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
379 : 7510667 : int n = pre_and_rev_post_order_compute_fn (cfun, NULL,
380 : : cfg_order_to_bb, false);
381 : 77533513 : for (int i = 0; i < n; ++i)
382 : 70022846 : bb_to_cfg_order[cfg_order_to_bb[i]] = i;
383 : 7510667 : cfg_blocks = BITMAP_ALLOC (NULL);
384 : :
385 : : /* Initially assume that every edge in the CFG is not executable.
386 : : (including the edges coming out of the entry block). Mark blocks
387 : : as not visited, blocks not yet visited will have all their statements
388 : : simulated once an incoming edge gets executable. */
389 : 7510667 : set_gimple_stmt_max_uid (cfun, 0);
390 : 77533513 : for (int i = 0; i < n; ++i)
391 : : {
392 : 70022846 : gimple_stmt_iterator si;
393 : 70022846 : bb = BASIC_BLOCK_FOR_FN (cfun, cfg_order_to_bb[i]);
394 : :
395 : 99007062 : for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
396 : : {
397 : 28984216 : gimple *stmt = gsi_stmt (si);
398 : 28984216 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
399 : : }
400 : :
401 : 642034541 : for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
402 : : {
403 : 501988849 : gimple *stmt = gsi_stmt (si);
404 : 501988849 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
405 : : }
406 : :
407 : 70022846 : bb->flags &= ~BB_VISITED;
408 : 168419063 : FOR_EACH_EDGE (e, ei, bb->succs)
409 : 98396217 : e->flags &= ~EDGE_EXECUTABLE;
410 : : }
411 : 7510667 : uid_to_stmt.safe_grow (gimple_stmt_max_uid (cfun), true);
412 : 7510667 : }
413 : :
414 : :
415 : : /* Free allocated storage. */
416 : :
417 : : static void
418 : 7510667 : ssa_prop_fini (void)
419 : : {
420 : 7510667 : BITMAP_FREE (cfg_blocks);
421 : 7510667 : free (bb_to_cfg_order);
422 : 7510667 : free (cfg_order_to_bb);
423 : 7510667 : BITMAP_FREE (ssa_edge_worklist);
424 : 7510667 : uid_to_stmt.release ();
425 : 7510667 : }
426 : :
427 : :
428 : : /* Entry point to the propagation engine.
429 : :
430 : : The VISIT_STMT virtual function is called for every statement
431 : : visited and the VISIT_PHI virtual function is called for every PHI
432 : : node visited. */
433 : :
434 : : void
435 : 7510667 : ssa_propagation_engine::ssa_propagate (void)
436 : : {
437 : 7510667 : ssa_prop_init ();
438 : :
439 : 7510667 : curr_order = 0;
440 : :
441 : : /* Iterate until the worklists are empty. We iterate both blocks
442 : : and stmts in RPO order, prioritizing backedge processing.
443 : : Seed the algorithm by adding the successors of the entry block to the
444 : : edge worklist. */
445 : 7510667 : edge e;
446 : 7510667 : edge_iterator ei;
447 : 15021334 : FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
448 : : {
449 : 7510667 : e->flags &= ~EDGE_EXECUTABLE;
450 : 7510667 : add_control_edge (e);
451 : : }
452 : 245144327 : while (1)
453 : : {
454 : 245144327 : int next_block_order = (bitmap_empty_p (cfg_blocks)
455 : 245144327 : ? -1 : bitmap_first_set_bit (cfg_blocks));
456 : 245144327 : int next_stmt_uid = (bitmap_empty_p (ssa_edge_worklist)
457 : 245144327 : ? -1 : bitmap_first_set_bit (ssa_edge_worklist));
458 : 245144327 : if (next_block_order == -1 && next_stmt_uid == -1)
459 : : break;
460 : :
461 : 237633660 : int next_stmt_bb_order = -1;
462 : 237633660 : gimple *next_stmt = NULL;
463 : 237633660 : if (next_stmt_uid != -1)
464 : : {
465 : 166745960 : next_stmt = uid_to_stmt[next_stmt_uid];
466 : 166745960 : next_stmt_bb_order = bb_to_cfg_order[gimple_bb (next_stmt)->index];
467 : : }
468 : :
469 : : /* Pull the next block to simulate off the worklist if it comes first. */
470 : 237633660 : if (next_block_order != -1
471 : 209600072 : && (next_stmt_bb_order == -1
472 : 209600072 : || next_block_order <= next_stmt_bb_order))
473 : : {
474 : 74444337 : curr_order = next_block_order;
475 : 74444337 : bitmap_clear_bit (cfg_blocks, next_block_order);
476 : 74444337 : basic_block bb
477 : 74444337 : = BASIC_BLOCK_FOR_FN (cfun, cfg_order_to_bb [next_block_order]);
478 : 74444337 : simulate_block (bb);
479 : 74444337 : }
480 : : /* Else simulate from the SSA edge worklist. */
481 : : else
482 : : {
483 : 163189323 : curr_order = next_stmt_bb_order;
484 : 163189323 : if (dump_file && (dump_flags & TDF_DETAILS))
485 : : {
486 : 0 : fprintf (dump_file, "\nSimulating statement: ");
487 : 0 : print_gimple_stmt (dump_file, next_stmt, 0, dump_flags);
488 : : }
489 : 163189323 : simulate_stmt (next_stmt);
490 : : }
491 : : }
492 : :
493 : 7510667 : ssa_prop_fini ();
494 : 7510667 : }
495 : :
496 : : /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
497 : : is a non-volatile pointer dereference, a structure reference or a
498 : : reference to a single _DECL. Ignore volatile memory references
499 : : because they are not interesting for the optimizers. */
500 : :
501 : : bool
502 : 25702711 : stmt_makes_single_store (gimple *stmt)
503 : : {
504 : 25702711 : tree lhs;
505 : :
506 : 25702711 : if (gimple_code (stmt) != GIMPLE_ASSIGN
507 : 25702711 : && gimple_code (stmt) != GIMPLE_CALL)
508 : : return false;
509 : :
510 : 25716648 : if (!gimple_vdef (stmt))
511 : : return false;
512 : :
513 : 2454447 : lhs = gimple_get_lhs (stmt);
514 : :
515 : : /* A call statement may have a null LHS. */
516 : 2454447 : if (!lhs)
517 : : return false;
518 : :
519 : 2454447 : return (!TREE_THIS_VOLATILE (lhs)
520 : 2454447 : && (DECL_P (lhs)
521 : 2440510 : || REFERENCE_CLASS_P (lhs)));
522 : : }
523 : :
524 : :
525 : : /* Propagation statistics. */
526 : : struct prop_stats_d
527 : : {
528 : : long num_const_prop;
529 : : long num_copy_prop;
530 : : long num_stmts_folded;
531 : : };
532 : :
533 : : static struct prop_stats_d prop_stats;
534 : :
535 : : // range_query default methods to drive from a value_of_expr() ranther than
536 : : // range_of_expr.
537 : :
538 : : tree
539 : 50775989 : substitute_and_fold_engine::value_on_edge (edge, tree expr)
540 : : {
541 : 50775989 : return value_of_expr (expr);
542 : : }
543 : :
544 : : tree
545 : 123191620 : substitute_and_fold_engine::value_of_stmt (gimple *stmt, tree name)
546 : : {
547 : 123191620 : if (!name)
548 : 0 : name = gimple_get_lhs (stmt);
549 : :
550 : 123191620 : gcc_checking_assert (!name || name == gimple_get_lhs (stmt));
551 : :
552 : 123191620 : if (name)
553 : 123191620 : return value_of_expr (name);
554 : : return NULL_TREE;
555 : : }
556 : :
557 : : bool
558 : 0 : substitute_and_fold_engine::range_of_expr (vrange &, tree, gimple *)
559 : : {
560 : 0 : return false;
561 : : }
562 : :
563 : : /* Replace USE references in statement STMT with the values stored in
564 : : PROP_VALUE. Return true if at least one reference was replaced. */
565 : :
566 : : bool
567 : 696680640 : substitute_and_fold_engine::replace_uses_in (gimple *stmt)
568 : : {
569 : 696680640 : bool replaced = false;
570 : 696680640 : use_operand_p use;
571 : 696680640 : ssa_op_iter iter;
572 : :
573 : 1040192501 : FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
574 : : {
575 : 343511861 : tree tuse = USE_FROM_PTR (use);
576 : 343511861 : tree val = value_of_expr (tuse, stmt);
577 : :
578 : 343511861 : if (val == tuse || val == NULL_TREE)
579 : 331016102 : continue;
580 : :
581 : 12495759 : if (gimple_code (stmt) == GIMPLE_ASM
582 : 12495759 : && !may_propagate_copy_into_asm (tuse))
583 : 0 : continue;
584 : :
585 : 12495759 : if (!may_propagate_copy (tuse, val))
586 : 450 : continue;
587 : :
588 : 12495309 : if (TREE_CODE (val) != SSA_NAME)
589 : 4685600 : prop_stats.num_const_prop++;
590 : : else
591 : 7809709 : prop_stats.num_copy_prop++;
592 : :
593 : 12495309 : propagate_value (use, val);
594 : :
595 : 12495309 : replaced = true;
596 : : }
597 : :
598 : 696680640 : return replaced;
599 : : }
600 : :
601 : :
602 : : /* Replace propagated values into all the arguments for PHI using the
603 : : values from PROP_VALUE. */
604 : :
605 : : bool
606 : 21688846 : substitute_and_fold_engine::replace_phi_args_in (gphi *phi)
607 : : {
608 : 21688846 : size_t i;
609 : 21688846 : bool replaced = false;
610 : :
611 : 71478676 : for (i = 0; i < gimple_phi_num_args (phi); i++)
612 : : {
613 : 49789830 : tree arg = gimple_phi_arg_def (phi, i);
614 : :
615 : 49789830 : if (TREE_CODE (arg) == SSA_NAME)
616 : : {
617 : 35175668 : edge e = gimple_phi_arg_edge (phi, i);
618 : 35175668 : tree val = value_on_edge (e, arg);
619 : :
620 : 35175668 : if (val && val != arg && may_propagate_copy (arg, val))
621 : : {
622 : 1057144 : if (TREE_CODE (val) != SSA_NAME)
623 : 30275 : prop_stats.num_const_prop++;
624 : : else
625 : 1026869 : prop_stats.num_copy_prop++;
626 : :
627 : 1057144 : propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
628 : 1057144 : replaced = true;
629 : :
630 : : /* If we propagated a copy and this argument flows
631 : : through an abnormal edge, update the replacement
632 : : accordingly. */
633 : 1057144 : if (TREE_CODE (val) == SSA_NAME
634 : 1026869 : && e->flags & EDGE_ABNORMAL
635 : 1057144 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
636 : : {
637 : : /* This can only occur for virtual operands, since
638 : : for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
639 : : would prevent replacement. */
640 : 0 : gcc_checking_assert (virtual_operand_p (val));
641 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
642 : : }
643 : : }
644 : : }
645 : : }
646 : :
647 : 21688846 : if (dump_file && (dump_flags & TDF_DETAILS))
648 : : {
649 : 144 : if (!replaced)
650 : 144 : fprintf (dump_file, "No folding possible\n");
651 : : else
652 : : {
653 : 0 : fprintf (dump_file, "Folded into: ");
654 : 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
655 : 0 : fprintf (dump_file, "\n");
656 : : }
657 : : }
658 : :
659 : 21688846 : return replaced;
660 : : }
661 : :
662 : :
663 : : class substitute_and_fold_dom_walker : public dom_walker
664 : : {
665 : : public:
666 : 11519376 : substitute_and_fold_dom_walker (cdi_direction direction,
667 : : class substitute_and_fold_engine *engine)
668 : 11519376 : : dom_walker (direction),
669 : 11519376 : something_changed (false),
670 : 11519376 : substitute_and_fold_engine (engine)
671 : : {
672 : 11519376 : dceworklist = BITMAP_ALLOC (NULL);
673 : 11519376 : stmts_to_fixup.create (0);
674 : 11519376 : need_eh_cleanup = BITMAP_ALLOC (NULL);
675 : 11519376 : need_ab_cleanup = BITMAP_ALLOC (NULL);
676 : 11519376 : }
677 : 11519376 : ~substitute_and_fold_dom_walker ()
678 : 11519376 : {
679 : 11519376 : BITMAP_FREE (dceworklist);
680 : 11519376 : stmts_to_fixup.release ();
681 : 11519376 : BITMAP_FREE (need_eh_cleanup);
682 : 11519376 : BITMAP_FREE (need_ab_cleanup);
683 : 11519376 : }
684 : :
685 : : edge before_dom_children (basic_block) final override;
686 : 112289295 : void after_dom_children (basic_block bb) final override
687 : : {
688 : 112289295 : substitute_and_fold_engine->post_fold_bb (bb);
689 : 112289295 : }
690 : :
691 : : bool something_changed;
692 : : bitmap dceworklist;
693 : : vec<gimple *> stmts_to_fixup;
694 : : bitmap need_eh_cleanup;
695 : : bitmap need_ab_cleanup;
696 : :
697 : : class substitute_and_fold_engine *substitute_and_fold_engine;
698 : :
699 : : private:
700 : : void foreach_new_stmt_in_bb (gimple_stmt_iterator old_gsi,
701 : : gimple_stmt_iterator new_gsi);
702 : : };
703 : :
704 : : /* Call post_new_stmt for each new statement that has been added
705 : : to the current BB. OLD_GSI is the statement iterator before the BB
706 : : changes ocurred. NEW_GSI is the iterator which may contain new
707 : : statements. */
708 : :
709 : : void
710 : 14299565 : substitute_and_fold_dom_walker::foreach_new_stmt_in_bb
711 : : (gimple_stmt_iterator old_gsi,
712 : : gimple_stmt_iterator new_gsi)
713 : : {
714 : 14299565 : basic_block bb = gsi_bb (new_gsi);
715 : 14299565 : if (gsi_end_p (old_gsi))
716 : 3228632 : old_gsi = gsi_start_bb (bb);
717 : : else
718 : 12685249 : gsi_next (&old_gsi);
719 : 14367450 : while (gsi_stmt (old_gsi) != gsi_stmt (new_gsi))
720 : : {
721 : 67885 : gimple *stmt = gsi_stmt (old_gsi);
722 : 67885 : substitute_and_fold_engine->post_new_stmt (stmt);
723 : 67885 : gsi_next (&old_gsi);
724 : : }
725 : 14299565 : }
726 : :
727 : : bool
728 : 112289295 : substitute_and_fold_engine::propagate_into_phi_args (basic_block bb)
729 : : {
730 : 112289295 : edge e;
731 : 112289295 : edge_iterator ei;
732 : 112289295 : bool propagated = false;
733 : :
734 : : /* Visit BB successor PHI nodes and replace PHI args. */
735 : 265173275 : FOR_EACH_EDGE (e, ei, bb->succs)
736 : : {
737 : 152883980 : for (gphi_iterator gpi = gsi_start_phis (e->dest);
738 : 253847539 : !gsi_end_p (gpi); gsi_next (&gpi))
739 : : {
740 : 100963559 : gphi *phi = gpi.phi ();
741 : 100963559 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
742 : 100963559 : tree arg = USE_FROM_PTR (use_p);
743 : 163470543 : if (TREE_CODE (arg) != SSA_NAME
744 : 100963559 : || virtual_operand_p (arg))
745 : 62506984 : continue;
746 : 38456575 : tree val = value_on_edge (e, arg);
747 : 38456575 : if (val
748 : 2870201 : && is_gimple_min_invariant (val)
749 : 40243903 : && may_propagate_copy (arg, val))
750 : : {
751 : 1785519 : propagate_value (use_p, val);
752 : 1785519 : propagated = true;
753 : : }
754 : : }
755 : : }
756 : 112289295 : return propagated;
757 : : }
758 : :
759 : : edge
760 : 112289295 : substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
761 : : {
762 : 112289295 : substitute_and_fold_engine->pre_fold_bb (bb);
763 : :
764 : : /* Propagate known values into PHI nodes. */
765 : 112289295 : for (gphi_iterator i = gsi_start_phis (bb);
766 : 155343607 : !gsi_end_p (i);
767 : 43054312 : gsi_next (&i))
768 : : {
769 : 43054312 : gphi *phi = i.phi ();
770 : 43054312 : tree res = gimple_phi_result (phi);
771 : 86108624 : if (virtual_operand_p (res))
772 : 19404522 : continue;
773 : 23649790 : if (dump_file && (dump_flags & TDF_DETAILS))
774 : : {
775 : 152 : fprintf (dump_file, "Folding PHI node: ");
776 : 152 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
777 : : }
778 : 23649790 : if (res && TREE_CODE (res) == SSA_NAME)
779 : : {
780 : 23649790 : tree sprime = substitute_and_fold_engine->value_of_expr (res, phi);
781 : 25610734 : if (sprime
782 : 23649790 : && sprime != res
783 : 23649790 : && may_propagate_copy (res, sprime))
784 : : {
785 : 1960944 : if (dump_file && (dump_flags & TDF_DETAILS))
786 : : {
787 : 8 : fprintf (dump_file, "Queued PHI for removal. Folds to: ");
788 : 8 : print_generic_expr (dump_file, sprime);
789 : 8 : fprintf (dump_file, "\n");
790 : : }
791 : 1960944 : bitmap_set_bit (dceworklist, SSA_NAME_VERSION (res));
792 : : /* As this now constitutes a copy duplicate points-to
793 : : and range info appropriately. */
794 : 1960944 : if (TREE_CODE (sprime) == SSA_NAME)
795 : 1236209 : maybe_duplicate_ssa_info_at_copy (res, sprime);
796 : 1960944 : continue;
797 : : }
798 : : }
799 : 21688846 : something_changed |= substitute_and_fold_engine->replace_phi_args_in (phi);
800 : : }
801 : :
802 : : /* Propagate known values into stmts. In some case it exposes
803 : : more trivially deletable stmts to walk backward. */
804 : 224578590 : for (gimple_stmt_iterator i = gsi_start_bb (bb);
805 : 822569763 : !gsi_end_p (i);
806 : 710280468 : gsi_next (&i))
807 : : {
808 : 710280468 : bool did_replace;
809 : 710280468 : gimple *stmt = gsi_stmt (i);
810 : :
811 : 710280468 : substitute_and_fold_engine->pre_fold_stmt (stmt);
812 : :
813 : 710280468 : if (dump_file && (dump_flags & TDF_DETAILS))
814 : : {
815 : 1760 : fprintf (dump_file, "Folding statement: ");
816 : 1760 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
817 : : }
818 : :
819 : : /* No point propagating into a stmt we have a value for we
820 : : can propagate into all uses. Mark it for removal instead. */
821 : 710280468 : tree lhs = gimple_get_lhs (stmt);
822 : 710280468 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
823 : : {
824 : 168043641 : tree sprime = substitute_and_fold_engine->value_of_stmt (stmt, lhs);
825 : 181643469 : if (sprime
826 : 168043641 : && sprime != lhs
827 : 13618163 : && may_propagate_copy (lhs, sprime)
828 : 13616874 : && !stmt_could_throw_p (cfun, stmt)
829 : 181647385 : && !gimple_has_side_effects (stmt))
830 : : {
831 : 13599828 : if (dump_file && (dump_flags & TDF_DETAILS))
832 : : {
833 : 50 : fprintf (dump_file, "Queued stmt for removal. Folds to: ");
834 : 50 : print_generic_expr (dump_file, sprime);
835 : 50 : fprintf (dump_file, "\n");
836 : : }
837 : 13599828 : bitmap_set_bit (dceworklist, SSA_NAME_VERSION (lhs));
838 : : /* As this now constitutes a copy duplicate points-to
839 : : and range info appropriately. */
840 : 13599828 : if (TREE_CODE (sprime) == SSA_NAME)
841 : 8080521 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
842 : 13599828 : continue;
843 : : }
844 : : }
845 : :
846 : : /* Replace the statement with its folded version and mark it
847 : : folded. */
848 : 696680640 : did_replace = false;
849 : 696680640 : gimple *old_stmt = stmt;
850 : 696680640 : bool was_noreturn = false;
851 : 696680640 : bool can_make_abnormal_goto = false;
852 : 696680640 : if (is_gimple_call (stmt))
853 : : {
854 : 49770560 : was_noreturn = gimple_call_noreturn_p (stmt);
855 : 49770560 : can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
856 : : }
857 : :
858 : : /* Replace real uses in the statement. */
859 : 696680640 : did_replace |= substitute_and_fold_engine->replace_uses_in (stmt);
860 : :
861 : 696680640 : gimple_stmt_iterator prev_gsi = i;
862 : 696680640 : gsi_prev (&prev_gsi);
863 : :
864 : : /* If we made a replacement, fold the statement. */
865 : 696680640 : if (did_replace)
866 : : {
867 : 11722774 : fold_stmt (&i, follow_single_use_edges);
868 : 11722774 : stmt = gsi_stmt (i);
869 : 11722774 : gimple_set_modified (stmt, true);
870 : : }
871 : : /* Also fold if we want to fold all statements. */
872 : 684957866 : else if (substitute_and_fold_engine->fold_all_stmts
873 : 684957866 : && fold_stmt (&i, follow_single_use_edges))
874 : : {
875 : 0 : did_replace = true;
876 : 0 : stmt = gsi_stmt (i);
877 : 0 : gimple_set_modified (stmt, true);
878 : : }
879 : :
880 : : /* Some statements may be simplified using propagator
881 : : specific information. Do this before propagating
882 : : into the stmt to not disturb pass specific information. */
883 : 696680640 : update_stmt_if_modified (stmt);
884 : 696680640 : if (substitute_and_fold_engine->fold_stmt (&i))
885 : : {
886 : 2969306 : did_replace = true;
887 : 2969306 : prop_stats.num_stmts_folded++;
888 : 2969306 : stmt = gsi_stmt (i);
889 : 2969306 : gimple_set_modified (stmt, true);
890 : : }
891 : :
892 : : /* If this is a control statement the propagator left edges
893 : : unexecuted on force the condition in a way consistent with
894 : : that. See PR66945 for cases where the propagator can end
895 : : up with a different idea of a taken edge than folding
896 : : (once undefined behavior is involved). */
897 : 696680640 : if (gimple_code (stmt) == GIMPLE_COND)
898 : : {
899 : 38917862 : if ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE)
900 : 38917862 : ^ (EDGE_SUCC (bb, 1)->flags & EDGE_EXECUTABLE))
901 : : {
902 : 345803 : if (((EDGE_SUCC (bb, 0)->flags & EDGE_TRUE_VALUE) != 0)
903 : 345803 : == ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE) != 0))
904 : 79095 : gimple_cond_make_true (as_a <gcond *> (stmt));
905 : : else
906 : 266708 : gimple_cond_make_false (as_a <gcond *> (stmt));
907 : 345803 : gimple_set_modified (stmt, true);
908 : 345803 : did_replace = true;
909 : : }
910 : : }
911 : :
912 : : /* Now cleanup. */
913 : 696680640 : if (did_replace)
914 : : {
915 : 14299565 : foreach_new_stmt_in_bb (prev_gsi, i);
916 : :
917 : : /* If we cleaned up EH information from the statement,
918 : : remove EH edges. */
919 : 14299565 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
920 : 155461 : bitmap_set_bit (need_eh_cleanup, bb->index);
921 : :
922 : : /* If we turned a call with possible abnormal control transfer
923 : : into one that doesn't, remove abnormal edges. */
924 : 14299565 : if (can_make_abnormal_goto
925 : 14299565 : && !stmt_can_make_abnormal_goto (stmt))
926 : 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
927 : :
928 : : /* If we turned a not noreturn call into a noreturn one
929 : : schedule it for fixup. */
930 : 14299565 : if (!was_noreturn
931 : 14176486 : && is_gimple_call (stmt)
932 : 15619586 : && gimple_call_noreturn_p (stmt))
933 : 53 : stmts_to_fixup.safe_push (stmt);
934 : :
935 : 14299565 : if (gimple_assign_single_p (stmt))
936 : : {
937 : 3170339 : tree rhs = gimple_assign_rhs1 (stmt);
938 : :
939 : 3170339 : if (TREE_CODE (rhs) == ADDR_EXPR)
940 : 341030 : recompute_tree_invariant_for_addr_expr (rhs);
941 : : }
942 : :
943 : : /* Determine what needs to be done to update the SSA form. */
944 : 14299565 : update_stmt_if_modified (stmt);
945 : 14299565 : if (!is_gimple_debug (stmt))
946 : 10965562 : something_changed = true;
947 : : }
948 : :
949 : 696680640 : if (dump_file && (dump_flags & TDF_DETAILS))
950 : : {
951 : 1710 : if (did_replace)
952 : : {
953 : 154 : fprintf (dump_file, "Folded into: ");
954 : 154 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
955 : 154 : fprintf (dump_file, "\n");
956 : : }
957 : : else
958 : 1556 : fprintf (dump_file, "Not folded\n");
959 : : }
960 : : }
961 : :
962 : 112289295 : something_changed |= substitute_and_fold_engine->propagate_into_phi_args (bb);
963 : :
964 : 112289295 : return NULL;
965 : : }
966 : :
967 : :
968 : :
969 : : /* Perform final substitution and folding of propagated values.
970 : : Process the whole function if BLOCK is null, otherwise only
971 : : process the blocks that BLOCK dominates. In the latter case,
972 : : it is the caller's responsibility to ensure that dominator
973 : : information is available and up-to-date.
974 : :
975 : : PROP_VALUE[I] contains the single value that should be substituted
976 : : at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
977 : : substituted.
978 : :
979 : : If FOLD_FN is non-NULL the function will be invoked on all statements
980 : : before propagating values for pass specific simplification.
981 : :
982 : : DO_DCE is true if trivially dead stmts can be removed.
983 : :
984 : : If DO_DCE is true, the statements within a BB are walked from
985 : : last to first element. Otherwise we scan from first to last element.
986 : :
987 : : Return TRUE when something changed. */
988 : :
989 : : bool
990 : 11519376 : substitute_and_fold_engine::substitute_and_fold (basic_block block)
991 : : {
992 : 11519376 : if (dump_file && (dump_flags & TDF_DETAILS))
993 : 157 : fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
994 : :
995 : 11519376 : memset (&prop_stats, 0, sizeof (prop_stats));
996 : :
997 : : /* Don't call calculate_dominance_info when iterating over a subgraph.
998 : : Callers that are using the interface this way are likely to want to
999 : : iterate over several disjoint subgraphs, and it would be expensive
1000 : : in enable-checking builds to revalidate the whole dominance tree
1001 : : each time. */
1002 : 11519376 : if (block)
1003 : 1161 : gcc_assert (dom_info_state (CDI_DOMINATORS));
1004 : : else
1005 : 11518215 : calculate_dominance_info (CDI_DOMINATORS);
1006 : 11519376 : substitute_and_fold_dom_walker walker (CDI_DOMINATORS, this);
1007 : 11519376 : walker.walk (block ? block : ENTRY_BLOCK_PTR_FOR_FN (cfun));
1008 : :
1009 : 11519376 : simple_dce_from_worklist (walker.dceworklist, walker.need_eh_cleanup);
1010 : 11519376 : if (!bitmap_empty_p (walker.need_eh_cleanup))
1011 : 36450 : gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1012 : 11519376 : if (!bitmap_empty_p (walker.need_ab_cleanup))
1013 : 3 : gimple_purge_all_dead_abnormal_call_edges (walker.need_ab_cleanup);
1014 : :
1015 : : /* Fixup stmts that became noreturn calls. This may require splitting
1016 : : blocks and thus isn't possible during the dominator walk. Do this
1017 : : in reverse order so we don't inadvertedly remove a stmt we want to
1018 : : fixup by visiting a dominating now noreturn call first. */
1019 : 11519429 : while (!walker.stmts_to_fixup.is_empty ())
1020 : : {
1021 : 53 : gimple *stmt = walker.stmts_to_fixup.pop ();
1022 : 0 : if (dump_file && dump_flags & TDF_DETAILS)
1023 : : {
1024 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
1025 : 0 : print_gimple_stmt (dump_file, stmt, 0);
1026 : 0 : fprintf (dump_file, "\n");
1027 : : }
1028 : 53 : fixup_noreturn_call (stmt);
1029 : : }
1030 : :
1031 : 11519376 : statistics_counter_event (cfun, "Constants propagated",
1032 : 11519376 : prop_stats.num_const_prop);
1033 : 11519376 : statistics_counter_event (cfun, "Copies propagated",
1034 : 11519376 : prop_stats.num_copy_prop);
1035 : 11519376 : statistics_counter_event (cfun, "Statements folded",
1036 : 11519376 : prop_stats.num_stmts_folded);
1037 : :
1038 : 11519376 : return walker.something_changed;
1039 : 11519376 : }
1040 : :
1041 : :
1042 : : /* Return true if we may propagate ORIG into DEST, false otherwise.
1043 : : If DEST_NOT_ABNORMAL_PHI_EDGE_P is true then assume the propagation does
1044 : : not happen into a PHI argument which flows in from an abnormal edge
1045 : : which relaxes some constraints. */
1046 : :
1047 : : bool
1048 : 137674522 : may_propagate_copy (tree dest, tree orig, bool dest_not_abnormal_phi_edge_p)
1049 : : {
1050 : 137674522 : tree type_d = TREE_TYPE (dest);
1051 : 137674522 : tree type_o = TREE_TYPE (orig);
1052 : :
1053 : : /* If ORIG is a default definition which flows in from an abnormal edge
1054 : : then the copy can be propagated. It is important that we do so to avoid
1055 : : uninitialized copies. */
1056 : 137674522 : if (TREE_CODE (orig) == SSA_NAME
1057 : 95594414 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1058 : 26257 : && SSA_NAME_IS_DEFAULT_DEF (orig)
1059 : 137677692 : && (SSA_NAME_VAR (orig) == NULL_TREE
1060 : 3170 : || VAR_P (SSA_NAME_VAR (orig))))
1061 : : ;
1062 : : /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1063 : : be propagated. */
1064 : 137671749 : else if (TREE_CODE (orig) == SSA_NAME
1065 : 137671749 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1066 : : return false;
1067 : : /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1068 : : propagated. If we know we do not propagate into such a PHI argument this
1069 : : does not apply. */
1070 : 137648265 : else if (!dest_not_abnormal_phi_edge_p
1071 : 72435189 : && TREE_CODE (dest) == SSA_NAME
1072 : 210083454 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1073 : : return false;
1074 : :
1075 : : /* Do not copy between types for which we *do* need a conversion. */
1076 : 137636330 : if (!useless_type_conversion_p (type_d, type_o))
1077 : : return false;
1078 : :
1079 : : /* Generally propagating virtual operands is not ok as that may
1080 : : create overlapping life-ranges. */
1081 : 137634894 : if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1082 : : return false;
1083 : :
1084 : : /* Keep lhs of [[gnu::musttail]] calls as is, those need to be still
1085 : : tail callable. */
1086 : 137201875 : if (TREE_CODE (dest) == SSA_NAME
1087 : 137071123 : && is_gimple_call (SSA_NAME_DEF_STMT (dest))
1088 : 138396605 : && gimple_call_must_tail_p (as_a <gcall *> (SSA_NAME_DEF_STMT (dest))))
1089 : : return false;
1090 : :
1091 : : /* Anything else is OK. */
1092 : : return true;
1093 : : }
1094 : :
1095 : : /* Like may_propagate_copy, but use as the destination expression
1096 : : the principal expression (typically, the RHS) contained in
1097 : : statement DEST. This is more efficient when working with the
1098 : : gimple tuples representation. */
1099 : :
1100 : : bool
1101 : 301194 : may_propagate_copy_into_stmt (gimple *dest, tree orig)
1102 : : {
1103 : 301194 : tree type_d;
1104 : 301194 : tree type_o;
1105 : :
1106 : : /* If the statement is a switch or a single-rhs assignment,
1107 : : then the expression to be replaced by the propagation may
1108 : : be an SSA_NAME. Fortunately, there is an explicit tree
1109 : : for the expression, so we delegate to may_propagate_copy. */
1110 : :
1111 : 301194 : if (gimple_assign_single_p (dest))
1112 : 130780 : return may_propagate_copy (gimple_assign_rhs1 (dest), orig, true);
1113 : 170414 : else if (gswitch *dest_swtch = dyn_cast <gswitch *> (dest))
1114 : 0 : return may_propagate_copy (gimple_switch_index (dest_swtch), orig, true);
1115 : :
1116 : : /* In other cases, the expression is not materialized, so there
1117 : : is no destination to pass to may_propagate_copy. On the other
1118 : : hand, the expression cannot be an SSA_NAME, so the analysis
1119 : : is much simpler. */
1120 : :
1121 : 170414 : if (TREE_CODE (orig) == SSA_NAME
1122 : 170414 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1123 : : return false;
1124 : :
1125 : 170414 : if (is_gimple_assign (dest))
1126 : 141004 : type_d = TREE_TYPE (gimple_assign_lhs (dest));
1127 : 29410 : else if (gimple_code (dest) == GIMPLE_COND)
1128 : 28848 : type_d = boolean_type_node;
1129 : 562 : else if (is_gimple_call (dest)
1130 : 562 : && gimple_call_lhs (dest) != NULL_TREE)
1131 : 562 : type_d = TREE_TYPE (gimple_call_lhs (dest));
1132 : : else
1133 : 0 : gcc_unreachable ();
1134 : :
1135 : 170414 : type_o = TREE_TYPE (orig);
1136 : :
1137 : 170414 : if (!useless_type_conversion_p (type_d, type_o))
1138 : : return false;
1139 : :
1140 : : return true;
1141 : : }
1142 : :
1143 : : /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1144 : :
1145 : : bool
1146 : 11491 : may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1147 : : {
1148 : 11491 : return true;
1149 : : }
1150 : :
1151 : :
1152 : : /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1153 : :
1154 : : Use this version when not const/copy propagating values. For example,
1155 : : PRE uses this version when building expressions as they would appear
1156 : : in specific blocks taking into account actions of PHI nodes.
1157 : :
1158 : : The statement in which an expression has been replaced should be
1159 : : folded using fold_stmt_inplace. */
1160 : :
1161 : : void
1162 : 49358389 : replace_exp (use_operand_p op_p, tree val)
1163 : : {
1164 : 49358389 : if (TREE_CODE (val) == SSA_NAME || CONSTANT_CLASS_P (val))
1165 : 46108278 : SET_USE (op_p, val);
1166 : : else
1167 : 3250111 : SET_USE (op_p, unshare_expr (val));
1168 : 49358389 : }
1169 : :
1170 : :
1171 : : /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1172 : : into the operand pointed to by OP_P.
1173 : :
1174 : : Use this version for const/copy propagation as it will perform additional
1175 : : checks to ensure validity of the const/copy propagation. */
1176 : :
1177 : : void
1178 : 43237230 : propagate_value (use_operand_p op_p, tree val)
1179 : : {
1180 : 43237230 : if (flag_checking)
1181 : : {
1182 : 43236758 : bool ab = (is_a <gphi *> (USE_STMT (op_p))
1183 : 58095364 : && (gimple_phi_arg_edge (as_a <gphi *> (USE_STMT (op_p)),
1184 : 14858606 : PHI_ARG_INDEX_FROM_USE (op_p))
1185 : 14858606 : ->flags & EDGE_ABNORMAL));
1186 : 43236758 : gcc_assert (may_propagate_copy (USE_FROM_PTR (op_p), val, !ab));
1187 : : }
1188 : 43237230 : replace_exp (op_p, val);
1189 : 43237230 : }
1190 : :
1191 : :
1192 : : /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1193 : : into the tree pointed to by OP_P.
1194 : :
1195 : : Use this version for const/copy propagation when SSA operands are not
1196 : : available. It will perform the additional checks to ensure validity of
1197 : : the const/copy propagation, but will not update any operand information.
1198 : : Be sure to mark the stmt as modified. */
1199 : :
1200 : : void
1201 : 338407 : propagate_tree_value (tree *op_p, tree val)
1202 : : {
1203 : 338407 : if (TREE_CODE (val) == SSA_NAME)
1204 : 301167 : *op_p = val;
1205 : : else
1206 : 37240 : *op_p = unshare_expr (val);
1207 : 338407 : }
1208 : :
1209 : :
1210 : : /* Like propagate_tree_value, but use as the operand to replace
1211 : : the principal expression (typically, the RHS) contained in the
1212 : : statement referenced by iterator GSI. Note that it is not
1213 : : always possible to update the statement in-place, so a new
1214 : : statement may be created to replace the original. */
1215 : :
1216 : : void
1217 : 338407 : propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1218 : : {
1219 : 338407 : gimple *stmt = gsi_stmt (*gsi);
1220 : :
1221 : 338407 : if (is_gimple_assign (stmt))
1222 : : {
1223 : 304079 : tree expr = NULL_TREE;
1224 : 304079 : if (gimple_assign_single_p (stmt))
1225 : 158821 : expr = gimple_assign_rhs1 (stmt);
1226 : 304079 : propagate_tree_value (&expr, val);
1227 : 304079 : gimple_assign_set_rhs_from_tree (gsi, expr);
1228 : : }
1229 : 34328 : else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
1230 : : {
1231 : 33536 : tree lhs = NULL_TREE;
1232 : 33536 : tree rhs = build_zero_cst (TREE_TYPE (val));
1233 : 33536 : propagate_tree_value (&lhs, val);
1234 : 33536 : gimple_cond_set_code (cond_stmt, NE_EXPR);
1235 : 33536 : gimple_cond_set_lhs (cond_stmt, lhs);
1236 : 33536 : gimple_cond_set_rhs (cond_stmt, rhs);
1237 : : }
1238 : 792 : else if (is_gimple_call (stmt)
1239 : 792 : && gimple_call_lhs (stmt) != NULL_TREE)
1240 : : {
1241 : 792 : tree expr = NULL_TREE;
1242 : 792 : propagate_tree_value (&expr, val);
1243 : 792 : replace_call_with_value (gsi, expr);
1244 : : }
1245 : 0 : else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1246 : 0 : propagate_tree_value (gimple_switch_index_ptr (swtch_stmt), val);
1247 : : else
1248 : 0 : gcc_unreachable ();
1249 : 338407 : }
1250 : :
1251 : : /* Check exits of each loop in FUN, walk over loop closed PHIs in
1252 : : each exit basic block and propagate degenerate PHIs. */
1253 : :
1254 : : unsigned
1255 : 227811 : clean_up_loop_closed_phi (function *fun)
1256 : : {
1257 : 227811 : gphi *phi;
1258 : 227811 : tree rhs;
1259 : 227811 : tree lhs;
1260 : 227811 : gphi_iterator gsi;
1261 : :
1262 : : /* Avoid possibly quadratic work when scanning for loop exits across
1263 : : all loops of a nest. */
1264 : 227811 : if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
1265 : : return 0;
1266 : :
1267 : : /* replace_uses_by might purge dead EH edges and we want it to also
1268 : : remove dominated blocks. */
1269 : 227811 : calculate_dominance_info (CDI_DOMINATORS);
1270 : :
1271 : : /* Walk over loop in function. */
1272 : 1269413 : for (auto loop : loops_list (fun, 0))
1273 : : {
1274 : : /* Check each exit edege of loop. */
1275 : 585980 : auto_vec<edge> exits = get_loop_exit_edges (loop);
1276 : 2908155 : for (edge e : exits)
1277 : 2112887 : if (single_pred_p (e->dest))
1278 : : /* Walk over loop-closed PHIs. */
1279 : 2053308 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);)
1280 : : {
1281 : 1102878 : phi = gsi.phi ();
1282 : 1102878 : rhs = gimple_phi_arg_def (phi, 0);
1283 : 1102878 : lhs = gimple_phi_result (phi);
1284 : :
1285 : 2205401 : if (virtual_operand_p (rhs))
1286 : : {
1287 : 580256 : imm_use_iterator iter;
1288 : 580256 : use_operand_p use_p;
1289 : 580256 : gimple *stmt;
1290 : :
1291 : 1360212 : FOR_EACH_IMM_USE_STMT (stmt, iter, lhs)
1292 : 2352208 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1293 : 1366382 : SET_USE (use_p, rhs);
1294 : :
1295 : 580256 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1296 : 48 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
1297 : 580256 : remove_phi_node (&gsi, true);
1298 : : }
1299 : 522622 : else if (may_propagate_copy (lhs, rhs))
1300 : : {
1301 : : /* Dump details. */
1302 : 522586 : if (dump_file && (dump_flags & TDF_DETAILS))
1303 : : {
1304 : 3 : fprintf (dump_file, " Replacing '");
1305 : 3 : print_generic_expr (dump_file, lhs, dump_flags);
1306 : 3 : fprintf (dump_file, "' with '");
1307 : 3 : print_generic_expr (dump_file, rhs, dump_flags);
1308 : 3 : fprintf (dump_file, "'\n");
1309 : : }
1310 : :
1311 : 522586 : replace_uses_by (lhs, rhs);
1312 : 522586 : remove_phi_node (&gsi, true);
1313 : : }
1314 : : else
1315 : 36 : gsi_next (&gsi);
1316 : : }
1317 : 585980 : }
1318 : :
1319 : 227811 : return 0;
1320 : : }
|