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