* tree.c (free_lang_data_in_decl): Also set target/optimization flags
[official-gcc.git] / gcc / tree-ssa-propagate.c
blobc4535a4eb2e8b22f99e7f66cdc24a2e886c6b6fd
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2016 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 "ssa.h"
28 #include "gimple-pretty-print.h"
29 #include "dumpfile.h"
30 #include "gimple-fold.h"
31 #include "tree-eh.h"
32 #include "gimplify.h"
33 #include "gimple-iterator.h"
34 #include "tree-cfg.h"
35 #include "tree-ssa.h"
36 #include "tree-ssa-propagate.h"
37 #include "domwalk.h"
38 #include "cfgloop.h"
39 #include "tree-cfgcleanup.h"
41 /* This file implements a generic value propagation engine based on
42 the same propagation used by the SSA-CCP algorithm [1].
44 Propagation is performed by simulating the execution of every
45 statement that produces the value being propagated. Simulation
46 proceeds as follows:
48 1- Initially, all edges of the CFG are marked not executable and
49 the CFG worklist is seeded with all the statements in the entry
50 basic block (block 0).
52 2- Every statement S is simulated with a call to the call-back
53 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
54 results:
56 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
57 interest and does not affect any of the work lists.
58 The statement may be simulated again if any of its input
59 operands change in future iterations of the simulator.
61 SSA_PROP_VARYING: The value produced by S cannot be determined
62 at compile time. Further simulation of S is not required.
63 If S is a conditional jump, all the outgoing edges for the
64 block are considered executable and added to the work
65 list.
67 SSA_PROP_INTERESTING: S produces a value that can be computed
68 at compile time. Its result can be propagated into the
69 statements that feed from S. Furthermore, if S is a
70 conditional jump, only the edge known to be taken is added
71 to the work list. Edges that are known not to execute are
72 never simulated.
74 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
75 return value from SSA_PROP_VISIT_PHI has the same semantics as
76 described in #2.
78 4- Three work lists are kept. Statements are only added to these
79 lists if they produce one of SSA_PROP_INTERESTING or
80 SSA_PROP_VARYING.
82 CFG_BLOCKS contains the list of blocks to be simulated.
83 Blocks are added to this list if their incoming edges are
84 found executable.
86 VARYING_SSA_EDGES contains the list of statements that feed
87 from statements that produce an SSA_PROP_VARYING result.
88 These are simulated first to speed up processing.
90 INTERESTING_SSA_EDGES contains the list of statements that
91 feed from statements that produce an SSA_PROP_INTERESTING
92 result.
94 5- Simulation terminates when all three work lists are drained.
96 Before calling ssa_propagate, it is important to clear
97 prop_simulate_again_p for all the statements in the program that
98 should be simulated. This initialization allows an implementation
99 to specify which statements should never be simulated.
101 It is also important to compute def-use information before calling
102 ssa_propagate.
104 References:
106 [1] Constant propagation with conditional branches,
107 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
109 [2] Building an Optimizing Compiler,
110 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
112 [3] Advanced Compiler Design and Implementation,
113 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
115 /* Function pointers used to parameterize the propagation engine. */
116 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
117 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
119 /* Keep track of statements that have been added to one of the SSA
120 edges worklists. This flag is used to avoid visiting statements
121 unnecessarily when draining an SSA edge worklist. If while
122 simulating a basic block, we find a statement with
123 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
124 processing from visiting it again.
126 NOTE: users of the propagation engine are not allowed to use
127 the GF_PLF_1 flag. */
128 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
130 /* A bitmap to keep track of executable blocks in the CFG. */
131 static sbitmap executable_blocks;
133 /* Array of control flow edges on the worklist. */
134 static vec<basic_block> cfg_blocks;
136 static unsigned int cfg_blocks_num = 0;
137 static int cfg_blocks_tail;
138 static int cfg_blocks_head;
140 static sbitmap bb_in_list;
142 /* Worklist of SSA edges which will need reexamination as their
143 definition has changed. SSA edges are def-use edges in the SSA
144 web. For each D-U edge, we store the target statement or PHI node
145 U. */
146 static vec<gimple *> interesting_ssa_edges;
148 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
149 list of SSA edges is split into two. One contains all SSA edges
150 who need to be reexamined because their lattice value changed to
151 varying (this worklist), and the other contains all other SSA edges
152 to be reexamined (INTERESTING_SSA_EDGES).
154 Since most values in the program are VARYING, the ideal situation
155 is to move them to that lattice value as quickly as possible.
156 Thus, it doesn't make sense to process any other type of lattice
157 value until all VARYING values are propagated fully, which is one
158 thing using the VARYING worklist achieves. In addition, if we
159 don't use a separate worklist for VARYING edges, we end up with
160 situations where lattice values move from
161 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
162 static vec<gimple *> varying_ssa_edges;
165 /* Return true if the block worklist empty. */
167 static inline bool
168 cfg_blocks_empty_p (void)
170 return (cfg_blocks_num == 0);
174 /* Add a basic block to the worklist. The block must not be already
175 in the worklist, and it must not be the ENTRY or EXIT block. */
177 static void
178 cfg_blocks_add (basic_block bb)
180 bool head = false;
182 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
183 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
184 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
186 if (cfg_blocks_empty_p ())
188 cfg_blocks_tail = cfg_blocks_head = 0;
189 cfg_blocks_num = 1;
191 else
193 cfg_blocks_num++;
194 if (cfg_blocks_num > cfg_blocks.length ())
196 /* We have to grow the array now. Adjust to queue to occupy
197 the full space of the original array. We do not need to
198 initialize the newly allocated portion of the array
199 because we keep track of CFG_BLOCKS_HEAD and
200 CFG_BLOCKS_HEAD. */
201 cfg_blocks_tail = cfg_blocks.length ();
202 cfg_blocks_head = 0;
203 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
205 /* Minor optimization: we prefer to see blocks with more
206 predecessors later, because there is more of a chance that
207 the incoming edges will be executable. */
208 else if (EDGE_COUNT (bb->preds)
209 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
210 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
211 else
213 if (cfg_blocks_head == 0)
214 cfg_blocks_head = cfg_blocks.length ();
215 --cfg_blocks_head;
216 head = true;
220 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
221 bitmap_set_bit (bb_in_list, bb->index);
225 /* Remove a block from the worklist. */
227 static basic_block
228 cfg_blocks_get (void)
230 basic_block bb;
232 bb = cfg_blocks[cfg_blocks_head];
234 gcc_assert (!cfg_blocks_empty_p ());
235 gcc_assert (bb);
237 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
238 --cfg_blocks_num;
239 bitmap_clear_bit (bb_in_list, bb->index);
241 return bb;
245 /* We have just defined a new value for VAR. If IS_VARYING is true,
246 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
247 them to INTERESTING_SSA_EDGES. */
249 static void
250 add_ssa_edge (tree var, bool is_varying)
252 imm_use_iterator iter;
253 use_operand_p use_p;
255 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
257 gimple *use_stmt = USE_STMT (use_p);
259 if (prop_simulate_again_p (use_stmt)
260 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
262 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
263 if (is_varying)
265 if (dump_file && (dump_flags & TDF_DETAILS))
267 fprintf (dump_file, "varying_ssa_edges: adding SSA use in ");
268 print_gimple_stmt (dump_file, use_stmt, 0, TDF_SLIM);
270 varying_ssa_edges.safe_push (use_stmt);
272 else
274 if (dump_file && (dump_flags & TDF_DETAILS))
276 fprintf (dump_file, "interesting_ssa_edges: adding SSA use in ");
277 print_gimple_stmt (dump_file, use_stmt, 0, TDF_SLIM);
279 interesting_ssa_edges.safe_push (use_stmt);
286 /* Add edge E to the control flow worklist. */
288 static void
289 add_control_edge (edge e)
291 basic_block bb = e->dest;
292 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
293 return;
295 /* If the edge had already been executed, skip it. */
296 if (e->flags & EDGE_EXECUTABLE)
297 return;
299 e->flags |= EDGE_EXECUTABLE;
301 /* If the block is already in the list, we're done. */
302 if (bitmap_bit_p (bb_in_list, bb->index))
303 return;
305 cfg_blocks_add (bb);
307 if (dump_file && (dump_flags & TDF_DETAILS))
308 fprintf (dump_file, "Adding destination of edge (%d -> %d) to worklist\n",
309 e->src->index, e->dest->index);
313 /* Simulate the execution of STMT and update the work lists accordingly. */
315 static void
316 simulate_stmt (gimple *stmt)
318 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
319 edge taken_edge = NULL;
320 tree output_name = NULL_TREE;
322 /* Don't bother visiting statements that are already
323 considered varying by the propagator. */
324 if (!prop_simulate_again_p (stmt))
325 return;
327 if (gimple_code (stmt) == GIMPLE_PHI)
329 val = ssa_prop_visit_phi (as_a <gphi *> (stmt));
330 output_name = gimple_phi_result (stmt);
332 else
333 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
335 if (val == SSA_PROP_VARYING)
337 prop_set_simulate_again (stmt, false);
339 /* If the statement produced a new varying value, add the SSA
340 edges coming out of OUTPUT_NAME. */
341 if (output_name)
342 add_ssa_edge (output_name, true);
344 /* If STMT transfers control out of its basic block, add
345 all outgoing edges to the work list. */
346 if (stmt_ends_bb_p (stmt))
348 edge e;
349 edge_iterator ei;
350 basic_block bb = gimple_bb (stmt);
351 FOR_EACH_EDGE (e, ei, bb->succs)
352 add_control_edge (e);
354 return;
356 else if (val == SSA_PROP_INTERESTING)
358 /* If the statement produced new value, add the SSA edges coming
359 out of OUTPUT_NAME. */
360 if (output_name)
361 add_ssa_edge (output_name, false);
363 /* If we know which edge is going to be taken out of this block,
364 add it to the CFG work list. */
365 if (taken_edge)
366 add_control_edge (taken_edge);
369 /* If there are no SSA uses on the stmt whose defs are simulated
370 again then this stmt will be never visited again. */
371 bool has_simulate_again_uses = false;
372 use_operand_p use_p;
373 ssa_op_iter iter;
374 if (gimple_code (stmt) == GIMPLE_PHI)
376 edge_iterator ei;
377 edge e;
378 tree arg;
379 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
380 if (!(e->flags & EDGE_EXECUTABLE)
381 || ((arg = PHI_ARG_DEF_FROM_EDGE (stmt, e))
382 && TREE_CODE (arg) == SSA_NAME
383 && !SSA_NAME_IS_DEFAULT_DEF (arg)
384 && prop_simulate_again_p (SSA_NAME_DEF_STMT (arg))))
386 has_simulate_again_uses = true;
387 break;
390 else
391 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
393 gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
394 if (!gimple_nop_p (def_stmt)
395 && prop_simulate_again_p (def_stmt))
397 has_simulate_again_uses = true;
398 break;
401 if (!has_simulate_again_uses)
403 if (dump_file && (dump_flags & TDF_DETAILS))
404 fprintf (dump_file, "marking stmt to be not simulated again\n");
405 prop_set_simulate_again (stmt, false);
409 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
410 drain. This pops statements off the given WORKLIST and processes
411 them until one statement was simulated or there are no more statements
412 on WORKLIST. We take a pointer to WORKLIST because it may be reallocated
413 when an SSA edge is added to it in simulate_stmt. Return true if a stmt
414 was simulated. */
416 static bool
417 process_ssa_edge_worklist (vec<gimple *> *worklist, const char *edge_list_name)
419 /* Process the next entry from the worklist. */
420 while (worklist->length () > 0)
422 basic_block bb;
424 /* Pull the statement to simulate off the worklist. */
425 gimple *stmt = worklist->pop ();
427 /* If this statement was already visited by simulate_block, then
428 we don't need to visit it again here. */
429 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
430 continue;
432 /* STMT is no longer in a worklist. */
433 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
435 bb = gimple_bb (stmt);
437 /* Visit the statement only if its block is marked executable.
438 If it is not executable then it will be visited when we simulate
439 all statements in the block as soon as an incoming edge gets
440 marked executable. */
441 if (!bitmap_bit_p (executable_blocks, bb->index))
443 if (dump_file && (dump_flags & TDF_DETAILS))
445 fprintf (dump_file, "\nDropping statement from SSA worklist: ");
446 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
448 continue;
451 if (dump_file && (dump_flags & TDF_DETAILS))
453 fprintf (dump_file, "\nSimulating statement (from %s): ",
454 edge_list_name);
455 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
458 simulate_stmt (stmt);
460 return true;
463 return false;
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);
929 continue;
932 /* In order to move things to varying as quickly as
933 possible,process the VARYING_SSA_EDGES worklist first. */
934 if (process_ssa_edge_worklist (&varying_ssa_edges, "varying_ssa_edges"))
935 continue;
937 /* Now process the INTERESTING_SSA_EDGES worklist. */
938 process_ssa_edge_worklist (&interesting_ssa_edges,
939 "interesting_ssa_edges");
942 ssa_prop_fini ();
946 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
947 is a non-volatile pointer dereference, a structure reference or a
948 reference to a single _DECL. Ignore volatile memory references
949 because they are not interesting for the optimizers. */
951 bool
952 stmt_makes_single_store (gimple *stmt)
954 tree lhs;
956 if (gimple_code (stmt) != GIMPLE_ASSIGN
957 && gimple_code (stmt) != GIMPLE_CALL)
958 return false;
960 if (!gimple_vdef (stmt))
961 return false;
963 lhs = gimple_get_lhs (stmt);
965 /* A call statement may have a null LHS. */
966 if (!lhs)
967 return false;
969 return (!TREE_THIS_VOLATILE (lhs)
970 && (DECL_P (lhs)
971 || REFERENCE_CLASS_P (lhs)));
975 /* Propagation statistics. */
976 struct prop_stats_d
978 long num_const_prop;
979 long num_copy_prop;
980 long num_stmts_folded;
981 long num_dce;
984 static struct prop_stats_d prop_stats;
986 /* Replace USE references in statement STMT with the values stored in
987 PROP_VALUE. Return true if at least one reference was replaced. */
989 static bool
990 replace_uses_in (gimple *stmt, ssa_prop_get_value_fn get_value)
992 bool replaced = false;
993 use_operand_p use;
994 ssa_op_iter iter;
996 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
998 tree tuse = USE_FROM_PTR (use);
999 tree val = (*get_value) (tuse);
1001 if (val == tuse || val == NULL_TREE)
1002 continue;
1004 if (gimple_code (stmt) == GIMPLE_ASM
1005 && !may_propagate_copy_into_asm (tuse))
1006 continue;
1008 if (!may_propagate_copy (tuse, val))
1009 continue;
1011 if (TREE_CODE (val) != SSA_NAME)
1012 prop_stats.num_const_prop++;
1013 else
1014 prop_stats.num_copy_prop++;
1016 propagate_value (use, val);
1018 replaced = true;
1021 return replaced;
1025 /* Replace propagated values into all the arguments for PHI using the
1026 values from PROP_VALUE. */
1028 static bool
1029 replace_phi_args_in (gphi *phi, ssa_prop_get_value_fn get_value)
1031 size_t i;
1032 bool replaced = false;
1034 if (dump_file && (dump_flags & TDF_DETAILS))
1036 fprintf (dump_file, "Folding PHI node: ");
1037 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1040 basic_block bb = gimple_bb (phi);
1041 for (i = 0; i < gimple_phi_num_args (phi); i++)
1043 tree arg = gimple_phi_arg_def (phi, i);
1045 if (TREE_CODE (arg) == SSA_NAME)
1047 tree val = (*get_value) (arg);
1049 if (val && val != arg && may_propagate_copy (arg, val))
1051 edge e = gimple_phi_arg_edge (phi, i);
1053 /* Avoid propagating constants into loop latch edge
1054 PHI arguments as this makes coalescing the copy
1055 across this edge impossible. If the argument is
1056 defined by an assert - otherwise the stmt will
1057 get removed without replacing its uses. */
1058 if (TREE_CODE (val) != SSA_NAME
1059 && bb->loop_father->header == bb
1060 && dominated_by_p (CDI_DOMINATORS, e->src, bb)
1061 && is_gimple_assign (SSA_NAME_DEF_STMT (arg))
1062 && (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (arg))
1063 == ASSERT_EXPR))
1064 continue;
1066 if (TREE_CODE (val) != SSA_NAME)
1067 prop_stats.num_const_prop++;
1068 else
1069 prop_stats.num_copy_prop++;
1071 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
1072 replaced = true;
1074 /* If we propagated a copy and this argument flows
1075 through an abnormal edge, update the replacement
1076 accordingly. */
1077 if (TREE_CODE (val) == SSA_NAME
1078 && e->flags & EDGE_ABNORMAL
1079 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1081 /* This can only occur for virtual operands, since
1082 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1083 would prevent replacement. */
1084 gcc_checking_assert (virtual_operand_p (val));
1085 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1091 if (dump_file && (dump_flags & TDF_DETAILS))
1093 if (!replaced)
1094 fprintf (dump_file, "No folding possible\n");
1095 else
1097 fprintf (dump_file, "Folded into: ");
1098 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1099 fprintf (dump_file, "\n");
1103 return replaced;
1107 class substitute_and_fold_dom_walker : public dom_walker
1109 public:
1110 substitute_and_fold_dom_walker (cdi_direction direction,
1111 ssa_prop_get_value_fn get_value_fn_,
1112 ssa_prop_fold_stmt_fn fold_fn_,
1113 bool do_dce_)
1114 : dom_walker (direction), get_value_fn (get_value_fn_),
1115 fold_fn (fold_fn_), do_dce (do_dce_), something_changed (false)
1117 stmts_to_remove.create (0);
1118 stmts_to_fixup.create (0);
1119 need_eh_cleanup = BITMAP_ALLOC (NULL);
1121 ~substitute_and_fold_dom_walker ()
1123 stmts_to_remove.release ();
1124 stmts_to_fixup.release ();
1125 BITMAP_FREE (need_eh_cleanup);
1128 virtual edge before_dom_children (basic_block);
1129 virtual void after_dom_children (basic_block) {}
1131 ssa_prop_get_value_fn get_value_fn;
1132 ssa_prop_fold_stmt_fn fold_fn;
1133 bool do_dce;
1134 bool something_changed;
1135 vec<gimple *> stmts_to_remove;
1136 vec<gimple *> stmts_to_fixup;
1137 bitmap need_eh_cleanup;
1140 edge
1141 substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
1143 /* Propagate known values into PHI nodes. */
1144 for (gphi_iterator i = gsi_start_phis (bb);
1145 !gsi_end_p (i);
1146 gsi_next (&i))
1148 gphi *phi = i.phi ();
1149 tree res = gimple_phi_result (phi);
1150 if (virtual_operand_p (res))
1151 continue;
1152 if (do_dce
1153 && res && TREE_CODE (res) == SSA_NAME)
1155 tree sprime = get_value_fn (res);
1156 if (sprime
1157 && sprime != res
1158 && may_propagate_copy (res, sprime))
1160 stmts_to_remove.safe_push (phi);
1161 continue;
1164 something_changed |= replace_phi_args_in (phi, get_value_fn);
1167 /* Propagate known values into stmts. In some case it exposes
1168 more trivially deletable stmts to walk backward. */
1169 for (gimple_stmt_iterator i = gsi_start_bb (bb);
1170 !gsi_end_p (i);
1171 gsi_next (&i))
1173 bool did_replace;
1174 gimple *stmt = gsi_stmt (i);
1175 enum gimple_code code = gimple_code (stmt);
1177 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1178 range information for names and they are discarded
1179 afterwards. */
1181 if (code == GIMPLE_ASSIGN
1182 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1183 continue;
1185 /* No point propagating into a stmt we have a value for we
1186 can propagate into all uses. Mark it for removal instead. */
1187 tree lhs = gimple_get_lhs (stmt);
1188 if (do_dce
1189 && lhs && TREE_CODE (lhs) == SSA_NAME)
1191 tree sprime = get_value_fn (lhs);
1192 if (sprime
1193 && sprime != lhs
1194 && may_propagate_copy (lhs, sprime)
1195 && !stmt_could_throw_p (stmt)
1196 && !gimple_has_side_effects (stmt))
1198 stmts_to_remove.safe_push (stmt);
1199 continue;
1203 /* Replace the statement with its folded version and mark it
1204 folded. */
1205 did_replace = false;
1206 if (dump_file && (dump_flags & TDF_DETAILS))
1208 fprintf (dump_file, "Folding statement: ");
1209 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1212 gimple *old_stmt = stmt;
1213 bool was_noreturn = (is_gimple_call (stmt)
1214 && gimple_call_noreturn_p (stmt));
1216 /* Some statements may be simplified using propagator
1217 specific information. Do this before propagating
1218 into the stmt to not disturb pass specific information. */
1219 if (fold_fn
1220 && (*fold_fn)(&i))
1222 did_replace = true;
1223 prop_stats.num_stmts_folded++;
1224 stmt = gsi_stmt (i);
1225 update_stmt (stmt);
1228 /* Replace real uses in the statement. */
1229 did_replace |= replace_uses_in (stmt, get_value_fn);
1231 /* If we made a replacement, fold the statement. */
1232 if (did_replace)
1234 fold_stmt (&i, follow_single_use_edges);
1235 stmt = gsi_stmt (i);
1238 /* If this is a control statement the propagator left edges
1239 unexecuted on force the condition in a way consistent with
1240 that. See PR66945 for cases where the propagator can end
1241 up with a different idea of a taken edge than folding
1242 (once undefined behavior is involved). */
1243 if (gimple_code (stmt) == GIMPLE_COND)
1245 if ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE)
1246 ^ (EDGE_SUCC (bb, 1)->flags & EDGE_EXECUTABLE))
1248 if (((EDGE_SUCC (bb, 0)->flags & EDGE_TRUE_VALUE) != 0)
1249 == ((EDGE_SUCC (bb, 0)->flags & EDGE_EXECUTABLE) != 0))
1250 gimple_cond_make_true (as_a <gcond *> (stmt));
1251 else
1252 gimple_cond_make_false (as_a <gcond *> (stmt));
1253 did_replace = true;
1257 /* Now cleanup. */
1258 if (did_replace)
1260 /* If we cleaned up EH information from the statement,
1261 remove EH edges. */
1262 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1263 bitmap_set_bit (need_eh_cleanup, bb->index);
1265 /* If we turned a not noreturn call into a noreturn one
1266 schedule it for fixup. */
1267 if (!was_noreturn
1268 && is_gimple_call (stmt)
1269 && gimple_call_noreturn_p (stmt))
1270 stmts_to_fixup.safe_push (stmt);
1272 if (gimple_assign_single_p (stmt))
1274 tree rhs = gimple_assign_rhs1 (stmt);
1276 if (TREE_CODE (rhs) == ADDR_EXPR)
1277 recompute_tree_invariant_for_addr_expr (rhs);
1280 /* Determine what needs to be done to update the SSA form. */
1281 update_stmt (stmt);
1282 if (!is_gimple_debug (stmt))
1283 something_changed = true;
1286 if (dump_file && (dump_flags & TDF_DETAILS))
1288 if (did_replace)
1290 fprintf (dump_file, "Folded into: ");
1291 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1292 fprintf (dump_file, "\n");
1294 else
1295 fprintf (dump_file, "Not folded\n");
1298 return NULL;
1303 /* Perform final substitution and folding of propagated values.
1305 PROP_VALUE[I] contains the single value that should be substituted
1306 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1307 substituted.
1309 If FOLD_FN is non-NULL the function will be invoked on all statements
1310 before propagating values for pass specific simplification.
1312 DO_DCE is true if trivially dead stmts can be removed.
1314 If DO_DCE is true, the statements within a BB are walked from
1315 last to first element. Otherwise we scan from first to last element.
1317 Return TRUE when something changed. */
1319 bool
1320 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1321 ssa_prop_fold_stmt_fn fold_fn,
1322 bool do_dce)
1324 gcc_assert (get_value_fn);
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1327 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1329 memset (&prop_stats, 0, sizeof (prop_stats));
1331 calculate_dominance_info (CDI_DOMINATORS);
1332 substitute_and_fold_dom_walker walker(CDI_DOMINATORS,
1333 get_value_fn, fold_fn, do_dce);
1334 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1336 /* We cannot remove stmts during the BB walk, especially not release
1337 SSA names there as that destroys the lattice of our callers.
1338 Remove stmts in reverse order to make debug stmt creation possible. */
1339 while (!walker.stmts_to_remove.is_empty ())
1341 gimple *stmt = walker.stmts_to_remove.pop ();
1342 if (dump_file && dump_flags & TDF_DETAILS)
1344 fprintf (dump_file, "Removing dead stmt ");
1345 print_gimple_stmt (dump_file, stmt, 0, 0);
1346 fprintf (dump_file, "\n");
1348 prop_stats.num_dce++;
1349 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1350 if (gimple_code (stmt) == GIMPLE_PHI)
1351 remove_phi_node (&gsi, true);
1352 else
1354 unlink_stmt_vdef (stmt);
1355 gsi_remove (&gsi, true);
1356 release_defs (stmt);
1360 if (!bitmap_empty_p (walker.need_eh_cleanup))
1361 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1363 /* Fixup stmts that became noreturn calls. This may require splitting
1364 blocks and thus isn't possible during the dominator walk. Do this
1365 in reverse order so we don't inadvertedly remove a stmt we want to
1366 fixup by visiting a dominating now noreturn call first. */
1367 while (!walker.stmts_to_fixup.is_empty ())
1369 gimple *stmt = walker.stmts_to_fixup.pop ();
1370 if (dump_file && dump_flags & TDF_DETAILS)
1372 fprintf (dump_file, "Fixing up noreturn call ");
1373 print_gimple_stmt (dump_file, stmt, 0, 0);
1374 fprintf (dump_file, "\n");
1376 fixup_noreturn_call (stmt);
1379 statistics_counter_event (cfun, "Constants propagated",
1380 prop_stats.num_const_prop);
1381 statistics_counter_event (cfun, "Copies propagated",
1382 prop_stats.num_copy_prop);
1383 statistics_counter_event (cfun, "Statements folded",
1384 prop_stats.num_stmts_folded);
1385 statistics_counter_event (cfun, "Statements deleted",
1386 prop_stats.num_dce);
1388 return walker.something_changed;
1392 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1394 bool
1395 may_propagate_copy (tree dest, tree orig)
1397 tree type_d = TREE_TYPE (dest);
1398 tree type_o = TREE_TYPE (orig);
1400 /* If ORIG is a default definition which flows in from an abnormal edge
1401 then the copy can be propagated. It is important that we do so to avoid
1402 uninitialized copies. */
1403 if (TREE_CODE (orig) == SSA_NAME
1404 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1405 && SSA_NAME_IS_DEFAULT_DEF (orig)
1406 && (SSA_NAME_VAR (orig) == NULL_TREE
1407 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL))
1409 /* Otherwise if ORIG just flows in from an abnormal edge then the copy cannot
1410 be propagated. */
1411 else if (TREE_CODE (orig) == SSA_NAME
1412 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1413 return false;
1414 /* Similarly if DEST flows in from an abnormal edge then the copy cannot be
1415 propagated. */
1416 else if (TREE_CODE (dest) == SSA_NAME
1417 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1418 return false;
1420 /* Do not copy between types for which we *do* need a conversion. */
1421 if (!useless_type_conversion_p (type_d, type_o))
1422 return false;
1424 /* Generally propagating virtual operands is not ok as that may
1425 create overlapping life-ranges. */
1426 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1427 return false;
1429 /* Anything else is OK. */
1430 return true;
1433 /* Like may_propagate_copy, but use as the destination expression
1434 the principal expression (typically, the RHS) contained in
1435 statement DEST. This is more efficient when working with the
1436 gimple tuples representation. */
1438 bool
1439 may_propagate_copy_into_stmt (gimple *dest, tree orig)
1441 tree type_d;
1442 tree type_o;
1444 /* If the statement is a switch or a single-rhs assignment,
1445 then the expression to be replaced by the propagation may
1446 be an SSA_NAME. Fortunately, there is an explicit tree
1447 for the expression, so we delegate to may_propagate_copy. */
1449 if (gimple_assign_single_p (dest))
1450 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1451 else if (gswitch *dest_swtch = dyn_cast <gswitch *> (dest))
1452 return may_propagate_copy (gimple_switch_index (dest_swtch), orig);
1454 /* In other cases, the expression is not materialized, so there
1455 is no destination to pass to may_propagate_copy. On the other
1456 hand, the expression cannot be an SSA_NAME, so the analysis
1457 is much simpler. */
1459 if (TREE_CODE (orig) == SSA_NAME
1460 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1461 return false;
1463 if (is_gimple_assign (dest))
1464 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1465 else if (gimple_code (dest) == GIMPLE_COND)
1466 type_d = boolean_type_node;
1467 else if (is_gimple_call (dest)
1468 && gimple_call_lhs (dest) != NULL_TREE)
1469 type_d = TREE_TYPE (gimple_call_lhs (dest));
1470 else
1471 gcc_unreachable ();
1473 type_o = TREE_TYPE (orig);
1475 if (!useless_type_conversion_p (type_d, type_o))
1476 return false;
1478 return true;
1481 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1483 bool
1484 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1486 return true;
1490 /* Common code for propagate_value and replace_exp.
1492 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1493 replacement is done to propagate a value or not. */
1495 static void
1496 replace_exp_1 (use_operand_p op_p, tree val,
1497 bool for_propagation ATTRIBUTE_UNUSED)
1499 if (flag_checking)
1501 tree op = USE_FROM_PTR (op_p);
1502 gcc_assert (!(for_propagation
1503 && TREE_CODE (op) == SSA_NAME
1504 && TREE_CODE (val) == SSA_NAME
1505 && !may_propagate_copy (op, val)));
1508 if (TREE_CODE (val) == SSA_NAME)
1509 SET_USE (op_p, val);
1510 else
1511 SET_USE (op_p, unshare_expr (val));
1515 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1516 into the operand pointed to by OP_P.
1518 Use this version for const/copy propagation as it will perform additional
1519 checks to ensure validity of the const/copy propagation. */
1521 void
1522 propagate_value (use_operand_p op_p, tree val)
1524 replace_exp_1 (op_p, val, true);
1527 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1529 Use this version when not const/copy propagating values. For example,
1530 PRE uses this version when building expressions as they would appear
1531 in specific blocks taking into account actions of PHI nodes.
1533 The statement in which an expression has been replaced should be
1534 folded using fold_stmt_inplace. */
1536 void
1537 replace_exp (use_operand_p op_p, tree val)
1539 replace_exp_1 (op_p, val, false);
1543 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1544 into the tree pointed to by OP_P.
1546 Use this version for const/copy propagation when SSA operands are not
1547 available. It will perform the additional checks to ensure validity of
1548 the const/copy propagation, but will not update any operand information.
1549 Be sure to mark the stmt as modified. */
1551 void
1552 propagate_tree_value (tree *op_p, tree val)
1554 if (TREE_CODE (val) == SSA_NAME)
1555 *op_p = val;
1556 else
1557 *op_p = unshare_expr (val);
1561 /* Like propagate_tree_value, but use as the operand to replace
1562 the principal expression (typically, the RHS) contained in the
1563 statement referenced by iterator GSI. Note that it is not
1564 always possible to update the statement in-place, so a new
1565 statement may be created to replace the original. */
1567 void
1568 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1570 gimple *stmt = gsi_stmt (*gsi);
1572 if (is_gimple_assign (stmt))
1574 tree expr = NULL_TREE;
1575 if (gimple_assign_single_p (stmt))
1576 expr = gimple_assign_rhs1 (stmt);
1577 propagate_tree_value (&expr, val);
1578 gimple_assign_set_rhs_from_tree (gsi, expr);
1580 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
1582 tree lhs = NULL_TREE;
1583 tree rhs = build_zero_cst (TREE_TYPE (val));
1584 propagate_tree_value (&lhs, val);
1585 gimple_cond_set_code (cond_stmt, NE_EXPR);
1586 gimple_cond_set_lhs (cond_stmt, lhs);
1587 gimple_cond_set_rhs (cond_stmt, rhs);
1589 else if (is_gimple_call (stmt)
1590 && gimple_call_lhs (stmt) != NULL_TREE)
1592 tree expr = NULL_TREE;
1593 bool res;
1594 propagate_tree_value (&expr, val);
1595 res = update_call_from_tree (gsi, expr);
1596 gcc_assert (res);
1598 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1599 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt), val);
1600 else
1601 gcc_unreachable ();