2015-08-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / tree-ssa-propagate.c
blobb7684e07905ce24f6bd21ff3817a6ed16b63053a
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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "hard-reg-set.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "flags.h"
32 #include "tm_p.h"
33 #include "gimple-pretty-print.h"
34 #include "dumpfile.h"
35 #include "internal-fn.h"
36 #include "gimple-fold.h"
37 #include "tree-eh.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "tree-cfg.h"
41 #include "tree-ssa.h"
42 #include "tree-ssa-propagate.h"
43 #include "langhooks.h"
44 #include "value-prof.h"
45 #include "domwalk.h"
46 #include "cfgloop.h"
47 #include "tree-cfgcleanup.h"
49 /* This file implements a generic value propagation engine based on
50 the same propagation used by the SSA-CCP algorithm [1].
52 Propagation is performed by simulating the execution of every
53 statement that produces the value being propagated. Simulation
54 proceeds as follows:
56 1- Initially, all edges of the CFG are marked not executable and
57 the CFG worklist is seeded with all the statements in the entry
58 basic block (block 0).
60 2- Every statement S is simulated with a call to the call-back
61 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
62 results:
64 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
65 interest and does not affect any of the work lists.
67 SSA_PROP_VARYING: The value produced by S cannot be determined
68 at compile time. Further simulation of S is not required.
69 If S is a conditional jump, all the outgoing edges for the
70 block are considered executable and added to the work
71 list.
73 SSA_PROP_INTERESTING: S produces a value that can be computed
74 at compile time. Its result can be propagated into the
75 statements that feed from S. Furthermore, if S is a
76 conditional jump, only the edge known to be taken is added
77 to the work list. Edges that are known not to execute are
78 never simulated.
80 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
81 return value from SSA_PROP_VISIT_PHI has the same semantics as
82 described in #2.
84 4- Three work lists are kept. Statements are only added to these
85 lists if they produce one of SSA_PROP_INTERESTING or
86 SSA_PROP_VARYING.
88 CFG_BLOCKS contains the list of blocks to be simulated.
89 Blocks are added to this list if their incoming edges are
90 found executable.
92 VARYING_SSA_EDGES contains the list of statements that feed
93 from statements that produce an SSA_PROP_VARYING result.
94 These are simulated first to speed up processing.
96 INTERESTING_SSA_EDGES contains the list of statements that
97 feed from statements that produce an SSA_PROP_INTERESTING
98 result.
100 5- Simulation terminates when all three work lists are drained.
102 Before calling ssa_propagate, it is important to clear
103 prop_simulate_again_p for all the statements in the program that
104 should be simulated. This initialization allows an implementation
105 to specify which statements should never be simulated.
107 It is also important to compute def-use information before calling
108 ssa_propagate.
110 References:
112 [1] Constant propagation with conditional branches,
113 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
115 [2] Building an Optimizing Compiler,
116 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
118 [3] Advanced Compiler Design and Implementation,
119 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
121 /* Function pointers used to parameterize the propagation engine. */
122 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
123 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
125 /* Keep track of statements that have been added to one of the SSA
126 edges worklists. This flag is used to avoid visiting statements
127 unnecessarily when draining an SSA edge worklist. If while
128 simulating a basic block, we find a statement with
129 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
130 processing from visiting it again.
132 NOTE: users of the propagation engine are not allowed to use
133 the GF_PLF_1 flag. */
134 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
136 /* A bitmap to keep track of executable blocks in the CFG. */
137 static sbitmap executable_blocks;
139 /* Array of control flow edges on the worklist. */
140 static vec<basic_block> cfg_blocks;
142 static unsigned int cfg_blocks_num = 0;
143 static int cfg_blocks_tail;
144 static int cfg_blocks_head;
146 static sbitmap bb_in_list;
148 /* Worklist of SSA edges which will need reexamination as their
149 definition has changed. SSA edges are def-use edges in the SSA
150 web. For each D-U edge, we store the target statement or PHI node
151 U. */
152 static vec<gimple> interesting_ssa_edges;
154 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
155 list of SSA edges is split into two. One contains all SSA edges
156 who need to be reexamined because their lattice value changed to
157 varying (this worklist), and the other contains all other SSA edges
158 to be reexamined (INTERESTING_SSA_EDGES).
160 Since most values in the program are VARYING, the ideal situation
161 is to move them to that lattice value as quickly as possible.
162 Thus, it doesn't make sense to process any other type of lattice
163 value until all VARYING values are propagated fully, which is one
164 thing using the VARYING worklist achieves. In addition, if we
165 don't use a separate worklist for VARYING edges, we end up with
166 situations where lattice values move from
167 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
168 static vec<gimple> varying_ssa_edges;
171 /* Return true if the block worklist empty. */
173 static inline bool
174 cfg_blocks_empty_p (void)
176 return (cfg_blocks_num == 0);
180 /* Add a basic block to the worklist. The block must not be already
181 in the worklist, and it must not be the ENTRY or EXIT block. */
183 static void
184 cfg_blocks_add (basic_block bb)
186 bool head = false;
188 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
189 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
190 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
192 if (cfg_blocks_empty_p ())
194 cfg_blocks_tail = cfg_blocks_head = 0;
195 cfg_blocks_num = 1;
197 else
199 cfg_blocks_num++;
200 if (cfg_blocks_num > cfg_blocks.length ())
202 /* We have to grow the array now. Adjust to queue to occupy
203 the full space of the original array. We do not need to
204 initialize the newly allocated portion of the array
205 because we keep track of CFG_BLOCKS_HEAD and
206 CFG_BLOCKS_HEAD. */
207 cfg_blocks_tail = cfg_blocks.length ();
208 cfg_blocks_head = 0;
209 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
211 /* Minor optimization: we prefer to see blocks with more
212 predecessors later, because there is more of a chance that
213 the incoming edges will be executable. */
214 else if (EDGE_COUNT (bb->preds)
215 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
216 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
217 else
219 if (cfg_blocks_head == 0)
220 cfg_blocks_head = cfg_blocks.length ();
221 --cfg_blocks_head;
222 head = true;
226 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
227 bitmap_set_bit (bb_in_list, bb->index);
231 /* Remove a block from the worklist. */
233 static basic_block
234 cfg_blocks_get (void)
236 basic_block bb;
238 bb = cfg_blocks[cfg_blocks_head];
240 gcc_assert (!cfg_blocks_empty_p ());
241 gcc_assert (bb);
243 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
244 --cfg_blocks_num;
245 bitmap_clear_bit (bb_in_list, bb->index);
247 return bb;
251 /* We have just defined a new value for VAR. If IS_VARYING is true,
252 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
253 them to INTERESTING_SSA_EDGES. */
255 static void
256 add_ssa_edge (tree var, bool is_varying)
258 imm_use_iterator iter;
259 use_operand_p use_p;
261 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
263 gimple use_stmt = USE_STMT (use_p);
265 if (prop_simulate_again_p (use_stmt)
266 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
268 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
269 if (is_varying)
271 if (dump_file && (dump_flags & TDF_DETAILS))
273 fprintf (dump_file, "varying_ssa_edges: adding SSA use in ");
274 print_gimple_stmt (dump_file, use_stmt, 0, TDF_SLIM);
276 varying_ssa_edges.safe_push (use_stmt);
278 else
280 if (dump_file && (dump_flags & TDF_DETAILS))
282 fprintf (dump_file, "interesting_ssa_edges: adding SSA use in ");
283 print_gimple_stmt (dump_file, use_stmt, 0, TDF_SLIM);
285 interesting_ssa_edges.safe_push (use_stmt);
292 /* Add edge E to the control flow worklist. */
294 static void
295 add_control_edge (edge e)
297 basic_block bb = e->dest;
298 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
299 return;
301 /* If the edge had already been executed, skip it. */
302 if (e->flags & EDGE_EXECUTABLE)
303 return;
305 e->flags |= EDGE_EXECUTABLE;
307 /* If the block is already in the list, we're done. */
308 if (bitmap_bit_p (bb_in_list, bb->index))
309 return;
311 cfg_blocks_add (bb);
313 if (dump_file && (dump_flags & TDF_DETAILS))
314 fprintf (dump_file, "Adding destination of edge (%d -> %d) to worklist\n",
315 e->src->index, e->dest->index);
319 /* Simulate the execution of STMT and update the work lists accordingly. */
321 static void
322 simulate_stmt (gimple stmt)
324 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
325 edge taken_edge = NULL;
326 tree output_name = NULL_TREE;
328 /* Don't bother visiting statements that are already
329 considered varying by the propagator. */
330 if (!prop_simulate_again_p (stmt))
331 return;
333 if (gimple_code (stmt) == GIMPLE_PHI)
335 val = ssa_prop_visit_phi (as_a <gphi *> (stmt));
336 output_name = gimple_phi_result (stmt);
338 else
339 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
341 if (val == SSA_PROP_VARYING)
343 prop_set_simulate_again (stmt, false);
345 /* If the statement produced a new varying value, add the SSA
346 edges coming out of OUTPUT_NAME. */
347 if (output_name)
348 add_ssa_edge (output_name, true);
350 /* If STMT transfers control out of its basic block, add
351 all outgoing edges to the work list. */
352 if (stmt_ends_bb_p (stmt))
354 edge e;
355 edge_iterator ei;
356 basic_block bb = gimple_bb (stmt);
357 FOR_EACH_EDGE (e, ei, bb->succs)
358 add_control_edge (e);
360 return;
362 else if (val == SSA_PROP_INTERESTING)
364 /* If the statement produced new value, add the SSA edges coming
365 out of OUTPUT_NAME. */
366 if (output_name)
367 add_ssa_edge (output_name, false);
369 /* If we know which edge is going to be taken out of this block,
370 add it to the CFG work list. */
371 if (taken_edge)
372 add_control_edge (taken_edge);
375 /* If there are no SSA uses on the stmt whose defs are simulated
376 again then this stmt will be never visited again. */
377 bool has_simulate_again_uses = false;
378 use_operand_p use_p;
379 ssa_op_iter iter;
380 if (gimple_code (stmt) == GIMPLE_PHI)
382 edge_iterator ei;
383 edge e;
384 tree arg;
385 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
386 if (!(e->flags & EDGE_EXECUTABLE)
387 || ((arg = PHI_ARG_DEF_FROM_EDGE (stmt, e))
388 && TREE_CODE (arg) == SSA_NAME
389 && !SSA_NAME_IS_DEFAULT_DEF (arg)
390 && prop_simulate_again_p (SSA_NAME_DEF_STMT (arg))))
392 has_simulate_again_uses = true;
393 break;
396 else
397 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
399 gimple def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
400 if (!gimple_nop_p (def_stmt)
401 && prop_simulate_again_p (def_stmt))
403 has_simulate_again_uses = true;
404 break;
407 if (!has_simulate_again_uses)
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, "marking stmt to be not simulated again\n");
411 prop_set_simulate_again (stmt, false);
415 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
416 drain. This pops statements off the given WORKLIST and processes
417 them until one statement was simulated or there are no more statements
418 on WORKLIST. We take a pointer to WORKLIST because it may be reallocated
419 when an SSA edge is added to it in simulate_stmt. Return true if a stmt
420 was simulated. */
422 static bool
423 process_ssa_edge_worklist (vec<gimple> *worklist, const char *edge_list_name)
425 /* Process the next entry from the worklist. */
426 while (worklist->length () > 0)
428 basic_block bb;
430 /* Pull the statement to simulate off the worklist. */
431 gimple stmt = worklist->pop ();
433 /* If this statement was already visited by simulate_block, then
434 we don't need to visit it again here. */
435 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
436 continue;
438 /* STMT is no longer in a worklist. */
439 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
441 bb = gimple_bb (stmt);
443 /* Visit the statement only if its block is marked executable.
444 If it is not executable then it will be visited when we simulate
445 all statements in the block as soon as an incoming edge gets
446 marked executable. */
447 if (!bitmap_bit_p (executable_blocks, bb->index))
449 if (dump_file && (dump_flags & TDF_DETAILS))
451 fprintf (dump_file, "\nDropping statement from SSA worklist: ");
452 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
454 continue;
457 if (dump_file && (dump_flags & TDF_DETAILS))
459 fprintf (dump_file, "\nSimulating statement (from %s): ",
460 edge_list_name);
461 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
464 simulate_stmt (stmt);
466 return true;
469 return false;
473 /* Simulate the execution of BLOCK. Evaluate the statement associated
474 with each variable reference inside the block. */
476 static void
477 simulate_block (basic_block block)
479 gimple_stmt_iterator gsi;
481 /* There is nothing to do for the exit block. */
482 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
483 return;
485 if (dump_file && (dump_flags & TDF_DETAILS))
486 fprintf (dump_file, "\nSimulating block %d\n", block->index);
488 /* Always simulate PHI nodes, even if we have simulated this block
489 before. */
490 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
491 simulate_stmt (gsi_stmt (gsi));
493 /* If this is the first time we've simulated this block, then we
494 must simulate each of its statements. */
495 if (!bitmap_bit_p (executable_blocks, block->index))
497 gimple_stmt_iterator j;
498 unsigned int normal_edge_count;
499 edge e, normal_edge;
500 edge_iterator ei;
502 /* Note that we have simulated this block. */
503 bitmap_set_bit (executable_blocks, block->index);
505 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
507 gimple stmt = gsi_stmt (j);
509 /* If this statement is already in the worklist then
510 "cancel" it. The reevaluation implied by the worklist
511 entry will produce the same value we generate here and
512 thus reevaluating it again from the worklist is
513 pointless. */
514 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
515 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
517 simulate_stmt (stmt);
520 /* We can not predict when abnormal and EH edges will be executed, so
521 once a block is considered executable, we consider any
522 outgoing abnormal edges as executable.
524 TODO: This is not exactly true. Simplifying statement might
525 prove it non-throwing and also computed goto can be handled
526 when destination is known.
528 At the same time, if this block has only one successor that is
529 reached by non-abnormal edges, then add that successor to the
530 worklist. */
531 normal_edge_count = 0;
532 normal_edge = NULL;
533 FOR_EACH_EDGE (e, ei, block->succs)
535 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
536 add_control_edge (e);
537 else
539 normal_edge_count++;
540 normal_edge = e;
544 if (normal_edge_count == 1)
545 add_control_edge (normal_edge);
550 /* Initialize local data structures and work lists. */
552 static void
553 ssa_prop_init (void)
555 edge e;
556 edge_iterator ei;
557 basic_block bb;
559 /* Worklists of SSA edges. */
560 interesting_ssa_edges.create (20);
561 varying_ssa_edges.create (20);
563 executable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
564 bitmap_clear (executable_blocks);
566 bb_in_list = sbitmap_alloc (last_basic_block_for_fn (cfun));
567 bitmap_clear (bb_in_list);
569 if (dump_file && (dump_flags & TDF_DETAILS))
570 dump_immediate_uses (dump_file);
572 cfg_blocks.create (20);
573 cfg_blocks.safe_grow_cleared (20);
575 /* Initially assume that every edge in the CFG is not executable.
576 (including the edges coming out of the entry block). */
577 FOR_ALL_BB_FN (bb, cfun)
579 gimple_stmt_iterator si;
581 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
582 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
584 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
585 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
587 FOR_EACH_EDGE (e, ei, bb->succs)
588 e->flags &= ~EDGE_EXECUTABLE;
591 /* Seed the algorithm by adding the successors of the entry block to the
592 edge worklist. */
593 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
594 add_control_edge (e);
598 /* Free allocated storage. */
600 static void
601 ssa_prop_fini (void)
603 interesting_ssa_edges.release ();
604 varying_ssa_edges.release ();
605 cfg_blocks.release ();
606 sbitmap_free (bb_in_list);
607 sbitmap_free (executable_blocks);
611 /* Return true if EXPR is an acceptable right-hand-side for a
612 GIMPLE assignment. We validate the entire tree, not just
613 the root node, thus catching expressions that embed complex
614 operands that are not permitted in GIMPLE. This function
615 is needed because the folding routines in fold-const.c
616 may return such expressions in some cases, e.g., an array
617 access with an embedded index addition. It may make more
618 sense to have folding routines that are sensitive to the
619 constraints on GIMPLE operands, rather than abandoning any
620 any attempt to fold if the usual folding turns out to be too
621 aggressive. */
623 bool
624 valid_gimple_rhs_p (tree expr)
626 enum tree_code code = TREE_CODE (expr);
628 switch (TREE_CODE_CLASS (code))
630 case tcc_declaration:
631 if (!is_gimple_variable (expr))
632 return false;
633 break;
635 case tcc_constant:
636 /* All constants are ok. */
637 break;
639 case tcc_comparison:
640 /* GENERIC allows comparisons with non-boolean types, reject
641 those for GIMPLE. Let vector-typed comparisons pass - rules
642 for GENERIC and GIMPLE are the same here. */
643 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
644 && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
645 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
646 && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
647 return false;
649 /* Fallthru. */
650 case tcc_binary:
651 if (!is_gimple_val (TREE_OPERAND (expr, 0))
652 || !is_gimple_val (TREE_OPERAND (expr, 1)))
653 return false;
654 break;
656 case tcc_unary:
657 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
658 return false;
659 break;
661 case tcc_expression:
662 switch (code)
664 case ADDR_EXPR:
666 tree t;
667 if (is_gimple_min_invariant (expr))
668 return true;
669 t = TREE_OPERAND (expr, 0);
670 while (handled_component_p (t))
672 /* ??? More checks needed, see the GIMPLE verifier. */
673 if ((TREE_CODE (t) == ARRAY_REF
674 || TREE_CODE (t) == ARRAY_RANGE_REF)
675 && !is_gimple_val (TREE_OPERAND (t, 1)))
676 return false;
677 t = TREE_OPERAND (t, 0);
679 if (!is_gimple_id (t))
680 return false;
682 break;
684 default:
685 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
687 if (((code == VEC_COND_EXPR || code == COND_EXPR)
688 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
689 : !is_gimple_val (TREE_OPERAND (expr, 0)))
690 || !is_gimple_val (TREE_OPERAND (expr, 1))
691 || !is_gimple_val (TREE_OPERAND (expr, 2)))
692 return false;
693 break;
695 return false;
697 break;
699 case tcc_vl_exp:
700 return false;
702 case tcc_exceptional:
703 if (code == CONSTRUCTOR)
705 unsigned i;
706 tree elt;
707 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
708 if (!is_gimple_val (elt))
709 return false;
710 return true;
712 if (code != SSA_NAME)
713 return false;
714 break;
716 case tcc_reference:
717 if (code == BIT_FIELD_REF)
718 return is_gimple_val (TREE_OPERAND (expr, 0));
719 return false;
721 default:
722 return false;
725 return true;
729 /* Return true if EXPR is a CALL_EXPR suitable for representation
730 as a single GIMPLE_CALL statement. If the arguments require
731 further gimplification, return false. */
733 static bool
734 valid_gimple_call_p (tree expr)
736 unsigned i, nargs;
738 if (TREE_CODE (expr) != CALL_EXPR)
739 return false;
741 nargs = call_expr_nargs (expr);
742 for (i = 0; i < nargs; i++)
744 tree arg = CALL_EXPR_ARG (expr, i);
745 if (is_gimple_reg_type (TREE_TYPE (arg)))
747 if (!is_gimple_val (arg))
748 return false;
750 else
751 if (!is_gimple_lvalue (arg))
752 return false;
755 return true;
759 /* Make SSA names defined by OLD_STMT point to NEW_STMT
760 as their defining statement. */
762 void
763 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
765 tree var;
766 ssa_op_iter iter;
768 if (gimple_in_ssa_p (cfun))
770 /* Make defined SSA_NAMEs point to the new
771 statement as their definition. */
772 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
774 if (TREE_CODE (var) == SSA_NAME)
775 SSA_NAME_DEF_STMT (var) = new_stmt;
780 /* Helper function for update_gimple_call and update_call_from_tree.
781 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
783 static void
784 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
785 gimple stmt)
787 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
788 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
789 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
790 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
791 gimple_set_location (new_stmt, gimple_location (stmt));
792 if (gimple_block (new_stmt) == NULL_TREE)
793 gimple_set_block (new_stmt, gimple_block (stmt));
794 gsi_replace (si_p, new_stmt, false);
797 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
798 with number of arguments NARGS, where the arguments in GIMPLE form
799 follow NARGS argument. */
801 bool
802 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
804 va_list ap;
805 gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
807 gcc_assert (is_gimple_call (stmt));
808 va_start (ap, nargs);
809 new_stmt = gimple_build_call_valist (fn, nargs, ap);
810 finish_update_gimple_call (si_p, new_stmt, stmt);
811 va_end (ap);
812 return true;
815 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
816 value of EXPR, which is expected to be the result of folding the
817 call. This can only be done if EXPR is a CALL_EXPR with valid
818 GIMPLE operands as arguments, or if it is a suitable RHS expression
819 for a GIMPLE_ASSIGN. More complex expressions will require
820 gimplification, which will introduce additional statements. In this
821 event, no update is performed, and the function returns false.
822 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
823 replace the statement at *SI_P with an entirely new statement.
824 The new statement need not be a call, e.g., if the original call
825 folded to a constant. */
827 bool
828 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
830 gimple stmt = gsi_stmt (*si_p);
832 if (valid_gimple_call_p (expr))
834 /* The call has simplified to another call. */
835 tree fn = CALL_EXPR_FN (expr);
836 unsigned i;
837 unsigned nargs = call_expr_nargs (expr);
838 vec<tree> args = vNULL;
839 gcall *new_stmt;
841 if (nargs > 0)
843 args.create (nargs);
844 args.safe_grow_cleared (nargs);
846 for (i = 0; i < nargs; i++)
847 args[i] = CALL_EXPR_ARG (expr, i);
850 new_stmt = gimple_build_call_vec (fn, args);
851 finish_update_gimple_call (si_p, new_stmt, stmt);
852 args.release ();
854 return true;
856 else if (valid_gimple_rhs_p (expr))
858 tree lhs = gimple_call_lhs (stmt);
859 gimple new_stmt;
861 /* The call has simplified to an expression
862 that cannot be represented as a GIMPLE_CALL. */
863 if (lhs)
865 /* A value is expected.
866 Introduce a new GIMPLE_ASSIGN statement. */
867 STRIP_USELESS_TYPE_CONVERSION (expr);
868 new_stmt = gimple_build_assign (lhs, expr);
869 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
870 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
871 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
873 else if (!TREE_SIDE_EFFECTS (expr))
875 /* No value is expected, and EXPR has no effect.
876 Replace it with an empty statement. */
877 new_stmt = gimple_build_nop ();
878 if (gimple_in_ssa_p (cfun))
880 unlink_stmt_vdef (stmt);
881 release_defs (stmt);
884 else
886 /* No value is expected, but EXPR has an effect,
887 e.g., it could be a reference to a volatile
888 variable. Create an assignment statement
889 with a dummy (unused) lhs variable. */
890 STRIP_USELESS_TYPE_CONVERSION (expr);
891 if (gimple_in_ssa_p (cfun))
892 lhs = make_ssa_name (TREE_TYPE (expr));
893 else
894 lhs = create_tmp_var (TREE_TYPE (expr));
895 new_stmt = gimple_build_assign (lhs, expr);
896 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
897 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
898 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
900 gimple_set_location (new_stmt, gimple_location (stmt));
901 gsi_replace (si_p, new_stmt, false);
902 return true;
904 else
905 /* The call simplified to an expression that is
906 not a valid GIMPLE RHS. */
907 return false;
911 /* Entry point to the propagation engine.
913 VISIT_STMT is called for every statement visited.
914 VISIT_PHI is called for every PHI node visited. */
916 void
917 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
918 ssa_prop_visit_phi_fn visit_phi)
920 ssa_prop_visit_stmt = visit_stmt;
921 ssa_prop_visit_phi = visit_phi;
923 ssa_prop_init ();
925 /* Iterate until the worklists are empty. */
926 while (!cfg_blocks_empty_p ()
927 || interesting_ssa_edges.length () > 0
928 || varying_ssa_edges.length () > 0)
930 if (!cfg_blocks_empty_p ())
932 /* Pull the next block to simulate off the worklist. */
933 basic_block dest_block = cfg_blocks_get ();
934 simulate_block (dest_block);
935 continue;
938 /* In order to move things to varying as quickly as
939 possible,process the VARYING_SSA_EDGES worklist first. */
940 if (process_ssa_edge_worklist (&varying_ssa_edges, "varying_ssa_edges"))
941 continue;
943 /* Now process the INTERESTING_SSA_EDGES worklist. */
944 process_ssa_edge_worklist (&interesting_ssa_edges,
945 "interesting_ssa_edges");
948 ssa_prop_fini ();
952 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
953 is a non-volatile pointer dereference, a structure reference or a
954 reference to a single _DECL. Ignore volatile memory references
955 because they are not interesting for the optimizers. */
957 bool
958 stmt_makes_single_store (gimple stmt)
960 tree lhs;
962 if (gimple_code (stmt) != GIMPLE_ASSIGN
963 && gimple_code (stmt) != GIMPLE_CALL)
964 return false;
966 if (!gimple_vdef (stmt))
967 return false;
969 lhs = gimple_get_lhs (stmt);
971 /* A call statement may have a null LHS. */
972 if (!lhs)
973 return false;
975 return (!TREE_THIS_VOLATILE (lhs)
976 && (DECL_P (lhs)
977 || REFERENCE_CLASS_P (lhs)));
981 /* Propagation statistics. */
982 struct prop_stats_d
984 long num_const_prop;
985 long num_copy_prop;
986 long num_stmts_folded;
987 long num_dce;
990 static struct prop_stats_d prop_stats;
992 /* Replace USE references in statement STMT with the values stored in
993 PROP_VALUE. Return true if at least one reference was replaced. */
995 static bool
996 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
998 bool replaced = false;
999 use_operand_p use;
1000 ssa_op_iter iter;
1002 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
1004 tree tuse = USE_FROM_PTR (use);
1005 tree val = (*get_value) (tuse);
1007 if (val == tuse || val == NULL_TREE)
1008 continue;
1010 if (gimple_code (stmt) == GIMPLE_ASM
1011 && !may_propagate_copy_into_asm (tuse))
1012 continue;
1014 if (!may_propagate_copy (tuse, val))
1015 continue;
1017 if (TREE_CODE (val) != SSA_NAME)
1018 prop_stats.num_const_prop++;
1019 else
1020 prop_stats.num_copy_prop++;
1022 propagate_value (use, val);
1024 replaced = true;
1027 return replaced;
1031 /* Replace propagated values into all the arguments for PHI using the
1032 values from PROP_VALUE. */
1034 static bool
1035 replace_phi_args_in (gphi *phi, ssa_prop_get_value_fn get_value)
1037 size_t i;
1038 bool replaced = false;
1040 if (dump_file && (dump_flags & TDF_DETAILS))
1042 fprintf (dump_file, "Folding PHI node: ");
1043 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1046 basic_block bb = gimple_bb (phi);
1047 for (i = 0; i < gimple_phi_num_args (phi); i++)
1049 tree arg = gimple_phi_arg_def (phi, i);
1051 if (TREE_CODE (arg) == SSA_NAME)
1053 tree val = (*get_value) (arg);
1055 if (val && val != arg && may_propagate_copy (arg, val))
1057 edge e = gimple_phi_arg_edge (phi, i);
1059 /* Avoid propagating constants into loop latch edge
1060 PHI arguments as this makes coalescing the copy
1061 across this edge impossible. If the argument is
1062 defined by an assert - otherwise the stmt will
1063 get removed without replacing its uses. */
1064 if (TREE_CODE (val) != SSA_NAME
1065 && bb->loop_father->header == bb
1066 && dominated_by_p (CDI_DOMINATORS, e->src, bb)
1067 && is_gimple_assign (SSA_NAME_DEF_STMT (arg))
1068 && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (arg))
1069 == ASSERT_EXPR))
1070 continue;
1072 if (TREE_CODE (val) != SSA_NAME)
1073 prop_stats.num_const_prop++;
1074 else
1075 prop_stats.num_copy_prop++;
1077 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
1078 replaced = true;
1080 /* If we propagated a copy and this argument flows
1081 through an abnormal edge, update the replacement
1082 accordingly. */
1083 if (TREE_CODE (val) == SSA_NAME
1084 && e->flags & EDGE_ABNORMAL
1085 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1087 /* This can only occur for virtual operands, since
1088 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1089 would prevent replacement. */
1090 gcc_checking_assert (virtual_operand_p (val));
1091 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1097 if (dump_file && (dump_flags & TDF_DETAILS))
1099 if (!replaced)
1100 fprintf (dump_file, "No folding possible\n");
1101 else
1103 fprintf (dump_file, "Folded into: ");
1104 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1105 fprintf (dump_file, "\n");
1109 return replaced;
1113 class substitute_and_fold_dom_walker : public dom_walker
1115 public:
1116 substitute_and_fold_dom_walker (cdi_direction direction,
1117 ssa_prop_get_value_fn get_value_fn_,
1118 ssa_prop_fold_stmt_fn fold_fn_,
1119 bool do_dce_)
1120 : dom_walker (direction), get_value_fn (get_value_fn_),
1121 fold_fn (fold_fn_), do_dce (do_dce_), something_changed (false)
1123 stmts_to_remove.create (0);
1124 stmts_to_fixup.create (0);
1125 need_eh_cleanup = BITMAP_ALLOC (NULL);
1127 ~substitute_and_fold_dom_walker ()
1129 stmts_to_remove.release ();
1130 stmts_to_fixup.release ();
1131 BITMAP_FREE (need_eh_cleanup);
1134 virtual void before_dom_children (basic_block);
1135 virtual void after_dom_children (basic_block) {}
1137 ssa_prop_get_value_fn get_value_fn;
1138 ssa_prop_fold_stmt_fn fold_fn;
1139 bool do_dce;
1140 bool something_changed;
1141 vec<gimple> stmts_to_remove;
1142 vec<gimple> stmts_to_fixup;
1143 bitmap need_eh_cleanup;
1146 void
1147 substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
1149 /* Propagate known values into PHI nodes. */
1150 for (gphi_iterator i = gsi_start_phis (bb);
1151 !gsi_end_p (i);
1152 gsi_next (&i))
1154 gphi *phi = i.phi ();
1155 tree res = gimple_phi_result (phi);
1156 if (virtual_operand_p (res))
1157 continue;
1158 if (do_dce
1159 && res && TREE_CODE (res) == SSA_NAME)
1161 tree sprime = get_value_fn (res);
1162 if (sprime
1163 && sprime != res
1164 && may_propagate_copy (res, sprime))
1166 stmts_to_remove.safe_push (phi);
1167 continue;
1170 something_changed |= replace_phi_args_in (phi, get_value_fn);
1173 /* Propagate known values into stmts. In some case it exposes
1174 more trivially deletable stmts to walk backward. */
1175 for (gimple_stmt_iterator i = gsi_start_bb (bb);
1176 !gsi_end_p (i);
1177 gsi_next (&i))
1179 bool did_replace;
1180 gimple stmt = gsi_stmt (i);
1181 enum gimple_code code = gimple_code (stmt);
1183 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1184 range information for names and they are discarded
1185 afterwards. */
1187 if (code == GIMPLE_ASSIGN
1188 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1189 continue;
1191 /* No point propagating into a stmt we have a value for we
1192 can propagate into all uses. Mark it for removal instead. */
1193 tree lhs = gimple_get_lhs (stmt);
1194 if (do_dce
1195 && lhs && TREE_CODE (lhs) == SSA_NAME)
1197 tree sprime = get_value_fn (lhs);
1198 if (sprime
1199 && sprime != lhs
1200 && may_propagate_copy (lhs, sprime)
1201 && !stmt_could_throw_p (stmt)
1202 && !gimple_has_side_effects (stmt))
1204 stmts_to_remove.safe_push (stmt);
1205 continue;
1209 /* Replace the statement with its folded version and mark it
1210 folded. */
1211 did_replace = false;
1212 if (dump_file && (dump_flags & TDF_DETAILS))
1214 fprintf (dump_file, "Folding statement: ");
1215 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1218 gimple old_stmt = stmt;
1219 bool was_noreturn = (is_gimple_call (stmt)
1220 && gimple_call_noreturn_p (stmt));
1222 /* Some statements may be simplified using propagator
1223 specific information. Do this before propagating
1224 into the stmt to not disturb pass specific information. */
1225 if (fold_fn
1226 && (*fold_fn)(&i))
1228 did_replace = true;
1229 prop_stats.num_stmts_folded++;
1230 stmt = gsi_stmt (i);
1231 update_stmt (stmt);
1234 /* Replace real uses in the statement. */
1235 did_replace |= replace_uses_in (stmt, get_value_fn);
1237 /* If we made a replacement, fold the statement. */
1238 if (did_replace)
1240 fold_stmt (&i, follow_single_use_edges);
1241 stmt = gsi_stmt (i);
1244 /* If this is a control statement the propagator left edges
1245 unexecuted on force the condition in a way consistent with
1246 that. See PR66945 for cases where the propagator can end
1247 up with a different idea of a taken edge than folding
1248 (once undefined behavior is involved). */
1249 if (gimple_code (stmt) == GIMPLE_COND)
1251 if ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE)
1252 ^ (EDGE_SUCC (bb, 1)->flags & EDGE_EXECUTABLE))
1254 if (((EDGE_SUCC (bb, 0)->flags & EDGE_TRUE_VALUE) != 0)
1255 == ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE) != 0))
1256 gimple_cond_make_true (as_a <gcond *> (stmt));
1257 else
1258 gimple_cond_make_false (as_a <gcond *> (stmt));
1259 did_replace = true;
1263 /* Now cleanup. */
1264 if (did_replace)
1266 /* If we cleaned up EH information from the statement,
1267 remove EH edges. */
1268 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1269 bitmap_set_bit (need_eh_cleanup, bb->index);
1271 /* If we turned a not noreturn call into a noreturn one
1272 schedule it for fixup. */
1273 if (!was_noreturn
1274 && is_gimple_call (stmt)
1275 && gimple_call_noreturn_p (stmt))
1276 stmts_to_fixup.safe_push (stmt);
1278 if (gimple_assign_single_p (stmt))
1280 tree rhs = gimple_assign_rhs1 (stmt);
1282 if (TREE_CODE (rhs) == ADDR_EXPR)
1283 recompute_tree_invariant_for_addr_expr (rhs);
1286 /* Determine what needs to be done to update the SSA form. */
1287 update_stmt (stmt);
1288 if (!is_gimple_debug (stmt))
1289 something_changed = true;
1292 if (dump_file && (dump_flags & TDF_DETAILS))
1294 if (did_replace)
1296 fprintf (dump_file, "Folded into: ");
1297 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1298 fprintf (dump_file, "\n");
1300 else
1301 fprintf (dump_file, "Not folded\n");
1308 /* Perform final substitution and folding of propagated values.
1310 PROP_VALUE[I] contains the single value that should be substituted
1311 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1312 substituted.
1314 If FOLD_FN is non-NULL the function will be invoked on all statements
1315 before propagating values for pass specific simplification.
1317 DO_DCE is true if trivially dead stmts can be removed.
1319 If DO_DCE is true, the statements within a BB are walked from
1320 last to first element. Otherwise we scan from first to last element.
1322 Return TRUE when something changed. */
1324 bool
1325 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1326 ssa_prop_fold_stmt_fn fold_fn,
1327 bool do_dce)
1329 gcc_assert (get_value_fn);
1331 if (dump_file && (dump_flags & TDF_DETAILS))
1332 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1334 memset (&prop_stats, 0, sizeof (prop_stats));
1336 calculate_dominance_info (CDI_DOMINATORS);
1337 substitute_and_fold_dom_walker walker(CDI_DOMINATORS,
1338 get_value_fn, fold_fn, do_dce);
1339 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1341 /* We cannot remove stmts during the BB walk, especially not release
1342 SSA names there as that destroys the lattice of our callers.
1343 Remove stmts in reverse order to make debug stmt creation possible. */
1344 while (!walker.stmts_to_remove.is_empty ())
1346 gimple stmt = walker.stmts_to_remove.pop ();
1347 if (dump_file && dump_flags & TDF_DETAILS)
1349 fprintf (dump_file, "Removing dead stmt ");
1350 print_gimple_stmt (dump_file, stmt, 0, 0);
1351 fprintf (dump_file, "\n");
1353 prop_stats.num_dce++;
1354 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1355 if (gimple_code (stmt) == GIMPLE_PHI)
1356 remove_phi_node (&gsi, true);
1357 else
1359 unlink_stmt_vdef (stmt);
1360 gsi_remove (&gsi, true);
1361 release_defs (stmt);
1365 if (!bitmap_empty_p (walker.need_eh_cleanup))
1366 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1368 /* Fixup stmts that became noreturn calls. This may require splitting
1369 blocks and thus isn't possible during the dominator walk. Do this
1370 in reverse order so we don't inadvertedly remove a stmt we want to
1371 fixup by visiting a dominating now noreturn call first. */
1372 while (!walker.stmts_to_fixup.is_empty ())
1374 gimple stmt = walker.stmts_to_fixup.pop ();
1375 if (dump_file && dump_flags & TDF_DETAILS)
1377 fprintf (dump_file, "Fixing up noreturn call ");
1378 print_gimple_stmt (dump_file, stmt, 0, 0);
1379 fprintf (dump_file, "\n");
1381 fixup_noreturn_call (stmt);
1384 statistics_counter_event (cfun, "Constants propagated",
1385 prop_stats.num_const_prop);
1386 statistics_counter_event (cfun, "Copies propagated",
1387 prop_stats.num_copy_prop);
1388 statistics_counter_event (cfun, "Statements folded",
1389 prop_stats.num_stmts_folded);
1390 statistics_counter_event (cfun, "Statements deleted",
1391 prop_stats.num_dce);
1393 return walker.something_changed;
1397 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1399 bool
1400 may_propagate_copy (tree dest, tree orig)
1402 tree type_d = TREE_TYPE (dest);
1403 tree type_o = TREE_TYPE (orig);
1405 /* If ORIG is a default definition which flows in from an abnormal edge
1406 then the copy can be propagated. It is important that we do so to avoid
1407 uninitialized copies. */
1408 if (TREE_CODE (orig) == SSA_NAME
1409 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1410 && SSA_NAME_IS_DEFAULT_DEF (orig)
1411 && (SSA_NAME_VAR (orig) == NULL_TREE
1412 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL))
1414 /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1415 be propagated. */
1416 else if (TREE_CODE (orig) == SSA_NAME
1417 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1418 return false;
1419 /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1420 propagated. */
1421 else if (TREE_CODE (dest) == SSA_NAME
1422 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1423 return false;
1425 /* Do not copy between types for which we *do* need a conversion. */
1426 if (!useless_type_conversion_p (type_d, type_o))
1427 return false;
1429 /* Generally propagating virtual operands is not ok as that may
1430 create overlapping life-ranges. */
1431 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1432 return false;
1434 /* Anything else is OK. */
1435 return true;
1438 /* Like may_propagate_copy, but use as the destination expression
1439 the principal expression (typically, the RHS) contained in
1440 statement DEST. This is more efficient when working with the
1441 gimple tuples representation. */
1443 bool
1444 may_propagate_copy_into_stmt (gimple dest, tree orig)
1446 tree type_d;
1447 tree type_o;
1449 /* If the statement is a switch or a single-rhs assignment,
1450 then the expression to be replaced by the propagation may
1451 be an SSA_NAME. Fortunately, there is an explicit tree
1452 for the expression, so we delegate to may_propagate_copy. */
1454 if (gimple_assign_single_p (dest))
1455 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1456 else if (gswitch *dest_swtch = dyn_cast <gswitch *> (dest))
1457 return may_propagate_copy (gimple_switch_index (dest_swtch), orig);
1459 /* In other cases, the expression is not materialized, so there
1460 is no destination to pass to may_propagate_copy. On the other
1461 hand, the expression cannot be an SSA_NAME, so the analysis
1462 is much simpler. */
1464 if (TREE_CODE (orig) == SSA_NAME
1465 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1466 return false;
1468 if (is_gimple_assign (dest))
1469 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1470 else if (gimple_code (dest) == GIMPLE_COND)
1471 type_d = boolean_type_node;
1472 else if (is_gimple_call (dest)
1473 && gimple_call_lhs (dest) != NULL_TREE)
1474 type_d = TREE_TYPE (gimple_call_lhs (dest));
1475 else
1476 gcc_unreachable ();
1478 type_o = TREE_TYPE (orig);
1480 if (!useless_type_conversion_p (type_d, type_o))
1481 return false;
1483 return true;
1486 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1488 bool
1489 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1491 return true;
1495 /* Common code for propagate_value and replace_exp.
1497 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1498 replacement is done to propagate a value or not. */
1500 static void
1501 replace_exp_1 (use_operand_p op_p, tree val,
1502 bool for_propagation ATTRIBUTE_UNUSED)
1504 #if defined ENABLE_CHECKING
1505 tree op = USE_FROM_PTR (op_p);
1507 gcc_assert (!(for_propagation
1508 && TREE_CODE (op) == SSA_NAME
1509 && TREE_CODE (val) == SSA_NAME
1510 && !may_propagate_copy (op, val)));
1511 #endif
1513 if (TREE_CODE (val) == SSA_NAME)
1514 SET_USE (op_p, val);
1515 else
1516 SET_USE (op_p, unshare_expr (val));
1520 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1521 into the operand pointed to by OP_P.
1523 Use this version for const/copy propagation as it will perform additional
1524 checks to ensure validity of the const/copy propagation. */
1526 void
1527 propagate_value (use_operand_p op_p, tree val)
1529 replace_exp_1 (op_p, val, true);
1532 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1534 Use this version when not const/copy propagating values. For example,
1535 PRE uses this version when building expressions as they would appear
1536 in specific blocks taking into account actions of PHI nodes.
1538 The statement in which an expression has been replaced should be
1539 folded using fold_stmt_inplace. */
1541 void
1542 replace_exp (use_operand_p op_p, tree val)
1544 replace_exp_1 (op_p, val, false);
1548 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1549 into the tree pointed to by OP_P.
1551 Use this version for const/copy propagation when SSA operands are not
1552 available. It will perform the additional checks to ensure validity of
1553 the const/copy propagation, but will not update any operand information.
1554 Be sure to mark the stmt as modified. */
1556 void
1557 propagate_tree_value (tree *op_p, tree val)
1559 if (TREE_CODE (val) == SSA_NAME)
1560 *op_p = val;
1561 else
1562 *op_p = unshare_expr (val);
1566 /* Like propagate_tree_value, but use as the operand to replace
1567 the principal expression (typically, the RHS) contained in the
1568 statement referenced by iterator GSI. Note that it is not
1569 always possible to update the statement in-place, so a new
1570 statement may be created to replace the original. */
1572 void
1573 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1575 gimple stmt = gsi_stmt (*gsi);
1577 if (is_gimple_assign (stmt))
1579 tree expr = NULL_TREE;
1580 if (gimple_assign_single_p (stmt))
1581 expr = gimple_assign_rhs1 (stmt);
1582 propagate_tree_value (&expr, val);
1583 gimple_assign_set_rhs_from_tree (gsi, expr);
1585 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
1587 tree lhs = NULL_TREE;
1588 tree rhs = build_zero_cst (TREE_TYPE (val));
1589 propagate_tree_value (&lhs, val);
1590 gimple_cond_set_code (cond_stmt, NE_EXPR);
1591 gimple_cond_set_lhs (cond_stmt, lhs);
1592 gimple_cond_set_rhs (cond_stmt, rhs);
1594 else if (is_gimple_call (stmt)
1595 && gimple_call_lhs (stmt) != NULL_TREE)
1597 tree expr = NULL_TREE;
1598 bool res;
1599 propagate_tree_value (&expr, val);
1600 res = update_call_from_tree (gsi, expr);
1601 gcc_assert (res);
1603 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1604 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt), val);
1605 else
1606 gcc_unreachable ();