Concretize gimple_cond_set_code
[official-gcc.git] / gcc / tree-ssa-propagate.c
blob200ee80fe7cb80caeda20c5f6f83cd57a17b31b7
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple-pretty-print.h"
31 #include "dumpfile.h"
32 #include "bitmap.h"
33 #include "sbitmap.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-fold.h"
37 #include "tree-eh.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "gimplify.h"
42 #include "gimple-iterator.h"
43 #include "gimple-ssa.h"
44 #include "tree-cfg.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "stringpool.h"
48 #include "tree-ssanames.h"
49 #include "tree-ssa.h"
50 #include "tree-ssa-propagate.h"
51 #include "langhooks.h"
52 #include "value-prof.h"
53 #include "domwalk.h"
55 /* This file implements a generic value propagation engine based on
56 the same propagation used by the SSA-CCP algorithm [1].
58 Propagation is performed by simulating the execution of every
59 statement that produces the value being propagated. Simulation
60 proceeds as follows:
62 1- Initially, all edges of the CFG are marked not executable and
63 the CFG worklist is seeded with all the statements in the entry
64 basic block (block 0).
66 2- Every statement S is simulated with a call to the call-back
67 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
68 results:
70 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
71 interest and does not affect any of the work lists.
73 SSA_PROP_VARYING: The value produced by S cannot be determined
74 at compile time. Further simulation of S is not required.
75 If S is a conditional jump, all the outgoing edges for the
76 block are considered executable and added to the work
77 list.
79 SSA_PROP_INTERESTING: S produces a value that can be computed
80 at compile time. Its result can be propagated into the
81 statements that feed from S. Furthermore, if S is a
82 conditional jump, only the edge known to be taken is added
83 to the work list. Edges that are known not to execute are
84 never simulated.
86 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
87 return value from SSA_PROP_VISIT_PHI has the same semantics as
88 described in #2.
90 4- Three work lists are kept. Statements are only added to these
91 lists if they produce one of SSA_PROP_INTERESTING or
92 SSA_PROP_VARYING.
94 CFG_BLOCKS contains the list of blocks to be simulated.
95 Blocks are added to this list if their incoming edges are
96 found executable.
98 VARYING_SSA_EDGES contains the list of statements that feed
99 from statements that produce an SSA_PROP_VARYING result.
100 These are simulated first to speed up processing.
102 INTERESTING_SSA_EDGES contains the list of statements that
103 feed from statements that produce an SSA_PROP_INTERESTING
104 result.
106 5- Simulation terminates when all three work lists are drained.
108 Before calling ssa_propagate, it is important to clear
109 prop_simulate_again_p for all the statements in the program that
110 should be simulated. This initialization allows an implementation
111 to specify which statements should never be simulated.
113 It is also important to compute def-use information before calling
114 ssa_propagate.
116 References:
118 [1] Constant propagation with conditional branches,
119 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
121 [2] Building an Optimizing Compiler,
122 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
124 [3] Advanced Compiler Design and Implementation,
125 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
127 /* Function pointers used to parameterize the propagation engine. */
128 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
129 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
131 /* Keep track of statements that have been added to one of the SSA
132 edges worklists. This flag is used to avoid visiting statements
133 unnecessarily when draining an SSA edge worklist. If while
134 simulating a basic block, we find a statement with
135 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
136 processing from visiting it again.
138 NOTE: users of the propagation engine are not allowed to use
139 the GF_PLF_1 flag. */
140 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
142 /* A bitmap to keep track of executable blocks in the CFG. */
143 static sbitmap executable_blocks;
145 /* Array of control flow edges on the worklist. */
146 static vec<basic_block> cfg_blocks;
148 static unsigned int cfg_blocks_num = 0;
149 static int cfg_blocks_tail;
150 static int cfg_blocks_head;
152 static sbitmap bb_in_list;
154 /* Worklist of SSA edges which will need reexamination as their
155 definition has changed. SSA edges are def-use edges in the SSA
156 web. For each D-U edge, we store the target statement or PHI node
157 U. */
158 static vec<gimple> interesting_ssa_edges;
160 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
161 list of SSA edges is split into two. One contains all SSA edges
162 who need to be reexamined because their lattice value changed to
163 varying (this worklist), and the other contains all other SSA edges
164 to be reexamined (INTERESTING_SSA_EDGES).
166 Since most values in the program are VARYING, the ideal situation
167 is to move them to that lattice value as quickly as possible.
168 Thus, it doesn't make sense to process any other type of lattice
169 value until all VARYING values are propagated fully, which is one
170 thing using the VARYING worklist achieves. In addition, if we
171 don't use a separate worklist for VARYING edges, we end up with
172 situations where lattice values move from
173 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
174 static vec<gimple> varying_ssa_edges;
177 /* Return true if the block worklist empty. */
179 static inline bool
180 cfg_blocks_empty_p (void)
182 return (cfg_blocks_num == 0);
186 /* Add a basic block to the worklist. The block must not be already
187 in the worklist, and it must not be the ENTRY or EXIT block. */
189 static void
190 cfg_blocks_add (basic_block bb)
192 bool head = false;
194 gcc_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
195 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
196 gcc_assert (!bitmap_bit_p (bb_in_list, bb->index));
198 if (cfg_blocks_empty_p ())
200 cfg_blocks_tail = cfg_blocks_head = 0;
201 cfg_blocks_num = 1;
203 else
205 cfg_blocks_num++;
206 if (cfg_blocks_num > cfg_blocks.length ())
208 /* We have to grow the array now. Adjust to queue to occupy
209 the full space of the original array. We do not need to
210 initialize the newly allocated portion of the array
211 because we keep track of CFG_BLOCKS_HEAD and
212 CFG_BLOCKS_HEAD. */
213 cfg_blocks_tail = cfg_blocks.length ();
214 cfg_blocks_head = 0;
215 cfg_blocks.safe_grow (2 * cfg_blocks_tail);
217 /* Minor optimization: we prefer to see blocks with more
218 predecessors later, because there is more of a chance that
219 the incoming edges will be executable. */
220 else if (EDGE_COUNT (bb->preds)
221 >= EDGE_COUNT (cfg_blocks[cfg_blocks_head]->preds))
222 cfg_blocks_tail = ((cfg_blocks_tail + 1) % cfg_blocks.length ());
223 else
225 if (cfg_blocks_head == 0)
226 cfg_blocks_head = cfg_blocks.length ();
227 --cfg_blocks_head;
228 head = true;
232 cfg_blocks[head ? cfg_blocks_head : cfg_blocks_tail] = bb;
233 bitmap_set_bit (bb_in_list, bb->index);
237 /* Remove a block from the worklist. */
239 static basic_block
240 cfg_blocks_get (void)
242 basic_block bb;
244 bb = cfg_blocks[cfg_blocks_head];
246 gcc_assert (!cfg_blocks_empty_p ());
247 gcc_assert (bb);
249 cfg_blocks_head = ((cfg_blocks_head + 1) % cfg_blocks.length ());
250 --cfg_blocks_num;
251 bitmap_clear_bit (bb_in_list, bb->index);
253 return bb;
257 /* We have just defined a new value for VAR. If IS_VARYING is true,
258 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
259 them to INTERESTING_SSA_EDGES. */
261 static void
262 add_ssa_edge (tree var, bool is_varying)
264 imm_use_iterator iter;
265 use_operand_p use_p;
267 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
269 gimple use_stmt = USE_STMT (use_p);
271 if (prop_simulate_again_p (use_stmt)
272 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
274 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
275 if (is_varying)
276 varying_ssa_edges.safe_push (use_stmt);
277 else
278 interesting_ssa_edges.safe_push (use_stmt);
284 /* Add edge E to the control flow worklist. */
286 static void
287 add_control_edge (edge e)
289 basic_block bb = e->dest;
290 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
291 return;
293 /* If the edge had already been executed, skip it. */
294 if (e->flags & EDGE_EXECUTABLE)
295 return;
297 e->flags |= EDGE_EXECUTABLE;
299 /* If the block is already in the list, we're done. */
300 if (bitmap_bit_p (bb_in_list, bb->index))
301 return;
303 cfg_blocks_add (bb);
305 if (dump_file && (dump_flags & TDF_DETAILS))
306 fprintf (dump_file, "\nAdding Destination of edge (%d -> %d) to worklist\n",
307 e->src->index, e->dest->index);
311 /* Simulate the execution of STMT and update the work lists accordingly. */
313 static void
314 simulate_stmt (gimple stmt)
316 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
317 edge taken_edge = NULL;
318 tree output_name = NULL_TREE;
320 /* Don't bother visiting statements that are already
321 considered varying by the propagator. */
322 if (!prop_simulate_again_p (stmt))
323 return;
325 if (gimple_code (stmt) == GIMPLE_PHI)
327 val = ssa_prop_visit_phi (as_a <gimple_phi> (stmt));
328 output_name = gimple_phi_result (stmt);
330 else
331 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
333 if (val == SSA_PROP_VARYING)
335 prop_set_simulate_again (stmt, false);
337 /* If the statement produced a new varying value, add the SSA
338 edges coming out of OUTPUT_NAME. */
339 if (output_name)
340 add_ssa_edge (output_name, true);
342 /* If STMT transfers control out of its basic block, add
343 all outgoing edges to the work list. */
344 if (stmt_ends_bb_p (stmt))
346 edge e;
347 edge_iterator ei;
348 basic_block bb = gimple_bb (stmt);
349 FOR_EACH_EDGE (e, ei, bb->succs)
350 add_control_edge (e);
353 else if (val == SSA_PROP_INTERESTING)
355 /* If the statement produced new value, add the SSA edges coming
356 out of OUTPUT_NAME. */
357 if (output_name)
358 add_ssa_edge (output_name, false);
360 /* If we know which edge is going to be taken out of this block,
361 add it to the CFG work list. */
362 if (taken_edge)
363 add_control_edge (taken_edge);
367 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
368 drain. This pops statements off the given WORKLIST and processes
369 them until there are no more statements on WORKLIST.
370 We take a pointer to WORKLIST because it may be reallocated when an
371 SSA edge is added to it in simulate_stmt. */
373 static void
374 process_ssa_edge_worklist (vec<gimple> *worklist)
376 /* Drain the entire worklist. */
377 while (worklist->length () > 0)
379 basic_block bb;
381 /* Pull the statement to simulate off the worklist. */
382 gimple stmt = worklist->pop ();
384 /* If this statement was already visited by simulate_block, then
385 we don't need to visit it again here. */
386 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
387 continue;
389 /* STMT is no longer in a worklist. */
390 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
392 if (dump_file && (dump_flags & TDF_DETAILS))
394 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
395 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
398 bb = gimple_bb (stmt);
400 /* PHI nodes are always visited, regardless of whether or not
401 the destination block is executable. Otherwise, visit the
402 statement only if its block is marked executable. */
403 if (gimple_code (stmt) == GIMPLE_PHI
404 || bitmap_bit_p (executable_blocks, bb->index))
405 simulate_stmt (stmt);
410 /* Simulate the execution of BLOCK. Evaluate the statement associated
411 with each variable reference inside the block. */
413 static void
414 simulate_block (basic_block block)
416 gimple_stmt_iterator gsi;
418 /* There is nothing to do for the exit block. */
419 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
420 return;
422 if (dump_file && (dump_flags & TDF_DETAILS))
423 fprintf (dump_file, "\nSimulating block %d\n", block->index);
425 /* Always simulate PHI nodes, even if we have simulated this block
426 before. */
427 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
428 simulate_stmt (gsi_stmt (gsi));
430 /* If this is the first time we've simulated this block, then we
431 must simulate each of its statements. */
432 if (!bitmap_bit_p (executable_blocks, block->index))
434 gimple_stmt_iterator j;
435 unsigned int normal_edge_count;
436 edge e, normal_edge;
437 edge_iterator ei;
439 /* Note that we have simulated this block. */
440 bitmap_set_bit (executable_blocks, block->index);
442 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
444 gimple stmt = gsi_stmt (j);
446 /* If this statement is already in the worklist then
447 "cancel" it. The reevaluation implied by the worklist
448 entry will produce the same value we generate here and
449 thus reevaluating it again from the worklist is
450 pointless. */
451 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
452 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
454 simulate_stmt (stmt);
457 /* We can not predict when abnormal and EH edges will be executed, so
458 once a block is considered executable, we consider any
459 outgoing abnormal edges as executable.
461 TODO: This is not exactly true. Simplifying statement might
462 prove it non-throwing and also computed goto can be handled
463 when destination is known.
465 At the same time, if this block has only one successor that is
466 reached by non-abnormal edges, then add that successor to the
467 worklist. */
468 normal_edge_count = 0;
469 normal_edge = NULL;
470 FOR_EACH_EDGE (e, ei, block->succs)
472 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
473 add_control_edge (e);
474 else
476 normal_edge_count++;
477 normal_edge = e;
481 if (normal_edge_count == 1)
482 add_control_edge (normal_edge);
487 /* Initialize local data structures and work lists. */
489 static void
490 ssa_prop_init (void)
492 edge e;
493 edge_iterator ei;
494 basic_block bb;
496 /* Worklists of SSA edges. */
497 interesting_ssa_edges.create (20);
498 varying_ssa_edges.create (20);
500 executable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
501 bitmap_clear (executable_blocks);
503 bb_in_list = sbitmap_alloc (last_basic_block_for_fn (cfun));
504 bitmap_clear (bb_in_list);
506 if (dump_file && (dump_flags & TDF_DETAILS))
507 dump_immediate_uses (dump_file);
509 cfg_blocks.create (20);
510 cfg_blocks.safe_grow_cleared (20);
512 /* Initially assume that every edge in the CFG is not executable.
513 (including the edges coming out of the entry block). */
514 FOR_ALL_BB_FN (bb, cfun)
516 gimple_stmt_iterator si;
518 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
519 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
521 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
522 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
524 FOR_EACH_EDGE (e, ei, bb->succs)
525 e->flags &= ~EDGE_EXECUTABLE;
528 /* Seed the algorithm by adding the successors of the entry block to the
529 edge worklist. */
530 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
531 add_control_edge (e);
535 /* Free allocated storage. */
537 static void
538 ssa_prop_fini (void)
540 interesting_ssa_edges.release ();
541 varying_ssa_edges.release ();
542 cfg_blocks.release ();
543 sbitmap_free (bb_in_list);
544 sbitmap_free (executable_blocks);
548 /* Return true if EXPR is an acceptable right-hand-side for a
549 GIMPLE assignment. We validate the entire tree, not just
550 the root node, thus catching expressions that embed complex
551 operands that are not permitted in GIMPLE. This function
552 is needed because the folding routines in fold-const.c
553 may return such expressions in some cases, e.g., an array
554 access with an embedded index addition. It may make more
555 sense to have folding routines that are sensitive to the
556 constraints on GIMPLE operands, rather than abandoning any
557 any attempt to fold if the usual folding turns out to be too
558 aggressive. */
560 bool
561 valid_gimple_rhs_p (tree expr)
563 enum tree_code code = TREE_CODE (expr);
565 switch (TREE_CODE_CLASS (code))
567 case tcc_declaration:
568 if (!is_gimple_variable (expr))
569 return false;
570 break;
572 case tcc_constant:
573 /* All constants are ok. */
574 break;
576 case tcc_comparison:
577 /* GENERIC allows comparisons with non-boolean types, reject
578 those for GIMPLE. Let vector-typed comparisons pass - rules
579 for GENERIC and GIMPLE are the same here. */
580 if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
581 && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
582 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
583 && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
584 return false;
586 /* Fallthru. */
587 case tcc_binary:
588 if (!is_gimple_val (TREE_OPERAND (expr, 0))
589 || !is_gimple_val (TREE_OPERAND (expr, 1)))
590 return false;
591 break;
593 case tcc_unary:
594 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
595 return false;
596 break;
598 case tcc_expression:
599 switch (code)
601 case ADDR_EXPR:
603 tree t;
604 if (is_gimple_min_invariant (expr))
605 return true;
606 t = TREE_OPERAND (expr, 0);
607 while (handled_component_p (t))
609 /* ??? More checks needed, see the GIMPLE verifier. */
610 if ((TREE_CODE (t) == ARRAY_REF
611 || TREE_CODE (t) == ARRAY_RANGE_REF)
612 && !is_gimple_val (TREE_OPERAND (t, 1)))
613 return false;
614 t = TREE_OPERAND (t, 0);
616 if (!is_gimple_id (t))
617 return false;
619 break;
621 default:
622 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
624 if (((code == VEC_COND_EXPR || code == COND_EXPR)
625 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
626 : !is_gimple_val (TREE_OPERAND (expr, 0)))
627 || !is_gimple_val (TREE_OPERAND (expr, 1))
628 || !is_gimple_val (TREE_OPERAND (expr, 2)))
629 return false;
630 break;
632 return false;
634 break;
636 case tcc_vl_exp:
637 return false;
639 case tcc_exceptional:
640 if (code == CONSTRUCTOR)
642 unsigned i;
643 tree elt;
644 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
645 if (!is_gimple_val (elt))
646 return false;
647 return true;
649 if (code != SSA_NAME)
650 return false;
651 break;
653 case tcc_reference:
654 if (code == BIT_FIELD_REF)
655 return is_gimple_val (TREE_OPERAND (expr, 0));
656 return false;
658 default:
659 return false;
662 return true;
666 /* Return true if EXPR is a CALL_EXPR suitable for representation
667 as a single GIMPLE_CALL statement. If the arguments require
668 further gimplification, return false. */
670 static bool
671 valid_gimple_call_p (tree expr)
673 unsigned i, nargs;
675 if (TREE_CODE (expr) != CALL_EXPR)
676 return false;
678 nargs = call_expr_nargs (expr);
679 for (i = 0; i < nargs; i++)
681 tree arg = CALL_EXPR_ARG (expr, i);
682 if (is_gimple_reg_type (TREE_TYPE (arg)))
684 if (!is_gimple_val (arg))
685 return false;
687 else
688 if (!is_gimple_lvalue (arg))
689 return false;
692 return true;
696 /* Make SSA names defined by OLD_STMT point to NEW_STMT
697 as their defining statement. */
699 void
700 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
702 tree var;
703 ssa_op_iter iter;
705 if (gimple_in_ssa_p (cfun))
707 /* Make defined SSA_NAMEs point to the new
708 statement as their definition. */
709 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
711 if (TREE_CODE (var) == SSA_NAME)
712 SSA_NAME_DEF_STMT (var) = new_stmt;
717 /* Helper function for update_gimple_call and update_call_from_tree.
718 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
720 static void
721 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
722 gimple stmt)
724 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
725 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
726 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
727 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
728 gimple_set_location (new_stmt, gimple_location (stmt));
729 if (gimple_block (new_stmt) == NULL_TREE)
730 gimple_set_block (new_stmt, gimple_block (stmt));
731 gsi_replace (si_p, new_stmt, false);
734 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
735 with number of arguments NARGS, where the arguments in GIMPLE form
736 follow NARGS argument. */
738 bool
739 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
741 va_list ap;
742 gimple_call new_stmt, stmt = as_a <gimple_call> (gsi_stmt (*si_p));
744 gcc_assert (is_gimple_call (stmt));
745 va_start (ap, nargs);
746 new_stmt = gimple_build_call_valist (fn, nargs, ap);
747 finish_update_gimple_call (si_p, new_stmt, stmt);
748 va_end (ap);
749 return true;
752 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
753 value of EXPR, which is expected to be the result of folding the
754 call. This can only be done if EXPR is a CALL_EXPR with valid
755 GIMPLE operands as arguments, or if it is a suitable RHS expression
756 for a GIMPLE_ASSIGN. More complex expressions will require
757 gimplification, which will introduce additional statements. In this
758 event, no update is performed, and the function returns false.
759 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
760 replace the statement at *SI_P with an entirely new statement.
761 The new statement need not be a call, e.g., if the original call
762 folded to a constant. */
764 bool
765 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
767 gimple stmt = gsi_stmt (*si_p);
769 if (valid_gimple_call_p (expr))
771 /* The call has simplified to another call. */
772 tree fn = CALL_EXPR_FN (expr);
773 unsigned i;
774 unsigned nargs = call_expr_nargs (expr);
775 vec<tree> args = vNULL;
776 gimple_call new_stmt;
778 if (nargs > 0)
780 args.create (nargs);
781 args.safe_grow_cleared (nargs);
783 for (i = 0; i < nargs; i++)
784 args[i] = CALL_EXPR_ARG (expr, i);
787 new_stmt = gimple_build_call_vec (fn, args);
788 finish_update_gimple_call (si_p, new_stmt, stmt);
789 args.release ();
791 return true;
793 else if (valid_gimple_rhs_p (expr))
795 tree lhs = gimple_call_lhs (stmt);
796 gimple new_stmt;
798 /* The call has simplified to an expression
799 that cannot be represented as a GIMPLE_CALL. */
800 if (lhs)
802 /* A value is expected.
803 Introduce a new GIMPLE_ASSIGN statement. */
804 STRIP_USELESS_TYPE_CONVERSION (expr);
805 new_stmt = gimple_build_assign (lhs, expr);
806 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
807 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
808 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
810 else if (!TREE_SIDE_EFFECTS (expr))
812 /* No value is expected, and EXPR has no effect.
813 Replace it with an empty statement. */
814 new_stmt = gimple_build_nop ();
815 if (gimple_in_ssa_p (cfun))
817 unlink_stmt_vdef (stmt);
818 release_defs (stmt);
821 else
823 /* No value is expected, but EXPR has an effect,
824 e.g., it could be a reference to a volatile
825 variable. Create an assignment statement
826 with a dummy (unused) lhs variable. */
827 STRIP_USELESS_TYPE_CONVERSION (expr);
828 if (gimple_in_ssa_p (cfun))
829 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
830 else
831 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
832 new_stmt = gimple_build_assign (lhs, expr);
833 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
834 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
835 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
837 gimple_set_location (new_stmt, gimple_location (stmt));
838 gsi_replace (si_p, new_stmt, false);
839 return true;
841 else
842 /* The call simplified to an expression that is
843 not a valid GIMPLE RHS. */
844 return false;
848 /* Entry point to the propagation engine.
850 VISIT_STMT is called for every statement visited.
851 VISIT_PHI is called for every PHI node visited. */
853 void
854 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
855 ssa_prop_visit_phi_fn visit_phi)
857 ssa_prop_visit_stmt = visit_stmt;
858 ssa_prop_visit_phi = visit_phi;
860 ssa_prop_init ();
862 /* Iterate until the worklists are empty. */
863 while (!cfg_blocks_empty_p ()
864 || interesting_ssa_edges.length () > 0
865 || varying_ssa_edges.length () > 0)
867 if (!cfg_blocks_empty_p ())
869 /* Pull the next block to simulate off the worklist. */
870 basic_block dest_block = cfg_blocks_get ();
871 simulate_block (dest_block);
874 /* In order to move things to varying as quickly as
875 possible,process the VARYING_SSA_EDGES worklist first. */
876 process_ssa_edge_worklist (&varying_ssa_edges);
878 /* Now process the INTERESTING_SSA_EDGES worklist. */
879 process_ssa_edge_worklist (&interesting_ssa_edges);
882 ssa_prop_fini ();
886 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
887 is a non-volatile pointer dereference, a structure reference or a
888 reference to a single _DECL. Ignore volatile memory references
889 because they are not interesting for the optimizers. */
891 bool
892 stmt_makes_single_store (gimple stmt)
894 tree lhs;
896 if (gimple_code (stmt) != GIMPLE_ASSIGN
897 && gimple_code (stmt) != GIMPLE_CALL)
898 return false;
900 if (!gimple_vdef (stmt))
901 return false;
903 lhs = gimple_get_lhs (stmt);
905 /* A call statement may have a null LHS. */
906 if (!lhs)
907 return false;
909 return (!TREE_THIS_VOLATILE (lhs)
910 && (DECL_P (lhs)
911 || REFERENCE_CLASS_P (lhs)));
915 /* Propagation statistics. */
916 struct prop_stats_d
918 long num_const_prop;
919 long num_copy_prop;
920 long num_stmts_folded;
921 long num_dce;
924 static struct prop_stats_d prop_stats;
926 /* Replace USE references in statement STMT with the values stored in
927 PROP_VALUE. Return true if at least one reference was replaced. */
929 static bool
930 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
932 bool replaced = false;
933 use_operand_p use;
934 ssa_op_iter iter;
936 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
938 tree tuse = USE_FROM_PTR (use);
939 tree val = (*get_value) (tuse);
941 if (val == tuse || val == NULL_TREE)
942 continue;
944 if (gimple_code (stmt) == GIMPLE_ASM
945 && !may_propagate_copy_into_asm (tuse))
946 continue;
948 if (!may_propagate_copy (tuse, val))
949 continue;
951 if (TREE_CODE (val) != SSA_NAME)
952 prop_stats.num_const_prop++;
953 else
954 prop_stats.num_copy_prop++;
956 propagate_value (use, val);
958 replaced = true;
961 return replaced;
965 /* Replace propagated values into all the arguments for PHI using the
966 values from PROP_VALUE. */
968 static bool
969 replace_phi_args_in (gimple_phi phi, ssa_prop_get_value_fn get_value)
971 size_t i;
972 bool replaced = false;
974 if (dump_file && (dump_flags & TDF_DETAILS))
976 fprintf (dump_file, "Folding PHI node: ");
977 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
980 for (i = 0; i < gimple_phi_num_args (phi); i++)
982 tree arg = gimple_phi_arg_def (phi, i);
984 if (TREE_CODE (arg) == SSA_NAME)
986 tree val = (*get_value) (arg);
988 if (val && val != arg && may_propagate_copy (arg, val))
990 if (TREE_CODE (val) != SSA_NAME)
991 prop_stats.num_const_prop++;
992 else
993 prop_stats.num_copy_prop++;
995 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
996 replaced = true;
998 /* If we propagated a copy and this argument flows
999 through an abnormal edge, update the replacement
1000 accordingly. */
1001 if (TREE_CODE (val) == SSA_NAME
1002 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
1003 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1008 if (dump_file && (dump_flags & TDF_DETAILS))
1010 if (!replaced)
1011 fprintf (dump_file, "No folding possible\n");
1012 else
1014 fprintf (dump_file, "Folded into: ");
1015 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1016 fprintf (dump_file, "\n");
1020 return replaced;
1024 class substitute_and_fold_dom_walker : public dom_walker
1026 public:
1027 substitute_and_fold_dom_walker (cdi_direction direction,
1028 ssa_prop_get_value_fn get_value_fn_,
1029 ssa_prop_fold_stmt_fn fold_fn_,
1030 bool do_dce_)
1031 : dom_walker (direction), get_value_fn (get_value_fn_),
1032 fold_fn (fold_fn_), do_dce (do_dce_), something_changed (false)
1034 stmts_to_remove.create (0);
1035 need_eh_cleanup = BITMAP_ALLOC (NULL);
1037 ~substitute_and_fold_dom_walker ()
1039 stmts_to_remove.release ();
1040 BITMAP_FREE (need_eh_cleanup);
1043 virtual void before_dom_children (basic_block);
1044 virtual void after_dom_children (basic_block) {}
1046 ssa_prop_get_value_fn get_value_fn;
1047 ssa_prop_fold_stmt_fn fold_fn;
1048 bool do_dce;
1049 bool something_changed;
1050 vec<gimple> stmts_to_remove;
1051 bitmap need_eh_cleanup;
1054 void
1055 substitute_and_fold_dom_walker::before_dom_children (basic_block bb)
1057 /* Propagate known values into PHI nodes. */
1058 for (gimple_phi_iterator i = gsi_start_phis (bb);
1059 !gsi_end_p (i);
1060 gsi_next (&i))
1062 gimple_phi phi = i.phi ();
1063 tree res = gimple_phi_result (phi);
1064 if (virtual_operand_p (res))
1065 continue;
1066 if (do_dce
1067 && res && TREE_CODE (res) == SSA_NAME)
1069 tree sprime = get_value_fn (res);
1070 if (sprime
1071 && sprime != res
1072 && may_propagate_copy (res, sprime))
1074 stmts_to_remove.safe_push (phi);
1075 continue;
1078 something_changed |= replace_phi_args_in (phi, get_value_fn);
1081 /* Propagate known values into stmts. In some case it exposes
1082 more trivially deletable stmts to walk backward. */
1083 for (gimple_stmt_iterator i = gsi_start_bb (bb);
1084 !gsi_end_p (i);
1085 gsi_next (&i))
1087 bool did_replace;
1088 gimple stmt = gsi_stmt (i);
1089 gimple old_stmt;
1090 enum gimple_code code = gimple_code (stmt);
1092 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1093 range information for names and they are discarded
1094 afterwards. */
1096 if (code == GIMPLE_ASSIGN
1097 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1098 continue;
1100 /* No point propagating into a stmt we have a value for we
1101 can propagate into all uses. Mark it for removal instead. */
1102 tree lhs = gimple_get_lhs (stmt);
1103 if (do_dce
1104 && lhs && TREE_CODE (lhs) == SSA_NAME)
1106 tree sprime = get_value_fn (lhs);
1107 if (sprime
1108 && sprime != lhs
1109 && may_propagate_copy (lhs, sprime)
1110 && !stmt_could_throw_p (stmt)
1111 && !gimple_has_side_effects (stmt))
1113 stmts_to_remove.safe_push (stmt);
1114 continue;
1118 /* Replace the statement with its folded version and mark it
1119 folded. */
1120 did_replace = false;
1121 if (dump_file && (dump_flags & TDF_DETAILS))
1123 fprintf (dump_file, "Folding statement: ");
1124 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1127 old_stmt = stmt;
1129 /* Some statements may be simplified using propagator
1130 specific information. Do this before propagating
1131 into the stmt to not disturb pass specific information. */
1132 if (fold_fn
1133 && (*fold_fn)(&i))
1135 did_replace = true;
1136 prop_stats.num_stmts_folded++;
1137 stmt = gsi_stmt (i);
1138 update_stmt (stmt);
1141 /* Replace real uses in the statement. */
1142 did_replace |= replace_uses_in (stmt, get_value_fn);
1144 /* If we made a replacement, fold the statement. */
1145 if (did_replace)
1146 fold_stmt (&i);
1148 /* Now cleanup. */
1149 if (did_replace)
1151 stmt = gsi_stmt (i);
1153 /* If we cleaned up EH information from the statement,
1154 remove EH edges. */
1155 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1156 bitmap_set_bit (need_eh_cleanup, bb->index);
1158 if (is_gimple_assign (stmt)
1159 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1160 == GIMPLE_SINGLE_RHS))
1162 tree rhs = gimple_assign_rhs1 (stmt);
1164 if (TREE_CODE (rhs) == ADDR_EXPR)
1165 recompute_tree_invariant_for_addr_expr (rhs);
1168 /* Determine what needs to be done to update the SSA form. */
1169 update_stmt (stmt);
1170 if (!is_gimple_debug (stmt))
1171 something_changed = true;
1174 if (dump_file && (dump_flags & TDF_DETAILS))
1176 if (did_replace)
1178 fprintf (dump_file, "Folded into: ");
1179 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1180 fprintf (dump_file, "\n");
1182 else
1183 fprintf (dump_file, "Not folded\n");
1190 /* Perform final substitution and folding of propagated values.
1192 PROP_VALUE[I] contains the single value that should be substituted
1193 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1194 substituted.
1196 If FOLD_FN is non-NULL the function will be invoked on all statements
1197 before propagating values for pass specific simplification.
1199 DO_DCE is true if trivially dead stmts can be removed.
1201 If DO_DCE is true, the statements within a BB are walked from
1202 last to first element. Otherwise we scan from first to last element.
1204 Return TRUE when something changed. */
1206 bool
1207 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1208 ssa_prop_fold_stmt_fn fold_fn,
1209 bool do_dce)
1211 gcc_assert (get_value_fn);
1213 if (dump_file && (dump_flags & TDF_DETAILS))
1214 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1216 memset (&prop_stats, 0, sizeof (prop_stats));
1218 calculate_dominance_info (CDI_DOMINATORS);
1219 substitute_and_fold_dom_walker walker(CDI_DOMINATORS,
1220 get_value_fn, fold_fn, do_dce);
1221 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1223 /* We cannot remove stmts during the BB walk, especially not release
1224 SSA names there as that destroys the lattice of our callers.
1225 Remove stmts in reverse order to make debug stmt creation possible. */
1226 while (!walker.stmts_to_remove.is_empty ())
1228 gimple stmt = walker.stmts_to_remove.pop ();
1229 if (dump_file && dump_flags & TDF_DETAILS)
1231 fprintf (dump_file, "Removing dead stmt ");
1232 print_gimple_stmt (dump_file, stmt, 0, 0);
1233 fprintf (dump_file, "\n");
1235 prop_stats.num_dce++;
1236 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1237 if (gimple_code (stmt) == GIMPLE_PHI)
1238 remove_phi_node (&gsi, true);
1239 else
1241 unlink_stmt_vdef (stmt);
1242 gsi_remove (&gsi, true);
1243 release_defs (stmt);
1247 if (!bitmap_empty_p (walker.need_eh_cleanup))
1248 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
1250 statistics_counter_event (cfun, "Constants propagated",
1251 prop_stats.num_const_prop);
1252 statistics_counter_event (cfun, "Copies propagated",
1253 prop_stats.num_copy_prop);
1254 statistics_counter_event (cfun, "Statements folded",
1255 prop_stats.num_stmts_folded);
1256 statistics_counter_event (cfun, "Statements deleted",
1257 prop_stats.num_dce);
1259 return walker.something_changed;
1263 /* Return true if we may propagate ORIG into DEST, false otherwise. */
1265 bool
1266 may_propagate_copy (tree dest, tree orig)
1268 tree type_d = TREE_TYPE (dest);
1269 tree type_o = TREE_TYPE (orig);
1271 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
1272 if (TREE_CODE (orig) == SSA_NAME
1273 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
1274 /* If it is the default definition and an automatic variable then
1275 we can though and it is important that we do to avoid
1276 uninitialized regular copies. */
1277 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
1278 && (SSA_NAME_VAR (orig) == NULL_TREE
1279 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
1280 return false;
1282 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
1283 cannot be replaced. */
1284 if (TREE_CODE (dest) == SSA_NAME
1285 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
1286 return false;
1288 /* Do not copy between types for which we *do* need a conversion. */
1289 if (!useless_type_conversion_p (type_d, type_o))
1290 return false;
1292 /* Generally propagating virtual operands is not ok as that may
1293 create overlapping life-ranges. */
1294 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
1295 return false;
1297 /* Anything else is OK. */
1298 return true;
1301 /* Like may_propagate_copy, but use as the destination expression
1302 the principal expression (typically, the RHS) contained in
1303 statement DEST. This is more efficient when working with the
1304 gimple tuples representation. */
1306 bool
1307 may_propagate_copy_into_stmt (gimple dest, tree orig)
1309 tree type_d;
1310 tree type_o;
1312 /* If the statement is a switch or a single-rhs assignment,
1313 then the expression to be replaced by the propagation may
1314 be an SSA_NAME. Fortunately, there is an explicit tree
1315 for the expression, so we delegate to may_propagate_copy. */
1317 if (gimple_assign_single_p (dest))
1318 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
1319 else if (gimple_switch dest_swtch = dyn_cast <gimple_switch> (dest))
1320 return may_propagate_copy (gimple_switch_index (dest_swtch), orig);
1322 /* In other cases, the expression is not materialized, so there
1323 is no destination to pass to may_propagate_copy. On the other
1324 hand, the expression cannot be an SSA_NAME, so the analysis
1325 is much simpler. */
1327 if (TREE_CODE (orig) == SSA_NAME
1328 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
1329 return false;
1331 if (is_gimple_assign (dest))
1332 type_d = TREE_TYPE (gimple_assign_lhs (dest));
1333 else if (gimple_code (dest) == GIMPLE_COND)
1334 type_d = boolean_type_node;
1335 else if (is_gimple_call (dest)
1336 && gimple_call_lhs (dest) != NULL_TREE)
1337 type_d = TREE_TYPE (gimple_call_lhs (dest));
1338 else
1339 gcc_unreachable ();
1341 type_o = TREE_TYPE (orig);
1343 if (!useless_type_conversion_p (type_d, type_o))
1344 return false;
1346 return true;
1349 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
1351 bool
1352 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
1354 return true;
1358 /* Common code for propagate_value and replace_exp.
1360 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
1361 replacement is done to propagate a value or not. */
1363 static void
1364 replace_exp_1 (use_operand_p op_p, tree val,
1365 bool for_propagation ATTRIBUTE_UNUSED)
1367 #if defined ENABLE_CHECKING
1368 tree op = USE_FROM_PTR (op_p);
1370 gcc_assert (!(for_propagation
1371 && TREE_CODE (op) == SSA_NAME
1372 && TREE_CODE (val) == SSA_NAME
1373 && !may_propagate_copy (op, val)));
1374 #endif
1376 if (TREE_CODE (val) == SSA_NAME)
1377 SET_USE (op_p, val);
1378 else
1379 SET_USE (op_p, unshare_expr (val));
1383 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1384 into the operand pointed to by OP_P.
1386 Use this version for const/copy propagation as it will perform additional
1387 checks to ensure validity of the const/copy propagation. */
1389 void
1390 propagate_value (use_operand_p op_p, tree val)
1392 replace_exp_1 (op_p, val, true);
1395 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
1397 Use this version when not const/copy propagating values. For example,
1398 PRE uses this version when building expressions as they would appear
1399 in specific blocks taking into account actions of PHI nodes.
1401 The statement in which an expression has been replaced should be
1402 folded using fold_stmt_inplace. */
1404 void
1405 replace_exp (use_operand_p op_p, tree val)
1407 replace_exp_1 (op_p, val, false);
1411 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
1412 into the tree pointed to by OP_P.
1414 Use this version for const/copy propagation when SSA operands are not
1415 available. It will perform the additional checks to ensure validity of
1416 the const/copy propagation, but will not update any operand information.
1417 Be sure to mark the stmt as modified. */
1419 void
1420 propagate_tree_value (tree *op_p, tree val)
1422 if (TREE_CODE (val) == SSA_NAME)
1423 *op_p = val;
1424 else
1425 *op_p = unshare_expr (val);
1429 /* Like propagate_tree_value, but use as the operand to replace
1430 the principal expression (typically, the RHS) contained in the
1431 statement referenced by iterator GSI. Note that it is not
1432 always possible to update the statement in-place, so a new
1433 statement may be created to replace the original. */
1435 void
1436 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
1438 gimple stmt = gsi_stmt (*gsi);
1440 if (is_gimple_assign (stmt))
1442 tree expr = NULL_TREE;
1443 if (gimple_assign_single_p (stmt))
1444 expr = gimple_assign_rhs1 (stmt);
1445 propagate_tree_value (&expr, val);
1446 gimple_assign_set_rhs_from_tree (gsi, expr);
1448 else if (gimple_cond cond_stmt = dyn_cast <gimple_cond> (stmt))
1450 tree lhs = NULL_TREE;
1451 tree rhs = build_zero_cst (TREE_TYPE (val));
1452 propagate_tree_value (&lhs, val);
1453 gimple_cond_set_code (cond_stmt, NE_EXPR);
1454 gimple_cond_set_lhs (cond_stmt, lhs);
1455 gimple_cond_set_rhs (cond_stmt, rhs);
1457 else if (is_gimple_call (stmt)
1458 && gimple_call_lhs (stmt) != NULL_TREE)
1460 tree expr = NULL_TREE;
1461 bool res;
1462 propagate_tree_value (&expr, val);
1463 res = update_call_from_tree (gsi, expr);
1464 gcc_assert (res);
1466 else if (gimple_switch swtch_stmt = dyn_cast <gimple_switch> (stmt))
1467 propagate_tree_value (gimple_switch_index_ptr (swtch_stmt), val);
1468 else
1469 gcc_unreachable ();