Only allow e500 double in SPE_SIMD_REGNO_P registers.
[official-gcc.git] / gcc / tree-ssa-propagate.c
blobc62749a16235402e9a1745126f60bd5718c3cc80
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 "hashtab.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "gimple-pretty-print.h"
37 #include "dumpfile.h"
38 #include "bitmap.h"
39 #include "sbitmap.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "gimple-ssa.h"
50 #include "tree-cfg.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "stringpool.h"
54 #include "tree-ssanames.h"
55 #include "tree-ssa.h"
56 #include "tree-ssa-propagate.h"
57 #include "langhooks.h"
58 #include "value-prof.h"
59 #include "domwalk.h"
61 /* This file implements a generic value propagation engine based on
62 the same propagation used by the SSA-CCP algorithm [1].
64 Propagation is performed by simulating the execution of every
65 statement that produces the value being propagated. Simulation
66 proceeds as follows:
68 1- Initially, all edges of the CFG are marked not executable and
69 the CFG worklist is seeded with all the statements in the entry
70 basic block (block 0).
72 2- Every statement S is simulated with a call to the call-back
73 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
74 results:
76 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
77 interest and does not affect any of the work lists.
79 SSA_PROP_VARYING: The value produced by S cannot be determined
80 at compile time. Further simulation of S is not required.
81 If S is a conditional jump, all the outgoing edges for the
82 block are considered executable and added to the work
83 list.
85 SSA_PROP_INTERESTING: S produces a value that can be computed
86 at compile time. Its result can be propagated into the
87 statements that feed from S. Furthermore, if S is a
88 conditional jump, only the edge known to be taken is added
89 to the work list. Edges that are known not to execute are
90 never simulated.
92 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
93 return value from SSA_PROP_VISIT_PHI has the same semantics as
94 described in #2.
96 4- Three work lists are kept. Statements are only added to these
97 lists if they produce one of SSA_PROP_INTERESTING or
98 SSA_PROP_VARYING.
100 CFG_BLOCKS contains the list of blocks to be simulated.
101 Blocks are added to this list if their incoming edges are
102 found executable.
104 VARYING_SSA_EDGES contains the list of statements that feed
105 from statements that produce an SSA_PROP_VARYING result.
106 These are simulated first to speed up processing.
108 INTERESTING_SSA_EDGES contains the list of statements that
109 feed from statements that produce an SSA_PROP_INTERESTING
110 result.
112 5- Simulation terminates when all three work lists are drained.
114 Before calling ssa_propagate, it is important to clear
115 prop_simulate_again_p for all the statements in the program that
116 should be simulated. This initialization allows an implementation
117 to specify which statements should never be simulated.
119 It is also important to compute def-use information before calling
120 ssa_propagate.
122 References:
124 [1] Constant propagation with conditional branches,
125 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
127 [2] Building an Optimizing Compiler,
128 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
130 [3] Advanced Compiler Design and Implementation,
131 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
133 /* Function pointers used to parameterize the propagation engine. */
134 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
135 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
137 /* Keep track of statements that have been added to one of the SSA
138 edges worklists. This flag is used to avoid visiting statements
139 unnecessarily when draining an SSA edge worklist. If while
140 simulating a basic block, we find a statement with
141 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
142 processing from visiting it again.
144 NOTE: users of the propagation engine are not allowed to use
145 the GF_PLF_1 flag. */
146 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
148 /* A bitmap to keep track of executable blocks in the CFG. */
149 static sbitmap executable_blocks;
151 /* Array of control flow edges on the worklist. */
152 static vec<basic_block> cfg_blocks;
154 static unsigned int cfg_blocks_num = 0;
155 static int cfg_blocks_tail;
156 static int cfg_blocks_head;
158 static sbitmap bb_in_list;
160 /* Worklist of SSA edges which will need reexamination as their
161 definition has changed. SSA edges are def-use edges in the SSA
162 web. For each D-U edge, we store the target statement or PHI node
163 U. */
164 static vec<gimple> interesting_ssa_edges;
166 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
167 list of SSA edges is split into two. One contains all SSA edges
168 who need to be reexamined because their lattice value changed to
169 varying (this worklist), and the other contains all other SSA edges
170 to be reexamined (INTERESTING_SSA_EDGES).
172 Since most values in the program are VARYING, the ideal situation
173 is to move them to that lattice value as quickly as possible.
174 Thus, it doesn't make sense to process any other type of lattice
175 value until all VARYING values are propagated fully, which is one
176 thing using the VARYING worklist achieves. In addition, if we
177 don't use a separate worklist for VARYING edges, we end up with
178 situations where lattice values move from
179 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
180 static vec<gimple> varying_ssa_edges;
183 /* Return true if the block worklist empty. */
185 static inline bool
186 cfg_blocks_empty_p (void)
188 return (cfg_blocks_num == 0);
192 /* Add a basic block to the worklist. The block must not be already
193 in the worklist, and it must not be the ENTRY or EXIT block. */
195 static void
196 cfg_blocks_add (basic_block bb)
198 bool head = false;
200 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
201 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
202 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
204 if (cfg_blocks_empty_p ())
206 cfg_blocks_tail = cfg_blocks_head = 0;
207 cfg_blocks_num = 1;
209 else
211 cfg_blocks_num++;
212 if (cfg_blocks_num > cfg_blocks.length ())
214 /* We have to grow the array now. Adjust to queue to occupy
215 the full space of the original array. We do not need to
216 initialize the newly allocated portion of the array
217 because we keep track of CFG_BLOCKS_HEAD and
218 CFG_BLOCKS_HEAD. */
219 cfg_blocks_tail = cfg_blocks.length ();
220 cfg_blocks_head = 0;
221 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
223 /* Minor optimization: we prefer to see blocks with more
224 predecessors later, because there is more of a chance that
225 the incoming edges will be executable. */
226 else if (EDGE_COUNT (bb->preds)
227 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
228 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
229 else
231 if (cfg_blocks_head == 0)
232 cfg_blocks_head = cfg_blocks.length ();
233 --cfg_blocks_head;
234 head = true;
238 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
239 bitmap_set_bit (bb_in_list, bb->index);
243 /* Remove a block from the worklist. */
245 static basic_block
246 cfg_blocks_get (void)
248 basic_block bb;
250 bb = cfg_blocks[cfg_blocks_head];
252 gcc_assert (!cfg_blocks_empty_p ());
253 gcc_assert (bb);
255 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
256 --cfg_blocks_num;
257 bitmap_clear_bit (bb_in_list, bb->index);
259 return bb;
263 /* We have just defined a new value for VAR. If IS_VARYING is true,
264 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
265 them to INTERESTING_SSA_EDGES. */
267 static void
268 add_ssa_edge (tree var, bool is_varying)
270 imm_use_iterator iter;
271 use_operand_p use_p;
273 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
275 gimple use_stmt = USE_STMT (use_p);
277 if (prop_simulate_again_p (use_stmt)
278 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
280 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
281 if (is_varying)
282 varying_ssa_edges.safe_push (use_stmt);
283 else
284 interesting_ssa_edges.safe_push (use_stmt);
290 /* Add edge E to the control flow worklist. */
292 static void
293 add_control_edge (edge e)
295 basic_block bb = e->dest;
296 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
297 return;
299 /* If the edge had already been executed, skip it. */
300 if (e->flags & EDGE_EXECUTABLE)
301 return;
303 e->flags |= EDGE_EXECUTABLE;
305 /* If the block is already in the list, we're done. */
306 if (bitmap_bit_p (bb_in_list, bb->index))
307 return;
309 cfg_blocks_add (bb);
311 if (dump_file && (dump_flags & TDF_DETAILS))
312 fprintf (dump_file, "\nAdding Destination of edge (%d -> %d) to worklist\n",
313 e->src->index, e->dest->index);
317 /* Simulate the execution of STMT and update the work lists accordingly. */
319 static void
320 simulate_stmt (gimple stmt)
322 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
323 edge taken_edge = NULL;
324 tree output_name = NULL_TREE;
326 /* Don't bother visiting statements that are already
327 considered varying by the propagator. */
328 if (!prop_simulate_again_p (stmt))
329 return;
331 if (gimple_code (stmt) == GIMPLE_PHI)
333 val = ssa_prop_visit_phi (stmt);
334 output_name = gimple_phi_result (stmt);
336 else
337 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
339 if (val == SSA_PROP_VARYING)
341 prop_set_simulate_again (stmt, false);
343 /* If the statement produced a new varying value, add the SSA
344 edges coming out of OUTPUT_NAME. */
345 if (output_name)
346 add_ssa_edge (output_name, true);
348 /* If STMT transfers control out of its basic block, add
349 all outgoing edges to the work list. */
350 if (stmt_ends_bb_p (stmt))
352 edge e;
353 edge_iterator ei;
354 basic_block bb = gimple_bb (stmt);
355 FOR_EACH_EDGE (e, ei, bb->succs)
356 add_control_edge (e);
359 else if (val == SSA_PROP_INTERESTING)
361 /* If the statement produced new value, add the SSA edges coming
362 out of OUTPUT_NAME. */
363 if (output_name)
364 add_ssa_edge (output_name, false);
366 /* If we know which edge is going to be taken out of this block,
367 add it to the CFG work list. */
368 if (taken_edge)
369 add_control_edge (taken_edge);
373 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
374 drain. This pops statements off the given WORKLIST and processes
375 them until there are no more statements on WORKLIST.
376 We take a pointer to WORKLIST because it may be reallocated when an
377 SSA edge is added to it in simulate_stmt. */
379 static void
380 process_ssa_edge_worklist (vec<gimple> *worklist)
382 /* Drain the entire worklist. */
383 while (worklist->length () > 0)
385 basic_block bb;
387 /* Pull the statement to simulate off the worklist. */
388 gimple stmt = worklist->pop ();
390 /* If this statement was already visited by simulate_block, then
391 we don't need to visit it again here. */
392 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
393 continue;
395 /* STMT is no longer in a worklist. */
396 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
398 if (dump_file && (dump_flags & TDF_DETAILS))
400 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
401 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
404 bb = gimple_bb (stmt);
406 /* PHI nodes are always visited, regardless of whether or not
407 the destination block is executable. Otherwise, visit the
408 statement only if its block is marked executable. */
409 if (gimple_code (stmt) == GIMPLE_PHI
410 || bitmap_bit_p (executable_blocks, bb->index))
411 simulate_stmt (stmt);
416 /* Simulate the execution of BLOCK. Evaluate the statement associated
417 with each variable reference inside the block. */
419 static void
420 simulate_block (basic_block block)
422 gimple_stmt_iterator gsi;
424 /* There is nothing to do for the exit block. */
425 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
426 return;
428 if (dump_file && (dump_flags & TDF_DETAILS))
429 fprintf (dump_file, "\nSimulating block %d\n", block->index);
431 /* Always simulate PHI nodes, even if we have simulated this block
432 before. */
433 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
434 simulate_stmt (gsi_stmt (gsi));
436 /* If this is the first time we've simulated this block, then we
437 must simulate each of its statements. */
438 if (!bitmap_bit_p (executable_blocks, block->index))
440 gimple_stmt_iterator j;
441 unsigned int normal_edge_count;
442 edge e, normal_edge;
443 edge_iterator ei;
445 /* Note that we have simulated this block. */
446 bitmap_set_bit (executable_blocks, block->index);
448 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
450 gimple stmt = gsi_stmt (j);
452 /* If this statement is already in the worklist then
453 "cancel" it. The reevaluation implied by the worklist
454 entry will produce the same value we generate here and
455 thus reevaluating it again from the worklist is
456 pointless. */
457 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
458 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
460 simulate_stmt (stmt);
463 /* We can not predict when abnormal and EH edges will be executed, so
464 once a block is considered executable, we consider any
465 outgoing abnormal edges as executable.
467 TODO: This is not exactly true. Simplifying statement might
468 prove it non-throwing and also computed goto can be handled
469 when destination is known.
471 At the same time, if this block has only one successor that is
472 reached by non-abnormal edges, then add that successor to the
473 worklist. */
474 normal_edge_count = 0;
475 normal_edge = NULL;
476 FOR_EACH_EDGE (e, ei, block->succs)
478 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
479 add_control_edge (e);
480 else
482 normal_edge_count++;
483 normal_edge = e;
487 if (normal_edge_count == 1)
488 add_control_edge (normal_edge);
493 /* Initialize local data structures and work lists. */
495 static void
496 ssa_prop_init (void)
498 edge e;
499 edge_iterator ei;
500 basic_block bb;
502 /* Worklists of SSA edges. */
503 interesting_ssa_edges.create (20);
504 varying_ssa_edges.create (20);
506 executable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
507 bitmap_clear (executable_blocks);
509 bb_in_list = sbitmap_alloc (last_basic_block_for_fn (cfun));
510 bitmap_clear (bb_in_list);
512 if (dump_file && (dump_flags & TDF_DETAILS))
513 dump_immediate_uses (dump_file);
515 cfg_blocks.create (20);
516 cfg_blocks.safe_grow_cleared (20);
518 /* Initially assume that every edge in the CFG is not executable.
519 (including the edges coming out of the entry block). */
520 FOR_ALL_BB_FN (bb, cfun)
522 gimple_stmt_iterator si;
524 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
525 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
527 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
528 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
530 FOR_EACH_EDGE (e, ei, bb->succs)
531 e->flags &= ~EDGE_EXECUTABLE;
534 /* Seed the algorithm by adding the successors of the entry block to the
535 edge worklist. */
536 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
537 add_control_edge (e);
541 /* Free allocated storage. */
543 static void
544 ssa_prop_fini (void)
546 interesting_ssa_edges.release ();
547 varying_ssa_edges.release ();
548 cfg_blocks.release ();
549 sbitmap_free (bb_in_list);
550 sbitmap_free (executable_blocks);
554 /* Return true if EXPR is an acceptable right-hand-side for a
555 GIMPLE assignment. We validate the entire tree, not just
556 the root node, thus catching expressions that embed complex
557 operands that are not permitted in GIMPLE. This function
558 is needed because the folding routines in fold-const.c
559 may return such expressions in some cases, e.g., an array
560 access with an embedded index addition. It may make more
561 sense to have folding routines that are sensitive to the
562 constraints on GIMPLE operands, rather than abandoning any
563 any attempt to fold if the usual folding turns out to be too
564 aggressive. */
566 bool
567 valid_gimple_rhs_p (tree expr)
569 enum tree_code code = TREE_CODE (expr);
571 switch (TREE_CODE_CLASS (code))
573 case tcc_declaration:
574 if (!is_gimple_variable (expr))
575 return false;
576 break;
578 case tcc_constant:
579 /* All constants are ok. */
580 break;
582 case tcc_comparison:
583 /* GENERIC allows comparisons with non-boolean types, reject
584 those for GIMPLE. Let vector-typed comparisons pass - rules
585 for GENERIC and GIMPLE are the same here. */
586 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
587 && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
588 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
589 && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
590 return false;
592 /* Fallthru. */
593 case tcc_binary:
594 if (!is_gimple_val (TREE_OPERAND (expr, 0))
595 || !is_gimple_val (TREE_OPERAND (expr, 1)))
596 return false;
597 break;
599 case tcc_unary:
600 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
601 return false;
602 break;
604 case tcc_expression:
605 switch (code)
607 case ADDR_EXPR:
609 tree t;
610 if (is_gimple_min_invariant (expr))
611 return true;
612 t = TREE_OPERAND (expr, 0);
613 while (handled_component_p (t))
615 /* ??? More checks needed, see the GIMPLE verifier. */
616 if ((TREE_CODE (t) == ARRAY_REF
617 || TREE_CODE (t) == ARRAY_RANGE_REF)
618 && !is_gimple_val (TREE_OPERAND (t, 1)))
619 return false;
620 t = TREE_OPERAND (t, 0);
622 if (!is_gimple_id (t))
623 return false;
625 break;
627 default:
628 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
630 if (((code == VEC_COND_EXPR || code == COND_EXPR)
631 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
632 : !is_gimple_val (TREE_OPERAND (expr, 0)))
633 || !is_gimple_val (TREE_OPERAND (expr, 1))
634 || !is_gimple_val (TREE_OPERAND (expr, 2)))
635 return false;
636 break;
638 return false;
640 break;
642 case tcc_vl_exp:
643 return false;
645 case tcc_exceptional:
646 if (code == CONSTRUCTOR)
648 unsigned i;
649 tree elt;
650 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
651 if (!is_gimple_val (elt))
652 return false;
653 return true;
655 if (code != SSA_NAME)
656 return false;
657 break;
659 case tcc_reference:
660 if (code == BIT_FIELD_REF)
661 return is_gimple_val (TREE_OPERAND (expr, 0));
662 return false;
664 default:
665 return false;
668 return true;
672 /* Return true if EXPR is a CALL_EXPR suitable for representation
673 as a single GIMPLE_CALL statement. If the arguments require
674 further gimplification, return false. */
676 static bool
677 valid_gimple_call_p (tree expr)
679 unsigned i, nargs;
681 if (TREE_CODE (expr) != CALL_EXPR)
682 return false;
684 nargs = call_expr_nargs (expr);
685 for (i = 0; i < nargs; i++)
687 tree arg = CALL_EXPR_ARG (expr, i);
688 if (is_gimple_reg_type (TREE_TYPE (arg)))
690 if (!is_gimple_val (arg))
691 return false;
693 else
694 if (!is_gimple_lvalue (arg))
695 return false;
698 return true;
702 /* Make SSA names defined by OLD_STMT point to NEW_STMT
703 as their defining statement. */
705 void
706 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
708 tree var;
709 ssa_op_iter iter;
711 if (gimple_in_ssa_p (cfun))
713 /* Make defined SSA_NAMEs point to the new
714 statement as their definition. */
715 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
717 if (TREE_CODE (var) == SSA_NAME)
718 SSA_NAME_DEF_STMT (var) = new_stmt;
723 /* Helper function for update_gimple_call and update_call_from_tree.
724 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
726 static void
727 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
728 gimple stmt)
730 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
731 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
732 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
733 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
734 gimple_set_location (new_stmt, gimple_location (stmt));
735 if (gimple_block (new_stmt) == NULL_TREE)
736 gimple_set_block (new_stmt, gimple_block (stmt));
737 gsi_replace (si_p, new_stmt, false);
740 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
741 with number of arguments NARGS, where the arguments in GIMPLE form
742 follow NARGS argument. */
744 bool
745 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
747 va_list ap;
748 gimple new_stmt, stmt = gsi_stmt (*si_p);
750 gcc_assert (is_gimple_call (stmt));
751 va_start (ap, nargs);
752 new_stmt = gimple_build_call_valist (fn, nargs, ap);
753 finish_update_gimple_call (si_p, new_stmt, stmt);
754 va_end (ap);
755 return true;
758 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
759 value of EXPR, which is expected to be the result of folding the
760 call. This can only be done if EXPR is a CALL_EXPR with valid
761 GIMPLE operands as arguments, or if it is a suitable RHS expression
762 for a GIMPLE_ASSIGN. More complex expressions will require
763 gimplification, which will introduce additional statements. In this
764 event, no update is performed, and the function returns false.
765 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
766 replace the statement at *SI_P with an entirely new statement.
767 The new statement need not be a call, e.g., if the original call
768 folded to a constant. */
770 bool
771 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
773 gimple stmt = gsi_stmt (*si_p);
775 if (valid_gimple_call_p (expr))
777 /* The call has simplified to another call. */
778 tree fn = CALL_EXPR_FN (expr);
779 unsigned i;
780 unsigned nargs = call_expr_nargs (expr);
781 vec<tree> args = vNULL;
782 gimple new_stmt;
784 if (nargs > 0)
786 args.create (nargs);
787 args.safe_grow_cleared (nargs);
789 for (i = 0; i < nargs; i++)
790 args[i] = CALL_EXPR_ARG (expr, i);
793 new_stmt = gimple_build_call_vec (fn, args);
794 finish_update_gimple_call (si_p, new_stmt, stmt);
795 args.release ();
797 return true;
799 else if (valid_gimple_rhs_p (expr))
801 tree lhs = gimple_call_lhs (stmt);
802 gimple new_stmt;
804 /* The call has simplified to an expression
805 that cannot be represented as a GIMPLE_CALL. */
806 if (lhs)
808 /* A value is expected.
809 Introduce a new GIMPLE_ASSIGN statement. */
810 STRIP_USELESS_TYPE_CONVERSION (expr);
811 new_stmt = gimple_build_assign (lhs, expr);
812 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
813 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
814 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
816 else if (!TREE_SIDE_EFFECTS (expr))
818 /* No value is expected, and EXPR has no effect.
819 Replace it with an empty statement. */
820 new_stmt = gimple_build_nop ();
821 if (gimple_in_ssa_p (cfun))
823 unlink_stmt_vdef (stmt);
824 release_defs (stmt);
827 else
829 /* No value is expected, but EXPR has an effect,
830 e.g., it could be a reference to a volatile
831 variable. Create an assignment statement
832 with a dummy (unused) lhs variable. */
833 STRIP_USELESS_TYPE_CONVERSION (expr);
834 if (gimple_in_ssa_p (cfun))
835 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
836 else
837 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
838 new_stmt = gimple_build_assign (lhs, expr);
839 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
840 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
841 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
843 gimple_set_location (new_stmt, gimple_location (stmt));
844 gsi_replace (si_p, new_stmt, false);
845 return true;
847 else
848 /* The call simplified to an expression that is
849 not a valid GIMPLE RHS. */
850 return false;
854 /* Entry point to the propagation engine.
856 VISIT_STMT is called for every statement visited.
857 VISIT_PHI is called for every PHI node visited. */
859 void
860 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
861 ssa_prop_visit_phi_fn visit_phi)
863 ssa_prop_visit_stmt = visit_stmt;
864 ssa_prop_visit_phi = visit_phi;
866 ssa_prop_init ();
868 /* Iterate until the worklists are empty. */
869 while (!cfg_blocks_empty_p ()
870 || interesting_ssa_edges.length () > 0
871 || varying_ssa_edges.length () > 0)
873 if (!cfg_blocks_empty_p ())
875 /* Pull the next block to simulate off the worklist. */
876 basic_block dest_block = cfg_blocks_get ();
877 simulate_block (dest_block);
880 /* In order to move things to varying as quickly as
881 possible,process the VARYING_SSA_EDGES worklist first. */
882 process_ssa_edge_worklist (&varying_ssa_edges);
884 /* Now process the INTERESTING_SSA_EDGES worklist. */
885 process_ssa_edge_worklist (&interesting_ssa_edges);
888 ssa_prop_fini ();
892 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
893 is a non-volatile pointer dereference, a structure reference or a
894 reference to a single _DECL. Ignore volatile memory references
895 because they are not interesting for the optimizers. */
897 bool
898 stmt_makes_single_store (gimple stmt)
900 tree lhs;
902 if (gimple_code (stmt) != GIMPLE_ASSIGN
903 && gimple_code (stmt) != GIMPLE_CALL)
904 return false;
906 if (!gimple_vdef (stmt))
907 return false;
909 lhs = gimple_get_lhs (stmt);
911 /* A call statement may have a null LHS. */
912 if (!lhs)
913 return false;
915 return (!TREE_THIS_VOLATILE (lhs)
916 && (DECL_P (lhs)
917 || REFERENCE_CLASS_P (lhs)));
921 /* Propagation statistics. */
922 struct prop_stats_d
924 long num_const_prop;
925 long num_copy_prop;
926 long num_stmts_folded;
927 long num_dce;
930 static struct prop_stats_d prop_stats;
932 /* Replace USE references in statement STMT with the values stored in
933 PROP_VALUE. Return true if at least one reference was replaced. */
935 static bool
936 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
938 bool replaced = false;
939 use_operand_p use;
940 ssa_op_iter iter;
942 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
944 tree tuse = USE_FROM_PTR (use);
945 tree val = (*get_value) (tuse);
947 if (val == tuse || val == NULL_TREE)
948 continue;
950 if (gimple_code (stmt) == GIMPLE_ASM
951 && !may_propagate_copy_into_asm (tuse))
952 continue;
954 if (!may_propagate_copy (tuse, val))
955 continue;
957 if (TREE_CODE (val) != SSA_NAME)
958 prop_stats.num_const_prop++;
959 else
960 prop_stats.num_copy_prop++;
962 propagate_value (use, val);
964 replaced = true;
967 return replaced;
971 /* Replace propagated values into all the arguments for PHI using the
972 values from PROP_VALUE. */
974 static bool
975 replace_phi_args_in (gimple phi, ssa_prop_get_value_fn get_value)
977 size_t i;
978 bool replaced = false;
980 if (dump_file && (dump_flags & TDF_DETAILS))
982 fprintf (dump_file, "Folding PHI node: ");
983 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
986 for (i = 0; i < gimple_phi_num_args (phi); i++)
988 tree arg = gimple_phi_arg_def (phi, i);
990 if (TREE_CODE (arg) == SSA_NAME)
992 tree val = (*get_value) (arg);
994 if (val && val != arg && may_propagate_copy (arg, val))
996 if (TREE_CODE (val) != SSA_NAME)
997 prop_stats.num_const_prop++;
998 else
999 prop_stats.num_copy_prop++;
1001 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
1002 replaced = true;
1004 /* If we propagated a copy and this argument flows
1005 through an abnormal edge, update the replacement
1006 accordingly. */
1007 if (TREE_CODE (val) == SSA_NAME
1008 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
1009 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1014 if (dump_file && (dump_flags & TDF_DETAILS))
1016 if (!replaced)
1017 fprintf (dump_file, "No folding possible\n");
1018 else
1020 fprintf (dump_file, "Folded into: ");
1021 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1022 fprintf (dump_file, "\n");
1026 return replaced;
1030 class substitute_and_fold_dom_walker : public dom_walker
1032 public:
1033 substitute_and_fold_dom_walker (cdi_direction direction,
1034 ssa_prop_get_value_fn get_value_fn_,
1035 ssa_prop_fold_stmt_fn fold_fn_,
1036 bool do_dce_)
1037 : dom_walker (direction), get_value_fn (get_value_fn_),
1038 fold_fn (fold_fn_), do_dce (do_dce_), something_changed (false)
1040 stmts_to_remove.create (0);
1041 need_eh_cleanup = BITMAP_ALLOC (NULL);
1043 ~substitute_and_fold_dom_walker ()
1045 stmts_to_remove.release ();
1046 BITMAP_FREE (need_eh_cleanup);
1049 virtual void before_dom_children (basic_block);
1050 virtual void after_dom_children (basic_block) {}
1052 ssa_prop_get_value_fn get_value_fn;
1053 ssa_prop_fold_stmt_fn fold_fn;
1054 bool do_dce;
1055 bool something_changed;
1056 vec<gimple> stmts_to_remove;
1057 bitmap need_eh_cleanup;
1060 void
1061 substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
1063 gimple_stmt_iterator i;
1065 /* Propagate known values into PHI nodes. */
1066 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
1068 gimple phi = gsi_stmt (i);
1069 tree res = gimple_phi_result (phi);
1070 if (virtual_operand_p (res))
1071 continue;
1072 if (do_dce
1073 && res && TREE_CODE (res) == SSA_NAME)
1075 tree sprime = get_value_fn (res);
1076 if (sprime
1077 && sprime != res
1078 && may_propagate_copy (res, sprime))
1080 stmts_to_remove.safe_push (phi);
1081 continue;
1084 something_changed |= replace_phi_args_in (phi, get_value_fn);
1087 /* Propagate known values into stmts. In some case it exposes
1088 more trivially deletable stmts to walk backward. */
1089 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1091 bool did_replace;
1092 gimple stmt = gsi_stmt (i);
1093 gimple old_stmt;
1094 enum gimple_code code = gimple_code (stmt);
1096 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1097 range information for names and they are discarded
1098 afterwards. */
1100 if (code == GIMPLE_ASSIGN
1101 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1102 continue;
1104 /* No point propagating into a stmt we have a value for we
1105 can propagate into all uses. Mark it for removal instead. */
1106 tree lhs = gimple_get_lhs (stmt);
1107 if (do_dce
1108 && lhs && TREE_CODE (lhs) == SSA_NAME)
1110 tree sprime = get_value_fn (lhs);
1111 if (sprime
1112 && sprime != lhs
1113 && may_propagate_copy (lhs, sprime)
1114 && !stmt_could_throw_p (stmt)
1115 && !gimple_has_side_effects (stmt))
1117 stmts_to_remove.safe_push (stmt);
1118 continue;
1122 /* Replace the statement with its folded version and mark it
1123 folded. */
1124 did_replace = false;
1125 if (dump_file && (dump_flags & TDF_DETAILS))
1127 fprintf (dump_file, "Folding statement: ");
1128 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1131 old_stmt = stmt;
1133 /* Some statements may be simplified using propagator
1134 specific information. Do this before propagating
1135 into the stmt to not disturb pass specific information. */
1136 if (fold_fn
1137 && (*fold_fn)(&i))
1139 did_replace = true;
1140 prop_stats.num_stmts_folded++;
1141 stmt = gsi_stmt (i);
1142 update_stmt (stmt);
1145 /* Replace real uses in the statement. */
1146 did_replace |= replace_uses_in (stmt, get_value_fn);
1148 /* If we made a replacement, fold the statement. */
1149 if (did_replace)
1150 fold_stmt (&i);
1152 /* Now cleanup. */
1153 if (did_replace)
1155 stmt = gsi_stmt (i);
1157 /* If we cleaned up EH information from the statement,
1158 remove EH edges. */
1159 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1160 bitmap_set_bit (need_eh_cleanup, bb->index);
1162 if (is_gimple_assign (stmt)
1163 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1164 == GIMPLE_SINGLE_RHS))
1166 tree rhs = gimple_assign_rhs1 (stmt);
1168 if (TREE_CODE (rhs) == ADDR_EXPR)
1169 recompute_tree_invariant_for_addr_expr (rhs);
1172 /* Determine what needs to be done to update the SSA form. */
1173 update_stmt (stmt);
1174 if (!is_gimple_debug (stmt))
1175 something_changed = true;
1178 if (dump_file && (dump_flags & TDF_DETAILS))
1180 if (did_replace)
1182 fprintf (dump_file, "Folded into: ");
1183 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1184 fprintf (dump_file, "\n");
1186 else
1187 fprintf (dump_file, "Not folded\n");
1194 /* Perform final substitution and folding of propagated values.
1196 PROP_VALUE[I] contains the single value that should be substituted
1197 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1198 substituted.
1200 If FOLD_FN is non-NULL the function will be invoked on all statements
1201 before propagating values for pass specific simplification.
1203 DO_DCE is true if trivially dead stmts can be removed.
1205 If DO_DCE is true, the statements within a BB are walked from
1206 last to first element. Otherwise we scan from first to last element.
1208 Return TRUE when something changed. */
1210 bool
1211 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1212 ssa_prop_fold_stmt_fn fold_fn,
1213 bool do_dce)
1215 gcc_assert (get_value_fn);
1217 if (dump_file && (dump_flags & TDF_DETAILS))
1218 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1220 memset (&prop_stats, 0, sizeof (prop_stats));
1222 calculate_dominance_info (CDI_DOMINATORS);
1223 substitute_and_fold_dom_walker walker(CDI_DOMINATORS,
1224 get_value_fn, fold_fn, do_dce);
1225 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1227 /* We cannot remove stmts during the BB walk, especially not release
1228 SSA names there as that destroys the lattice of our callers.
1229 Remove stmts in reverse order to make debug stmt creation possible. */
1230 while (!walker.stmts_to_remove.is_empty ())
1232 gimple stmt = walker.stmts_to_remove.pop ();
1233 if (dump_file && dump_flags & TDF_DETAILS)
1235 fprintf (dump_file, "Removing dead stmt ");
1236 print_gimple_stmt (dump_file, stmt, 0, 0);
1237 fprintf (dump_file, "\n");
1239 prop_stats.num_dce++;
1240 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1241 if (gimple_code (stmt) == GIMPLE_PHI)
1242 remove_phi_node (&gsi, true);
1243 else
1245 unlink_stmt_vdef (stmt);
1246 gsi_remove (&gsi, true);
1247 release_defs (stmt);
1251 if (!bitmap_empty_p (walker.need_eh_cleanup))
1252 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1254 statistics_counter_event (cfun, "Constants propagated",
1255 prop_stats.num_const_prop);
1256 statistics_counter_event (cfun, "Copies propagated",
1257 prop_stats.num_copy_prop);
1258 statistics_counter_event (cfun, "Statements folded",
1259 prop_stats.num_stmts_folded);
1260 statistics_counter_event (cfun, "Statements deleted",
1261 prop_stats.num_dce);
1263 return walker.something_changed;
1267 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1269 bool
1270 may_propagate_copy (tree dest, tree orig)
1272 tree type_d = TREE_TYPE (dest);
1273 tree type_o = TREE_TYPE (orig);
1275 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
1276 if (TREE_CODE (orig) == SSA_NAME
1277 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1278 /* If it is the default definition and an automatic variable then
1279 we can though and it is important that we do to avoid
1280 uninitialized regular copies. */
1281 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
1282 && (SSA_NAME_VAR (orig) == NULL_TREE
1283 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
1284 return false;
1286 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
1287 cannot be replaced. */
1288 if (TREE_CODE (dest) == SSA_NAME
1289 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1290 return false;
1292 /* Do not copy between types for which we *do* need a conversion. */
1293 if (!useless_type_conversion_p (type_d, type_o))
1294 return false;
1296 /* Generally propagating virtual operands is not ok as that may
1297 create overlapping life-ranges. */
1298 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1299 return false;
1301 /* Anything else is OK. */
1302 return true;
1305 /* Like may_propagate_copy, but use as the destination expression
1306 the principal expression (typically, the RHS) contained in
1307 statement DEST. This is more efficient when working with the
1308 gimple tuples representation. */
1310 bool
1311 may_propagate_copy_into_stmt (gimple dest, tree orig)
1313 tree type_d;
1314 tree type_o;
1316 /* If the statement is a switch or a single-rhs assignment,
1317 then the expression to be replaced by the propagation may
1318 be an SSA_NAME. Fortunately, there is an explicit tree
1319 for the expression, so we delegate to may_propagate_copy. */
1321 if (gimple_assign_single_p (dest))
1322 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1323 else if (gimple_code (dest) == GIMPLE_SWITCH)
1324 return may_propagate_copy (gimple_switch_index (dest), orig);
1326 /* In other cases, the expression is not materialized, so there
1327 is no destination to pass to may_propagate_copy. On the other
1328 hand, the expression cannot be an SSA_NAME, so the analysis
1329 is much simpler. */
1331 if (TREE_CODE (orig) == SSA_NAME
1332 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1333 return false;
1335 if (is_gimple_assign (dest))
1336 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1337 else if (gimple_code (dest) == GIMPLE_COND)
1338 type_d = boolean_type_node;
1339 else if (is_gimple_call (dest)
1340 && gimple_call_lhs (dest) != NULL_TREE)
1341 type_d = TREE_TYPE (gimple_call_lhs (dest));
1342 else
1343 gcc_unreachable ();
1345 type_o = TREE_TYPE (orig);
1347 if (!useless_type_conversion_p (type_d, type_o))
1348 return false;
1350 return true;
1353 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1355 bool
1356 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1358 return true;
1362 /* Common code for propagate_value and replace_exp.
1364 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1365 replacement is done to propagate a value or not. */
1367 static void
1368 replace_exp_1 (use_operand_p op_p, tree val,
1369 bool for_propagation ATTRIBUTE_UNUSED)
1371 #if defined ENABLE_CHECKING
1372 tree op = USE_FROM_PTR (op_p);
1374 gcc_assert (!(for_propagation
1375 && TREE_CODE (op) == SSA_NAME
1376 && TREE_CODE (val) == SSA_NAME
1377 && !may_propagate_copy (op, val)));
1378 #endif
1380 if (TREE_CODE (val) == SSA_NAME)
1381 SET_USE (op_p, val);
1382 else
1383 SET_USE (op_p, unshare_expr (val));
1387 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1388 into the operand pointed to by OP_P.
1390 Use this version for const/copy propagation as it will perform additional
1391 checks to ensure validity of the const/copy propagation. */
1393 void
1394 propagate_value (use_operand_p op_p, tree val)
1396 replace_exp_1 (op_p, val, true);
1399 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1401 Use this version when not const/copy propagating values. For example,
1402 PRE uses this version when building expressions as they would appear
1403 in specific blocks taking into account actions of PHI nodes.
1405 The statement in which an expression has been replaced should be
1406 folded using fold_stmt_inplace. */
1408 void
1409 replace_exp (use_operand_p op_p, tree val)
1411 replace_exp_1 (op_p, val, false);
1415 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1416 into the tree pointed to by OP_P.
1418 Use this version for const/copy propagation when SSA operands are not
1419 available. It will perform the additional checks to ensure validity of
1420 the const/copy propagation, but will not update any operand information.
1421 Be sure to mark the stmt as modified. */
1423 void
1424 propagate_tree_value (tree *op_p, tree val)
1426 if (TREE_CODE (val) == SSA_NAME)
1427 *op_p = val;
1428 else
1429 *op_p = unshare_expr (val);
1433 /* Like propagate_tree_value, but use as the operand to replace
1434 the principal expression (typically, the RHS) contained in the
1435 statement referenced by iterator GSI. Note that it is not
1436 always possible to update the statement in-place, so a new
1437 statement may be created to replace the original. */
1439 void
1440 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1442 gimple stmt = gsi_stmt (*gsi);
1444 if (is_gimple_assign (stmt))
1446 tree expr = NULL_TREE;
1447 if (gimple_assign_single_p (stmt))
1448 expr = gimple_assign_rhs1 (stmt);
1449 propagate_tree_value (&expr, val);
1450 gimple_assign_set_rhs_from_tree (gsi, expr);
1452 else if (gimple_code (stmt) == GIMPLE_COND)
1454 tree lhs = NULL_TREE;
1455 tree rhs = build_zero_cst (TREE_TYPE (val));
1456 propagate_tree_value (&lhs, val);
1457 gimple_cond_set_code (stmt, NE_EXPR);
1458 gimple_cond_set_lhs (stmt, lhs);
1459 gimple_cond_set_rhs (stmt, rhs);
1461 else if (is_gimple_call (stmt)
1462 && gimple_call_lhs (stmt) != NULL_TREE)
1464 tree expr = NULL_TREE;
1465 bool res;
1466 propagate_tree_value (&expr, val);
1467 res = update_call_from_tree (gsi, expr);
1468 gcc_assert (res);
1470 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1471 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
1472 else
1473 gcc_unreachable ();