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 #include "tree-cfgcleanup.h"
72 /* This file implements a generic value propagation engine based on
73 the same propagation used by the SSA-CCP algorithm [1].
75 Propagation is performed by simulating the execution of every
76 statement that produces the value being propagated. Simulation
79 1- Initially, all edges of the CFG are marked not executable and
80 the CFG worklist is seeded with all the statements in the entry
81 basic block (block 0).
83 2- Every statement S is simulated with a call to the call-back
84 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
87 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
88 interest and does not affect any of the work lists.
90 SSA_PROP_VARYING: The value produced by S cannot be determined
91 at compile time. Further simulation of S is not required.
92 If S is a conditional jump, all the outgoing edges for the
93 block are considered executable and added to the work
96 SSA_PROP_INTERESTING: S produces a value that can be computed
97 at compile time. Its result can be propagated into the
98 statements that feed from S. Furthermore, if S is a
99 conditional jump, only the edge known to be taken is added
100 to the work list. Edges that are known not to execute are
103 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
104 return value from SSA_PROP_VISIT_PHI has the same semantics as
107 4- Three work lists are kept. Statements are only added to these
108 lists if they produce one of SSA_PROP_INTERESTING or
111 CFG_BLOCKS contains the list of blocks to be simulated.
112 Blocks are added to this list if their incoming edges are
115 VARYING_SSA_EDGES contains the list of statements that feed
116 from statements that produce an SSA_PROP_VARYING result.
117 These are simulated first to speed up processing.
119 INTERESTING_SSA_EDGES contains the list of statements that
120 feed from statements that produce an SSA_PROP_INTERESTING
123 5- Simulation terminates when all three work lists are drained.
125 Before calling ssa_propagate, it is important to clear
126 prop_simulate_again_p for all the statements in the program that
127 should be simulated. This initialization allows an implementation
128 to specify which statements should never be simulated.
130 It is also important to compute def-use information before calling
135 [1] Constant propagation with conditional branches,
136 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
138 [2] Building an Optimizing Compiler,
139 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
141 [3] Advanced Compiler Design and Implementation,
142 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
144 /* Function pointers used to parameterize the propagation engine. */
145 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt
;
146 static ssa_prop_visit_phi_fn ssa_prop_visit_phi
;
148 /* Keep track of statements that have been added to one of the SSA
149 edges worklists. This flag is used to avoid visiting statements
150 unnecessarily when draining an SSA edge worklist. If while
151 simulating a basic block, we find a statement with
152 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
153 processing from visiting it again.
155 NOTE: users of the propagation engine are not allowed to use
156 the GF_PLF_1 flag. */
157 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
159 /* A bitmap to keep track of executable blocks in the CFG. */
160 static sbitmap executable_blocks
;
162 /* Array of control flow edges on the worklist. */
163 static vec
<basic_block
> cfg_blocks
;
165 static unsigned int cfg_blocks_num
= 0;
166 static int cfg_blocks_tail
;
167 static int cfg_blocks_head
;
169 static sbitmap bb_in_list
;
171 /* Worklist of SSA edges which will need reexamination as their
172 definition has changed. SSA edges are def-use edges in the SSA
173 web. For each D-U edge, we store the target statement or PHI node
175 static vec
<gimple
> interesting_ssa_edges
;
177 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
178 list of SSA edges is split into two. One contains all SSA edges
179 who need to be reexamined because their lattice value changed to
180 varying (this worklist), and the other contains all other SSA edges
181 to be reexamined (INTERESTING_SSA_EDGES).
183 Since most values in the program are VARYING, the ideal situation
184 is to move them to that lattice value as quickly as possible.
185 Thus, it doesn't make sense to process any other type of lattice
186 value until all VARYING values are propagated fully, which is one
187 thing using the VARYING worklist achieves. In addition, if we
188 don't use a separate worklist for VARYING edges, we end up with
189 situations where lattice values move from
190 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
191 static vec
<gimple
> varying_ssa_edges
;
194 /* Return true if the block worklist empty. */
197 cfg_blocks_empty_p (void)
199 return (cfg_blocks_num
== 0);
203 /* Add a basic block to the worklist. The block must not be already
204 in the worklist, and it must not be the ENTRY or EXIT block. */
207 cfg_blocks_add (basic_block bb
)
211 gcc_assert (bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
212 && bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
213 gcc_assert (!bitmap_bit_p (bb_in_list
, bb
->index
));
215 if (cfg_blocks_empty_p ())
217 cfg_blocks_tail
= cfg_blocks_head
= 0;
223 if (cfg_blocks_num
> cfg_blocks
.length ())
225 /* We have to grow the array now. Adjust to queue to occupy
226 the full space of the original array. We do not need to
227 initialize the newly allocated portion of the array
228 because we keep track of CFG_BLOCKS_HEAD and
230 cfg_blocks_tail
= cfg_blocks
.length ();
232 cfg_blocks
.safe_grow (2 * cfg_blocks_tail
);
234 /* Minor optimization: we prefer to see blocks with more
235 predecessors later, because there is more of a chance that
236 the incoming edges will be executable. */
237 else if (EDGE_COUNT (bb
->preds
)
238 >= EDGE_COUNT (cfg_blocks
[cfg_blocks_head
]->preds
))
239 cfg_blocks_tail
= ((cfg_blocks_tail
+ 1) % cfg_blocks
.length ());
242 if (cfg_blocks_head
== 0)
243 cfg_blocks_head
= cfg_blocks
.length ();
249 cfg_blocks
[head
? cfg_blocks_head
: cfg_blocks_tail
] = bb
;
250 bitmap_set_bit (bb_in_list
, bb
->index
);
254 /* Remove a block from the worklist. */
257 cfg_blocks_get (void)
261 bb
= cfg_blocks
[cfg_blocks_head
];
263 gcc_assert (!cfg_blocks_empty_p ());
266 cfg_blocks_head
= ((cfg_blocks_head
+ 1) % cfg_blocks
.length ());
268 bitmap_clear_bit (bb_in_list
, bb
->index
);
274 /* We have just defined a new value for VAR. If IS_VARYING is true,
275 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
276 them to INTERESTING_SSA_EDGES. */
279 add_ssa_edge (tree var
, bool is_varying
)
281 imm_use_iterator iter
;
284 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
286 gimple use_stmt
= USE_STMT (use_p
);
288 if (prop_simulate_again_p (use_stmt
)
289 && !gimple_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
291 gimple_set_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
, true);
293 varying_ssa_edges
.safe_push (use_stmt
);
295 interesting_ssa_edges
.safe_push (use_stmt
);
301 /* Add edge E to the control flow worklist. */
304 add_control_edge (edge e
)
306 basic_block bb
= e
->dest
;
307 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
310 /* If the edge had already been executed, skip it. */
311 if (e
->flags
& EDGE_EXECUTABLE
)
314 e
->flags
|= EDGE_EXECUTABLE
;
316 /* If the block is already in the list, we're done. */
317 if (bitmap_bit_p (bb_in_list
, bb
->index
))
322 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
323 fprintf (dump_file
, "\nAdding Destination of edge (%d -> %d) to worklist\n",
324 e
->src
->index
, e
->dest
->index
);
328 /* Simulate the execution of STMT and update the work lists accordingly. */
331 simulate_stmt (gimple stmt
)
333 enum ssa_prop_result val
= SSA_PROP_NOT_INTERESTING
;
334 edge taken_edge
= NULL
;
335 tree output_name
= NULL_TREE
;
337 /* Don't bother visiting statements that are already
338 considered varying by the propagator. */
339 if (!prop_simulate_again_p (stmt
))
342 if (gimple_code (stmt
) == GIMPLE_PHI
)
344 val
= ssa_prop_visit_phi (as_a
<gphi
*> (stmt
));
345 output_name
= gimple_phi_result (stmt
);
348 val
= ssa_prop_visit_stmt (stmt
, &taken_edge
, &output_name
);
350 if (val
== SSA_PROP_VARYING
)
352 prop_set_simulate_again (stmt
, false);
354 /* If the statement produced a new varying value, add the SSA
355 edges coming out of OUTPUT_NAME. */
357 add_ssa_edge (output_name
, true);
359 /* If STMT transfers control out of its basic block, add
360 all outgoing edges to the work list. */
361 if (stmt_ends_bb_p (stmt
))
365 basic_block bb
= gimple_bb (stmt
);
366 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
367 add_control_edge (e
);
371 else if (val
== SSA_PROP_INTERESTING
)
373 /* If the statement produced new value, add the SSA edges coming
374 out of OUTPUT_NAME. */
376 add_ssa_edge (output_name
, false);
378 /* If we know which edge is going to be taken out of this block,
379 add it to the CFG work list. */
381 add_control_edge (taken_edge
);
384 /* If there are no SSA uses on the stmt whose defs are simulated
385 again then this stmt will be never visited again. */
386 bool has_simulate_again_uses
= false;
389 if (gimple_code (stmt
) == GIMPLE_PHI
)
394 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->preds
)
395 if (!(e
->flags
& EDGE_EXECUTABLE
)
396 || ((arg
= PHI_ARG_DEF_FROM_EDGE (stmt
, e
))
397 && TREE_CODE (arg
) == SSA_NAME
398 && !SSA_NAME_IS_DEFAULT_DEF (arg
)
399 && prop_simulate_again_p (SSA_NAME_DEF_STMT (arg
))))
401 has_simulate_again_uses
= true;
406 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
408 gimple def_stmt
= SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p
));
409 if (!gimple_nop_p (def_stmt
)
410 && prop_simulate_again_p (def_stmt
))
412 has_simulate_again_uses
= true;
416 if (!has_simulate_again_uses
)
418 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
419 fprintf (dump_file
, "marking stmt to be not simulated again\n");
420 prop_set_simulate_again (stmt
, false);
424 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
425 drain. This pops statements off the given WORKLIST and processes
426 them until there are no more statements on WORKLIST.
427 We take a pointer to WORKLIST because it may be reallocated when an
428 SSA edge is added to it in simulate_stmt. */
431 process_ssa_edge_worklist (vec
<gimple
> *worklist
)
433 /* Drain the entire worklist. */
434 while (worklist
->length () > 0)
438 /* Pull the statement to simulate off the worklist. */
439 gimple stmt
= worklist
->pop ();
441 /* If this statement was already visited by simulate_block, then
442 we don't need to visit it again here. */
443 if (!gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
446 /* STMT is no longer in a worklist. */
447 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
449 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
451 fprintf (dump_file
, "\nSimulating statement (from ssa_edges): ");
452 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
455 bb
= gimple_bb (stmt
);
457 /* PHI nodes are always visited, regardless of whether or not
458 the destination block is executable. Otherwise, visit the
459 statement only if its block is marked executable. */
460 if (gimple_code (stmt
) == GIMPLE_PHI
461 || bitmap_bit_p (executable_blocks
, bb
->index
))
462 simulate_stmt (stmt
);
467 /* Simulate the execution of BLOCK. Evaluate the statement associated
468 with each variable reference inside the block. */
471 simulate_block (basic_block block
)
473 gimple_stmt_iterator gsi
;
475 /* There is nothing to do for the exit block. */
476 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
479 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
480 fprintf (dump_file
, "\nSimulating block %d\n", block
->index
);
482 /* Always simulate PHI nodes, even if we have simulated this block
484 for (gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
485 simulate_stmt (gsi_stmt (gsi
));
487 /* If this is the first time we've simulated this block, then we
488 must simulate each of its statements. */
489 if (!bitmap_bit_p (executable_blocks
, block
->index
))
491 gimple_stmt_iterator j
;
492 unsigned int normal_edge_count
;
496 /* Note that we have simulated this block. */
497 bitmap_set_bit (executable_blocks
, block
->index
);
499 for (j
= gsi_start_bb (block
); !gsi_end_p (j
); gsi_next (&j
))
501 gimple stmt
= gsi_stmt (j
);
503 /* If this statement is already in the worklist then
504 "cancel" it. The reevaluation implied by the worklist
505 entry will produce the same value we generate here and
506 thus reevaluating it again from the worklist is
508 if (gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
509 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
511 simulate_stmt (stmt
);
514 /* We can not predict when abnormal and EH edges will be executed, so
515 once a block is considered executable, we consider any
516 outgoing abnormal edges as executable.
518 TODO: This is not exactly true. Simplifying statement might
519 prove it non-throwing and also computed goto can be handled
520 when destination is known.
522 At the same time, if this block has only one successor that is
523 reached by non-abnormal edges, then add that successor to the
525 normal_edge_count
= 0;
527 FOR_EACH_EDGE (e
, ei
, block
->succs
)
529 if (e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
))
530 add_control_edge (e
);
538 if (normal_edge_count
== 1)
539 add_control_edge (normal_edge
);
544 /* Initialize local data structures and work lists. */
553 /* Worklists of SSA edges. */
554 interesting_ssa_edges
.create (20);
555 varying_ssa_edges
.create (20);
557 executable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
558 bitmap_clear (executable_blocks
);
560 bb_in_list
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
561 bitmap_clear (bb_in_list
);
563 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
564 dump_immediate_uses (dump_file
);
566 cfg_blocks
.create (20);
567 cfg_blocks
.safe_grow_cleared (20);
569 /* Initially assume that every edge in the CFG is not executable.
570 (including the edges coming out of the entry block). */
571 FOR_ALL_BB_FN (bb
, cfun
)
573 gimple_stmt_iterator si
;
575 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
576 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
578 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
579 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
581 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
582 e
->flags
&= ~EDGE_EXECUTABLE
;
585 /* Seed the algorithm by adding the successors of the entry block to the
587 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
)
588 add_control_edge (e
);
592 /* Free allocated storage. */
597 interesting_ssa_edges
.release ();
598 varying_ssa_edges
.release ();
599 cfg_blocks
.release ();
600 sbitmap_free (bb_in_list
);
601 sbitmap_free (executable_blocks
);
605 /* Return true if EXPR is an acceptable right-hand-side for a
606 GIMPLE assignment. We validate the entire tree, not just
607 the root node, thus catching expressions that embed complex
608 operands that are not permitted in GIMPLE. This function
609 is needed because the folding routines in fold-const.c
610 may return such expressions in some cases, e.g., an array
611 access with an embedded index addition. It may make more
612 sense to have folding routines that are sensitive to the
613 constraints on GIMPLE operands, rather than abandoning any
614 any attempt to fold if the usual folding turns out to be too
618 valid_gimple_rhs_p (tree expr
)
620 enum tree_code code
= TREE_CODE (expr
);
622 switch (TREE_CODE_CLASS (code
))
624 case tcc_declaration
:
625 if (!is_gimple_variable (expr
))
630 /* All constants are ok. */
634 /* GENERIC allows comparisons with non-boolean types, reject
635 those for GIMPLE. Let vector-typed comparisons pass - rules
636 for GENERIC and GIMPLE are the same here. */
637 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr
))
638 && (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
639 || TYPE_PRECISION (TREE_TYPE (expr
)) == 1))
640 && ! VECTOR_TYPE_P (TREE_TYPE (expr
)))
645 if (!is_gimple_val (TREE_OPERAND (expr
, 0))
646 || !is_gimple_val (TREE_OPERAND (expr
, 1)))
651 if (!is_gimple_val (TREE_OPERAND (expr
, 0)))
661 if (is_gimple_min_invariant (expr
))
663 t
= TREE_OPERAND (expr
, 0);
664 while (handled_component_p (t
))
666 /* ??? More checks needed, see the GIMPLE verifier. */
667 if ((TREE_CODE (t
) == ARRAY_REF
668 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
669 && !is_gimple_val (TREE_OPERAND (t
, 1)))
671 t
= TREE_OPERAND (t
, 0);
673 if (!is_gimple_id (t
))
679 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
681 if (((code
== VEC_COND_EXPR
|| code
== COND_EXPR
)
682 ? !is_gimple_condexpr (TREE_OPERAND (expr
, 0))
683 : !is_gimple_val (TREE_OPERAND (expr
, 0)))
684 || !is_gimple_val (TREE_OPERAND (expr
, 1))
685 || !is_gimple_val (TREE_OPERAND (expr
, 2)))
696 case tcc_exceptional
:
697 if (code
== CONSTRUCTOR
)
701 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
702 if (!is_gimple_val (elt
))
706 if (code
!= SSA_NAME
)
711 if (code
== BIT_FIELD_REF
)
712 return is_gimple_val (TREE_OPERAND (expr
, 0));
723 /* Return true if EXPR is a CALL_EXPR suitable for representation
724 as a single GIMPLE_CALL statement. If the arguments require
725 further gimplification, return false. */
728 valid_gimple_call_p (tree expr
)
732 if (TREE_CODE (expr
) != CALL_EXPR
)
735 nargs
= call_expr_nargs (expr
);
736 for (i
= 0; i
< nargs
; i
++)
738 tree arg
= CALL_EXPR_ARG (expr
, i
);
739 if (is_gimple_reg_type (TREE_TYPE (arg
)))
741 if (!is_gimple_val (arg
))
745 if (!is_gimple_lvalue (arg
))
753 /* Make SSA names defined by OLD_STMT point to NEW_STMT
754 as their defining statement. */
757 move_ssa_defining_stmt_for_defs (gimple new_stmt
, gimple old_stmt
)
762 if (gimple_in_ssa_p (cfun
))
764 /* Make defined SSA_NAMEs point to the new
765 statement as their definition. */
766 FOR_EACH_SSA_TREE_OPERAND (var
, old_stmt
, iter
, SSA_OP_ALL_DEFS
)
768 if (TREE_CODE (var
) == SSA_NAME
)
769 SSA_NAME_DEF_STMT (var
) = new_stmt
;
774 /* Helper function for update_gimple_call and update_call_from_tree.
775 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
778 finish_update_gimple_call (gimple_stmt_iterator
*si_p
, gimple new_stmt
,
781 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
782 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
783 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
784 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
785 gimple_set_location (new_stmt
, gimple_location (stmt
));
786 if (gimple_block (new_stmt
) == NULL_TREE
)
787 gimple_set_block (new_stmt
, gimple_block (stmt
));
788 gsi_replace (si_p
, new_stmt
, false);
791 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
792 with number of arguments NARGS, where the arguments in GIMPLE form
793 follow NARGS argument. */
796 update_gimple_call (gimple_stmt_iterator
*si_p
, tree fn
, int nargs
, ...)
799 gcall
*new_stmt
, *stmt
= as_a
<gcall
*> (gsi_stmt (*si_p
));
801 gcc_assert (is_gimple_call (stmt
));
802 va_start (ap
, nargs
);
803 new_stmt
= gimple_build_call_valist (fn
, nargs
, ap
);
804 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
809 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
810 value of EXPR, which is expected to be the result of folding the
811 call. This can only be done if EXPR is a CALL_EXPR with valid
812 GIMPLE operands as arguments, or if it is a suitable RHS expression
813 for a GIMPLE_ASSIGN. More complex expressions will require
814 gimplification, which will introduce additional statements. In this
815 event, no update is performed, and the function returns false.
816 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
817 replace the statement at *SI_P with an entirely new statement.
818 The new statement need not be a call, e.g., if the original call
819 folded to a constant. */
822 update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
824 gimple stmt
= gsi_stmt (*si_p
);
826 if (valid_gimple_call_p (expr
))
828 /* The call has simplified to another call. */
829 tree fn
= CALL_EXPR_FN (expr
);
831 unsigned nargs
= call_expr_nargs (expr
);
832 vec
<tree
> args
= vNULL
;
838 args
.safe_grow_cleared (nargs
);
840 for (i
= 0; i
< nargs
; i
++)
841 args
[i
] = CALL_EXPR_ARG (expr
, i
);
844 new_stmt
= gimple_build_call_vec (fn
, args
);
845 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
850 else if (valid_gimple_rhs_p (expr
))
852 tree lhs
= gimple_call_lhs (stmt
);
855 /* The call has simplified to an expression
856 that cannot be represented as a GIMPLE_CALL. */
859 /* A value is expected.
860 Introduce a new GIMPLE_ASSIGN statement. */
861 STRIP_USELESS_TYPE_CONVERSION (expr
);
862 new_stmt
= gimple_build_assign (lhs
, expr
);
863 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
864 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
865 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
867 else if (!TREE_SIDE_EFFECTS (expr
))
869 /* No value is expected, and EXPR has no effect.
870 Replace it with an empty statement. */
871 new_stmt
= gimple_build_nop ();
872 if (gimple_in_ssa_p (cfun
))
874 unlink_stmt_vdef (stmt
);
880 /* No value is expected, but EXPR has an effect,
881 e.g., it could be a reference to a volatile
882 variable. Create an assignment statement
883 with a dummy (unused) lhs variable. */
884 STRIP_USELESS_TYPE_CONVERSION (expr
);
885 if (gimple_in_ssa_p (cfun
))
886 lhs
= make_ssa_name (TREE_TYPE (expr
));
888 lhs
= create_tmp_var (TREE_TYPE (expr
));
889 new_stmt
= gimple_build_assign (lhs
, expr
);
890 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
891 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
892 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
894 gimple_set_location (new_stmt
, gimple_location (stmt
));
895 gsi_replace (si_p
, new_stmt
, false);
899 /* The call simplified to an expression that is
900 not a valid GIMPLE RHS. */
905 /* Entry point to the propagation engine.
907 VISIT_STMT is called for every statement visited.
908 VISIT_PHI is called for every PHI node visited. */
911 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt
,
912 ssa_prop_visit_phi_fn visit_phi
)
914 ssa_prop_visit_stmt
= visit_stmt
;
915 ssa_prop_visit_phi
= visit_phi
;
919 /* Iterate until the worklists are empty. */
920 while (!cfg_blocks_empty_p ()
921 || interesting_ssa_edges
.length () > 0
922 || varying_ssa_edges
.length () > 0)
924 if (!cfg_blocks_empty_p ())
926 /* Pull the next block to simulate off the worklist. */
927 basic_block dest_block
= cfg_blocks_get ();
928 simulate_block (dest_block
);
931 /* In order to move things to varying as quickly as
932 possible,process the VARYING_SSA_EDGES worklist first. */
933 process_ssa_edge_worklist (&varying_ssa_edges
);
935 /* Now process the INTERESTING_SSA_EDGES worklist. */
936 process_ssa_edge_worklist (&interesting_ssa_edges
);
943 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
944 is a non-volatile pointer dereference, a structure reference or a
945 reference to a single _DECL. Ignore volatile memory references
946 because they are not interesting for the optimizers. */
949 stmt_makes_single_store (gimple stmt
)
953 if (gimple_code (stmt
) != GIMPLE_ASSIGN
954 && gimple_code (stmt
) != GIMPLE_CALL
)
957 if (!gimple_vdef (stmt
))
960 lhs
= gimple_get_lhs (stmt
);
962 /* A call statement may have a null LHS. */
966 return (!TREE_THIS_VOLATILE (lhs
)
968 || REFERENCE_CLASS_P (lhs
)));
972 /* Propagation statistics. */
977 long num_stmts_folded
;
981 static struct prop_stats_d prop_stats
;
983 /* Replace USE references in statement STMT with the values stored in
984 PROP_VALUE. Return true if at least one reference was replaced. */
987 replace_uses_in (gimple stmt
, ssa_prop_get_value_fn get_value
)
989 bool replaced
= false;
993 FOR_EACH_SSA_USE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
995 tree tuse
= USE_FROM_PTR (use
);
996 tree val
= (*get_value
) (tuse
);
998 if (val
== tuse
|| val
== NULL_TREE
)
1001 if (gimple_code (stmt
) == GIMPLE_ASM
1002 && !may_propagate_copy_into_asm (tuse
))
1005 if (!may_propagate_copy (tuse
, val
))
1008 if (TREE_CODE (val
) != SSA_NAME
)
1009 prop_stats
.num_const_prop
++;
1011 prop_stats
.num_copy_prop
++;
1013 propagate_value (use
, val
);
1022 /* Replace propagated values into all the arguments for PHI using the
1023 values from PROP_VALUE. */
1026 replace_phi_args_in (gphi
*phi
, ssa_prop_get_value_fn get_value
)
1029 bool replaced
= false;
1031 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1033 fprintf (dump_file
, "Folding PHI node: ");
1034 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
1037 basic_block bb
= gimple_bb (phi
);
1038 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1040 tree arg
= gimple_phi_arg_def (phi
, i
);
1042 if (TREE_CODE (arg
) == SSA_NAME
)
1044 tree val
= (*get_value
) (arg
);
1046 if (val
&& val
!= arg
&& may_propagate_copy (arg
, val
))
1048 edge e
= gimple_phi_arg_edge (phi
, i
);
1050 /* Avoid propagating constants into loop latch edge
1051 PHI arguments as this makes coalescing the copy
1052 across this edge impossible. If the argument is
1053 defined by an assert - otherwise the stmt will
1054 get removed without replacing its uses. */
1055 if (TREE_CODE (val
) != SSA_NAME
1056 && bb
->loop_father
->header
== bb
1057 && dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
)
1058 && is_gimple_assign (SSA_NAME_DEF_STMT (arg
))
1059 && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (arg
))
1063 if (TREE_CODE (val
) != SSA_NAME
)
1064 prop_stats
.num_const_prop
++;
1066 prop_stats
.num_copy_prop
++;
1068 propagate_value (PHI_ARG_DEF_PTR (phi
, i
), val
);
1071 /* If we propagated a copy and this argument flows
1072 through an abnormal edge, update the replacement
1074 if (TREE_CODE (val
) == SSA_NAME
1075 && e
->flags
& EDGE_ABNORMAL
1076 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
))
1078 /* This can only occur for virtual operands, since
1079 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1080 would prevent replacement. */
1081 gcc_checking_assert (virtual_operand_p (val
));
1082 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
) = 1;
1088 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1091 fprintf (dump_file
, "No folding possible\n");
1094 fprintf (dump_file
, "Folded into: ");
1095 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
1096 fprintf (dump_file
, "\n");
1104 class substitute_and_fold_dom_walker
: public dom_walker
1107 substitute_and_fold_dom_walker (cdi_direction direction
,
1108 ssa_prop_get_value_fn get_value_fn_
,
1109 ssa_prop_fold_stmt_fn fold_fn_
,
1111 : dom_walker (direction
), get_value_fn (get_value_fn_
),
1112 fold_fn (fold_fn_
), do_dce (do_dce_
), something_changed (false)
1114 stmts_to_remove
.create (0);
1115 stmts_to_fixup
.create (0);
1116 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1118 ~substitute_and_fold_dom_walker ()
1120 stmts_to_remove
.release ();
1121 stmts_to_fixup
.release ();
1122 BITMAP_FREE (need_eh_cleanup
);
1125 virtual void before_dom_children (basic_block
);
1126 virtual void after_dom_children (basic_block
) {}
1128 ssa_prop_get_value_fn get_value_fn
;
1129 ssa_prop_fold_stmt_fn fold_fn
;
1131 bool something_changed
;
1132 vec
<gimple
> stmts_to_remove
;
1133 vec
<gimple
> stmts_to_fixup
;
1134 bitmap need_eh_cleanup
;
1138 substitute_and_fold_dom_walker::before_dom_children (basic_block bb
)
1140 /* Propagate known values into PHI nodes. */
1141 for (gphi_iterator i
= gsi_start_phis (bb
);
1145 gphi
*phi
= i
.phi ();
1146 tree res
= gimple_phi_result (phi
);
1147 if (virtual_operand_p (res
))
1150 && res
&& TREE_CODE (res
) == SSA_NAME
)
1152 tree sprime
= get_value_fn (res
);
1155 && may_propagate_copy (res
, sprime
))
1157 stmts_to_remove
.safe_push (phi
);
1161 something_changed
|= replace_phi_args_in (phi
, get_value_fn
);
1164 /* Propagate known values into stmts. In some case it exposes
1165 more trivially deletable stmts to walk backward. */
1166 for (gimple_stmt_iterator i
= gsi_start_bb (bb
);
1171 gimple stmt
= gsi_stmt (i
);
1172 enum gimple_code code
= gimple_code (stmt
);
1174 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1175 range information for names and they are discarded
1178 if (code
== GIMPLE_ASSIGN
1179 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
1182 /* No point propagating into a stmt we have a value for we
1183 can propagate into all uses. Mark it for removal instead. */
1184 tree lhs
= gimple_get_lhs (stmt
);
1186 && lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1188 tree sprime
= get_value_fn (lhs
);
1191 && may_propagate_copy (lhs
, sprime
)
1192 && !stmt_could_throw_p (stmt
)
1193 && !gimple_has_side_effects (stmt
))
1195 stmts_to_remove
.safe_push (stmt
);
1200 /* Replace the statement with its folded version and mark it
1202 did_replace
= false;
1203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1205 fprintf (dump_file
, "Folding statement: ");
1206 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1209 gimple old_stmt
= stmt
;
1210 bool was_noreturn
= (is_gimple_call (stmt
)
1211 && gimple_call_noreturn_p (stmt
));
1213 /* Some statements may be simplified using propagator
1214 specific information. Do this before propagating
1215 into the stmt to not disturb pass specific information. */
1220 prop_stats
.num_stmts_folded
++;
1221 stmt
= gsi_stmt (i
);
1225 /* Replace real uses in the statement. */
1226 did_replace
|= replace_uses_in (stmt
, get_value_fn
);
1228 /* If we made a replacement, fold the statement. */
1230 fold_stmt (&i
, follow_single_use_edges
);
1235 stmt
= gsi_stmt (i
);
1237 /* If we cleaned up EH information from the statement,
1239 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1240 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
1242 /* If we turned a not noreturn call into a noreturn one
1243 schedule it for fixup. */
1245 && is_gimple_call (stmt
)
1246 && gimple_call_noreturn_p (stmt
))
1247 stmts_to_fixup
.safe_push (stmt
);
1249 if (gimple_assign_single_p (stmt
))
1251 tree rhs
= gimple_assign_rhs1 (stmt
);
1253 if (TREE_CODE (rhs
) == ADDR_EXPR
)
1254 recompute_tree_invariant_for_addr_expr (rhs
);
1257 /* Determine what needs to be done to update the SSA form. */
1259 if (!is_gimple_debug (stmt
))
1260 something_changed
= true;
1263 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1267 fprintf (dump_file
, "Folded into: ");
1268 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1269 fprintf (dump_file
, "\n");
1272 fprintf (dump_file
, "Not folded\n");
1279 /* Perform final substitution and folding of propagated values.
1281 PROP_VALUE[I] contains the single value that should be substituted
1282 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1285 If FOLD_FN is non-NULL the function will be invoked on all statements
1286 before propagating values for pass specific simplification.
1288 DO_DCE is true if trivially dead stmts can be removed.
1290 If DO_DCE is true, the statements within a BB are walked from
1291 last to first element. Otherwise we scan from first to last element.
1293 Return TRUE when something changed. */
1296 substitute_and_fold (ssa_prop_get_value_fn get_value_fn
,
1297 ssa_prop_fold_stmt_fn fold_fn
,
1300 gcc_assert (get_value_fn
);
1302 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1303 fprintf (dump_file
, "\nSubstituting values and folding statements\n\n");
1305 memset (&prop_stats
, 0, sizeof (prop_stats
));
1307 calculate_dominance_info (CDI_DOMINATORS
);
1308 substitute_and_fold_dom_walker
walker(CDI_DOMINATORS
,
1309 get_value_fn
, fold_fn
, do_dce
);
1310 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1312 /* We cannot remove stmts during the BB walk, especially not release
1313 SSA names there as that destroys the lattice of our callers.
1314 Remove stmts in reverse order to make debug stmt creation possible. */
1315 while (!walker
.stmts_to_remove
.is_empty ())
1317 gimple stmt
= walker
.stmts_to_remove
.pop ();
1318 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1320 fprintf (dump_file
, "Removing dead stmt ");
1321 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1322 fprintf (dump_file
, "\n");
1324 prop_stats
.num_dce
++;
1325 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1326 if (gimple_code (stmt
) == GIMPLE_PHI
)
1327 remove_phi_node (&gsi
, true);
1330 unlink_stmt_vdef (stmt
);
1331 gsi_remove (&gsi
, true);
1332 release_defs (stmt
);
1336 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
1337 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
1339 /* Fixup stmts that became noreturn calls. This may require splitting
1340 blocks and thus isn't possible during the dominator walk. Do this
1341 in reverse order so we don't inadvertedly remove a stmt we want to
1342 fixup by visiting a dominating now noreturn call first. */
1343 while (!walker
.stmts_to_fixup
.is_empty ())
1345 gimple stmt
= walker
.stmts_to_fixup
.pop ();
1346 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1348 fprintf (dump_file
, "Fixing up noreturn call ");
1349 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1350 fprintf (dump_file
, "\n");
1352 fixup_noreturn_call (stmt
);
1355 statistics_counter_event (cfun
, "Constants propagated",
1356 prop_stats
.num_const_prop
);
1357 statistics_counter_event (cfun
, "Copies propagated",
1358 prop_stats
.num_copy_prop
);
1359 statistics_counter_event (cfun
, "Statements folded",
1360 prop_stats
.num_stmts_folded
);
1361 statistics_counter_event (cfun
, "Statements deleted",
1362 prop_stats
.num_dce
);
1364 return walker
.something_changed
;
1368 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1371 may_propagate_copy (tree dest
, tree orig
)
1373 tree type_d
= TREE_TYPE (dest
);
1374 tree type_o
= TREE_TYPE (orig
);
1376 /* If ORIG is a default definition which flows in from an abnormal edge
1377 then the copy can be propagated. It is important that we do so to avoid
1378 uninitialized copies. */
1379 if (TREE_CODE (orig
) == SSA_NAME
1380 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
)
1381 && SSA_NAME_IS_DEFAULT_DEF (orig
)
1382 && (SSA_NAME_VAR (orig
) == NULL_TREE
1383 || TREE_CODE (SSA_NAME_VAR (orig
)) == VAR_DECL
))
1385 /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1387 else if (TREE_CODE (orig
) == SSA_NAME
1388 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
1390 /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1392 else if (TREE_CODE (dest
) == SSA_NAME
1393 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest
))
1396 /* Do not copy between types for which we *do* need a conversion. */
1397 if (!useless_type_conversion_p (type_d
, type_o
))
1400 /* Generally propagating virtual operands is not ok as that may
1401 create overlapping life-ranges. */
1402 if (TREE_CODE (dest
) == SSA_NAME
&& virtual_operand_p (dest
))
1405 /* Anything else is OK. */
1409 /* Like may_propagate_copy, but use as the destination expression
1410 the principal expression (typically, the RHS) contained in
1411 statement DEST. This is more efficient when working with the
1412 gimple tuples representation. */
1415 may_propagate_copy_into_stmt (gimple dest
, tree orig
)
1420 /* If the statement is a switch or a single-rhs assignment,
1421 then the expression to be replaced by the propagation may
1422 be an SSA_NAME. Fortunately, there is an explicit tree
1423 for the expression, so we delegate to may_propagate_copy. */
1425 if (gimple_assign_single_p (dest
))
1426 return may_propagate_copy (gimple_assign_rhs1 (dest
), orig
);
1427 else if (gswitch
*dest_swtch
= dyn_cast
<gswitch
*> (dest
))
1428 return may_propagate_copy (gimple_switch_index (dest_swtch
), orig
);
1430 /* In other cases, the expression is not materialized, so there
1431 is no destination to pass to may_propagate_copy. On the other
1432 hand, the expression cannot be an SSA_NAME, so the analysis
1435 if (TREE_CODE (orig
) == SSA_NAME
1436 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
1439 if (is_gimple_assign (dest
))
1440 type_d
= TREE_TYPE (gimple_assign_lhs (dest
));
1441 else if (gimple_code (dest
) == GIMPLE_COND
)
1442 type_d
= boolean_type_node
;
1443 else if (is_gimple_call (dest
)
1444 && gimple_call_lhs (dest
) != NULL_TREE
)
1445 type_d
= TREE_TYPE (gimple_call_lhs (dest
));
1449 type_o
= TREE_TYPE (orig
);
1451 if (!useless_type_conversion_p (type_d
, type_o
))
1457 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1460 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED
)
1466 /* Common code for propagate_value and replace_exp.
1468 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1469 replacement is done to propagate a value or not. */
1472 replace_exp_1 (use_operand_p op_p
, tree val
,
1473 bool for_propagation ATTRIBUTE_UNUSED
)
1475 #if defined ENABLE_CHECKING
1476 tree op
= USE_FROM_PTR (op_p
);
1478 gcc_assert (!(for_propagation
1479 && TREE_CODE (op
) == SSA_NAME
1480 && TREE_CODE (val
) == SSA_NAME
1481 && !may_propagate_copy (op
, val
)));
1484 if (TREE_CODE (val
) == SSA_NAME
)
1485 SET_USE (op_p
, val
);
1487 SET_USE (op_p
, unshare_expr (val
));
1491 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1492 into the operand pointed to by OP_P.
1494 Use this version for const/copy propagation as it will perform additional
1495 checks to ensure validity of the const/copy propagation. */
1498 propagate_value (use_operand_p op_p
, tree val
)
1500 replace_exp_1 (op_p
, val
, true);
1503 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1505 Use this version when not const/copy propagating values. For example,
1506 PRE uses this version when building expressions as they would appear
1507 in specific blocks taking into account actions of PHI nodes.
1509 The statement in which an expression has been replaced should be
1510 folded using fold_stmt_inplace. */
1513 replace_exp (use_operand_p op_p
, tree val
)
1515 replace_exp_1 (op_p
, val
, false);
1519 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1520 into the tree pointed to by OP_P.
1522 Use this version for const/copy propagation when SSA operands are not
1523 available. It will perform the additional checks to ensure validity of
1524 the const/copy propagation, but will not update any operand information.
1525 Be sure to mark the stmt as modified. */
1528 propagate_tree_value (tree
*op_p
, tree val
)
1530 if (TREE_CODE (val
) == SSA_NAME
)
1533 *op_p
= unshare_expr (val
);
1537 /* Like propagate_tree_value, but use as the operand to replace
1538 the principal expression (typically, the RHS) contained in the
1539 statement referenced by iterator GSI. Note that it is not
1540 always possible to update the statement in-place, so a new
1541 statement may be created to replace the original. */
1544 propagate_tree_value_into_stmt (gimple_stmt_iterator
*gsi
, tree val
)
1546 gimple stmt
= gsi_stmt (*gsi
);
1548 if (is_gimple_assign (stmt
))
1550 tree expr
= NULL_TREE
;
1551 if (gimple_assign_single_p (stmt
))
1552 expr
= gimple_assign_rhs1 (stmt
);
1553 propagate_tree_value (&expr
, val
);
1554 gimple_assign_set_rhs_from_tree (gsi
, expr
);
1556 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
1558 tree lhs
= NULL_TREE
;
1559 tree rhs
= build_zero_cst (TREE_TYPE (val
));
1560 propagate_tree_value (&lhs
, val
);
1561 gimple_cond_set_code (cond_stmt
, NE_EXPR
);
1562 gimple_cond_set_lhs (cond_stmt
, lhs
);
1563 gimple_cond_set_rhs (cond_stmt
, rhs
);
1565 else if (is_gimple_call (stmt
)
1566 && gimple_call_lhs (stmt
) != NULL_TREE
)
1568 tree expr
= NULL_TREE
;
1570 propagate_tree_value (&expr
, val
);
1571 res
= update_call_from_tree (gsi
, expr
);
1574 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
1575 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt
), val
);