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