* ChangeLog: Fix whitespace.
[official-gcc.git] / gcc / tree-ssa-propagate.c
blob783b6513e30d235598a8bceaa92fc85497efc1aa
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple-pretty-print.h"
31 #include "dumpfile.h"
32 #include "sbitmap.h"
33 #include "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);
499 bitmap_clear (executable_blocks);
501 bb_in_list = sbitmap_alloc (last_basic_block);
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 (bb)
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_binary:
575 case tcc_comparison:
576 if (!is_gimple_val (TREE_OPERAND (expr, 0))
577 || !is_gimple_val (TREE_OPERAND (expr, 1)))
578 return false;
579 break;
581 case tcc_unary:
582 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
583 return false;
584 break;
586 case tcc_expression:
587 switch (code)
589 case ADDR_EXPR:
591 tree t;
592 if (is_gimple_min_invariant (expr))
593 return true;
594 t = TREE_OPERAND (expr, 0);
595 while (handled_component_p (t))
597 /* ??? More checks needed, see the GIMPLE verifier. */
598 if ((TREE_CODE (t) == ARRAY_REF
599 || TREE_CODE (t) == ARRAY_RANGE_REF)
600 && !is_gimple_val (TREE_OPERAND (t, 1)))
601 return false;
602 t = TREE_OPERAND (t, 0);
604 if (!is_gimple_id (t))
605 return false;
607 break;
609 default:
610 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
612 if (((code == VEC_COND_EXPR || code == COND_EXPR)
613 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
614 : !is_gimple_val (TREE_OPERAND (expr, 0)))
615 || !is_gimple_val (TREE_OPERAND (expr, 1))
616 || !is_gimple_val (TREE_OPERAND (expr, 2)))
617 return false;
618 break;
620 return false;
622 break;
624 case tcc_vl_exp:
625 return false;
627 case tcc_exceptional:
628 if (code == CONSTRUCTOR)
630 unsigned i;
631 tree elt;
632 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
633 if (!is_gimple_val (elt))
634 return false;
635 return true;
637 if (code != SSA_NAME)
638 return false;
639 break;
641 case tcc_reference:
642 if (code == BIT_FIELD_REF)
643 return is_gimple_val (TREE_OPERAND (expr, 0));
644 return false;
646 default:
647 return false;
650 return true;
654 /* Return true if EXPR is a CALL_EXPR suitable for representation
655 as a single GIMPLE_CALL statement. If the arguments require
656 further gimplification, return false. */
658 static bool
659 valid_gimple_call_p (tree expr)
661 unsigned i, nargs;
663 if (TREE_CODE (expr) != CALL_EXPR)
664 return false;
666 nargs = call_expr_nargs (expr);
667 for (i = 0; i < nargs; i++)
669 tree arg = CALL_EXPR_ARG (expr, i);
670 if (is_gimple_reg_type (TREE_TYPE (arg)))
672 if (!is_gimple_val (arg))
673 return false;
675 else
676 if (!is_gimple_lvalue (arg))
677 return false;
680 return true;
684 /* Make SSA names defined by OLD_STMT point to NEW_STMT
685 as their defining statement. */
687 void
688 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
690 tree var;
691 ssa_op_iter iter;
693 if (gimple_in_ssa_p (cfun))
695 /* Make defined SSA_NAMEs point to the new
696 statement as their definition. */
697 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
699 if (TREE_CODE (var) == SSA_NAME)
700 SSA_NAME_DEF_STMT (var) = new_stmt;
705 /* Helper function for update_gimple_call and update_call_from_tree.
706 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
708 static void
709 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
710 gimple stmt)
712 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
713 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
714 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
715 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
716 gimple_set_location (new_stmt, gimple_location (stmt));
717 if (gimple_block (new_stmt) == NULL_TREE)
718 gimple_set_block (new_stmt, gimple_block (stmt));
719 gsi_replace (si_p, new_stmt, false);
722 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
723 with number of arguments NARGS, where the arguments in GIMPLE form
724 follow NARGS argument. */
726 bool
727 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
729 va_list ap;
730 gimple new_stmt, stmt = gsi_stmt (*si_p);
732 gcc_assert (is_gimple_call (stmt));
733 va_start (ap, nargs);
734 new_stmt = gimple_build_call_valist (fn, nargs, ap);
735 finish_update_gimple_call (si_p, new_stmt, stmt);
736 va_end (ap);
737 return true;
740 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
741 value of EXPR, which is expected to be the result of folding the
742 call. This can only be done if EXPR is a CALL_EXPR with valid
743 GIMPLE operands as arguments, or if it is a suitable RHS expression
744 for a GIMPLE_ASSIGN. More complex expressions will require
745 gimplification, which will introduce additional statements. In this
746 event, no update is performed, and the function returns false.
747 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
748 replace the statement at *SI_P with an entirely new statement.
749 The new statement need not be a call, e.g., if the original call
750 folded to a constant. */
752 bool
753 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
755 gimple stmt = gsi_stmt (*si_p);
757 if (valid_gimple_call_p (expr))
759 /* The call has simplified to another call. */
760 tree fn = CALL_EXPR_FN (expr);
761 unsigned i;
762 unsigned nargs = call_expr_nargs (expr);
763 vec<tree> args = vNULL;
764 gimple new_stmt;
766 if (nargs > 0)
768 args.create (nargs);
769 args.safe_grow_cleared (nargs);
771 for (i = 0; i < nargs; i++)
772 args[i] = CALL_EXPR_ARG (expr, i);
775 new_stmt = gimple_build_call_vec (fn, args);
776 finish_update_gimple_call (si_p, new_stmt, stmt);
777 args.release ();
779 return true;
781 else if (valid_gimple_rhs_p (expr))
783 tree lhs = gimple_call_lhs (stmt);
784 gimple new_stmt;
786 /* The call has simplified to an expression
787 that cannot be represented as a GIMPLE_CALL. */
788 if (lhs)
790 /* A value is expected.
791 Introduce a new GIMPLE_ASSIGN statement. */
792 STRIP_USELESS_TYPE_CONVERSION (expr);
793 new_stmt = gimple_build_assign (lhs, expr);
794 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
795 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
796 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
798 else if (!TREE_SIDE_EFFECTS (expr))
800 /* No value is expected, and EXPR has no effect.
801 Replace it with an empty statement. */
802 new_stmt = gimple_build_nop ();
803 if (gimple_in_ssa_p (cfun))
805 unlink_stmt_vdef (stmt);
806 release_defs (stmt);
809 else
811 /* No value is expected, but EXPR has an effect,
812 e.g., it could be a reference to a volatile
813 variable. Create an assignment statement
814 with a dummy (unused) lhs variable. */
815 STRIP_USELESS_TYPE_CONVERSION (expr);
816 if (gimple_in_ssa_p (cfun))
817 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
818 else
819 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
820 new_stmt = gimple_build_assign (lhs, expr);
821 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
822 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
823 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
825 gimple_set_location (new_stmt, gimple_location (stmt));
826 gsi_replace (si_p, new_stmt, false);
827 return true;
829 else
830 /* The call simplified to an expression that is
831 not a valid GIMPLE RHS. */
832 return false;
836 /* Entry point to the propagation engine.
838 VISIT_STMT is called for every statement visited.
839 VISIT_PHI is called for every PHI node visited. */
841 void
842 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
843 ssa_prop_visit_phi_fn visit_phi)
845 ssa_prop_visit_stmt = visit_stmt;
846 ssa_prop_visit_phi = visit_phi;
848 ssa_prop_init ();
850 /* Iterate until the worklists are empty. */
851 while (!cfg_blocks_empty_p ()
852 || interesting_ssa_edges->length () > 0
853 || varying_ssa_edges->length () > 0)
855 if (!cfg_blocks_empty_p ())
857 /* Pull the next block to simulate off the worklist. */
858 basic_block dest_block = cfg_blocks_get ();
859 simulate_block (dest_block);
862 /* In order to move things to varying as quickly as
863 possible,process the VARYING_SSA_EDGES worklist first. */
864 process_ssa_edge_worklist (&varying_ssa_edges);
866 /* Now process the INTERESTING_SSA_EDGES worklist. */
867 process_ssa_edge_worklist (&interesting_ssa_edges);
870 ssa_prop_fini ();
874 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
875 is a non-volatile pointer dereference, a structure reference or a
876 reference to a single _DECL. Ignore volatile memory references
877 because they are not interesting for the optimizers. */
879 bool
880 stmt_makes_single_store (gimple stmt)
882 tree lhs;
884 if (gimple_code (stmt) != GIMPLE_ASSIGN
885 && gimple_code (stmt) != GIMPLE_CALL)
886 return false;
888 if (!gimple_vdef (stmt))
889 return false;
891 lhs = gimple_get_lhs (stmt);
893 /* A call statement may have a null LHS. */
894 if (!lhs)
895 return false;
897 return (!TREE_THIS_VOLATILE (lhs)
898 && (DECL_P (lhs)
899 || REFERENCE_CLASS_P (lhs)));
903 /* Propagation statistics. */
904 struct prop_stats_d
906 long num_const_prop;
907 long num_copy_prop;
908 long num_stmts_folded;
909 long num_dce;
912 static struct prop_stats_d prop_stats;
914 /* Replace USE references in statement STMT with the values stored in
915 PROP_VALUE. Return true if at least one reference was replaced. */
917 static bool
918 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
920 bool replaced = false;
921 use_operand_p use;
922 ssa_op_iter iter;
924 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
926 tree tuse = USE_FROM_PTR (use);
927 tree val = (*get_value) (tuse);
929 if (val == tuse || val == NULL_TREE)
930 continue;
932 if (gimple_code (stmt) == GIMPLE_ASM
933 && !may_propagate_copy_into_asm (tuse))
934 continue;
936 if (!may_propagate_copy (tuse, val))
937 continue;
939 if (TREE_CODE (val) != SSA_NAME)
940 prop_stats.num_const_prop++;
941 else
942 prop_stats.num_copy_prop++;
944 propagate_value (use, val);
946 replaced = true;
949 return replaced;
953 /* Replace propagated values into all the arguments for PHI using the
954 values from PROP_VALUE. */
956 static void
957 replace_phi_args_in (gimple phi, ssa_prop_get_value_fn get_value)
959 size_t i;
960 bool replaced = false;
962 if (dump_file && (dump_flags & TDF_DETAILS))
964 fprintf (dump_file, "Folding PHI node: ");
965 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
968 for (i = 0; i < gimple_phi_num_args (phi); i++)
970 tree arg = gimple_phi_arg_def (phi, i);
972 if (TREE_CODE (arg) == SSA_NAME)
974 tree val = (*get_value) (arg);
976 if (val && val != arg && may_propagate_copy (arg, val))
978 if (TREE_CODE (val) != SSA_NAME)
979 prop_stats.num_const_prop++;
980 else
981 prop_stats.num_copy_prop++;
983 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
984 replaced = true;
986 /* If we propagated a copy and this argument flows
987 through an abnormal edge, update the replacement
988 accordingly. */
989 if (TREE_CODE (val) == SSA_NAME
990 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
991 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
996 if (dump_file && (dump_flags & TDF_DETAILS))
998 if (!replaced)
999 fprintf (dump_file, "No folding possible\n");
1000 else
1002 fprintf (dump_file, "Folded into: ");
1003 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1004 fprintf (dump_file, "\n");
1010 /* Perform final substitution and folding of propagated values.
1012 PROP_VALUE[I] contains the single value that should be substituted
1013 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1014 substituted.
1016 If FOLD_FN is non-NULL the function will be invoked on all statements
1017 before propagating values for pass specific simplification.
1019 DO_DCE is true if trivially dead stmts can be removed.
1021 If DO_DCE is true, the statements within a BB are walked from
1022 last to first element. Otherwise we scan from first to last element.
1024 Return TRUE when something changed. */
1026 bool
1027 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1028 ssa_prop_fold_stmt_fn fold_fn,
1029 bool do_dce)
1031 basic_block bb;
1032 bool something_changed = false;
1033 unsigned i;
1035 if (!get_value_fn && !fold_fn)
1036 return false;
1038 if (dump_file && (dump_flags & TDF_DETAILS))
1039 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1041 memset (&prop_stats, 0, sizeof (prop_stats));
1043 /* Substitute lattice values at definition sites. */
1044 if (get_value_fn)
1045 for (i = 1; i < num_ssa_names; ++i)
1047 tree name = ssa_name (i);
1048 tree val;
1049 gimple def_stmt;
1050 gimple_stmt_iterator gsi;
1052 if (!name
1053 || virtual_operand_p (name))
1054 continue;
1056 def_stmt = SSA_NAME_DEF_STMT (name);
1057 if (gimple_nop_p (def_stmt)
1058 /* Do not substitute ASSERT_EXPR rhs, this will confuse VRP. */
1059 || (gimple_assign_single_p (def_stmt)
1060 && gimple_assign_rhs_code (def_stmt) == ASSERT_EXPR)
1061 || !(val = (*get_value_fn) (name))
1062 || !may_propagate_copy (name, val))
1063 continue;
1065 gsi = gsi_for_stmt (def_stmt);
1066 if (is_gimple_assign (def_stmt))
1068 gimple_assign_set_rhs_with_ops (&gsi, TREE_CODE (val),
1069 val, NULL_TREE);
1070 gcc_assert (gsi_stmt (gsi) == def_stmt);
1071 if (maybe_clean_eh_stmt (def_stmt))
1072 gimple_purge_dead_eh_edges (gimple_bb (def_stmt));
1073 update_stmt (def_stmt);
1075 else if (is_gimple_call (def_stmt))
1077 int flags = gimple_call_flags (def_stmt);
1079 /* Don't optimize away calls that have side-effects. */
1080 if ((flags & (ECF_CONST|ECF_PURE)) == 0
1081 || (flags & ECF_LOOPING_CONST_OR_PURE))
1082 continue;
1083 if (update_call_from_tree (&gsi, val)
1084 && maybe_clean_or_replace_eh_stmt (def_stmt, gsi_stmt (gsi)))
1085 gimple_purge_dead_eh_edges (gimple_bb (gsi_stmt (gsi)));
1087 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1089 gimple new_stmt = gimple_build_assign (name, val);
1090 gimple_stmt_iterator gsi2;
1091 gsi2 = gsi_after_labels (gimple_bb (def_stmt));
1092 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
1093 remove_phi_node (&gsi, false);
1096 something_changed = true;
1099 /* Propagate into all uses and fold. */
1100 FOR_EACH_BB (bb)
1102 gimple_stmt_iterator i;
1104 /* Propagate known values into PHI nodes. */
1105 if (get_value_fn)
1106 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
1107 replace_phi_args_in (gsi_stmt (i), get_value_fn);
1109 /* Propagate known values into stmts. Do a backward walk if
1110 do_dce is true. In some case it exposes
1111 more trivially deletable stmts to walk backward. */
1112 for (i = (do_dce ? gsi_last_bb (bb) : gsi_start_bb (bb)); !gsi_end_p (i);)
1114 bool did_replace;
1115 gimple stmt = gsi_stmt (i);
1116 gimple old_stmt;
1117 enum gimple_code code = gimple_code (stmt);
1118 gimple_stmt_iterator oldi;
1120 oldi = i;
1121 if (do_dce)
1122 gsi_prev (&i);
1123 else
1124 gsi_next (&i);
1126 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1127 range information for names and they are discarded
1128 afterwards. */
1130 if (code == GIMPLE_ASSIGN
1131 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1132 continue;
1134 /* No point propagating into a stmt whose result is not used,
1135 but instead we might be able to remove a trivially dead stmt.
1136 Don't do this when called from VRP, since the SSA_NAME which
1137 is going to be released could be still referenced in VRP
1138 ranges. */
1139 if (do_dce
1140 && gimple_get_lhs (stmt)
1141 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1142 && has_zero_uses (gimple_get_lhs (stmt))
1143 && !stmt_could_throw_p (stmt)
1144 && !gimple_has_side_effects (stmt))
1146 gimple_stmt_iterator i2;
1148 if (dump_file && dump_flags & TDF_DETAILS)
1150 fprintf (dump_file, "Removing dead stmt ");
1151 print_gimple_stmt (dump_file, stmt, 0, 0);
1152 fprintf (dump_file, "\n");
1154 prop_stats.num_dce++;
1155 i2 = gsi_for_stmt (stmt);
1156 gsi_remove (&i2, true);
1157 release_defs (stmt);
1158 continue;
1161 /* Replace the statement with its folded version and mark it
1162 folded. */
1163 did_replace = false;
1164 if (dump_file && (dump_flags & TDF_DETAILS))
1166 fprintf (dump_file, "Folding statement: ");
1167 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1170 old_stmt = stmt;
1172 /* Some statements may be simplified using propagator
1173 specific information. Do this before propagating
1174 into the stmt to not disturb pass specific information. */
1175 if (fold_fn
1176 && (*fold_fn)(&oldi))
1178 did_replace = true;
1179 prop_stats.num_stmts_folded++;
1180 stmt = gsi_stmt (oldi);
1181 update_stmt (stmt);
1184 /* Replace real uses in the statement. */
1185 if (get_value_fn)
1186 did_replace |= replace_uses_in (stmt, get_value_fn);
1188 /* If we made a replacement, fold the statement. */
1189 if (did_replace)
1190 fold_stmt (&oldi);
1192 /* Now cleanup. */
1193 if (did_replace)
1195 stmt = gsi_stmt (oldi);
1197 /* If we cleaned up EH information from the statement,
1198 remove EH edges. */
1199 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1200 gimple_purge_dead_eh_edges (bb);
1202 if (is_gimple_assign (stmt)
1203 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1204 == GIMPLE_SINGLE_RHS))
1206 tree rhs = gimple_assign_rhs1 (stmt);
1208 if (TREE_CODE (rhs) == ADDR_EXPR)
1209 recompute_tree_invariant_for_addr_expr (rhs);
1212 /* Determine what needs to be done to update the SSA form. */
1213 update_stmt (stmt);
1214 if (!is_gimple_debug (stmt))
1215 something_changed = true;
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1220 if (did_replace)
1222 fprintf (dump_file, "Folded into: ");
1223 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1224 fprintf (dump_file, "\n");
1226 else
1227 fprintf (dump_file, "Not folded\n");
1232 statistics_counter_event (cfun, "Constants propagated",
1233 prop_stats.num_const_prop);
1234 statistics_counter_event (cfun, "Copies propagated",
1235 prop_stats.num_copy_prop);
1236 statistics_counter_event (cfun, "Statements folded",
1237 prop_stats.num_stmts_folded);
1238 statistics_counter_event (cfun, "Statements deleted",
1239 prop_stats.num_dce);
1240 return something_changed;
1244 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1246 bool
1247 may_propagate_copy (tree dest, tree orig)
1249 tree type_d = TREE_TYPE (dest);
1250 tree type_o = TREE_TYPE (orig);
1252 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
1253 if (TREE_CODE (orig) == SSA_NAME
1254 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1255 /* If it is the default definition and an automatic variable then
1256 we can though and it is important that we do to avoid
1257 uninitialized regular copies. */
1258 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
1259 && (SSA_NAME_VAR (orig) == NULL_TREE
1260 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
1261 return false;
1263 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
1264 cannot be replaced. */
1265 if (TREE_CODE (dest) == SSA_NAME
1266 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1267 return false;
1269 /* Do not copy between types for which we *do* need a conversion. */
1270 if (!useless_type_conversion_p (type_d, type_o))
1271 return false;
1273 /* Generally propagating virtual operands is not ok as that may
1274 create overlapping life-ranges. */
1275 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1276 return false;
1278 /* Anything else is OK. */
1279 return true;
1282 /* Like may_propagate_copy, but use as the destination expression
1283 the principal expression (typically, the RHS) contained in
1284 statement DEST. This is more efficient when working with the
1285 gimple tuples representation. */
1287 bool
1288 may_propagate_copy_into_stmt (gimple dest, tree orig)
1290 tree type_d;
1291 tree type_o;
1293 /* If the statement is a switch or a single-rhs assignment,
1294 then the expression to be replaced by the propagation may
1295 be an SSA_NAME. Fortunately, there is an explicit tree
1296 for the expression, so we delegate to may_propagate_copy. */
1298 if (gimple_assign_single_p (dest))
1299 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1300 else if (gimple_code (dest) == GIMPLE_SWITCH)
1301 return may_propagate_copy (gimple_switch_index (dest), orig);
1303 /* In other cases, the expression is not materialized, so there
1304 is no destination to pass to may_propagate_copy. On the other
1305 hand, the expression cannot be an SSA_NAME, so the analysis
1306 is much simpler. */
1308 if (TREE_CODE (orig) == SSA_NAME
1309 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1310 return false;
1312 if (is_gimple_assign (dest))
1313 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1314 else if (gimple_code (dest) == GIMPLE_COND)
1315 type_d = boolean_type_node;
1316 else if (is_gimple_call (dest)
1317 && gimple_call_lhs (dest) != NULL_TREE)
1318 type_d = TREE_TYPE (gimple_call_lhs (dest));
1319 else
1320 gcc_unreachable ();
1322 type_o = TREE_TYPE (orig);
1324 if (!useless_type_conversion_p (type_d, type_o))
1325 return false;
1327 return true;
1330 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1332 bool
1333 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1335 return true;
1339 /* Common code for propagate_value and replace_exp.
1341 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1342 replacement is done to propagate a value or not. */
1344 static void
1345 replace_exp_1 (use_operand_p op_p, tree val,
1346 bool for_propagation ATTRIBUTE_UNUSED)
1348 #if defined ENABLE_CHECKING
1349 tree op = USE_FROM_PTR (op_p);
1351 gcc_assert (!(for_propagation
1352 && TREE_CODE (op) == SSA_NAME
1353 && TREE_CODE (val) == SSA_NAME
1354 && !may_propagate_copy (op, val)));
1355 #endif
1357 if (TREE_CODE (val) == SSA_NAME)
1358 SET_USE (op_p, val);
1359 else
1360 SET_USE (op_p, unshare_expr (val));
1364 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1365 into the operand pointed to by OP_P.
1367 Use this version for const/copy propagation as it will perform additional
1368 checks to ensure validity of the const/copy propagation. */
1370 void
1371 propagate_value (use_operand_p op_p, tree val)
1373 replace_exp_1 (op_p, val, true);
1376 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1378 Use this version when not const/copy propagating values. For example,
1379 PRE uses this version when building expressions as they would appear
1380 in specific blocks taking into account actions of PHI nodes.
1382 The statement in which an expression has been replaced should be
1383 folded using fold_stmt_inplace. */
1385 void
1386 replace_exp (use_operand_p op_p, tree val)
1388 replace_exp_1 (op_p, val, false);
1392 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1393 into the tree pointed to by OP_P.
1395 Use this version for const/copy propagation when SSA operands are not
1396 available. It will perform the additional checks to ensure validity of
1397 the const/copy propagation, but will not update any operand information.
1398 Be sure to mark the stmt as modified. */
1400 void
1401 propagate_tree_value (tree *op_p, tree val)
1403 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
1404 && *op_p
1405 && TREE_CODE (*op_p) == SSA_NAME
1406 && !may_propagate_copy (*op_p, val)));
1408 if (TREE_CODE (val) == SSA_NAME)
1409 *op_p = val;
1410 else
1411 *op_p = unshare_expr (val);
1415 /* Like propagate_tree_value, but use as the operand to replace
1416 the principal expression (typically, the RHS) contained in the
1417 statement referenced by iterator GSI. Note that it is not
1418 always possible to update the statement in-place, so a new
1419 statement may be created to replace the original. */
1421 void
1422 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1424 gimple stmt = gsi_stmt (*gsi);
1426 if (is_gimple_assign (stmt))
1428 tree expr = NULL_TREE;
1429 if (gimple_assign_single_p (stmt))
1430 expr = gimple_assign_rhs1 (stmt);
1431 propagate_tree_value (&expr, val);
1432 gimple_assign_set_rhs_from_tree (gsi, expr);
1434 else if (gimple_code (stmt) == GIMPLE_COND)
1436 tree lhs = NULL_TREE;
1437 tree rhs = build_zero_cst (TREE_TYPE (val));
1438 propagate_tree_value (&lhs, val);
1439 gimple_cond_set_code (stmt, NE_EXPR);
1440 gimple_cond_set_lhs (stmt, lhs);
1441 gimple_cond_set_rhs (stmt, rhs);
1443 else if (is_gimple_call (stmt)
1444 && gimple_call_lhs (stmt) != NULL_TREE)
1446 tree expr = NULL_TREE;
1447 bool res;
1448 propagate_tree_value (&expr, val);
1449 res = update_call_from_tree (gsi, expr);
1450 gcc_assert (res);
1452 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1453 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
1454 else
1455 gcc_unreachable ();
1458 #include "gt-tree-ssa-propagate.h"