* ipa/devirt9.C: Fix previous change.
[official-gcc.git] / gcc / tree-ssa-propagate.c
blobbd33071303b1ec67eb329c1e85baffce3332cdb1
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple-pretty-print.h"
31 #include "dumpfile.h"
32 #include "sbitmap.h"
33 #include "gimple.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimple-ssa.h"
37 #include "tree-cfg.h"
38 #include "tree-phinodes.h"
39 #include "ssa-iterators.h"
40 #include "stringpool.h"
41 #include "tree-ssanames.h"
42 #include "tree-ssa.h"
43 #include "tree-ssa-propagate.h"
44 #include "langhooks.h"
45 #include "vec.h"
46 #include "value-prof.h"
48 /* This file implements a generic value propagation engine based on
49 the same propagation used by the SSA-CCP algorithm [1].
51 Propagation is performed by simulating the execution of every
52 statement that produces the value being propagated. Simulation
53 proceeds as follows:
55 1- Initially, all edges of the CFG are marked not executable and
56 the CFG worklist is seeded with all the statements in the entry
57 basic block (block 0).
59 2- Every statement S is simulated with a call to the call-back
60 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
61 results:
63 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
64 interest and does not affect any of the work lists.
66 SSA_PROP_VARYING: The value produced by S cannot be determined
67 at compile time. Further simulation of S is not required.
68 If S is a conditional jump, all the outgoing edges for the
69 block are considered executable and added to the work
70 list.
72 SSA_PROP_INTERESTING: S produces a value that can be computed
73 at compile time. Its result can be propagated into the
74 statements that feed from S. Furthermore, if S is a
75 conditional jump, only the edge known to be taken is added
76 to the work list. Edges that are known not to execute are
77 never simulated.
79 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
80 return value from SSA_PROP_VISIT_PHI has the same semantics as
81 described in #2.
83 4- Three work lists are kept. Statements are only added to these
84 lists if they produce one of SSA_PROP_INTERESTING or
85 SSA_PROP_VARYING.
87 CFG_BLOCKS contains the list of blocks to be simulated.
88 Blocks are added to this list if their incoming edges are
89 found executable.
91 VARYING_SSA_EDGES contains the list of statements that feed
92 from statements that produce an SSA_PROP_VARYING result.
93 These are simulated first to speed up processing.
95 INTERESTING_SSA_EDGES contains the list of statements that
96 feed from statements that produce an SSA_PROP_INTERESTING
97 result.
99 5- Simulation terminates when all three work lists are drained.
101 Before calling ssa_propagate, it is important to clear
102 prop_simulate_again_p for all the statements in the program that
103 should be simulated. This initialization allows an implementation
104 to specify which statements should never be simulated.
106 It is also important to compute def-use information before calling
107 ssa_propagate.
109 References:
111 [1] Constant propagation with conditional branches,
112 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
114 [2] Building an Optimizing Compiler,
115 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
117 [3] Advanced Compiler Design and Implementation,
118 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
120 /* Function pointers used to parameterize the propagation engine. */
121 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
122 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
124 /* Keep track of statements that have been added to one of the SSA
125 edges worklists. This flag is used to avoid visiting statements
126 unnecessarily when draining an SSA edge worklist. If while
127 simulating a basic block, we find a statement with
128 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
129 processing from visiting it again.
131 NOTE: users of the propagation engine are not allowed to use
132 the GF_PLF_1 flag. */
133 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
135 /* A bitmap to keep track of executable blocks in the CFG. */
136 static sbitmap executable_blocks;
138 /* Array of control flow edges on the worklist. */
139 static vec<basic_block> cfg_blocks;
141 static unsigned int cfg_blocks_num = 0;
142 static int cfg_blocks_tail;
143 static int cfg_blocks_head;
145 static sbitmap bb_in_list;
147 /* Worklist of SSA edges which will need reexamination as their
148 definition has changed. SSA edges are def-use edges in the SSA
149 web. For each D-U edge, we store the target statement or PHI node
150 U. */
151 static GTY(()) vec<gimple, va_gc> *interesting_ssa_edges;
153 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
154 list of SSA edges is split into two. One contains all SSA edges
155 who need to be reexamined because their lattice value changed to
156 varying (this worklist), and the other contains all other SSA edges
157 to be reexamined (INTERESTING_SSA_EDGES).
159 Since most values in the program are VARYING, the ideal situation
160 is to move them to that lattice value as quickly as possible.
161 Thus, it doesn't make sense to process any other type of lattice
162 value until all VARYING values are propagated fully, which is one
163 thing using the VARYING worklist achieves. In addition, if we
164 don't use a separate worklist for VARYING edges, we end up with
165 situations where lattice values move from
166 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
167 static GTY(()) vec<gimple, va_gc> *varying_ssa_edges;
170 /* Return true if the block worklist empty. */
172 static inline bool
173 cfg_blocks_empty_p (void)
175 return (cfg_blocks_num == 0);
179 /* Add a basic block to the worklist. The block must not be already
180 in the worklist, and it must not be the ENTRY or EXIT block. */
182 static void
183 cfg_blocks_add (basic_block bb)
185 bool head = false;
187 gcc_assert (bb != ENTRY_BLOCK_PTR && bb != EXIT_BLOCK_PTR);
188 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
190 if (cfg_blocks_empty_p ())
192 cfg_blocks_tail = cfg_blocks_head = 0;
193 cfg_blocks_num = 1;
195 else
197 cfg_blocks_num++;
198 if (cfg_blocks_num > cfg_blocks.length ())
200 /* We have to grow the array now. Adjust to queue to occupy
201 the full space of the original array. We do not need to
202 initialize the newly allocated portion of the array
203 because we keep track of CFG_BLOCKS_HEAD and
204 CFG_BLOCKS_HEAD. */
205 cfg_blocks_tail = cfg_blocks.length ();
206 cfg_blocks_head = 0;
207 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
209 /* Minor optimization: we prefer to see blocks with more
210 predecessors later, because there is more of a chance that
211 the incoming edges will be executable. */
212 else if (EDGE_COUNT (bb->preds)
213 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
214 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
215 else
217 if (cfg_blocks_head == 0)
218 cfg_blocks_head = cfg_blocks.length ();
219 --cfg_blocks_head;
220 head = true;
224 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
225 bitmap_set_bit (bb_in_list, bb->index);
229 /* Remove a block from the worklist. */
231 static basic_block
232 cfg_blocks_get (void)
234 basic_block bb;
236 bb = cfg_blocks[cfg_blocks_head];
238 gcc_assert (!cfg_blocks_empty_p ());
239 gcc_assert (bb);
241 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
242 --cfg_blocks_num;
243 bitmap_clear_bit (bb_in_list, bb->index);
245 return bb;
249 /* We have just defined a new value for VAR. If IS_VARYING is true,
250 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
251 them to INTERESTING_SSA_EDGES. */
253 static void
254 add_ssa_edge (tree var, bool is_varying)
256 imm_use_iterator iter;
257 use_operand_p use_p;
259 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
261 gimple use_stmt = USE_STMT (use_p);
263 if (prop_simulate_again_p (use_stmt)
264 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
266 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
267 if (is_varying)
268 vec_safe_push (varying_ssa_edges, use_stmt);
269 else
270 vec_safe_push (interesting_ssa_edges, use_stmt);
276 /* Add edge E to the control flow worklist. */
278 static void
279 add_control_edge (edge e)
281 basic_block bb = e->dest;
282 if (bb == EXIT_BLOCK_PTR)
283 return;
285 /* If the edge had already been executed, skip it. */
286 if (e->flags & EDGE_EXECUTABLE)
287 return;
289 e->flags |= EDGE_EXECUTABLE;
291 /* If the block is already in the list, we're done. */
292 if (bitmap_bit_p (bb_in_list, bb->index))
293 return;
295 cfg_blocks_add (bb);
297 if (dump_file && (dump_flags & TDF_DETAILS))
298 fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
299 e->src->index, e->dest->index);
303 /* Simulate the execution of STMT and update the work lists accordingly. */
305 static void
306 simulate_stmt (gimple stmt)
308 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
309 edge taken_edge = NULL;
310 tree output_name = NULL_TREE;
312 /* Don't bother visiting statements that are already
313 considered varying by the propagator. */
314 if (!prop_simulate_again_p (stmt))
315 return;
317 if (gimple_code (stmt) == GIMPLE_PHI)
319 val = ssa_prop_visit_phi (stmt);
320 output_name = gimple_phi_result (stmt);
322 else
323 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
325 if (val == SSA_PROP_VARYING)
327 prop_set_simulate_again (stmt, false);
329 /* If the statement produced a new varying value, add the SSA
330 edges coming out of OUTPUT_NAME. */
331 if (output_name)
332 add_ssa_edge (output_name, true);
334 /* If STMT transfers control out of its basic block, add
335 all outgoing edges to the work list. */
336 if (stmt_ends_bb_p (stmt))
338 edge e;
339 edge_iterator ei;
340 basic_block bb = gimple_bb (stmt);
341 FOR_EACH_EDGE (e, ei, bb->succs)
342 add_control_edge (e);
345 else if (val == SSA_PROP_INTERESTING)
347 /* If the statement produced new value, add the SSA edges coming
348 out of OUTPUT_NAME. */
349 if (output_name)
350 add_ssa_edge (output_name, false);
352 /* If we know which edge is going to be taken out of this block,
353 add it to the CFG work list. */
354 if (taken_edge)
355 add_control_edge (taken_edge);
359 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
360 drain. This pops statements off the given WORKLIST and processes
361 them until there are no more statements on WORKLIST.
362 We take a pointer to WORKLIST because it may be reallocated when an
363 SSA edge is added to it in simulate_stmt. */
365 static void
366 process_ssa_edge_worklist (vec<gimple, va_gc> **worklist)
368 /* Drain the entire worklist. */
369 while ((*worklist)->length () > 0)
371 basic_block bb;
373 /* Pull the statement to simulate off the worklist. */
374 gimple stmt = (*worklist)->pop ();
376 /* If this statement was already visited by simulate_block, then
377 we don't need to visit it again here. */
378 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
379 continue;
381 /* STMT is no longer in a worklist. */
382 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
384 if (dump_file && (dump_flags & TDF_DETAILS))
386 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
387 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
390 bb = gimple_bb (stmt);
392 /* PHI nodes are always visited, regardless of whether or not
393 the destination block is executable. Otherwise, visit the
394 statement only if its block is marked executable. */
395 if (gimple_code (stmt) == GIMPLE_PHI
396 || bitmap_bit_p (executable_blocks, bb->index))
397 simulate_stmt (stmt);
402 /* Simulate the execution of BLOCK. Evaluate the statement associated
403 with each variable reference inside the block. */
405 static void
406 simulate_block (basic_block block)
408 gimple_stmt_iterator gsi;
410 /* There is nothing to do for the exit block. */
411 if (block == EXIT_BLOCK_PTR)
412 return;
414 if (dump_file && (dump_flags & TDF_DETAILS))
415 fprintf (dump_file, "\nSimulating block %d\n", block->index);
417 /* Always simulate PHI nodes, even if we have simulated this block
418 before. */
419 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
420 simulate_stmt (gsi_stmt (gsi));
422 /* If this is the first time we've simulated this block, then we
423 must simulate each of its statements. */
424 if (!bitmap_bit_p (executable_blocks, block->index))
426 gimple_stmt_iterator j;
427 unsigned int normal_edge_count;
428 edge e, normal_edge;
429 edge_iterator ei;
431 /* Note that we have simulated this block. */
432 bitmap_set_bit (executable_blocks, block->index);
434 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
436 gimple stmt = gsi_stmt (j);
438 /* If this statement is already in the worklist then
439 "cancel" it. The reevaluation implied by the worklist
440 entry will produce the same value we generate here and
441 thus reevaluating it again from the worklist is
442 pointless. */
443 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
444 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
446 simulate_stmt (stmt);
449 /* We can not predict when abnormal and EH edges will be executed, so
450 once a block is considered executable, we consider any
451 outgoing abnormal edges as executable.
453 TODO: This is not exactly true. Simplifying statement might
454 prove it non-throwing and also computed goto can be handled
455 when destination is known.
457 At the same time, if this block has only one successor that is
458 reached by non-abnormal edges, then add that successor to the
459 worklist. */
460 normal_edge_count = 0;
461 normal_edge = NULL;
462 FOR_EACH_EDGE (e, ei, block->succs)
464 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
465 add_control_edge (e);
466 else
468 normal_edge_count++;
469 normal_edge = e;
473 if (normal_edge_count == 1)
474 add_control_edge (normal_edge);
479 /* Initialize local data structures and work lists. */
481 static void
482 ssa_prop_init (void)
484 edge e;
485 edge_iterator ei;
486 basic_block bb;
488 /* Worklists of SSA edges. */
489 vec_alloc (interesting_ssa_edges, 20);
490 vec_alloc (varying_ssa_edges, 20);
492 executable_blocks = sbitmap_alloc (last_basic_block);
493 bitmap_clear (executable_blocks);
495 bb_in_list = sbitmap_alloc (last_basic_block);
496 bitmap_clear (bb_in_list);
498 if (dump_file && (dump_flags & TDF_DETAILS))
499 dump_immediate_uses (dump_file);
501 cfg_blocks.create (20);
502 cfg_blocks.safe_grow_cleared (20);
504 /* Initially assume that every edge in the CFG is not executable.
505 (including the edges coming out of ENTRY_BLOCK_PTR). */
506 FOR_ALL_BB (bb)
508 gimple_stmt_iterator si;
510 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
511 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
513 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
514 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
516 FOR_EACH_EDGE (e, ei, bb->succs)
517 e->flags &= ~EDGE_EXECUTABLE;
520 /* Seed the algorithm by adding the successors of the entry block to the
521 edge worklist. */
522 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
523 add_control_edge (e);
527 /* Free allocated storage. */
529 static void
530 ssa_prop_fini (void)
532 vec_free (interesting_ssa_edges);
533 vec_free (varying_ssa_edges);
534 cfg_blocks.release ();
535 sbitmap_free (bb_in_list);
536 sbitmap_free (executable_blocks);
540 /* Return true if EXPR is an acceptable right-hand-side for a
541 GIMPLE assignment. We validate the entire tree, not just
542 the root node, thus catching expressions that embed complex
543 operands that are not permitted in GIMPLE. This function
544 is needed because the folding routines in fold-const.c
545 may return such expressions in some cases, e.g., an array
546 access with an embedded index addition. It may make more
547 sense to have folding routines that are sensitive to the
548 constraints on GIMPLE operands, rather than abandoning any
549 any attempt to fold if the usual folding turns out to be too
550 aggressive. */
552 bool
553 valid_gimple_rhs_p (tree expr)
555 enum tree_code code = TREE_CODE (expr);
557 switch (TREE_CODE_CLASS (code))
559 case tcc_declaration:
560 if (!is_gimple_variable (expr))
561 return false;
562 break;
564 case tcc_constant:
565 /* All constants are ok. */
566 break;
568 case tcc_binary:
569 case tcc_comparison:
570 if (!is_gimple_val (TREE_OPERAND (expr, 0))
571 || !is_gimple_val (TREE_OPERAND (expr, 1)))
572 return false;
573 break;
575 case tcc_unary:
576 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
577 return false;
578 break;
580 case tcc_expression:
581 switch (code)
583 case ADDR_EXPR:
585 tree t;
586 if (is_gimple_min_invariant (expr))
587 return true;
588 t = TREE_OPERAND (expr, 0);
589 while (handled_component_p (t))
591 /* ??? More checks needed, see the GIMPLE verifier. */
592 if ((TREE_CODE (t) == ARRAY_REF
593 || TREE_CODE (t) == ARRAY_RANGE_REF)
594 && !is_gimple_val (TREE_OPERAND (t, 1)))
595 return false;
596 t = TREE_OPERAND (t, 0);
598 if (!is_gimple_id (t))
599 return false;
601 break;
603 default:
604 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
606 if (((code == VEC_COND_EXPR || code == COND_EXPR)
607 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
608 : !is_gimple_val (TREE_OPERAND (expr, 0)))
609 || !is_gimple_val (TREE_OPERAND (expr, 1))
610 || !is_gimple_val (TREE_OPERAND (expr, 2)))
611 return false;
612 break;
614 return false;
616 break;
618 case tcc_vl_exp:
619 return false;
621 case tcc_exceptional:
622 if (code == CONSTRUCTOR)
624 unsigned i;
625 tree elt;
626 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
627 if (!is_gimple_val (elt))
628 return false;
629 return true;
631 if (code != SSA_NAME)
632 return false;
633 break;
635 case tcc_reference:
636 if (code == BIT_FIELD_REF)
637 return is_gimple_val (TREE_OPERAND (expr, 0));
638 return false;
640 default:
641 return false;
644 return true;
648 /* Return true if EXPR is a CALL_EXPR suitable for representation
649 as a single GIMPLE_CALL statement. If the arguments require
650 further gimplification, return false. */
652 static bool
653 valid_gimple_call_p (tree expr)
655 unsigned i, nargs;
657 if (TREE_CODE (expr) != CALL_EXPR)
658 return false;
660 nargs = call_expr_nargs (expr);
661 for (i = 0; i < nargs; i++)
663 tree arg = CALL_EXPR_ARG (expr, i);
664 if (is_gimple_reg_type (arg))
666 if (!is_gimple_val (arg))
667 return false;
669 else
670 if (!is_gimple_lvalue (arg))
671 return false;
674 return true;
678 /* Make SSA names defined by OLD_STMT point to NEW_STMT
679 as their defining statement. */
681 void
682 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
684 tree var;
685 ssa_op_iter iter;
687 if (gimple_in_ssa_p (cfun))
689 /* Make defined SSA_NAMEs point to the new
690 statement as their definition. */
691 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
693 if (TREE_CODE (var) == SSA_NAME)
694 SSA_NAME_DEF_STMT (var) = new_stmt;
699 /* Helper function for update_gimple_call and update_call_from_tree.
700 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
702 static void
703 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
704 gimple stmt)
706 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
707 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
708 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
709 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
710 gimple_set_location (new_stmt, gimple_location (stmt));
711 if (gimple_block (new_stmt) == NULL_TREE)
712 gimple_set_block (new_stmt, gimple_block (stmt));
713 gsi_replace (si_p, new_stmt, false);
716 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
717 with number of arguments NARGS, where the arguments in GIMPLE form
718 follow NARGS argument. */
720 bool
721 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
723 va_list ap;
724 gimple new_stmt, stmt = gsi_stmt (*si_p);
726 gcc_assert (is_gimple_call (stmt));
727 va_start (ap, nargs);
728 new_stmt = gimple_build_call_valist (fn, nargs, ap);
729 finish_update_gimple_call (si_p, new_stmt, stmt);
730 va_end (ap);
731 return true;
734 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
735 value of EXPR, which is expected to be the result of folding the
736 call. This can only be done if EXPR is a CALL_EXPR with valid
737 GIMPLE operands as arguments, or if it is a suitable RHS expression
738 for a GIMPLE_ASSIGN. More complex expressions will require
739 gimplification, which will introduce additional statements. In this
740 event, no update is performed, and the function returns false.
741 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
742 replace the statement at *SI_P with an entirely new statement.
743 The new statement need not be a call, e.g., if the original call
744 folded to a constant. */
746 bool
747 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
749 gimple stmt = gsi_stmt (*si_p);
751 if (valid_gimple_call_p (expr))
753 /* The call has simplified to another call. */
754 tree fn = CALL_EXPR_FN (expr);
755 unsigned i;
756 unsigned nargs = call_expr_nargs (expr);
757 vec<tree> args = vNULL;
758 gimple new_stmt;
760 if (nargs > 0)
762 args.create (nargs);
763 args.safe_grow_cleared (nargs);
765 for (i = 0; i < nargs; i++)
766 args[i] = CALL_EXPR_ARG (expr, i);
769 new_stmt = gimple_build_call_vec (fn, args);
770 finish_update_gimple_call (si_p, new_stmt, stmt);
771 args.release ();
773 return true;
775 else if (valid_gimple_rhs_p (expr))
777 tree lhs = gimple_call_lhs (stmt);
778 gimple new_stmt;
780 /* The call has simplified to an expression
781 that cannot be represented as a GIMPLE_CALL. */
782 if (lhs)
784 /* A value is expected.
785 Introduce a new GIMPLE_ASSIGN statement. */
786 STRIP_USELESS_TYPE_CONVERSION (expr);
787 new_stmt = gimple_build_assign (lhs, expr);
788 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
789 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
790 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
792 else if (!TREE_SIDE_EFFECTS (expr))
794 /* No value is expected, and EXPR has no effect.
795 Replace it with an empty statement. */
796 new_stmt = gimple_build_nop ();
797 if (gimple_in_ssa_p (cfun))
799 unlink_stmt_vdef (stmt);
800 release_defs (stmt);
803 else
805 /* No value is expected, but EXPR has an effect,
806 e.g., it could be a reference to a volatile
807 variable. Create an assignment statement
808 with a dummy (unused) lhs variable. */
809 STRIP_USELESS_TYPE_CONVERSION (expr);
810 if (gimple_in_ssa_p (cfun))
811 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
812 else
813 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
814 new_stmt = gimple_build_assign (lhs, expr);
815 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
816 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
817 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
819 gimple_set_location (new_stmt, gimple_location (stmt));
820 gsi_replace (si_p, new_stmt, false);
821 return true;
823 else
824 /* The call simplified to an expression that is
825 not a valid GIMPLE RHS. */
826 return false;
830 /* Entry point to the propagation engine.
832 VISIT_STMT is called for every statement visited.
833 VISIT_PHI is called for every PHI node visited. */
835 void
836 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
837 ssa_prop_visit_phi_fn visit_phi)
839 ssa_prop_visit_stmt = visit_stmt;
840 ssa_prop_visit_phi = visit_phi;
842 ssa_prop_init ();
844 /* Iterate until the worklists are empty. */
845 while (!cfg_blocks_empty_p ()
846 || interesting_ssa_edges->length () > 0
847 || varying_ssa_edges->length () > 0)
849 if (!cfg_blocks_empty_p ())
851 /* Pull the next block to simulate off the worklist. */
852 basic_block dest_block = cfg_blocks_get ();
853 simulate_block (dest_block);
856 /* In order to move things to varying as quickly as
857 possible,process the VARYING_SSA_EDGES worklist first. */
858 process_ssa_edge_worklist (&varying_ssa_edges);
860 /* Now process the INTERESTING_SSA_EDGES worklist. */
861 process_ssa_edge_worklist (&interesting_ssa_edges);
864 ssa_prop_fini ();
868 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
869 is a non-volatile pointer dereference, a structure reference or a
870 reference to a single _DECL. Ignore volatile memory references
871 because they are not interesting for the optimizers. */
873 bool
874 stmt_makes_single_store (gimple stmt)
876 tree lhs;
878 if (gimple_code (stmt) != GIMPLE_ASSIGN
879 && gimple_code (stmt) != GIMPLE_CALL)
880 return false;
882 if (!gimple_vdef (stmt))
883 return false;
885 lhs = gimple_get_lhs (stmt);
887 /* A call statement may have a null LHS. */
888 if (!lhs)
889 return false;
891 return (!TREE_THIS_VOLATILE (lhs)
892 && (DECL_P (lhs)
893 || REFERENCE_CLASS_P (lhs)));
897 /* Propagation statistics. */
898 struct prop_stats_d
900 long num_const_prop;
901 long num_copy_prop;
902 long num_stmts_folded;
903 long num_dce;
906 static struct prop_stats_d prop_stats;
908 /* Replace USE references in statement STMT with the values stored in
909 PROP_VALUE. Return true if at least one reference was replaced. */
911 static bool
912 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
914 bool replaced = false;
915 use_operand_p use;
916 ssa_op_iter iter;
918 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
920 tree tuse = USE_FROM_PTR (use);
921 tree val = (*get_value) (tuse);
923 if (val == tuse || val == NULL_TREE)
924 continue;
926 if (gimple_code (stmt) == GIMPLE_ASM
927 && !may_propagate_copy_into_asm (tuse))
928 continue;
930 if (!may_propagate_copy (tuse, val))
931 continue;
933 if (TREE_CODE (val) != SSA_NAME)
934 prop_stats.num_const_prop++;
935 else
936 prop_stats.num_copy_prop++;
938 propagate_value (use, val);
940 replaced = true;
943 return replaced;
947 /* Replace propagated values into all the arguments for PHI using the
948 values from PROP_VALUE. */
950 static void
951 replace_phi_args_in (gimple phi, ssa_prop_get_value_fn get_value)
953 size_t i;
954 bool replaced = false;
956 if (dump_file && (dump_flags & TDF_DETAILS))
958 fprintf (dump_file, "Folding PHI node: ");
959 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
962 for (i = 0; i < gimple_phi_num_args (phi); i++)
964 tree arg = gimple_phi_arg_def (phi, i);
966 if (TREE_CODE (arg) == SSA_NAME)
968 tree val = (*get_value) (arg);
970 if (val && val != arg && may_propagate_copy (arg, val))
972 if (TREE_CODE (val) != SSA_NAME)
973 prop_stats.num_const_prop++;
974 else
975 prop_stats.num_copy_prop++;
977 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
978 replaced = true;
980 /* If we propagated a copy and this argument flows
981 through an abnormal edge, update the replacement
982 accordingly. */
983 if (TREE_CODE (val) == SSA_NAME
984 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
985 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
990 if (dump_file && (dump_flags & TDF_DETAILS))
992 if (!replaced)
993 fprintf (dump_file, "No folding possible\n");
994 else
996 fprintf (dump_file, "Folded into: ");
997 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
998 fprintf (dump_file, "\n");
1004 /* Perform final substitution and folding of propagated values.
1006 PROP_VALUE[I] contains the single value that should be substituted
1007 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1008 substituted.
1010 If FOLD_FN is non-NULL the function will be invoked on all statements
1011 before propagating values for pass specific simplification.
1013 DO_DCE is true if trivially dead stmts can be removed.
1015 If DO_DCE is true, the statements within a BB are walked from
1016 last to first element. Otherwise we scan from first to last element.
1018 Return TRUE when something changed. */
1020 bool
1021 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1022 ssa_prop_fold_stmt_fn fold_fn,
1023 bool do_dce)
1025 basic_block bb;
1026 bool something_changed = false;
1027 unsigned i;
1029 if (!get_value_fn && !fold_fn)
1030 return false;
1032 if (dump_file && (dump_flags & TDF_DETAILS))
1033 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1035 memset (&prop_stats, 0, sizeof (prop_stats));
1037 /* Substitute lattice values at definition sites. */
1038 if (get_value_fn)
1039 for (i = 1; i < num_ssa_names; ++i)
1041 tree name = ssa_name (i);
1042 tree val;
1043 gimple def_stmt;
1044 gimple_stmt_iterator gsi;
1046 if (!name
1047 || virtual_operand_p (name))
1048 continue;
1050 def_stmt = SSA_NAME_DEF_STMT (name);
1051 if (gimple_nop_p (def_stmt)
1052 /* Do not substitute ASSERT_EXPR rhs, this will confuse VRP. */
1053 || (gimple_assign_single_p (def_stmt)
1054 && gimple_assign_rhs_code (def_stmt) == ASSERT_EXPR)
1055 || !(val = (*get_value_fn) (name))
1056 || !may_propagate_copy (name, val))
1057 continue;
1059 gsi = gsi_for_stmt (def_stmt);
1060 if (is_gimple_assign (def_stmt))
1062 gimple_assign_set_rhs_with_ops (&gsi, TREE_CODE (val),
1063 val, NULL_TREE);
1064 gcc_assert (gsi_stmt (gsi) == def_stmt);
1065 if (maybe_clean_eh_stmt (def_stmt))
1066 gimple_purge_dead_eh_edges (gimple_bb (def_stmt));
1067 update_stmt (def_stmt);
1069 else if (is_gimple_call (def_stmt))
1071 int flags = gimple_call_flags (def_stmt);
1073 /* Don't optimize away calls that have side-effects. */
1074 if ((flags & (ECF_CONST|ECF_PURE)) == 0
1075 || (flags & ECF_LOOPING_CONST_OR_PURE))
1076 continue;
1077 if (update_call_from_tree (&gsi, val)
1078 && maybe_clean_or_replace_eh_stmt (def_stmt, gsi_stmt (gsi)))
1079 gimple_purge_dead_eh_edges (gimple_bb (gsi_stmt (gsi)));
1081 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1083 gimple new_stmt = gimple_build_assign (name, val);
1084 gimple_stmt_iterator gsi2;
1085 gsi2 = gsi_after_labels (gimple_bb (def_stmt));
1086 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
1087 remove_phi_node (&gsi, false);
1090 something_changed = true;
1093 /* Propagate into all uses and fold. */
1094 FOR_EACH_BB (bb)
1096 gimple_stmt_iterator i;
1098 /* Propagate known values into PHI nodes. */
1099 if (get_value_fn)
1100 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
1101 replace_phi_args_in (gsi_stmt (i), get_value_fn);
1103 /* Propagate known values into stmts. Do a backward walk if
1104 do_dce is true. In some case it exposes
1105 more trivially deletable stmts to walk backward. */
1106 for (i = (do_dce ? gsi_last_bb (bb) : gsi_start_bb (bb)); !gsi_end_p (i);)
1108 bool did_replace;
1109 gimple stmt = gsi_stmt (i);
1110 gimple old_stmt;
1111 enum gimple_code code = gimple_code (stmt);
1112 gimple_stmt_iterator oldi;
1114 oldi = i;
1115 if (do_dce)
1116 gsi_prev (&i);
1117 else
1118 gsi_next (&i);
1120 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1121 range information for names and they are discarded
1122 afterwards. */
1124 if (code == GIMPLE_ASSIGN
1125 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1126 continue;
1128 /* No point propagating into a stmt whose result is not used,
1129 but instead we might be able to remove a trivially dead stmt.
1130 Don't do this when called from VRP, since the SSA_NAME which
1131 is going to be released could be still referenced in VRP
1132 ranges. */
1133 if (do_dce
1134 && gimple_get_lhs (stmt)
1135 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1136 && has_zero_uses (gimple_get_lhs (stmt))
1137 && !stmt_could_throw_p (stmt)
1138 && !gimple_has_side_effects (stmt))
1140 gimple_stmt_iterator i2;
1142 if (dump_file && dump_flags & TDF_DETAILS)
1144 fprintf (dump_file, "Removing dead stmt ");
1145 print_gimple_stmt (dump_file, stmt, 0, 0);
1146 fprintf (dump_file, "\n");
1148 prop_stats.num_dce++;
1149 i2 = gsi_for_stmt (stmt);
1150 gsi_remove (&i2, true);
1151 release_defs (stmt);
1152 continue;
1155 /* Replace the statement with its folded version and mark it
1156 folded. */
1157 did_replace = false;
1158 if (dump_file && (dump_flags & TDF_DETAILS))
1160 fprintf (dump_file, "Folding statement: ");
1161 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1164 old_stmt = stmt;
1166 /* Some statements may be simplified using propagator
1167 specific information. Do this before propagating
1168 into the stmt to not disturb pass specific information. */
1169 if (fold_fn
1170 && (*fold_fn)(&oldi))
1172 did_replace = true;
1173 prop_stats.num_stmts_folded++;
1174 stmt = gsi_stmt (oldi);
1175 update_stmt (stmt);
1178 /* Replace real uses in the statement. */
1179 if (get_value_fn)
1180 did_replace |= replace_uses_in (stmt, get_value_fn);
1182 /* If we made a replacement, fold the statement. */
1183 if (did_replace)
1184 fold_stmt (&oldi);
1186 /* Now cleanup. */
1187 if (did_replace)
1189 stmt = gsi_stmt (oldi);
1191 /* If we cleaned up EH information from the statement,
1192 remove EH edges. */
1193 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1194 gimple_purge_dead_eh_edges (bb);
1196 if (is_gimple_assign (stmt)
1197 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1198 == GIMPLE_SINGLE_RHS))
1200 tree rhs = gimple_assign_rhs1 (stmt);
1202 if (TREE_CODE (rhs) == ADDR_EXPR)
1203 recompute_tree_invariant_for_addr_expr (rhs);
1206 /* Determine what needs to be done to update the SSA form. */
1207 update_stmt (stmt);
1208 if (!is_gimple_debug (stmt))
1209 something_changed = true;
1212 if (dump_file && (dump_flags & TDF_DETAILS))
1214 if (did_replace)
1216 fprintf (dump_file, "Folded into: ");
1217 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1218 fprintf (dump_file, "\n");
1220 else
1221 fprintf (dump_file, "Not folded\n");
1226 statistics_counter_event (cfun, "Constants propagated",
1227 prop_stats.num_const_prop);
1228 statistics_counter_event (cfun, "Copies propagated",
1229 prop_stats.num_copy_prop);
1230 statistics_counter_event (cfun, "Statements folded",
1231 prop_stats.num_stmts_folded);
1232 statistics_counter_event (cfun, "Statements deleted",
1233 prop_stats.num_dce);
1234 return something_changed;
1238 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1240 bool
1241 may_propagate_copy (tree dest, tree orig)
1243 tree type_d = TREE_TYPE (dest);
1244 tree type_o = TREE_TYPE (orig);
1246 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
1247 if (TREE_CODE (orig) == SSA_NAME
1248 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1249 /* If it is the default definition and an automatic variable then
1250 we can though and it is important that we do to avoid
1251 uninitialized regular copies. */
1252 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
1253 && (SSA_NAME_VAR (orig) == NULL_TREE
1254 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
1255 return false;
1257 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
1258 cannot be replaced. */
1259 if (TREE_CODE (dest) == SSA_NAME
1260 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1261 return false;
1263 /* Do not copy between types for which we *do* need a conversion. */
1264 if (!useless_type_conversion_p (type_d, type_o))
1265 return false;
1267 /* Generally propagating virtual operands is not ok as that may
1268 create overlapping life-ranges. */
1269 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1270 return false;
1272 /* Anything else is OK. */
1273 return true;
1276 /* Like may_propagate_copy, but use as the destination expression
1277 the principal expression (typically, the RHS) contained in
1278 statement DEST. This is more efficient when working with the
1279 gimple tuples representation. */
1281 bool
1282 may_propagate_copy_into_stmt (gimple dest, tree orig)
1284 tree type_d;
1285 tree type_o;
1287 /* If the statement is a switch or a single-rhs assignment,
1288 then the expression to be replaced by the propagation may
1289 be an SSA_NAME. Fortunately, there is an explicit tree
1290 for the expression, so we delegate to may_propagate_copy. */
1292 if (gimple_assign_single_p (dest))
1293 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1294 else if (gimple_code (dest) == GIMPLE_SWITCH)
1295 return may_propagate_copy (gimple_switch_index (dest), orig);
1297 /* In other cases, the expression is not materialized, so there
1298 is no destination to pass to may_propagate_copy. On the other
1299 hand, the expression cannot be an SSA_NAME, so the analysis
1300 is much simpler. */
1302 if (TREE_CODE (orig) == SSA_NAME
1303 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1304 return false;
1306 if (is_gimple_assign (dest))
1307 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1308 else if (gimple_code (dest) == GIMPLE_COND)
1309 type_d = boolean_type_node;
1310 else if (is_gimple_call (dest)
1311 && gimple_call_lhs (dest) != NULL_TREE)
1312 type_d = TREE_TYPE (gimple_call_lhs (dest));
1313 else
1314 gcc_unreachable ();
1316 type_o = TREE_TYPE (orig);
1318 if (!useless_type_conversion_p (type_d, type_o))
1319 return false;
1321 return true;
1324 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1326 bool
1327 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1329 return true;
1333 /* Common code for propagate_value and replace_exp.
1335 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1336 replacement is done to propagate a value or not. */
1338 static void
1339 replace_exp_1 (use_operand_p op_p, tree val,
1340 bool for_propagation ATTRIBUTE_UNUSED)
1342 #if defined ENABLE_CHECKING
1343 tree op = USE_FROM_PTR (op_p);
1345 gcc_assert (!(for_propagation
1346 && TREE_CODE (op) == SSA_NAME
1347 && TREE_CODE (val) == SSA_NAME
1348 && !may_propagate_copy (op, val)));
1349 #endif
1351 if (TREE_CODE (val) == SSA_NAME)
1352 SET_USE (op_p, val);
1353 else
1354 SET_USE (op_p, unshare_expr (val));
1358 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1359 into the operand pointed to by OP_P.
1361 Use this version for const/copy propagation as it will perform additional
1362 checks to ensure validity of the const/copy propagation. */
1364 void
1365 propagate_value (use_operand_p op_p, tree val)
1367 replace_exp_1 (op_p, val, true);
1370 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1372 Use this version when not const/copy propagating values. For example,
1373 PRE uses this version when building expressions as they would appear
1374 in specific blocks taking into account actions of PHI nodes.
1376 The statement in which an expression has been replaced should be
1377 folded using fold_stmt_inplace. */
1379 void
1380 replace_exp (use_operand_p op_p, tree val)
1382 replace_exp_1 (op_p, val, false);
1386 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1387 into the tree pointed to by OP_P.
1389 Use this version for const/copy propagation when SSA operands are not
1390 available. It will perform the additional checks to ensure validity of
1391 the const/copy propagation, but will not update any operand information.
1392 Be sure to mark the stmt as modified. */
1394 void
1395 propagate_tree_value (tree *op_p, tree val)
1397 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
1398 && *op_p
1399 && TREE_CODE (*op_p) == SSA_NAME
1400 && !may_propagate_copy (*op_p, val)));
1402 if (TREE_CODE (val) == SSA_NAME)
1403 *op_p = val;
1404 else
1405 *op_p = unshare_expr (val);
1409 /* Like propagate_tree_value, but use as the operand to replace
1410 the principal expression (typically, the RHS) contained in the
1411 statement referenced by iterator GSI. Note that it is not
1412 always possible to update the statement in-place, so a new
1413 statement may be created to replace the original. */
1415 void
1416 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1418 gimple stmt = gsi_stmt (*gsi);
1420 if (is_gimple_assign (stmt))
1422 tree expr = NULL_TREE;
1423 if (gimple_assign_single_p (stmt))
1424 expr = gimple_assign_rhs1 (stmt);
1425 propagate_tree_value (&expr, val);
1426 gimple_assign_set_rhs_from_tree (gsi, expr);
1428 else if (gimple_code (stmt) == GIMPLE_COND)
1430 tree lhs = NULL_TREE;
1431 tree rhs = build_zero_cst (TREE_TYPE (val));
1432 propagate_tree_value (&lhs, val);
1433 gimple_cond_set_code (stmt, NE_EXPR);
1434 gimple_cond_set_lhs (stmt, lhs);
1435 gimple_cond_set_rhs (stmt, rhs);
1437 else if (is_gimple_call (stmt)
1438 && gimple_call_lhs (stmt) != NULL_TREE)
1440 tree expr = NULL_TREE;
1441 bool res;
1442 propagate_tree_value (&expr, val);
1443 res = update_call_from_tree (gsi, expr);
1444 gcc_assert (res);
1446 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1447 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
1448 else
1449 gcc_unreachable ();
1452 #include "gt-tree-ssa-propagate.h"