OSDN Git Service

PR testsuite/36057
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-propagate.c
1 /* Generic SSA value propagation engine.
2    Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Diego Novillo <dnovillo@redhat.com>
4
5    This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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 "varray.h"
42 #include "vec.h"
43
44 /* This file implements a generic value propagation engine based on
45    the same propagation used by the SSA-CCP algorithm [1].
46
47    Propagation is performed by simulating the execution of every
48    statement that produces the value being propagated.  Simulation
49    proceeds as follows:
50
51    1- Initially, all edges of the CFG are marked not executable and
52       the CFG worklist is seeded with all the statements in the entry
53       basic block (block 0).
54
55    2- Every statement S is simulated with a call to the call-back
56       function SSA_PROP_VISIT_STMT.  This evaluation may produce 3
57       results:
58
59         SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
60             interest and does not affect any of the work lists.
61
62         SSA_PROP_VARYING: The value produced by S cannot be determined
63             at compile time.  Further simulation of S is not required.
64             If S is a conditional jump, all the outgoing edges for the
65             block are considered executable and added to the work
66             list.
67
68         SSA_PROP_INTERESTING: S produces a value that can be computed
69             at compile time.  Its result can be propagated into the
70             statements that feed from S.  Furthermore, if S is a
71             conditional jump, only the edge known to be taken is added
72             to the work list.  Edges that are known not to execute are
73             never simulated.
74
75    3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI.  The
76       return value from SSA_PROP_VISIT_PHI has the same semantics as
77       described in #2.
78
79    4- Three work lists are kept.  Statements are only added to these
80       lists if they produce one of SSA_PROP_INTERESTING or
81       SSA_PROP_VARYING.
82
83         CFG_BLOCKS contains the list of blocks to be simulated.
84             Blocks are added to this list if their incoming edges are
85             found executable.
86
87         VARYING_SSA_EDGES contains the list of statements that feed
88             from statements that produce an SSA_PROP_VARYING result.
89             These are simulated first to speed up processing.
90
91         INTERESTING_SSA_EDGES contains the list of statements that
92             feed from statements that produce an SSA_PROP_INTERESTING
93             result.
94
95    5- Simulation terminates when all three work lists are drained.
96
97    Before calling ssa_propagate, it is important to clear
98    DONT_SIMULATE_AGAIN for all the statements in the program that
99    should be simulated.  This initialization allows an implementation
100    to specify which statements should never be simulated.
101
102    It is also important to compute def-use information before calling
103    ssa_propagate.
104
105    References:
106
107      [1] Constant propagation with conditional branches,
108          Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
109
110      [2] Building an Optimizing Compiler,
111          Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
112
113      [3] Advanced Compiler Design and Implementation,
114          Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6  */
115
116 /* Function pointers used to parameterize the propagation engine.  */
117 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
118 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
119
120 /* Use the TREE_DEPRECATED bitflag to mark statements that have been
121    added to one of the SSA edges worklists.  This flag is used to
122    avoid visiting statements unnecessarily when draining an SSA edge
123    worklist.  If while simulating a basic block, we find a statement with
124    STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
125    processing from visiting it again.  */
126 #define STMT_IN_SSA_EDGE_WORKLIST(T)    TREE_DEPRECATED (T)
127
128 /* A bitmap to keep track of executable blocks in the CFG.  */
129 static sbitmap executable_blocks;
130
131 /* Array of control flow edges on the worklist.  */
132 static VEC(basic_block,heap) *cfg_blocks;
133
134 static unsigned int cfg_blocks_num = 0;
135 static int cfg_blocks_tail;
136 static int cfg_blocks_head;
137
138 static sbitmap bb_in_list;
139
140 /* Worklist of SSA edges which will need reexamination as their
141    definition has changed.  SSA edges are def-use edges in the SSA
142    web.  For each D-U edge, we store the target statement or PHI node
143    U.  */
144 static GTY(()) VEC(tree,gc) *interesting_ssa_edges;
145
146 /* Identical to INTERESTING_SSA_EDGES.  For performance reasons, the
147    list of SSA edges is split into two.  One contains all SSA edges
148    who need to be reexamined because their lattice value changed to
149    varying (this worklist), and the other contains all other SSA edges
150    to be reexamined (INTERESTING_SSA_EDGES).
151
152    Since most values in the program are VARYING, the ideal situation
153    is to move them to that lattice value as quickly as possible.
154    Thus, it doesn't make sense to process any other type of lattice
155    value until all VARYING values are propagated fully, which is one
156    thing using the VARYING worklist achieves.  In addition, if we
157    don't use a separate worklist for VARYING edges, we end up with
158    situations where lattice values move from
159    UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING.  */
160 static GTY(()) VEC(tree,gc) *varying_ssa_edges;
161
162
163 /* Return true if the block worklist empty.  */
164
165 static inline bool
166 cfg_blocks_empty_p (void)
167 {
168   return (cfg_blocks_num == 0);
169 }
170
171
172 /* Add a basic block to the worklist.  The block must not be already
173    in the worklist, and it must not be the ENTRY or EXIT block.  */
174
175 static void 
176 cfg_blocks_add (basic_block bb)
177 {
178   bool head = false;
179
180   gcc_assert (bb != ENTRY_BLOCK_PTR && bb != EXIT_BLOCK_PTR);
181   gcc_assert (!TEST_BIT (bb_in_list, bb->index));
182
183   if (cfg_blocks_empty_p ())
184     {
185       cfg_blocks_tail = cfg_blocks_head = 0;
186       cfg_blocks_num = 1;
187     }
188   else
189     {
190       cfg_blocks_num++;
191       if (cfg_blocks_num > VEC_length (basic_block, cfg_blocks))
192         {
193           /* We have to grow the array now.  Adjust to queue to occupy
194              the full space of the original array.  We do not need to
195              initialize the newly allocated portion of the array
196              because we keep track of CFG_BLOCKS_HEAD and
197              CFG_BLOCKS_HEAD.  */
198           cfg_blocks_tail = VEC_length (basic_block, cfg_blocks);
199           cfg_blocks_head = 0;
200           VEC_safe_grow (basic_block, heap, cfg_blocks, 2 * cfg_blocks_tail);
201         }
202       /* Minor optimization: we prefer to see blocks with more
203          predecessors later, because there is more of a chance that
204          the incoming edges will be executable.  */
205       else if (EDGE_COUNT (bb->preds)
206                >= EDGE_COUNT (VEC_index (basic_block, cfg_blocks,
207                                          cfg_blocks_head)->preds))
208         cfg_blocks_tail = ((cfg_blocks_tail + 1)
209                            % VEC_length (basic_block, cfg_blocks));
210       else
211         {
212           if (cfg_blocks_head == 0)
213             cfg_blocks_head = VEC_length (basic_block, cfg_blocks);
214           --cfg_blocks_head;
215           head = true;
216         }
217     }
218
219   VEC_replace (basic_block, cfg_blocks,
220                head ? cfg_blocks_head : cfg_blocks_tail,
221                bb);
222   SET_BIT (bb_in_list, bb->index);
223 }
224
225
226 /* Remove a block from the worklist.  */
227
228 static basic_block
229 cfg_blocks_get (void)
230 {
231   basic_block bb;
232
233   bb = VEC_index (basic_block, cfg_blocks, cfg_blocks_head);
234
235   gcc_assert (!cfg_blocks_empty_p ());
236   gcc_assert (bb);
237
238   cfg_blocks_head = ((cfg_blocks_head + 1)
239                      % VEC_length (basic_block, cfg_blocks));
240   --cfg_blocks_num;
241   RESET_BIT (bb_in_list, bb->index);
242
243   return bb;
244 }
245
246
247 /* We have just defined a new value for VAR.  If IS_VARYING is true,
248    add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
249    them to INTERESTING_SSA_EDGES.  */
250
251 static void
252 add_ssa_edge (tree var, bool is_varying)
253 {
254   imm_use_iterator iter;
255   use_operand_p use_p;
256
257   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
258     {
259       tree use_stmt = USE_STMT (use_p);
260
261       if (!DONT_SIMULATE_AGAIN (use_stmt)
262           && !STMT_IN_SSA_EDGE_WORKLIST (use_stmt))
263         {
264           STMT_IN_SSA_EDGE_WORKLIST (use_stmt) = 1;
265           if (is_varying)
266             VEC_safe_push (tree, gc, varying_ssa_edges, use_stmt);
267           else
268             VEC_safe_push (tree, gc, interesting_ssa_edges, use_stmt);
269         }
270     }
271 }
272
273
274 /* Add edge E to the control flow worklist.  */
275
276 static void
277 add_control_edge (edge e)
278 {
279   basic_block bb = e->dest;
280   if (bb == EXIT_BLOCK_PTR)
281     return;
282
283   /* If the edge had already been executed, skip it.  */
284   if (e->flags & EDGE_EXECUTABLE)
285     return;
286
287   e->flags |= EDGE_EXECUTABLE;
288
289   /* If the block is already in the list, we're done.  */
290   if (TEST_BIT (bb_in_list, bb->index))
291     return;
292
293   cfg_blocks_add (bb);
294
295   if (dump_file && (dump_flags & TDF_DETAILS))
296     fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
297         e->src->index, e->dest->index);
298 }
299
300
301 /* Simulate the execution of STMT and update the work lists accordingly.  */
302
303 static void
304 simulate_stmt (tree stmt)
305 {
306   enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
307   edge taken_edge = NULL;
308   tree output_name = NULL_TREE;
309
310   /* Don't bother visiting statements that are already
311      considered varying by the propagator.  */
312   if (DONT_SIMULATE_AGAIN (stmt))
313     return;
314
315   if (TREE_CODE (stmt) == PHI_NODE)
316     {
317       val = ssa_prop_visit_phi (stmt);
318       output_name = PHI_RESULT (stmt);
319     }
320   else
321     val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
322
323   if (val == SSA_PROP_VARYING)
324     {
325       DONT_SIMULATE_AGAIN (stmt) = 1;
326
327       /* If the statement produced a new varying value, add the SSA
328          edges coming out of OUTPUT_NAME.  */
329       if (output_name)
330         add_ssa_edge (output_name, true);
331
332       /* If STMT transfers control out of its basic block, add
333          all outgoing edges to the work list.  */
334       if (stmt_ends_bb_p (stmt))
335         {
336           edge e;
337           edge_iterator ei;
338           basic_block bb = bb_for_stmt (stmt);
339           FOR_EACH_EDGE (e, ei, bb->succs)
340             add_control_edge (e);
341         }
342     }
343   else if (val == SSA_PROP_INTERESTING)
344     {
345       /* If the statement produced new value, add the SSA edges coming
346          out of OUTPUT_NAME.  */
347       if (output_name)
348         add_ssa_edge (output_name, false);
349
350       /* If we know which edge is going to be taken out of this block,
351          add it to the CFG work list.  */
352       if (taken_edge)
353         add_control_edge (taken_edge);
354     }
355 }
356
357 /* Process an SSA edge worklist.  WORKLIST is the SSA edge worklist to
358    drain.  This pops statements off the given WORKLIST and processes
359    them until there are no more statements on WORKLIST.
360    We take a pointer to WORKLIST because it may be reallocated when an
361    SSA edge is added to it in simulate_stmt.  */
362
363 static void
364 process_ssa_edge_worklist (VEC(tree,gc) **worklist)
365 {
366   /* Drain the entire worklist.  */
367   while (VEC_length (tree, *worklist) > 0)
368     {
369       basic_block bb;
370
371       /* Pull the statement to simulate off the worklist.  */
372       tree stmt = VEC_pop (tree, *worklist);
373
374       /* If this statement was already visited by simulate_block, then
375          we don't need to visit it again here.  */
376       if (!STMT_IN_SSA_EDGE_WORKLIST (stmt))
377         continue;
378
379       /* STMT is no longer in a worklist.  */
380       STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
381
382       if (dump_file && (dump_flags & TDF_DETAILS))
383         {
384           fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
385           print_generic_stmt (dump_file, stmt, dump_flags);
386         }
387
388       bb = bb_for_stmt (stmt);
389
390       /* PHI nodes are always visited, regardless of whether or not
391          the destination block is executable.  Otherwise, visit the
392          statement only if its block is marked executable.  */
393       if (TREE_CODE (stmt) == PHI_NODE
394           || TEST_BIT (executable_blocks, bb->index))
395         simulate_stmt (stmt);
396     }
397 }
398
399
400 /* Simulate the execution of BLOCK.  Evaluate the statement associated
401    with each variable reference inside the block.  */
402
403 static void
404 simulate_block (basic_block block)
405 {
406   tree phi;
407
408   /* There is nothing to do for the exit block.  */
409   if (block == EXIT_BLOCK_PTR)
410     return;
411
412   if (dump_file && (dump_flags & TDF_DETAILS))
413     fprintf (dump_file, "\nSimulating block %d\n", block->index);
414
415   /* Always simulate PHI nodes, even if we have simulated this block
416      before.  */
417   for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
418     simulate_stmt (phi);
419
420   /* If this is the first time we've simulated this block, then we
421      must simulate each of its statements.  */
422   if (!TEST_BIT (executable_blocks, block->index))
423     {
424       block_stmt_iterator j;
425       unsigned int normal_edge_count;
426       edge e, normal_edge;
427       edge_iterator ei;
428
429       /* Note that we have simulated this block.  */
430       SET_BIT (executable_blocks, block->index);
431
432       for (j = bsi_start (block); !bsi_end_p (j); bsi_next (&j))
433         {
434           tree stmt = bsi_stmt (j);
435
436           /* If this statement is already in the worklist then
437              "cancel" it.  The reevaluation implied by the worklist
438              entry will produce the same value we generate here and
439              thus reevaluating it again from the worklist is
440              pointless.  */
441           if (STMT_IN_SSA_EDGE_WORKLIST (stmt))
442             STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
443
444           simulate_stmt (stmt);
445         }
446
447       /* We can not predict when abnormal edges will be executed, so
448          once a block is considered executable, we consider any
449          outgoing abnormal edges as executable.
450
451          At the same time, if this block has only one successor that is
452          reached by non-abnormal edges, then add that successor to the
453          worklist.  */
454       normal_edge_count = 0;
455       normal_edge = NULL;
456       FOR_EACH_EDGE (e, ei, block->succs)
457         {
458           if (e->flags & EDGE_ABNORMAL)
459             add_control_edge (e);
460           else
461             {
462               normal_edge_count++;
463               normal_edge = e;
464             }
465         }
466
467       if (normal_edge_count == 1)
468         add_control_edge (normal_edge);
469     }
470 }
471
472
473 /* Initialize local data structures and work lists.  */
474
475 static void
476 ssa_prop_init (void)
477 {
478   edge e;
479   edge_iterator ei;
480   basic_block bb;
481   size_t i;
482
483   /* Worklists of SSA edges.  */
484   interesting_ssa_edges = VEC_alloc (tree, gc, 20);
485   varying_ssa_edges = VEC_alloc (tree, gc, 20);
486
487   executable_blocks = sbitmap_alloc (last_basic_block);
488   sbitmap_zero (executable_blocks);
489
490   bb_in_list = sbitmap_alloc (last_basic_block);
491   sbitmap_zero (bb_in_list);
492
493   if (dump_file && (dump_flags & TDF_DETAILS))
494     dump_immediate_uses (dump_file);
495
496   cfg_blocks = VEC_alloc (basic_block, heap, 20);
497   VEC_safe_grow (basic_block, heap, cfg_blocks, 20);
498
499   /* Initialize the values for every SSA_NAME.  */
500   for (i = 1; i < num_ssa_names; i++)
501     if (ssa_name (i))
502       SSA_NAME_VALUE (ssa_name (i)) = NULL_TREE;
503
504   /* Initially assume that every edge in the CFG is not executable.
505      (including the edges coming out of ENTRY_BLOCK_PTR).  */
506   FOR_ALL_BB (bb)
507     {
508       block_stmt_iterator si;
509
510       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
511         STMT_IN_SSA_EDGE_WORKLIST (bsi_stmt (si)) = 0;
512
513       FOR_EACH_EDGE (e, ei, bb->succs)
514         e->flags &= ~EDGE_EXECUTABLE;
515     }
516
517   /* Seed the algorithm by adding the successors of the entry block to the
518      edge worklist.  */
519   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
520     add_control_edge (e);
521 }
522
523
524 /* Free allocated storage.  */
525
526 static void
527 ssa_prop_fini (void)
528 {
529   VEC_free (tree, gc, interesting_ssa_edges);
530   VEC_free (tree, gc, varying_ssa_edges);
531   VEC_free (basic_block, heap, cfg_blocks);
532   cfg_blocks = NULL;
533   sbitmap_free (bb_in_list);
534   sbitmap_free (executable_blocks);
535 }
536
537
538 /* Get the main expression from statement STMT.  */
539
540 tree
541 get_rhs (tree stmt)
542 {
543   enum tree_code code = TREE_CODE (stmt);
544
545   switch (code)
546     {
547     case RETURN_EXPR:
548       stmt = TREE_OPERAND (stmt, 0);
549       if (!stmt || TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
550         return stmt;
551       /* FALLTHRU */
552
553     case GIMPLE_MODIFY_STMT:
554       stmt = GENERIC_TREE_OPERAND (stmt, 1);
555       if (TREE_CODE (stmt) == WITH_SIZE_EXPR)
556         return TREE_OPERAND (stmt, 0);
557       else
558         return stmt;
559
560     case COND_EXPR:
561       return COND_EXPR_COND (stmt);
562     case SWITCH_EXPR:
563       return SWITCH_COND (stmt);
564     case GOTO_EXPR:
565       return GOTO_DESTINATION (stmt);
566     case LABEL_EXPR:
567       return LABEL_EXPR_LABEL (stmt);
568
569     default:
570       return stmt;
571     }
572 }
573
574
575 /* Return true if EXPR is a valid GIMPLE expression.  */
576
577 bool
578 valid_gimple_expression_p (tree expr)
579 {
580   enum tree_code code = TREE_CODE (expr);
581
582   switch (TREE_CODE_CLASS (code))
583     {
584     case tcc_declaration:
585       if (!is_gimple_variable (expr))
586         return false;
587       break;
588
589     case tcc_constant:
590       break;
591
592     case tcc_binary:
593     case tcc_comparison:
594       if (!is_gimple_val (TREE_OPERAND (expr, 0))
595           || !is_gimple_val (TREE_OPERAND (expr, 1)))
596         return false;
597       break;
598
599     case tcc_unary:
600       if (!is_gimple_val (TREE_OPERAND (expr, 0)))
601         return false;
602       break;
603
604     case tcc_expression:
605       switch (code)
606         {
607         case ADDR_EXPR:
608          {
609            tree t = TREE_OPERAND (expr, 0);
610            while (handled_component_p (t))
611              {
612                /* ??? More checks needed, see the GIMPLE verifier.  */
613                if ((TREE_CODE (t) == ARRAY_REF
614                     || TREE_CODE (t) == ARRAY_RANGE_REF)
615                    && !is_gimple_val (TREE_OPERAND (t, 1)))
616                  return false;
617                t = TREE_OPERAND (t, 0);
618              }
619            if (!is_gimple_id (t))
620              return false;
621            break;
622          }
623
624         case TRUTH_NOT_EXPR:
625           if (!is_gimple_val (TREE_OPERAND (expr, 0)))
626             return false;
627           break;
628
629         case TRUTH_AND_EXPR:
630         case TRUTH_XOR_EXPR:
631         case TRUTH_OR_EXPR:
632           if (!is_gimple_val (TREE_OPERAND (expr, 0))
633               || !is_gimple_val (TREE_OPERAND (expr, 1)))
634             return false;
635           break;
636
637         case EXC_PTR_EXPR:
638         case FILTER_EXPR:
639           break;
640
641         default:
642           return false;
643         }
644       break;
645
646     case tcc_vl_exp:
647       switch (code)
648         {
649         case CALL_EXPR:
650           break;
651         default:
652           return false;
653         }
654       break;
655
656     case tcc_exceptional:
657       switch (code)
658         {
659         case SSA_NAME:
660           break;
661
662         default:
663           return false;
664         }
665       break;
666
667     default:
668       return false;
669     }
670
671   return true;
672 }
673
674
675 /* Set the main expression of *STMT_P to EXPR.  If EXPR is not a valid
676    GIMPLE expression no changes are done and the function returns
677    false.  */
678
679 bool
680 set_rhs (tree *stmt_p, tree expr)
681 {
682   tree stmt = *stmt_p, op;
683   stmt_ann_t ann;
684   tree var;
685   ssa_op_iter iter;
686
687   if (!valid_gimple_expression_p (expr))
688     return false;
689
690   if (EXPR_HAS_LOCATION (stmt)
691       && (EXPR_P (expr)
692           || GIMPLE_STMT_P (expr))
693       && ! EXPR_HAS_LOCATION (expr)
694       && TREE_SIDE_EFFECTS (expr)
695       && TREE_CODE (expr) != LABEL_EXPR)
696     SET_EXPR_LOCATION (expr, EXPR_LOCATION (stmt));
697
698   switch (TREE_CODE (stmt))
699     {
700     case RETURN_EXPR:
701       op = TREE_OPERAND (stmt, 0);
702       if (TREE_CODE (op) != GIMPLE_MODIFY_STMT)
703         {
704           GIMPLE_STMT_OPERAND (stmt, 0) = expr;
705           break;
706         }
707       stmt = op;
708       /* FALLTHRU */
709
710     case GIMPLE_MODIFY_STMT:
711       op = GIMPLE_STMT_OPERAND (stmt, 1);
712       if (TREE_CODE (op) == WITH_SIZE_EXPR)
713         TREE_OPERAND (op, 0) = expr;
714       else
715         GIMPLE_STMT_OPERAND (stmt, 1) = expr;
716       break;
717
718     case COND_EXPR:
719       if (!is_gimple_condexpr (expr))
720         return false;
721       COND_EXPR_COND (stmt) = expr;
722       break;
723     case SWITCH_EXPR:
724       SWITCH_COND (stmt) = expr;
725       break;
726     case GOTO_EXPR:
727       GOTO_DESTINATION (stmt) = expr;
728       break;
729     case LABEL_EXPR:
730       LABEL_EXPR_LABEL (stmt) = expr;
731       break;
732
733     default:
734       /* Replace the whole statement with EXPR.  If EXPR has no side
735          effects, then replace *STMT_P with an empty statement.  */
736       ann = stmt_ann (stmt);
737       *stmt_p = TREE_SIDE_EFFECTS (expr) ? expr : build_empty_stmt ();
738       (*stmt_p)->base.ann = (tree_ann_t) ann;
739
740       if (gimple_in_ssa_p (cfun)
741           && TREE_SIDE_EFFECTS (expr))
742         {
743           /* Fix all the SSA_NAMEs created by *STMT_P to point to its new
744              replacement.  */
745           FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_DEFS)
746             {
747               if (TREE_CODE (var) == SSA_NAME)
748                 SSA_NAME_DEF_STMT (var) = *stmt_p;
749             }
750         }
751       stmt->base.ann = NULL;
752       break;
753     }
754
755   return true;
756 }
757
758
759 /* Entry point to the propagation engine.
760
761    VISIT_STMT is called for every statement visited.
762    VISIT_PHI is called for every PHI node visited.  */
763
764 void
765 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
766                ssa_prop_visit_phi_fn visit_phi)
767 {
768   ssa_prop_visit_stmt = visit_stmt;
769   ssa_prop_visit_phi = visit_phi;
770
771   ssa_prop_init ();
772
773   /* Iterate until the worklists are empty.  */
774   while (!cfg_blocks_empty_p () 
775          || VEC_length (tree, interesting_ssa_edges) > 0
776          || VEC_length (tree, varying_ssa_edges) > 0)
777     {
778       if (!cfg_blocks_empty_p ())
779         {
780           /* Pull the next block to simulate off the worklist.  */
781           basic_block dest_block = cfg_blocks_get ();
782           simulate_block (dest_block);
783         }
784
785       /* In order to move things to varying as quickly as
786          possible,process the VARYING_SSA_EDGES worklist first.  */
787       process_ssa_edge_worklist (&varying_ssa_edges);
788
789       /* Now process the INTERESTING_SSA_EDGES worklist.  */
790       process_ssa_edge_worklist (&interesting_ssa_edges);
791     }
792
793   ssa_prop_fini ();
794 }
795
796
797 /* Return the first VDEF operand for STMT.  */
798
799 tree
800 first_vdef (tree stmt)
801 {
802   ssa_op_iter iter;
803   tree op;
804
805   /* Simply return the first operand we arrive at.  */
806   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
807     return (op);
808
809   gcc_unreachable ();
810 }
811
812
813 /* Return true if STMT is of the form 'LHS = mem_ref', where 'mem_ref'
814    is a non-volatile pointer dereference, a structure reference or a
815    reference to a single _DECL.  Ignore volatile memory references
816    because they are not interesting for the optimizers.  */
817
818 bool
819 stmt_makes_single_load (tree stmt)
820 {
821   tree rhs;
822
823   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
824     return false;
825
826   if (ZERO_SSA_OPERANDS (stmt, SSA_OP_VDEF|SSA_OP_VUSE))
827     return false;
828
829   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
830   STRIP_NOPS (rhs);
831
832   return (!TREE_THIS_VOLATILE (rhs)
833           && (DECL_P (rhs)
834               || REFERENCE_CLASS_P (rhs)));
835 }
836
837
838 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
839    is a non-volatile pointer dereference, a structure reference or a
840    reference to a single _DECL.  Ignore volatile memory references
841    because they are not interesting for the optimizers.  */
842
843 bool
844 stmt_makes_single_store (tree stmt)
845 {
846   tree lhs;
847
848   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
849     return false;
850
851   if (ZERO_SSA_OPERANDS (stmt, SSA_OP_VDEF))
852     return false;
853
854   lhs = GIMPLE_STMT_OPERAND (stmt, 0);
855   STRIP_NOPS (lhs);
856
857   return (!TREE_THIS_VOLATILE (lhs)
858           && (DECL_P (lhs)
859               || REFERENCE_CLASS_P (lhs)));
860 }
861
862
863 /* If STMT makes a single memory load and all the virtual use operands
864    have the same value in array VALUES, return it.  Otherwise, return
865    NULL.  */
866
867 prop_value_t *
868 get_value_loaded_by (tree stmt, prop_value_t *values)
869 {
870   ssa_op_iter i;
871   tree vuse;
872   prop_value_t *prev_val = NULL;
873   prop_value_t *val = NULL;
874
875   FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
876     {
877       val = &values[SSA_NAME_VERSION (vuse)];
878       if (prev_val && prev_val->value != val->value)
879         return NULL;
880       prev_val = val;
881     }
882
883   return val;
884 }
885
886
887 /* Propagation statistics.  */
888 struct prop_stats_d
889 {
890   long num_const_prop;
891   long num_copy_prop;
892   long num_pred_folded;
893   long num_dce;
894 };
895
896 static struct prop_stats_d prop_stats;
897
898 /* Replace USE references in statement STMT with the values stored in
899    PROP_VALUE. Return true if at least one reference was replaced.  If
900    REPLACED_ADDRESSES_P is given, it will be set to true if an address
901    constant was replaced.  */
902
903 bool
904 replace_uses_in (tree stmt, bool *replaced_addresses_p,
905                  prop_value_t *prop_value)
906 {
907   bool replaced = false;
908   use_operand_p use;
909   ssa_op_iter iter;
910
911   FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
912     {
913       tree tuse = USE_FROM_PTR (use);
914       tree val = prop_value[SSA_NAME_VERSION (tuse)].value;
915
916       if (val == tuse || val == NULL_TREE)
917         continue;
918
919       if (TREE_CODE (stmt) == ASM_EXPR
920           && !may_propagate_copy_into_asm (tuse))
921         continue;
922
923       if (!may_propagate_copy (tuse, val))
924         continue;
925
926       if (TREE_CODE (val) != SSA_NAME)
927         prop_stats.num_const_prop++;
928       else
929         prop_stats.num_copy_prop++;
930
931       propagate_value (use, val);
932
933       replaced = true;
934       if (POINTER_TYPE_P (TREE_TYPE (tuse)) && replaced_addresses_p)
935         *replaced_addresses_p = true;
936     }
937
938   return replaced;
939 }
940
941
942 /* Replace the VUSE references in statement STMT with the values
943    stored in PROP_VALUE.  Return true if a reference was replaced.  If
944    REPLACED_ADDRESSES_P is given, it will be set to true if an address
945    constant was replaced.
946
947    Replacing VUSE operands is slightly more complex than replacing
948    regular USEs.  We are only interested in two types of replacements
949    here:
950    
951    1- If the value to be replaced is a constant or an SSA name for a
952       GIMPLE register, then we are making a copy/constant propagation
953       from a memory store.  For instance,
954
955         # a_3 = VDEF <a_2>
956         a.b = x_1;
957         ...
958         # VUSE <a_3>
959         y_4 = a.b;
960
961       This replacement is only possible iff STMT is an assignment
962       whose RHS is identical to the LHS of the statement that created
963       the VUSE(s) that we are replacing.  Otherwise, we may do the
964       wrong replacement:
965
966         # a_3 = VDEF <a_2>
967         # b_5 = VDEF <b_4>
968         *p = 10;
969         ...
970         # VUSE <b_5>
971         x_8 = b;
972
973       Even though 'b_5' acquires the value '10' during propagation,
974       there is no way for the propagator to tell whether the
975       replacement is correct in every reached use, because values are
976       computed at definition sites.  Therefore, when doing final
977       substitution of propagated values, we have to check each use
978       site.  Since the RHS of STMT ('b') is different from the LHS of
979       the originating statement ('*p'), we cannot replace 'b' with
980       '10'.
981
982       Similarly, when merging values from PHI node arguments,
983       propagators need to take care not to merge the same values
984       stored in different locations:
985
986                 if (...)
987                   # a_3 = VDEF <a_2>
988                   a.b = 3;
989                 else
990                   # a_4 = VDEF <a_2>
991                   a.c = 3;
992                 # a_5 = PHI <a_3, a_4>
993
994       It would be wrong to propagate '3' into 'a_5' because that
995       operation merges two stores to different memory locations.
996
997
998    2- If the value to be replaced is an SSA name for a virtual
999       register, then we simply replace each VUSE operand with its
1000       value from PROP_VALUE.  This is the same replacement done by
1001       replace_uses_in.  */
1002
1003 static bool
1004 replace_vuses_in (tree stmt, bool *replaced_addresses_p,
1005                   prop_value_t *prop_value)
1006 {
1007   bool replaced = false;
1008   ssa_op_iter iter;
1009   use_operand_p vuse;
1010
1011   if (stmt_makes_single_load (stmt))
1012     {
1013       /* If STMT is an assignment whose RHS is a single memory load,
1014          see if we are trying to propagate a constant or a GIMPLE
1015          register (case #1 above).  */
1016       prop_value_t *val = get_value_loaded_by (stmt, prop_value);
1017       tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1018
1019       if (val
1020           && val->value
1021           && (is_gimple_reg (val->value)
1022               || is_gimple_min_invariant (val->value))
1023           && simple_cst_equal (rhs, val->mem_ref) == 1)
1024
1025         {
1026           /* If we are replacing a constant address, inform our
1027              caller.  */
1028           if (TREE_CODE (val->value) != SSA_NAME
1029               && POINTER_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1)))
1030               && replaced_addresses_p)
1031             *replaced_addresses_p = true;
1032
1033           /* We can only perform the substitution if the load is done
1034              from the same memory location as the original store.
1035              Since we already know that there are no intervening
1036              stores between DEF_STMT and STMT, we only need to check
1037              that the RHS of STMT is the same as the memory reference
1038              propagated together with the value.  */
1039           GIMPLE_STMT_OPERAND (stmt, 1) = val->value;
1040
1041           if (TREE_CODE (val->value) != SSA_NAME)
1042             prop_stats.num_const_prop++;
1043           else
1044             prop_stats.num_copy_prop++;
1045
1046           /* Since we have replaced the whole RHS of STMT, there
1047              is no point in checking the other VUSEs, as they will
1048              all have the same value.  */
1049           return true;
1050         }
1051     }
1052
1053   /* Otherwise, the values for every VUSE operand must be other
1054      SSA_NAMEs that can be propagated into STMT.  */
1055   FOR_EACH_SSA_USE_OPERAND (vuse, stmt, iter, SSA_OP_VIRTUAL_USES)
1056     {
1057       tree var = USE_FROM_PTR (vuse);
1058       tree val = prop_value[SSA_NAME_VERSION (var)].value;
1059
1060       if (val == NULL_TREE || var == val)
1061         continue;
1062
1063       /* Constants and copies propagated between real and virtual
1064          operands are only possible in the cases handled above.  They
1065          should be ignored in any other context.  */
1066       if (is_gimple_min_invariant (val) || is_gimple_reg (val))
1067         continue;
1068
1069       propagate_value (vuse, val);
1070       prop_stats.num_copy_prop++;
1071       replaced = true;
1072     }
1073
1074   return replaced;
1075 }
1076
1077
1078 /* Replace propagated values into all the arguments for PHI using the
1079    values from PROP_VALUE.  */
1080
1081 static void
1082 replace_phi_args_in (tree phi, prop_value_t *prop_value)
1083 {
1084   int i;
1085   bool replaced = false;
1086   tree prev_phi = NULL;
1087
1088   if (dump_file && (dump_flags & TDF_DETAILS))
1089     prev_phi = unshare_expr (phi);
1090
1091   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1092     {
1093       tree arg = PHI_ARG_DEF (phi, i);
1094
1095       if (TREE_CODE (arg) == SSA_NAME)
1096         {
1097           tree val = prop_value[SSA_NAME_VERSION (arg)].value;
1098
1099           if (val && val != arg && may_propagate_copy (arg, val))
1100             {
1101               if (TREE_CODE (val) != SSA_NAME)
1102                 prop_stats.num_const_prop++;
1103               else
1104                 prop_stats.num_copy_prop++;
1105
1106               propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
1107               replaced = true;
1108
1109               /* If we propagated a copy and this argument flows
1110                  through an abnormal edge, update the replacement
1111                  accordingly.  */
1112               if (TREE_CODE (val) == SSA_NAME
1113                   && PHI_ARG_EDGE (phi, i)->flags & EDGE_ABNORMAL)
1114                 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1115             }
1116         }
1117     }
1118   
1119   if (replaced && dump_file && (dump_flags & TDF_DETAILS))
1120     {
1121       fprintf (dump_file, "Folded PHI node: ");
1122       print_generic_stmt (dump_file, prev_phi, TDF_SLIM);
1123       fprintf (dump_file, "           into: ");
1124       print_generic_stmt (dump_file, phi, TDF_SLIM);
1125       fprintf (dump_file, "\n");
1126     }
1127 }
1128
1129
1130 /* If STMT has a predicate whose value can be computed using the value
1131    range information computed by VRP, compute its value and return true.
1132    Otherwise, return false.  */
1133
1134 static bool
1135 fold_predicate_in (tree stmt)
1136 {
1137   tree *pred_p = NULL;
1138   bool modify_stmt_p = false;
1139   tree val;
1140
1141   if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1142       && COMPARISON_CLASS_P (GIMPLE_STMT_OPERAND (stmt, 1)))
1143     {
1144       modify_stmt_p = true;
1145       pred_p = &GIMPLE_STMT_OPERAND (stmt, 1);
1146     }
1147   else if (TREE_CODE (stmt) == COND_EXPR)
1148     pred_p = &COND_EXPR_COND (stmt);
1149   else
1150     return false;
1151
1152   if (TREE_CODE (*pred_p) == SSA_NAME)
1153     val = vrp_evaluate_conditional (EQ_EXPR,
1154                                     *pred_p,
1155                                     boolean_true_node,
1156                                     stmt);
1157   else
1158     val = vrp_evaluate_conditional (TREE_CODE (*pred_p),
1159                                     TREE_OPERAND (*pred_p, 0),
1160                                     TREE_OPERAND (*pred_p, 1),
1161                                     stmt);
1162
1163   if (val)
1164     {
1165       if (modify_stmt_p)
1166         val = fold_convert (TREE_TYPE (*pred_p), val);
1167       
1168       if (dump_file)
1169         {
1170           fprintf (dump_file, "Folding predicate ");
1171           print_generic_expr (dump_file, *pred_p, 0);
1172           fprintf (dump_file, " to ");
1173           print_generic_expr (dump_file, val, 0);
1174           fprintf (dump_file, "\n");
1175         }
1176
1177       prop_stats.num_pred_folded++;
1178       *pred_p = val;
1179       return true;
1180     }
1181
1182   return false;
1183 }
1184
1185
1186 /* Perform final substitution and folding of propagated values.
1187
1188    PROP_VALUE[I] contains the single value that should be substituted
1189    at every use of SSA name N_I.  If PROP_VALUE is NULL, no values are
1190    substituted.
1191
1192    If USE_RANGES_P is true, statements that contain predicate
1193    expressions are evaluated with a call to vrp_evaluate_conditional.
1194    This will only give meaningful results when called from tree-vrp.c
1195    (the information used by vrp_evaluate_conditional is built by the
1196    VRP pass).  
1197
1198    Return TRUE when something changed.  */
1199
1200 bool
1201 substitute_and_fold (prop_value_t *prop_value, bool use_ranges_p)
1202 {
1203   basic_block bb;
1204   bool something_changed = false;
1205
1206   if (prop_value == NULL && !use_ranges_p)
1207     return false;
1208
1209   if (dump_file && (dump_flags & TDF_DETAILS))
1210     fprintf (dump_file, "\nSubstituing values and folding statements\n\n");
1211
1212   memset (&prop_stats, 0, sizeof (prop_stats));
1213
1214   /* Substitute values in every statement of every basic block.  */
1215   FOR_EACH_BB (bb)
1216     {
1217       block_stmt_iterator i;
1218       tree phi;
1219
1220       /* Propagate known values into PHI nodes.  */
1221       if (prop_value)
1222         for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1223           replace_phi_args_in (phi, prop_value);
1224
1225       /* Propagate known values into stmts.  Do a backward walk to expose
1226          more trivially deletable stmts.  */
1227       for (i = bsi_last (bb); !bsi_end_p (i);)
1228         {
1229           bool replaced_address, did_replace;
1230           tree call, prev_stmt = NULL;
1231           tree stmt = bsi_stmt (i);
1232
1233           /* Ignore ASSERT_EXPRs.  They are used by VRP to generate
1234              range information for names and they are discarded
1235              afterwards.  */
1236           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1237               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ASSERT_EXPR)
1238             {
1239               bsi_prev (&i);
1240               continue;
1241             }
1242
1243           /* No point propagating into a stmt whose result is not used,
1244              but instead we might be able to remove a trivially dead stmt.  */
1245           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1246               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
1247               && !stmt_ann (stmt)->has_volatile_ops
1248               && has_zero_uses (GIMPLE_STMT_OPERAND (stmt, 0))
1249               && !tree_could_throw_p (stmt)
1250               && (!(call = get_call_expr_in (stmt))
1251                   || !TREE_SIDE_EFFECTS (call)))
1252             {
1253               block_stmt_iterator i2;
1254               if (dump_file && dump_flags & TDF_DETAILS)
1255                 {
1256                   fprintf (dump_file, "Removing dead stmt ");
1257                   print_generic_expr (dump_file, stmt, 0);
1258                   fprintf (dump_file, "\n");
1259                 }
1260               prop_stats.num_dce++;
1261               bsi_prev (&i);
1262               i2 = bsi_for_stmt (stmt);
1263               bsi_remove (&i2, true);
1264               release_defs (stmt);
1265               continue;
1266             }
1267
1268           /* Record the state of the statement before replacements.  */
1269           push_stmt_changes (bsi_stmt_ptr (i));
1270
1271           /* Replace the statement with its folded version and mark it
1272              folded.  */
1273           did_replace = false;
1274           replaced_address = false;
1275           if (dump_file && (dump_flags & TDF_DETAILS))
1276             prev_stmt = unshare_expr (stmt);
1277
1278           /* If we have range information, see if we can fold
1279              predicate expressions.  */
1280           if (use_ranges_p)
1281             did_replace = fold_predicate_in (stmt);
1282
1283           if (prop_value)
1284             {
1285               /* Only replace real uses if we couldn't fold the
1286                  statement using value range information (value range
1287                  information is not collected on virtuals, so we only
1288                  need to check this for real uses).  */
1289               if (!did_replace)
1290                 did_replace |= replace_uses_in (stmt, &replaced_address,
1291                                                 prop_value);
1292
1293               did_replace |= replace_vuses_in (stmt, &replaced_address,
1294                                                prop_value);
1295             }
1296
1297           /* If we made a replacement, fold and cleanup the statement.  */
1298           if (did_replace)
1299             {
1300               tree old_stmt = stmt;
1301               tree rhs;
1302
1303               fold_stmt (bsi_stmt_ptr (i));
1304               stmt = bsi_stmt (i);
1305
1306               /* If we cleaned up EH information from the statement,
1307                  remove EH edges.  */
1308               if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1309                 tree_purge_dead_eh_edges (bb);
1310
1311               rhs = get_rhs (stmt);
1312               if (TREE_CODE (rhs) == ADDR_EXPR)
1313                 recompute_tree_invariant_for_addr_expr (rhs);
1314
1315               if (dump_file && (dump_flags & TDF_DETAILS))
1316                 {
1317                   fprintf (dump_file, "Folded statement: ");
1318                   print_generic_stmt (dump_file, prev_stmt, TDF_SLIM);
1319                   fprintf (dump_file, "            into: ");
1320                   print_generic_stmt (dump_file, stmt, TDF_SLIM);
1321                   fprintf (dump_file, "\n");
1322                 }
1323
1324               /* Determine what needs to be done to update the SSA form.  */
1325               pop_stmt_changes (bsi_stmt_ptr (i));
1326               something_changed = true;
1327             }
1328           else
1329             {
1330               /* The statement was not modified, discard the change buffer.  */
1331               discard_stmt_changes (bsi_stmt_ptr (i));
1332             }
1333
1334           /* Some statements may be simplified using ranges.  For
1335              example, division may be replaced by shifts, modulo
1336              replaced with bitwise and, etc.   Do this after 
1337              substituting constants, folding, etc so that we're
1338              presented with a fully propagated, canonicalized
1339              statement.  */
1340           if (use_ranges_p)
1341             simplify_stmt_using_ranges (stmt);
1342
1343           bsi_prev (&i);
1344         }
1345     }
1346
1347   statistics_counter_event (cfun, "Constants propagated",
1348                             prop_stats.num_const_prop);
1349   statistics_counter_event (cfun, "Copies propagated",
1350                             prop_stats.num_copy_prop);
1351   statistics_counter_event (cfun, "Predicates folded",
1352                             prop_stats.num_pred_folded);
1353   statistics_counter_event (cfun, "Statements deleted",
1354                             prop_stats.num_dce);
1355   return something_changed;
1356 }
1357
1358 #include "gt-tree-ssa-propagate.h"