1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "basic-block.h"
36 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-flow.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-propagate.h"
44 #include "langhooks.h"
47 /* This file implements optimizations on the dominator tree. */
50 /* Structure for recording edge equivalences as well as any pending
51 edge redirections during the dominator optimizer.
53 Computing and storing the edge equivalences instead of creating
54 them on-demand can save significant amounts of time, particularly
55 for pathological cases involving switch statements.
57 These structures live for a single iteration of the dominator
58 optimizer in the edge's AUX field. At the end of an iteration we
59 free each of these structures and update the AUX field to point
60 to any requested redirection target (the code for updating the
61 CFG and SSA graph for edge redirection expects redirection edge
62 targets to be in the AUX field for each edge. */
66 /* If this edge creates a simple equivalence, the LHS and RHS of
67 the equivalence will be stored here. */
71 /* Traversing an edge may also indicate one or more particular conditions
72 are true or false. The number of recorded conditions can vary, but
73 can be determined by the condition's code. So we have an array
74 and its maximum index rather than use a varray. */
75 tree
*cond_equivalences
;
76 unsigned int max_cond_equivalences
;
80 /* Hash table with expressions made available during the renaming process.
81 When an assignment of the form X_i = EXPR is found, the statement is
82 stored in this table. If the same expression EXPR is later found on the
83 RHS of another statement, it is replaced with X_i (thus performing
84 global redundancy elimination). Similarly as we pass through conditionals
85 we record the conditional itself as having either a true or false value
87 static htab_t avail_exprs
;
89 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
90 expressions it enters into the hash table along with a marker entry
91 (null). When we finish processing the block, we pop off entries and
92 remove the expressions from the global hash table until we hit the
94 static VEC(tree
,heap
) *avail_exprs_stack
;
96 /* Stack of statements we need to rescan during finalization for newly
99 Statement rescanning must occur after the current block's available
100 expressions are removed from AVAIL_EXPRS. Else we may change the
101 hash code for an expression and be unable to find/remove it from
103 typedef tree
*tree_p
;
105 DEF_VEC_ALLOC_P(tree_p
,heap
);
107 static VEC(tree_p
,heap
) *stmts_to_rescan
;
109 /* Structure for entries in the expression hash table.
111 This requires more memory for the hash table entries, but allows us
112 to avoid creating silly tree nodes and annotations for conditionals,
113 eliminates 2 global hash tables and two block local varrays.
115 It also allows us to reduce the number of hash table lookups we
116 have to perform in lookup_avail_expr and finally it allows us to
117 significantly reduce the number of calls into the hashing routine
122 /* The value (lhs) of this expression. */
125 /* The expression (rhs) we want to record. */
128 /* The stmt pointer if this element corresponds to a statement. */
131 /* The hash value for RHS/ann. */
135 /* Stack of dest,src pairs that need to be restored during finalization.
137 A NULL entry is used to mark the end of pairs which need to be
138 restored during finalization of this block. */
139 static VEC(tree
,heap
) *const_and_copies_stack
;
141 /* Track whether or not we have changed the control flow graph. */
142 static bool cfg_altered
;
144 /* Bitmap of blocks that have had EH statements cleaned. We should
145 remove their dead edges eventually. */
146 static bitmap need_eh_cleanup
;
148 /* Statistics for dominator optimizations. */
152 long num_exprs_considered
;
158 static struct opt_stats_d opt_stats
;
166 /* Local functions. */
167 static void optimize_stmt (struct dom_walk_data
*,
169 block_stmt_iterator
);
170 static tree
lookup_avail_expr (tree
, bool);
171 static hashval_t
avail_expr_hash (const void *);
172 static hashval_t
real_avail_expr_hash (const void *);
173 static int avail_expr_eq (const void *, const void *);
174 static void htab_statistics (FILE *, htab_t
);
175 static void record_cond (tree
, tree
);
176 static void record_const_or_copy (tree
, tree
);
177 static void record_equality (tree
, tree
);
178 static void record_equivalences_from_phis (basic_block
);
179 static void record_equivalences_from_incoming_edge (basic_block
);
180 static bool eliminate_redundant_computations (tree
);
181 static void record_equivalences_from_stmt (tree
, int, stmt_ann_t
);
182 static void dom_thread_across_edge (struct dom_walk_data
*, edge
);
183 static void dom_opt_finalize_block (struct dom_walk_data
*, basic_block
);
184 static void dom_opt_initialize_block (struct dom_walk_data
*, basic_block
);
185 static void propagate_to_outgoing_edges (struct dom_walk_data
*, basic_block
);
186 static void remove_local_expressions_from_table (void);
187 static void restore_vars_to_original_value (void);
188 static edge
single_incoming_edge_ignoring_loop_edges (basic_block
);
191 /* Allocate an EDGE_INFO for edge E and attach it to E.
192 Return the new EDGE_INFO structure. */
194 static struct edge_info
*
195 allocate_edge_info (edge e
)
197 struct edge_info
*edge_info
;
199 edge_info
= XCNEW (struct edge_info
);
205 /* Free all EDGE_INFO structures associated with edges in the CFG.
206 If a particular edge can be threaded, copy the redirection
207 target from the EDGE_INFO structure into the edge's AUX field
208 as required by code to update the CFG and SSA graph for
212 free_all_edge_infos (void)
220 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
222 struct edge_info
*edge_info
= (struct edge_info
*) e
->aux
;
226 if (edge_info
->cond_equivalences
)
227 free (edge_info
->cond_equivalences
);
235 /* Jump threading, redundancy elimination and const/copy propagation.
237 This pass may expose new symbols that need to be renamed into SSA. For
238 every new symbol exposed, its corresponding bit will be set in
242 tree_ssa_dominator_optimize (void)
244 struct dom_walk_data walk_data
;
247 memset (&opt_stats
, 0, sizeof (opt_stats
));
249 /* Create our hash tables. */
250 avail_exprs
= htab_create (1024, real_avail_expr_hash
, avail_expr_eq
, free
);
251 avail_exprs_stack
= VEC_alloc (tree
, heap
, 20);
252 const_and_copies_stack
= VEC_alloc (tree
, heap
, 20);
253 stmts_to_rescan
= VEC_alloc (tree_p
, heap
, 20);
254 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
256 /* Setup callbacks for the generic dominator tree walker. */
257 walk_data
.walk_stmts_backward
= false;
258 walk_data
.dom_direction
= CDI_DOMINATORS
;
259 walk_data
.initialize_block_local_data
= NULL
;
260 walk_data
.before_dom_children_before_stmts
= dom_opt_initialize_block
;
261 walk_data
.before_dom_children_walk_stmts
= optimize_stmt
;
262 walk_data
.before_dom_children_after_stmts
= propagate_to_outgoing_edges
;
263 walk_data
.after_dom_children_before_stmts
= NULL
;
264 walk_data
.after_dom_children_walk_stmts
= NULL
;
265 walk_data
.after_dom_children_after_stmts
= dom_opt_finalize_block
;
266 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
267 When we attach more stuff we'll need to fill this out with a real
269 walk_data
.global_data
= NULL
;
270 walk_data
.block_local_data_size
= 0;
271 walk_data
.interesting_blocks
= NULL
;
273 /* Now initialize the dominator walker. */
274 init_walk_dominator_tree (&walk_data
);
276 calculate_dominance_info (CDI_DOMINATORS
);
279 /* We need to know loop structures in order to avoid destroying them
280 in jump threading. Note that we still can e.g. thread through loop
281 headers to an exit edge, or through loop header to the loop body, assuming
282 that we update the loop info. */
283 loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES
);
285 /* We need accurate information regarding back edges in the CFG
286 for jump threading; this may include back edes that are not part of
288 mark_dfs_back_edges ();
290 /* Recursively walk the dominator tree optimizing statements. */
291 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
294 block_stmt_iterator bsi
;
298 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
299 update_stmt_if_modified (bsi_stmt (bsi
));
303 /* If we exposed any new variables, go ahead and put them into
304 SSA form now, before we handle jump threading. This simplifies
305 interactions between rewriting of _DECL nodes into SSA form
306 and rewriting SSA_NAME nodes into SSA form after block
307 duplication and CFG manipulation. */
308 update_ssa (TODO_update_ssa
);
310 free_all_edge_infos ();
312 /* Thread jumps, creating duplicate blocks as needed. */
313 cfg_altered
|= thread_through_all_blocks (first_pass_instance
);
316 free_dominance_info (CDI_DOMINATORS
);
318 /* Removal of statements may make some EH edges dead. Purge
319 such edges from the CFG as needed. */
320 if (!bitmap_empty_p (need_eh_cleanup
))
322 tree_purge_all_dead_eh_edges (need_eh_cleanup
);
323 bitmap_zero (need_eh_cleanup
);
326 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
328 Long term we will be able to let everything in SSA_NAME_VALUE
329 persist. However, for now, we know this is the safe thing to do. */
330 for (i
= 0; i
< num_ssa_names
; i
++)
332 tree name
= ssa_name (i
);
338 value
= SSA_NAME_VALUE (name
);
339 if (value
&& !is_gimple_min_invariant (value
))
340 SSA_NAME_VALUE (name
) = NULL
;
343 /* Debugging dumps. */
344 if (dump_file
&& (dump_flags
& TDF_STATS
))
345 dump_dominator_optimization_stats (dump_file
);
347 loop_optimizer_finalize ();
349 /* Delete our main hashtable. */
350 htab_delete (avail_exprs
);
352 /* And finalize the dominator walker. */
353 fini_walk_dominator_tree (&walk_data
);
355 /* Free asserted bitmaps and stacks. */
356 BITMAP_FREE (need_eh_cleanup
);
358 VEC_free (tree
, heap
, avail_exprs_stack
);
359 VEC_free (tree
, heap
, const_and_copies_stack
);
360 VEC_free (tree_p
, heap
, stmts_to_rescan
);
365 gate_dominator (void)
367 return flag_tree_dom
!= 0;
370 struct gimple_opt_pass pass_dominator
=
375 gate_dominator
, /* gate */
376 tree_ssa_dominator_optimize
, /* execute */
379 0, /* static_pass_number */
380 TV_TREE_SSA_DOMINATOR_OPTS
, /* tv_id */
381 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
382 0, /* properties_provided */
383 0, /* properties_destroyed */
384 0, /* todo_flags_start */
388 | TODO_verify_ssa
/* todo_flags_finish */
393 /* Given a stmt CONDSTMT containing a COND_EXPR, canonicalize the
394 COND_EXPR into a canonical form. */
397 canonicalize_comparison (tree condstmt
)
399 tree cond
= COND_EXPR_COND (condstmt
);
402 enum tree_code code
= TREE_CODE (cond
);
404 if (!COMPARISON_CLASS_P (cond
))
407 op0
= TREE_OPERAND (cond
, 0);
408 op1
= TREE_OPERAND (cond
, 1);
410 /* If it would be profitable to swap the operands, then do so to
411 canonicalize the statement, enabling better optimization.
413 By placing canonicalization of such expressions here we
414 transparently keep statements in canonical form, even
415 when the statement is modified. */
416 if (tree_swap_operands_p (op0
, op1
, false))
418 /* For relationals we need to swap the operands
419 and change the code. */
425 TREE_SET_CODE (cond
, swap_tree_comparison (code
));
426 swap_tree_operands (condstmt
,
427 &TREE_OPERAND (cond
, 0),
428 &TREE_OPERAND (cond
, 1));
429 /* If one operand was in the operand cache, but the other is
430 not, because it is a constant, this is a case that the
431 internal updating code of swap_tree_operands can't handle
433 if (TREE_CODE_CLASS (TREE_CODE (op0
))
434 != TREE_CODE_CLASS (TREE_CODE (op1
)))
435 update_stmt (condstmt
);
440 /* Initialize local stacks for this optimizer and record equivalences
441 upon entry to BB. Equivalences can come from the edge traversed to
442 reach BB or they may come from PHI nodes at the start of BB. */
445 dom_opt_initialize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
448 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
449 fprintf (dump_file
, "\n\nOptimizing block #%d\n\n", bb
->index
);
451 /* Push a marker on the stacks of local information so that we know how
452 far to unwind when we finalize this block. */
453 VEC_safe_push (tree
, heap
, avail_exprs_stack
, NULL_TREE
);
454 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
456 record_equivalences_from_incoming_edge (bb
);
458 /* PHI nodes can create equivalences too. */
459 record_equivalences_from_phis (bb
);
462 /* Given an expression EXPR (a relational expression or a statement),
463 initialize the hash table element pointed to by ELEMENT. */
466 initialize_hash_element (tree expr
, tree lhs
, struct expr_hash_elt
*element
)
468 /* Hash table elements may be based on conditional expressions or statements.
470 For the former case, we have no annotation and we want to hash the
471 conditional expression. In the latter case we have an annotation and
472 we want to record the expression the statement evaluates. */
473 if (COMPARISON_CLASS_P (expr
) || TREE_CODE (expr
) == TRUTH_NOT_EXPR
)
475 element
->stmt
= NULL
;
478 else if (TREE_CODE (expr
) == COND_EXPR
)
480 element
->stmt
= expr
;
481 element
->rhs
= COND_EXPR_COND (expr
);
483 else if (TREE_CODE (expr
) == SWITCH_EXPR
)
485 element
->stmt
= expr
;
486 element
->rhs
= SWITCH_COND (expr
);
488 else if (TREE_CODE (expr
) == RETURN_EXPR
&& TREE_OPERAND (expr
, 0))
490 element
->stmt
= expr
;
491 element
->rhs
= GIMPLE_STMT_OPERAND (TREE_OPERAND (expr
, 0), 1);
493 else if (TREE_CODE (expr
) == GOTO_EXPR
)
495 element
->stmt
= expr
;
496 element
->rhs
= GOTO_DESTINATION (expr
);
500 element
->stmt
= expr
;
501 element
->rhs
= GENERIC_TREE_OPERAND (expr
, 1);
505 element
->hash
= avail_expr_hash (element
);
508 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
509 LIMIT entries left in LOCALs. */
512 remove_local_expressions_from_table (void)
514 /* Remove all the expressions made available in this block. */
515 while (VEC_length (tree
, avail_exprs_stack
) > 0)
517 struct expr_hash_elt element
;
518 tree expr
= VEC_pop (tree
, avail_exprs_stack
);
520 if (expr
== NULL_TREE
)
523 initialize_hash_element (expr
, NULL
, &element
);
524 htab_remove_elt_with_hash (avail_exprs
, &element
, element
.hash
);
528 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
529 CONST_AND_COPIES to its original state, stopping when we hit a
533 restore_vars_to_original_value (void)
535 while (VEC_length (tree
, const_and_copies_stack
) > 0)
537 tree prev_value
, dest
;
539 dest
= VEC_pop (tree
, const_and_copies_stack
);
544 prev_value
= VEC_pop (tree
, const_and_copies_stack
);
545 SSA_NAME_VALUE (dest
) = prev_value
;
549 /* A trivial wrapper so that we can present the generic jump
550 threading code with a simple API for simplifying statements. */
552 simplify_stmt_for_jump_threading (tree stmt
, tree within_stmt ATTRIBUTE_UNUSED
)
554 return lookup_avail_expr (stmt
, false);
557 /* Wrapper for common code to attempt to thread an edge. For example,
558 it handles lazily building the dummy condition and the bookkeeping
559 when jump threading is successful. */
562 dom_thread_across_edge (struct dom_walk_data
*walk_data
, edge e
)
564 /* If we don't already have a dummy condition, build it now. */
565 if (! walk_data
->global_data
)
567 tree dummy_cond
= build2 (NE_EXPR
, boolean_type_node
,
568 integer_zero_node
, integer_zero_node
);
569 dummy_cond
= build3 (COND_EXPR
, void_type_node
, dummy_cond
, NULL
, NULL
);
570 walk_data
->global_data
= dummy_cond
;
573 thread_across_edge ((tree
) walk_data
->global_data
, e
, false,
574 &const_and_copies_stack
,
575 simplify_stmt_for_jump_threading
);
578 /* We have finished processing the dominator children of BB, perform
579 any finalization actions in preparation for leaving this node in
580 the dominator tree. */
583 dom_opt_finalize_block (struct dom_walk_data
*walk_data
, basic_block bb
)
588 /* If we have an outgoing edge to a block with multiple incoming and
589 outgoing edges, then we may be able to thread the edge. ie, we
590 may be able to statically determine which of the outgoing edges
591 will be traversed when the incoming edge from BB is traversed. */
592 if (single_succ_p (bb
)
593 && (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
) == 0
594 && potentially_threadable_block (single_succ (bb
)))
596 dom_thread_across_edge (walk_data
, single_succ_edge (bb
));
598 else if ((last
= last_stmt (bb
))
599 && TREE_CODE (last
) == COND_EXPR
600 && (COMPARISON_CLASS_P (COND_EXPR_COND (last
))
601 || TREE_CODE (COND_EXPR_COND (last
)) == SSA_NAME
)
602 && EDGE_COUNT (bb
->succs
) == 2
603 && (EDGE_SUCC (bb
, 0)->flags
& EDGE_ABNORMAL
) == 0
604 && (EDGE_SUCC (bb
, 1)->flags
& EDGE_ABNORMAL
) == 0)
606 edge true_edge
, false_edge
;
608 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
610 /* Only try to thread the edge if it reaches a target block with
611 more than one predecessor and more than one successor. */
612 if (potentially_threadable_block (true_edge
->dest
))
614 struct edge_info
*edge_info
;
617 /* Push a marker onto the available expression stack so that we
618 unwind any expressions related to the TRUE arm before processing
619 the false arm below. */
620 VEC_safe_push (tree
, heap
, avail_exprs_stack
, NULL_TREE
);
621 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
623 edge_info
= (struct edge_info
*) true_edge
->aux
;
625 /* If we have info associated with this edge, record it into
626 our equivalency tables. */
629 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
630 tree lhs
= edge_info
->lhs
;
631 tree rhs
= edge_info
->rhs
;
633 /* If we have a simple NAME = VALUE equivalency record it. */
634 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
635 record_const_or_copy (lhs
, rhs
);
637 /* If we have 0 = COND or 1 = COND equivalences, record them
638 into our expression hash tables. */
639 if (cond_equivalences
)
640 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
642 tree expr
= cond_equivalences
[i
];
643 tree value
= cond_equivalences
[i
+ 1];
645 record_cond (expr
, value
);
649 dom_thread_across_edge (walk_data
, true_edge
);
651 /* And restore the various tables to their state before
652 we threaded this edge. */
653 remove_local_expressions_from_table ();
656 /* Similarly for the ELSE arm. */
657 if (potentially_threadable_block (false_edge
->dest
))
659 struct edge_info
*edge_info
;
662 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
663 edge_info
= (struct edge_info
*) false_edge
->aux
;
665 /* If we have info associated with this edge, record it into
666 our equivalency tables. */
669 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
670 tree lhs
= edge_info
->lhs
;
671 tree rhs
= edge_info
->rhs
;
673 /* If we have a simple NAME = VALUE equivalency record it. */
674 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
675 record_const_or_copy (lhs
, rhs
);
677 /* If we have 0 = COND or 1 = COND equivalences, record them
678 into our expression hash tables. */
679 if (cond_equivalences
)
680 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
682 tree expr
= cond_equivalences
[i
];
683 tree value
= cond_equivalences
[i
+ 1];
685 record_cond (expr
, value
);
689 /* Now thread the edge. */
690 dom_thread_across_edge (walk_data
, false_edge
);
692 /* No need to remove local expressions from our tables
693 or restore vars to their original value as that will
694 be done immediately below. */
698 remove_local_expressions_from_table ();
699 restore_vars_to_original_value ();
701 /* If we queued any statements to rescan in this block, then
702 go ahead and rescan them now. */
703 while (VEC_length (tree_p
, stmts_to_rescan
) > 0)
705 tree
*stmt_p
= VEC_last (tree_p
, stmts_to_rescan
);
707 basic_block stmt_bb
= bb_for_stmt (stmt
);
712 VEC_pop (tree_p
, stmts_to_rescan
);
713 pop_stmt_changes (stmt_p
);
717 /* PHI nodes can create equivalences too.
719 Ignoring any alternatives which are the same as the result, if
720 all the alternatives are equal, then the PHI node creates an
724 record_equivalences_from_phis (basic_block bb
)
728 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
730 tree lhs
= PHI_RESULT (phi
);
734 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
736 tree t
= PHI_ARG_DEF (phi
, i
);
738 /* Ignore alternatives which are the same as our LHS. Since
739 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
740 can simply compare pointers. */
744 /* If we have not processed an alternative yet, then set
745 RHS to this alternative. */
748 /* If we have processed an alternative (stored in RHS), then
749 see if it is equal to this one. If it isn't, then stop
751 else if (! operand_equal_for_phi_arg_p (rhs
, t
))
755 /* If we had no interesting alternatives, then all the RHS alternatives
756 must have been the same as LHS. */
760 /* If we managed to iterate through each PHI alternative without
761 breaking out of the loop, then we have a PHI which may create
762 a useful equivalence. We do not need to record unwind data for
763 this, since this is a true assignment and not an equivalence
764 inferred from a comparison. All uses of this ssa name are dominated
765 by this assignment, so unwinding just costs time and space. */
766 if (i
== PHI_NUM_ARGS (phi
)
767 && may_propagate_copy (lhs
, rhs
))
768 SSA_NAME_VALUE (lhs
) = rhs
;
772 /* Ignoring loop backedges, if BB has precisely one incoming edge then
773 return that edge. Otherwise return NULL. */
775 single_incoming_edge_ignoring_loop_edges (basic_block bb
)
781 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
783 /* A loop back edge can be identified by the destination of
784 the edge dominating the source of the edge. */
785 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
788 /* If we have already seen a non-loop edge, then we must have
789 multiple incoming non-loop edges and thus we return NULL. */
793 /* This is the first non-loop incoming edge we have found. Record
801 /* Record any equivalences created by the incoming edge to BB. If BB
802 has more than one incoming edge, then no equivalence is created. */
805 record_equivalences_from_incoming_edge (basic_block bb
)
809 struct edge_info
*edge_info
;
811 /* If our parent block ended with a control statement, then we may be
812 able to record some equivalences based on which outgoing edge from
813 the parent was followed. */
814 parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
816 e
= single_incoming_edge_ignoring_loop_edges (bb
);
818 /* If we had a single incoming edge from our parent block, then enter
819 any data associated with the edge into our tables. */
820 if (e
&& e
->src
== parent
)
824 edge_info
= (struct edge_info
*) e
->aux
;
828 tree lhs
= edge_info
->lhs
;
829 tree rhs
= edge_info
->rhs
;
830 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
833 record_equality (lhs
, rhs
);
835 if (cond_equivalences
)
837 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
839 tree expr
= cond_equivalences
[i
];
840 tree value
= cond_equivalences
[i
+ 1];
842 record_cond (expr
, value
);
849 /* Dump SSA statistics on FILE. */
852 dump_dominator_optimization_stats (FILE *file
)
856 fprintf (file
, "Total number of statements: %6ld\n\n",
857 opt_stats
.num_stmts
);
858 fprintf (file
, "Exprs considered for dominator optimizations: %6ld\n",
859 opt_stats
.num_exprs_considered
);
861 n_exprs
= opt_stats
.num_exprs_considered
;
865 fprintf (file
, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
866 opt_stats
.num_re
, PERCENT (opt_stats
.num_re
,
868 fprintf (file
, " Constants propagated: %6ld\n",
869 opt_stats
.num_const_prop
);
870 fprintf (file
, " Copies propagated: %6ld\n",
871 opt_stats
.num_copy_prop
);
873 fprintf (file
, "\nHash table statistics:\n");
875 fprintf (file
, " avail_exprs: ");
876 htab_statistics (file
, avail_exprs
);
880 /* Dump SSA statistics on stderr. */
883 debug_dominator_optimization_stats (void)
885 dump_dominator_optimization_stats (stderr
);
889 /* Dump statistics for the hash table HTAB. */
892 htab_statistics (FILE *file
, htab_t htab
)
894 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
895 (long) htab_size (htab
),
896 (long) htab_elements (htab
),
897 htab_collisions (htab
));
900 /* Enter a statement into the true/false expression hash table indicating
901 that the condition COND has the value VALUE. */
904 record_cond (tree cond
, tree value
)
906 struct expr_hash_elt
*element
= XCNEW (struct expr_hash_elt
);
909 initialize_hash_element (cond
, value
, element
);
911 slot
= htab_find_slot_with_hash (avail_exprs
, (void *)element
,
912 element
->hash
, INSERT
);
915 *slot
= (void *) element
;
916 VEC_safe_push (tree
, heap
, avail_exprs_stack
, cond
);
922 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
923 the new conditional into *p, then store a boolean_true_node
927 build_and_record_new_cond (enum tree_code new_code
, tree op0
, tree op1
, tree
*p
)
929 *p
= build2 (new_code
, boolean_type_node
, op0
, op1
);
931 *p
= boolean_true_node
;
934 /* Record that COND is true and INVERTED is false into the edge information
935 structure. Also record that any conditions dominated by COND are true
938 For example, if a < b is true, then a <= b must also be true. */
941 record_conditions (struct edge_info
*edge_info
, tree cond
, tree inverted
)
945 if (!COMPARISON_CLASS_P (cond
))
948 op0
= TREE_OPERAND (cond
, 0);
949 op1
= TREE_OPERAND (cond
, 1);
951 switch (TREE_CODE (cond
))
955 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
957 edge_info
->max_cond_equivalences
= 12;
958 edge_info
->cond_equivalences
= XNEWVEC (tree
, 12);
959 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
960 &edge_info
->cond_equivalences
[8]);
961 build_and_record_new_cond (LTGT_EXPR
, op0
, op1
,
962 &edge_info
->cond_equivalences
[10]);
966 edge_info
->max_cond_equivalences
= 8;
967 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
970 build_and_record_new_cond ((TREE_CODE (cond
) == LT_EXPR
971 ? LE_EXPR
: GE_EXPR
),
972 op0
, op1
, &edge_info
->cond_equivalences
[4]);
973 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
974 &edge_info
->cond_equivalences
[6]);
979 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
981 edge_info
->max_cond_equivalences
= 6;
982 edge_info
->cond_equivalences
= XNEWVEC (tree
, 6);
983 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
984 &edge_info
->cond_equivalences
[4]);
988 edge_info
->max_cond_equivalences
= 4;
989 edge_info
->cond_equivalences
= XNEWVEC (tree
, 4);
994 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
996 edge_info
->max_cond_equivalences
= 10;
997 edge_info
->cond_equivalences
= XNEWVEC (tree
, 10);
998 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
999 &edge_info
->cond_equivalences
[8]);
1003 edge_info
->max_cond_equivalences
= 8;
1004 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1006 build_and_record_new_cond (LE_EXPR
, op0
, op1
,
1007 &edge_info
->cond_equivalences
[4]);
1008 build_and_record_new_cond (GE_EXPR
, op0
, op1
,
1009 &edge_info
->cond_equivalences
[6]);
1012 case UNORDERED_EXPR
:
1013 edge_info
->max_cond_equivalences
= 16;
1014 edge_info
->cond_equivalences
= XNEWVEC (tree
, 16);
1015 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
1016 &edge_info
->cond_equivalences
[4]);
1017 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
1018 &edge_info
->cond_equivalences
[6]);
1019 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
1020 &edge_info
->cond_equivalences
[8]);
1021 build_and_record_new_cond (UNEQ_EXPR
, op0
, op1
,
1022 &edge_info
->cond_equivalences
[10]);
1023 build_and_record_new_cond (UNLT_EXPR
, op0
, op1
,
1024 &edge_info
->cond_equivalences
[12]);
1025 build_and_record_new_cond (UNGT_EXPR
, op0
, op1
,
1026 &edge_info
->cond_equivalences
[14]);
1031 edge_info
->max_cond_equivalences
= 8;
1032 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1033 build_and_record_new_cond ((TREE_CODE (cond
) == UNLT_EXPR
1034 ? UNLE_EXPR
: UNGE_EXPR
),
1035 op0
, op1
, &edge_info
->cond_equivalences
[4]);
1036 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
1037 &edge_info
->cond_equivalences
[6]);
1041 edge_info
->max_cond_equivalences
= 8;
1042 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1043 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
1044 &edge_info
->cond_equivalences
[4]);
1045 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
1046 &edge_info
->cond_equivalences
[6]);
1050 edge_info
->max_cond_equivalences
= 8;
1051 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1052 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
1053 &edge_info
->cond_equivalences
[4]);
1054 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
1055 &edge_info
->cond_equivalences
[6]);
1059 edge_info
->max_cond_equivalences
= 4;
1060 edge_info
->cond_equivalences
= XNEWVEC (tree
, 4);
1064 /* Now store the original true and false conditions into the first
1066 edge_info
->cond_equivalences
[0] = cond
;
1067 edge_info
->cond_equivalences
[1] = boolean_true_node
;
1068 edge_info
->cond_equivalences
[2] = inverted
;
1069 edge_info
->cond_equivalences
[3] = boolean_false_node
;
1072 /* A helper function for record_const_or_copy and record_equality.
1073 Do the work of recording the value and undo info. */
1076 record_const_or_copy_1 (tree x
, tree y
, tree prev_x
)
1078 SSA_NAME_VALUE (x
) = y
;
1080 VEC_reserve (tree
, heap
, const_and_copies_stack
, 2);
1081 VEC_quick_push (tree
, const_and_copies_stack
, prev_x
);
1082 VEC_quick_push (tree
, const_and_copies_stack
, x
);
1086 /* Return the loop depth of the basic block of the defining statement of X.
1087 This number should not be treated as absolutely correct because the loop
1088 information may not be completely up-to-date when dom runs. However, it
1089 will be relatively correct, and as more passes are taught to keep loop info
1090 up to date, the result will become more and more accurate. */
1093 loop_depth_of_name (tree x
)
1098 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1099 if (TREE_CODE (x
) != SSA_NAME
)
1102 /* Otherwise return the loop depth of the defining statement's bb.
1103 Note that there may not actually be a bb for this statement, if the
1104 ssa_name is live on entry. */
1105 defstmt
= SSA_NAME_DEF_STMT (x
);
1106 defbb
= bb_for_stmt (defstmt
);
1110 return defbb
->loop_depth
;
1114 /* Record that X is equal to Y in const_and_copies. Record undo
1115 information in the block-local vector. */
1118 record_const_or_copy (tree x
, tree y
)
1120 tree prev_x
= SSA_NAME_VALUE (x
);
1122 if (TREE_CODE (y
) == SSA_NAME
)
1124 tree tmp
= SSA_NAME_VALUE (y
);
1129 record_const_or_copy_1 (x
, y
, prev_x
);
1132 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1133 This constrains the cases in which we may treat this as assignment. */
1136 record_equality (tree x
, tree y
)
1138 tree prev_x
= NULL
, prev_y
= NULL
;
1140 if (TREE_CODE (x
) == SSA_NAME
)
1141 prev_x
= SSA_NAME_VALUE (x
);
1142 if (TREE_CODE (y
) == SSA_NAME
)
1143 prev_y
= SSA_NAME_VALUE (y
);
1145 /* If one of the previous values is invariant, or invariant in more loops
1146 (by depth), then use that.
1147 Otherwise it doesn't matter which value we choose, just so
1148 long as we canonicalize on one value. */
1149 if (is_gimple_min_invariant (y
))
1151 else if (is_gimple_min_invariant (x
)
1152 || (loop_depth_of_name (x
) <= loop_depth_of_name (y
)))
1153 prev_x
= x
, x
= y
, y
= prev_x
, prev_x
= prev_y
;
1154 else if (prev_x
&& is_gimple_min_invariant (prev_x
))
1155 x
= y
, y
= prev_x
, prev_x
= prev_y
;
1156 else if (prev_y
&& TREE_CODE (prev_y
) != VALUE_HANDLE
)
1159 /* After the swapping, we must have one SSA_NAME. */
1160 if (TREE_CODE (x
) != SSA_NAME
)
1163 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1164 variable compared against zero. If we're honoring signed zeros,
1165 then we cannot record this value unless we know that the value is
1167 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x
)))
1168 && (TREE_CODE (y
) != REAL_CST
1169 || REAL_VALUES_EQUAL (dconst0
, TREE_REAL_CST (y
))))
1172 record_const_or_copy_1 (x
, y
, prev_x
);
1175 /* Returns true when STMT is a simple iv increment. It detects the
1176 following situation:
1178 i_1 = phi (..., i_2)
1179 i_2 = i_1 +/- ... */
1182 simple_iv_increment_p (tree stmt
)
1184 tree lhs
, rhs
, preinc
, phi
;
1187 if (TREE_CODE (stmt
) != GIMPLE_MODIFY_STMT
)
1190 lhs
= GIMPLE_STMT_OPERAND (stmt
, 0);
1191 if (TREE_CODE (lhs
) != SSA_NAME
)
1194 rhs
= GIMPLE_STMT_OPERAND (stmt
, 1);
1196 if (TREE_CODE (rhs
) != PLUS_EXPR
1197 && TREE_CODE (rhs
) != MINUS_EXPR
)
1200 preinc
= TREE_OPERAND (rhs
, 0);
1201 if (TREE_CODE (preinc
) != SSA_NAME
)
1204 phi
= SSA_NAME_DEF_STMT (preinc
);
1205 if (TREE_CODE (phi
) != PHI_NODE
)
1208 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
1209 if (PHI_ARG_DEF (phi
, i
) == lhs
)
1215 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1216 known value for that SSA_NAME (or NULL if no value is known).
1218 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1219 successors of BB. */
1222 cprop_into_successor_phis (basic_block bb
)
1227 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1232 /* If this is an abnormal edge, then we do not want to copy propagate
1233 into the PHI alternative associated with this edge. */
1234 if (e
->flags
& EDGE_ABNORMAL
)
1237 phi
= phi_nodes (e
->dest
);
1242 for ( ; phi
; phi
= PHI_CHAIN (phi
))
1245 use_operand_p orig_p
;
1248 /* The alternative may be associated with a constant, so verify
1249 it is an SSA_NAME before doing anything with it. */
1250 orig_p
= PHI_ARG_DEF_PTR (phi
, indx
);
1251 orig_val
= USE_FROM_PTR (orig_p
);
1252 if (TREE_CODE (orig_val
) != SSA_NAME
)
1255 /* If we have *ORIG_P in our constant/copy table, then replace
1256 ORIG_P with its value in our constant/copy table. */
1257 new_val
= SSA_NAME_VALUE (orig_val
);
1259 && new_val
!= orig_val
1260 && (TREE_CODE (new_val
) == SSA_NAME
1261 || is_gimple_min_invariant (new_val
))
1262 && may_propagate_copy (orig_val
, new_val
))
1263 propagate_value (orig_p
, new_val
);
1268 /* We have finished optimizing BB, record any information implied by
1269 taking a specific outgoing edge from BB. */
1272 record_edge_info (basic_block bb
)
1274 block_stmt_iterator bsi
= bsi_last (bb
);
1275 struct edge_info
*edge_info
;
1277 if (! bsi_end_p (bsi
))
1279 tree stmt
= bsi_stmt (bsi
);
1281 if (stmt
&& TREE_CODE (stmt
) == SWITCH_EXPR
)
1283 tree cond
= SWITCH_COND (stmt
);
1285 if (TREE_CODE (cond
) == SSA_NAME
)
1287 tree labels
= SWITCH_LABELS (stmt
);
1288 int i
, n_labels
= TREE_VEC_LENGTH (labels
);
1289 tree
*info
= XCNEWVEC (tree
, last_basic_block
);
1293 for (i
= 0; i
< n_labels
; i
++)
1295 tree label
= TREE_VEC_ELT (labels
, i
);
1296 basic_block target_bb
= label_to_block (CASE_LABEL (label
));
1298 if (CASE_HIGH (label
)
1299 || !CASE_LOW (label
)
1300 || info
[target_bb
->index
])
1301 info
[target_bb
->index
] = error_mark_node
;
1303 info
[target_bb
->index
] = label
;
1306 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1308 basic_block target_bb
= e
->dest
;
1309 tree node
= info
[target_bb
->index
];
1311 if (node
!= NULL
&& node
!= error_mark_node
)
1313 tree x
= fold_convert (TREE_TYPE (cond
), CASE_LOW (node
));
1314 edge_info
= allocate_edge_info (e
);
1315 edge_info
->lhs
= cond
;
1323 /* A COND_EXPR may create equivalences too. */
1324 if (stmt
&& TREE_CODE (stmt
) == COND_EXPR
)
1326 tree cond
= COND_EXPR_COND (stmt
);
1330 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
1332 /* If the conditional is a single variable 'X', record 'X = 1'
1333 for the true edge and 'X = 0' on the false edge. */
1334 if (SSA_VAR_P (cond
))
1336 struct edge_info
*edge_info
;
1338 edge_info
= allocate_edge_info (true_edge
);
1339 edge_info
->lhs
= cond
;
1340 edge_info
->rhs
= constant_boolean_node (1, TREE_TYPE (cond
));
1342 edge_info
= allocate_edge_info (false_edge
);
1343 edge_info
->lhs
= cond
;
1344 edge_info
->rhs
= constant_boolean_node (0, TREE_TYPE (cond
));
1346 /* Equality tests may create one or two equivalences. */
1347 else if (COMPARISON_CLASS_P (cond
))
1349 tree op0
= TREE_OPERAND (cond
, 0);
1350 tree op1
= TREE_OPERAND (cond
, 1);
1352 /* Special case comparing booleans against a constant as we
1353 know the value of OP0 on both arms of the branch. i.e., we
1354 can record an equivalence for OP0 rather than COND. */
1355 if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
1356 && TREE_CODE (op0
) == SSA_NAME
1357 && TREE_CODE (TREE_TYPE (op0
)) == BOOLEAN_TYPE
1358 && is_gimple_min_invariant (op1
))
1360 if (TREE_CODE (cond
) == EQ_EXPR
)
1362 edge_info
= allocate_edge_info (true_edge
);
1363 edge_info
->lhs
= op0
;
1364 edge_info
->rhs
= (integer_zerop (op1
)
1365 ? boolean_false_node
1366 : boolean_true_node
);
1368 edge_info
= allocate_edge_info (false_edge
);
1369 edge_info
->lhs
= op0
;
1370 edge_info
->rhs
= (integer_zerop (op1
)
1372 : boolean_false_node
);
1376 edge_info
= allocate_edge_info (true_edge
);
1377 edge_info
->lhs
= op0
;
1378 edge_info
->rhs
= (integer_zerop (op1
)
1380 : boolean_false_node
);
1382 edge_info
= allocate_edge_info (false_edge
);
1383 edge_info
->lhs
= op0
;
1384 edge_info
->rhs
= (integer_zerop (op1
)
1385 ? boolean_false_node
1386 : boolean_true_node
);
1390 else if (is_gimple_min_invariant (op0
)
1391 && (TREE_CODE (op1
) == SSA_NAME
1392 || is_gimple_min_invariant (op1
)))
1394 tree inverted
= invert_truthvalue (cond
);
1395 struct edge_info
*edge_info
;
1397 edge_info
= allocate_edge_info (true_edge
);
1398 record_conditions (edge_info
, cond
, inverted
);
1400 if (TREE_CODE (cond
) == EQ_EXPR
)
1402 edge_info
->lhs
= op1
;
1403 edge_info
->rhs
= op0
;
1406 edge_info
= allocate_edge_info (false_edge
);
1407 record_conditions (edge_info
, inverted
, cond
);
1409 if (TREE_CODE (cond
) == NE_EXPR
)
1411 edge_info
->lhs
= op1
;
1412 edge_info
->rhs
= op0
;
1416 else if (TREE_CODE (op0
) == SSA_NAME
1417 && (is_gimple_min_invariant (op1
)
1418 || TREE_CODE (op1
) == SSA_NAME
))
1420 tree inverted
= invert_truthvalue (cond
);
1421 struct edge_info
*edge_info
;
1423 edge_info
= allocate_edge_info (true_edge
);
1424 record_conditions (edge_info
, cond
, inverted
);
1426 if (TREE_CODE (cond
) == EQ_EXPR
)
1428 edge_info
->lhs
= op0
;
1429 edge_info
->rhs
= op1
;
1432 edge_info
= allocate_edge_info (false_edge
);
1433 record_conditions (edge_info
, inverted
, cond
);
1435 if (TREE_CODE (cond
) == NE_EXPR
)
1437 edge_info
->lhs
= op0
;
1438 edge_info
->rhs
= op1
;
1443 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1448 /* Propagate information from BB to its outgoing edges.
1450 This can include equivalency information implied by control statements
1451 at the end of BB and const/copy propagation into PHIs in BB's
1452 successor blocks. */
1455 propagate_to_outgoing_edges (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1458 record_edge_info (bb
);
1459 cprop_into_successor_phis (bb
);
1462 /* Search for redundant computations in STMT. If any are found, then
1463 replace them with the variable holding the result of the computation.
1465 If safe, record this expression into the available expression hash
1469 eliminate_redundant_computations (tree stmt
)
1471 tree
*expr_p
, def
= NULL_TREE
;
1474 bool retval
= false;
1475 bool modify_expr_p
= false;
1477 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
)
1478 def
= GIMPLE_STMT_OPERAND (stmt
, 0);
1480 /* Certain expressions on the RHS can be optimized away, but can not
1481 themselves be entered into the hash tables. */
1483 || TREE_CODE (def
) != SSA_NAME
1484 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def
)
1485 || !ZERO_SSA_OPERANDS (stmt
, SSA_OP_VDEF
)
1486 /* Do not record equivalences for increments of ivs. This would create
1487 overlapping live ranges for a very questionable gain. */
1488 || simple_iv_increment_p (stmt
))
1491 /* Check if the expression has been computed before. */
1492 cached_lhs
= lookup_avail_expr (stmt
, insert
);
1494 opt_stats
.num_exprs_considered
++;
1496 /* Get a pointer to the expression we are trying to optimize. */
1497 if (TREE_CODE (stmt
) == COND_EXPR
)
1498 expr_p
= &COND_EXPR_COND (stmt
);
1499 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1500 expr_p
= &SWITCH_COND (stmt
);
1501 else if (TREE_CODE (stmt
) == RETURN_EXPR
&& TREE_OPERAND (stmt
, 0))
1503 expr_p
= &GIMPLE_STMT_OPERAND (TREE_OPERAND (stmt
, 0), 1);
1504 modify_expr_p
= true;
1508 expr_p
= &GENERIC_TREE_OPERAND (stmt
, 1);
1509 modify_expr_p
= true;
1512 /* It is safe to ignore types here since we have already done
1513 type checking in the hashing and equality routines. In fact
1514 type checking here merely gets in the way of constant
1515 propagation. Also, make sure that it is safe to propagate
1516 CACHED_LHS into *EXPR_P. */
1518 && ((TREE_CODE (cached_lhs
) != SSA_NAME
1520 || useless_type_conversion_p (TREE_TYPE (*expr_p
),
1521 TREE_TYPE (cached_lhs
))))
1522 || may_propagate_copy (*expr_p
, cached_lhs
)))
1524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1526 fprintf (dump_file
, " Replaced redundant expr '");
1527 print_generic_expr (dump_file
, *expr_p
, dump_flags
);
1528 fprintf (dump_file
, "' with '");
1529 print_generic_expr (dump_file
, cached_lhs
, dump_flags
);
1530 fprintf (dump_file
, "'\n");
1535 #if defined ENABLE_CHECKING
1536 gcc_assert (TREE_CODE (cached_lhs
) == SSA_NAME
1537 || is_gimple_min_invariant (cached_lhs
));
1540 if (TREE_CODE (cached_lhs
) == ADDR_EXPR
1541 || (POINTER_TYPE_P (TREE_TYPE (*expr_p
))
1542 && is_gimple_min_invariant (cached_lhs
)))
1546 && !useless_type_conversion_p (TREE_TYPE (*expr_p
),
1547 TREE_TYPE (cached_lhs
)))
1548 cached_lhs
= fold_convert (TREE_TYPE (*expr_p
), cached_lhs
);
1550 propagate_tree_value (expr_p
, cached_lhs
);
1551 mark_stmt_modified (stmt
);
1556 /* STMT, a GIMPLE_MODIFY_STMT, may create certain equivalences, in either
1557 the available expressions table or the const_and_copies table.
1558 Detect and record those equivalences. */
1561 record_equivalences_from_stmt (tree stmt
, int may_optimize_p
, stmt_ann_t ann
)
1563 tree lhs
= GIMPLE_STMT_OPERAND (stmt
, 0);
1564 enum tree_code lhs_code
= TREE_CODE (lhs
);
1566 if (lhs_code
== SSA_NAME
)
1568 tree rhs
= GIMPLE_STMT_OPERAND (stmt
, 1);
1570 /* Strip away any useless type conversions. */
1571 STRIP_USELESS_TYPE_CONVERSION (rhs
);
1573 /* If the RHS of the assignment is a constant or another variable that
1574 may be propagated, register it in the CONST_AND_COPIES table. We
1575 do not need to record unwind data for this, since this is a true
1576 assignment and not an equivalence inferred from a comparison. All
1577 uses of this ssa name are dominated by this assignment, so unwinding
1578 just costs time and space. */
1580 && (TREE_CODE (rhs
) == SSA_NAME
1581 || is_gimple_min_invariant (rhs
)))
1582 SSA_NAME_VALUE (lhs
) = rhs
;
1585 /* A memory store, even an aliased store, creates a useful
1586 equivalence. By exchanging the LHS and RHS, creating suitable
1587 vops and recording the result in the available expression table,
1588 we may be able to expose more redundant loads. */
1589 if (!ann
->has_volatile_ops
1590 && stmt_references_memory_p (stmt
)
1591 && (TREE_CODE (GIMPLE_STMT_OPERAND (stmt
, 1)) == SSA_NAME
1592 || is_gimple_min_invariant (GIMPLE_STMT_OPERAND (stmt
, 1)))
1593 && !is_gimple_reg (lhs
))
1595 tree rhs
= GIMPLE_STMT_OPERAND (stmt
, 1);
1598 /* Build a new statement with the RHS and LHS exchanged. */
1599 new_stmt
= build_gimple_modify_stmt (rhs
, lhs
);
1600 create_ssa_artificial_load_stmt (new_stmt
, stmt
, true);
1602 /* Finally enter the statement into the available expression
1604 lookup_avail_expr (new_stmt
, true);
1608 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1609 CONST_AND_COPIES. */
1612 cprop_operand (tree stmt
, use_operand_p op_p
)
1614 bool may_have_exposed_new_symbols
= false;
1616 tree op
= USE_FROM_PTR (op_p
);
1618 /* If the operand has a known constant value or it is known to be a
1619 copy of some other variable, use the value or copy stored in
1620 CONST_AND_COPIES. */
1621 val
= SSA_NAME_VALUE (op
);
1622 if (val
&& val
!= op
&& TREE_CODE (val
) != VALUE_HANDLE
)
1624 tree op_type
, val_type
;
1626 /* Do not change the base variable in the virtual operand
1627 tables. That would make it impossible to reconstruct
1628 the renamed virtual operand if we later modify this
1629 statement. Also only allow the new value to be an SSA_NAME
1630 for propagation into virtual operands. */
1631 if (!is_gimple_reg (op
)
1632 && (TREE_CODE (val
) != SSA_NAME
1633 || is_gimple_reg (val
)
1634 || get_virtual_var (val
) != get_virtual_var (op
)))
1637 /* Do not replace hard register operands in asm statements. */
1638 if (TREE_CODE (stmt
) == ASM_EXPR
1639 && !may_propagate_copy_into_asm (op
))
1642 /* Get the toplevel type of each operand. */
1643 op_type
= TREE_TYPE (op
);
1644 val_type
= TREE_TYPE (val
);
1646 /* While both types are pointers, get the type of the object
1648 while (POINTER_TYPE_P (op_type
) && POINTER_TYPE_P (val_type
))
1650 op_type
= TREE_TYPE (op_type
);
1651 val_type
= TREE_TYPE (val_type
);
1654 /* Make sure underlying types match before propagating a constant by
1655 converting the constant to the proper type. Note that convert may
1656 return a non-gimple expression, in which case we ignore this
1657 propagation opportunity. */
1658 if (TREE_CODE (val
) != SSA_NAME
)
1660 if (!useless_type_conversion_p (op_type
, val_type
))
1662 val
= fold_convert (TREE_TYPE (op
), val
);
1663 if (!is_gimple_min_invariant (val
))
1668 /* Certain operands are not allowed to be copy propagated due
1669 to their interaction with exception handling and some GCC
1671 else if (!may_propagate_copy (op
, val
))
1674 /* Do not propagate copies if the propagated value is at a deeper loop
1675 depth than the propagatee. Otherwise, this may move loop variant
1676 variables outside of their loops and prevent coalescing
1677 opportunities. If the value was loop invariant, it will be hoisted
1678 by LICM and exposed for copy propagation. */
1679 if (loop_depth_of_name (val
) > loop_depth_of_name (op
))
1683 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1685 fprintf (dump_file
, " Replaced '");
1686 print_generic_expr (dump_file
, op
, dump_flags
);
1687 fprintf (dump_file
, "' with %s '",
1688 (TREE_CODE (val
) != SSA_NAME
? "constant" : "variable"));
1689 print_generic_expr (dump_file
, val
, dump_flags
);
1690 fprintf (dump_file
, "'\n");
1693 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
1694 that we may have exposed a new symbol for SSA renaming. */
1695 if (TREE_CODE (val
) == ADDR_EXPR
1696 || (POINTER_TYPE_P (TREE_TYPE (op
))
1697 && is_gimple_min_invariant (val
)))
1698 may_have_exposed_new_symbols
= true;
1700 if (TREE_CODE (val
) != SSA_NAME
)
1701 opt_stats
.num_const_prop
++;
1703 opt_stats
.num_copy_prop
++;
1705 propagate_value (op_p
, val
);
1707 /* And note that we modified this statement. This is now
1708 safe, even if we changed virtual operands since we will
1709 rescan the statement and rewrite its operands again. */
1710 mark_stmt_modified (stmt
);
1712 return may_have_exposed_new_symbols
;
1715 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1716 known value for that SSA_NAME (or NULL if no value is known).
1718 Propagate values from CONST_AND_COPIES into the uses, vuses and
1719 vdef_ops of STMT. */
1722 cprop_into_stmt (tree stmt
)
1724 bool may_have_exposed_new_symbols
= false;
1728 FOR_EACH_SSA_USE_OPERAND (op_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1730 if (TREE_CODE (USE_FROM_PTR (op_p
)) == SSA_NAME
)
1731 may_have_exposed_new_symbols
|= cprop_operand (stmt
, op_p
);
1734 return may_have_exposed_new_symbols
;
1738 /* Optimize the statement pointed to by iterator SI.
1740 We try to perform some simplistic global redundancy elimination and
1741 constant propagation:
1743 1- To detect global redundancy, we keep track of expressions that have
1744 been computed in this block and its dominators. If we find that the
1745 same expression is computed more than once, we eliminate repeated
1746 computations by using the target of the first one.
1748 2- Constant values and copy assignments. This is used to do very
1749 simplistic constant and copy propagation. When a constant or copy
1750 assignment is found, we map the value on the RHS of the assignment to
1751 the variable in the LHS in the CONST_AND_COPIES table. */
1754 optimize_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1755 basic_block bb
, block_stmt_iterator si
)
1758 tree stmt
, old_stmt
;
1759 bool may_optimize_p
;
1760 bool may_have_exposed_new_symbols
= false;
1762 old_stmt
= stmt
= bsi_stmt (si
);
1764 if (TREE_CODE (stmt
) == COND_EXPR
)
1765 canonicalize_comparison (stmt
);
1767 update_stmt_if_modified (stmt
);
1768 ann
= stmt_ann (stmt
);
1769 opt_stats
.num_stmts
++;
1770 may_have_exposed_new_symbols
= false;
1771 push_stmt_changes (bsi_stmt_ptr (si
));
1773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1775 fprintf (dump_file
, "Optimizing statement ");
1776 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1779 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1780 may_have_exposed_new_symbols
= cprop_into_stmt (stmt
);
1782 /* If the statement has been modified with constant replacements,
1783 fold its RHS before checking for redundant computations. */
1788 /* Try to fold the statement making sure that STMT is kept
1790 if (fold_stmt (bsi_stmt_ptr (si
)))
1792 stmt
= bsi_stmt (si
);
1793 ann
= stmt_ann (stmt
);
1795 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1797 fprintf (dump_file
, " Folded to: ");
1798 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1802 rhs
= get_rhs (stmt
);
1803 if (rhs
&& TREE_CODE (rhs
) == ADDR_EXPR
)
1804 recompute_tree_invariant_for_addr_expr (rhs
);
1806 /* Constant/copy propagation above may change the set of
1807 virtual operands associated with this statement. Folding
1808 may remove the need for some virtual operands.
1810 Indicate we will need to rescan and rewrite the statement. */
1811 may_have_exposed_new_symbols
= true;
1814 /* Check for redundant computations. Do this optimization only
1815 for assignments that have no volatile ops and conditionals. */
1816 may_optimize_p
= (!ann
->has_volatile_ops
1817 && ((TREE_CODE (stmt
) == RETURN_EXPR
1818 && TREE_OPERAND (stmt
, 0)
1819 && TREE_CODE (TREE_OPERAND (stmt
, 0))
1820 == GIMPLE_MODIFY_STMT
1821 && ! (TREE_SIDE_EFFECTS
1822 (GIMPLE_STMT_OPERAND
1823 (TREE_OPERAND (stmt
, 0), 1))))
1824 || (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
1825 && ! TREE_SIDE_EFFECTS (GIMPLE_STMT_OPERAND (stmt
,
1827 || TREE_CODE (stmt
) == COND_EXPR
1828 || TREE_CODE (stmt
) == SWITCH_EXPR
));
1831 may_have_exposed_new_symbols
|= eliminate_redundant_computations (stmt
);
1833 /* Record any additional equivalences created by this statement. */
1834 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
)
1835 record_equivalences_from_stmt (stmt
, may_optimize_p
, ann
);
1837 /* If STMT is a COND_EXPR and it was modified, then we may know
1838 where it goes. If that is the case, then mark the CFG as altered.
1840 This will cause us to later call remove_unreachable_blocks and
1841 cleanup_tree_cfg when it is safe to do so. It is not safe to
1842 clean things up here since removal of edges and such can trigger
1843 the removal of PHI nodes, which in turn can release SSA_NAMEs to
1846 That's all fine and good, except that once SSA_NAMEs are released
1847 to the manager, we must not call create_ssa_name until all references
1848 to released SSA_NAMEs have been eliminated.
1850 All references to the deleted SSA_NAMEs can not be eliminated until
1851 we remove unreachable blocks.
1853 We can not remove unreachable blocks until after we have completed
1854 any queued jump threading.
1856 We can not complete any queued jump threads until we have taken
1857 appropriate variables out of SSA form. Taking variables out of
1858 SSA form can call create_ssa_name and thus we lose.
1860 Ultimately I suspect we're going to need to change the interface
1861 into the SSA_NAME manager. */
1866 if (TREE_CODE (stmt
) == COND_EXPR
)
1867 val
= COND_EXPR_COND (stmt
);
1868 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1869 val
= SWITCH_COND (stmt
);
1871 if (val
&& TREE_CODE (val
) == INTEGER_CST
&& find_taken_edge (bb
, val
))
1874 /* If we simplified a statement in such a way as to be shown that it
1875 cannot trap, update the eh information and the cfg to match. */
1876 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1878 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
1879 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1880 fprintf (dump_file
, " Flagged to clear EH edges.\n");
1884 if (may_have_exposed_new_symbols
)
1886 /* Queue the statement to be re-scanned after all the
1887 AVAIL_EXPRS have been processed. The change buffer stack for
1888 all the pushed statements will be processed when this queue
1890 VEC_safe_push (tree_p
, heap
, stmts_to_rescan
, bsi_stmt_ptr (si
));
1894 /* Otherwise, just discard the recently pushed change buffer. If
1895 not, the STMTS_TO_RESCAN queue will get out of synch with the
1896 change buffer stack. */
1897 discard_stmt_changes (bsi_stmt_ptr (si
));
1901 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
1902 found, return its LHS. Otherwise insert STMT in the table and return
1905 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
1906 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
1907 can be removed when we finish processing this block and its children.
1909 NOTE: This function assumes that STMT is a GIMPLE_MODIFY_STMT node that
1910 contains no CALL_EXPR on its RHS and makes no volatile nor
1911 aliased references. */
1914 lookup_avail_expr (tree stmt
, bool insert
)
1919 struct expr_hash_elt
*element
= XNEW (struct expr_hash_elt
);
1921 lhs
= TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
1922 ? GIMPLE_STMT_OPERAND (stmt
, 0) : NULL
;
1924 initialize_hash_element (stmt
, lhs
, element
);
1926 /* Don't bother remembering constant assignments and copy operations.
1927 Constants and copy operations are handled by the constant/copy propagator
1928 in optimize_stmt. */
1929 if (TREE_CODE (element
->rhs
) == SSA_NAME
1930 || is_gimple_min_invariant (element
->rhs
))
1936 /* Finally try to find the expression in the main expression hash table. */
1937 slot
= htab_find_slot_with_hash (avail_exprs
, element
, element
->hash
,
1938 (insert
? INSERT
: NO_INSERT
));
1947 *slot
= (void *) element
;
1948 VEC_safe_push (tree
, heap
, avail_exprs_stack
,
1949 stmt
? stmt
: element
->rhs
);
1953 /* Extract the LHS of the assignment so that it can be used as the current
1954 definition of another variable. */
1955 lhs
= ((struct expr_hash_elt
*)*slot
)->lhs
;
1957 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
1958 use the value from the const_and_copies table. */
1959 if (TREE_CODE (lhs
) == SSA_NAME
)
1961 temp
= SSA_NAME_VALUE (lhs
);
1962 if (temp
&& TREE_CODE (temp
) != VALUE_HANDLE
)
1970 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
1971 GIMPLE_MODIFY_STMT statements. We compute a value number for expressions
1972 using the code of the expression and the SSA numbers of its operands. */
1975 avail_expr_hash (const void *p
)
1977 tree stmt
= ((const struct expr_hash_elt
*)p
)->stmt
;
1978 tree rhs
= ((const struct expr_hash_elt
*)p
)->rhs
;
1983 /* iterative_hash_expr knows how to deal with any expression and
1984 deals with commutative operators as well, so just use it instead
1985 of duplicating such complexities here. */
1986 val
= iterative_hash_expr (rhs
, val
);
1988 /* If the hash table entry is not associated with a statement, then we
1989 can just hash the expression and not worry about virtual operands
1991 if (!stmt
|| !stmt_ann (stmt
))
1994 /* Add the SSA version numbers of every vuse operand. This is important
1995 because compound variables like arrays are not renamed in the
1996 operands. Rather, the rename is done on the virtual variable
1997 representing all the elements of the array. */
1998 FOR_EACH_SSA_TREE_OPERAND (vuse
, stmt
, iter
, SSA_OP_VUSE
)
1999 val
= iterative_hash_expr (vuse
, val
);
2005 real_avail_expr_hash (const void *p
)
2007 return ((const struct expr_hash_elt
*)p
)->hash
;
2011 avail_expr_eq (const void *p1
, const void *p2
)
2013 tree stmt1
= ((const struct expr_hash_elt
*)p1
)->stmt
;
2014 tree rhs1
= ((const struct expr_hash_elt
*)p1
)->rhs
;
2015 tree stmt2
= ((const struct expr_hash_elt
*)p2
)->stmt
;
2016 tree rhs2
= ((const struct expr_hash_elt
*)p2
)->rhs
;
2018 /* If they are the same physical expression, return true. */
2019 if (rhs1
== rhs2
&& stmt1
== stmt2
)
2022 /* If their codes are not equal, then quit now. */
2023 if (TREE_CODE (rhs1
) != TREE_CODE (rhs2
))
2026 /* In case of a collision, both RHS have to be identical and have the
2027 same VUSE operands. */
2028 if (types_compatible_p (TREE_TYPE (rhs1
), TREE_TYPE (rhs2
))
2029 && operand_equal_p (rhs1
, rhs2
, OEP_PURE_SAME
))
2031 bool ret
= compare_ssa_operands_equal (stmt1
, stmt2
, SSA_OP_VUSE
);
2032 gcc_assert (!ret
|| ((const struct expr_hash_elt
*)p1
)->hash
2033 == ((const struct expr_hash_elt
*)p2
)->hash
);
2040 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2041 up degenerate PHIs created by or exposed by jump threading. */
2043 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2047 degenerate_phi_result (tree phi
)
2049 tree lhs
= PHI_RESULT (phi
);
2053 /* Ignoring arguments which are the same as LHS, if all the remaining
2054 arguments are the same, then the PHI is a degenerate and has the
2055 value of that common argument. */
2056 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
2058 tree arg
= PHI_ARG_DEF (phi
, i
);
2064 else if (!operand_equal_p (arg
, val
, 0))
2067 return (i
== PHI_NUM_ARGS (phi
) ? val
: NULL
);
2070 /* Given a tree node T, which is either a PHI_NODE or GIMPLE_MODIFY_STMT,
2071 remove it from the IL. */
2074 remove_stmt_or_phi (tree t
)
2076 if (TREE_CODE (t
) == PHI_NODE
)
2077 remove_phi_node (t
, NULL
, true);
2080 block_stmt_iterator bsi
= bsi_for_stmt (t
);
2081 bsi_remove (&bsi
, true);
2086 /* Given a tree node T, which is either a PHI_NODE or GIMPLE_MODIFY_STMT,
2087 return the "rhs" of the node, in the case of a non-degenerate
2088 PHI, NULL is returned. */
2091 get_rhs_or_phi_arg (tree t
)
2093 if (TREE_CODE (t
) == PHI_NODE
)
2094 return degenerate_phi_result (t
);
2095 else if (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
)
2096 return GIMPLE_STMT_OPERAND (t
, 1);
2101 /* Given a tree node T, which is either a PHI_NODE or a GIMPLE_MODIFY_STMT,
2102 return the "lhs" of the node. */
2105 get_lhs_or_phi_result (tree t
)
2107 if (TREE_CODE (t
) == PHI_NODE
)
2108 return PHI_RESULT (t
);
2109 else if (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
)
2110 return GIMPLE_STMT_OPERAND (t
, 0);
2114 /* Propagate RHS into all uses of LHS (when possible).
2116 RHS and LHS are derived from STMT, which is passed in solely so
2117 that we can remove it if propagation is successful.
2119 When propagating into a PHI node or into a statement which turns
2120 into a trivial copy or constant initialization, set the
2121 appropriate bit in INTERESTING_NAMEs so that we will visit those
2122 nodes as well in an effort to pick up secondary optimization
2126 propagate_rhs_into_lhs (tree stmt
, tree lhs
, tree rhs
, bitmap interesting_names
)
2128 /* First verify that propagation is valid and isn't going to move a
2129 loop variant variable outside its loop. */
2130 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
)
2131 && (TREE_CODE (rhs
) != SSA_NAME
2132 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
))
2133 && may_propagate_copy (lhs
, rhs
)
2134 && loop_depth_of_name (lhs
) >= loop_depth_of_name (rhs
))
2136 use_operand_p use_p
;
2137 imm_use_iterator iter
;
2142 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2144 fprintf (dump_file
, " Replacing '");
2145 print_generic_expr (dump_file
, lhs
, dump_flags
);
2146 fprintf (dump_file
, "' with %s '",
2147 (TREE_CODE (rhs
) != SSA_NAME
? "constant" : "variable"));
2148 print_generic_expr (dump_file
, rhs
, dump_flags
);
2149 fprintf (dump_file
, "'\n");
2152 /* Walk over every use of LHS and try to replace the use with RHS.
2153 At this point the only reason why such a propagation would not
2154 be successful would be if the use occurs in an ASM_EXPR. */
2155 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
2158 /* It's not always safe to propagate into an ASM_EXPR. */
2159 if (TREE_CODE (use_stmt
) == ASM_EXPR
2160 && ! may_propagate_copy_into_asm (lhs
))
2167 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2169 fprintf (dump_file
, " Original statement:");
2170 print_generic_expr (dump_file
, use_stmt
, dump_flags
);
2171 fprintf (dump_file
, "\n");
2174 push_stmt_changes (&use_stmt
);
2176 /* Propagate the RHS into this use of the LHS. */
2177 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
2178 propagate_value (use_p
, rhs
);
2180 /* Special cases to avoid useless calls into the folding
2181 routines, operand scanning, etc.
2183 First, propagation into a PHI may cause the PHI to become
2184 a degenerate, so mark the PHI as interesting. No other
2185 actions are necessary.
2187 Second, if we're propagating a virtual operand and the
2188 propagation does not change the underlying _DECL node for
2189 the virtual operand, then no further actions are necessary. */
2190 if (TREE_CODE (use_stmt
) == PHI_NODE
2191 || (! is_gimple_reg (lhs
)
2192 && TREE_CODE (rhs
) == SSA_NAME
2193 && SSA_NAME_VAR (lhs
) == SSA_NAME_VAR (rhs
)))
2196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2198 fprintf (dump_file
, " Updated statement:");
2199 print_generic_expr (dump_file
, use_stmt
, dump_flags
);
2200 fprintf (dump_file
, "\n");
2203 /* Propagation into a PHI may expose new degenerate PHIs,
2204 so mark the result of the PHI as interesting. */
2205 if (TREE_CODE (use_stmt
) == PHI_NODE
)
2207 tree result
= get_lhs_or_phi_result (use_stmt
);
2208 bitmap_set_bit (interesting_names
, SSA_NAME_VERSION (result
));
2211 discard_stmt_changes (&use_stmt
);
2215 /* From this point onward we are propagating into a
2216 real statement. Folding may (or may not) be possible,
2217 we may expose new operands, expose dead EH edges,
2219 fold_stmt_inplace (use_stmt
);
2221 /* Sometimes propagation can expose new operands to the
2222 renamer. Note this will call update_stmt at the
2223 appropriate time. */
2224 pop_stmt_changes (&use_stmt
);
2227 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2229 fprintf (dump_file
, " Updated statement:");
2230 print_generic_expr (dump_file
, use_stmt
, dump_flags
);
2231 fprintf (dump_file
, "\n");
2234 /* If we replaced a variable index with a constant, then
2235 we would need to update the invariant flag for ADDR_EXPRs. */
2236 if (TREE_CODE (use_stmt
) == GIMPLE_MODIFY_STMT
2237 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt
, 1)) == ADDR_EXPR
)
2238 recompute_tree_invariant_for_addr_expr
2239 (GIMPLE_STMT_OPERAND (use_stmt
, 1));
2241 /* If we cleaned up EH information from the statement,
2242 mark its containing block as needing EH cleanups. */
2243 if (maybe_clean_or_replace_eh_stmt (use_stmt
, use_stmt
))
2245 bitmap_set_bit (need_eh_cleanup
, bb_for_stmt (use_stmt
)->index
);
2246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2247 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2250 /* Propagation may expose new trivial copy/constant propagation
2252 if (TREE_CODE (use_stmt
) == GIMPLE_MODIFY_STMT
2253 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt
, 0)) == SSA_NAME
2254 && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt
, 1)) == SSA_NAME
2255 || is_gimple_min_invariant (GIMPLE_STMT_OPERAND (use_stmt
,
2258 tree result
= get_lhs_or_phi_result (use_stmt
);
2259 bitmap_set_bit (interesting_names
, SSA_NAME_VERSION (result
));
2262 /* Propagation into these nodes may make certain edges in
2263 the CFG unexecutable. We want to identify them as PHI nodes
2264 at the destination of those unexecutable edges may become
2266 else if (TREE_CODE (use_stmt
) == COND_EXPR
2267 || TREE_CODE (use_stmt
) == SWITCH_EXPR
2268 || TREE_CODE (use_stmt
) == GOTO_EXPR
)
2272 if (TREE_CODE (use_stmt
) == COND_EXPR
)
2273 val
= COND_EXPR_COND (use_stmt
);
2274 else if (TREE_CODE (use_stmt
) == SWITCH_EXPR
)
2275 val
= SWITCH_COND (use_stmt
);
2277 val
= GOTO_DESTINATION (use_stmt
);
2279 if (is_gimple_min_invariant (val
))
2281 basic_block bb
= bb_for_stmt (use_stmt
);
2282 edge te
= find_taken_edge (bb
, val
);
2285 block_stmt_iterator bsi
;
2287 /* Remove all outgoing edges except TE. */
2288 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
));)
2294 /* Mark all the PHI nodes at the destination of
2295 the unexecutable edge as interesting. */
2296 for (phi
= phi_nodes (e
->dest
);
2298 phi
= PHI_CHAIN (phi
))
2300 tree result
= PHI_RESULT (phi
);
2301 int version
= SSA_NAME_VERSION (result
);
2303 bitmap_set_bit (interesting_names
, version
);
2306 te
->probability
+= e
->probability
;
2308 te
->count
+= e
->count
;
2316 bsi
= bsi_last (bb_for_stmt (use_stmt
));
2317 bsi_remove (&bsi
, true);
2319 /* And fixup the flags on the single remaining edge. */
2320 te
->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
2321 te
->flags
&= ~EDGE_ABNORMAL
;
2322 te
->flags
|= EDGE_FALLTHRU
;
2323 if (te
->probability
> REG_BR_PROB_BASE
)
2324 te
->probability
= REG_BR_PROB_BASE
;
2329 /* Ensure there is nothing else to do. */
2330 gcc_assert (!all
|| has_zero_uses (lhs
));
2332 /* If we were able to propagate away all uses of LHS, then
2333 we can remove STMT. */
2335 remove_stmt_or_phi (stmt
);
2339 /* T is either a PHI node (potentially a degenerate PHI node) or
2340 a statement that is a trivial copy or constant initialization.
2342 Attempt to eliminate T by propagating its RHS into all uses of
2343 its LHS. This may in turn set new bits in INTERESTING_NAMES
2344 for nodes we want to revisit later.
2346 All exit paths should clear INTERESTING_NAMES for the result
2350 eliminate_const_or_copy (tree t
, bitmap interesting_names
)
2352 tree lhs
= get_lhs_or_phi_result (t
);
2354 int version
= SSA_NAME_VERSION (lhs
);
2356 /* If the LHS of this statement or PHI has no uses, then we can
2357 just eliminate it. This can occur if, for example, the PHI
2358 was created by block duplication due to threading and its only
2359 use was in the conditional at the end of the block which was
2361 if (has_zero_uses (lhs
))
2363 bitmap_clear_bit (interesting_names
, version
);
2364 remove_stmt_or_phi (t
);
2368 /* Get the RHS of the assignment or PHI node if the PHI is a
2370 rhs
= get_rhs_or_phi_arg (t
);
2373 bitmap_clear_bit (interesting_names
, version
);
2377 propagate_rhs_into_lhs (t
, lhs
, rhs
, interesting_names
);
2379 /* Note that T may well have been deleted by now, so do
2380 not access it, instead use the saved version # to clear
2381 T's entry in the worklist. */
2382 bitmap_clear_bit (interesting_names
, version
);
2385 /* The first phase in degenerate PHI elimination.
2387 Eliminate the degenerate PHIs in BB, then recurse on the
2388 dominator children of BB. */
2391 eliminate_degenerate_phis_1 (basic_block bb
, bitmap interesting_names
)
2396 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
2398 next
= PHI_CHAIN (phi
);
2399 eliminate_const_or_copy (phi
, interesting_names
);
2402 /* Recurse into the dominator children of BB. */
2403 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2405 son
= next_dom_son (CDI_DOMINATORS
, son
))
2406 eliminate_degenerate_phis_1 (son
, interesting_names
);
2410 /* A very simple pass to eliminate degenerate PHI nodes from the
2411 IL. This is meant to be fast enough to be able to be run several
2412 times in the optimization pipeline.
2414 Certain optimizations, particularly those which duplicate blocks
2415 or remove edges from the CFG can create or expose PHIs which are
2416 trivial copies or constant initializations.
2418 While we could pick up these optimizations in DOM or with the
2419 combination of copy-prop and CCP, those solutions are far too
2420 heavy-weight for our needs.
2422 This implementation has two phases so that we can efficiently
2423 eliminate the first order degenerate PHIs and second order
2426 The first phase performs a dominator walk to identify and eliminate
2427 the vast majority of the degenerate PHIs. When a degenerate PHI
2428 is identified and eliminated any affected statements or PHIs
2429 are put on a worklist.
2431 The second phase eliminates degenerate PHIs and trivial copies
2432 or constant initializations using the worklist. This is how we
2433 pick up the secondary optimization opportunities with minimal
2437 eliminate_degenerate_phis (void)
2439 bitmap interesting_names
;
2440 bitmap interesting_names1
;
2442 /* Bitmap of blocks which need EH information updated. We can not
2443 update it on-the-fly as doing so invalidates the dominator tree. */
2444 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
2446 /* INTERESTING_NAMES is effectively our worklist, indexed by
2449 A set bit indicates that the statement or PHI node which
2450 defines the SSA_NAME should be (re)examined to determine if
2451 it has become a degenerate PHI or trivial const/copy propagation
2454 Experiments have show we generally get better compilation
2455 time behavior with bitmaps rather than sbitmaps. */
2456 interesting_names
= BITMAP_ALLOC (NULL
);
2457 interesting_names1
= BITMAP_ALLOC (NULL
);
2459 calculate_dominance_info (CDI_DOMINATORS
);
2460 cfg_altered
= false;
2462 /* First phase. Eliminate degenerate PHIs via a dominator
2465 Experiments have indicated that we generally get better
2466 compile-time behavior by visiting blocks in the first
2467 phase in dominator order. Presumably this is because walking
2468 in dominator order leaves fewer PHIs for later examination
2469 by the worklist phase. */
2470 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR
, interesting_names
);
2472 /* Second phase. Eliminate second order degenerate PHIs as well
2473 as trivial copies or constant initializations identified by
2474 the first phase or this phase. Basically we keep iterating
2475 until our set of INTERESTING_NAMEs is empty. */
2476 while (!bitmap_empty_p (interesting_names
))
2481 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
2482 changed during the loop. Copy it to another bitmap and
2484 bitmap_copy (interesting_names1
, interesting_names
);
2486 EXECUTE_IF_SET_IN_BITMAP (interesting_names1
, 0, i
, bi
)
2488 tree name
= ssa_name (i
);
2490 /* Ignore SSA_NAMEs that have been released because
2491 their defining statement was deleted (unreachable). */
2493 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i
)),
2499 free_dominance_info (CDI_DOMINATORS
);
2501 /* Propagation of const and copies may make some EH edges dead. Purge
2502 such edges from the CFG as needed. */
2503 if (!bitmap_empty_p (need_eh_cleanup
))
2505 tree_purge_all_dead_eh_edges (need_eh_cleanup
);
2506 BITMAP_FREE (need_eh_cleanup
);
2509 BITMAP_FREE (interesting_names
);
2510 BITMAP_FREE (interesting_names1
);
2514 struct gimple_opt_pass pass_phi_only_cprop
=
2518 "phicprop", /* name */
2519 gate_dominator
, /* gate */
2520 eliminate_degenerate_phis
, /* execute */
2523 0, /* static_pass_number */
2524 TV_TREE_PHI_CPROP
, /* tv_id */
2525 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
2526 0, /* properties_provided */
2527 0, /* properties_destroyed */
2528 0, /* todo_flags_start */
2534 | TODO_update_ssa
/* todo_flags_finish */