]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-propagate.c
Fix failure with pragma once where buffer is NULL and buffer_valid is true.
[thirdparty/gcc.git] / gcc / tree-ssa-propagate.c
CommitLineData
750628d8 1/* Generic SSA value propagation engine.
0bca51f0 2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
750628d8
DN
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
366ccddb
KC
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
750628d8
DN
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"
750628d8
DN
33#include "expr.h"
34#include "function.h"
35#include "diagnostic.h"
36#include "timevar.h"
37#include "tree-dump.h"
38#include "tree-flow.h"
39#include "tree-pass.h"
40#include "tree-ssa-propagate.h"
41#include "langhooks.h"
78492bf5
SB
42#include "varray.h"
43#include "vec.h"
750628d8
DN
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
766ff1b1 53 the CFG worklist is seeded with all the statements in the entry
750628d8
DN
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
2a7e31df 71 statements that feed from S. Furthermore, if S is a
750628d8
DN
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
766ff1b1 78 described in #2.
750628d8
DN
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. */
118static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
119static 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. */
130static sbitmap executable_blocks;
131
132/* Array of control flow edges on the worklist. */
133static GTY(()) varray_type cfg_blocks = NULL;
134
135static unsigned int cfg_blocks_num = 0;
136static int cfg_blocks_tail;
137static int cfg_blocks_head;
138
139static 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. */
d4e6fecb 145static GTY(()) VEC(tree,gc) *interesting_ssa_edges;
750628d8
DN
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. */
d4e6fecb 161static GTY(()) VEC(tree,gc) *varying_ssa_edges;
750628d8
DN
162
163
164/* Return true if the block worklist empty. */
165
166static inline bool
167cfg_blocks_empty_p (void)
168{
169 return (cfg_blocks_num == 0);
170}
171
172
78492bf5 173/* Add a basic block to the worklist. The block must not be already
39850c0b 174 in the worklist, and it must not be the ENTRY or EXIT block. */
750628d8
DN
175
176static void
177cfg_blocks_add (basic_block bb)
178{
39850c0b 179 gcc_assert (bb != ENTRY_BLOCK_PTR && bb != EXIT_BLOCK_PTR);
78492bf5 180 gcc_assert (!TEST_BIT (bb_in_list, bb->index));
750628d8
DN
181
182 if (cfg_blocks_empty_p ())
183 {
184 cfg_blocks_tail = cfg_blocks_head = 0;
185 cfg_blocks_num = 1;
186 }
187 else
188 {
189 cfg_blocks_num++;
190 if (cfg_blocks_num > VARRAY_SIZE (cfg_blocks))
191 {
192 /* We have to grow the array now. Adjust to queue to occupy the
193 full space of the original array. */
194 cfg_blocks_tail = VARRAY_SIZE (cfg_blocks);
195 cfg_blocks_head = 0;
196 VARRAY_GROW (cfg_blocks, 2 * VARRAY_SIZE (cfg_blocks));
197 }
198 else
199 cfg_blocks_tail = (cfg_blocks_tail + 1) % VARRAY_SIZE (cfg_blocks);
200 }
201
202 VARRAY_BB (cfg_blocks, cfg_blocks_tail) = bb;
203 SET_BIT (bb_in_list, bb->index);
204}
205
206
207/* Remove a block from the worklist. */
208
209static basic_block
210cfg_blocks_get (void)
211{
212 basic_block bb;
213
214 bb = VARRAY_BB (cfg_blocks, cfg_blocks_head);
215
1e128c5f
GB
216 gcc_assert (!cfg_blocks_empty_p ());
217 gcc_assert (bb);
750628d8
DN
218
219 cfg_blocks_head = (cfg_blocks_head + 1) % VARRAY_SIZE (cfg_blocks);
220 --cfg_blocks_num;
221 RESET_BIT (bb_in_list, bb->index);
222
223 return bb;
224}
225
226
227/* We have just defined a new value for VAR. If IS_VARYING is true,
228 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
229 them to INTERESTING_SSA_EDGES. */
230
231static void
232add_ssa_edge (tree var, bool is_varying)
233{
f430bae8
AM
234 imm_use_iterator iter;
235 use_operand_p use_p;
750628d8 236
f430bae8 237 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
750628d8 238 {
f430bae8 239 tree use_stmt = USE_STMT (use_p);
750628d8
DN
240
241 if (!DONT_SIMULATE_AGAIN (use_stmt)
242 && !STMT_IN_SSA_EDGE_WORKLIST (use_stmt))
243 {
244 STMT_IN_SSA_EDGE_WORKLIST (use_stmt) = 1;
245 if (is_varying)
d4e6fecb 246 VEC_safe_push (tree, gc, varying_ssa_edges, use_stmt);
750628d8 247 else
d4e6fecb 248 VEC_safe_push (tree, gc, interesting_ssa_edges, use_stmt);
750628d8
DN
249 }
250 }
251}
252
253
254/* Add edge E to the control flow worklist. */
255
256static void
257add_control_edge (edge e)
258{
259 basic_block bb = e->dest;
260 if (bb == EXIT_BLOCK_PTR)
261 return;
262
263 /* If the edge had already been executed, skip it. */
264 if (e->flags & EDGE_EXECUTABLE)
265 return;
266
267 e->flags |= EDGE_EXECUTABLE;
268
269 /* If the block is already in the list, we're done. */
270 if (TEST_BIT (bb_in_list, bb->index))
271 return;
272
273 cfg_blocks_add (bb);
274
275 if (dump_file && (dump_flags & TDF_DETAILS))
276 fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
277 e->src->index, e->dest->index);
278}
279
280
281/* Simulate the execution of STMT and update the work lists accordingly. */
282
283static void
284simulate_stmt (tree stmt)
285{
286 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
287 edge taken_edge = NULL;
288 tree output_name = NULL_TREE;
289
290 /* Don't bother visiting statements that are already
291 considered varying by the propagator. */
292 if (DONT_SIMULATE_AGAIN (stmt))
293 return;
294
295 if (TREE_CODE (stmt) == PHI_NODE)
296 {
297 val = ssa_prop_visit_phi (stmt);
298 output_name = PHI_RESULT (stmt);
299 }
300 else
301 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
302
303 if (val == SSA_PROP_VARYING)
304 {
305 DONT_SIMULATE_AGAIN (stmt) = 1;
306
307 /* If the statement produced a new varying value, add the SSA
308 edges coming out of OUTPUT_NAME. */
309 if (output_name)
310 add_ssa_edge (output_name, true);
311
312 /* If STMT transfers control out of its basic block, add
313 all outgoing edges to the work list. */
314 if (stmt_ends_bb_p (stmt))
315 {
316 edge e;
628f6a4e 317 edge_iterator ei;
750628d8 318 basic_block bb = bb_for_stmt (stmt);
628f6a4e 319 FOR_EACH_EDGE (e, ei, bb->succs)
750628d8
DN
320 add_control_edge (e);
321 }
322 }
323 else if (val == SSA_PROP_INTERESTING)
324 {
325 /* If the statement produced new value, add the SSA edges coming
326 out of OUTPUT_NAME. */
327 if (output_name)
328 add_ssa_edge (output_name, false);
329
330 /* If we know which edge is going to be taken out of this block,
331 add it to the CFG work list. */
332 if (taken_edge)
333 add_control_edge (taken_edge);
334 }
335}
336
337/* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
338 drain. This pops statements off the given WORKLIST and processes
78492bf5
SB
339 them until there are no more statements on WORKLIST.
340 We take a pointer to WORKLIST because it may be reallocated when an
341 SSA edge is added to it in simulate_stmt. */
750628d8
DN
342
343static void
d4e6fecb 344process_ssa_edge_worklist (VEC(tree,gc) **worklist)
750628d8
DN
345{
346 /* Drain the entire worklist. */
78492bf5 347 while (VEC_length (tree, *worklist) > 0)
750628d8
DN
348 {
349 basic_block bb;
350
351 /* Pull the statement to simulate off the worklist. */
78492bf5 352 tree stmt = VEC_pop (tree, *worklist);
750628d8
DN
353
354 /* If this statement was already visited by simulate_block, then
355 we don't need to visit it again here. */
356 if (!STMT_IN_SSA_EDGE_WORKLIST (stmt))
357 continue;
358
359 /* STMT is no longer in a worklist. */
360 STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
361
362 if (dump_file && (dump_flags & TDF_DETAILS))
363 {
364 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
365 print_generic_stmt (dump_file, stmt, dump_flags);
366 }
367
368 bb = bb_for_stmt (stmt);
369
370 /* PHI nodes are always visited, regardless of whether or not
371 the destination block is executable. Otherwise, visit the
372 statement only if its block is marked executable. */
373 if (TREE_CODE (stmt) == PHI_NODE
374 || TEST_BIT (executable_blocks, bb->index))
375 simulate_stmt (stmt);
376 }
377}
378
379
380/* Simulate the execution of BLOCK. Evaluate the statement associated
381 with each variable reference inside the block. */
382
383static void
384simulate_block (basic_block block)
385{
386 tree phi;
387
388 /* There is nothing to do for the exit block. */
389 if (block == EXIT_BLOCK_PTR)
390 return;
391
392 if (dump_file && (dump_flags & TDF_DETAILS))
393 fprintf (dump_file, "\nSimulating block %d\n", block->index);
394
395 /* Always simulate PHI nodes, even if we have simulated this block
396 before. */
397 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
398 simulate_stmt (phi);
399
400 /* If this is the first time we've simulated this block, then we
401 must simulate each of its statements. */
402 if (!TEST_BIT (executable_blocks, block->index))
403 {
404 block_stmt_iterator j;
405 unsigned int normal_edge_count;
406 edge e, normal_edge;
628f6a4e 407 edge_iterator ei;
750628d8
DN
408
409 /* Note that we have simulated this block. */
410 SET_BIT (executable_blocks, block->index);
411
412 for (j = bsi_start (block); !bsi_end_p (j); bsi_next (&j))
413 {
414 tree stmt = bsi_stmt (j);
415
416 /* If this statement is already in the worklist then
417 "cancel" it. The reevaluation implied by the worklist
418 entry will produce the same value we generate here and
419 thus reevaluating it again from the worklist is
420 pointless. */
421 if (STMT_IN_SSA_EDGE_WORKLIST (stmt))
422 STMT_IN_SSA_EDGE_WORKLIST (stmt) = 0;
423
424 simulate_stmt (stmt);
425 }
426
427 /* We can not predict when abnormal edges will be executed, so
428 once a block is considered executable, we consider any
429 outgoing abnormal edges as executable.
430
431 At the same time, if this block has only one successor that is
432 reached by non-abnormal edges, then add that successor to the
433 worklist. */
434 normal_edge_count = 0;
435 normal_edge = NULL;
628f6a4e 436 FOR_EACH_EDGE (e, ei, block->succs)
750628d8
DN
437 {
438 if (e->flags & EDGE_ABNORMAL)
439 add_control_edge (e);
440 else
441 {
442 normal_edge_count++;
443 normal_edge = e;
444 }
445 }
446
447 if (normal_edge_count == 1)
448 add_control_edge (normal_edge);
449 }
450}
451
452
453/* Initialize local data structures and work lists. */
454
455static void
456ssa_prop_init (void)
457{
458 edge e;
628f6a4e 459 edge_iterator ei;
750628d8 460 basic_block bb;
0bca51f0 461 size_t i;
750628d8
DN
462
463 /* Worklists of SSA edges. */
d4e6fecb
NS
464 interesting_ssa_edges = VEC_alloc (tree, gc, 20);
465 varying_ssa_edges = VEC_alloc (tree, gc, 20);
750628d8
DN
466
467 executable_blocks = sbitmap_alloc (last_basic_block);
468 sbitmap_zero (executable_blocks);
469
470 bb_in_list = sbitmap_alloc (last_basic_block);
471 sbitmap_zero (bb_in_list);
472
473 if (dump_file && (dump_flags & TDF_DETAILS))
474 dump_immediate_uses (dump_file);
475
476 VARRAY_BB_INIT (cfg_blocks, 20, "cfg_blocks");
477
0bca51f0
DN
478 /* Initialize the values for every SSA_NAME. */
479 for (i = 1; i < num_ssa_names; i++)
480 if (ssa_name (i))
481 SSA_NAME_VALUE (ssa_name (i)) = NULL_TREE;
482
483 /* Initially assume that every edge in the CFG is not executable.
0777d852
DN
484 (including the edges coming out of ENTRY_BLOCK_PTR). */
485 FOR_ALL_BB (bb)
750628d8
DN
486 {
487 block_stmt_iterator si;
488
489 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
490 STMT_IN_SSA_EDGE_WORKLIST (bsi_stmt (si)) = 0;
491
628f6a4e 492 FOR_EACH_EDGE (e, ei, bb->succs)
750628d8
DN
493 e->flags &= ~EDGE_EXECUTABLE;
494 }
495
496 /* Seed the algorithm by adding the successors of the entry block to the
497 edge worklist. */
628f6a4e 498 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
39850c0b 499 add_control_edge (e);
750628d8
DN
500}
501
502
503/* Free allocated storage. */
504
505static void
506ssa_prop_fini (void)
507{
d4e6fecb
NS
508 VEC_free (tree, gc, interesting_ssa_edges);
509 VEC_free (tree, gc, varying_ssa_edges);
750628d8
DN
510 cfg_blocks = NULL;
511 sbitmap_free (bb_in_list);
512 sbitmap_free (executable_blocks);
750628d8
DN
513}
514
515
516/* Get the main expression from statement STMT. */
517
518tree
519get_rhs (tree stmt)
520{
521 enum tree_code code = TREE_CODE (stmt);
522
523 switch (code)
524 {
525 case RETURN_EXPR:
526 stmt = TREE_OPERAND (stmt, 0);
527 if (!stmt || TREE_CODE (stmt) != MODIFY_EXPR)
528 return stmt;
529 /* FALLTHRU */
530
531 case MODIFY_EXPR:
532 stmt = TREE_OPERAND (stmt, 1);
533 if (TREE_CODE (stmt) == WITH_SIZE_EXPR)
534 return TREE_OPERAND (stmt, 0);
535 else
536 return stmt;
537
538 case COND_EXPR:
539 return COND_EXPR_COND (stmt);
540 case SWITCH_EXPR:
541 return SWITCH_COND (stmt);
542 case GOTO_EXPR:
543 return GOTO_DESTINATION (stmt);
544 case LABEL_EXPR:
545 return LABEL_EXPR_LABEL (stmt);
546
547 default:
548 return stmt;
549 }
550}
551
552
553/* Set the main expression of *STMT_P to EXPR. If EXPR is not a valid
554 GIMPLE expression no changes are done and the function returns
555 false. */
556
557bool
558set_rhs (tree *stmt_p, tree expr)
559{
560 tree stmt = *stmt_p, op;
561 enum tree_code code = TREE_CODE (expr);
562 stmt_ann_t ann;
563 tree var;
564 ssa_op_iter iter;
565
566 /* Verify the constant folded result is valid gimple. */
6615c446 567 if (TREE_CODE_CLASS (code) == tcc_binary)
750628d8
DN
568 {
569 if (!is_gimple_val (TREE_OPERAND (expr, 0))
570 || !is_gimple_val (TREE_OPERAND (expr, 1)))
571 return false;
572 }
887da9bd 573 else if (TREE_CODE_CLASS (code) == tcc_unary)
750628d8
DN
574 {
575 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
576 return false;
577 }
c5542940
RG
578 else if (code == ADDR_EXPR)
579 {
580 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ARRAY_REF
581 && !is_gimple_val (TREE_OPERAND (TREE_OPERAND (expr, 0), 1)))
582 return false;
583 }
31d66fa9
JJ
584 else if (code == COMPOUND_EXPR)
585 return false;
750628d8
DN
586
587 switch (TREE_CODE (stmt))
588 {
589 case RETURN_EXPR:
590 op = TREE_OPERAND (stmt, 0);
591 if (TREE_CODE (op) != MODIFY_EXPR)
592 {
593 TREE_OPERAND (stmt, 0) = expr;
594 break;
595 }
596 stmt = op;
597 /* FALLTHRU */
598
599 case MODIFY_EXPR:
600 op = TREE_OPERAND (stmt, 1);
601 if (TREE_CODE (op) == WITH_SIZE_EXPR)
602 stmt = op;
603 TREE_OPERAND (stmt, 1) = expr;
604 break;
605
606 case COND_EXPR:
43e364dd
AP
607 if (!is_gimple_condexpr (expr))
608 return false;
750628d8
DN
609 COND_EXPR_COND (stmt) = expr;
610 break;
611 case SWITCH_EXPR:
612 SWITCH_COND (stmt) = expr;
613 break;
614 case GOTO_EXPR:
615 GOTO_DESTINATION (stmt) = expr;
616 break;
617 case LABEL_EXPR:
618 LABEL_EXPR_LABEL (stmt) = expr;
619 break;
620
621 default:
622 /* Replace the whole statement with EXPR. If EXPR has no side
623 effects, then replace *STMT_P with an empty statement. */
624 ann = stmt_ann (stmt);
625 *stmt_p = TREE_SIDE_EFFECTS (expr) ? expr : build_empty_stmt ();
626 (*stmt_p)->common.ann = (tree_ann_t) ann;
627
628 if (TREE_SIDE_EFFECTS (expr))
629 {
630 /* Fix all the SSA_NAMEs created by *STMT_P to point to its new
631 replacement. */
632 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_DEFS)
633 {
634 if (TREE_CODE (var) == SSA_NAME)
635 SSA_NAME_DEF_STMT (var) = *stmt_p;
636 }
637 }
638 break;
639 }
640
641 return true;
642}
643
644
645/* Entry point to the propagation engine.
646
647 VISIT_STMT is called for every statement visited.
648 VISIT_PHI is called for every PHI node visited. */
649
650void
651ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
652 ssa_prop_visit_phi_fn visit_phi)
653{
654 ssa_prop_visit_stmt = visit_stmt;
655 ssa_prop_visit_phi = visit_phi;
656
657 ssa_prop_init ();
658
659 /* Iterate until the worklists are empty. */
660 while (!cfg_blocks_empty_p ()
78492bf5
SB
661 || VEC_length (tree, interesting_ssa_edges) > 0
662 || VEC_length (tree, varying_ssa_edges) > 0)
750628d8
DN
663 {
664 if (!cfg_blocks_empty_p ())
665 {
666 /* Pull the next block to simulate off the worklist. */
667 basic_block dest_block = cfg_blocks_get ();
668 simulate_block (dest_block);
669 }
670
671 /* In order to move things to varying as quickly as
672 possible,process the VARYING_SSA_EDGES worklist first. */
673 process_ssa_edge_worklist (&varying_ssa_edges);
674
675 /* Now process the INTERESTING_SSA_EDGES worklist. */
676 process_ssa_edge_worklist (&interesting_ssa_edges);
677 }
678
679 ssa_prop_fini ();
680}
681
0bca51f0
DN
682
683/* Return the first V_MAY_DEF or V_MUST_DEF operand for STMT. */
684
685tree
686first_vdef (tree stmt)
687{
f47c96aa
AM
688 ssa_op_iter iter;
689 tree op;
690
691 /* Simply return the first operand we arrive at. */
692 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
693 return (op);
694
695 gcc_unreachable ();
0bca51f0
DN
696}
697
698
699/* Return true if STMT is of the form 'LHS = mem_ref', where 'mem_ref'
700 is a non-volatile pointer dereference, a structure reference or a
701 reference to a single _DECL. Ignore volatile memory references
702 because they are not interesting for the optimizers. */
703
704bool
705stmt_makes_single_load (tree stmt)
706{
707 tree rhs;
708
709 if (TREE_CODE (stmt) != MODIFY_EXPR)
710 return false;
711
f47c96aa 712 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF|SSA_OP_VUSE))
0bca51f0
DN
713 return false;
714
715 rhs = TREE_OPERAND (stmt, 1);
716 STRIP_NOPS (rhs);
717
718 return (!TREE_THIS_VOLATILE (rhs)
719 && (DECL_P (rhs)
7da4bf7d 720 || REFERENCE_CLASS_P (rhs)));
0bca51f0
DN
721}
722
723
724/* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
725 is a non-volatile pointer dereference, a structure reference or a
726 reference to a single _DECL. Ignore volatile memory references
727 because they are not interesting for the optimizers. */
728
729bool
730stmt_makes_single_store (tree stmt)
731{
732 tree lhs;
733
734 if (TREE_CODE (stmt) != MODIFY_EXPR)
735 return false;
736
f47c96aa 737 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF|SSA_OP_VMUSTDEF))
0bca51f0
DN
738 return false;
739
740 lhs = TREE_OPERAND (stmt, 0);
741 STRIP_NOPS (lhs);
742
743 return (!TREE_THIS_VOLATILE (lhs)
744 && (DECL_P (lhs)
7da4bf7d 745 || REFERENCE_CLASS_P (lhs)));
0bca51f0
DN
746}
747
748
749/* If STMT makes a single memory load and all the virtual use operands
750 have the same value in array VALUES, return it. Otherwise, return
751 NULL. */
752
753prop_value_t *
754get_value_loaded_by (tree stmt, prop_value_t *values)
755{
756 ssa_op_iter i;
757 tree vuse;
758 prop_value_t *prev_val = NULL;
759 prop_value_t *val = NULL;
760
761 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
762 {
763 val = &values[SSA_NAME_VERSION (vuse)];
764 if (prev_val && prev_val->value != val->value)
765 return NULL;
766 prev_val = val;
767 }
768
769 return val;
770}
771
772
773/* Propagation statistics. */
774struct prop_stats_d
775{
776 long num_const_prop;
777 long num_copy_prop;
227858d1 778 long num_pred_folded;
0bca51f0
DN
779};
780
781static struct prop_stats_d prop_stats;
782
783/* Replace USE references in statement STMT with the values stored in
784 PROP_VALUE. Return true if at least one reference was replaced. If
785 REPLACED_ADDRESSES_P is given, it will be set to true if an address
786 constant was replaced. */
787
788bool
789replace_uses_in (tree stmt, bool *replaced_addresses_p,
790 prop_value_t *prop_value)
791{
792 bool replaced = false;
793 use_operand_p use;
794 ssa_op_iter iter;
795
796 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
797 {
798 tree tuse = USE_FROM_PTR (use);
799 tree val = prop_value[SSA_NAME_VERSION (tuse)].value;
800
801 if (val == tuse || val == NULL_TREE)
802 continue;
803
804 if (TREE_CODE (stmt) == ASM_EXPR
805 && !may_propagate_copy_into_asm (tuse))
806 continue;
807
808 if (!may_propagate_copy (tuse, val))
809 continue;
810
811 if (TREE_CODE (val) != SSA_NAME)
812 prop_stats.num_const_prop++;
813 else
814 prop_stats.num_copy_prop++;
815
816 propagate_value (use, val);
817
818 replaced = true;
819 if (POINTER_TYPE_P (TREE_TYPE (tuse)) && replaced_addresses_p)
820 *replaced_addresses_p = true;
821 }
822
823 return replaced;
824}
825
826
827/* Replace the VUSE references in statement STMT with the values
828 stored in PROP_VALUE. Return true if a reference was replaced. If
829 REPLACED_ADDRESSES_P is given, it will be set to true if an address
830 constant was replaced.
831
832 Replacing VUSE operands is slightly more complex than replacing
833 regular USEs. We are only interested in two types of replacements
834 here:
835
836 1- If the value to be replaced is a constant or an SSA name for a
837 GIMPLE register, then we are making a copy/constant propagation
838 from a memory store. For instance,
839
840 # a_3 = V_MAY_DEF <a_2>
841 a.b = x_1;
842 ...
843 # VUSE <a_3>
844 y_4 = a.b;
845
846 This replacement is only possible iff STMT is an assignment
847 whose RHS is identical to the LHS of the statement that created
848 the VUSE(s) that we are replacing. Otherwise, we may do the
849 wrong replacement:
850
851 # a_3 = V_MAY_DEF <a_2>
852 # b_5 = V_MAY_DEF <b_4>
853 *p = 10;
854 ...
855 # VUSE <b_5>
856 x_8 = b;
857
858 Even though 'b_5' acquires the value '10' during propagation,
859 there is no way for the propagator to tell whether the
860 replacement is correct in every reached use, because values are
861 computed at definition sites. Therefore, when doing final
862 substitution of propagated values, we have to check each use
863 site. Since the RHS of STMT ('b') is different from the LHS of
864 the originating statement ('*p'), we cannot replace 'b' with
865 '10'.
866
867 Similarly, when merging values from PHI node arguments,
868 propagators need to take care not to merge the same values
869 stored in different locations:
870
871 if (...)
872 # a_3 = V_MAY_DEF <a_2>
873 a.b = 3;
874 else
875 # a_4 = V_MAY_DEF <a_2>
876 a.c = 3;
877 # a_5 = PHI <a_3, a_4>
878
879 It would be wrong to propagate '3' into 'a_5' because that
880 operation merges two stores to different memory locations.
881
882
883 2- If the value to be replaced is an SSA name for a virtual
884 register, then we simply replace each VUSE operand with its
885 value from PROP_VALUE. This is the same replacement done by
886 replace_uses_in. */
887
888static bool
889replace_vuses_in (tree stmt, bool *replaced_addresses_p,
890 prop_value_t *prop_value)
891{
892 bool replaced = false;
893 ssa_op_iter iter;
894 use_operand_p vuse;
895
896 if (stmt_makes_single_load (stmt))
897 {
898 /* If STMT is an assignment whose RHS is a single memory load,
899 see if we are trying to propagate a constant or a GIMPLE
900 register (case #1 above). */
901 prop_value_t *val = get_value_loaded_by (stmt, prop_value);
902 tree rhs = TREE_OPERAND (stmt, 1);
903
904 if (val
905 && val->value
906 && (is_gimple_reg (val->value)
907 || is_gimple_min_invariant (val->value))
908 && simple_cst_equal (rhs, val->mem_ref) == 1)
909
910 {
911 /* If we are replacing a constant address, inform our
912 caller. */
913 if (TREE_CODE (val->value) != SSA_NAME
914 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1)))
915 && replaced_addresses_p)
916 *replaced_addresses_p = true;
917
918 /* We can only perform the substitution if the load is done
919 from the same memory location as the original store.
920 Since we already know that there are no intervening
921 stores between DEF_STMT and STMT, we only need to check
922 that the RHS of STMT is the same as the memory reference
923 propagated together with the value. */
924 TREE_OPERAND (stmt, 1) = val->value;
925
926 if (TREE_CODE (val->value) != SSA_NAME)
927 prop_stats.num_const_prop++;
928 else
929 prop_stats.num_copy_prop++;
930
931 /* Since we have replaced the whole RHS of STMT, there
932 is no point in checking the other VUSEs, as they will
933 all have the same value. */
934 return true;
935 }
936 }
937
938 /* Otherwise, the values for every VUSE operand must be other
939 SSA_NAMEs that can be propagated into STMT. */
940 FOR_EACH_SSA_USE_OPERAND (vuse, stmt, iter, SSA_OP_VIRTUAL_USES)
941 {
942 tree var = USE_FROM_PTR (vuse);
943 tree val = prop_value[SSA_NAME_VERSION (var)].value;
944
945 if (val == NULL_TREE || var == val)
946 continue;
947
948 /* Constants and copies propagated between real and virtual
949 operands are only possible in the cases handled above. They
950 should be ignored in any other context. */
951 if (is_gimple_min_invariant (val) || is_gimple_reg (val))
952 continue;
953
954 propagate_value (vuse, val);
955 prop_stats.num_copy_prop++;
956 replaced = true;
957 }
958
959 return replaced;
960}
961
962
963/* Replace propagated values into all the arguments for PHI using the
964 values from PROP_VALUE. */
965
966static void
967replace_phi_args_in (tree phi, prop_value_t *prop_value)
968{
969 int i;
227858d1
DN
970 bool replaced = false;
971 tree prev_phi = NULL;
972
973 if (dump_file && (dump_flags & TDF_DETAILS))
974 prev_phi = unshare_expr (phi);
0bca51f0
DN
975
976 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
977 {
978 tree arg = PHI_ARG_DEF (phi, i);
979
980 if (TREE_CODE (arg) == SSA_NAME)
981 {
982 tree val = prop_value[SSA_NAME_VERSION (arg)].value;
983
984 if (val && val != arg && may_propagate_copy (arg, val))
985 {
986 if (TREE_CODE (val) != SSA_NAME)
987 prop_stats.num_const_prop++;
988 else
989 prop_stats.num_copy_prop++;
990
991 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
227858d1 992 replaced = true;
0bca51f0
DN
993
994 /* If we propagated a copy and this argument flows
995 through an abnormal edge, update the replacement
996 accordingly. */
997 if (TREE_CODE (val) == SSA_NAME
998 && PHI_ARG_EDGE (phi, i)->flags & EDGE_ABNORMAL)
999 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1000 }
1001 }
1002 }
227858d1
DN
1003
1004 if (replaced && dump_file && (dump_flags & TDF_DETAILS))
1005 {
1006 fprintf (dump_file, "Folded PHI node: ");
1007 print_generic_stmt (dump_file, prev_phi, TDF_SLIM);
1008 fprintf (dump_file, " into: ");
1009 print_generic_stmt (dump_file, phi, TDF_SLIM);
1010 fprintf (dump_file, "\n");
1011 }
1012}
1013
1014
1015/* If STMT has a predicate whose value can be computed using the value
1016 range information computed by VRP, compute its value and return true.
1017 Otherwise, return false. */
1018
1019static bool
1020fold_predicate_in (tree stmt)
1021{
1022 tree *pred_p = NULL;
52b27f98 1023 bool modify_expr_p = false;
227858d1
DN
1024 tree val;
1025
1026 if (TREE_CODE (stmt) == MODIFY_EXPR
1027 && COMPARISON_CLASS_P (TREE_OPERAND (stmt, 1)))
52b27f98
AP
1028 {
1029 modify_expr_p = true;
1030 pred_p = &TREE_OPERAND (stmt, 1);
1031 }
227858d1
DN
1032 else if (TREE_CODE (stmt) == COND_EXPR)
1033 pred_p = &COND_EXPR_COND (stmt);
1034 else
1035 return false;
1036
1037 val = vrp_evaluate_conditional (*pred_p, true);
1038 if (val)
1039 {
52b27f98
AP
1040 if (modify_expr_p)
1041 val = fold_convert (TREE_TYPE (*pred_p), val);
1042
227858d1
DN
1043 if (dump_file)
1044 {
1045 fprintf (dump_file, "Folding predicate ");
1046 print_generic_expr (dump_file, *pred_p, 0);
1047 fprintf (dump_file, " to ");
1048 print_generic_expr (dump_file, val, 0);
1049 fprintf (dump_file, "\n");
1050 }
1051
1052 prop_stats.num_pred_folded++;
1053 *pred_p = val;
1054 return true;
1055 }
1056
1057 return false;
0bca51f0
DN
1058}
1059
1060
227858d1
DN
1061/* Perform final substitution and folding of propagated values.
1062
1063 PROP_VALUE[I] contains the single value that should be substituted
1064 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
1065 substituted.
1066
1067 If USE_RANGES_P is true, statements that contain predicate
1068 expressions are evaluated with a call to vrp_evaluate_conditional.
1069 This will only give meaningful results when called from tree-vrp.c
1070 (the information used by vrp_evaluate_conditional is built by the
1071 VRP pass). */
0bca51f0
DN
1072
1073void
227858d1 1074substitute_and_fold (prop_value_t *prop_value, bool use_ranges_p)
0bca51f0
DN
1075{
1076 basic_block bb;
1077
227858d1
DN
1078 if (prop_value == NULL && !use_ranges_p)
1079 return;
1080
0bca51f0 1081 if (dump_file && (dump_flags & TDF_DETAILS))
227858d1 1082 fprintf (dump_file, "\nSubstituing values and folding statements\n\n");
0bca51f0
DN
1083
1084 memset (&prop_stats, 0, sizeof (prop_stats));
1085
1086 /* Substitute values in every statement of every basic block. */
1087 FOR_EACH_BB (bb)
1088 {
1089 block_stmt_iterator i;
1090 tree phi;
1091
227858d1
DN
1092 /* Propagate known values into PHI nodes. */
1093 if (prop_value)
1094 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
0bca51f0
DN
1095 replace_phi_args_in (phi, prop_value);
1096
0bca51f0
DN
1097 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
1098 {
1099 bool replaced_address, did_replace;
227858d1 1100 tree prev_stmt = NULL;
0bca51f0
DN
1101 tree stmt = bsi_stmt (i);
1102
227858d1
DN
1103 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1104 range information for names and they are discarded
1105 afterwards. */
1106 if (TREE_CODE (stmt) == MODIFY_EXPR
1107 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
1108 continue;
1109
0bca51f0
DN
1110 /* Replace the statement with its folded version and mark it
1111 folded. */
227858d1
DN
1112 did_replace = false;
1113 replaced_address = false;
0bca51f0 1114 if (dump_file && (dump_flags & TDF_DETAILS))
227858d1
DN
1115 prev_stmt = unshare_expr (stmt);
1116
1117 /* If we have range information, see if we can fold
1118 predicate expressions. */
1119 if (use_ranges_p)
1a557723
JL
1120 {
1121 did_replace = fold_predicate_in (stmt);
1122
1123 /* Some statements may be simplified using ranges. For
1124 example, division may be replaced by shifts, modulo
1125 replaced with bitwise and, etc. */
1126 simplify_stmt_using_ranges (stmt);
1127 }
227858d1
DN
1128
1129 if (prop_value)
0bca51f0 1130 {
227858d1
DN
1131 /* Only replace real uses if we couldn't fold the
1132 statement using value range information (value range
1133 information is not collected on virtuals, so we only
1134 need to check this for real uses). */
1135 if (!did_replace)
1136 did_replace |= replace_uses_in (stmt, &replaced_address,
1137 prop_value);
1138
1139 did_replace |= replace_vuses_in (stmt, &replaced_address,
1140 prop_value);
0bca51f0
DN
1141 }
1142
227858d1 1143 /* If we made a replacement, fold and cleanup the statement. */
0bca51f0
DN
1144 if (did_replace)
1145 {
af47810a 1146 tree old_stmt = stmt;
6cedb4ac
JL
1147 tree rhs;
1148
0bca51f0 1149 fold_stmt (bsi_stmt_ptr (i));
6cedb4ac 1150 stmt = bsi_stmt (i);
0bca51f0
DN
1151
1152 /* If we folded a builtin function, we'll likely
1153 need to rename VDEFs. */
1154 mark_new_vars_to_rename (stmt);
1155
1156 /* If we cleaned up EH information from the statement,
1157 remove EH edges. */
af47810a 1158 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
0bca51f0 1159 tree_purge_dead_eh_edges (bb);
6cedb4ac
JL
1160
1161 rhs = get_rhs (stmt);
1162 if (TREE_CODE (rhs) == ADDR_EXPR)
1163 recompute_tree_invarant_for_addr_expr (rhs);
0bca51f0 1164
227858d1
DN
1165 if (dump_file && (dump_flags & TDF_DETAILS))
1166 {
1167 fprintf (dump_file, "Folded statement: ");
1168 print_generic_stmt (dump_file, prev_stmt, TDF_SLIM);
1169 fprintf (dump_file, " into: ");
1170 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1171 fprintf (dump_file, "\n");
1172 }
0bca51f0
DN
1173 }
1174 }
1175 }
1176
1177 if (dump_file && (dump_flags & TDF_STATS))
1178 {
1179 fprintf (dump_file, "Constants propagated: %6ld\n",
1180 prop_stats.num_const_prop);
1181 fprintf (dump_file, "Copies propagated: %6ld\n",
1182 prop_stats.num_copy_prop);
227858d1
DN
1183 fprintf (dump_file, "Predicates folded: %6ld\n",
1184 prop_stats.num_pred_folded);
0bca51f0
DN
1185 }
1186}
227858d1 1187
750628d8 1188#include "gt-tree-ssa-propagate.h"