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