Daily bump.
[official-gcc.git] / gcc / tree-ssa-propagate.c
blobe23da7094a4efb4fcb51f87e8f91360e7253fc0b
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "flags.h"
37 #include "tm_p.h"
38 #include "predict.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "basic-block.h"
45 #include "gimple-pretty-print.h"
46 #include "dumpfile.h"
47 #include "bitmap.h"
48 #include "sbitmap.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "gimple-fold.h"
52 #include "tree-eh.h"
53 #include "gimple-expr.h"
54 #include "is-a.h"
55 #include "gimple.h"
56 #include "gimplify.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-cfg.h"
60 #include "tree-phinodes.h"
61 #include "ssa-iterators.h"
62 #include "stringpool.h"
63 #include "tree-ssanames.h"
64 #include "tree-ssa.h"
65 #include "tree-ssa-propagate.h"
66 #include "langhooks.h"
67 #include "value-prof.h"
68 #include "domwalk.h"
69 #include "cfgloop.h"
70 #include "tree-cfgcleanup.h"
72 /* This file implements a generic value propagation engine based on
73 the same propagation used by the SSA-CCP algorithm [1].
75 Propagation is performed by simulating the execution of every
76 statement that produces the value being propagated. Simulation
77 proceeds as follows:
79 1- Initially, all edges of the CFG are marked not executable and
80 the CFG worklist is seeded with all the statements in the entry
81 basic block (block 0).
83 2- Every statement S is simulated with a call to the call-back
84 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
85 results:
87 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
88 interest and does not affect any of the work lists.
90 SSA_PROP_VARYING: The value produced by S cannot be determined
91 at compile time. Further simulation of S is not required.
92 If S is a conditional jump, all the outgoing edges for the
93 block are considered executable and added to the work
94 list.
96 SSA_PROP_INTERESTING: S produces a value that can be computed
97 at compile time. Its result can be propagated into the
98 statements that feed from S. Furthermore, if S is a
99 conditional jump, only the edge known to be taken is added
100 to the work list. Edges that are known not to execute are
101 never simulated.
103 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
104 return value from SSA_PROP_VISIT_PHI has the same semantics as
105 described in #2.
107 4- Three work lists are kept. Statements are only added to these
108 lists if they produce one of SSA_PROP_INTERESTING or
109 SSA_PROP_VARYING.
111 CFG_BLOCKS contains the list of blocks to be simulated.
112 Blocks are added to this list if their incoming edges are
113 found executable.
115 VARYING_SSA_EDGES contains the list of statements that feed
116 from statements that produce an SSA_PROP_VARYING result.
117 These are simulated first to speed up processing.
119 INTERESTING_SSA_EDGES contains the list of statements that
120 feed from statements that produce an SSA_PROP_INTERESTING
121 result.
123 5- Simulation terminates when all three work lists are drained.
125 Before calling ssa_propagate, it is important to clear
126 prop_simulate_again_p for all the statements in the program that
127 should be simulated. This initialization allows an implementation
128 to specify which statements should never be simulated.
130 It is also important to compute def-use information before calling
131 ssa_propagate.
133 References:
135 [1] Constant propagation with conditional branches,
136 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
138 [2] Building an Optimizing Compiler,
139 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
141 [3] Advanced Compiler Design and Implementation,
142 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
144 /* Function pointers used to parameterize the propagation engine. */
145 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
146 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
148 /* Keep track of statements that have been added to one of the SSA
149 edges worklists. This flag is used to avoid visiting statements
150 unnecessarily when draining an SSA edge worklist. If while
151 simulating a basic block, we find a statement with
152 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
153 processing from visiting it again.
155 NOTE: users of the propagation engine are not allowed to use
156 the GF_PLF_1 flag. */
157 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
159 /* A bitmap to keep track of executable blocks in the CFG. */
160 static sbitmap executable_blocks;
162 /* Array of control flow edges on the worklist. */
163 static vec<basic_block> cfg_blocks;
165 static unsigned int cfg_blocks_num = 0;
166 static int cfg_blocks_tail;
167 static int cfg_blocks_head;
169 static sbitmap bb_in_list;
171 /* Worklist of SSA edges which will need reexamination as their
172 definition has changed. SSA edges are def-use edges in the SSA
173 web. For each D-U edge, we store the target statement or PHI node
174 U. */
175 static vec<gimple> interesting_ssa_edges;
177 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
178 list of SSA edges is split into two. One contains all SSA edges
179 who need to be reexamined because their lattice value changed to
180 varying (this worklist), and the other contains all other SSA edges
181 to be reexamined (INTERESTING_SSA_EDGES).
183 Since most values in the program are VARYING, the ideal situation
184 is to move them to that lattice value as quickly as possible.
185 Thus, it doesn't make sense to process any other type of lattice
186 value until all VARYING values are propagated fully, which is one
187 thing using the VARYING worklist achieves. In addition, if we
188 don't use a separate worklist for VARYING edges, we end up with
189 situations where lattice values move from
190 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
191 static vec<gimple> varying_ssa_edges;
194 /* Return true if the block worklist empty. */
196 static inline bool
197 cfg_blocks_empty_p (void)
199 return (cfg_blocks_num == 0);
203 /* Add a basic block to the worklist. The block must not be already
204 in the worklist, and it must not be the ENTRY or EXIT block. */
206 static void
207 cfg_blocks_add (basic_block bb)
209 bool head = false;
211 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
212 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
213 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
215 if (cfg_blocks_empty_p ())
217 cfg_blocks_tail = cfg_blocks_head = 0;
218 cfg_blocks_num = 1;
220 else
222 cfg_blocks_num++;
223 if (cfg_blocks_num > cfg_blocks.length ())
225 /* We have to grow the array now. Adjust to queue to occupy
226 the full space of the original array. We do not need to
227 initialize the newly allocated portion of the array
228 because we keep track of CFG_BLOCKS_HEAD and
229 CFG_BLOCKS_HEAD. */
230 cfg_blocks_tail = cfg_blocks.length ();
231 cfg_blocks_head = 0;
232 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
234 /* Minor optimization: we prefer to see blocks with more
235 predecessors later, because there is more of a chance that
236 the incoming edges will be executable. */
237 else if (EDGE_COUNT (bb->preds)
238 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
239 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
240 else
242 if (cfg_blocks_head == 0)
243 cfg_blocks_head = cfg_blocks.length ();
244 --cfg_blocks_head;
245 head = true;
249 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
250 bitmap_set_bit (bb_in_list, bb->index);
254 /* Remove a block from the worklist. */
256 static basic_block
257 cfg_blocks_get (void)
259 basic_block bb;
261 bb = cfg_blocks[cfg_blocks_head];
263 gcc_assert (!cfg_blocks_empty_p ());
264 gcc_assert (bb);
266 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
267 --cfg_blocks_num;
268 bitmap_clear_bit (bb_in_list, bb->index);
270 return bb;
274 /* We have just defined a new value for VAR. If IS_VARYING is true,
275 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
276 them to INTERESTING_SSA_EDGES. */
278 static void
279 add_ssa_edge (tree var, bool is_varying)
281 imm_use_iterator iter;
282 use_operand_p use_p;
284 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
286 gimple use_stmt = USE_STMT (use_p);
288 if (prop_simulate_again_p (use_stmt)
289 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
291 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
292 if (is_varying)
293 varying_ssa_edges.safe_push (use_stmt);
294 else
295 interesting_ssa_edges.safe_push (use_stmt);
301 /* Add edge E to the control flow worklist. */
303 static void
304 add_control_edge (edge e)
306 basic_block bb = e->dest;
307 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
308 return;
310 /* If the edge had already been executed, skip it. */
311 if (e->flags & EDGE_EXECUTABLE)
312 return;
314 e->flags |= EDGE_EXECUTABLE;
316 /* If the block is already in the list, we're done. */
317 if (bitmap_bit_p (bb_in_list, bb->index))
318 return;
320 cfg_blocks_add (bb);
322 if (dump_file && (dump_flags & TDF_DETAILS))
323 fprintf (dump_file, "\nAdding Destination of edge (%d -> %d) to worklist\n",
324 e->src->index, e->dest->index);
328 /* Simulate the execution of STMT and update the work lists accordingly. */
330 static void
331 simulate_stmt (gimple stmt)
333 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
334 edge taken_edge = NULL;
335 tree output_name = NULL_TREE;
337 /* Don't bother visiting statements that are already
338 considered varying by the propagator. */
339 if (!prop_simulate_again_p (stmt))
340 return;
342 if (gimple_code (stmt) == GIMPLE_PHI)
344 val = ssa_prop_visit_phi (as_a <gphi *> (stmt));
345 output_name = gimple_phi_result (stmt);
347 else
348 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
350 if (val == SSA_PROP_VARYING)
352 prop_set_simulate_again (stmt, false);
354 /* If the statement produced a new varying value, add the SSA
355 edges coming out of OUTPUT_NAME. */
356 if (output_name)
357 add_ssa_edge (output_name, true);
359 /* If STMT transfers control out of its basic block, add
360 all outgoing edges to the work list. */
361 if (stmt_ends_bb_p (stmt))
363 edge e;
364 edge_iterator ei;
365 basic_block bb = gimple_bb (stmt);
366 FOR_EACH_EDGE (e, ei, bb->succs)
367 add_control_edge (e);
369 return;
371 else if (val == SSA_PROP_INTERESTING)
373 /* If the statement produced new value, add the SSA edges coming
374 out of OUTPUT_NAME. */
375 if (output_name)
376 add_ssa_edge (output_name, false);
378 /* If we know which edge is going to be taken out of this block,
379 add it to the CFG work list. */
380 if (taken_edge)
381 add_control_edge (taken_edge);
384 /* If there are no SSA uses on the stmt whose defs are simulated
385 again then this stmt will be never visited again. */
386 bool has_simulate_again_uses = false;
387 use_operand_p use_p;
388 ssa_op_iter iter;
389 if (gimple_code (stmt) == GIMPLE_PHI)
391 edge_iterator ei;
392 edge e;
393 tree arg;
394 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
395 if (!(e->flags & EDGE_EXECUTABLE)
396 || ((arg = PHI_ARG_DEF_FROM_EDGE (stmt, e))
397 && TREE_CODE (arg) == SSA_NAME
398 && !SSA_NAME_IS_DEFAULT_DEF (arg)
399 && prop_simulate_again_p (SSA_NAME_DEF_STMT (arg))))
401 has_simulate_again_uses = true;
402 break;
405 else
406 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
408 gimple def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
409 if (!gimple_nop_p (def_stmt)
410 && prop_simulate_again_p (def_stmt))
412 has_simulate_again_uses = true;
413 break;
416 if (!has_simulate_again_uses)
418 if (dump_file && (dump_flags & TDF_DETAILS))
419 fprintf (dump_file, "marking stmt to be not simulated again\n");
420 prop_set_simulate_again (stmt, false);
424 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
425 drain. This pops statements off the given WORKLIST and processes
426 them until there are no more statements on WORKLIST.
427 We take a pointer to WORKLIST because it may be reallocated when an
428 SSA edge is added to it in simulate_stmt. */
430 static void
431 process_ssa_edge_worklist (vec<gimple> *worklist)
433 /* Drain the entire worklist. */
434 while (worklist->length () > 0)
436 basic_block bb;
438 /* Pull the statement to simulate off the worklist. */
439 gimple stmt = worklist->pop ();
441 /* If this statement was already visited by simulate_block, then
442 we don't need to visit it again here. */
443 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
444 continue;
446 /* STMT is no longer in a worklist. */
447 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
449 if (dump_file && (dump_flags & TDF_DETAILS))
451 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
452 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
455 bb = gimple_bb (stmt);
457 /* PHI nodes are always visited, regardless of whether or not
458 the destination block is executable. Otherwise, visit the
459 statement only if its block is marked executable. */
460 if (gimple_code (stmt) == GIMPLE_PHI
461 || bitmap_bit_p (executable_blocks, bb->index))
462 simulate_stmt (stmt);
467 /* Simulate the execution of BLOCK. Evaluate the statement associated
468 with each variable reference inside the block. */
470 static void
471 simulate_block (basic_block block)
473 gimple_stmt_iterator gsi;
475 /* There is nothing to do for the exit block. */
476 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
477 return;
479 if (dump_file && (dump_flags & TDF_DETAILS))
480 fprintf (dump_file, "\nSimulating block %d\n", block->index);
482 /* Always simulate PHI nodes, even if we have simulated this block
483 before. */
484 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
485 simulate_stmt (gsi_stmt (gsi));
487 /* If this is the first time we've simulated this block, then we
488 must simulate each of its statements. */
489 if (!bitmap_bit_p (executable_blocks, block->index))
491 gimple_stmt_iterator j;
492 unsigned int normal_edge_count;
493 edge e, normal_edge;
494 edge_iterator ei;
496 /* Note that we have simulated this block. */
497 bitmap_set_bit (executable_blocks, block->index);
499 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
501 gimple stmt = gsi_stmt (j);
503 /* If this statement is already in the worklist then
504 "cancel" it. The reevaluation implied by the worklist
505 entry will produce the same value we generate here and
506 thus reevaluating it again from the worklist is
507 pointless. */
508 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
509 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
511 simulate_stmt (stmt);
514 /* We can not predict when abnormal and EH edges will be executed, so
515 once a block is considered executable, we consider any
516 outgoing abnormal edges as executable.
518 TODO: This is not exactly true. Simplifying statement might
519 prove it non-throwing and also computed goto can be handled
520 when destination is known.
522 At the same time, if this block has only one successor that is
523 reached by non-abnormal edges, then add that successor to the
524 worklist. */
525 normal_edge_count = 0;
526 normal_edge = NULL;
527 FOR_EACH_EDGE (e, ei, block->succs)
529 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
530 add_control_edge (e);
531 else
533 normal_edge_count++;
534 normal_edge = e;
538 if (normal_edge_count == 1)
539 add_control_edge (normal_edge);
544 /* Initialize local data structures and work lists. */
546 static void
547 ssa_prop_init (void)
549 edge e;
550 edge_iterator ei;
551 basic_block bb;
553 /* Worklists of SSA edges. */
554 interesting_ssa_edges.create (20);
555 varying_ssa_edges.create (20);
557 executable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
558 bitmap_clear (executable_blocks);
560 bb_in_list = sbitmap_alloc (last_basic_block_for_fn (cfun));
561 bitmap_clear (bb_in_list);
563 if (dump_file && (dump_flags & TDF_DETAILS))
564 dump_immediate_uses (dump_file);
566 cfg_blocks.create (20);
567 cfg_blocks.safe_grow_cleared (20);
569 /* Initially assume that every edge in the CFG is not executable.
570 (including the edges coming out of the entry block). */
571 FOR_ALL_BB_FN (bb, cfun)
573 gimple_stmt_iterator si;
575 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
576 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
578 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
579 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
581 FOR_EACH_EDGE (e, ei, bb->succs)
582 e->flags &= ~EDGE_EXECUTABLE;
585 /* Seed the algorithm by adding the successors of the entry block to the
586 edge worklist. */
587 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
588 add_control_edge (e);
592 /* Free allocated storage. */
594 static void
595 ssa_prop_fini (void)
597 interesting_ssa_edges.release ();
598 varying_ssa_edges.release ();
599 cfg_blocks.release ();
600 sbitmap_free (bb_in_list);
601 sbitmap_free (executable_blocks);
605 /* Return true if EXPR is an acceptable right-hand-side for a
606 GIMPLE assignment. We validate the entire tree, not just
607 the root node, thus catching expressions that embed complex
608 operands that are not permitted in GIMPLE. This function
609 is needed because the folding routines in fold-const.c
610 may return such expressions in some cases, e.g., an array
611 access with an embedded index addition. It may make more
612 sense to have folding routines that are sensitive to the
613 constraints on GIMPLE operands, rather than abandoning any
614 any attempt to fold if the usual folding turns out to be too
615 aggressive. */
617 bool
618 valid_gimple_rhs_p (tree expr)
620 enum tree_code code = TREE_CODE (expr);
622 switch (TREE_CODE_CLASS (code))
624 case tcc_declaration:
625 if (!is_gimple_variable (expr))
626 return false;
627 break;
629 case tcc_constant:
630 /* All constants are ok. */
631 break;
633 case tcc_comparison:
634 /* GENERIC allows comparisons with non-boolean types, reject
635 those for GIMPLE. Let vector-typed comparisons pass - rules
636 for GENERIC and GIMPLE are the same here. */
637 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
638 && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
639 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
640 && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
641 return false;
643 /* Fallthru. */
644 case tcc_binary:
645 if (!is_gimple_val (TREE_OPERAND (expr, 0))
646 || !is_gimple_val (TREE_OPERAND (expr, 1)))
647 return false;
648 break;
650 case tcc_unary:
651 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
652 return false;
653 break;
655 case tcc_expression:
656 switch (code)
658 case ADDR_EXPR:
660 tree t;
661 if (is_gimple_min_invariant (expr))
662 return true;
663 t = TREE_OPERAND (expr, 0);
664 while (handled_component_p (t))
666 /* ??? More checks needed, see the GIMPLE verifier. */
667 if ((TREE_CODE (t) == ARRAY_REF
668 || TREE_CODE (t) == ARRAY_RANGE_REF)
669 && !is_gimple_val (TREE_OPERAND (t, 1)))
670 return false;
671 t = TREE_OPERAND (t, 0);
673 if (!is_gimple_id (t))
674 return false;
676 break;
678 default:
679 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
681 if (((code == VEC_COND_EXPR || code == COND_EXPR)
682 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
683 : !is_gimple_val (TREE_OPERAND (expr, 0)))
684 || !is_gimple_val (TREE_OPERAND (expr, 1))
685 || !is_gimple_val (TREE_OPERAND (expr, 2)))
686 return false;
687 break;
689 return false;
691 break;
693 case tcc_vl_exp:
694 return false;
696 case tcc_exceptional:
697 if (code == CONSTRUCTOR)
699 unsigned i;
700 tree elt;
701 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
702 if (!is_gimple_val (elt))
703 return false;
704 return true;
706 if (code != SSA_NAME)
707 return false;
708 break;
710 case tcc_reference:
711 if (code == BIT_FIELD_REF)
712 return is_gimple_val (TREE_OPERAND (expr, 0));
713 return false;
715 default:
716 return false;
719 return true;
723 /* Return true if EXPR is a CALL_EXPR suitable for representation
724 as a single GIMPLE_CALL statement. If the arguments require
725 further gimplification, return false. */
727 static bool
728 valid_gimple_call_p (tree expr)
730 unsigned i, nargs;
732 if (TREE_CODE (expr) != CALL_EXPR)
733 return false;
735 nargs = call_expr_nargs (expr);
736 for (i = 0; i < nargs; i++)
738 tree arg = CALL_EXPR_ARG (expr, i);
739 if (is_gimple_reg_type (TREE_TYPE (arg)))
741 if (!is_gimple_val (arg))
742 return false;
744 else
745 if (!is_gimple_lvalue (arg))
746 return false;
749 return true;
753 /* Make SSA names defined by OLD_STMT point to NEW_STMT
754 as their defining statement. */
756 void
757 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
759 tree var;
760 ssa_op_iter iter;
762 if (gimple_in_ssa_p (cfun))
764 /* Make defined SSA_NAMEs point to the new
765 statement as their definition. */
766 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
768 if (TREE_CODE (var) == SSA_NAME)
769 SSA_NAME_DEF_STMT (var) = new_stmt;
774 /* Helper function for update_gimple_call and update_call_from_tree.
775 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
777 static void
778 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
779 gimple stmt)
781 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
782 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
783 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
784 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
785 gimple_set_location (new_stmt, gimple_location (stmt));
786 if (gimple_block (new_stmt) == NULL_TREE)
787 gimple_set_block (new_stmt, gimple_block (stmt));
788 gsi_replace (si_p, new_stmt, false);
791 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
792 with number of arguments NARGS, where the arguments in GIMPLE form
793 follow NARGS argument. */
795 bool
796 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
798 va_list ap;
799 gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
801 gcc_assert (is_gimple_call (stmt));
802 va_start (ap, nargs);
803 new_stmt = gimple_build_call_valist (fn, nargs, ap);
804 finish_update_gimple_call (si_p, new_stmt, stmt);
805 va_end (ap);
806 return true;
809 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
810 value of EXPR, which is expected to be the result of folding the
811 call. This can only be done if EXPR is a CALL_EXPR with valid
812 GIMPLE operands as arguments, or if it is a suitable RHS expression
813 for a GIMPLE_ASSIGN. More complex expressions will require
814 gimplification, which will introduce additional statements. In this
815 event, no update is performed, and the function returns false.
816 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
817 replace the statement at *SI_P with an entirely new statement.
818 The new statement need not be a call, e.g., if the original call
819 folded to a constant. */
821 bool
822 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
824 gimple stmt = gsi_stmt (*si_p);
826 if (valid_gimple_call_p (expr))
828 /* The call has simplified to another call. */
829 tree fn = CALL_EXPR_FN (expr);
830 unsigned i;
831 unsigned nargs = call_expr_nargs (expr);
832 vec<tree> args = vNULL;
833 gcall *new_stmt;
835 if (nargs > 0)
837 args.create (nargs);
838 args.safe_grow_cleared (nargs);
840 for (i = 0; i < nargs; i++)
841 args[i] = CALL_EXPR_ARG (expr, i);
844 new_stmt = gimple_build_call_vec (fn, args);
845 finish_update_gimple_call (si_p, new_stmt, stmt);
846 args.release ();
848 return true;
850 else if (valid_gimple_rhs_p (expr))
852 tree lhs = gimple_call_lhs (stmt);
853 gimple new_stmt;
855 /* The call has simplified to an expression
856 that cannot be represented as a GIMPLE_CALL. */
857 if (lhs)
859 /* A value is expected.
860 Introduce a new GIMPLE_ASSIGN statement. */
861 STRIP_USELESS_TYPE_CONVERSION (expr);
862 new_stmt = gimple_build_assign (lhs, expr);
863 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
864 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
865 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
867 else if (!TREE_SIDE_EFFECTS (expr))
869 /* No value is expected, and EXPR has no effect.
870 Replace it with an empty statement. */
871 new_stmt = gimple_build_nop ();
872 if (gimple_in_ssa_p (cfun))
874 unlink_stmt_vdef (stmt);
875 release_defs (stmt);
878 else
880 /* No value is expected, but EXPR has an effect,
881 e.g., it could be a reference to a volatile
882 variable. Create an assignment statement
883 with a dummy (unused) lhs variable. */
884 STRIP_USELESS_TYPE_CONVERSION (expr);
885 if (gimple_in_ssa_p (cfun))
886 lhs = make_ssa_name (TREE_TYPE (expr));
887 else
888 lhs = create_tmp_var (TREE_TYPE (expr));
889 new_stmt = gimple_build_assign (lhs, expr);
890 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
891 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
892 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
894 gimple_set_location (new_stmt, gimple_location (stmt));
895 gsi_replace (si_p, new_stmt, false);
896 return true;
898 else
899 /* The call simplified to an expression that is
900 not a valid GIMPLE RHS. */
901 return false;
905 /* Entry point to the propagation engine.
907 VISIT_STMT is called for every statement visited.
908 VISIT_PHI is called for every PHI node visited. */
910 void
911 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
912 ssa_prop_visit_phi_fn visit_phi)
914 ssa_prop_visit_stmt = visit_stmt;
915 ssa_prop_visit_phi = visit_phi;
917 ssa_prop_init ();
919 /* Iterate until the worklists are empty. */
920 while (!cfg_blocks_empty_p ()
921 || interesting_ssa_edges.length () > 0
922 || varying_ssa_edges.length () > 0)
924 if (!cfg_blocks_empty_p ())
926 /* Pull the next block to simulate off the worklist. */
927 basic_block dest_block = cfg_blocks_get ();
928 simulate_block (dest_block);
931 /* In order to move things to varying as quickly as
932 possible,process the VARYING_SSA_EDGES worklist first. */
933 process_ssa_edge_worklist (&varying_ssa_edges);
935 /* Now process the INTERESTING_SSA_EDGES worklist. */
936 process_ssa_edge_worklist (&interesting_ssa_edges);
939 ssa_prop_fini ();
943 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
944 is a non-volatile pointer dereference, a structure reference or a
945 reference to a single _DECL. Ignore volatile memory references
946 because they are not interesting for the optimizers. */
948 bool
949 stmt_makes_single_store (gimple stmt)
951 tree lhs;
953 if (gimple_code (stmt) != GIMPLE_ASSIGN
954 && gimple_code (stmt) != GIMPLE_CALL)
955 return false;
957 if (!gimple_vdef (stmt))
958 return false;
960 lhs = gimple_get_lhs (stmt);
962 /* A call statement may have a null LHS. */
963 if (!lhs)
964 return false;
966 return (!TREE_THIS_VOLATILE (lhs)
967 && (DECL_P (lhs)
968 || REFERENCE_CLASS_P (lhs)));
972 /* Propagation statistics. */
973 struct prop_stats_d
975 long num_const_prop;
976 long num_copy_prop;
977 long num_stmts_folded;
978 long num_dce;
981 static struct prop_stats_d prop_stats;
983 /* Replace USE references in statement STMT with the values stored in
984 PROP_VALUE. Return true if at least one reference was replaced. */
986 static bool
987 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
989 bool replaced = false;
990 use_operand_p use;
991 ssa_op_iter iter;
993 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
995 tree tuse = USE_FROM_PTR (use);
996 tree val = (*get_value) (tuse);
998 if (val == tuse || val == NULL_TREE)
999 continue;
1001 if (gimple_code (stmt) == GIMPLE_ASM
1002 && !may_propagate_copy_into_asm (tuse))
1003 continue;
1005 if (!may_propagate_copy (tuse, val))
1006 continue;
1008 if (TREE_CODE (val) != SSA_NAME)
1009 prop_stats.num_const_prop++;
1010 else
1011 prop_stats.num_copy_prop++;
1013 propagate_value (use, val);
1015 replaced = true;
1018 return replaced;
1022 /* Replace propagated values into all the arguments for PHI using the
1023 values from PROP_VALUE. */
1025 static bool
1026 replace_phi_args_in (gphi *phi, ssa_prop_get_value_fn get_value)
1028 size_t i;
1029 bool replaced = false;
1031 if (dump_file && (dump_flags & TDF_DETAILS))
1033 fprintf (dump_file, "Folding PHI node: ");
1034 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1037 basic_block bb = gimple_bb (phi);
1038 for (i = 0; i < gimple_phi_num_args (phi); i++)
1040 tree arg = gimple_phi_arg_def (phi, i);
1042 if (TREE_CODE (arg) == SSA_NAME)
1044 tree val = (*get_value) (arg);
1046 if (val && val != arg && may_propagate_copy (arg, val))
1048 edge e = gimple_phi_arg_edge (phi, i);
1050 /* Avoid propagating constants into loop latch edge
1051 PHI arguments as this makes coalescing the copy
1052 across this edge impossible. If the argument is
1053 defined by an assert - otherwise the stmt will
1054 get removed without replacing its uses. */
1055 if (TREE_CODE (val) != SSA_NAME
1056 && bb->loop_father->header == bb
1057 && dominated_by_p (CDI_DOMINATORS, e->src, bb)
1058 && is_gimple_assign (SSA_NAME_DEF_STMT (arg))
1059 && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (arg))
1060 == ASSERT_EXPR))
1061 continue;
1063 if (TREE_CODE (val) != SSA_NAME)
1064 prop_stats.num_const_prop++;
1065 else
1066 prop_stats.num_copy_prop++;
1068 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
1069 replaced = true;
1071 /* If we propagated a copy and this argument flows
1072 through an abnormal edge, update the replacement
1073 accordingly. */
1074 if (TREE_CODE (val) == SSA_NAME
1075 && e->flags & EDGE_ABNORMAL
1076 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1078 /* This can only occur for virtual operands, since
1079 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1080 would prevent replacement. */
1081 gcc_checking_assert (virtual_operand_p (val));
1082 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1090 if (!replaced)
1091 fprintf (dump_file, "No folding possible\n");
1092 else
1094 fprintf (dump_file, "Folded into: ");
1095 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1096 fprintf (dump_file, "\n");
1100 return replaced;
1104 class substitute_and_fold_dom_walker : public dom_walker
1106 public:
1107 substitute_and_fold_dom_walker (cdi_direction direction,
1108 ssa_prop_get_value_fn get_value_fn_,
1109 ssa_prop_fold_stmt_fn fold_fn_,
1110 bool do_dce_)
1111 : dom_walker (direction), get_value_fn (get_value_fn_),
1112 fold_fn (fold_fn_), do_dce (do_dce_), something_changed (false)
1114 stmts_to_remove.create (0);
1115 stmts_to_fixup.create (0);
1116 need_eh_cleanup = BITMAP_ALLOC (NULL);
1118 ~substitute_and_fold_dom_walker ()
1120 stmts_to_remove.release ();
1121 stmts_to_fixup.release ();
1122 BITMAP_FREE (need_eh_cleanup);
1125 virtual void before_dom_children (basic_block);
1126 virtual void after_dom_children (basic_block) {}
1128 ssa_prop_get_value_fn get_value_fn;
1129 ssa_prop_fold_stmt_fn fold_fn;
1130 bool do_dce;
1131 bool something_changed;
1132 vec<gimple> stmts_to_remove;
1133 vec<gimple> stmts_to_fixup;
1134 bitmap need_eh_cleanup;
1137 void
1138 substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
1140 /* Propagate known values into PHI nodes. */
1141 for (gphi_iterator i = gsi_start_phis (bb);
1142 !gsi_end_p (i);
1143 gsi_next (&i))
1145 gphi *phi = i.phi ();
1146 tree res = gimple_phi_result (phi);
1147 if (virtual_operand_p (res))
1148 continue;
1149 if (do_dce
1150 && res && TREE_CODE (res) == SSA_NAME)
1152 tree sprime = get_value_fn (res);
1153 if (sprime
1154 && sprime != res
1155 && may_propagate_copy (res, sprime))
1157 stmts_to_remove.safe_push (phi);
1158 continue;
1161 something_changed |= replace_phi_args_in (phi, get_value_fn);
1164 /* Propagate known values into stmts. In some case it exposes
1165 more trivially deletable stmts to walk backward. */
1166 for (gimple_stmt_iterator i = gsi_start_bb (bb);
1167 !gsi_end_p (i);
1168 gsi_next (&i))
1170 bool did_replace;
1171 gimple stmt = gsi_stmt (i);
1172 enum gimple_code code = gimple_code (stmt);
1174 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1175 range information for names and they are discarded
1176 afterwards. */
1178 if (code == GIMPLE_ASSIGN
1179 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1180 continue;
1182 /* No point propagating into a stmt we have a value for we
1183 can propagate into all uses. Mark it for removal instead. */
1184 tree lhs = gimple_get_lhs (stmt);
1185 if (do_dce
1186 && lhs && TREE_CODE (lhs) == SSA_NAME)
1188 tree sprime = get_value_fn (lhs);
1189 if (sprime
1190 && sprime != lhs
1191 && may_propagate_copy (lhs, sprime)
1192 && !stmt_could_throw_p (stmt)
1193 && !gimple_has_side_effects (stmt))
1195 stmts_to_remove.safe_push (stmt);
1196 continue;
1200 /* Replace the statement with its folded version and mark it
1201 folded. */
1202 did_replace = false;
1203 if (dump_file && (dump_flags & TDF_DETAILS))
1205 fprintf (dump_file, "Folding statement: ");
1206 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1209 gimple old_stmt = stmt;
1210 bool was_noreturn = (is_gimple_call (stmt)
1211 && gimple_call_noreturn_p (stmt));
1213 /* Some statements may be simplified using propagator
1214 specific information. Do this before propagating
1215 into the stmt to not disturb pass specific information. */
1216 if (fold_fn
1217 && (*fold_fn)(&i))
1219 did_replace = true;
1220 prop_stats.num_stmts_folded++;
1221 stmt = gsi_stmt (i);
1222 update_stmt (stmt);
1225 /* Replace real uses in the statement. */
1226 did_replace |= replace_uses_in (stmt, get_value_fn);
1228 /* If we made a replacement, fold the statement. */
1229 if (did_replace)
1230 fold_stmt (&i, follow_single_use_edges);
1232 /* Now cleanup. */
1233 if (did_replace)
1235 stmt = gsi_stmt (i);
1237 /* If we cleaned up EH information from the statement,
1238 remove EH edges. */
1239 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1240 bitmap_set_bit (need_eh_cleanup, bb->index);
1242 /* If we turned a not noreturn call into a noreturn one
1243 schedule it for fixup. */
1244 if (!was_noreturn
1245 && is_gimple_call (stmt)
1246 && gimple_call_noreturn_p (stmt))
1247 stmts_to_fixup.safe_push (stmt);
1249 if (is_gimple_assign (stmt)
1250 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1251 == GIMPLE_SINGLE_RHS))
1253 tree rhs = gimple_assign_rhs1 (stmt);
1255 if (TREE_CODE (rhs) == ADDR_EXPR)
1256 recompute_tree_invariant_for_addr_expr (rhs);
1259 /* Determine what needs to be done to update the SSA form. */
1260 update_stmt (stmt);
1261 if (!is_gimple_debug (stmt))
1262 something_changed = true;
1265 if (dump_file && (dump_flags & TDF_DETAILS))
1267 if (did_replace)
1269 fprintf (dump_file, "Folded into: ");
1270 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1271 fprintf (dump_file, "\n");
1273 else
1274 fprintf (dump_file, "Not folded\n");
1281 /* Perform final substitution and folding of propagated values.
1283 PROP_VALUE[I] contains the single value that should be substituted
1284 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1285 substituted.
1287 If FOLD_FN is non-NULL the function will be invoked on all statements
1288 before propagating values for pass specific simplification.
1290 DO_DCE is true if trivially dead stmts can be removed.
1292 If DO_DCE is true, the statements within a BB are walked from
1293 last to first element. Otherwise we scan from first to last element.
1295 Return TRUE when something changed. */
1297 bool
1298 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1299 ssa_prop_fold_stmt_fn fold_fn,
1300 bool do_dce)
1302 gcc_assert (get_value_fn);
1304 if (dump_file && (dump_flags & TDF_DETAILS))
1305 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1307 memset (&prop_stats, 0, sizeof (prop_stats));
1309 calculate_dominance_info (CDI_DOMINATORS);
1310 substitute_and_fold_dom_walker walker(CDI_DOMINATORS,
1311 get_value_fn, fold_fn, do_dce);
1312 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1314 /* We cannot remove stmts during the BB walk, especially not release
1315 SSA names there as that destroys the lattice of our callers.
1316 Remove stmts in reverse order to make debug stmt creation possible. */
1317 while (!walker.stmts_to_remove.is_empty ())
1319 gimple stmt = walker.stmts_to_remove.pop ();
1320 if (dump_file && dump_flags & TDF_DETAILS)
1322 fprintf (dump_file, "Removing dead stmt ");
1323 print_gimple_stmt (dump_file, stmt, 0, 0);
1324 fprintf (dump_file, "\n");
1326 prop_stats.num_dce++;
1327 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1328 if (gimple_code (stmt) == GIMPLE_PHI)
1329 remove_phi_node (&gsi, true);
1330 else
1332 unlink_stmt_vdef (stmt);
1333 gsi_remove (&gsi, true);
1334 release_defs (stmt);
1338 if (!bitmap_empty_p (walker.need_eh_cleanup))
1339 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1341 /* Fixup stmts that became noreturn calls. This may require splitting
1342 blocks and thus isn't possible during the dominator walk. Do this
1343 in reverse order so we don't inadvertedly remove a stmt we want to
1344 fixup by visiting a dominating now noreturn call first. */
1345 while (!walker.stmts_to_fixup.is_empty ())
1347 gimple stmt = walker.stmts_to_fixup.pop ();
1348 if (dump_file && dump_flags & TDF_DETAILS)
1350 fprintf (dump_file, "Fixing up noreturn call ");
1351 print_gimple_stmt (dump_file, stmt, 0, 0);
1352 fprintf (dump_file, "\n");
1354 fixup_noreturn_call (stmt);
1357 statistics_counter_event (cfun, "Constants propagated",
1358 prop_stats.num_const_prop);
1359 statistics_counter_event (cfun, "Copies propagated",
1360 prop_stats.num_copy_prop);
1361 statistics_counter_event (cfun, "Statements folded",
1362 prop_stats.num_stmts_folded);
1363 statistics_counter_event (cfun, "Statements deleted",
1364 prop_stats.num_dce);
1366 return walker.something_changed;
1370 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1372 bool
1373 may_propagate_copy (tree dest, tree orig)
1375 tree type_d = TREE_TYPE (dest);
1376 tree type_o = TREE_TYPE (orig);
1378 /* If ORIG is a default definition which flows in from an abnormal edge
1379 then the copy can be propagated. It is important that we do so to avoid
1380 uninitialized copies. */
1381 if (TREE_CODE (orig) == SSA_NAME
1382 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1383 && SSA_NAME_IS_DEFAULT_DEF (orig)
1384 && (SSA_NAME_VAR (orig) == NULL_TREE
1385 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL))
1387 /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1388 be propagated. */
1389 else if (TREE_CODE (orig) == SSA_NAME
1390 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1391 return false;
1392 /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1393 propagated. */
1394 else if (TREE_CODE (dest) == SSA_NAME
1395 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1396 return false;
1398 /* Do not copy between types for which we *do* need a conversion. */
1399 if (!useless_type_conversion_p (type_d, type_o))
1400 return false;
1402 /* Generally propagating virtual operands is not ok as that may
1403 create overlapping life-ranges. */
1404 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1405 return false;
1407 /* Anything else is OK. */
1408 return true;
1411 /* Like may_propagate_copy, but use as the destination expression
1412 the principal expression (typically, the RHS) contained in
1413 statement DEST. This is more efficient when working with the
1414 gimple tuples representation. */
1416 bool
1417 may_propagate_copy_into_stmt (gimple dest, tree orig)
1419 tree type_d;
1420 tree type_o;
1422 /* If the statement is a switch or a single-rhs assignment,
1423 then the expression to be replaced by the propagation may
1424 be an SSA_NAME. Fortunately, there is an explicit tree
1425 for the expression, so we delegate to may_propagate_copy. */
1427 if (gimple_assign_single_p (dest))
1428 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1429 else if (gswitch *dest_swtch = dyn_cast <gswitch *> (dest))
1430 return may_propagate_copy (gimple_switch_index (dest_swtch), orig);
1432 /* In other cases, the expression is not materialized, so there
1433 is no destination to pass to may_propagate_copy. On the other
1434 hand, the expression cannot be an SSA_NAME, so the analysis
1435 is much simpler. */
1437 if (TREE_CODE (orig) == SSA_NAME
1438 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1439 return false;
1441 if (is_gimple_assign (dest))
1442 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1443 else if (gimple_code (dest) == GIMPLE_COND)
1444 type_d = boolean_type_node;
1445 else if (is_gimple_call (dest)
1446 && gimple_call_lhs (dest) != NULL_TREE)
1447 type_d = TREE_TYPE (gimple_call_lhs (dest));
1448 else
1449 gcc_unreachable ();
1451 type_o = TREE_TYPE (orig);
1453 if (!useless_type_conversion_p (type_d, type_o))
1454 return false;
1456 return true;
1459 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1461 bool
1462 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1464 return true;
1468 /* Common code for propagate_value and replace_exp.
1470 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1471 replacement is done to propagate a value or not. */
1473 static void
1474 replace_exp_1 (use_operand_p op_p, tree val,
1475 bool for_propagation ATTRIBUTE_UNUSED)
1477 #if defined ENABLE_CHECKING
1478 tree op = USE_FROM_PTR (op_p);
1480 gcc_assert (!(for_propagation
1481 && TREE_CODE (op) == SSA_NAME
1482 && TREE_CODE (val) == SSA_NAME
1483 && !may_propagate_copy (op, val)));
1484 #endif
1486 if (TREE_CODE (val) == SSA_NAME)
1487 SET_USE (op_p, val);
1488 else
1489 SET_USE (op_p, unshare_expr (val));
1493 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1494 into the operand pointed to by OP_P.
1496 Use this version for const/copy propagation as it will perform additional
1497 checks to ensure validity of the const/copy propagation. */
1499 void
1500 propagate_value (use_operand_p op_p, tree val)
1502 replace_exp_1 (op_p, val, true);
1505 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1507 Use this version when not const/copy propagating values. For example,
1508 PRE uses this version when building expressions as they would appear
1509 in specific blocks taking into account actions of PHI nodes.
1511 The statement in which an expression has been replaced should be
1512 folded using fold_stmt_inplace. */
1514 void
1515 replace_exp (use_operand_p op_p, tree val)
1517 replace_exp_1 (op_p, val, false);
1521 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1522 into the tree pointed to by OP_P.
1524 Use this version for const/copy propagation when SSA operands are not
1525 available. It will perform the additional checks to ensure validity of
1526 the const/copy propagation, but will not update any operand information.
1527 Be sure to mark the stmt as modified. */
1529 void
1530 propagate_tree_value (tree *op_p, tree val)
1532 if (TREE_CODE (val) == SSA_NAME)
1533 *op_p = val;
1534 else
1535 *op_p = unshare_expr (val);
1539 /* Like propagate_tree_value, but use as the operand to replace
1540 the principal expression (typically, the RHS) contained in the
1541 statement referenced by iterator GSI. Note that it is not
1542 always possible to update the statement in-place, so a new
1543 statement may be created to replace the original. */
1545 void
1546 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1548 gimple stmt = gsi_stmt (*gsi);
1550 if (is_gimple_assign (stmt))
1552 tree expr = NULL_TREE;
1553 if (gimple_assign_single_p (stmt))
1554 expr = gimple_assign_rhs1 (stmt);
1555 propagate_tree_value (&expr, val);
1556 gimple_assign_set_rhs_from_tree (gsi, expr);
1558 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
1560 tree lhs = NULL_TREE;
1561 tree rhs = build_zero_cst (TREE_TYPE (val));
1562 propagate_tree_value (&lhs, val);
1563 gimple_cond_set_code (cond_stmt, NE_EXPR);
1564 gimple_cond_set_lhs (cond_stmt, lhs);
1565 gimple_cond_set_rhs (cond_stmt, rhs);
1567 else if (is_gimple_call (stmt)
1568 && gimple_call_lhs (stmt) != NULL_TREE)
1570 tree expr = NULL_TREE;
1571 bool res;
1572 propagate_tree_value (&expr, val);
1573 res = update_call_from_tree (gsi, expr);
1574 gcc_assert (res);
1576 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1577 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt), val);
1578 else
1579 gcc_unreachable ();