]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-propagate.c
This patch implements the unification of the *bitmap interfaces as discussed.
[thirdparty/gcc.git] / gcc / tree-ssa-propagate.c
1 /* Generic SSA value propagation engine.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
11 later version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "tm_p.h"
29 #include "basic-block.h"
30 #include "function.h"
31 #include "gimple-pretty-print.h"
32 #include "dumpfile.h"
33 #include "tree-flow.h"
34 #include "tree-ssa-propagate.h"
35 #include "langhooks.h"
36 #include "vec.h"
37 #include "value-prof.h"
38 #include "gimple.h"
39
40 /* This file implements a generic value propagation engine based on
41 the same propagation used by the SSA-CCP algorithm [1].
42
43 Propagation is performed by simulating the execution of every
44 statement that produces the value being propagated. Simulation
45 proceeds as follows:
46
47 1- Initially, all edges of the CFG are marked not executable and
48 the CFG worklist is seeded with all the statements in the entry
49 basic block (block 0).
50
51 2- Every statement S is simulated with a call to the call-back
52 function SSA_PROP_VISIT_STMT. This evaluation may produce 3
53 results:
54
55 SSA_PROP_NOT_INTERESTING: Statement S produces nothing of
56 interest and does not affect any of the work lists.
57
58 SSA_PROP_VARYING: The value produced by S cannot be determined
59 at compile time. Further simulation of S is not required.
60 If S is a conditional jump, all the outgoing edges for the
61 block are considered executable and added to the work
62 list.
63
64 SSA_PROP_INTERESTING: S produces a value that can be computed
65 at compile time. Its result can be propagated into the
66 statements that feed from S. Furthermore, if S is a
67 conditional jump, only the edge known to be taken is added
68 to the work list. Edges that are known not to execute are
69 never simulated.
70
71 3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The
72 return value from SSA_PROP_VISIT_PHI has the same semantics as
73 described in #2.
74
75 4- Three work lists are kept. Statements are only added to these
76 lists if they produce one of SSA_PROP_INTERESTING or
77 SSA_PROP_VARYING.
78
79 CFG_BLOCKS contains the list of blocks to be simulated.
80 Blocks are added to this list if their incoming edges are
81 found executable.
82
83 VARYING_SSA_EDGES contains the list of statements that feed
84 from statements that produce an SSA_PROP_VARYING result.
85 These are simulated first to speed up processing.
86
87 INTERESTING_SSA_EDGES contains the list of statements that
88 feed from statements that produce an SSA_PROP_INTERESTING
89 result.
90
91 5- Simulation terminates when all three work lists are drained.
92
93 Before calling ssa_propagate, it is important to clear
94 prop_simulate_again_p for all the statements in the program that
95 should be simulated. This initialization allows an implementation
96 to specify which statements should never be simulated.
97
98 It is also important to compute def-use information before calling
99 ssa_propagate.
100
101 References:
102
103 [1] Constant propagation with conditional branches,
104 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
105
106 [2] Building an Optimizing Compiler,
107 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
108
109 [3] Advanced Compiler Design and Implementation,
110 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
111
112 /* Function pointers used to parameterize the propagation engine. */
113 static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt;
114 static ssa_prop_visit_phi_fn ssa_prop_visit_phi;
115
116 /* Keep track of statements that have been added to one of the SSA
117 edges worklists. This flag is used to avoid visiting statements
118 unnecessarily when draining an SSA edge worklist. If while
119 simulating a basic block, we find a statement with
120 STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge
121 processing from visiting it again.
122
123 NOTE: users of the propagation engine are not allowed to use
124 the GF_PLF_1 flag. */
125 #define STMT_IN_SSA_EDGE_WORKLIST GF_PLF_1
126
127 /* A bitmap to keep track of executable blocks in the CFG. */
128 static sbitmap executable_blocks;
129
130 /* Array of control flow edges on the worklist. */
131 static VEC(basic_block,heap) *cfg_blocks;
132
133 static unsigned int cfg_blocks_num = 0;
134 static int cfg_blocks_tail;
135 static int cfg_blocks_head;
136
137 static sbitmap bb_in_list;
138
139 /* Worklist of SSA edges which will need reexamination as their
140 definition has changed. SSA edges are def-use edges in the SSA
141 web. For each D-U edge, we store the target statement or PHI node
142 U. */
143 static GTY(()) VEC(gimple,gc) *interesting_ssa_edges;
144
145 /* Identical to INTERESTING_SSA_EDGES. For performance reasons, the
146 list of SSA edges is split into two. One contains all SSA edges
147 who need to be reexamined because their lattice value changed to
148 varying (this worklist), and the other contains all other SSA edges
149 to be reexamined (INTERESTING_SSA_EDGES).
150
151 Since most values in the program are VARYING, the ideal situation
152 is to move them to that lattice value as quickly as possible.
153 Thus, it doesn't make sense to process any other type of lattice
154 value until all VARYING values are propagated fully, which is one
155 thing using the VARYING worklist achieves. In addition, if we
156 don't use a separate worklist for VARYING edges, we end up with
157 situations where lattice values move from
158 UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING. */
159 static GTY(()) VEC(gimple,gc) *varying_ssa_edges;
160
161
162 /* Return true if the block worklist empty. */
163
164 static inline bool
165 cfg_blocks_empty_p (void)
166 {
167 return (cfg_blocks_num == 0);
168 }
169
170
171 /* Add a basic block to the worklist. The block must not be already
172 in the worklist, and it must not be the ENTRY or EXIT block. */
173
174 static void
175 cfg_blocks_add (basic_block bb)
176 {
177 bool head = false;
178
179 gcc_assert (bb != ENTRY_BLOCK_PTR && bb != EXIT_BLOCK_PTR);
180 gcc_assert (!TEST_BIT (bb_in_list, bb->index));
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 > VEC_length (basic_block, cfg_blocks))
191 {
192 /* We have to grow the array now. Adjust to queue to occupy
193 the full space of the original array. We do not need to
194 initialize the newly allocated portion of the array
195 because we keep track of CFG_BLOCKS_HEAD and
196 CFG_BLOCKS_HEAD. */
197 cfg_blocks_tail = VEC_length (basic_block, cfg_blocks);
198 cfg_blocks_head = 0;
199 VEC_safe_grow (basic_block, heap, cfg_blocks, 2 * cfg_blocks_tail);
200 }
201 /* Minor optimization: we prefer to see blocks with more
202 predecessors later, because there is more of a chance that
203 the incoming edges will be executable. */
204 else if (EDGE_COUNT (bb->preds)
205 >= EDGE_COUNT (VEC_index (basic_block, cfg_blocks,
206 cfg_blocks_head)->preds))
207 cfg_blocks_tail = ((cfg_blocks_tail + 1)
208 % VEC_length (basic_block, cfg_blocks));
209 else
210 {
211 if (cfg_blocks_head == 0)
212 cfg_blocks_head = VEC_length (basic_block, cfg_blocks);
213 --cfg_blocks_head;
214 head = true;
215 }
216 }
217
218 VEC_replace (basic_block, cfg_blocks,
219 head ? cfg_blocks_head : cfg_blocks_tail,
220 bb);
221 SET_BIT (bb_in_list, bb->index);
222 }
223
224
225 /* Remove a block from the worklist. */
226
227 static basic_block
228 cfg_blocks_get (void)
229 {
230 basic_block bb;
231
232 bb = VEC_index (basic_block, cfg_blocks, cfg_blocks_head);
233
234 gcc_assert (!cfg_blocks_empty_p ());
235 gcc_assert (bb);
236
237 cfg_blocks_head = ((cfg_blocks_head + 1)
238 % VEC_length (basic_block, cfg_blocks));
239 --cfg_blocks_num;
240 RESET_BIT (bb_in_list, bb->index);
241
242 return bb;
243 }
244
245
246 /* We have just defined a new value for VAR. If IS_VARYING is true,
247 add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add
248 them to INTERESTING_SSA_EDGES. */
249
250 static void
251 add_ssa_edge (tree var, bool is_varying)
252 {
253 imm_use_iterator iter;
254 use_operand_p use_p;
255
256 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
257 {
258 gimple use_stmt = USE_STMT (use_p);
259
260 if (prop_simulate_again_p (use_stmt)
261 && !gimple_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST))
262 {
263 gimple_set_plf (use_stmt, STMT_IN_SSA_EDGE_WORKLIST, true);
264 if (is_varying)
265 VEC_safe_push (gimple, gc, varying_ssa_edges, use_stmt);
266 else
267 VEC_safe_push (gimple, gc, interesting_ssa_edges, use_stmt);
268 }
269 }
270 }
271
272
273 /* Add edge E to the control flow worklist. */
274
275 static void
276 add_control_edge (edge e)
277 {
278 basic_block bb = e->dest;
279 if (bb == EXIT_BLOCK_PTR)
280 return;
281
282 /* If the edge had already been executed, skip it. */
283 if (e->flags & EDGE_EXECUTABLE)
284 return;
285
286 e->flags |= EDGE_EXECUTABLE;
287
288 /* If the block is already in the list, we're done. */
289 if (TEST_BIT (bb_in_list, bb->index))
290 return;
291
292 cfg_blocks_add (bb);
293
294 if (dump_file && (dump_flags & TDF_DETAILS))
295 fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n",
296 e->src->index, e->dest->index);
297 }
298
299
300 /* Simulate the execution of STMT and update the work lists accordingly. */
301
302 static void
303 simulate_stmt (gimple stmt)
304 {
305 enum ssa_prop_result val = SSA_PROP_NOT_INTERESTING;
306 edge taken_edge = NULL;
307 tree output_name = NULL_TREE;
308
309 /* Don't bother visiting statements that are already
310 considered varying by the propagator. */
311 if (!prop_simulate_again_p (stmt))
312 return;
313
314 if (gimple_code (stmt) == GIMPLE_PHI)
315 {
316 val = ssa_prop_visit_phi (stmt);
317 output_name = gimple_phi_result (stmt);
318 }
319 else
320 val = ssa_prop_visit_stmt (stmt, &taken_edge, &output_name);
321
322 if (val == SSA_PROP_VARYING)
323 {
324 prop_set_simulate_again (stmt, false);
325
326 /* If the statement produced a new varying value, add the SSA
327 edges coming out of OUTPUT_NAME. */
328 if (output_name)
329 add_ssa_edge (output_name, true);
330
331 /* If STMT transfers control out of its basic block, add
332 all outgoing edges to the work list. */
333 if (stmt_ends_bb_p (stmt))
334 {
335 edge e;
336 edge_iterator ei;
337 basic_block bb = gimple_bb (stmt);
338 FOR_EACH_EDGE (e, ei, bb->succs)
339 add_control_edge (e);
340 }
341 }
342 else if (val == SSA_PROP_INTERESTING)
343 {
344 /* If the statement produced new value, add the SSA edges coming
345 out of OUTPUT_NAME. */
346 if (output_name)
347 add_ssa_edge (output_name, false);
348
349 /* If we know which edge is going to be taken out of this block,
350 add it to the CFG work list. */
351 if (taken_edge)
352 add_control_edge (taken_edge);
353 }
354 }
355
356 /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to
357 drain. This pops statements off the given WORKLIST and processes
358 them until there are no more statements on WORKLIST.
359 We take a pointer to WORKLIST because it may be reallocated when an
360 SSA edge is added to it in simulate_stmt. */
361
362 static void
363 process_ssa_edge_worklist (VEC(gimple,gc) **worklist)
364 {
365 /* Drain the entire worklist. */
366 while (VEC_length (gimple, *worklist) > 0)
367 {
368 basic_block bb;
369
370 /* Pull the statement to simulate off the worklist. */
371 gimple stmt = VEC_pop (gimple, *worklist);
372
373 /* If this statement was already visited by simulate_block, then
374 we don't need to visit it again here. */
375 if (!gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
376 continue;
377
378 /* STMT is no longer in a worklist. */
379 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
380
381 if (dump_file && (dump_flags & TDF_DETAILS))
382 {
383 fprintf (dump_file, "\nSimulating statement (from ssa_edges): ");
384 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
385 }
386
387 bb = gimple_bb (stmt);
388
389 /* PHI nodes are always visited, regardless of whether or not
390 the destination block is executable. Otherwise, visit the
391 statement only if its block is marked executable. */
392 if (gimple_code (stmt) == GIMPLE_PHI
393 || TEST_BIT (executable_blocks, bb->index))
394 simulate_stmt (stmt);
395 }
396 }
397
398
399 /* Simulate the execution of BLOCK. Evaluate the statement associated
400 with each variable reference inside the block. */
401
402 static void
403 simulate_block (basic_block block)
404 {
405 gimple_stmt_iterator gsi;
406
407 /* There is nothing to do for the exit block. */
408 if (block == EXIT_BLOCK_PTR)
409 return;
410
411 if (dump_file && (dump_flags & TDF_DETAILS))
412 fprintf (dump_file, "\nSimulating block %d\n", block->index);
413
414 /* Always simulate PHI nodes, even if we have simulated this block
415 before. */
416 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
417 simulate_stmt (gsi_stmt (gsi));
418
419 /* If this is the first time we've simulated this block, then we
420 must simulate each of its statements. */
421 if (!TEST_BIT (executable_blocks, block->index))
422 {
423 gimple_stmt_iterator j;
424 unsigned int normal_edge_count;
425 edge e, normal_edge;
426 edge_iterator ei;
427
428 /* Note that we have simulated this block. */
429 SET_BIT (executable_blocks, block->index);
430
431 for (j = gsi_start_bb (block); !gsi_end_p (j); gsi_next (&j))
432 {
433 gimple stmt = gsi_stmt (j);
434
435 /* If this statement is already in the worklist then
436 "cancel" it. The reevaluation implied by the worklist
437 entry will produce the same value we generate here and
438 thus reevaluating it again from the worklist is
439 pointless. */
440 if (gimple_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST))
441 gimple_set_plf (stmt, STMT_IN_SSA_EDGE_WORKLIST, false);
442
443 simulate_stmt (stmt);
444 }
445
446 /* We can not predict when abnormal and EH edges will be executed, so
447 once a block is considered executable, we consider any
448 outgoing abnormal edges as executable.
449
450 TODO: This is not exactly true. Simplifying statement might
451 prove it non-throwing and also computed goto can be handled
452 when destination is known.
453
454 At the same time, if this block has only one successor that is
455 reached by non-abnormal edges, then add that successor to the
456 worklist. */
457 normal_edge_count = 0;
458 normal_edge = NULL;
459 FOR_EACH_EDGE (e, ei, block->succs)
460 {
461 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
462 add_control_edge (e);
463 else
464 {
465 normal_edge_count++;
466 normal_edge = e;
467 }
468 }
469
470 if (normal_edge_count == 1)
471 add_control_edge (normal_edge);
472 }
473 }
474
475
476 /* Initialize local data structures and work lists. */
477
478 static void
479 ssa_prop_init (void)
480 {
481 edge e;
482 edge_iterator ei;
483 basic_block bb;
484
485 /* Worklists of SSA edges. */
486 interesting_ssa_edges = VEC_alloc (gimple, gc, 20);
487 varying_ssa_edges = VEC_alloc (gimple, gc, 20);
488
489 executable_blocks = sbitmap_alloc (last_basic_block);
490 bitmap_clear (executable_blocks);
491
492 bb_in_list = sbitmap_alloc (last_basic_block);
493 bitmap_clear (bb_in_list);
494
495 if (dump_file && (dump_flags & TDF_DETAILS))
496 dump_immediate_uses (dump_file);
497
498 cfg_blocks = VEC_alloc (basic_block, heap, 20);
499 VEC_safe_grow (basic_block, heap, cfg_blocks, 20);
500
501 /* Initially assume that every edge in the CFG is not executable.
502 (including the edges coming out of ENTRY_BLOCK_PTR). */
503 FOR_ALL_BB (bb)
504 {
505 gimple_stmt_iterator si;
506
507 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
508 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
509
510 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
511 gimple_set_plf (gsi_stmt (si), STMT_IN_SSA_EDGE_WORKLIST, false);
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 (gimple, gc, interesting_ssa_edges);
530 VEC_free (gimple, 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 /* Return true if EXPR is an acceptable right-hand-side for a
539 GIMPLE assignment. We validate the entire tree, not just
540 the root node, thus catching expressions that embed complex
541 operands that are not permitted in GIMPLE. This function
542 is needed because the folding routines in fold-const.c
543 may return such expressions in some cases, e.g., an array
544 access with an embedded index addition. It may make more
545 sense to have folding routines that are sensitive to the
546 constraints on GIMPLE operands, rather than abandoning any
547 any attempt to fold if the usual folding turns out to be too
548 aggressive. */
549
550 bool
551 valid_gimple_rhs_p (tree expr)
552 {
553 enum tree_code code = TREE_CODE (expr);
554
555 switch (TREE_CODE_CLASS (code))
556 {
557 case tcc_declaration:
558 if (!is_gimple_variable (expr))
559 return false;
560 break;
561
562 case tcc_constant:
563 /* All constants are ok. */
564 break;
565
566 case tcc_binary:
567 case tcc_comparison:
568 if (!is_gimple_val (TREE_OPERAND (expr, 0))
569 || !is_gimple_val (TREE_OPERAND (expr, 1)))
570 return false;
571 break;
572
573 case tcc_unary:
574 if (!is_gimple_val (TREE_OPERAND (expr, 0)))
575 return false;
576 break;
577
578 case tcc_expression:
579 switch (code)
580 {
581 case ADDR_EXPR:
582 {
583 tree t;
584 if (is_gimple_min_invariant (expr))
585 return true;
586 t = TREE_OPERAND (expr, 0);
587 while (handled_component_p (t))
588 {
589 /* ??? More checks needed, see the GIMPLE verifier. */
590 if ((TREE_CODE (t) == ARRAY_REF
591 || TREE_CODE (t) == ARRAY_RANGE_REF)
592 && !is_gimple_val (TREE_OPERAND (t, 1)))
593 return false;
594 t = TREE_OPERAND (t, 0);
595 }
596 if (!is_gimple_id (t))
597 return false;
598 }
599 break;
600
601 default:
602 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
603 {
604 if (((code == VEC_COND_EXPR || code == COND_EXPR)
605 ? !is_gimple_condexpr (TREE_OPERAND (expr, 0))
606 : !is_gimple_val (TREE_OPERAND (expr, 0)))
607 || !is_gimple_val (TREE_OPERAND (expr, 1))
608 || !is_gimple_val (TREE_OPERAND (expr, 2)))
609 return false;
610 break;
611 }
612 return false;
613 }
614 break;
615
616 case tcc_vl_exp:
617 return false;
618
619 case tcc_exceptional:
620 if (code != SSA_NAME)
621 return false;
622 break;
623
624 default:
625 return false;
626 }
627
628 return true;
629 }
630
631
632 /* Return true if EXPR is a CALL_EXPR suitable for representation
633 as a single GIMPLE_CALL statement. If the arguments require
634 further gimplification, return false. */
635
636 static bool
637 valid_gimple_call_p (tree expr)
638 {
639 unsigned i, nargs;
640
641 if (TREE_CODE (expr) != CALL_EXPR)
642 return false;
643
644 nargs = call_expr_nargs (expr);
645 for (i = 0; i < nargs; i++)
646 {
647 tree arg = CALL_EXPR_ARG (expr, i);
648 if (is_gimple_reg_type (arg))
649 {
650 if (!is_gimple_val (arg))
651 return false;
652 }
653 else
654 if (!is_gimple_lvalue (arg))
655 return false;
656 }
657
658 return true;
659 }
660
661
662 /* Make SSA names defined by OLD_STMT point to NEW_STMT
663 as their defining statement. */
664
665 void
666 move_ssa_defining_stmt_for_defs (gimple new_stmt, gimple old_stmt)
667 {
668 tree var;
669 ssa_op_iter iter;
670
671 if (gimple_in_ssa_p (cfun))
672 {
673 /* Make defined SSA_NAMEs point to the new
674 statement as their definition. */
675 FOR_EACH_SSA_TREE_OPERAND (var, old_stmt, iter, SSA_OP_ALL_DEFS)
676 {
677 if (TREE_CODE (var) == SSA_NAME)
678 SSA_NAME_DEF_STMT (var) = new_stmt;
679 }
680 }
681 }
682
683 /* Helper function for update_gimple_call and update_call_from_tree.
684 A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT. */
685
686 static void
687 finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt,
688 gimple stmt)
689 {
690 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
691 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
692 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
693 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
694 gimple_set_location (new_stmt, gimple_location (stmt));
695 if (gimple_block (new_stmt) == NULL_TREE)
696 gimple_set_block (new_stmt, gimple_block (stmt));
697 gsi_replace (si_p, new_stmt, false);
698 }
699
700 /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
701 with number of arguments NARGS, where the arguments in GIMPLE form
702 follow NARGS argument. */
703
704 bool
705 update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
706 {
707 va_list ap;
708 gimple new_stmt, stmt = gsi_stmt (*si_p);
709
710 gcc_assert (is_gimple_call (stmt));
711 va_start (ap, nargs);
712 new_stmt = gimple_build_call_valist (fn, nargs, ap);
713 finish_update_gimple_call (si_p, new_stmt, stmt);
714 va_end (ap);
715 return true;
716 }
717
718 /* Update a GIMPLE_CALL statement at iterator *SI_P to reflect the
719 value of EXPR, which is expected to be the result of folding the
720 call. This can only be done if EXPR is a CALL_EXPR with valid
721 GIMPLE operands as arguments, or if it is a suitable RHS expression
722 for a GIMPLE_ASSIGN. More complex expressions will require
723 gimplification, which will introduce additional statements. In this
724 event, no update is performed, and the function returns false.
725 Note that we cannot mutate a GIMPLE_CALL in-place, so we always
726 replace the statement at *SI_P with an entirely new statement.
727 The new statement need not be a call, e.g., if the original call
728 folded to a constant. */
729
730 bool
731 update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
732 {
733 gimple stmt = gsi_stmt (*si_p);
734
735 if (valid_gimple_call_p (expr))
736 {
737 /* The call has simplified to another call. */
738 tree fn = CALL_EXPR_FN (expr);
739 unsigned i;
740 unsigned nargs = call_expr_nargs (expr);
741 VEC(tree, heap) *args = NULL;
742 gimple new_stmt;
743
744 if (nargs > 0)
745 {
746 args = VEC_alloc (tree, heap, nargs);
747 VEC_safe_grow (tree, heap, args, nargs);
748
749 for (i = 0; i < nargs; i++)
750 VEC_replace (tree, args, i, CALL_EXPR_ARG (expr, i));
751 }
752
753 new_stmt = gimple_build_call_vec (fn, args);
754 finish_update_gimple_call (si_p, new_stmt, stmt);
755 VEC_free (tree, heap, args);
756
757 return true;
758 }
759 else if (valid_gimple_rhs_p (expr))
760 {
761 tree lhs = gimple_call_lhs (stmt);
762 gimple new_stmt;
763
764 /* The call has simplified to an expression
765 that cannot be represented as a GIMPLE_CALL. */
766 if (lhs)
767 {
768 /* A value is expected.
769 Introduce a new GIMPLE_ASSIGN statement. */
770 STRIP_USELESS_TYPE_CONVERSION (expr);
771 new_stmt = gimple_build_assign (lhs, expr);
772 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
773 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
774 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
775 }
776 else if (!TREE_SIDE_EFFECTS (expr))
777 {
778 /* No value is expected, and EXPR has no effect.
779 Replace it with an empty statement. */
780 new_stmt = gimple_build_nop ();
781 if (gimple_in_ssa_p (cfun))
782 {
783 unlink_stmt_vdef (stmt);
784 release_defs (stmt);
785 }
786 }
787 else
788 {
789 /* No value is expected, but EXPR has an effect,
790 e.g., it could be a reference to a volatile
791 variable. Create an assignment statement
792 with a dummy (unused) lhs variable. */
793 STRIP_USELESS_TYPE_CONVERSION (expr);
794 if (gimple_in_ssa_p (cfun))
795 lhs = make_ssa_name (TREE_TYPE (expr), NULL);
796 else
797 lhs = create_tmp_var (TREE_TYPE (expr), NULL);
798 new_stmt = gimple_build_assign (lhs, expr);
799 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
800 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
801 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
802 }
803 gimple_set_location (new_stmt, gimple_location (stmt));
804 gsi_replace (si_p, new_stmt, false);
805 return true;
806 }
807 else
808 /* The call simplified to an expression that is
809 not a valid GIMPLE RHS. */
810 return false;
811 }
812
813
814 /* Entry point to the propagation engine.
815
816 VISIT_STMT is called for every statement visited.
817 VISIT_PHI is called for every PHI node visited. */
818
819 void
820 ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt,
821 ssa_prop_visit_phi_fn visit_phi)
822 {
823 ssa_prop_visit_stmt = visit_stmt;
824 ssa_prop_visit_phi = visit_phi;
825
826 ssa_prop_init ();
827
828 /* Iterate until the worklists are empty. */
829 while (!cfg_blocks_empty_p ()
830 || VEC_length (gimple, interesting_ssa_edges) > 0
831 || VEC_length (gimple, varying_ssa_edges) > 0)
832 {
833 if (!cfg_blocks_empty_p ())
834 {
835 /* Pull the next block to simulate off the worklist. */
836 basic_block dest_block = cfg_blocks_get ();
837 simulate_block (dest_block);
838 }
839
840 /* In order to move things to varying as quickly as
841 possible,process the VARYING_SSA_EDGES worklist first. */
842 process_ssa_edge_worklist (&varying_ssa_edges);
843
844 /* Now process the INTERESTING_SSA_EDGES worklist. */
845 process_ssa_edge_worklist (&interesting_ssa_edges);
846 }
847
848 ssa_prop_fini ();
849 }
850
851
852 /* Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref'
853 is a non-volatile pointer dereference, a structure reference or a
854 reference to a single _DECL. Ignore volatile memory references
855 because they are not interesting for the optimizers. */
856
857 bool
858 stmt_makes_single_store (gimple stmt)
859 {
860 tree lhs;
861
862 if (gimple_code (stmt) != GIMPLE_ASSIGN
863 && gimple_code (stmt) != GIMPLE_CALL)
864 return false;
865
866 if (!gimple_vdef (stmt))
867 return false;
868
869 lhs = gimple_get_lhs (stmt);
870
871 /* A call statement may have a null LHS. */
872 if (!lhs)
873 return false;
874
875 return (!TREE_THIS_VOLATILE (lhs)
876 && (DECL_P (lhs)
877 || REFERENCE_CLASS_P (lhs)));
878 }
879
880
881 /* Propagation statistics. */
882 struct prop_stats_d
883 {
884 long num_const_prop;
885 long num_copy_prop;
886 long num_stmts_folded;
887 long num_dce;
888 };
889
890 static struct prop_stats_d prop_stats;
891
892 /* Replace USE references in statement STMT with the values stored in
893 PROP_VALUE. Return true if at least one reference was replaced. */
894
895 static bool
896 replace_uses_in (gimple stmt, ssa_prop_get_value_fn get_value)
897 {
898 bool replaced = false;
899 use_operand_p use;
900 ssa_op_iter iter;
901
902 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
903 {
904 tree tuse = USE_FROM_PTR (use);
905 tree val = (*get_value) (tuse);
906
907 if (val == tuse || val == NULL_TREE)
908 continue;
909
910 if (gimple_code (stmt) == GIMPLE_ASM
911 && !may_propagate_copy_into_asm (tuse))
912 continue;
913
914 if (!may_propagate_copy (tuse, val))
915 continue;
916
917 if (TREE_CODE (val) != SSA_NAME)
918 prop_stats.num_const_prop++;
919 else
920 prop_stats.num_copy_prop++;
921
922 propagate_value (use, val);
923
924 replaced = true;
925 }
926
927 return replaced;
928 }
929
930
931 /* Replace propagated values into all the arguments for PHI using the
932 values from PROP_VALUE. */
933
934 static void
935 replace_phi_args_in (gimple phi, ssa_prop_get_value_fn get_value)
936 {
937 size_t i;
938 bool replaced = false;
939
940 if (dump_file && (dump_flags & TDF_DETAILS))
941 {
942 fprintf (dump_file, "Folding PHI node: ");
943 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
944 }
945
946 for (i = 0; i < gimple_phi_num_args (phi); i++)
947 {
948 tree arg = gimple_phi_arg_def (phi, i);
949
950 if (TREE_CODE (arg) == SSA_NAME)
951 {
952 tree val = (*get_value) (arg);
953
954 if (val && val != arg && may_propagate_copy (arg, val))
955 {
956 if (TREE_CODE (val) != SSA_NAME)
957 prop_stats.num_const_prop++;
958 else
959 prop_stats.num_copy_prop++;
960
961 propagate_value (PHI_ARG_DEF_PTR (phi, i), val);
962 replaced = true;
963
964 /* If we propagated a copy and this argument flows
965 through an abnormal edge, update the replacement
966 accordingly. */
967 if (TREE_CODE (val) == SSA_NAME
968 && gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
969 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
970 }
971 }
972 }
973
974 if (dump_file && (dump_flags & TDF_DETAILS))
975 {
976 if (!replaced)
977 fprintf (dump_file, "No folding possible\n");
978 else
979 {
980 fprintf (dump_file, "Folded into: ");
981 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
982 fprintf (dump_file, "\n");
983 }
984 }
985 }
986
987
988 /* Perform final substitution and folding of propagated values.
989
990 PROP_VALUE[I] contains the single value that should be substituted
991 at every use of SSA name N_I. If PROP_VALUE is NULL, no values are
992 substituted.
993
994 If FOLD_FN is non-NULL the function will be invoked on all statements
995 before propagating values for pass specific simplification.
996
997 DO_DCE is true if trivially dead stmts can be removed.
998
999 If DO_DCE is true, the statements within a BB are walked from
1000 last to first element. Otherwise we scan from first to last element.
1001
1002 Return TRUE when something changed. */
1003
1004 bool
1005 substitute_and_fold (ssa_prop_get_value_fn get_value_fn,
1006 ssa_prop_fold_stmt_fn fold_fn,
1007 bool do_dce)
1008 {
1009 basic_block bb;
1010 bool something_changed = false;
1011 unsigned i;
1012
1013 if (!get_value_fn && !fold_fn)
1014 return false;
1015
1016 if (dump_file && (dump_flags & TDF_DETAILS))
1017 fprintf (dump_file, "\nSubstituting values and folding statements\n\n");
1018
1019 memset (&prop_stats, 0, sizeof (prop_stats));
1020
1021 /* Substitute lattice values at definition sites. */
1022 if (get_value_fn)
1023 for (i = 1; i < num_ssa_names; ++i)
1024 {
1025 tree name = ssa_name (i);
1026 tree val;
1027 gimple def_stmt;
1028 gimple_stmt_iterator gsi;
1029
1030 if (!name
1031 || virtual_operand_p (name))
1032 continue;
1033
1034 def_stmt = SSA_NAME_DEF_STMT (name);
1035 if (gimple_nop_p (def_stmt)
1036 /* Do not substitute ASSERT_EXPR rhs, this will confuse VRP. */
1037 || (gimple_assign_single_p (def_stmt)
1038 && gimple_assign_rhs_code (def_stmt) == ASSERT_EXPR)
1039 || !(val = (*get_value_fn) (name))
1040 || !may_propagate_copy (name, val))
1041 continue;
1042
1043 gsi = gsi_for_stmt (def_stmt);
1044 if (is_gimple_assign (def_stmt))
1045 {
1046 gimple_assign_set_rhs_with_ops (&gsi, TREE_CODE (val),
1047 val, NULL_TREE);
1048 gcc_assert (gsi_stmt (gsi) == def_stmt);
1049 if (maybe_clean_eh_stmt (def_stmt))
1050 gimple_purge_dead_eh_edges (gimple_bb (def_stmt));
1051 update_stmt (def_stmt);
1052 }
1053 else if (is_gimple_call (def_stmt))
1054 {
1055 int flags = gimple_call_flags (def_stmt);
1056
1057 /* Don't optimize away calls that have side-effects. */
1058 if ((flags & (ECF_CONST|ECF_PURE)) == 0
1059 || (flags & ECF_LOOPING_CONST_OR_PURE))
1060 continue;
1061 if (update_call_from_tree (&gsi, val)
1062 && maybe_clean_or_replace_eh_stmt (def_stmt, gsi_stmt (gsi)))
1063 gimple_purge_dead_eh_edges (gimple_bb (gsi_stmt (gsi)));
1064 }
1065 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1066 {
1067 gimple new_stmt = gimple_build_assign (name, val);
1068 gimple_stmt_iterator gsi2;
1069 SSA_NAME_DEF_STMT (name) = new_stmt;
1070 gsi2 = gsi_after_labels (gimple_bb (def_stmt));
1071 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
1072 remove_phi_node (&gsi, false);
1073 }
1074
1075 something_changed = true;
1076 }
1077
1078 /* Propagate into all uses and fold. */
1079 FOR_EACH_BB (bb)
1080 {
1081 gimple_stmt_iterator i;
1082
1083 /* Propagate known values into PHI nodes. */
1084 if (get_value_fn)
1085 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
1086 replace_phi_args_in (gsi_stmt (i), get_value_fn);
1087
1088 /* Propagate known values into stmts. Do a backward walk if
1089 do_dce is true. In some case it exposes
1090 more trivially deletable stmts to walk backward. */
1091 for (i = (do_dce ? gsi_last_bb (bb) : gsi_start_bb (bb)); !gsi_end_p (i);)
1092 {
1093 bool did_replace;
1094 gimple stmt = gsi_stmt (i);
1095 gimple old_stmt;
1096 enum gimple_code code = gimple_code (stmt);
1097 gimple_stmt_iterator oldi;
1098
1099 oldi = i;
1100 if (do_dce)
1101 gsi_prev (&i);
1102 else
1103 gsi_next (&i);
1104
1105 /* Ignore ASSERT_EXPRs. They are used by VRP to generate
1106 range information for names and they are discarded
1107 afterwards. */
1108
1109 if (code == GIMPLE_ASSIGN
1110 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
1111 continue;
1112
1113 /* No point propagating into a stmt whose result is not used,
1114 but instead we might be able to remove a trivially dead stmt.
1115 Don't do this when called from VRP, since the SSA_NAME which
1116 is going to be released could be still referenced in VRP
1117 ranges. */
1118 if (do_dce
1119 && gimple_get_lhs (stmt)
1120 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1121 && has_zero_uses (gimple_get_lhs (stmt))
1122 && !stmt_could_throw_p (stmt)
1123 && !gimple_has_side_effects (stmt))
1124 {
1125 gimple_stmt_iterator i2;
1126
1127 if (dump_file && dump_flags & TDF_DETAILS)
1128 {
1129 fprintf (dump_file, "Removing dead stmt ");
1130 print_gimple_stmt (dump_file, stmt, 0, 0);
1131 fprintf (dump_file, "\n");
1132 }
1133 prop_stats.num_dce++;
1134 i2 = gsi_for_stmt (stmt);
1135 gsi_remove (&i2, true);
1136 release_defs (stmt);
1137 continue;
1138 }
1139
1140 /* Replace the statement with its folded version and mark it
1141 folded. */
1142 did_replace = false;
1143 if (dump_file && (dump_flags & TDF_DETAILS))
1144 {
1145 fprintf (dump_file, "Folding statement: ");
1146 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1147 }
1148
1149 old_stmt = stmt;
1150
1151 /* Some statements may be simplified using propagator
1152 specific information. Do this before propagating
1153 into the stmt to not disturb pass specific information. */
1154 if (fold_fn
1155 && (*fold_fn)(&oldi))
1156 {
1157 did_replace = true;
1158 prop_stats.num_stmts_folded++;
1159 stmt = gsi_stmt (oldi);
1160 update_stmt (stmt);
1161 }
1162
1163 /* Replace real uses in the statement. */
1164 if (get_value_fn)
1165 did_replace |= replace_uses_in (stmt, get_value_fn);
1166
1167 /* If we made a replacement, fold the statement. */
1168 if (did_replace)
1169 fold_stmt (&oldi);
1170
1171 /* Now cleanup. */
1172 if (did_replace)
1173 {
1174 stmt = gsi_stmt (oldi);
1175
1176 /* If we cleaned up EH information from the statement,
1177 remove EH edges. */
1178 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1179 gimple_purge_dead_eh_edges (bb);
1180
1181 if (is_gimple_assign (stmt)
1182 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1183 == GIMPLE_SINGLE_RHS))
1184 {
1185 tree rhs = gimple_assign_rhs1 (stmt);
1186
1187 if (TREE_CODE (rhs) == ADDR_EXPR)
1188 recompute_tree_invariant_for_addr_expr (rhs);
1189 }
1190
1191 /* Determine what needs to be done to update the SSA form. */
1192 update_stmt (stmt);
1193 if (!is_gimple_debug (stmt))
1194 something_changed = true;
1195 }
1196
1197 if (dump_file && (dump_flags & TDF_DETAILS))
1198 {
1199 if (did_replace)
1200 {
1201 fprintf (dump_file, "Folded into: ");
1202 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1203 fprintf (dump_file, "\n");
1204 }
1205 else
1206 fprintf (dump_file, "Not folded\n");
1207 }
1208 }
1209 }
1210
1211 statistics_counter_event (cfun, "Constants propagated",
1212 prop_stats.num_const_prop);
1213 statistics_counter_event (cfun, "Copies propagated",
1214 prop_stats.num_copy_prop);
1215 statistics_counter_event (cfun, "Statements folded",
1216 prop_stats.num_stmts_folded);
1217 statistics_counter_event (cfun, "Statements deleted",
1218 prop_stats.num_dce);
1219 return something_changed;
1220 }
1221
1222 #include "gt-tree-ssa-propagate.h"