2010-05-14 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-propagate.c
blob7c8ec4527b9887a551b7ee63044b1af71545920e
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 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 "rtl.h"
28 #include "tm_p.h"
29 #include "ggc.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "function.h"
34 #include "diagnostic.h"
35 #include "timevar.h"
36 #include "tree-dump.h"
37 #include "tree-flow.h"
38 #include "tree-pass.h"
39 #include "tree-ssa-propagate.h"
40 #include "langhooks.h"
41 #include "vec.h"
42 #include "value-prof.h"
43 #include "gimple.h"
45 /* This file implements a generic value propagation engine based on
46 the same propagation used by the SSA-CCP algorithm [1].
48 Propagation is performed by simulating the execution of every
49 statement that produces the value being propagated. Simulation
50 proceeds as follows:
52 1- Initially, all edges of the CFG are marked not executable and
53 the CFG worklist is seeded with all the statements in the entry
54 basic block (block 0).
56 2- Every statement S is simulated with a call to the call-back
57 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
58 results:
60 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
61 interest and does not affect any of the work lists.
63 SSA_PROP_VARYING: The value produced by S cannot be determined
64 at compile time. Further simulation of S is not required.
65 If S is a conditional jump, all the outgoing edges for the
66 block are considered executable and added to the work
67 list.
69 SSA_PROP_INTERESTING: S produces a value that can be computed
70 at compile time. Its result can be propagated into the
71 statements that feed from S. Furthermore, if S is a
72 conditional jump, only the edge known to be taken is added
73 to the work list. Edges that are known not to execute are
74 never simulated.
76 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
77 return value from SSA_PROP_VISIT_PHI has the same semantics as
78 described in #2.
80 4- Three work lists are kept. Statements are only added to these
81 lists if they produce one of SSA_PROP_INTERESTING or
82 SSA_PROP_VARYING.
84 CFG_BLOCKS contains the list of blocks to be simulated.
85 Blocks are added to this list if their incoming edges are
86 found executable.
88 VARYING_SSA_EDGES contains the list of statements that feed
89 from statements that produce an SSA_PROP_VARYING result.
90 These are simulated first to speed up processing.
92 INTERESTING_SSA_EDGES contains the list of statements that
93 feed from statements that produce an SSA_PROP_INTERESTING
94 result.
96 5- Simulation terminates when all three work lists are drained.
98 Before calling ssa_propagate, it is important to clear
99 prop_simulate_again_p for all the statements in the program that
100 should be simulated. This initialization allows an implementation
101 to specify which statements should never be simulated.
103 It is also important to compute def-use information before calling
104 ssa_propagate.
106 References:
108 [1] Constant propagation with conditional branches,
109 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
111 [2] Building an Optimizing Compiler,
112 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
114 [3] Advanced Compiler Design and Implementation,
115 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
117 /* Function pointers used to parameterize the propagation engine. */
118 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
119 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
121 /* Keep track of statements that have been added to one of the SSA
122 edges worklists. This flag is used to avoid visiting statements
123 unnecessarily when draining an SSA edge worklist. If while
124 simulating a basic block, we find a statement with
125 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
126 processing from visiting it again.
128 NOTE: users of the propagation engine are not allowed to use
129 the GF_PLF_1 flag. */
130 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
132 /* A bitmap to keep track of executable blocks in the CFG. */
133 static sbitmap executable_blocks;
135 /* Array of control flow edges on the worklist. */
136 static VEC(basic_block,heap) *cfg_blocks;
138 static unsigned int cfg_blocks_num = 0;
139 static int cfg_blocks_tail;
140 static int cfg_blocks_head;
142 static sbitmap bb_in_list;
144 /* Worklist of SSA edges which will need reexamination as their
145 definition has changed. SSA edges are def-use edges in the SSA
146 web. For each D-U edge, we store the target statement or PHI node
147 U. */
148 static GTY(()) VEC(gimple,gc) *interesting_ssa_edges;
150 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
151 list of SSA edges is split into two. One contains all SSA edges
152 who need to be reexamined because their lattice value changed to
153 varying (this worklist), and the other contains all other SSA edges
154 to be reexamined (INTERESTING_SSA_EDGES).
156 Since most values in the program are VARYING, the ideal situation
157 is to move them to that lattice value as quickly as possible.
158 Thus, it doesn't make sense to process any other type of lattice
159 value until all VARYING values are propagated fully, which is one
160 thing using the VARYING worklist achieves. In addition, if we
161 don't use a separate worklist for VARYING edges, we end up with
162 situations where lattice values move from
163 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
164 static GTY(()) VEC(gimple,gc) *varying_ssa_edges;
167 /* Return true if the block worklist empty. */
169 static inline bool
170 cfg_blocks_empty_p (void)
172 return (cfg_blocks_num == 0);
176 /* Add a basic block to the worklist. The block must not be already
177 in the worklist, and it must not be the ENTRY or EXIT block. */
179 static void
180 cfg_blocks_add (basic_block bb)
182 bool head = false;
184 gcc_assert (bb != ENTRY_BLOCK_PTR && bb != EXIT_BLOCK_PTR);
185 gcc_assert (!TEST_BIT (bb_in_list, bb->index));
187 if (cfg_blocks_empty_p ())
189 cfg_blocks_tail = cfg_blocks_head = 0;
190 cfg_blocks_num = 1;
192 else
194 cfg_blocks_num++;
195 if (cfg_blocks_num > VEC_length (basic_block, cfg_blocks))
197 /* We have to grow the array now. Adjust to queue to occupy
198 the full space of the original array. We do not need to
199 initialize the newly allocated portion of the array
200 because we keep track of CFG_BLOCKS_HEAD and
201 CFG_BLOCKS_HEAD. */
202 cfg_blocks_tail = VEC_length (basic_block, cfg_blocks);
203 cfg_blocks_head = 0;
204 VEC_safe_grow (basic_block, heap, cfg_blocks, 2 * cfg_blocks_tail);
206 /* Minor optimization: we prefer to see blocks with more
207 predecessors later, because there is more of a chance that
208 the incoming edges will be executable. */
209 else if (EDGE_COUNT (bb->preds)
210 >= EDGE_COUNT (VEC_index (basic_block, cfg_blocks,
211 cfg_blocks_head)->preds))
212 cfg_blocks_tail = ((cfg_blocks_tail + 1)
213 % VEC_length (basic_block, cfg_blocks));
214 else
216 if (cfg_blocks_head == 0)
217 cfg_blocks_head = VEC_length (basic_block, cfg_blocks);
218 --cfg_blocks_head;
219 head = true;
223 VEC_replace (basic_block, cfg_blocks,
224 head ? cfg_blocks_head : cfg_blocks_tail,
225 bb);
226 SET_BIT (bb_in_list, bb->index);
230 /* Remove a block from the worklist. */
232 static basic_block
233 cfg_blocks_get (void)
235 basic_block bb;
237 bb = VEC_index (basic_block, cfg_blocks, cfg_blocks_head);
239 gcc_assert (!cfg_blocks_empty_p ());
240 gcc_assert (bb);
242 cfg_blocks_head = ((cfg_blocks_head + 1)
243 % VEC_length (basic_block, cfg_blocks));
244 --cfg_blocks_num;
245 RESET_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)
270 VEC_safe_push (gimple, gc, varying_ssa_edges, use_stmt);
271 else
272 VEC_safe_push (gimple, gc, interesting_ssa_edges, use_stmt);
278 /* Add edge E to the control flow worklist. */
280 static void
281 add_control_edge (edge e)
283 basic_block bb = e->dest;
284 if (bb == EXIT_BLOCK_PTR)
285 return;
287 /* If the edge had already been executed, skip it. */
288 if (e->flags & EDGE_EXECUTABLE)
289 return;
291 e->flags |= EDGE_EXECUTABLE;
293 /* If the block is already in the list, we're done. */
294 if (TEST_BIT (bb_in_list, bb->index))
295 return;
297 cfg_blocks_add (bb);
299 if (dump_file && (dump_flags & TDF_DETAILS))
300 fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
301 e->src->index, e->dest->index);
305 /* Simulate the execution of STMT and update the work lists accordingly. */
307 static void
308 simulate_stmt (gimple stmt)
310 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
311 edge taken_edge = NULL;
312 tree output_name = NULL_TREE;
314 /* Don't bother visiting statements that are already
315 considered varying by the propagator. */
316 if (!prop_simulate_again_p (stmt))
317 return;
319 if (gimple_code (stmt) == GIMPLE_PHI)
321 val = ssa_prop_visit_phi (stmt);
322 output_name = gimple_phi_result (stmt);
324 else
325 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
327 if (val == SSA_PROP_VARYING)
329 prop_set_simulate_again (stmt, false);
331 /* If the statement produced a new varying value, add the SSA
332 edges coming out of OUTPUT_NAME. */
333 if (output_name)
334 add_ssa_edge (output_name, true);
336 /* If STMT transfers control out of its basic block, add
337 all outgoing edges to the work list. */
338 if (stmt_ends_bb_p (stmt))
340 edge e;
341 edge_iterator ei;
342 basic_block bb = gimple_bb (stmt);
343 FOR_EACH_EDGE (e, ei, bb->succs)
344 add_control_edge (e);
347 else if (val == SSA_PROP_INTERESTING)
349 /* If the statement produced new value, add the SSA edges coming
350 out of OUTPUT_NAME. */
351 if (output_name)
352 add_ssa_edge (output_name, false);
354 /* If we know which edge is going to be taken out of this block,
355 add it to the CFG work list. */
356 if (taken_edge)
357 add_control_edge (taken_edge);
361 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
362 drain. This pops statements off the given WORKLIST and processes
363 them until there are no more statements on WORKLIST.
364 We take a pointer to WORKLIST because it may be reallocated when an
365 SSA edge is added to it in simulate_stmt. */
367 static void
368 process_ssa_edge_worklist (VEC(gimple,gc) **worklist)
370 /* Drain the entire worklist. */
371 while (VEC_length (gimple, *worklist) > 0)
373 basic_block bb;
375 /* Pull the statement to simulate off the worklist. */
376 gimple stmt = VEC_pop (gimple, *worklist);
378 /* If this statement was already visited by simulate_block, then
379 we don't need to visit it again here. */
380 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
381 continue;
383 /* STMT is no longer in a worklist. */
384 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
386 if (dump_file && (dump_flags & TDF_DETAILS))
388 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
389 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
392 bb = gimple_bb (stmt);
394 /* PHI nodes are always visited, regardless of whether or not
395 the destination block is executable. Otherwise, visit the
396 statement only if its block is marked executable. */
397 if (gimple_code (stmt) == GIMPLE_PHI
398 || TEST_BIT (executable_blocks, bb->index))
399 simulate_stmt (stmt);
404 /* Simulate the execution of BLOCK. Evaluate the statement associated
405 with each variable reference inside the block. */
407 static void
408 simulate_block (basic_block block)
410 gimple_stmt_iterator gsi;
412 /* There is nothing to do for the exit block. */
413 if (block == EXIT_BLOCK_PTR)
414 return;
416 if (dump_file && (dump_flags & TDF_DETAILS))
417 fprintf (dump_file, "\nSimulating block %d\n", block->index);
419 /* Always simulate PHI nodes, even if we have simulated this block
420 before. */
421 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
422 simulate_stmt (gsi_stmt (gsi));
424 /* If this is the first time we've simulated this block, then we
425 must simulate each of its statements. */
426 if (!TEST_BIT (executable_blocks, block->index))
428 gimple_stmt_iterator j;
429 unsigned int normal_edge_count;
430 edge e, normal_edge;
431 edge_iterator ei;
433 /* Note that we have simulated this block. */
434 SET_BIT (executable_blocks, block->index);
436 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
438 gimple stmt = gsi_stmt (j);
440 /* If this statement is already in the worklist then
441 "cancel" it. The reevaluation implied by the worklist
442 entry will produce the same value we generate here and
443 thus reevaluating it again from the worklist is
444 pointless. */
445 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
446 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
448 simulate_stmt (stmt);
451 /* We can not predict when abnormal and EH edges will be executed, so
452 once a block is considered executable, we consider any
453 outgoing abnormal edges as executable.
455 TODO: This is not exactly true. Simplifying statement might
456 prove it non-throwing and also computed goto can be handled
457 when destination is known.
459 At the same time, if this block has only one successor that is
460 reached by non-abnormal edges, then add that successor to the
461 worklist. */
462 normal_edge_count = 0;
463 normal_edge = NULL;
464 FOR_EACH_EDGE (e, ei, block->succs)
466 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
467 add_control_edge (e);
468 else
470 normal_edge_count++;
471 normal_edge = e;
475 if (normal_edge_count == 1)
476 add_control_edge (normal_edge);
481 /* Initialize local data structures and work lists. */
483 static void
484 ssa_prop_init (void)
486 edge e;
487 edge_iterator ei;
488 basic_block bb;
490 /* Worklists of SSA edges. */
491 interesting_ssa_edges = VEC_alloc (gimple, gc, 20);
492 varying_ssa_edges = VEC_alloc (gimple, gc, 20);
494 executable_blocks = sbitmap_alloc (last_basic_block);
495 sbitmap_zero (executable_blocks);
497 bb_in_list = sbitmap_alloc (last_basic_block);
498 sbitmap_zero (bb_in_list);
500 if (dump_file && (dump_flags & TDF_DETAILS))
501 dump_immediate_uses (dump_file);
503 cfg_blocks = VEC_alloc (basic_block, heap, 20);
504 VEC_safe_grow (basic_block, heap, cfg_blocks, 20);
506 /* Initially assume that every edge in the CFG is not executable.
507 (including the edges coming out of ENTRY_BLOCK_PTR). */
508 FOR_ALL_BB (bb)
510 gimple_stmt_iterator si;
512 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
513 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
515 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
516 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
518 FOR_EACH_EDGE (e, ei, bb->succs)
519 e->flags &= ~EDGE_EXECUTABLE;
522 /* Seed the algorithm by adding the successors of the entry block to the
523 edge worklist. */
524 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
525 add_control_edge (e);
529 /* Free allocated storage. */
531 static void
532 ssa_prop_fini (void)
534 VEC_free (gimple, gc, interesting_ssa_edges);
535 VEC_free (gimple, gc, varying_ssa_edges);
536 VEC_free (basic_block, heap, cfg_blocks);
537 cfg_blocks = NULL;
538 sbitmap_free (bb_in_list);
539 sbitmap_free (executable_blocks);
543 /* Return true if EXPR is an acceptable right-hand-side for a
544 GIMPLE assignment. We validate the entire tree, not just
545 the root node, thus catching expressions that embed complex
546 operands that are not permitted in GIMPLE. This function
547 is needed because the folding routines in fold-const.c
548 may return such expressions in some cases, e.g., an array
549 access with an embedded index addition. It may make more
550 sense to have folding routines that are sensitive to the
551 constraints on GIMPLE operands, rather than abandoning any
552 any attempt to fold if the usual folding turns out to be too
553 aggressive. */
555 bool
556 valid_gimple_rhs_p (tree expr)
558 enum tree_code code = TREE_CODE (expr);
560 switch (TREE_CODE_CLASS (code))
562 case tcc_declaration:
563 if (!is_gimple_variable (expr))
564 return false;
565 break;
567 case tcc_constant:
568 /* All constants are ok. */
569 break;
571 case tcc_binary:
572 case tcc_comparison:
573 if (!is_gimple_val (TREE_OPERAND (expr, 0))
574 || !is_gimple_val (TREE_OPERAND (expr, 1)))
575 return false;
576 break;
578 case tcc_unary:
579 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
580 return false;
581 break;
583 case tcc_expression:
584 switch (code)
586 case ADDR_EXPR:
588 tree t;
589 if (is_gimple_min_invariant (expr))
590 return true;
591 t = TREE_OPERAND (expr, 0);
592 while (handled_component_p (t))
594 /* ??? More checks needed, see the GIMPLE verifier. */
595 if ((TREE_CODE (t) == ARRAY_REF
596 || TREE_CODE (t) == ARRAY_RANGE_REF)
597 && !is_gimple_val (TREE_OPERAND (t, 1)))
598 return false;
599 t = TREE_OPERAND (t, 0);
601 if (!is_gimple_id (t))
602 return false;
604 break;
606 case TRUTH_NOT_EXPR:
607 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
608 return false;
609 break;
611 case TRUTH_AND_EXPR:
612 case TRUTH_XOR_EXPR:
613 case TRUTH_OR_EXPR:
614 if (!is_gimple_val (TREE_OPERAND (expr, 0))
615 || !is_gimple_val (TREE_OPERAND (expr, 1)))
616 return false;
617 break;
619 default:
620 return false;
622 break;
624 case tcc_vl_exp:
625 return false;
627 case tcc_exceptional:
628 if (code != SSA_NAME)
629 return false;
630 break;
632 default:
633 return false;
636 return true;
640 /* Return true if EXPR is a CALL_EXPR suitable for representation
641 as a single GIMPLE_CALL statement. If the arguments require
642 further gimplification, return false. */
644 bool
645 valid_gimple_call_p (tree expr)
647 unsigned i, nargs;
649 if (TREE_CODE (expr) != CALL_EXPR)
650 return false;
652 nargs = call_expr_nargs (expr);
653 for (i = 0; i < nargs; i++)
654 if (! is_gimple_operand (CALL_EXPR_ARG (expr, i)))
655 return false;
657 return true;
661 /* Make SSA names defined by OLD_STMT point to NEW_STMT
662 as their defining statement. */
664 void
665 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
667 tree var;
668 ssa_op_iter iter;
670 if (gimple_in_ssa_p (cfun))
672 /* Make defined SSA_NAMEs point to the new
673 statement as their definition. */
674 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
676 if (TREE_CODE (var) == SSA_NAME)
677 SSA_NAME_DEF_STMT (var) = new_stmt;
683 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
684 value of EXPR, which is expected to be the result of folding the
685 call. This can only be done if EXPR is a CALL_EXPR with valid
686 GIMPLE operands as arguments, or if it is a suitable RHS expression
687 for a GIMPLE_ASSIGN. More complex expressions will require
688 gimplification, which will introduce addtional statements. In this
689 event, no update is performed, and the function returns false.
690 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
691 replace the statement at *SI_P with an entirely new statement.
692 The new statement need not be a call, e.g., if the original call
693 folded to a constant. */
695 bool
696 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
698 tree lhs;
700 gimple stmt = gsi_stmt (*si_p);
702 gcc_assert (is_gimple_call (stmt));
704 lhs = gimple_call_lhs (stmt);
706 if (valid_gimple_call_p (expr))
708 /* The call has simplified to another call. */
709 tree fn = CALL_EXPR_FN (expr);
710 unsigned i;
711 unsigned nargs = call_expr_nargs (expr);
712 VEC(tree, heap) *args = NULL;
713 gimple new_stmt;
715 if (nargs > 0)
717 args = VEC_alloc (tree, heap, nargs);
718 VEC_safe_grow (tree, heap, args, nargs);
720 for (i = 0; i < nargs; i++)
721 VEC_replace (tree, args, i, CALL_EXPR_ARG (expr, i));
724 new_stmt = gimple_build_call_vec (fn, args);
725 gimple_call_set_lhs (new_stmt, lhs);
726 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
727 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
728 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
729 gimple_set_location (new_stmt, gimple_location (stmt));
730 gsi_replace (si_p, new_stmt, false);
731 VEC_free (tree, heap, args);
733 return true;
735 else if (valid_gimple_rhs_p (expr))
737 gimple new_stmt;
739 /* The call has simplified to an expression
740 that cannot be represented as a GIMPLE_CALL. */
741 if (lhs)
743 /* A value is expected.
744 Introduce a new GIMPLE_ASSIGN statement. */
745 STRIP_USELESS_TYPE_CONVERSION (expr);
746 new_stmt = gimple_build_assign (lhs, expr);
747 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
748 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
749 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
751 else if (!TREE_SIDE_EFFECTS (expr))
753 /* No value is expected, and EXPR has no effect.
754 Replace it with an empty statement. */
755 new_stmt = gimple_build_nop ();
756 unlink_stmt_vdef (stmt);
757 release_defs (stmt);
759 else
761 /* No value is expected, but EXPR has an effect,
762 e.g., it could be a reference to a volatile
763 variable. Create an assignment statement
764 with a dummy (unused) lhs variable. */
765 STRIP_USELESS_TYPE_CONVERSION (expr);
766 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
767 new_stmt = gimple_build_assign (lhs, expr);
768 add_referenced_var (lhs);
769 lhs = make_ssa_name (lhs, new_stmt);
770 gimple_assign_set_lhs (new_stmt, lhs);
771 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
772 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
773 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
775 gimple_set_location (new_stmt, gimple_location (stmt));
776 gsi_replace (si_p, new_stmt, false);
777 return true;
779 else
780 /* The call simplified to an expression that is
781 not a valid GIMPLE RHS. */
782 return false;
786 /* Entry point to the propagation engine.
788 VISIT_STMT is called for every statement visited.
789 VISIT_PHI is called for every PHI node visited. */
791 void
792 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
793 ssa_prop_visit_phi_fn visit_phi)
795 ssa_prop_visit_stmt = visit_stmt;
796 ssa_prop_visit_phi = visit_phi;
798 ssa_prop_init ();
800 /* Iterate until the worklists are empty. */
801 while (!cfg_blocks_empty_p ()
802 || VEC_length (gimple, interesting_ssa_edges) > 0
803 || VEC_length (gimple, varying_ssa_edges) > 0)
805 if (!cfg_blocks_empty_p ())
807 /* Pull the next block to simulate off the worklist. */
808 basic_block dest_block = cfg_blocks_get ();
809 simulate_block (dest_block);
812 /* In order to move things to varying as quickly as
813 possible,process the VARYING_SSA_EDGES worklist first. */
814 process_ssa_edge_worklist (&varying_ssa_edges);
816 /* Now process the INTERESTING_SSA_EDGES worklist. */
817 process_ssa_edge_worklist (&interesting_ssa_edges);
820 ssa_prop_fini ();
824 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
825 is a non-volatile pointer dereference, a structure reference or a
826 reference to a single _DECL. Ignore volatile memory references
827 because they are not interesting for the optimizers. */
829 bool
830 stmt_makes_single_store (gimple stmt)
832 tree lhs;
834 if (gimple_code (stmt) != GIMPLE_ASSIGN
835 && gimple_code (stmt) != GIMPLE_CALL)
836 return false;
838 if (!gimple_vdef (stmt))
839 return false;
841 lhs = gimple_get_lhs (stmt);
843 /* A call statement may have a null LHS. */
844 if (!lhs)
845 return false;
847 return (!TREE_THIS_VOLATILE (lhs)
848 && (DECL_P (lhs)
849 || REFERENCE_CLASS_P (lhs)));
853 /* Propagation statistics. */
854 struct prop_stats_d
856 long num_const_prop;
857 long num_copy_prop;
858 long num_stmts_folded;
859 long num_dce;
862 static struct prop_stats_d prop_stats;
864 /* Replace USE references in statement STMT with the values stored in
865 PROP_VALUE. Return true if at least one reference was replaced. */
867 static bool
868 replace_uses_in (gimple stmt, prop_value_t *prop_value)
870 bool replaced = false;
871 use_operand_p use;
872 ssa_op_iter iter;
874 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
876 tree tuse = USE_FROM_PTR (use);
877 tree val = prop_value[SSA_NAME_VERSION (tuse)].value;
879 if (val == tuse || val == NULL_TREE)
880 continue;
882 if (gimple_code (stmt) == GIMPLE_ASM
883 && !may_propagate_copy_into_asm (tuse))
884 continue;
886 if (!may_propagate_copy (tuse, val))
887 continue;
889 if (TREE_CODE (val) != SSA_NAME)
890 prop_stats.num_const_prop++;
891 else
892 prop_stats.num_copy_prop++;
894 propagate_value (use, val);
896 replaced = true;
899 return replaced;
903 /* Replace propagated values into all the arguments for PHI using the
904 values from PROP_VALUE. */
906 static void
907 replace_phi_args_in (gimple phi, prop_value_t *prop_value)
909 size_t i;
910 bool replaced = false;
912 if (dump_file && (dump_flags & TDF_DETAILS))
914 fprintf (dump_file, "Folding PHI node: ");
915 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
918 for (i = 0; i < gimple_phi_num_args (phi); i++)
920 tree arg = gimple_phi_arg_def (phi, i);
922 if (TREE_CODE (arg) == SSA_NAME)
924 tree val = prop_value[SSA_NAME_VERSION (arg)].value;
926 if (val && val != arg && may_propagate_copy (arg, val))
928 if (TREE_CODE (val) != SSA_NAME)
929 prop_stats.num_const_prop++;
930 else
931 prop_stats.num_copy_prop++;
933 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
934 replaced = true;
936 /* If we propagated a copy and this argument flows
937 through an abnormal edge, update the replacement
938 accordingly. */
939 if (TREE_CODE (val) == SSA_NAME
940 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
941 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
946 if (dump_file && (dump_flags & TDF_DETAILS))
948 if (!replaced)
949 fprintf (dump_file, "No folding possible\n");
950 else
952 fprintf (dump_file, "Folded into: ");
953 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
954 fprintf (dump_file, "\n");
960 /* Perform final substitution and folding of propagated values.
962 PROP_VALUE[I] contains the single value that should be substituted
963 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
964 substituted.
966 If FOLD_FN is non-NULL the function will be invoked on all statements
967 before propagating values for pass specific simplification.
969 Return TRUE when something changed. */
971 bool
972 substitute_and_fold (prop_value_t *prop_value, ssa_prop_fold_stmt_fn fold_fn)
974 basic_block bb;
975 bool something_changed = false;
977 if (prop_value == NULL && !fold_fn)
978 return false;
980 if (dump_file && (dump_flags & TDF_DETAILS))
981 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
983 memset (&prop_stats, 0, sizeof (prop_stats));
985 /* Substitute values in every statement of every basic block. */
986 FOR_EACH_BB (bb)
988 gimple_stmt_iterator i;
990 /* Propagate known values into PHI nodes. */
991 if (prop_value)
992 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
993 replace_phi_args_in (gsi_stmt (i), prop_value);
995 /* Propagate known values into stmts. Do a backward walk to expose
996 more trivially deletable stmts. */
997 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
999 bool did_replace;
1000 gimple stmt = gsi_stmt (i);
1001 gimple old_stmt;
1002 enum gimple_code code = gimple_code (stmt);
1003 gimple_stmt_iterator oldi;
1005 oldi = i;
1006 gsi_prev (&i);
1008 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1009 range information for names and they are discarded
1010 afterwards. */
1012 if (code == GIMPLE_ASSIGN
1013 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1014 continue;
1016 /* No point propagating into a stmt whose result is not used,
1017 but instead we might be able to remove a trivially dead stmt. */
1018 if (gimple_get_lhs (stmt)
1019 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1020 && has_zero_uses (gimple_get_lhs (stmt))
1021 && !stmt_could_throw_p (stmt)
1022 && !gimple_has_side_effects (stmt))
1024 gimple_stmt_iterator i2;
1026 if (dump_file && dump_flags & TDF_DETAILS)
1028 fprintf (dump_file, "Removing dead stmt ");
1029 print_gimple_stmt (dump_file, stmt, 0, 0);
1030 fprintf (dump_file, "\n");
1032 prop_stats.num_dce++;
1033 i2 = gsi_for_stmt (stmt);
1034 gsi_remove (&i2, true);
1035 release_defs (stmt);
1036 continue;
1039 /* Replace the statement with its folded version and mark it
1040 folded. */
1041 did_replace = false;
1042 if (dump_file && (dump_flags & TDF_DETAILS))
1044 fprintf (dump_file, "Folding statement: ");
1045 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1048 old_stmt = stmt;
1050 /* Some statements may be simplified using propagator
1051 specific information. Do this before propagating
1052 into the stmt to not disturb pass specific information. */
1053 if (fold_fn
1054 && (*fold_fn)(&oldi))
1056 did_replace = true;
1057 prop_stats.num_stmts_folded++;
1060 /* Only replace real uses if we couldn't fold the
1061 statement using value range information. */
1062 if (prop_value
1063 && !did_replace)
1064 did_replace |= replace_uses_in (stmt, prop_value);
1066 /* If we made a replacement, fold the statement. */
1067 if (did_replace)
1068 fold_stmt (&oldi);
1070 /* Now cleanup. */
1071 if (did_replace)
1073 stmt = gsi_stmt (oldi);
1075 /* If we cleaned up EH information from the statement,
1076 remove EH edges. */
1077 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1078 gimple_purge_dead_eh_edges (bb);
1080 if (is_gimple_assign (stmt)
1081 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1082 == GIMPLE_SINGLE_RHS))
1084 tree rhs = gimple_assign_rhs1 (stmt);
1086 if (TREE_CODE (rhs) == ADDR_EXPR)
1087 recompute_tree_invariant_for_addr_expr (rhs);
1090 /* Determine what needs to be done to update the SSA form. */
1091 update_stmt (stmt);
1092 if (!is_gimple_debug (stmt))
1093 something_changed = true;
1096 if (dump_file && (dump_flags & TDF_DETAILS))
1098 if (did_replace)
1100 fprintf (dump_file, "Folded into: ");
1101 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1102 fprintf (dump_file, "\n");
1104 else
1105 fprintf (dump_file, "Not folded\n");
1110 statistics_counter_event (cfun, "Constants propagated",
1111 prop_stats.num_const_prop);
1112 statistics_counter_event (cfun, "Copies propagated",
1113 prop_stats.num_copy_prop);
1114 statistics_counter_event (cfun, "Statements folded",
1115 prop_stats.num_stmts_folded);
1116 statistics_counter_event (cfun, "Statements deleted",
1117 prop_stats.num_dce);
1118 return something_changed;
1121 #include "gt-tree-ssa-propagate.h"