1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2015 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
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
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/>. */
23 #include "coretypes.h"
28 #include "double-int.h"
35 #include "fold-const.h"
39 #include "hard-reg-set.h"
42 #include "dominance.h"
44 #include "basic-block.h"
45 #include "gimple-pretty-print.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "gimple-fold.h"
53 #include "gimple-expr.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
60 #include "tree-phinodes.h"
61 #include "ssa-iterators.h"
62 #include "stringpool.h"
63 #include "tree-ssanames.h"
65 #include "tree-ssa-propagate.h"
66 #include "langhooks.h"
67 #include "value-prof.h"
70 /* This file implements a generic value propagation engine based on
71 the same propagation used by the SSA-CCP algorithm [1].
73 Propagation is performed by simulating the execution of every
74 statement that produces the value being propagated. Simulation
77 1- Initially, all edges of the CFG are marked not executable and
78 the CFG worklist is seeded with all the statements in the entry
79 basic block (block 0).
81 2- Every statement S is simulated with a call to the call-back
82 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
85 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
86 interest and does not affect any of the work lists.
88 SSA_PROP_VARYING: The value produced by S cannot be determined
89 at compile time. Further simulation of S is not required.
90 If S is a conditional jump, all the outgoing edges for the
91 block are considered executable and added to the work
94 SSA_PROP_INTERESTING: S produces a value that can be computed
95 at compile time. Its result can be propagated into the
96 statements that feed from S. Furthermore, if S is a
97 conditional jump, only the edge known to be taken is added
98 to the work list. Edges that are known not to execute are
101 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
102 return value from SSA_PROP_VISIT_PHI has the same semantics as
105 4- Three work lists are kept. Statements are only added to these
106 lists if they produce one of SSA_PROP_INTERESTING or
109 CFG_BLOCKS contains the list of blocks to be simulated.
110 Blocks are added to this list if their incoming edges are
113 VARYING_SSA_EDGES contains the list of statements that feed
114 from statements that produce an SSA_PROP_VARYING result.
115 These are simulated first to speed up processing.
117 INTERESTING_SSA_EDGES contains the list of statements that
118 feed from statements that produce an SSA_PROP_INTERESTING
121 5- Simulation terminates when all three work lists are drained.
123 Before calling ssa_propagate, it is important to clear
124 prop_simulate_again_p for all the statements in the program that
125 should be simulated. This initialization allows an implementation
126 to specify which statements should never be simulated.
128 It is also important to compute def-use information before calling
133 [1] Constant propagation with conditional branches,
134 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
136 [2] Building an Optimizing Compiler,
137 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
139 [3] Advanced Compiler Design and Implementation,
140 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
142 /* Function pointers used to parameterize the propagation engine. */
143 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt
;
144 static ssa_prop_visit_phi_fn ssa_prop_visit_phi
;
146 /* Keep track of statements that have been added to one of the SSA
147 edges worklists. This flag is used to avoid visiting statements
148 unnecessarily when draining an SSA edge worklist. If while
149 simulating a basic block, we find a statement with
150 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
151 processing from visiting it again.
153 NOTE: users of the propagation engine are not allowed to use
154 the GF_PLF_1 flag. */
155 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
157 /* A bitmap to keep track of executable blocks in the CFG. */
158 static sbitmap executable_blocks
;
160 /* Array of control flow edges on the worklist. */
161 static vec
<basic_block
> cfg_blocks
;
163 static unsigned int cfg_blocks_num
= 0;
164 static int cfg_blocks_tail
;
165 static int cfg_blocks_head
;
167 static sbitmap bb_in_list
;
169 /* Worklist of SSA edges which will need reexamination as their
170 definition has changed. SSA edges are def-use edges in the SSA
171 web. For each D-U edge, we store the target statement or PHI node
173 static vec
<gimple
> interesting_ssa_edges
;
175 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
176 list of SSA edges is split into two. One contains all SSA edges
177 who need to be reexamined because their lattice value changed to
178 varying (this worklist), and the other contains all other SSA edges
179 to be reexamined (INTERESTING_SSA_EDGES).
181 Since most values in the program are VARYING, the ideal situation
182 is to move them to that lattice value as quickly as possible.
183 Thus, it doesn't make sense to process any other type of lattice
184 value until all VARYING values are propagated fully, which is one
185 thing using the VARYING worklist achieves. In addition, if we
186 don't use a separate worklist for VARYING edges, we end up with
187 situations where lattice values move from
188 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
189 static vec
<gimple
> varying_ssa_edges
;
192 /* Return true if the block worklist empty. */
195 cfg_blocks_empty_p (void)
197 return (cfg_blocks_num
== 0);
201 /* Add a basic block to the worklist. The block must not be already
202 in the worklist, and it must not be the ENTRY or EXIT block. */
205 cfg_blocks_add (basic_block bb
)
209 gcc_assert (bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
210 && bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
211 gcc_assert (!bitmap_bit_p (bb_in_list
, bb
->index
));
213 if (cfg_blocks_empty_p ())
215 cfg_blocks_tail
= cfg_blocks_head
= 0;
221 if (cfg_blocks_num
> cfg_blocks
.length ())
223 /* We have to grow the array now. Adjust to queue to occupy
224 the full space of the original array. We do not need to
225 initialize the newly allocated portion of the array
226 because we keep track of CFG_BLOCKS_HEAD and
228 cfg_blocks_tail
= cfg_blocks
.length ();
230 cfg_blocks
.safe_grow (2 * cfg_blocks_tail
);
232 /* Minor optimization: we prefer to see blocks with more
233 predecessors later, because there is more of a chance that
234 the incoming edges will be executable. */
235 else if (EDGE_COUNT (bb
->preds
)
236 >= EDGE_COUNT (cfg_blocks
[cfg_blocks_head
]->preds
))
237 cfg_blocks_tail
= ((cfg_blocks_tail
+ 1) % cfg_blocks
.length ());
240 if (cfg_blocks_head
== 0)
241 cfg_blocks_head
= cfg_blocks
.length ();
247 cfg_blocks
[head
? cfg_blocks_head
: cfg_blocks_tail
] = bb
;
248 bitmap_set_bit (bb_in_list
, bb
->index
);
252 /* Remove a block from the worklist. */
255 cfg_blocks_get (void)
259 bb
= cfg_blocks
[cfg_blocks_head
];
261 gcc_assert (!cfg_blocks_empty_p ());
264 cfg_blocks_head
= ((cfg_blocks_head
+ 1) % cfg_blocks
.length ());
266 bitmap_clear_bit (bb_in_list
, bb
->index
);
272 /* We have just defined a new value for VAR. If IS_VARYING is true,
273 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
274 them to INTERESTING_SSA_EDGES. */
277 add_ssa_edge (tree var
, bool is_varying
)
279 imm_use_iterator iter
;
282 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
284 gimple use_stmt
= USE_STMT (use_p
);
286 if (prop_simulate_again_p (use_stmt
)
287 && !gimple_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
289 gimple_set_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
, true);
291 varying_ssa_edges
.safe_push (use_stmt
);
293 interesting_ssa_edges
.safe_push (use_stmt
);
299 /* Add edge E to the control flow worklist. */
302 add_control_edge (edge e
)
304 basic_block bb
= e
->dest
;
305 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
308 /* If the edge had already been executed, skip it. */
309 if (e
->flags
& EDGE_EXECUTABLE
)
312 e
->flags
|= EDGE_EXECUTABLE
;
314 /* If the block is already in the list, we're done. */
315 if (bitmap_bit_p (bb_in_list
, bb
->index
))
320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
321 fprintf (dump_file
, "\nAdding Destination of edge (%d -> %d) to worklist\n",
322 e
->src
->index
, e
->dest
->index
);
326 /* Simulate the execution of STMT and update the work lists accordingly. */
329 simulate_stmt (gimple stmt
)
331 enum ssa_prop_result val
= SSA_PROP_NOT_INTERESTING
;
332 edge taken_edge
= NULL
;
333 tree output_name
= NULL_TREE
;
335 /* Don't bother visiting statements that are already
336 considered varying by the propagator. */
337 if (!prop_simulate_again_p (stmt
))
340 if (gimple_code (stmt
) == GIMPLE_PHI
)
342 val
= ssa_prop_visit_phi (as_a
<gphi
*> (stmt
));
343 output_name
= gimple_phi_result (stmt
);
346 val
= ssa_prop_visit_stmt (stmt
, &taken_edge
, &output_name
);
348 if (val
== SSA_PROP_VARYING
)
350 prop_set_simulate_again (stmt
, false);
352 /* If the statement produced a new varying value, add the SSA
353 edges coming out of OUTPUT_NAME. */
355 add_ssa_edge (output_name
, true);
357 /* If STMT transfers control out of its basic block, add
358 all outgoing edges to the work list. */
359 if (stmt_ends_bb_p (stmt
))
363 basic_block bb
= gimple_bb (stmt
);
364 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
365 add_control_edge (e
);
368 else if (val
== SSA_PROP_INTERESTING
)
370 /* If the statement produced new value, add the SSA edges coming
371 out of OUTPUT_NAME. */
373 add_ssa_edge (output_name
, false);
375 /* If we know which edge is going to be taken out of this block,
376 add it to the CFG work list. */
378 add_control_edge (taken_edge
);
382 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
383 drain. This pops statements off the given WORKLIST and processes
384 them until there are no more statements on WORKLIST.
385 We take a pointer to WORKLIST because it may be reallocated when an
386 SSA edge is added to it in simulate_stmt. */
389 process_ssa_edge_worklist (vec
<gimple
> *worklist
)
391 /* Drain the entire worklist. */
392 while (worklist
->length () > 0)
396 /* Pull the statement to simulate off the worklist. */
397 gimple stmt
= worklist
->pop ();
399 /* If this statement was already visited by simulate_block, then
400 we don't need to visit it again here. */
401 if (!gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
404 /* STMT is no longer in a worklist. */
405 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
407 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
409 fprintf (dump_file
, "\nSimulating statement (from ssa_edges): ");
410 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
413 bb
= gimple_bb (stmt
);
415 /* PHI nodes are always visited, regardless of whether or not
416 the destination block is executable. Otherwise, visit the
417 statement only if its block is marked executable. */
418 if (gimple_code (stmt
) == GIMPLE_PHI
419 || bitmap_bit_p (executable_blocks
, bb
->index
))
420 simulate_stmt (stmt
);
425 /* Simulate the execution of BLOCK. Evaluate the statement associated
426 with each variable reference inside the block. */
429 simulate_block (basic_block block
)
431 gimple_stmt_iterator gsi
;
433 /* There is nothing to do for the exit block. */
434 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
438 fprintf (dump_file
, "\nSimulating block %d\n", block
->index
);
440 /* Always simulate PHI nodes, even if we have simulated this block
442 for (gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
443 simulate_stmt (gsi_stmt (gsi
));
445 /* If this is the first time we've simulated this block, then we
446 must simulate each of its statements. */
447 if (!bitmap_bit_p (executable_blocks
, block
->index
))
449 gimple_stmt_iterator j
;
450 unsigned int normal_edge_count
;
454 /* Note that we have simulated this block. */
455 bitmap_set_bit (executable_blocks
, block
->index
);
457 for (j
= gsi_start_bb (block
); !gsi_end_p (j
); gsi_next (&j
))
459 gimple stmt
= gsi_stmt (j
);
461 /* If this statement is already in the worklist then
462 "cancel" it. The reevaluation implied by the worklist
463 entry will produce the same value we generate here and
464 thus reevaluating it again from the worklist is
466 if (gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
467 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
469 simulate_stmt (stmt
);
472 /* We can not predict when abnormal and EH edges will be executed, so
473 once a block is considered executable, we consider any
474 outgoing abnormal edges as executable.
476 TODO: This is not exactly true. Simplifying statement might
477 prove it non-throwing and also computed goto can be handled
478 when destination is known.
480 At the same time, if this block has only one successor that is
481 reached by non-abnormal edges, then add that successor to the
483 normal_edge_count
= 0;
485 FOR_EACH_EDGE (e
, ei
, block
->succs
)
487 if (e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
))
488 add_control_edge (e
);
496 if (normal_edge_count
== 1)
497 add_control_edge (normal_edge
);
502 /* Initialize local data structures and work lists. */
511 /* Worklists of SSA edges. */
512 interesting_ssa_edges
.create (20);
513 varying_ssa_edges
.create (20);
515 executable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
516 bitmap_clear (executable_blocks
);
518 bb_in_list
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
519 bitmap_clear (bb_in_list
);
521 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
522 dump_immediate_uses (dump_file
);
524 cfg_blocks
.create (20);
525 cfg_blocks
.safe_grow_cleared (20);
527 /* Initially assume that every edge in the CFG is not executable.
528 (including the edges coming out of the entry block). */
529 FOR_ALL_BB_FN (bb
, cfun
)
531 gimple_stmt_iterator si
;
533 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
534 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
536 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
537 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
539 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
540 e
->flags
&= ~EDGE_EXECUTABLE
;
543 /* Seed the algorithm by adding the successors of the entry block to the
545 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
)
546 add_control_edge (e
);
550 /* Free allocated storage. */
555 interesting_ssa_edges
.release ();
556 varying_ssa_edges
.release ();
557 cfg_blocks
.release ();
558 sbitmap_free (bb_in_list
);
559 sbitmap_free (executable_blocks
);
563 /* Return true if EXPR is an acceptable right-hand-side for a
564 GIMPLE assignment. We validate the entire tree, not just
565 the root node, thus catching expressions that embed complex
566 operands that are not permitted in GIMPLE. This function
567 is needed because the folding routines in fold-const.c
568 may return such expressions in some cases, e.g., an array
569 access with an embedded index addition. It may make more
570 sense to have folding routines that are sensitive to the
571 constraints on GIMPLE operands, rather than abandoning any
572 any attempt to fold if the usual folding turns out to be too
576 valid_gimple_rhs_p (tree expr
)
578 enum tree_code code
= TREE_CODE (expr
);
580 switch (TREE_CODE_CLASS (code
))
582 case tcc_declaration
:
583 if (!is_gimple_variable (expr
))
588 /* All constants are ok. */
592 /* GENERIC allows comparisons with non-boolean types, reject
593 those for GIMPLE. Let vector-typed comparisons pass - rules
594 for GENERIC and GIMPLE are the same here. */
595 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr
))
596 && (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
597 || TYPE_PRECISION (TREE_TYPE (expr
)) == 1))
598 && ! VECTOR_TYPE_P (TREE_TYPE (expr
)))
603 if (!is_gimple_val (TREE_OPERAND (expr
, 0))
604 || !is_gimple_val (TREE_OPERAND (expr
, 1)))
609 if (!is_gimple_val (TREE_OPERAND (expr
, 0)))
619 if (is_gimple_min_invariant (expr
))
621 t
= TREE_OPERAND (expr
, 0);
622 while (handled_component_p (t
))
624 /* ??? More checks needed, see the GIMPLE verifier. */
625 if ((TREE_CODE (t
) == ARRAY_REF
626 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
627 && !is_gimple_val (TREE_OPERAND (t
, 1)))
629 t
= TREE_OPERAND (t
, 0);
631 if (!is_gimple_id (t
))
637 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
639 if (((code
== VEC_COND_EXPR
|| code
== COND_EXPR
)
640 ? !is_gimple_condexpr (TREE_OPERAND (expr
, 0))
641 : !is_gimple_val (TREE_OPERAND (expr
, 0)))
642 || !is_gimple_val (TREE_OPERAND (expr
, 1))
643 || !is_gimple_val (TREE_OPERAND (expr
, 2)))
654 case tcc_exceptional
:
655 if (code
== CONSTRUCTOR
)
659 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
660 if (!is_gimple_val (elt
))
664 if (code
!= SSA_NAME
)
669 if (code
== BIT_FIELD_REF
)
670 return is_gimple_val (TREE_OPERAND (expr
, 0));
681 /* Return true if EXPR is a CALL_EXPR suitable for representation
682 as a single GIMPLE_CALL statement. If the arguments require
683 further gimplification, return false. */
686 valid_gimple_call_p (tree expr
)
690 if (TREE_CODE (expr
) != CALL_EXPR
)
693 nargs
= call_expr_nargs (expr
);
694 for (i
= 0; i
< nargs
; i
++)
696 tree arg
= CALL_EXPR_ARG (expr
, i
);
697 if (is_gimple_reg_type (TREE_TYPE (arg
)))
699 if (!is_gimple_val (arg
))
703 if (!is_gimple_lvalue (arg
))
711 /* Make SSA names defined by OLD_STMT point to NEW_STMT
712 as their defining statement. */
715 move_ssa_defining_stmt_for_defs (gimple new_stmt
, gimple old_stmt
)
720 if (gimple_in_ssa_p (cfun
))
722 /* Make defined SSA_NAMEs point to the new
723 statement as their definition. */
724 FOR_EACH_SSA_TREE_OPERAND (var
, old_stmt
, iter
, SSA_OP_ALL_DEFS
)
726 if (TREE_CODE (var
) == SSA_NAME
)
727 SSA_NAME_DEF_STMT (var
) = new_stmt
;
732 /* Helper function for update_gimple_call and update_call_from_tree.
733 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
736 finish_update_gimple_call (gimple_stmt_iterator
*si_p
, gimple new_stmt
,
739 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
740 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
741 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
742 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
743 gimple_set_location (new_stmt
, gimple_location (stmt
));
744 if (gimple_block (new_stmt
) == NULL_TREE
)
745 gimple_set_block (new_stmt
, gimple_block (stmt
));
746 gsi_replace (si_p
, new_stmt
, false);
749 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
750 with number of arguments NARGS, where the arguments in GIMPLE form
751 follow NARGS argument. */
754 update_gimple_call (gimple_stmt_iterator
*si_p
, tree fn
, int nargs
, ...)
757 gcall
*new_stmt
, *stmt
= as_a
<gcall
*> (gsi_stmt (*si_p
));
759 gcc_assert (is_gimple_call (stmt
));
760 va_start (ap
, nargs
);
761 new_stmt
= gimple_build_call_valist (fn
, nargs
, ap
);
762 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
767 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
768 value of EXPR, which is expected to be the result of folding the
769 call. This can only be done if EXPR is a CALL_EXPR with valid
770 GIMPLE operands as arguments, or if it is a suitable RHS expression
771 for a GIMPLE_ASSIGN. More complex expressions will require
772 gimplification, which will introduce additional statements. In this
773 event, no update is performed, and the function returns false.
774 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
775 replace the statement at *SI_P with an entirely new statement.
776 The new statement need not be a call, e.g., if the original call
777 folded to a constant. */
780 update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
782 gimple stmt
= gsi_stmt (*si_p
);
784 if (valid_gimple_call_p (expr
))
786 /* The call has simplified to another call. */
787 tree fn
= CALL_EXPR_FN (expr
);
789 unsigned nargs
= call_expr_nargs (expr
);
790 vec
<tree
> args
= vNULL
;
796 args
.safe_grow_cleared (nargs
);
798 for (i
= 0; i
< nargs
; i
++)
799 args
[i
] = CALL_EXPR_ARG (expr
, i
);
802 new_stmt
= gimple_build_call_vec (fn
, args
);
803 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
808 else if (valid_gimple_rhs_p (expr
))
810 tree lhs
= gimple_call_lhs (stmt
);
813 /* The call has simplified to an expression
814 that cannot be represented as a GIMPLE_CALL. */
817 /* A value is expected.
818 Introduce a new GIMPLE_ASSIGN statement. */
819 STRIP_USELESS_TYPE_CONVERSION (expr
);
820 new_stmt
= gimple_build_assign (lhs
, expr
);
821 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
822 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
823 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
825 else if (!TREE_SIDE_EFFECTS (expr
))
827 /* No value is expected, and EXPR has no effect.
828 Replace it with an empty statement. */
829 new_stmt
= gimple_build_nop ();
830 if (gimple_in_ssa_p (cfun
))
832 unlink_stmt_vdef (stmt
);
838 /* No value is expected, but EXPR has an effect,
839 e.g., it could be a reference to a volatile
840 variable. Create an assignment statement
841 with a dummy (unused) lhs variable. */
842 STRIP_USELESS_TYPE_CONVERSION (expr
);
843 if (gimple_in_ssa_p (cfun
))
844 lhs
= make_ssa_name (TREE_TYPE (expr
));
846 lhs
= create_tmp_var (TREE_TYPE (expr
));
847 new_stmt
= gimple_build_assign (lhs
, expr
);
848 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
849 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
850 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
852 gimple_set_location (new_stmt
, gimple_location (stmt
));
853 gsi_replace (si_p
, new_stmt
, false);
857 /* The call simplified to an expression that is
858 not a valid GIMPLE RHS. */
863 /* Entry point to the propagation engine.
865 VISIT_STMT is called for every statement visited.
866 VISIT_PHI is called for every PHI node visited. */
869 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt
,
870 ssa_prop_visit_phi_fn visit_phi
)
872 ssa_prop_visit_stmt
= visit_stmt
;
873 ssa_prop_visit_phi
= visit_phi
;
877 /* Iterate until the worklists are empty. */
878 while (!cfg_blocks_empty_p ()
879 || interesting_ssa_edges
.length () > 0
880 || varying_ssa_edges
.length () > 0)
882 if (!cfg_blocks_empty_p ())
884 /* Pull the next block to simulate off the worklist. */
885 basic_block dest_block
= cfg_blocks_get ();
886 simulate_block (dest_block
);
889 /* In order to move things to varying as quickly as
890 possible,process the VARYING_SSA_EDGES worklist first. */
891 process_ssa_edge_worklist (&varying_ssa_edges
);
893 /* Now process the INTERESTING_SSA_EDGES worklist. */
894 process_ssa_edge_worklist (&interesting_ssa_edges
);
901 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
902 is a non-volatile pointer dereference, a structure reference or a
903 reference to a single _DECL. Ignore volatile memory references
904 because they are not interesting for the optimizers. */
907 stmt_makes_single_store (gimple stmt
)
911 if (gimple_code (stmt
) != GIMPLE_ASSIGN
912 && gimple_code (stmt
) != GIMPLE_CALL
)
915 if (!gimple_vdef (stmt
))
918 lhs
= gimple_get_lhs (stmt
);
920 /* A call statement may have a null LHS. */
924 return (!TREE_THIS_VOLATILE (lhs
)
926 || REFERENCE_CLASS_P (lhs
)));
930 /* Propagation statistics. */
935 long num_stmts_folded
;
939 static struct prop_stats_d prop_stats
;
941 /* Replace USE references in statement STMT with the values stored in
942 PROP_VALUE. Return true if at least one reference was replaced. */
945 replace_uses_in (gimple stmt
, ssa_prop_get_value_fn get_value
)
947 bool replaced
= false;
951 FOR_EACH_SSA_USE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
953 tree tuse
= USE_FROM_PTR (use
);
954 tree val
= (*get_value
) (tuse
);
956 if (val
== tuse
|| val
== NULL_TREE
)
959 if (gimple_code (stmt
) == GIMPLE_ASM
960 && !may_propagate_copy_into_asm (tuse
))
963 if (!may_propagate_copy (tuse
, val
))
966 if (TREE_CODE (val
) != SSA_NAME
)
967 prop_stats
.num_const_prop
++;
969 prop_stats
.num_copy_prop
++;
971 propagate_value (use
, val
);
980 /* Replace propagated values into all the arguments for PHI using the
981 values from PROP_VALUE. */
984 replace_phi_args_in (gphi
*phi
, ssa_prop_get_value_fn get_value
)
987 bool replaced
= false;
989 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
991 fprintf (dump_file
, "Folding PHI node: ");
992 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
995 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
997 tree arg
= gimple_phi_arg_def (phi
, i
);
999 if (TREE_CODE (arg
) == SSA_NAME
)
1001 tree val
= (*get_value
) (arg
);
1003 if (val
&& val
!= arg
&& may_propagate_copy (arg
, val
))
1005 if (TREE_CODE (val
) != SSA_NAME
)
1006 prop_stats
.num_const_prop
++;
1008 prop_stats
.num_copy_prop
++;
1010 propagate_value (PHI_ARG_DEF_PTR (phi
, i
), val
);
1013 /* If we propagated a copy and this argument flows
1014 through an abnormal edge, update the replacement
1016 if (TREE_CODE (val
) == SSA_NAME
1017 && gimple_phi_arg_edge (phi
, i
)->flags
& EDGE_ABNORMAL
)
1018 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
) = 1;
1023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1026 fprintf (dump_file
, "No folding possible\n");
1029 fprintf (dump_file
, "Folded into: ");
1030 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
1031 fprintf (dump_file
, "\n");
1039 class substitute_and_fold_dom_walker
: public dom_walker
1042 substitute_and_fold_dom_walker (cdi_direction direction
,
1043 ssa_prop_get_value_fn get_value_fn_
,
1044 ssa_prop_fold_stmt_fn fold_fn_
,
1046 : dom_walker (direction
), get_value_fn (get_value_fn_
),
1047 fold_fn (fold_fn_
), do_dce (do_dce_
), something_changed (false)
1049 stmts_to_remove
.create (0);
1050 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1052 ~substitute_and_fold_dom_walker ()
1054 stmts_to_remove
.release ();
1055 BITMAP_FREE (need_eh_cleanup
);
1058 virtual void before_dom_children (basic_block
);
1059 virtual void after_dom_children (basic_block
) {}
1061 ssa_prop_get_value_fn get_value_fn
;
1062 ssa_prop_fold_stmt_fn fold_fn
;
1064 bool something_changed
;
1065 vec
<gimple
> stmts_to_remove
;
1066 bitmap need_eh_cleanup
;
1070 substitute_and_fold_dom_walker::before_dom_children (basic_block bb
)
1072 /* Propagate known values into PHI nodes. */
1073 for (gphi_iterator i
= gsi_start_phis (bb
);
1077 gphi
*phi
= i
.phi ();
1078 tree res
= gimple_phi_result (phi
);
1079 if (virtual_operand_p (res
))
1082 && res
&& TREE_CODE (res
) == SSA_NAME
)
1084 tree sprime
= get_value_fn (res
);
1087 && may_propagate_copy (res
, sprime
))
1089 stmts_to_remove
.safe_push (phi
);
1093 something_changed
|= replace_phi_args_in (phi
, get_value_fn
);
1096 /* Propagate known values into stmts. In some case it exposes
1097 more trivially deletable stmts to walk backward. */
1098 for (gimple_stmt_iterator i
= gsi_start_bb (bb
);
1103 gimple stmt
= gsi_stmt (i
);
1105 enum gimple_code code
= gimple_code (stmt
);
1107 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1108 range information for names and they are discarded
1111 if (code
== GIMPLE_ASSIGN
1112 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
1115 /* No point propagating into a stmt we have a value for we
1116 can propagate into all uses. Mark it for removal instead. */
1117 tree lhs
= gimple_get_lhs (stmt
);
1119 && lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1121 tree sprime
= get_value_fn (lhs
);
1124 && may_propagate_copy (lhs
, sprime
)
1125 && !stmt_could_throw_p (stmt
)
1126 && !gimple_has_side_effects (stmt
))
1128 stmts_to_remove
.safe_push (stmt
);
1133 /* Replace the statement with its folded version and mark it
1135 did_replace
= false;
1136 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1138 fprintf (dump_file
, "Folding statement: ");
1139 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1144 /* Some statements may be simplified using propagator
1145 specific information. Do this before propagating
1146 into the stmt to not disturb pass specific information. */
1151 prop_stats
.num_stmts_folded
++;
1152 stmt
= gsi_stmt (i
);
1156 /* Replace real uses in the statement. */
1157 did_replace
|= replace_uses_in (stmt
, get_value_fn
);
1159 /* If we made a replacement, fold the statement. */
1161 fold_stmt (&i
, follow_single_use_edges
);
1166 stmt
= gsi_stmt (i
);
1168 /* If we cleaned up EH information from the statement,
1170 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1171 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
1173 if (is_gimple_assign (stmt
)
1174 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
))
1175 == GIMPLE_SINGLE_RHS
))
1177 tree rhs
= gimple_assign_rhs1 (stmt
);
1179 if (TREE_CODE (rhs
) == ADDR_EXPR
)
1180 recompute_tree_invariant_for_addr_expr (rhs
);
1183 /* Determine what needs to be done to update the SSA form. */
1185 if (!is_gimple_debug (stmt
))
1186 something_changed
= true;
1189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1193 fprintf (dump_file
, "Folded into: ");
1194 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1195 fprintf (dump_file
, "\n");
1198 fprintf (dump_file
, "Not folded\n");
1205 /* Perform final substitution and folding of propagated values.
1207 PROP_VALUE[I] contains the single value that should be substituted
1208 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1211 If FOLD_FN is non-NULL the function will be invoked on all statements
1212 before propagating values for pass specific simplification.
1214 DO_DCE is true if trivially dead stmts can be removed.
1216 If DO_DCE is true, the statements within a BB are walked from
1217 last to first element. Otherwise we scan from first to last element.
1219 Return TRUE when something changed. */
1222 substitute_and_fold (ssa_prop_get_value_fn get_value_fn
,
1223 ssa_prop_fold_stmt_fn fold_fn
,
1226 gcc_assert (get_value_fn
);
1228 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1229 fprintf (dump_file
, "\nSubstituting values and folding statements\n\n");
1231 memset (&prop_stats
, 0, sizeof (prop_stats
));
1233 calculate_dominance_info (CDI_DOMINATORS
);
1234 substitute_and_fold_dom_walker
walker(CDI_DOMINATORS
,
1235 get_value_fn
, fold_fn
, do_dce
);
1236 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1238 /* We cannot remove stmts during the BB walk, especially not release
1239 SSA names there as that destroys the lattice of our callers.
1240 Remove stmts in reverse order to make debug stmt creation possible. */
1241 while (!walker
.stmts_to_remove
.is_empty ())
1243 gimple stmt
= walker
.stmts_to_remove
.pop ();
1244 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1246 fprintf (dump_file
, "Removing dead stmt ");
1247 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1248 fprintf (dump_file
, "\n");
1250 prop_stats
.num_dce
++;
1251 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1252 if (gimple_code (stmt
) == GIMPLE_PHI
)
1253 remove_phi_node (&gsi
, true);
1256 unlink_stmt_vdef (stmt
);
1257 gsi_remove (&gsi
, true);
1258 release_defs (stmt
);
1262 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
1263 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
1265 statistics_counter_event (cfun
, "Constants propagated",
1266 prop_stats
.num_const_prop
);
1267 statistics_counter_event (cfun
, "Copies propagated",
1268 prop_stats
.num_copy_prop
);
1269 statistics_counter_event (cfun
, "Statements folded",
1270 prop_stats
.num_stmts_folded
);
1271 statistics_counter_event (cfun
, "Statements deleted",
1272 prop_stats
.num_dce
);
1274 return walker
.something_changed
;
1278 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1281 may_propagate_copy (tree dest
, tree orig
)
1283 tree type_d
= TREE_TYPE (dest
);
1284 tree type_o
= TREE_TYPE (orig
);
1286 /* If ORIG is a default definition which flows in from an abnormal edge
1287 then the copy can be propagated. It is important that we do so to avoid
1288 uninitialized copies. */
1289 if (TREE_CODE (orig
) == SSA_NAME
1290 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
)
1291 && SSA_NAME_IS_DEFAULT_DEF (orig
)
1292 && (SSA_NAME_VAR (orig
) == NULL_TREE
1293 || TREE_CODE (SSA_NAME_VAR (orig
)) == VAR_DECL
))
1295 /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1297 else if (TREE_CODE (orig
) == SSA_NAME
1298 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
1300 /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1302 else if (TREE_CODE (dest
) == SSA_NAME
1303 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest
))
1306 /* Do not copy between types for which we *do* need a conversion. */
1307 if (!useless_type_conversion_p (type_d
, type_o
))
1310 /* Generally propagating virtual operands is not ok as that may
1311 create overlapping life-ranges. */
1312 if (TREE_CODE (dest
) == SSA_NAME
&& virtual_operand_p (dest
))
1315 /* Anything else is OK. */
1319 /* Like may_propagate_copy, but use as the destination expression
1320 the principal expression (typically, the RHS) contained in
1321 statement DEST. This is more efficient when working with the
1322 gimple tuples representation. */
1325 may_propagate_copy_into_stmt (gimple dest
, tree orig
)
1330 /* If the statement is a switch or a single-rhs assignment,
1331 then the expression to be replaced by the propagation may
1332 be an SSA_NAME. Fortunately, there is an explicit tree
1333 for the expression, so we delegate to may_propagate_copy. */
1335 if (gimple_assign_single_p (dest
))
1336 return may_propagate_copy (gimple_assign_rhs1 (dest
), orig
);
1337 else if (gswitch
*dest_swtch
= dyn_cast
<gswitch
*> (dest
))
1338 return may_propagate_copy (gimple_switch_index (dest_swtch
), orig
);
1340 /* In other cases, the expression is not materialized, so there
1341 is no destination to pass to may_propagate_copy. On the other
1342 hand, the expression cannot be an SSA_NAME, so the analysis
1345 if (TREE_CODE (orig
) == SSA_NAME
1346 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
1349 if (is_gimple_assign (dest
))
1350 type_d
= TREE_TYPE (gimple_assign_lhs (dest
));
1351 else if (gimple_code (dest
) == GIMPLE_COND
)
1352 type_d
= boolean_type_node
;
1353 else if (is_gimple_call (dest
)
1354 && gimple_call_lhs (dest
) != NULL_TREE
)
1355 type_d
= TREE_TYPE (gimple_call_lhs (dest
));
1359 type_o
= TREE_TYPE (orig
);
1361 if (!useless_type_conversion_p (type_d
, type_o
))
1367 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1370 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED
)
1376 /* Common code for propagate_value and replace_exp.
1378 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1379 replacement is done to propagate a value or not. */
1382 replace_exp_1 (use_operand_p op_p
, tree val
,
1383 bool for_propagation ATTRIBUTE_UNUSED
)
1385 #if defined ENABLE_CHECKING
1386 tree op
= USE_FROM_PTR (op_p
);
1388 gcc_assert (!(for_propagation
1389 && TREE_CODE (op
) == SSA_NAME
1390 && TREE_CODE (val
) == SSA_NAME
1391 && !may_propagate_copy (op
, val
)));
1394 if (TREE_CODE (val
) == SSA_NAME
)
1395 SET_USE (op_p
, val
);
1397 SET_USE (op_p
, unshare_expr (val
));
1401 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1402 into the operand pointed to by OP_P.
1404 Use this version for const/copy propagation as it will perform additional
1405 checks to ensure validity of the const/copy propagation. */
1408 propagate_value (use_operand_p op_p
, tree val
)
1410 replace_exp_1 (op_p
, val
, true);
1413 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1415 Use this version when not const/copy propagating values. For example,
1416 PRE uses this version when building expressions as they would appear
1417 in specific blocks taking into account actions of PHI nodes.
1419 The statement in which an expression has been replaced should be
1420 folded using fold_stmt_inplace. */
1423 replace_exp (use_operand_p op_p
, tree val
)
1425 replace_exp_1 (op_p
, val
, false);
1429 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1430 into the tree pointed to by OP_P.
1432 Use this version for const/copy propagation when SSA operands are not
1433 available. It will perform the additional checks to ensure validity of
1434 the const/copy propagation, but will not update any operand information.
1435 Be sure to mark the stmt as modified. */
1438 propagate_tree_value (tree
*op_p
, tree val
)
1440 if (TREE_CODE (val
) == SSA_NAME
)
1443 *op_p
= unshare_expr (val
);
1447 /* Like propagate_tree_value, but use as the operand to replace
1448 the principal expression (typically, the RHS) contained in the
1449 statement referenced by iterator GSI. Note that it is not
1450 always possible to update the statement in-place, so a new
1451 statement may be created to replace the original. */
1454 propagate_tree_value_into_stmt (gimple_stmt_iterator
*gsi
, tree val
)
1456 gimple stmt
= gsi_stmt (*gsi
);
1458 if (is_gimple_assign (stmt
))
1460 tree expr
= NULL_TREE
;
1461 if (gimple_assign_single_p (stmt
))
1462 expr
= gimple_assign_rhs1 (stmt
);
1463 propagate_tree_value (&expr
, val
);
1464 gimple_assign_set_rhs_from_tree (gsi
, expr
);
1466 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
1468 tree lhs
= NULL_TREE
;
1469 tree rhs
= build_zero_cst (TREE_TYPE (val
));
1470 propagate_tree_value (&lhs
, val
);
1471 gimple_cond_set_code (cond_stmt
, NE_EXPR
);
1472 gimple_cond_set_lhs (cond_stmt
, lhs
);
1473 gimple_cond_set_rhs (cond_stmt
, rhs
);
1475 else if (is_gimple_call (stmt
)
1476 && gimple_call_lhs (stmt
) != NULL_TREE
)
1478 tree expr
= NULL_TREE
;
1480 propagate_tree_value (&expr
, val
);
1481 res
= update_call_from_tree (gsi
, expr
);
1484 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
1485 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt
), val
);