OSDN Git Service

2004-09-28 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-propagate.c
1 /* Generic SSA value propagation engine.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 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 2, 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 COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "ggc.h"
31 #include "basic-block.h"
32 #include "output.h"
33 #include "errors.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "timevar.h"
38 #include "tree-dump.h"
39 #include "tree-flow.h"
40 #include "tree-pass.h"
41 #include "tree-ssa-propagate.h"
42 #include "langhooks.h"
43
44
45 /* This file implements a generic value propagation engine based on
46    the same propagation used by the SSA-CCP algorithm [1].
47
48    Propagation is performed by simulating the execution of every
49    statement that produces the value being propagated.  Simulation
50    proceeds as follows:
51
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).
55
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:
59
60         SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
61             interest and does not affect any of the work lists.
62
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.
68
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.
75
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.
79
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.
83
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.
87
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.
91
92         INTERESTING_SSA_EDGES contains the list of statements that
93             feed from statements that produce an SSA_PROP_INTERESTING
94             result.
95
96    5- Simulation terminates when all three work lists are drained.
97
98    Before calling ssa_propagate, it is important to clear
99    DONT_SIMULATE_AGAIN 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.
102
103    It is also important to compute def-use information before calling
104    ssa_propagate.
105
106    References:
107
108      [1] Constant propagation with conditional branches,
109          Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
110
111      [2] Building an Optimizing Compiler,
112          Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
113
114      [3] Advanced Compiler Design and Implementation,
115          Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6  */
116
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;
120
121 /* Use the TREE_DEPRECATED bitflag to mark statements that have been
122    added to one of the SSA edges worklists.  This flag is used to
123    avoid visiting statements unnecessarily when draining an SSA edge
124    worklist.  If while 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.  */
127 #define STMT_IN_SSA_EDGE_WORKLIST(T)    TREE_DEPRECATED (T)
128
129 /* A bitmap to keep track of executable blocks in the CFG.  */
130 static sbitmap executable_blocks;
131
132 /* Array of control flow edges on the worklist.  */
133 static GTY(()) varray_type cfg_blocks = NULL;
134
135 static unsigned int cfg_blocks_num = 0;
136 static int cfg_blocks_tail;
137 static int cfg_blocks_head;
138
139 static sbitmap bb_in_list;
140
141 /* Worklist of SSA edges which will need reexamination as their
142    definition has changed.  SSA edges are def-use edges in the SSA
143    web.  For each D-U edge, we store the target statement or PHI node
144    U.  */
145 static GTY(()) varray_type interesting_ssa_edges;
146
147 /* Identical to INTERESTING_SSA_EDGES.  For performance reasons, the
148    list of SSA edges is split into two.  One contains all SSA edges
149    who need to be reexamined because their lattice value changed to
150    varying (this worklist), and the other contains all other SSA edges
151    to be reexamined (INTERESTING_SSA_EDGES).
152
153    Since most values in the program are VARYING, the ideal situation
154    is to move them to that lattice value as quickly as possible.
155    Thus, it doesn't make sense to process any other type of lattice
156    value until all VARYING values are propagated fully, which is one
157    thing using the VARYING worklist achieves.  In addition, if we
158    don't use a separate worklist for VARYING edges, we end up with
159    situations where lattice values move from
160    UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING.  */
161 static GTY(()) varray_type varying_ssa_edges;
162
163
164 /* Return true if the block worklist empty.  */
165
166 static inline bool
167 cfg_blocks_empty_p (void)
168 {
169   return (cfg_blocks_num == 0);
170 }
171
172
173 /* Add a basic block to the worklist.  */
174
175 static void 
176 cfg_blocks_add (basic_block bb)
177 {
178   if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
179     return;
180
181   if (TEST_BIT (bb_in_list, bb->index))
182     return;
183
184   if (cfg_blocks_empty_p ())
185     {
186       cfg_blocks_tail = cfg_blocks_head = 0;
187       cfg_blocks_num = 1;
188     }
189   else
190     {
191       cfg_blocks_num++;
192       if (cfg_blocks_num > VARRAY_SIZE (cfg_blocks))
193         {
194           /* We have to grow the array now.  Adjust to queue to occupy the
195              full space of the original array.  */
196           cfg_blocks_tail = VARRAY_SIZE (cfg_blocks);
197           cfg_blocks_head = 0;
198           VARRAY_GROW (cfg_blocks, 2 * VARRAY_SIZE (cfg_blocks));
199         }
200       else
201         cfg_blocks_tail = (cfg_blocks_tail + 1) % VARRAY_SIZE (cfg_blocks);
202     }
203
204   VARRAY_BB (cfg_blocks, cfg_blocks_tail) = bb;
205   SET_BIT (bb_in_list, bb->index);
206 }
207
208
209 /* Remove a block from the worklist.  */
210
211 static basic_block
212 cfg_blocks_get (void)
213 {
214   basic_block bb;
215
216   bb = VARRAY_BB (cfg_blocks, cfg_blocks_head);
217
218   gcc_assert (!cfg_blocks_empty_p ());
219   gcc_assert (bb);
220
221   cfg_blocks_head = (cfg_blocks_head + 1) % VARRAY_SIZE (cfg_blocks);
222   --cfg_blocks_num;
223   RESET_BIT (bb_in_list, bb->index);
224
225   return bb;
226 }
227
228
229 /* We have just defined a new value for VAR.  If IS_VARYING is true,
230    add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
231    them to INTERESTING_SSA_EDGES.  */
232
233 static void
234 add_ssa_edge (tree var, bool is_varying)
235 {
236   tree stmt = SSA_NAME_DEF_STMT (var);
237   dataflow_t df = get_immediate_uses (stmt);
238   int num_uses = num_immediate_uses (df);
239   int i;
240
241   for (i = 0; i < num_uses; i++)
242     {
243       tree use_stmt = immediate_use (df, i);
244
245       if (!DONT_SIMULATE_AGAIN (use_stmt)
246           && !STMT_IN_SSA_EDGE_WORKLIST (use_stmt))
247         {
248           STMT_IN_SSA_EDGE_WORKLIST (use_stmt) = 1;
249           if (is_varying)
250             VARRAY_PUSH_TREE (varying_ssa_edges, use_stmt);
251           else
252             VARRAY_PUSH_TREE (interesting_ssa_edges, use_stmt);
253         }
254     }
255 }
256
257
258 /* Add edge E to the control flow worklist.  */
259
260 static void
261 add_control_edge (edge e)
262 {
263   basic_block bb = e->dest;
264   if (bb == EXIT_BLOCK_PTR)
265     return;
266
267   /* If the edge had already been executed, skip it.  */
268   if (e->flags & EDGE_EXECUTABLE)
269     return;
270
271   e->flags |= EDGE_EXECUTABLE;
272
273   /* If the block is already in the list, we're done.  */
274   if (TEST_BIT (bb_in_list, bb->index))
275     return;
276
277   cfg_blocks_add (bb);
278
279   if (dump_file && (dump_flags & TDF_DETAILS))
280     fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
281         e->src->index, e->dest->index);
282 }
283
284
285 /* Simulate the execution of STMT and update the work lists accordingly.  */
286
287 static void
288 simulate_stmt (tree stmt)
289 {
290   enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
291   edge taken_edge = NULL;
292   tree output_name = NULL_TREE;
293
294   /* Don't bother visiting statements that are already
295      considered varying by the propagator.  */
296   if (DONT_SIMULATE_AGAIN (stmt))
297     return;
298
299   if (TREE_CODE (stmt) == PHI_NODE)
300     {
301       val = ssa_prop_visit_phi (stmt);
302       output_name = PHI_RESULT (stmt);
303     }
304   else
305     val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
306
307   if (val == SSA_PROP_VARYING)
308     {
309       DONT_SIMULATE_AGAIN (stmt) = 1;
310
311       /* If the statement produced a new varying value, add the SSA
312          edges coming out of OUTPUT_NAME.  */
313       if (output_name)
314         add_ssa_edge (output_name, true);
315
316       /* If STMT transfers control out of its basic block, add
317          all outgoing edges to the work list.  */
318       if (stmt_ends_bb_p (stmt))
319         {
320           edge e;
321           edge_iterator ei;
322           basic_block bb = bb_for_stmt (stmt);
323           FOR_EACH_EDGE (e, ei, bb->succs)
324             add_control_edge (e);
325         }
326     }
327   else if (val == SSA_PROP_INTERESTING)
328     {
329       /* If the statement produced new value, add the SSA edges coming
330          out of OUTPUT_NAME.  */
331       if (output_name)
332         add_ssa_edge (output_name, false);
333
334       /* If we know which edge is going to be taken out of this block,
335          add it to the CFG work list.  */
336       if (taken_edge)
337         add_control_edge (taken_edge);
338     }
339 }
340
341 /* Process an SSA edge worklist.  WORKLIST is the SSA edge worklist to
342    drain.  This pops statements off the given WORKLIST and processes
343    them until there are no more statements on WORKLIST.  */
344
345 static void
346 process_ssa_edge_worklist (varray_type *worklist)
347 {
348   /* Drain the entire worklist.  */
349   while (VARRAY_ACTIVE_SIZE (*worklist) > 0)
350     {
351       basic_block bb;
352
353       /* Pull the statement to simulate off the worklist.  */
354       tree stmt = VARRAY_TOP_TREE (*worklist);
355       VARRAY_POP (*worklist);
356
357       /* If this statement was already visited by simulate_block, then
358          we don't need to visit it again here.  */
359       if (!STMT_IN_SSA_EDGE_WORKLIST (stmt))
360         continue;
361
362       /* STMT is no longer in a worklist.  */
363       STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
364
365       if (dump_file && (dump_flags & TDF_DETAILS))
366         {
367           fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
368           print_generic_stmt (dump_file, stmt, dump_flags);
369         }
370
371       bb = bb_for_stmt (stmt);
372
373       /* PHI nodes are always visited, regardless of whether or not
374          the destination block is executable.  Otherwise, visit the
375          statement only if its block is marked executable.  */
376       if (TREE_CODE (stmt) == PHI_NODE
377           || TEST_BIT (executable_blocks, bb->index))
378         simulate_stmt (stmt);
379     }
380 }
381
382
383 /* Simulate the execution of BLOCK.  Evaluate the statement associated
384    with each variable reference inside the block.  */
385
386 static void
387 simulate_block (basic_block block)
388 {
389   tree phi;
390
391   /* There is nothing to do for the exit block.  */
392   if (block == EXIT_BLOCK_PTR)
393     return;
394
395   if (dump_file && (dump_flags & TDF_DETAILS))
396     fprintf (dump_file, "\nSimulating block %d\n", block->index);
397
398   /* Always simulate PHI nodes, even if we have simulated this block
399      before.  */
400   for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
401     simulate_stmt (phi);
402
403   /* If this is the first time we've simulated this block, then we
404      must simulate each of its statements.  */
405   if (!TEST_BIT (executable_blocks, block->index))
406     {
407       block_stmt_iterator j;
408       unsigned int normal_edge_count;
409       edge e, normal_edge;
410       edge_iterator ei;
411
412       /* Note that we have simulated this block.  */
413       SET_BIT (executable_blocks, block->index);
414
415       for (j = bsi_start (block); !bsi_end_p (j); bsi_next (&j))
416         {
417           tree stmt = bsi_stmt (j);
418
419           /* If this statement is already in the worklist then
420              "cancel" it.  The reevaluation implied by the worklist
421              entry will produce the same value we generate here and
422              thus reevaluating it again from the worklist is
423              pointless.  */
424           if (STMT_IN_SSA_EDGE_WORKLIST (stmt))
425             STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
426
427           simulate_stmt (stmt);
428         }
429
430       /* We can not predict when abnormal edges will be executed, so
431          once a block is considered executable, we consider any
432          outgoing abnormal edges as executable.
433
434          At the same time, if this block has only one successor that is
435          reached by non-abnormal edges, then add that successor to the
436          worklist.  */
437       normal_edge_count = 0;
438       normal_edge = NULL;
439       FOR_EACH_EDGE (e, ei, block->succs)
440         {
441           if (e->flags & EDGE_ABNORMAL)
442             add_control_edge (e);
443           else
444             {
445               normal_edge_count++;
446               normal_edge = e;
447             }
448         }
449
450       if (normal_edge_count == 1)
451         add_control_edge (normal_edge);
452     }
453 }
454
455
456 /* Initialize local data structures and work lists.  */
457
458 static void
459 ssa_prop_init (void)
460 {
461   edge e;
462   edge_iterator ei;
463   basic_block bb;
464
465   /* Worklists of SSA edges.  */
466   VARRAY_TREE_INIT (interesting_ssa_edges, 20, "interesting_ssa_edges");
467   VARRAY_TREE_INIT (varying_ssa_edges, 20, "varying_ssa_edges");
468
469   executable_blocks = sbitmap_alloc (last_basic_block);
470   sbitmap_zero (executable_blocks);
471
472   bb_in_list = sbitmap_alloc (last_basic_block);
473   sbitmap_zero (bb_in_list);
474
475   if (dump_file && (dump_flags & TDF_DETAILS))
476     dump_immediate_uses (dump_file);
477
478   VARRAY_BB_INIT (cfg_blocks, 20, "cfg_blocks");
479
480   /* Initially assume that every edge in the CFG is not executable.  */
481   FOR_EACH_BB (bb)
482     {
483       block_stmt_iterator si;
484
485       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
486         STMT_IN_SSA_EDGE_WORKLIST (bsi_stmt (si)) = 0;
487
488       FOR_EACH_EDGE (e, ei, bb->succs)
489         e->flags &= ~EDGE_EXECUTABLE;
490     }
491
492   /* Seed the algorithm by adding the successors of the entry block to the
493      edge worklist.  */
494   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
495     {
496       if (e->dest != EXIT_BLOCK_PTR)
497         {
498           e->flags |= EDGE_EXECUTABLE;
499           cfg_blocks_add (e->dest);
500         }
501     }
502 }
503
504
505 /* Free allocated storage.  */
506
507 static void
508 ssa_prop_fini (void)
509 {
510   interesting_ssa_edges = NULL;
511   varying_ssa_edges = NULL;
512   cfg_blocks = NULL;
513   sbitmap_free (bb_in_list);
514   sbitmap_free (executable_blocks);
515   free_df ();
516 }
517
518
519 /* Get the main expression from statement STMT.  */
520
521 tree
522 get_rhs (tree stmt)
523 {
524   enum tree_code code = TREE_CODE (stmt);
525
526   switch (code)
527     {
528     case RETURN_EXPR:
529       stmt = TREE_OPERAND (stmt, 0);
530       if (!stmt || TREE_CODE (stmt) != MODIFY_EXPR)
531         return stmt;
532       /* FALLTHRU */
533
534     case MODIFY_EXPR:
535       stmt = TREE_OPERAND (stmt, 1);
536       if (TREE_CODE (stmt) == WITH_SIZE_EXPR)
537         return TREE_OPERAND (stmt, 0);
538       else
539         return stmt;
540
541     case COND_EXPR:
542       return COND_EXPR_COND (stmt);
543     case SWITCH_EXPR:
544       return SWITCH_COND (stmt);
545     case GOTO_EXPR:
546       return GOTO_DESTINATION (stmt);
547     case LABEL_EXPR:
548       return LABEL_EXPR_LABEL (stmt);
549
550     default:
551       return stmt;
552     }
553 }
554
555
556 /* Set the main expression of *STMT_P to EXPR.  If EXPR is not a valid
557    GIMPLE expression no changes are done and the function returns
558    false.  */
559
560 bool
561 set_rhs (tree *stmt_p, tree expr)
562 {
563   tree stmt = *stmt_p, op;
564   enum tree_code code = TREE_CODE (expr);
565   stmt_ann_t ann;
566   tree var;
567   ssa_op_iter iter;
568
569   /* Verify the constant folded result is valid gimple.  */
570   if (TREE_CODE_CLASS (code) == tcc_binary)
571     {
572       if (!is_gimple_val (TREE_OPERAND (expr, 0))
573           || !is_gimple_val (TREE_OPERAND (expr, 1)))
574         return false;
575     }
576   else if (TREE_CODE_CLASS (code) == tcc_unary)
577     {
578       if (!is_gimple_val (TREE_OPERAND (expr, 0)))
579         return false;
580     }
581   else if (code == COMPOUND_EXPR)
582     return false;
583
584   switch (TREE_CODE (stmt))
585     {
586     case RETURN_EXPR:
587       op = TREE_OPERAND (stmt, 0);
588       if (TREE_CODE (op) != MODIFY_EXPR)
589         {
590           TREE_OPERAND (stmt, 0) = expr;
591           break;
592         }
593       stmt = op;
594       /* FALLTHRU */
595
596     case MODIFY_EXPR:
597       op = TREE_OPERAND (stmt, 1);
598       if (TREE_CODE (op) == WITH_SIZE_EXPR)
599         stmt = op;
600       TREE_OPERAND (stmt, 1) = expr;
601       break;
602
603     case COND_EXPR:
604       COND_EXPR_COND (stmt) = expr;
605       break;
606     case SWITCH_EXPR:
607       SWITCH_COND (stmt) = expr;
608       break;
609     case GOTO_EXPR:
610       GOTO_DESTINATION (stmt) = expr;
611       break;
612     case LABEL_EXPR:
613       LABEL_EXPR_LABEL (stmt) = expr;
614       break;
615
616     default:
617       /* Replace the whole statement with EXPR.  If EXPR has no side
618          effects, then replace *STMT_P with an empty statement.  */
619       ann = stmt_ann (stmt);
620       *stmt_p = TREE_SIDE_EFFECTS (expr) ? expr : build_empty_stmt ();
621       (*stmt_p)->common.ann = (tree_ann_t) ann;
622
623       if (TREE_SIDE_EFFECTS (expr))
624         {
625           /* Fix all the SSA_NAMEs created by *STMT_P to point to its new
626              replacement.  */
627           FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_DEFS)
628             {
629               if (TREE_CODE (var) == SSA_NAME)
630                 SSA_NAME_DEF_STMT (var) = *stmt_p;
631             }
632         }
633       break;
634     }
635
636   return true;
637 }
638
639
640 /* Entry point to the propagation engine.
641
642    VISIT_STMT is called for every statement visited.
643    VISIT_PHI is called for every PHI node visited.  */
644
645 void
646 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
647                ssa_prop_visit_phi_fn visit_phi)
648 {
649   ssa_prop_visit_stmt = visit_stmt;
650   ssa_prop_visit_phi = visit_phi;
651
652   ssa_prop_init ();
653
654   /* Iterate until the worklists are empty.  */
655   while (!cfg_blocks_empty_p () 
656          || VARRAY_ACTIVE_SIZE (interesting_ssa_edges) > 0
657          || VARRAY_ACTIVE_SIZE (varying_ssa_edges) > 0)
658     {
659       if (!cfg_blocks_empty_p ())
660         {
661           /* Pull the next block to simulate off the worklist.  */
662           basic_block dest_block = cfg_blocks_get ();
663           simulate_block (dest_block);
664         }
665
666       /* In order to move things to varying as quickly as
667          possible,process the VARYING_SSA_EDGES worklist first.  */
668       process_ssa_edge_worklist (&varying_ssa_edges);
669
670       /* Now process the INTERESTING_SSA_EDGES worklist.  */
671       process_ssa_edge_worklist (&interesting_ssa_edges);
672     }
673
674   ssa_prop_fini ();
675 }
676
677 #include "gt-tree-ssa-propagate.h"