Bump version number, post release.
[official-gcc.git] / gcc-4_9-branch / gcc / tree-ssa-propagate.c
blob172b6386e1654e19269ffe7a588eafbeb01bddf1
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple-pretty-print.h"
31 #include "dumpfile.h"
32 #include "sbitmap.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-fold.h"
36 #include "tree-eh.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimplify.h"
41 #include "gimple-iterator.h"
42 #include "gimple-ssa.h"
43 #include "tree-cfg.h"
44 #include "tree-phinodes.h"
45 #include "ssa-iterators.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "tree-ssa.h"
49 #include "tree-ssa-propagate.h"
50 #include "langhooks.h"
51 #include "value-prof.h"
53 /* This file implements a generic value propagation engine based on
54 the same propagation used by the SSA-CCP algorithm [1].
56 Propagation is performed by simulating the execution of every
57 statement that produces the value being propagated. Simulation
58 proceeds as follows:
60 1- Initially, all edges of the CFG are marked not executable and
61 the CFG worklist is seeded with all the statements in the entry
62 basic block (block 0).
64 2- Every statement S is simulated with a call to the call-back
65 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
66 results:
68 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
69 interest and does not affect any of the work lists.
71 SSA_PROP_VARYING: The value produced by S cannot be determined
72 at compile time. Further simulation of S is not required.
73 If S is a conditional jump, all the outgoing edges for the
74 block are considered executable and added to the work
75 list.
77 SSA_PROP_INTERESTING: S produces a value that can be computed
78 at compile time. Its result can be propagated into the
79 statements that feed from S. Furthermore, if S is a
80 conditional jump, only the edge known to be taken is added
81 to the work list. Edges that are known not to execute are
82 never simulated.
84 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
85 return value from SSA_PROP_VISIT_PHI has the same semantics as
86 described in #2.
88 4- Three work lists are kept. Statements are only added to these
89 lists if they produce one of SSA_PROP_INTERESTING or
90 SSA_PROP_VARYING.
92 CFG_BLOCKS contains the list of blocks to be simulated.
93 Blocks are added to this list if their incoming edges are
94 found executable.
96 VARYING_SSA_EDGES contains the list of statements that feed
97 from statements that produce an SSA_PROP_VARYING result.
98 These are simulated first to speed up processing.
100 INTERESTING_SSA_EDGES contains the list of statements that
101 feed from statements that produce an SSA_PROP_INTERESTING
102 result.
104 5- Simulation terminates when all three work lists are drained.
106 Before calling ssa_propagate, it is important to clear
107 prop_simulate_again_p for all the statements in the program that
108 should be simulated. This initialization allows an implementation
109 to specify which statements should never be simulated.
111 It is also important to compute def-use information before calling
112 ssa_propagate.
114 References:
116 [1] Constant propagation with conditional branches,
117 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
119 [2] Building an Optimizing Compiler,
120 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
122 [3] Advanced Compiler Design and Implementation,
123 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
125 /* Function pointers used to parameterize the propagation engine. */
126 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
127 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
129 /* Keep track of statements that have been added to one of the SSA
130 edges worklists. This flag is used to avoid visiting statements
131 unnecessarily when draining an SSA edge worklist. If while
132 simulating a basic block, we find a statement with
133 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
134 processing from visiting it again.
136 NOTE: users of the propagation engine are not allowed to use
137 the GF_PLF_1 flag. */
138 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
140 /* A bitmap to keep track of executable blocks in the CFG. */
141 static sbitmap executable_blocks;
143 /* Array of control flow edges on the worklist. */
144 static vec<basic_block> cfg_blocks;
146 static unsigned int cfg_blocks_num = 0;
147 static int cfg_blocks_tail;
148 static int cfg_blocks_head;
150 static sbitmap bb_in_list;
152 /* Worklist of SSA edges which will need reexamination as their
153 definition has changed. SSA edges are def-use edges in the SSA
154 web. For each D-U edge, we store the target statement or PHI node
155 U. */
156 static GTY(()) vec<gimple, va_gc> *interesting_ssa_edges;
158 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
159 list of SSA edges is split into two. One contains all SSA edges
160 who need to be reexamined because their lattice value changed to
161 varying (this worklist), and the other contains all other SSA edges
162 to be reexamined (INTERESTING_SSA_EDGES).
164 Since most values in the program are VARYING, the ideal situation
165 is to move them to that lattice value as quickly as possible.
166 Thus, it doesn't make sense to process any other type of lattice
167 value until all VARYING values are propagated fully, which is one
168 thing using the VARYING worklist achieves. In addition, if we
169 don't use a separate worklist for VARYING edges, we end up with
170 situations where lattice values move from
171 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
172 static GTY(()) vec<gimple, va_gc> *varying_ssa_edges;
175 /* Return true if the block worklist empty. */
177 static inline bool
178 cfg_blocks_empty_p (void)
180 return (cfg_blocks_num == 0);
184 /* Add a basic block to the worklist. The block must not be already
185 in the worklist, and it must not be the ENTRY or EXIT block. */
187 static void
188 cfg_blocks_add (basic_block bb)
190 bool head = false;
192 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
193 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
194 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
196 if (cfg_blocks_empty_p ())
198 cfg_blocks_tail = cfg_blocks_head = 0;
199 cfg_blocks_num = 1;
201 else
203 cfg_blocks_num++;
204 if (cfg_blocks_num > cfg_blocks.length ())
206 /* We have to grow the array now. Adjust to queue to occupy
207 the full space of the original array. We do not need to
208 initialize the newly allocated portion of the array
209 because we keep track of CFG_BLOCKS_HEAD and
210 CFG_BLOCKS_HEAD. */
211 cfg_blocks_tail = cfg_blocks.length ();
212 cfg_blocks_head = 0;
213 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
215 /* Minor optimization: we prefer to see blocks with more
216 predecessors later, because there is more of a chance that
217 the incoming edges will be executable. */
218 else if (EDGE_COUNT (bb->preds)
219 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
220 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
221 else
223 if (cfg_blocks_head == 0)
224 cfg_blocks_head = cfg_blocks.length ();
225 --cfg_blocks_head;
226 head = true;
230 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
231 bitmap_set_bit (bb_in_list, bb->index);
235 /* Remove a block from the worklist. */
237 static basic_block
238 cfg_blocks_get (void)
240 basic_block bb;
242 bb = cfg_blocks[cfg_blocks_head];
244 gcc_assert (!cfg_blocks_empty_p ());
245 gcc_assert (bb);
247 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
248 --cfg_blocks_num;
249 bitmap_clear_bit (bb_in_list, bb->index);
251 return bb;
255 /* We have just defined a new value for VAR. If IS_VARYING is true,
256 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
257 them to INTERESTING_SSA_EDGES. */
259 static void
260 add_ssa_edge (tree var, bool is_varying)
262 imm_use_iterator iter;
263 use_operand_p use_p;
265 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
267 gimple use_stmt = USE_STMT (use_p);
269 if (prop_simulate_again_p (use_stmt)
270 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
272 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
273 if (is_varying)
274 vec_safe_push (varying_ssa_edges, use_stmt);
275 else
276 vec_safe_push (interesting_ssa_edges, use_stmt);
282 /* Add edge E to the control flow worklist. */
284 static void
285 add_control_edge (edge e)
287 basic_block bb = e->dest;
288 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
289 return;
291 /* If the edge had already been executed, skip it. */
292 if (e->flags & EDGE_EXECUTABLE)
293 return;
295 e->flags |= EDGE_EXECUTABLE;
297 /* If the block is already in the list, we're done. */
298 if (bitmap_bit_p (bb_in_list, bb->index))
299 return;
301 cfg_blocks_add (bb);
303 if (dump_file && (dump_flags & TDF_DETAILS))
304 fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
305 e->src->index, e->dest->index);
309 /* Simulate the execution of STMT and update the work lists accordingly. */
311 static void
312 simulate_stmt (gimple stmt)
314 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
315 edge taken_edge = NULL;
316 tree output_name = NULL_TREE;
318 /* Don't bother visiting statements that are already
319 considered varying by the propagator. */
320 if (!prop_simulate_again_p (stmt))
321 return;
323 if (gimple_code (stmt) == GIMPLE_PHI)
325 val = ssa_prop_visit_phi (stmt);
326 output_name = gimple_phi_result (stmt);
328 else
329 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
331 if (val == SSA_PROP_VARYING)
333 prop_set_simulate_again (stmt, false);
335 /* If the statement produced a new varying value, add the SSA
336 edges coming out of OUTPUT_NAME. */
337 if (output_name)
338 add_ssa_edge (output_name, true);
340 /* If STMT transfers control out of its basic block, add
341 all outgoing edges to the work list. */
342 if (stmt_ends_bb_p (stmt))
344 edge e;
345 edge_iterator ei;
346 basic_block bb = gimple_bb (stmt);
347 FOR_EACH_EDGE (e, ei, bb->succs)
348 add_control_edge (e);
351 else if (val == SSA_PROP_INTERESTING)
353 /* If the statement produced new value, add the SSA edges coming
354 out of OUTPUT_NAME. */
355 if (output_name)
356 add_ssa_edge (output_name, false);
358 /* If we know which edge is going to be taken out of this block,
359 add it to the CFG work list. */
360 if (taken_edge)
361 add_control_edge (taken_edge);
365 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
366 drain. This pops statements off the given WORKLIST and processes
367 them until there are no more statements on WORKLIST.
368 We take a pointer to WORKLIST because it may be reallocated when an
369 SSA edge is added to it in simulate_stmt. */
371 static void
372 process_ssa_edge_worklist (vec<gimple, va_gc> **worklist)
374 /* Drain the entire worklist. */
375 while ((*worklist)->length () > 0)
377 basic_block bb;
379 /* Pull the statement to simulate off the worklist. */
380 gimple stmt = (*worklist)->pop ();
382 /* If this statement was already visited by simulate_block, then
383 we don't need to visit it again here. */
384 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
385 continue;
387 /* STMT is no longer in a worklist. */
388 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
390 if (dump_file && (dump_flags & TDF_DETAILS))
392 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
393 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
396 bb = gimple_bb (stmt);
398 /* PHI nodes are always visited, regardless of whether or not
399 the destination block is executable. Otherwise, visit the
400 statement only if its block is marked executable. */
401 if (gimple_code (stmt) == GIMPLE_PHI
402 || bitmap_bit_p (executable_blocks, bb->index))
403 simulate_stmt (stmt);
408 /* Simulate the execution of BLOCK. Evaluate the statement associated
409 with each variable reference inside the block. */
411 static void
412 simulate_block (basic_block block)
414 gimple_stmt_iterator gsi;
416 /* There is nothing to do for the exit block. */
417 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
418 return;
420 if (dump_file && (dump_flags & TDF_DETAILS))
421 fprintf (dump_file, "\nSimulating block %d\n", block->index);
423 /* Always simulate PHI nodes, even if we have simulated this block
424 before. */
425 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
426 simulate_stmt (gsi_stmt (gsi));
428 /* If this is the first time we've simulated this block, then we
429 must simulate each of its statements. */
430 if (!bitmap_bit_p (executable_blocks, block->index))
432 gimple_stmt_iterator j;
433 unsigned int normal_edge_count;
434 edge e, normal_edge;
435 edge_iterator ei;
437 /* Note that we have simulated this block. */
438 bitmap_set_bit (executable_blocks, block->index);
440 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
442 gimple stmt = gsi_stmt (j);
444 /* If this statement is already in the worklist then
445 "cancel" it. The reevaluation implied by the worklist
446 entry will produce the same value we generate here and
447 thus reevaluating it again from the worklist is
448 pointless. */
449 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
450 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
452 simulate_stmt (stmt);
455 /* We can not predict when abnormal and EH edges will be executed, so
456 once a block is considered executable, we consider any
457 outgoing abnormal edges as executable.
459 TODO: This is not exactly true. Simplifying statement might
460 prove it non-throwing and also computed goto can be handled
461 when destination is known.
463 At the same time, if this block has only one successor that is
464 reached by non-abnormal edges, then add that successor to the
465 worklist. */
466 normal_edge_count = 0;
467 normal_edge = NULL;
468 FOR_EACH_EDGE (e, ei, block->succs)
470 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
471 add_control_edge (e);
472 else
474 normal_edge_count++;
475 normal_edge = e;
479 if (normal_edge_count == 1)
480 add_control_edge (normal_edge);
485 /* Initialize local data structures and work lists. */
487 static void
488 ssa_prop_init (void)
490 edge e;
491 edge_iterator ei;
492 basic_block bb;
494 /* Worklists of SSA edges. */
495 vec_alloc (interesting_ssa_edges, 20);
496 vec_alloc (varying_ssa_edges, 20);
498 executable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
499 bitmap_clear (executable_blocks);
501 bb_in_list = sbitmap_alloc (last_basic_block_for_fn (cfun));
502 bitmap_clear (bb_in_list);
504 if (dump_file && (dump_flags & TDF_DETAILS))
505 dump_immediate_uses (dump_file);
507 cfg_blocks.create (20);
508 cfg_blocks.safe_grow_cleared (20);
510 /* Initially assume that every edge in the CFG is not executable.
511 (including the edges coming out of the entry block). */
512 FOR_ALL_BB_FN (bb, cfun)
514 gimple_stmt_iterator si;
516 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
517 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
519 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
520 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
522 FOR_EACH_EDGE (e, ei, bb->succs)
523 e->flags &= ~EDGE_EXECUTABLE;
526 /* Seed the algorithm by adding the successors of the entry block to the
527 edge worklist. */
528 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
529 add_control_edge (e);
533 /* Free allocated storage. */
535 static void
536 ssa_prop_fini (void)
538 vec_free (interesting_ssa_edges);
539 vec_free (varying_ssa_edges);
540 cfg_blocks.release ();
541 sbitmap_free (bb_in_list);
542 sbitmap_free (executable_blocks);
546 /* Return true if EXPR is an acceptable right-hand-side for a
547 GIMPLE assignment. We validate the entire tree, not just
548 the root node, thus catching expressions that embed complex
549 operands that are not permitted in GIMPLE. This function
550 is needed because the folding routines in fold-const.c
551 may return such expressions in some cases, e.g., an array
552 access with an embedded index addition. It may make more
553 sense to have folding routines that are sensitive to the
554 constraints on GIMPLE operands, rather than abandoning any
555 any attempt to fold if the usual folding turns out to be too
556 aggressive. */
558 bool
559 valid_gimple_rhs_p (tree expr)
561 enum tree_code code = TREE_CODE (expr);
563 switch (TREE_CODE_CLASS (code))
565 case tcc_declaration:
566 if (!is_gimple_variable (expr))
567 return false;
568 break;
570 case tcc_constant:
571 /* All constants are ok. */
572 break;
574 case tcc_comparison:
575 /* GENERIC allows comparisons with non-boolean types, reject
576 those for GIMPLE. Let vector-typed comparisons pass - rules
577 for GENERIC and GIMPLE are the same here. */
578 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
579 && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
580 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
581 && TREE_CODE (TREE_TYPE (expr)) != VECTOR_TYPE)
582 return false;
584 /* Fallthru. */
585 case tcc_binary:
586 if (!is_gimple_val (TREE_OPERAND (expr, 0))
587 || !is_gimple_val (TREE_OPERAND (expr, 1)))
588 return false;
589 break;
591 case tcc_unary:
592 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
593 return false;
594 break;
596 case tcc_expression:
597 switch (code)
599 case ADDR_EXPR:
601 tree t;
602 if (is_gimple_min_invariant (expr))
603 return true;
604 t = TREE_OPERAND (expr, 0);
605 while (handled_component_p (t))
607 /* ??? More checks needed, see the GIMPLE verifier. */
608 if ((TREE_CODE (t) == ARRAY_REF
609 || TREE_CODE (t) == ARRAY_RANGE_REF)
610 && !is_gimple_val (TREE_OPERAND (t, 1)))
611 return false;
612 t = TREE_OPERAND (t, 0);
614 if (!is_gimple_id (t))
615 return false;
617 break;
619 default:
620 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
622 if (((code == VEC_COND_EXPR || code == COND_EXPR)
623 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
624 : !is_gimple_val (TREE_OPERAND (expr, 0)))
625 || !is_gimple_val (TREE_OPERAND (expr, 1))
626 || !is_gimple_val (TREE_OPERAND (expr, 2)))
627 return false;
628 break;
630 return false;
632 break;
634 case tcc_vl_exp:
635 return false;
637 case tcc_exceptional:
638 if (code == CONSTRUCTOR)
640 unsigned i;
641 tree elt;
642 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
643 if (!is_gimple_val (elt))
644 return false;
645 return true;
647 if (code != SSA_NAME)
648 return false;
649 break;
651 case tcc_reference:
652 if (code == BIT_FIELD_REF)
653 return is_gimple_val (TREE_OPERAND (expr, 0));
654 return false;
656 default:
657 return false;
660 return true;
664 /* Return true if EXPR is a CALL_EXPR suitable for representation
665 as a single GIMPLE_CALL statement. If the arguments require
666 further gimplification, return false. */
668 static bool
669 valid_gimple_call_p (tree expr)
671 unsigned i, nargs;
673 if (TREE_CODE (expr) != CALL_EXPR)
674 return false;
676 nargs = call_expr_nargs (expr);
677 for (i = 0; i < nargs; i++)
679 tree arg = CALL_EXPR_ARG (expr, i);
680 if (is_gimple_reg_type (TREE_TYPE (arg)))
682 if (!is_gimple_val (arg))
683 return false;
685 else
686 if (!is_gimple_lvalue (arg))
687 return false;
690 return true;
694 /* Make SSA names defined by OLD_STMT point to NEW_STMT
695 as their defining statement. */
697 void
698 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
700 tree var;
701 ssa_op_iter iter;
703 if (gimple_in_ssa_p (cfun))
705 /* Make defined SSA_NAMEs point to the new
706 statement as their definition. */
707 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
709 if (TREE_CODE (var) == SSA_NAME)
710 SSA_NAME_DEF_STMT (var) = new_stmt;
715 /* Helper function for update_gimple_call and update_call_from_tree.
716 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
718 static void
719 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
720 gimple stmt)
722 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
723 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
724 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
725 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
726 gimple_set_location (new_stmt, gimple_location (stmt));
727 if (gimple_block (new_stmt) == NULL_TREE)
728 gimple_set_block (new_stmt, gimple_block (stmt));
729 gsi_replace (si_p, new_stmt, false);
732 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
733 with number of arguments NARGS, where the arguments in GIMPLE form
734 follow NARGS argument. */
736 bool
737 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
739 va_list ap;
740 gimple new_stmt, stmt = gsi_stmt (*si_p);
742 gcc_assert (is_gimple_call (stmt));
743 va_start (ap, nargs);
744 new_stmt = gimple_build_call_valist (fn, nargs, ap);
745 finish_update_gimple_call (si_p, new_stmt, stmt);
746 va_end (ap);
747 return true;
750 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
751 value of EXPR, which is expected to be the result of folding the
752 call. This can only be done if EXPR is a CALL_EXPR with valid
753 GIMPLE operands as arguments, or if it is a suitable RHS expression
754 for a GIMPLE_ASSIGN. More complex expressions will require
755 gimplification, which will introduce additional statements. In this
756 event, no update is performed, and the function returns false.
757 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
758 replace the statement at *SI_P with an entirely new statement.
759 The new statement need not be a call, e.g., if the original call
760 folded to a constant. */
762 bool
763 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
765 gimple stmt = gsi_stmt (*si_p);
767 if (valid_gimple_call_p (expr))
769 /* The call has simplified to another call. */
770 tree fn = CALL_EXPR_FN (expr);
771 unsigned i;
772 unsigned nargs = call_expr_nargs (expr);
773 vec<tree> args = vNULL;
774 gimple new_stmt;
776 if (nargs > 0)
778 args.create (nargs);
779 args.safe_grow_cleared (nargs);
781 for (i = 0; i < nargs; i++)
782 args[i] = CALL_EXPR_ARG (expr, i);
785 new_stmt = gimple_build_call_vec (fn, args);
786 finish_update_gimple_call (si_p, new_stmt, stmt);
787 args.release ();
789 return true;
791 else if (valid_gimple_rhs_p (expr))
793 tree lhs = gimple_call_lhs (stmt);
794 gimple new_stmt;
796 /* The call has simplified to an expression
797 that cannot be represented as a GIMPLE_CALL. */
798 if (lhs)
800 /* A value is expected.
801 Introduce a new GIMPLE_ASSIGN statement. */
802 STRIP_USELESS_TYPE_CONVERSION (expr);
803 new_stmt = gimple_build_assign (lhs, expr);
804 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
805 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
806 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
808 else if (!TREE_SIDE_EFFECTS (expr))
810 /* No value is expected, and EXPR has no effect.
811 Replace it with an empty statement. */
812 new_stmt = gimple_build_nop ();
813 if (gimple_in_ssa_p (cfun))
815 unlink_stmt_vdef (stmt);
816 release_defs (stmt);
819 else
821 /* No value is expected, but EXPR has an effect,
822 e.g., it could be a reference to a volatile
823 variable. Create an assignment statement
824 with a dummy (unused) lhs variable. */
825 STRIP_USELESS_TYPE_CONVERSION (expr);
826 if (gimple_in_ssa_p (cfun))
827 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
828 else
829 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
830 new_stmt = gimple_build_assign (lhs, expr);
831 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
832 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
833 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
835 gimple_set_location (new_stmt, gimple_location (stmt));
836 gsi_replace (si_p, new_stmt, false);
837 return true;
839 else
840 /* The call simplified to an expression that is
841 not a valid GIMPLE RHS. */
842 return false;
846 /* Entry point to the propagation engine.
848 VISIT_STMT is called for every statement visited.
849 VISIT_PHI is called for every PHI node visited. */
851 void
852 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
853 ssa_prop_visit_phi_fn visit_phi)
855 ssa_prop_visit_stmt = visit_stmt;
856 ssa_prop_visit_phi = visit_phi;
858 ssa_prop_init ();
860 /* Iterate until the worklists are empty. */
861 while (!cfg_blocks_empty_p ()
862 || interesting_ssa_edges->length () > 0
863 || varying_ssa_edges->length () > 0)
865 if (!cfg_blocks_empty_p ())
867 /* Pull the next block to simulate off the worklist. */
868 basic_block dest_block = cfg_blocks_get ();
869 simulate_block (dest_block);
872 /* In order to move things to varying as quickly as
873 possible,process the VARYING_SSA_EDGES worklist first. */
874 process_ssa_edge_worklist (&varying_ssa_edges);
876 /* Now process the INTERESTING_SSA_EDGES worklist. */
877 process_ssa_edge_worklist (&interesting_ssa_edges);
880 ssa_prop_fini ();
884 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
885 is a non-volatile pointer dereference, a structure reference or a
886 reference to a single _DECL. Ignore volatile memory references
887 because they are not interesting for the optimizers. */
889 bool
890 stmt_makes_single_store (gimple stmt)
892 tree lhs;
894 if (gimple_code (stmt) != GIMPLE_ASSIGN
895 && gimple_code (stmt) != GIMPLE_CALL)
896 return false;
898 if (!gimple_vdef (stmt))
899 return false;
901 lhs = gimple_get_lhs (stmt);
903 /* A call statement may have a null LHS. */
904 if (!lhs)
905 return false;
907 return (!TREE_THIS_VOLATILE (lhs)
908 && (DECL_P (lhs)
909 || REFERENCE_CLASS_P (lhs)));
913 /* Propagation statistics. */
914 struct prop_stats_d
916 long num_const_prop;
917 long num_copy_prop;
918 long num_stmts_folded;
919 long num_dce;
922 static struct prop_stats_d prop_stats;
924 /* Replace USE references in statement STMT with the values stored in
925 PROP_VALUE. Return true if at least one reference was replaced. */
927 static bool
928 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
930 bool replaced = false;
931 use_operand_p use;
932 ssa_op_iter iter;
934 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
936 tree tuse = USE_FROM_PTR (use);
937 tree val = (*get_value) (tuse);
939 if (val == tuse || val == NULL_TREE)
940 continue;
942 if (gimple_code (stmt) == GIMPLE_ASM
943 && !may_propagate_copy_into_asm (tuse))
944 continue;
946 if (!may_propagate_copy (tuse, val))
947 continue;
949 if (TREE_CODE (val) != SSA_NAME)
950 prop_stats.num_const_prop++;
951 else
952 prop_stats.num_copy_prop++;
954 propagate_value (use, val);
956 replaced = true;
959 return replaced;
963 /* Replace propagated values into all the arguments for PHI using the
964 values from PROP_VALUE. */
966 static void
967 replace_phi_args_in (gimple phi, ssa_prop_get_value_fn get_value)
969 size_t i;
970 bool replaced = false;
972 if (dump_file && (dump_flags & TDF_DETAILS))
974 fprintf (dump_file, "Folding PHI node: ");
975 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
978 for (i = 0; i < gimple_phi_num_args (phi); i++)
980 tree arg = gimple_phi_arg_def (phi, i);
982 if (TREE_CODE (arg) == SSA_NAME)
984 tree val = (*get_value) (arg);
986 if (val && val != arg && may_propagate_copy (arg, val))
988 if (TREE_CODE (val) != SSA_NAME)
989 prop_stats.num_const_prop++;
990 else
991 prop_stats.num_copy_prop++;
993 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
994 replaced = true;
996 /* If we propagated a copy and this argument flows
997 through an abnormal edge, update the replacement
998 accordingly. */
999 if (TREE_CODE (val) == SSA_NAME
1000 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
1001 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1006 if (dump_file && (dump_flags & TDF_DETAILS))
1008 if (!replaced)
1009 fprintf (dump_file, "No folding possible\n");
1010 else
1012 fprintf (dump_file, "Folded into: ");
1013 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1014 fprintf (dump_file, "\n");
1020 /* Perform final substitution and folding of propagated values.
1022 PROP_VALUE[I] contains the single value that should be substituted
1023 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1024 substituted.
1026 If FOLD_FN is non-NULL the function will be invoked on all statements
1027 before propagating values for pass specific simplification.
1029 DO_DCE is true if trivially dead stmts can be removed.
1031 If DO_DCE is true, the statements within a BB are walked from
1032 last to first element. Otherwise we scan from first to last element.
1034 Return TRUE when something changed. */
1036 bool
1037 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1038 ssa_prop_fold_stmt_fn fold_fn,
1039 bool do_dce)
1041 basic_block bb;
1042 bool something_changed = false;
1043 unsigned i;
1045 if (!get_value_fn && !fold_fn)
1046 return false;
1048 if (dump_file && (dump_flags & TDF_DETAILS))
1049 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1051 memset (&prop_stats, 0, sizeof (prop_stats));
1053 /* Substitute lattice values at definition sites. */
1054 if (get_value_fn)
1055 for (i = 1; i < num_ssa_names; ++i)
1057 tree name = ssa_name (i);
1058 tree val;
1059 gimple def_stmt;
1060 gimple_stmt_iterator gsi;
1062 if (!name
1063 || virtual_operand_p (name))
1064 continue;
1066 def_stmt = SSA_NAME_DEF_STMT (name);
1067 if (gimple_nop_p (def_stmt)
1068 /* Do not substitute ASSERT_EXPR rhs, this will confuse VRP. */
1069 || (gimple_assign_single_p (def_stmt)
1070 && gimple_assign_rhs_code (def_stmt) == ASSERT_EXPR)
1071 || !(val = (*get_value_fn) (name))
1072 || !may_propagate_copy (name, val))
1073 continue;
1075 gsi = gsi_for_stmt (def_stmt);
1076 if (is_gimple_assign (def_stmt))
1078 gimple_assign_set_rhs_with_ops (&gsi, TREE_CODE (val),
1079 val, NULL_TREE);
1080 gcc_assert (gsi_stmt (gsi) == def_stmt);
1081 if (maybe_clean_eh_stmt (def_stmt))
1082 gimple_purge_dead_eh_edges (gimple_bb (def_stmt));
1083 update_stmt (def_stmt);
1085 else if (is_gimple_call (def_stmt))
1087 int flags = gimple_call_flags (def_stmt);
1089 /* Don't optimize away calls that have side-effects. */
1090 if ((flags & (ECF_CONST|ECF_PURE)) == 0
1091 || (flags & ECF_LOOPING_CONST_OR_PURE))
1092 continue;
1093 if (update_call_from_tree (&gsi, val)
1094 && maybe_clean_or_replace_eh_stmt (def_stmt, gsi_stmt (gsi)))
1095 gimple_purge_dead_eh_edges (gimple_bb (gsi_stmt (gsi)));
1097 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1099 gimple new_stmt = gimple_build_assign (name, val);
1100 gimple_stmt_iterator gsi2;
1101 gsi2 = gsi_after_labels (gimple_bb (def_stmt));
1102 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
1103 remove_phi_node (&gsi, false);
1106 something_changed = true;
1109 /* Propagate into all uses and fold. */
1110 FOR_EACH_BB_FN (bb, cfun)
1112 gimple_stmt_iterator i;
1114 /* Propagate known values into PHI nodes. */
1115 if (get_value_fn)
1116 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
1117 replace_phi_args_in (gsi_stmt (i), get_value_fn);
1119 /* Propagate known values into stmts. Do a backward walk if
1120 do_dce is true. In some case it exposes
1121 more trivially deletable stmts to walk backward. */
1122 for (i = (do_dce ? gsi_last_bb (bb) : gsi_start_bb (bb)); !gsi_end_p (i);)
1124 bool did_replace;
1125 gimple stmt = gsi_stmt (i);
1126 gimple old_stmt;
1127 enum gimple_code code = gimple_code (stmt);
1128 gimple_stmt_iterator oldi;
1130 oldi = i;
1131 if (do_dce)
1132 gsi_prev (&i);
1133 else
1134 gsi_next (&i);
1136 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1137 range information for names and they are discarded
1138 afterwards. */
1140 if (code == GIMPLE_ASSIGN
1141 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1142 continue;
1144 /* No point propagating into a stmt whose result is not used,
1145 but instead we might be able to remove a trivially dead stmt.
1146 Don't do this when called from VRP, since the SSA_NAME which
1147 is going to be released could be still referenced in VRP
1148 ranges. */
1149 if (do_dce
1150 && gimple_get_lhs (stmt)
1151 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1152 && has_zero_uses (gimple_get_lhs (stmt))
1153 && !stmt_could_throw_p (stmt)
1154 && !gimple_has_side_effects (stmt))
1156 gimple_stmt_iterator i2;
1158 if (dump_file && dump_flags & TDF_DETAILS)
1160 fprintf (dump_file, "Removing dead stmt ");
1161 print_gimple_stmt (dump_file, stmt, 0, 0);
1162 fprintf (dump_file, "\n");
1164 prop_stats.num_dce++;
1165 i2 = gsi_for_stmt (stmt);
1166 gsi_remove (&i2, true);
1167 release_defs (stmt);
1168 continue;
1171 /* Replace the statement with its folded version and mark it
1172 folded. */
1173 did_replace = false;
1174 if (dump_file && (dump_flags & TDF_DETAILS))
1176 fprintf (dump_file, "Folding statement: ");
1177 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1180 old_stmt = stmt;
1182 /* Some statements may be simplified using propagator
1183 specific information. Do this before propagating
1184 into the stmt to not disturb pass specific information. */
1185 if (fold_fn
1186 && (*fold_fn)(&oldi))
1188 did_replace = true;
1189 prop_stats.num_stmts_folded++;
1190 stmt = gsi_stmt (oldi);
1191 update_stmt (stmt);
1194 /* Replace real uses in the statement. */
1195 if (get_value_fn)
1196 did_replace |= replace_uses_in (stmt, get_value_fn);
1198 /* If we made a replacement, fold the statement. */
1199 if (did_replace)
1200 fold_stmt (&oldi);
1202 /* Now cleanup. */
1203 if (did_replace)
1205 stmt = gsi_stmt (oldi);
1207 /* If we cleaned up EH information from the statement,
1208 remove EH edges. */
1209 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1210 gimple_purge_dead_eh_edges (bb);
1212 if (is_gimple_assign (stmt)
1213 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1214 == GIMPLE_SINGLE_RHS))
1216 tree rhs = gimple_assign_rhs1 (stmt);
1218 if (TREE_CODE (rhs) == ADDR_EXPR)
1219 recompute_tree_invariant_for_addr_expr (rhs);
1222 /* Determine what needs to be done to update the SSA form. */
1223 update_stmt (stmt);
1224 if (!is_gimple_debug (stmt))
1225 something_changed = true;
1228 if (dump_file && (dump_flags & TDF_DETAILS))
1230 if (did_replace)
1232 fprintf (dump_file, "Folded into: ");
1233 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1234 fprintf (dump_file, "\n");
1236 else
1237 fprintf (dump_file, "Not folded\n");
1242 statistics_counter_event (cfun, "Constants propagated",
1243 prop_stats.num_const_prop);
1244 statistics_counter_event (cfun, "Copies propagated",
1245 prop_stats.num_copy_prop);
1246 statistics_counter_event (cfun, "Statements folded",
1247 prop_stats.num_stmts_folded);
1248 statistics_counter_event (cfun, "Statements deleted",
1249 prop_stats.num_dce);
1250 return something_changed;
1254 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1256 bool
1257 may_propagate_copy (tree dest, tree orig)
1259 tree type_d = TREE_TYPE (dest);
1260 tree type_o = TREE_TYPE (orig);
1262 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
1263 if (TREE_CODE (orig) == SSA_NAME
1264 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1265 /* If it is the default definition and an automatic variable then
1266 we can though and it is important that we do to avoid
1267 uninitialized regular copies. */
1268 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
1269 && (SSA_NAME_VAR (orig) == NULL_TREE
1270 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
1271 return false;
1273 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
1274 cannot be replaced. */
1275 if (TREE_CODE (dest) == SSA_NAME
1276 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1277 return false;
1279 /* Do not copy between types for which we *do* need a conversion. */
1280 if (!useless_type_conversion_p (type_d, type_o))
1281 return false;
1283 /* Generally propagating virtual operands is not ok as that may
1284 create overlapping life-ranges. */
1285 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1286 return false;
1288 /* Anything else is OK. */
1289 return true;
1292 /* Like may_propagate_copy, but use as the destination expression
1293 the principal expression (typically, the RHS) contained in
1294 statement DEST. This is more efficient when working with the
1295 gimple tuples representation. */
1297 bool
1298 may_propagate_copy_into_stmt (gimple dest, tree orig)
1300 tree type_d;
1301 tree type_o;
1303 /* If the statement is a switch or a single-rhs assignment,
1304 then the expression to be replaced by the propagation may
1305 be an SSA_NAME. Fortunately, there is an explicit tree
1306 for the expression, so we delegate to may_propagate_copy. */
1308 if (gimple_assign_single_p (dest))
1309 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1310 else if (gimple_code (dest) == GIMPLE_SWITCH)
1311 return may_propagate_copy (gimple_switch_index (dest), orig);
1313 /* In other cases, the expression is not materialized, so there
1314 is no destination to pass to may_propagate_copy. On the other
1315 hand, the expression cannot be an SSA_NAME, so the analysis
1316 is much simpler. */
1318 if (TREE_CODE (orig) == SSA_NAME
1319 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1320 return false;
1322 if (is_gimple_assign (dest))
1323 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1324 else if (gimple_code (dest) == GIMPLE_COND)
1325 type_d = boolean_type_node;
1326 else if (is_gimple_call (dest)
1327 && gimple_call_lhs (dest) != NULL_TREE)
1328 type_d = TREE_TYPE (gimple_call_lhs (dest));
1329 else
1330 gcc_unreachable ();
1332 type_o = TREE_TYPE (orig);
1334 if (!useless_type_conversion_p (type_d, type_o))
1335 return false;
1337 return true;
1340 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1342 bool
1343 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1345 return true;
1349 /* Common code for propagate_value and replace_exp.
1351 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1352 replacement is done to propagate a value or not. */
1354 static void
1355 replace_exp_1 (use_operand_p op_p, tree val,
1356 bool for_propagation ATTRIBUTE_UNUSED)
1358 #if defined ENABLE_CHECKING
1359 tree op = USE_FROM_PTR (op_p);
1361 gcc_assert (!(for_propagation
1362 && TREE_CODE (op) == SSA_NAME
1363 && TREE_CODE (val) == SSA_NAME
1364 && !may_propagate_copy (op, val)));
1365 #endif
1367 if (TREE_CODE (val) == SSA_NAME)
1368 SET_USE (op_p, val);
1369 else
1370 SET_USE (op_p, unshare_expr (val));
1374 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1375 into the operand pointed to by OP_P.
1377 Use this version for const/copy propagation as it will perform additional
1378 checks to ensure validity of the const/copy propagation. */
1380 void
1381 propagate_value (use_operand_p op_p, tree val)
1383 replace_exp_1 (op_p, val, true);
1386 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1388 Use this version when not const/copy propagating values. For example,
1389 PRE uses this version when building expressions as they would appear
1390 in specific blocks taking into account actions of PHI nodes.
1392 The statement in which an expression has been replaced should be
1393 folded using fold_stmt_inplace. */
1395 void
1396 replace_exp (use_operand_p op_p, tree val)
1398 replace_exp_1 (op_p, val, false);
1402 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1403 into the tree pointed to by OP_P.
1405 Use this version for const/copy propagation when SSA operands are not
1406 available. It will perform the additional checks to ensure validity of
1407 the const/copy propagation, but will not update any operand information.
1408 Be sure to mark the stmt as modified. */
1410 void
1411 propagate_tree_value (tree *op_p, tree val)
1413 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
1414 && *op_p
1415 && TREE_CODE (*op_p) == SSA_NAME
1416 && !may_propagate_copy (*op_p, val)));
1418 if (TREE_CODE (val) == SSA_NAME)
1419 *op_p = val;
1420 else
1421 *op_p = unshare_expr (val);
1425 /* Like propagate_tree_value, but use as the operand to replace
1426 the principal expression (typically, the RHS) contained in the
1427 statement referenced by iterator GSI. Note that it is not
1428 always possible to update the statement in-place, so a new
1429 statement may be created to replace the original. */
1431 void
1432 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1434 gimple stmt = gsi_stmt (*gsi);
1436 if (is_gimple_assign (stmt))
1438 tree expr = NULL_TREE;
1439 if (gimple_assign_single_p (stmt))
1440 expr = gimple_assign_rhs1 (stmt);
1441 propagate_tree_value (&expr, val);
1442 gimple_assign_set_rhs_from_tree (gsi, expr);
1444 else if (gimple_code (stmt) == GIMPLE_COND)
1446 tree lhs = NULL_TREE;
1447 tree rhs = build_zero_cst (TREE_TYPE (val));
1448 propagate_tree_value (&lhs, val);
1449 gimple_cond_set_code (stmt, NE_EXPR);
1450 gimple_cond_set_lhs (stmt, lhs);
1451 gimple_cond_set_rhs (stmt, rhs);
1453 else if (is_gimple_call (stmt)
1454 && gimple_call_lhs (stmt) != NULL_TREE)
1456 tree expr = NULL_TREE;
1457 bool res;
1458 propagate_tree_value (&expr, val);
1459 res = update_call_from_tree (gsi, expr);
1460 gcc_assert (res);
1462 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1463 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
1464 else
1465 gcc_unreachable ();
1468 #include "gt-tree-ssa-propagate.h"