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
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 "basic-block.h"
30 #include "gimple-pretty-print.h"
33 #include "tree-ssa-propagate.h"
34 #include "langhooks.h"
36 #include "value-prof.h"
39 /* This file implements a generic value propagation engine based on
40 the same propagation used by the SSA-CCP algorithm [1].
42 Propagation is performed by simulating the execution of every
43 statement that produces the value being propagated. Simulation
46 1- Initially, all edges of the CFG are marked not executable and
47 the CFG worklist is seeded with all the statements in the entry
48 basic block (block 0).
50 2- Every statement S is simulated with a call to the call-back
51 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
54 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
55 interest and does not affect any of the work lists.
57 SSA_PROP_VARYING: The value produced by S cannot be determined
58 at compile time. Further simulation of S is not required.
59 If S is a conditional jump, all the outgoing edges for the
60 block are considered executable and added to the work
63 SSA_PROP_INTERESTING: S produces a value that can be computed
64 at compile time. Its result can be propagated into the
65 statements that feed from S. Furthermore, if S is a
66 conditional jump, only the edge known to be taken is added
67 to the work list. Edges that are known not to execute are
70 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
71 return value from SSA_PROP_VISIT_PHI has the same semantics as
74 4- Three work lists are kept. Statements are only added to these
75 lists if they produce one of SSA_PROP_INTERESTING or
78 CFG_BLOCKS contains the list of blocks to be simulated.
79 Blocks are added to this list if their incoming edges are
82 VARYING_SSA_EDGES contains the list of statements that feed
83 from statements that produce an SSA_PROP_VARYING result.
84 These are simulated first to speed up processing.
86 INTERESTING_SSA_EDGES contains the list of statements that
87 feed from statements that produce an SSA_PROP_INTERESTING
90 5- Simulation terminates when all three work lists are drained.
92 Before calling ssa_propagate, it is important to clear
93 prop_simulate_again_p for all the statements in the program that
94 should be simulated. This initialization allows an implementation
95 to specify which statements should never be simulated.
97 It is also important to compute def-use information before calling
102 [1] Constant propagation with conditional branches,
103 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
105 [2] Building an Optimizing Compiler,
106 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
108 [3] Advanced Compiler Design and Implementation,
109 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
111 /* Function pointers used to parameterize the propagation engine. */
112 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt
;
113 static ssa_prop_visit_phi_fn ssa_prop_visit_phi
;
115 /* Keep track of statements that have been added to one of the SSA
116 edges worklists. This flag is used to avoid visiting statements
117 unnecessarily when draining an SSA edge worklist. If while
118 simulating a basic block, we find a statement with
119 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
120 processing from visiting it again.
122 NOTE: users of the propagation engine are not allowed to use
123 the GF_PLF_1 flag. */
124 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
126 /* A bitmap to keep track of executable blocks in the CFG. */
127 static sbitmap executable_blocks
;
129 /* Array of control flow edges on the worklist. */
130 static vec
<basic_block
> cfg_blocks
;
132 static unsigned int cfg_blocks_num
= 0;
133 static int cfg_blocks_tail
;
134 static int cfg_blocks_head
;
136 static sbitmap bb_in_list
;
138 /* Worklist of SSA edges which will need reexamination as their
139 definition has changed. SSA edges are def-use edges in the SSA
140 web. For each D-U edge, we store the target statement or PHI node
142 static GTY(()) vec
<gimple
, va_gc
> *interesting_ssa_edges
;
144 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
145 list of SSA edges is split into two. One contains all SSA edges
146 who need to be reexamined because their lattice value changed to
147 varying (this worklist), and the other contains all other SSA edges
148 to be reexamined (INTERESTING_SSA_EDGES).
150 Since most values in the program are VARYING, the ideal situation
151 is to move them to that lattice value as quickly as possible.
152 Thus, it doesn't make sense to process any other type of lattice
153 value until all VARYING values are propagated fully, which is one
154 thing using the VARYING worklist achieves. In addition, if we
155 don't use a separate worklist for VARYING edges, we end up with
156 situations where lattice values move from
157 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
158 static GTY(()) vec
<gimple
, va_gc
> *varying_ssa_edges
;
161 /* Return true if the block worklist empty. */
164 cfg_blocks_empty_p (void)
166 return (cfg_blocks_num
== 0);
170 /* Add a basic block to the worklist. The block must not be already
171 in the worklist, and it must not be the ENTRY or EXIT block. */
174 cfg_blocks_add (basic_block bb
)
178 gcc_assert (bb
!= ENTRY_BLOCK_PTR
&& bb
!= EXIT_BLOCK_PTR
);
179 gcc_assert (!bitmap_bit_p (bb_in_list
, bb
->index
));
181 if (cfg_blocks_empty_p ())
183 cfg_blocks_tail
= cfg_blocks_head
= 0;
189 if (cfg_blocks_num
> cfg_blocks
.length ())
191 /* We have to grow the array now. Adjust to queue to occupy
192 the full space of the original array. We do not need to
193 initialize the newly allocated portion of the array
194 because we keep track of CFG_BLOCKS_HEAD and
196 cfg_blocks_tail
= cfg_blocks
.length ();
198 cfg_blocks
.safe_grow (2 * cfg_blocks_tail
);
200 /* Minor optimization: we prefer to see blocks with more
201 predecessors later, because there is more of a chance that
202 the incoming edges will be executable. */
203 else if (EDGE_COUNT (bb
->preds
)
204 >= EDGE_COUNT (cfg_blocks
[cfg_blocks_head
]->preds
))
205 cfg_blocks_tail
= ((cfg_blocks_tail
+ 1) % cfg_blocks
.length ());
208 if (cfg_blocks_head
== 0)
209 cfg_blocks_head
= cfg_blocks
.length ();
215 cfg_blocks
[head
? cfg_blocks_head
: cfg_blocks_tail
] = bb
;
216 bitmap_set_bit (bb_in_list
, bb
->index
);
220 /* Remove a block from the worklist. */
223 cfg_blocks_get (void)
227 bb
= cfg_blocks
[cfg_blocks_head
];
229 gcc_assert (!cfg_blocks_empty_p ());
232 cfg_blocks_head
= ((cfg_blocks_head
+ 1) % cfg_blocks
.length ());
234 bitmap_clear_bit (bb_in_list
, bb
->index
);
240 /* We have just defined a new value for VAR. If IS_VARYING is true,
241 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
242 them to INTERESTING_SSA_EDGES. */
245 add_ssa_edge (tree var
, bool is_varying
)
247 imm_use_iterator iter
;
250 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
252 gimple use_stmt
= USE_STMT (use_p
);
254 if (prop_simulate_again_p (use_stmt
)
255 && !gimple_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
257 gimple_set_plf (use_stmt
, STMT_IN_SSA_EDGE_WORKLIST
, true);
259 vec_safe_push (varying_ssa_edges
, use_stmt
);
261 vec_safe_push (interesting_ssa_edges
, use_stmt
);
267 /* Add edge E to the control flow worklist. */
270 add_control_edge (edge e
)
272 basic_block bb
= e
->dest
;
273 if (bb
== EXIT_BLOCK_PTR
)
276 /* If the edge had already been executed, skip it. */
277 if (e
->flags
& EDGE_EXECUTABLE
)
280 e
->flags
|= EDGE_EXECUTABLE
;
282 /* If the block is already in the list, we're done. */
283 if (bitmap_bit_p (bb_in_list
, bb
->index
))
288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
289 fprintf (dump_file
, "Adding Destination of edge (%d -> %d) to worklist\n\n",
290 e
->src
->index
, e
->dest
->index
);
294 /* Simulate the execution of STMT and update the work lists accordingly. */
297 simulate_stmt (gimple stmt
)
299 enum ssa_prop_result val
= SSA_PROP_NOT_INTERESTING
;
300 edge taken_edge
= NULL
;
301 tree output_name
= NULL_TREE
;
303 /* Don't bother visiting statements that are already
304 considered varying by the propagator. */
305 if (!prop_simulate_again_p (stmt
))
308 if (gimple_code (stmt
) == GIMPLE_PHI
)
310 val
= ssa_prop_visit_phi (stmt
);
311 output_name
= gimple_phi_result (stmt
);
314 val
= ssa_prop_visit_stmt (stmt
, &taken_edge
, &output_name
);
316 if (val
== SSA_PROP_VARYING
)
318 prop_set_simulate_again (stmt
, false);
320 /* If the statement produced a new varying value, add the SSA
321 edges coming out of OUTPUT_NAME. */
323 add_ssa_edge (output_name
, true);
325 /* If STMT transfers control out of its basic block, add
326 all outgoing edges to the work list. */
327 if (stmt_ends_bb_p (stmt
))
331 basic_block bb
= gimple_bb (stmt
);
332 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
333 add_control_edge (e
);
336 else if (val
== SSA_PROP_INTERESTING
)
338 /* If the statement produced new value, add the SSA edges coming
339 out of OUTPUT_NAME. */
341 add_ssa_edge (output_name
, false);
343 /* If we know which edge is going to be taken out of this block,
344 add it to the CFG work list. */
346 add_control_edge (taken_edge
);
350 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
351 drain. This pops statements off the given WORKLIST and processes
352 them until there are no more statements on WORKLIST.
353 We take a pointer to WORKLIST because it may be reallocated when an
354 SSA edge is added to it in simulate_stmt. */
357 process_ssa_edge_worklist (vec
<gimple
, va_gc
> **worklist
)
359 /* Drain the entire worklist. */
360 while ((*worklist
)->length () > 0)
364 /* Pull the statement to simulate off the worklist. */
365 gimple stmt
= (*worklist
)->pop ();
367 /* If this statement was already visited by simulate_block, then
368 we don't need to visit it again here. */
369 if (!gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
372 /* STMT is no longer in a worklist. */
373 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
377 fprintf (dump_file
, "\nSimulating statement (from ssa_edges): ");
378 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
381 bb
= gimple_bb (stmt
);
383 /* PHI nodes are always visited, regardless of whether or not
384 the destination block is executable. Otherwise, visit the
385 statement only if its block is marked executable. */
386 if (gimple_code (stmt
) == GIMPLE_PHI
387 || bitmap_bit_p (executable_blocks
, bb
->index
))
388 simulate_stmt (stmt
);
393 /* Simulate the execution of BLOCK. Evaluate the statement associated
394 with each variable reference inside the block. */
397 simulate_block (basic_block block
)
399 gimple_stmt_iterator gsi
;
401 /* There is nothing to do for the exit block. */
402 if (block
== EXIT_BLOCK_PTR
)
405 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
406 fprintf (dump_file
, "\nSimulating block %d\n", block
->index
);
408 /* Always simulate PHI nodes, even if we have simulated this block
410 for (gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
411 simulate_stmt (gsi_stmt (gsi
));
413 /* If this is the first time we've simulated this block, then we
414 must simulate each of its statements. */
415 if (!bitmap_bit_p (executable_blocks
, block
->index
))
417 gimple_stmt_iterator j
;
418 unsigned int normal_edge_count
;
422 /* Note that we have simulated this block. */
423 bitmap_set_bit (executable_blocks
, block
->index
);
425 for (j
= gsi_start_bb (block
); !gsi_end_p (j
); gsi_next (&j
))
427 gimple stmt
= gsi_stmt (j
);
429 /* If this statement is already in the worklist then
430 "cancel" it. The reevaluation implied by the worklist
431 entry will produce the same value we generate here and
432 thus reevaluating it again from the worklist is
434 if (gimple_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
))
435 gimple_set_plf (stmt
, STMT_IN_SSA_EDGE_WORKLIST
, false);
437 simulate_stmt (stmt
);
440 /* We can not predict when abnormal and EH edges will be executed, so
441 once a block is considered executable, we consider any
442 outgoing abnormal edges as executable.
444 TODO: This is not exactly true. Simplifying statement might
445 prove it non-throwing and also computed goto can be handled
446 when destination is known.
448 At the same time, if this block has only one successor that is
449 reached by non-abnormal edges, then add that successor to the
451 normal_edge_count
= 0;
453 FOR_EACH_EDGE (e
, ei
, block
->succs
)
455 if (e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
))
456 add_control_edge (e
);
464 if (normal_edge_count
== 1)
465 add_control_edge (normal_edge
);
470 /* Initialize local data structures and work lists. */
479 /* Worklists of SSA edges. */
480 vec_alloc (interesting_ssa_edges
, 20);
481 vec_alloc (varying_ssa_edges
, 20);
483 executable_blocks
= sbitmap_alloc (last_basic_block
);
484 bitmap_clear (executable_blocks
);
486 bb_in_list
= sbitmap_alloc (last_basic_block
);
487 bitmap_clear (bb_in_list
);
489 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
490 dump_immediate_uses (dump_file
);
492 cfg_blocks
.create (20);
493 cfg_blocks
.safe_grow_cleared (20);
495 /* Initially assume that every edge in the CFG is not executable.
496 (including the edges coming out of ENTRY_BLOCK_PTR). */
499 gimple_stmt_iterator si
;
501 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
502 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
504 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
505 gimple_set_plf (gsi_stmt (si
), STMT_IN_SSA_EDGE_WORKLIST
, false);
507 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
508 e
->flags
&= ~EDGE_EXECUTABLE
;
511 /* Seed the algorithm by adding the successors of the entry block to the
513 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
514 add_control_edge (e
);
518 /* Free allocated storage. */
523 vec_free (interesting_ssa_edges
);
524 vec_free (varying_ssa_edges
);
525 cfg_blocks
.release ();
526 sbitmap_free (bb_in_list
);
527 sbitmap_free (executable_blocks
);
531 /* Return true if EXPR is an acceptable right-hand-side for a
532 GIMPLE assignment. We validate the entire tree, not just
533 the root node, thus catching expressions that embed complex
534 operands that are not permitted in GIMPLE. This function
535 is needed because the folding routines in fold-const.c
536 may return such expressions in some cases, e.g., an array
537 access with an embedded index addition. It may make more
538 sense to have folding routines that are sensitive to the
539 constraints on GIMPLE operands, rather than abandoning any
540 any attempt to fold if the usual folding turns out to be too
544 valid_gimple_rhs_p (tree expr
)
546 enum tree_code code
= TREE_CODE (expr
);
548 switch (TREE_CODE_CLASS (code
))
550 case tcc_declaration
:
551 if (!is_gimple_variable (expr
))
556 /* All constants are ok. */
561 if (!is_gimple_val (TREE_OPERAND (expr
, 0))
562 || !is_gimple_val (TREE_OPERAND (expr
, 1)))
567 if (!is_gimple_val (TREE_OPERAND (expr
, 0)))
577 if (is_gimple_min_invariant (expr
))
579 t
= TREE_OPERAND (expr
, 0);
580 while (handled_component_p (t
))
582 /* ??? More checks needed, see the GIMPLE verifier. */
583 if ((TREE_CODE (t
) == ARRAY_REF
584 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
585 && !is_gimple_val (TREE_OPERAND (t
, 1)))
587 t
= TREE_OPERAND (t
, 0);
589 if (!is_gimple_id (t
))
595 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
597 if (((code
== VEC_COND_EXPR
|| code
== COND_EXPR
)
598 ? !is_gimple_condexpr (TREE_OPERAND (expr
, 0))
599 : !is_gimple_val (TREE_OPERAND (expr
, 0)))
600 || !is_gimple_val (TREE_OPERAND (expr
, 1))
601 || !is_gimple_val (TREE_OPERAND (expr
, 2)))
612 case tcc_exceptional
:
613 if (code
== CONSTRUCTOR
)
617 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
618 if (!is_gimple_val (elt
))
622 if (code
!= SSA_NAME
)
627 if (code
== BIT_FIELD_REF
)
628 return is_gimple_val (TREE_OPERAND (expr
, 0));
639 /* Return true if EXPR is a CALL_EXPR suitable for representation
640 as a single GIMPLE_CALL statement. If the arguments require
641 further gimplification, return false. */
644 valid_gimple_call_p (tree expr
)
648 if (TREE_CODE (expr
) != CALL_EXPR
)
651 nargs
= call_expr_nargs (expr
);
652 for (i
= 0; i
< nargs
; i
++)
654 tree arg
= CALL_EXPR_ARG (expr
, i
);
655 if (is_gimple_reg_type (arg
))
657 if (!is_gimple_val (arg
))
661 if (!is_gimple_lvalue (arg
))
669 /* Make SSA names defined by OLD_STMT point to NEW_STMT
670 as their defining statement. */
673 move_ssa_defining_stmt_for_defs (gimple new_stmt
, gimple old_stmt
)
678 if (gimple_in_ssa_p (cfun
))
680 /* Make defined SSA_NAMEs point to the new
681 statement as their definition. */
682 FOR_EACH_SSA_TREE_OPERAND (var
, old_stmt
, iter
, SSA_OP_ALL_DEFS
)
684 if (TREE_CODE (var
) == SSA_NAME
)
685 SSA_NAME_DEF_STMT (var
) = new_stmt
;
690 /* Helper function for update_gimple_call and update_call_from_tree.
691 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
694 finish_update_gimple_call (gimple_stmt_iterator
*si_p
, gimple new_stmt
,
697 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
698 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
699 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
700 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
701 gimple_set_location (new_stmt
, gimple_location (stmt
));
702 if (gimple_block (new_stmt
) == NULL_TREE
)
703 gimple_set_block (new_stmt
, gimple_block (stmt
));
704 gsi_replace (si_p
, new_stmt
, false);
707 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
708 with number of arguments NARGS, where the arguments in GIMPLE form
709 follow NARGS argument. */
712 update_gimple_call (gimple_stmt_iterator
*si_p
, tree fn
, int nargs
, ...)
715 gimple new_stmt
, stmt
= gsi_stmt (*si_p
);
717 gcc_assert (is_gimple_call (stmt
));
718 va_start (ap
, nargs
);
719 new_stmt
= gimple_build_call_valist (fn
, nargs
, ap
);
720 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
725 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
726 value of EXPR, which is expected to be the result of folding the
727 call. This can only be done if EXPR is a CALL_EXPR with valid
728 GIMPLE operands as arguments, or if it is a suitable RHS expression
729 for a GIMPLE_ASSIGN. More complex expressions will require
730 gimplification, which will introduce additional statements. In this
731 event, no update is performed, and the function returns false.
732 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
733 replace the statement at *SI_P with an entirely new statement.
734 The new statement need not be a call, e.g., if the original call
735 folded to a constant. */
738 update_call_from_tree (gimple_stmt_iterator
*si_p
, tree expr
)
740 gimple stmt
= gsi_stmt (*si_p
);
742 if (valid_gimple_call_p (expr
))
744 /* The call has simplified to another call. */
745 tree fn
= CALL_EXPR_FN (expr
);
747 unsigned nargs
= call_expr_nargs (expr
);
748 vec
<tree
> args
= vNULL
;
754 args
.safe_grow_cleared (nargs
);
756 for (i
= 0; i
< nargs
; i
++)
757 args
[i
] = CALL_EXPR_ARG (expr
, i
);
760 new_stmt
= gimple_build_call_vec (fn
, args
);
761 finish_update_gimple_call (si_p
, new_stmt
, stmt
);
766 else if (valid_gimple_rhs_p (expr
))
768 tree lhs
= gimple_call_lhs (stmt
);
771 /* The call has simplified to an expression
772 that cannot be represented as a GIMPLE_CALL. */
775 /* A value is expected.
776 Introduce a new GIMPLE_ASSIGN statement. */
777 STRIP_USELESS_TYPE_CONVERSION (expr
);
778 new_stmt
= gimple_build_assign (lhs
, expr
);
779 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
780 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
781 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
783 else if (!TREE_SIDE_EFFECTS (expr
))
785 /* No value is expected, and EXPR has no effect.
786 Replace it with an empty statement. */
787 new_stmt
= gimple_build_nop ();
788 if (gimple_in_ssa_p (cfun
))
790 unlink_stmt_vdef (stmt
);
796 /* No value is expected, but EXPR has an effect,
797 e.g., it could be a reference to a volatile
798 variable. Create an assignment statement
799 with a dummy (unused) lhs variable. */
800 STRIP_USELESS_TYPE_CONVERSION (expr
);
801 if (gimple_in_ssa_p (cfun
))
802 lhs
= make_ssa_name (TREE_TYPE (expr
), NULL
);
804 lhs
= create_tmp_var (TREE_TYPE (expr
), NULL
);
805 new_stmt
= gimple_build_assign (lhs
, expr
);
806 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
807 gimple_set_vdef (new_stmt
, gimple_vdef (stmt
));
808 move_ssa_defining_stmt_for_defs (new_stmt
, stmt
);
810 gimple_set_location (new_stmt
, gimple_location (stmt
));
811 gsi_replace (si_p
, new_stmt
, false);
815 /* The call simplified to an expression that is
816 not a valid GIMPLE RHS. */
821 /* Entry point to the propagation engine.
823 VISIT_STMT is called for every statement visited.
824 VISIT_PHI is called for every PHI node visited. */
827 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt
,
828 ssa_prop_visit_phi_fn visit_phi
)
830 ssa_prop_visit_stmt
= visit_stmt
;
831 ssa_prop_visit_phi
= visit_phi
;
835 /* Iterate until the worklists are empty. */
836 while (!cfg_blocks_empty_p ()
837 || interesting_ssa_edges
->length () > 0
838 || varying_ssa_edges
->length () > 0)
840 if (!cfg_blocks_empty_p ())
842 /* Pull the next block to simulate off the worklist. */
843 basic_block dest_block
= cfg_blocks_get ();
844 simulate_block (dest_block
);
847 /* In order to move things to varying as quickly as
848 possible,process the VARYING_SSA_EDGES worklist first. */
849 process_ssa_edge_worklist (&varying_ssa_edges
);
851 /* Now process the INTERESTING_SSA_EDGES worklist. */
852 process_ssa_edge_worklist (&interesting_ssa_edges
);
859 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
860 is a non-volatile pointer dereference, a structure reference or a
861 reference to a single _DECL. Ignore volatile memory references
862 because they are not interesting for the optimizers. */
865 stmt_makes_single_store (gimple stmt
)
869 if (gimple_code (stmt
) != GIMPLE_ASSIGN
870 && gimple_code (stmt
) != GIMPLE_CALL
)
873 if (!gimple_vdef (stmt
))
876 lhs
= gimple_get_lhs (stmt
);
878 /* A call statement may have a null LHS. */
882 return (!TREE_THIS_VOLATILE (lhs
)
884 || REFERENCE_CLASS_P (lhs
)));
888 /* Propagation statistics. */
893 long num_stmts_folded
;
897 static struct prop_stats_d prop_stats
;
899 /* Replace USE references in statement STMT with the values stored in
900 PROP_VALUE. Return true if at least one reference was replaced. */
903 replace_uses_in (gimple stmt
, ssa_prop_get_value_fn get_value
)
905 bool replaced
= false;
909 FOR_EACH_SSA_USE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
911 tree tuse
= USE_FROM_PTR (use
);
912 tree val
= (*get_value
) (tuse
);
914 if (val
== tuse
|| val
== NULL_TREE
)
917 if (gimple_code (stmt
) == GIMPLE_ASM
918 && !may_propagate_copy_into_asm (tuse
))
921 if (!may_propagate_copy (tuse
, val
))
924 if (TREE_CODE (val
) != SSA_NAME
)
925 prop_stats
.num_const_prop
++;
927 prop_stats
.num_copy_prop
++;
929 propagate_value (use
, val
);
938 /* Replace propagated values into all the arguments for PHI using the
939 values from PROP_VALUE. */
942 replace_phi_args_in (gimple phi
, ssa_prop_get_value_fn get_value
)
945 bool replaced
= false;
947 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
949 fprintf (dump_file
, "Folding PHI node: ");
950 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
953 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
955 tree arg
= gimple_phi_arg_def (phi
, i
);
957 if (TREE_CODE (arg
) == SSA_NAME
)
959 tree val
= (*get_value
) (arg
);
961 if (val
&& val
!= arg
&& may_propagate_copy (arg
, val
))
963 if (TREE_CODE (val
) != SSA_NAME
)
964 prop_stats
.num_const_prop
++;
966 prop_stats
.num_copy_prop
++;
968 propagate_value (PHI_ARG_DEF_PTR (phi
, i
), val
);
971 /* If we propagated a copy and this argument flows
972 through an abnormal edge, update the replacement
974 if (TREE_CODE (val
) == SSA_NAME
975 && gimple_phi_arg_edge (phi
, i
)->flags
& EDGE_ABNORMAL
)
976 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
) = 1;
981 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
984 fprintf (dump_file
, "No folding possible\n");
987 fprintf (dump_file
, "Folded into: ");
988 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
989 fprintf (dump_file
, "\n");
995 /* Perform final substitution and folding of propagated values.
997 PROP_VALUE[I] contains the single value that should be substituted
998 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1001 If FOLD_FN is non-NULL the function will be invoked on all statements
1002 before propagating values for pass specific simplification.
1004 DO_DCE is true if trivially dead stmts can be removed.
1006 If DO_DCE is true, the statements within a BB are walked from
1007 last to first element. Otherwise we scan from first to last element.
1009 Return TRUE when something changed. */
1012 substitute_and_fold (ssa_prop_get_value_fn get_value_fn
,
1013 ssa_prop_fold_stmt_fn fold_fn
,
1017 bool something_changed
= false;
1020 if (!get_value_fn
&& !fold_fn
)
1023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1024 fprintf (dump_file
, "\nSubstituting values and folding statements\n\n");
1026 memset (&prop_stats
, 0, sizeof (prop_stats
));
1028 /* Substitute lattice values at definition sites. */
1030 for (i
= 1; i
< num_ssa_names
; ++i
)
1032 tree name
= ssa_name (i
);
1035 gimple_stmt_iterator gsi
;
1038 || virtual_operand_p (name
))
1041 def_stmt
= SSA_NAME_DEF_STMT (name
);
1042 if (gimple_nop_p (def_stmt
)
1043 /* Do not substitute ASSERT_EXPR rhs, this will confuse VRP. */
1044 || (gimple_assign_single_p (def_stmt
)
1045 && gimple_assign_rhs_code (def_stmt
) == ASSERT_EXPR
)
1046 || !(val
= (*get_value_fn
) (name
))
1047 || !may_propagate_copy (name
, val
))
1050 gsi
= gsi_for_stmt (def_stmt
);
1051 if (is_gimple_assign (def_stmt
))
1053 gimple_assign_set_rhs_with_ops (&gsi
, TREE_CODE (val
),
1055 gcc_assert (gsi_stmt (gsi
) == def_stmt
);
1056 if (maybe_clean_eh_stmt (def_stmt
))
1057 gimple_purge_dead_eh_edges (gimple_bb (def_stmt
));
1058 update_stmt (def_stmt
);
1060 else if (is_gimple_call (def_stmt
))
1062 int flags
= gimple_call_flags (def_stmt
);
1064 /* Don't optimize away calls that have side-effects. */
1065 if ((flags
& (ECF_CONST
|ECF_PURE
)) == 0
1066 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
1068 if (update_call_from_tree (&gsi
, val
)
1069 && maybe_clean_or_replace_eh_stmt (def_stmt
, gsi_stmt (gsi
)))
1070 gimple_purge_dead_eh_edges (gimple_bb (gsi_stmt (gsi
)));
1072 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1074 gimple new_stmt
= gimple_build_assign (name
, val
);
1075 gimple_stmt_iterator gsi2
;
1076 SSA_NAME_DEF_STMT (name
) = new_stmt
;
1077 gsi2
= gsi_after_labels (gimple_bb (def_stmt
));
1078 gsi_insert_before (&gsi2
, new_stmt
, GSI_SAME_STMT
);
1079 remove_phi_node (&gsi
, false);
1082 something_changed
= true;
1085 /* Propagate into all uses and fold. */
1088 gimple_stmt_iterator i
;
1090 /* Propagate known values into PHI nodes. */
1092 for (i
= gsi_start_phis (bb
); !gsi_end_p (i
); gsi_next (&i
))
1093 replace_phi_args_in (gsi_stmt (i
), get_value_fn
);
1095 /* Propagate known values into stmts. Do a backward walk if
1096 do_dce is true. In some case it exposes
1097 more trivially deletable stmts to walk backward. */
1098 for (i
= (do_dce
? gsi_last_bb (bb
) : gsi_start_bb (bb
)); !gsi_end_p (i
);)
1101 gimple stmt
= gsi_stmt (i
);
1103 enum gimple_code code
= gimple_code (stmt
);
1104 gimple_stmt_iterator oldi
;
1112 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1113 range information for names and they are discarded
1116 if (code
== GIMPLE_ASSIGN
1117 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
1120 /* No point propagating into a stmt whose result is not used,
1121 but instead we might be able to remove a trivially dead stmt.
1122 Don't do this when called from VRP, since the SSA_NAME which
1123 is going to be released could be still referenced in VRP
1126 && gimple_get_lhs (stmt
)
1127 && TREE_CODE (gimple_get_lhs (stmt
)) == SSA_NAME
1128 && has_zero_uses (gimple_get_lhs (stmt
))
1129 && !stmt_could_throw_p (stmt
)
1130 && !gimple_has_side_effects (stmt
))
1132 gimple_stmt_iterator i2
;
1134 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1136 fprintf (dump_file
, "Removing dead stmt ");
1137 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1138 fprintf (dump_file
, "\n");
1140 prop_stats
.num_dce
++;
1141 i2
= gsi_for_stmt (stmt
);
1142 gsi_remove (&i2
, true);
1143 release_defs (stmt
);
1147 /* Replace the statement with its folded version and mark it
1149 did_replace
= false;
1150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1152 fprintf (dump_file
, "Folding statement: ");
1153 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1158 /* Some statements may be simplified using propagator
1159 specific information. Do this before propagating
1160 into the stmt to not disturb pass specific information. */
1162 && (*fold_fn
)(&oldi
))
1165 prop_stats
.num_stmts_folded
++;
1166 stmt
= gsi_stmt (oldi
);
1170 /* Replace real uses in the statement. */
1172 did_replace
|= replace_uses_in (stmt
, get_value_fn
);
1174 /* If we made a replacement, fold the statement. */
1181 stmt
= gsi_stmt (oldi
);
1183 /* If we cleaned up EH information from the statement,
1185 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1186 gimple_purge_dead_eh_edges (bb
);
1188 if (is_gimple_assign (stmt
)
1189 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
))
1190 == GIMPLE_SINGLE_RHS
))
1192 tree rhs
= gimple_assign_rhs1 (stmt
);
1194 if (TREE_CODE (rhs
) == ADDR_EXPR
)
1195 recompute_tree_invariant_for_addr_expr (rhs
);
1198 /* Determine what needs to be done to update the SSA form. */
1200 if (!is_gimple_debug (stmt
))
1201 something_changed
= true;
1204 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1208 fprintf (dump_file
, "Folded into: ");
1209 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1210 fprintf (dump_file
, "\n");
1213 fprintf (dump_file
, "Not folded\n");
1218 statistics_counter_event (cfun
, "Constants propagated",
1219 prop_stats
.num_const_prop
);
1220 statistics_counter_event (cfun
, "Copies propagated",
1221 prop_stats
.num_copy_prop
);
1222 statistics_counter_event (cfun
, "Statements folded",
1223 prop_stats
.num_stmts_folded
);
1224 statistics_counter_event (cfun
, "Statements deleted",
1225 prop_stats
.num_dce
);
1226 return something_changed
;
1229 #include "gt-tree-ssa-propagate.h"