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