1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
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 2, 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 COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "coretypes.h"
32 #include "basic-block.h"
37 #include "diagnostic.h"
39 #include "tree-dump.h"
40 #include "tree-flow.h"
43 #include "tree-pass.h"
44 #include "tree-ssa-propagate.h"
45 #include "langhooks.h"
48 /* This file implements optimizations on the dominator tree. */
51 /* Structure for recording edge equivalences as well as any pending
52 edge redirections during the dominator optimizer.
54 Computing and storing the edge equivalences instead of creating
55 them on-demand can save significant amounts of time, particularly
56 for pathological cases involving switch statements.
58 These structures live for a single iteration of the dominator
59 optimizer in the edge's AUX field. At the end of an iteration we
60 free each of these structures and update the AUX field to point
61 to any requested redirection target (the code for updating the
62 CFG and SSA graph for edge redirection expects redirection edge
63 targets to be in the AUX field for each edge. */
67 /* If this edge creates a simple equivalence, the LHS and RHS of
68 the equivalence will be stored here. */
72 /* Traversing an edge may also indicate one or more particular conditions
73 are true or false. The number of recorded conditions can vary, but
74 can be determined by the condition's code. So we have an array
75 and its maximum index rather than use a varray. */
76 tree
*cond_equivalences
;
77 unsigned int max_cond_equivalences
;
81 /* Hash table with expressions made available during the renaming process.
82 When an assignment of the form X_i = EXPR is found, the statement is
83 stored in this table. If the same expression EXPR is later found on the
84 RHS of another statement, it is replaced with X_i (thus performing
85 global redundancy elimination). Similarly as we pass through conditionals
86 we record the conditional itself as having either a true or false value
88 static htab_t avail_exprs
;
90 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
91 expressions it enters into the hash table along with a marker entry
92 (null). When we finish processing the block, we pop off entries and
93 remove the expressions from the global hash table until we hit the
95 static VEC(tree
,heap
) *avail_exprs_stack
;
97 /* Stack of statements we need to rescan during finalization for newly
100 Statement rescanning must occur after the current block's available
101 expressions are removed from AVAIL_EXPRS. Else we may change the
102 hash code for an expression and be unable to find/remove it from
104 static VEC(tree
,heap
) *stmts_to_rescan
;
106 /* Structure for entries in the expression hash table.
108 This requires more memory for the hash table entries, but allows us
109 to avoid creating silly tree nodes and annotations for conditionals,
110 eliminates 2 global hash tables and two block local varrays.
112 It also allows us to reduce the number of hash table lookups we
113 have to perform in lookup_avail_expr and finally it allows us to
114 significantly reduce the number of calls into the hashing routine
119 /* The value (lhs) of this expression. */
122 /* The expression (rhs) we want to record. */
125 /* The stmt pointer if this element corresponds to a statement. */
128 /* The hash value for RHS/ann. */
132 /* Stack of dest,src pairs that need to be restored during finalization.
134 A NULL entry is used to mark the end of pairs which need to be
135 restored during finalization of this block. */
136 static VEC(tree
,heap
) *const_and_copies_stack
;
138 /* Track whether or not we have changed the control flow graph. */
139 static bool cfg_altered
;
141 /* Bitmap of blocks that have had EH statements cleaned. We should
142 remove their dead edges eventually. */
143 static bitmap need_eh_cleanup
;
145 /* Statistics for dominator optimizations. */
149 long num_exprs_considered
;
155 static struct opt_stats_d opt_stats
;
163 /* Local functions. */
164 static void optimize_stmt (struct dom_walk_data
*,
166 block_stmt_iterator
);
167 static tree
lookup_avail_expr (tree
, bool);
168 static hashval_t
avail_expr_hash (const void *);
169 static hashval_t
real_avail_expr_hash (const void *);
170 static int avail_expr_eq (const void *, const void *);
171 static void htab_statistics (FILE *, htab_t
);
172 static void record_cond (tree
, tree
);
173 static void record_const_or_copy (tree
, tree
);
174 static void record_equality (tree
, tree
);
175 static void record_equivalences_from_phis (basic_block
);
176 static void record_equivalences_from_incoming_edge (basic_block
);
177 static bool eliminate_redundant_computations (tree
);
178 static void record_equivalences_from_stmt (tree
, int, stmt_ann_t
);
179 static void dom_thread_across_edge (struct dom_walk_data
*, edge
);
180 static void dom_opt_finalize_block (struct dom_walk_data
*, basic_block
);
181 static void dom_opt_initialize_block (struct dom_walk_data
*, basic_block
);
182 static void propagate_to_outgoing_edges (struct dom_walk_data
*, basic_block
);
183 static void remove_local_expressions_from_table (void);
184 static void restore_vars_to_original_value (void);
185 static edge
single_incoming_edge_ignoring_loop_edges (basic_block
);
188 /* Allocate an EDGE_INFO for edge E and attach it to E.
189 Return the new EDGE_INFO structure. */
191 static struct edge_info
*
192 allocate_edge_info (edge e
)
194 struct edge_info
*edge_info
;
196 edge_info
= XCNEW (struct edge_info
);
202 /* Free all EDGE_INFO structures associated with edges in the CFG.
203 If a particular edge can be threaded, copy the redirection
204 target from the EDGE_INFO structure into the edge's AUX field
205 as required by code to update the CFG and SSA graph for
209 free_all_edge_infos (void)
217 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
219 struct edge_info
*edge_info
= (struct edge_info
*) e
->aux
;
223 if (edge_info
->cond_equivalences
)
224 free (edge_info
->cond_equivalences
);
232 /* Jump threading, redundancy elimination and const/copy propagation.
234 This pass may expose new symbols that need to be renamed into SSA. For
235 every new symbol exposed, its corresponding bit will be set in
239 tree_ssa_dominator_optimize (void)
241 struct dom_walk_data walk_data
;
243 struct loops loops_info
;
245 memset (&opt_stats
, 0, sizeof (opt_stats
));
247 /* Create our hash tables. */
248 avail_exprs
= htab_create (1024, real_avail_expr_hash
, avail_expr_eq
, free
);
249 avail_exprs_stack
= VEC_alloc (tree
, heap
, 20);
250 const_and_copies_stack
= VEC_alloc (tree
, heap
, 20);
251 stmts_to_rescan
= VEC_alloc (tree
, heap
, 20);
252 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
254 /* Setup callbacks for the generic dominator tree walker. */
255 walk_data
.walk_stmts_backward
= false;
256 walk_data
.dom_direction
= CDI_DOMINATORS
;
257 walk_data
.initialize_block_local_data
= NULL
;
258 walk_data
.before_dom_children_before_stmts
= dom_opt_initialize_block
;
259 walk_data
.before_dom_children_walk_stmts
= optimize_stmt
;
260 walk_data
.before_dom_children_after_stmts
= propagate_to_outgoing_edges
;
261 walk_data
.after_dom_children_before_stmts
= NULL
;
262 walk_data
.after_dom_children_walk_stmts
= NULL
;
263 walk_data
.after_dom_children_after_stmts
= dom_opt_finalize_block
;
264 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
265 When we attach more stuff we'll need to fill this out with a real
267 walk_data
.global_data
= NULL
;
268 walk_data
.block_local_data_size
= 0;
269 walk_data
.interesting_blocks
= NULL
;
271 /* Now initialize the dominator walker. */
272 init_walk_dominator_tree (&walk_data
);
274 calculate_dominance_info (CDI_DOMINATORS
);
276 /* We need to know which edges exit loops so that we can
277 aggressively thread through loop headers to an exit
279 flow_loops_find (&loops_info
);
280 mark_loop_exit_edges (&loops_info
);
281 flow_loops_free (&loops_info
);
283 /* Clean up the CFG so that any forwarder blocks created by loop
284 canonicalization are removed. */
286 calculate_dominance_info (CDI_DOMINATORS
);
288 /* We need accurate information regarding back edges in the CFG
289 for jump threading. */
290 mark_dfs_back_edges ();
292 /* Recursively walk the dominator tree optimizing statements. */
293 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
296 block_stmt_iterator bsi
;
300 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
301 update_stmt_if_modified (bsi_stmt (bsi
));
305 /* If we exposed any new variables, go ahead and put them into
306 SSA form now, before we handle jump threading. This simplifies
307 interactions between rewriting of _DECL nodes into SSA form
308 and rewriting SSA_NAME nodes into SSA form after block
309 duplication and CFG manipulation. */
310 update_ssa (TODO_update_ssa
);
312 free_all_edge_infos ();
314 /* Thread jumps, creating duplicate blocks as needed. */
315 cfg_altered
|= thread_through_all_blocks ();
317 /* Removal of statements may make some EH edges dead. Purge
318 such edges from the CFG as needed. */
319 if (!bitmap_empty_p (need_eh_cleanup
))
321 cfg_altered
|= tree_purge_all_dead_eh_edges (need_eh_cleanup
);
322 bitmap_zero (need_eh_cleanup
);
326 free_dominance_info (CDI_DOMINATORS
);
328 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
330 Long term we will be able to let everything in SSA_NAME_VALUE
331 persist. However, for now, we know this is the safe thing to do. */
332 for (i
= 0; i
< num_ssa_names
; i
++)
334 tree name
= ssa_name (i
);
340 value
= SSA_NAME_VALUE (name
);
341 if (value
&& !is_gimple_min_invariant (value
))
342 SSA_NAME_VALUE (name
) = NULL
;
345 /* Debugging dumps. */
346 if (dump_file
&& (dump_flags
& TDF_STATS
))
347 dump_dominator_optimization_stats (dump_file
);
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
, heap
, stmts_to_rescan
);
364 gate_dominator (void)
366 return flag_tree_dom
!= 0;
369 struct tree_opt_pass pass_dominator
=
372 gate_dominator
, /* gate */
373 tree_ssa_dominator_optimize
, /* execute */
376 0, /* static_pass_number */
377 TV_TREE_SSA_DOMINATOR_OPTS
, /* tv_id */
378 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
379 0, /* properties_provided */
380 0, /* properties_destroyed */
381 0, /* todo_flags_start */
385 | TODO_verify_ssa
, /* todo_flags_finish */
390 /* Given a stmt CONDSTMT containing a COND_EXPR, canonicalize the
391 COND_EXPR into a canonical form. */
394 canonicalize_comparison (tree condstmt
)
396 tree cond
= COND_EXPR_COND (condstmt
);
399 enum tree_code code
= TREE_CODE (cond
);
401 if (!COMPARISON_CLASS_P (cond
))
404 op0
= TREE_OPERAND (cond
, 0);
405 op1
= TREE_OPERAND (cond
, 1);
407 /* If it would be profitable to swap the operands, then do so to
408 canonicalize the statement, enabling better optimization.
410 By placing canonicalization of such expressions here we
411 transparently keep statements in canonical form, even
412 when the statement is modified. */
413 if (tree_swap_operands_p (op0
, op1
, false))
415 /* For relationals we need to swap the operands
416 and change the code. */
422 TREE_SET_CODE (cond
, swap_tree_comparison (code
));
423 swap_tree_operands (condstmt
,
424 &TREE_OPERAND (cond
, 0),
425 &TREE_OPERAND (cond
, 1));
426 /* If one operand was in the operand cache, but the other is
427 not, because it is a constant, this is a case that the
428 internal updating code of swap_tree_operands can't handle
430 if (TREE_CODE_CLASS (TREE_CODE (op0
))
431 != TREE_CODE_CLASS (TREE_CODE (op1
)))
432 update_stmt (condstmt
);
437 /* Initialize local stacks for this optimizer and record equivalences
438 upon entry to BB. Equivalences can come from the edge traversed to
439 reach BB or they may come from PHI nodes at the start of BB. */
442 dom_opt_initialize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
445 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
446 fprintf (dump_file
, "\n\nOptimizing block #%d\n\n", bb
->index
);
448 /* Push a marker on the stacks of local information so that we know how
449 far to unwind when we finalize this block. */
450 VEC_safe_push (tree
, heap
, avail_exprs_stack
, NULL_TREE
);
451 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
453 record_equivalences_from_incoming_edge (bb
);
455 /* PHI nodes can create equivalences too. */
456 record_equivalences_from_phis (bb
);
459 /* Given an expression EXPR (a relational expression or a statement),
460 initialize the hash table element pointed to by ELEMENT. */
463 initialize_hash_element (tree expr
, tree lhs
, struct expr_hash_elt
*element
)
465 /* Hash table elements may be based on conditional expressions or statements.
467 For the former case, we have no annotation and we want to hash the
468 conditional expression. In the latter case we have an annotation and
469 we want to record the expression the statement evaluates. */
470 if (COMPARISON_CLASS_P (expr
) || TREE_CODE (expr
) == TRUTH_NOT_EXPR
)
472 element
->stmt
= NULL
;
475 else if (TREE_CODE (expr
) == COND_EXPR
)
477 element
->stmt
= expr
;
478 element
->rhs
= COND_EXPR_COND (expr
);
480 else if (TREE_CODE (expr
) == SWITCH_EXPR
)
482 element
->stmt
= expr
;
483 element
->rhs
= SWITCH_COND (expr
);
485 else if (TREE_CODE (expr
) == RETURN_EXPR
&& TREE_OPERAND (expr
, 0))
487 element
->stmt
= expr
;
488 element
->rhs
= TREE_OPERAND (TREE_OPERAND (expr
, 0), 1);
490 else if (TREE_CODE (expr
) == GOTO_EXPR
)
492 element
->stmt
= expr
;
493 element
->rhs
= GOTO_DESTINATION (expr
);
497 element
->stmt
= expr
;
498 element
->rhs
= TREE_OPERAND (expr
, 1);
502 element
->hash
= avail_expr_hash (element
);
505 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
506 LIMIT entries left in LOCALs. */
509 remove_local_expressions_from_table (void)
511 /* Remove all the expressions made available in this block. */
512 while (VEC_length (tree
, avail_exprs_stack
) > 0)
514 struct expr_hash_elt element
;
515 tree expr
= VEC_pop (tree
, avail_exprs_stack
);
517 if (expr
== NULL_TREE
)
520 initialize_hash_element (expr
, NULL
, &element
);
521 htab_remove_elt_with_hash (avail_exprs
, &element
, element
.hash
);
525 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
526 CONST_AND_COPIES to its original state, stopping when we hit a
530 restore_vars_to_original_value (void)
532 while (VEC_length (tree
, const_and_copies_stack
) > 0)
534 tree prev_value
, dest
;
536 dest
= VEC_pop (tree
, const_and_copies_stack
);
541 prev_value
= VEC_pop (tree
, const_and_copies_stack
);
542 SSA_NAME_VALUE (dest
) = prev_value
;
546 /* A trivial wrapper so that we can present the generic jump
547 threading code with a simple API for simplifying statements. */
549 simplify_stmt_for_jump_threading (tree stmt
)
551 return lookup_avail_expr (stmt
, false);
554 /* Wrapper for common code to attempt to thread an edge. For example,
555 it handles lazily building the dummy condition and the bookkeeping
556 when jump threading is successful. */
559 dom_thread_across_edge (struct dom_walk_data
*walk_data
, edge e
)
561 /* If we don't already have a dummy condition, build it now. */
562 if (! walk_data
->global_data
)
564 tree dummy_cond
= build2 (NE_EXPR
, boolean_type_node
,
565 integer_zero_node
, integer_zero_node
);
566 dummy_cond
= build3 (COND_EXPR
, void_type_node
, dummy_cond
, NULL
, NULL
);
567 walk_data
->global_data
= dummy_cond
;
570 thread_across_edge (walk_data
->global_data
, e
, false,
571 &const_and_copies_stack
,
572 simplify_stmt_for_jump_threading
);
575 /* We have finished processing the dominator children of BB, perform
576 any finalization actions in preparation for leaving this node in
577 the dominator tree. */
580 dom_opt_finalize_block (struct dom_walk_data
*walk_data
, basic_block bb
)
585 /* If we have an outgoing edge to a block with multiple incoming and
586 outgoing edges, then we may be able to thread the edge. ie, we
587 may be able to statically determine which of the outgoing edges
588 will be traversed when the incoming edge from BB is traversed. */
589 if (single_succ_p (bb
)
590 && (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
) == 0
591 && potentially_threadable_block (single_succ (bb
)))
593 dom_thread_across_edge (walk_data
, single_succ_edge (bb
));
595 else if ((last
= last_stmt (bb
))
596 && TREE_CODE (last
) == COND_EXPR
597 && (COMPARISON_CLASS_P (COND_EXPR_COND (last
))
598 || TREE_CODE (COND_EXPR_COND (last
)) == SSA_NAME
)
599 && EDGE_COUNT (bb
->succs
) == 2
600 && (EDGE_SUCC (bb
, 0)->flags
& EDGE_ABNORMAL
) == 0
601 && (EDGE_SUCC (bb
, 1)->flags
& EDGE_ABNORMAL
) == 0)
603 edge true_edge
, false_edge
;
605 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
607 /* Only try to thread the edge if it reaches a target block with
608 more than one predecessor and more than one successor. */
609 if (potentially_threadable_block (true_edge
->dest
))
611 struct edge_info
*edge_info
;
614 /* Push a marker onto the available expression stack so that we
615 unwind any expressions related to the TRUE arm before processing
616 the false arm below. */
617 VEC_safe_push (tree
, heap
, avail_exprs_stack
, NULL_TREE
);
618 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
620 edge_info
= (struct edge_info
*) true_edge
->aux
;
622 /* If we have info associated with this edge, record it into
623 our equivalency tables. */
626 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
627 tree lhs
= edge_info
->lhs
;
628 tree rhs
= edge_info
->rhs
;
630 /* If we have a simple NAME = VALUE equivalency record it. */
631 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
632 record_const_or_copy (lhs
, rhs
);
634 /* If we have 0 = COND or 1 = COND equivalences, record them
635 into our expression hash tables. */
636 if (cond_equivalences
)
637 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
639 tree expr
= cond_equivalences
[i
];
640 tree value
= cond_equivalences
[i
+ 1];
642 record_cond (expr
, value
);
646 dom_thread_across_edge (walk_data
, true_edge
);
648 /* And restore the various tables to their state before
649 we threaded this edge. */
650 remove_local_expressions_from_table ();
653 /* Similarly for the ELSE arm. */
654 if (potentially_threadable_block (false_edge
->dest
))
656 struct edge_info
*edge_info
;
659 VEC_safe_push (tree
, heap
, const_and_copies_stack
, NULL_TREE
);
660 edge_info
= (struct edge_info
*) false_edge
->aux
;
662 /* If we have info associated with this edge, record it into
663 our equivalency tables. */
666 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
667 tree lhs
= edge_info
->lhs
;
668 tree rhs
= edge_info
->rhs
;
670 /* If we have a simple NAME = VALUE equivalency record it. */
671 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
672 record_const_or_copy (lhs
, rhs
);
674 /* If we have 0 = COND or 1 = COND equivalences, record them
675 into our expression hash tables. */
676 if (cond_equivalences
)
677 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
679 tree expr
= cond_equivalences
[i
];
680 tree value
= cond_equivalences
[i
+ 1];
682 record_cond (expr
, value
);
686 /* Now thread the edge. */
687 dom_thread_across_edge (walk_data
, false_edge
);
689 /* No need to remove local expressions from our tables
690 or restore vars to their original value as that will
691 be done immediately below. */
695 remove_local_expressions_from_table ();
696 restore_vars_to_original_value ();
698 /* If we queued any statements to rescan in this block, then
699 go ahead and rescan them now. */
700 while (VEC_length (tree
, stmts_to_rescan
) > 0)
702 tree stmt
= VEC_last (tree
, stmts_to_rescan
);
703 basic_block stmt_bb
= bb_for_stmt (stmt
);
708 VEC_pop (tree
, stmts_to_rescan
);
709 mark_new_vars_to_rename (stmt
);
713 /* PHI nodes can create equivalences too.
715 Ignoring any alternatives which are the same as the result, if
716 all the alternatives are equal, then the PHI node creates an
720 record_equivalences_from_phis (basic_block bb
)
724 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
726 tree lhs
= PHI_RESULT (phi
);
730 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
732 tree t
= PHI_ARG_DEF (phi
, i
);
734 /* Ignore alternatives which are the same as our LHS. Since
735 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
736 can simply compare pointers. */
740 /* If we have not processed an alternative yet, then set
741 RHS to this alternative. */
744 /* If we have processed an alternative (stored in RHS), then
745 see if it is equal to this one. If it isn't, then stop
747 else if (! operand_equal_for_phi_arg_p (rhs
, t
))
751 /* If we had no interesting alternatives, then all the RHS alternatives
752 must have been the same as LHS. */
756 /* If we managed to iterate through each PHI alternative without
757 breaking out of the loop, then we have a PHI which may create
758 a useful equivalence. We do not need to record unwind data for
759 this, since this is a true assignment and not an equivalence
760 inferred from a comparison. All uses of this ssa name are dominated
761 by this assignment, so unwinding just costs time and space. */
762 if (i
== PHI_NUM_ARGS (phi
)
763 && may_propagate_copy (lhs
, rhs
))
764 SSA_NAME_VALUE (lhs
) = rhs
;
768 /* Ignoring loop backedges, if BB has precisely one incoming edge then
769 return that edge. Otherwise return NULL. */
771 single_incoming_edge_ignoring_loop_edges (basic_block bb
)
777 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
779 /* A loop back edge can be identified by the destination of
780 the edge dominating the source of the edge. */
781 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
784 /* If we have already seen a non-loop edge, then we must have
785 multiple incoming non-loop edges and thus we return NULL. */
789 /* This is the first non-loop incoming edge we have found. Record
797 /* Record any equivalences created by the incoming edge to BB. If BB
798 has more than one incoming edge, then no equivalence is created. */
801 record_equivalences_from_incoming_edge (basic_block bb
)
805 struct edge_info
*edge_info
;
807 /* If our parent block ended with a control statement, then we may be
808 able to record some equivalences based on which outgoing edge from
809 the parent was followed. */
810 parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
812 e
= single_incoming_edge_ignoring_loop_edges (bb
);
814 /* If we had a single incoming edge from our parent block, then enter
815 any data associated with the edge into our tables. */
816 if (e
&& e
->src
== parent
)
820 edge_info
= (struct edge_info
*) e
->aux
;
824 tree lhs
= edge_info
->lhs
;
825 tree rhs
= edge_info
->rhs
;
826 tree
*cond_equivalences
= edge_info
->cond_equivalences
;
829 record_equality (lhs
, rhs
);
831 if (cond_equivalences
)
833 for (i
= 0; i
< edge_info
->max_cond_equivalences
; i
+= 2)
835 tree expr
= cond_equivalences
[i
];
836 tree value
= cond_equivalences
[i
+ 1];
838 record_cond (expr
, value
);
845 /* Dump SSA statistics on FILE. */
848 dump_dominator_optimization_stats (FILE *file
)
852 fprintf (file
, "Total number of statements: %6ld\n\n",
853 opt_stats
.num_stmts
);
854 fprintf (file
, "Exprs considered for dominator optimizations: %6ld\n",
855 opt_stats
.num_exprs_considered
);
857 n_exprs
= opt_stats
.num_exprs_considered
;
861 fprintf (file
, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
862 opt_stats
.num_re
, PERCENT (opt_stats
.num_re
,
864 fprintf (file
, " Constants propagated: %6ld\n",
865 opt_stats
.num_const_prop
);
866 fprintf (file
, " Copies propagated: %6ld\n",
867 opt_stats
.num_copy_prop
);
869 fprintf (file
, "\nHash table statistics:\n");
871 fprintf (file
, " avail_exprs: ");
872 htab_statistics (file
, avail_exprs
);
876 /* Dump SSA statistics on stderr. */
879 debug_dominator_optimization_stats (void)
881 dump_dominator_optimization_stats (stderr
);
885 /* Dump statistics for the hash table HTAB. */
888 htab_statistics (FILE *file
, htab_t htab
)
890 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
891 (long) htab_size (htab
),
892 (long) htab_elements (htab
),
893 htab_collisions (htab
));
896 /* Enter a statement into the true/false expression hash table indicating
897 that the condition COND has the value VALUE. */
900 record_cond (tree cond
, tree value
)
902 struct expr_hash_elt
*element
= XCNEW (struct expr_hash_elt
);
905 initialize_hash_element (cond
, value
, element
);
907 slot
= htab_find_slot_with_hash (avail_exprs
, (void *)element
,
908 element
->hash
, INSERT
);
911 *slot
= (void *) element
;
912 VEC_safe_push (tree
, heap
, avail_exprs_stack
, cond
);
918 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
919 the new conditional into *p, then store a boolean_true_node
923 build_and_record_new_cond (enum tree_code new_code
, tree op0
, tree op1
, tree
*p
)
925 *p
= build2 (new_code
, boolean_type_node
, op0
, op1
);
927 *p
= boolean_true_node
;
930 /* Record that COND is true and INVERTED is false into the edge information
931 structure. Also record that any conditions dominated by COND are true
934 For example, if a < b is true, then a <= b must also be true. */
937 record_conditions (struct edge_info
*edge_info
, tree cond
, tree inverted
)
941 if (!COMPARISON_CLASS_P (cond
))
944 op0
= TREE_OPERAND (cond
, 0);
945 op1
= TREE_OPERAND (cond
, 1);
947 switch (TREE_CODE (cond
))
951 edge_info
->max_cond_equivalences
= 12;
952 edge_info
->cond_equivalences
= XNEWVEC (tree
, 12);
953 build_and_record_new_cond ((TREE_CODE (cond
) == LT_EXPR
954 ? LE_EXPR
: GE_EXPR
),
955 op0
, op1
, &edge_info
->cond_equivalences
[4]);
956 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
957 &edge_info
->cond_equivalences
[6]);
958 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
959 &edge_info
->cond_equivalences
[8]);
960 build_and_record_new_cond (LTGT_EXPR
, op0
, op1
,
961 &edge_info
->cond_equivalences
[10]);
966 edge_info
->max_cond_equivalences
= 6;
967 edge_info
->cond_equivalences
= XNEWVEC (tree
, 6);
968 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
969 &edge_info
->cond_equivalences
[4]);
973 edge_info
->max_cond_equivalences
= 10;
974 edge_info
->cond_equivalences
= XNEWVEC (tree
, 10);
975 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
976 &edge_info
->cond_equivalences
[4]);
977 build_and_record_new_cond (LE_EXPR
, op0
, op1
,
978 &edge_info
->cond_equivalences
[6]);
979 build_and_record_new_cond (GE_EXPR
, op0
, op1
,
980 &edge_info
->cond_equivalences
[8]);
984 edge_info
->max_cond_equivalences
= 16;
985 edge_info
->cond_equivalences
= XNEWVEC (tree
, 16);
986 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
987 &edge_info
->cond_equivalences
[4]);
988 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
989 &edge_info
->cond_equivalences
[6]);
990 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
991 &edge_info
->cond_equivalences
[8]);
992 build_and_record_new_cond (UNEQ_EXPR
, op0
, op1
,
993 &edge_info
->cond_equivalences
[10]);
994 build_and_record_new_cond (UNLT_EXPR
, op0
, op1
,
995 &edge_info
->cond_equivalences
[12]);
996 build_and_record_new_cond (UNGT_EXPR
, op0
, op1
,
997 &edge_info
->cond_equivalences
[14]);
1002 edge_info
->max_cond_equivalences
= 8;
1003 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1004 build_and_record_new_cond ((TREE_CODE (cond
) == UNLT_EXPR
1005 ? UNLE_EXPR
: UNGE_EXPR
),
1006 op0
, op1
, &edge_info
->cond_equivalences
[4]);
1007 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
1008 &edge_info
->cond_equivalences
[6]);
1012 edge_info
->max_cond_equivalences
= 8;
1013 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1014 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
1015 &edge_info
->cond_equivalences
[4]);
1016 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
1017 &edge_info
->cond_equivalences
[6]);
1021 edge_info
->max_cond_equivalences
= 8;
1022 edge_info
->cond_equivalences
= XNEWVEC (tree
, 8);
1023 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
1024 &edge_info
->cond_equivalences
[4]);
1025 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
1026 &edge_info
->cond_equivalences
[6]);
1030 edge_info
->max_cond_equivalences
= 4;
1031 edge_info
->cond_equivalences
= XNEWVEC (tree
, 4);
1035 /* Now store the original true and false conditions into the first
1037 edge_info
->cond_equivalences
[0] = cond
;
1038 edge_info
->cond_equivalences
[1] = boolean_true_node
;
1039 edge_info
->cond_equivalences
[2] = inverted
;
1040 edge_info
->cond_equivalences
[3] = boolean_false_node
;
1043 /* A helper function for record_const_or_copy and record_equality.
1044 Do the work of recording the value and undo info. */
1047 record_const_or_copy_1 (tree x
, tree y
, tree prev_x
)
1049 SSA_NAME_VALUE (x
) = y
;
1051 VEC_reserve (tree
, heap
, const_and_copies_stack
, 2);
1052 VEC_quick_push (tree
, const_and_copies_stack
, prev_x
);
1053 VEC_quick_push (tree
, const_and_copies_stack
, x
);
1057 /* Return the loop depth of the basic block of the defining statement of X.
1058 This number should not be treated as absolutely correct because the loop
1059 information may not be completely up-to-date when dom runs. However, it
1060 will be relatively correct, and as more passes are taught to keep loop info
1061 up to date, the result will become more and more accurate. */
1064 loop_depth_of_name (tree x
)
1069 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1070 if (TREE_CODE (x
) != SSA_NAME
)
1073 /* Otherwise return the loop depth of the defining statement's bb.
1074 Note that there may not actually be a bb for this statement, if the
1075 ssa_name is live on entry. */
1076 defstmt
= SSA_NAME_DEF_STMT (x
);
1077 defbb
= bb_for_stmt (defstmt
);
1081 return defbb
->loop_depth
;
1085 /* Record that X is equal to Y in const_and_copies. Record undo
1086 information in the block-local vector. */
1089 record_const_or_copy (tree x
, tree y
)
1091 tree prev_x
= SSA_NAME_VALUE (x
);
1093 if (TREE_CODE (y
) == SSA_NAME
)
1095 tree tmp
= SSA_NAME_VALUE (y
);
1100 record_const_or_copy_1 (x
, y
, prev_x
);
1103 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1104 This constrains the cases in which we may treat this as assignment. */
1107 record_equality (tree x
, tree y
)
1109 tree prev_x
= NULL
, prev_y
= NULL
;
1111 if (TREE_CODE (x
) == SSA_NAME
)
1112 prev_x
= SSA_NAME_VALUE (x
);
1113 if (TREE_CODE (y
) == SSA_NAME
)
1114 prev_y
= SSA_NAME_VALUE (y
);
1116 /* If one of the previous values is invariant, or invariant in more loops
1117 (by depth), then use that.
1118 Otherwise it doesn't matter which value we choose, just so
1119 long as we canonicalize on one value. */
1120 if (TREE_INVARIANT (y
))
1122 else if (TREE_INVARIANT (x
) || (loop_depth_of_name (x
) <= loop_depth_of_name (y
)))
1123 prev_x
= x
, x
= y
, y
= prev_x
, prev_x
= prev_y
;
1124 else if (prev_x
&& TREE_INVARIANT (prev_x
))
1125 x
= y
, y
= prev_x
, prev_x
= prev_y
;
1126 else if (prev_y
&& TREE_CODE (prev_y
) != VALUE_HANDLE
)
1129 /* After the swapping, we must have one SSA_NAME. */
1130 if (TREE_CODE (x
) != SSA_NAME
)
1133 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1134 variable compared against zero. If we're honoring signed zeros,
1135 then we cannot record this value unless we know that the value is
1137 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x
)))
1138 && (TREE_CODE (y
) != REAL_CST
1139 || REAL_VALUES_EQUAL (dconst0
, TREE_REAL_CST (y
))))
1142 record_const_or_copy_1 (x
, y
, prev_x
);
1145 /* Returns true when STMT is a simple iv increment. It detects the
1146 following situation:
1148 i_1 = phi (..., i_2)
1149 i_2 = i_1 +/- ... */
1152 simple_iv_increment_p (tree stmt
)
1154 tree lhs
, rhs
, preinc
, phi
;
1157 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
1160 lhs
= TREE_OPERAND (stmt
, 0);
1161 if (TREE_CODE (lhs
) != SSA_NAME
)
1164 rhs
= TREE_OPERAND (stmt
, 1);
1166 if (TREE_CODE (rhs
) != PLUS_EXPR
1167 && TREE_CODE (rhs
) != MINUS_EXPR
)
1170 preinc
= TREE_OPERAND (rhs
, 0);
1171 if (TREE_CODE (preinc
) != SSA_NAME
)
1174 phi
= SSA_NAME_DEF_STMT (preinc
);
1175 if (TREE_CODE (phi
) != PHI_NODE
)
1178 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
1179 if (PHI_ARG_DEF (phi
, i
) == lhs
)
1185 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1186 known value for that SSA_NAME (or NULL if no value is known).
1188 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1189 successors of BB. */
1192 cprop_into_successor_phis (basic_block bb
)
1197 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1202 /* If this is an abnormal edge, then we do not want to copy propagate
1203 into the PHI alternative associated with this edge. */
1204 if (e
->flags
& EDGE_ABNORMAL
)
1207 phi
= phi_nodes (e
->dest
);
1212 for ( ; phi
; phi
= PHI_CHAIN (phi
))
1215 use_operand_p orig_p
;
1218 /* The alternative may be associated with a constant, so verify
1219 it is an SSA_NAME before doing anything with it. */
1220 orig_p
= PHI_ARG_DEF_PTR (phi
, indx
);
1221 orig
= USE_FROM_PTR (orig_p
);
1222 if (TREE_CODE (orig
) != SSA_NAME
)
1225 /* If we have *ORIG_P in our constant/copy table, then replace
1226 ORIG_P with its value in our constant/copy table. */
1227 new = SSA_NAME_VALUE (orig
);
1230 && (TREE_CODE (new) == SSA_NAME
1231 || is_gimple_min_invariant (new))
1232 && may_propagate_copy (orig
, new))
1233 propagate_value (orig_p
, new);
1238 /* We have finished optimizing BB, record any information implied by
1239 taking a specific outgoing edge from BB. */
1242 record_edge_info (basic_block bb
)
1244 block_stmt_iterator bsi
= bsi_last (bb
);
1245 struct edge_info
*edge_info
;
1247 if (! bsi_end_p (bsi
))
1249 tree stmt
= bsi_stmt (bsi
);
1251 if (stmt
&& TREE_CODE (stmt
) == SWITCH_EXPR
)
1253 tree cond
= SWITCH_COND (stmt
);
1255 if (TREE_CODE (cond
) == SSA_NAME
)
1257 tree labels
= SWITCH_LABELS (stmt
);
1258 int i
, n_labels
= TREE_VEC_LENGTH (labels
);
1259 tree
*info
= XCNEWVEC (tree
, last_basic_block
);
1263 for (i
= 0; i
< n_labels
; i
++)
1265 tree label
= TREE_VEC_ELT (labels
, i
);
1266 basic_block target_bb
= label_to_block (CASE_LABEL (label
));
1268 if (CASE_HIGH (label
)
1269 || !CASE_LOW (label
)
1270 || info
[target_bb
->index
])
1271 info
[target_bb
->index
] = error_mark_node
;
1273 info
[target_bb
->index
] = label
;
1276 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1278 basic_block target_bb
= e
->dest
;
1279 tree node
= info
[target_bb
->index
];
1281 if (node
!= NULL
&& node
!= error_mark_node
)
1283 tree x
= fold_convert (TREE_TYPE (cond
), CASE_LOW (node
));
1284 edge_info
= allocate_edge_info (e
);
1285 edge_info
->lhs
= cond
;
1293 /* A COND_EXPR may create equivalences too. */
1294 if (stmt
&& TREE_CODE (stmt
) == COND_EXPR
)
1296 tree cond
= COND_EXPR_COND (stmt
);
1300 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
1302 /* If the conditional is a single variable 'X', record 'X = 1'
1303 for the true edge and 'X = 0' on the false edge. */
1304 if (SSA_VAR_P (cond
))
1306 struct edge_info
*edge_info
;
1308 edge_info
= allocate_edge_info (true_edge
);
1309 edge_info
->lhs
= cond
;
1310 edge_info
->rhs
= constant_boolean_node (1, TREE_TYPE (cond
));
1312 edge_info
= allocate_edge_info (false_edge
);
1313 edge_info
->lhs
= cond
;
1314 edge_info
->rhs
= constant_boolean_node (0, TREE_TYPE (cond
));
1316 /* Equality tests may create one or two equivalences. */
1317 else if (COMPARISON_CLASS_P (cond
))
1319 tree op0
= TREE_OPERAND (cond
, 0);
1320 tree op1
= TREE_OPERAND (cond
, 1);
1322 /* Special case comparing booleans against a constant as we
1323 know the value of OP0 on both arms of the branch. i.e., we
1324 can record an equivalence for OP0 rather than COND. */
1325 if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
1326 && TREE_CODE (op0
) == SSA_NAME
1327 && TREE_CODE (TREE_TYPE (op0
)) == BOOLEAN_TYPE
1328 && is_gimple_min_invariant (op1
))
1330 if (TREE_CODE (cond
) == EQ_EXPR
)
1332 edge_info
= allocate_edge_info (true_edge
);
1333 edge_info
->lhs
= op0
;
1334 edge_info
->rhs
= (integer_zerop (op1
)
1335 ? boolean_false_node
1336 : boolean_true_node
);
1338 edge_info
= allocate_edge_info (false_edge
);
1339 edge_info
->lhs
= op0
;
1340 edge_info
->rhs
= (integer_zerop (op1
)
1342 : boolean_false_node
);
1346 edge_info
= allocate_edge_info (true_edge
);
1347 edge_info
->lhs
= op0
;
1348 edge_info
->rhs
= (integer_zerop (op1
)
1350 : boolean_false_node
);
1352 edge_info
= allocate_edge_info (false_edge
);
1353 edge_info
->lhs
= op0
;
1354 edge_info
->rhs
= (integer_zerop (op1
)
1355 ? boolean_false_node
1356 : boolean_true_node
);
1360 else if (is_gimple_min_invariant (op0
)
1361 && (TREE_CODE (op1
) == SSA_NAME
1362 || is_gimple_min_invariant (op1
)))
1364 tree inverted
= invert_truthvalue (cond
);
1365 struct edge_info
*edge_info
;
1367 edge_info
= allocate_edge_info (true_edge
);
1368 record_conditions (edge_info
, cond
, inverted
);
1370 if (TREE_CODE (cond
) == EQ_EXPR
)
1372 edge_info
->lhs
= op1
;
1373 edge_info
->rhs
= op0
;
1376 edge_info
= allocate_edge_info (false_edge
);
1377 record_conditions (edge_info
, inverted
, cond
);
1379 if (TREE_CODE (cond
) == NE_EXPR
)
1381 edge_info
->lhs
= op1
;
1382 edge_info
->rhs
= op0
;
1386 else if (TREE_CODE (op0
) == SSA_NAME
1387 && (is_gimple_min_invariant (op1
)
1388 || TREE_CODE (op1
) == SSA_NAME
))
1390 tree inverted
= invert_truthvalue (cond
);
1391 struct edge_info
*edge_info
;
1393 edge_info
= allocate_edge_info (true_edge
);
1394 record_conditions (edge_info
, cond
, inverted
);
1396 if (TREE_CODE (cond
) == EQ_EXPR
)
1398 edge_info
->lhs
= op0
;
1399 edge_info
->rhs
= op1
;
1402 edge_info
= allocate_edge_info (false_edge
);
1403 record_conditions (edge_info
, inverted
, cond
);
1405 if (TREE_CODE (cond
) == NE_EXPR
)
1407 edge_info
->lhs
= op0
;
1408 edge_info
->rhs
= op1
;
1413 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1418 /* Propagate information from BB to its outgoing edges.
1420 This can include equivalency information implied by control statements
1421 at the end of BB and const/copy propagation into PHIs in BB's
1422 successor blocks. */
1425 propagate_to_outgoing_edges (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1428 record_edge_info (bb
);
1429 cprop_into_successor_phis (bb
);
1432 /* Search for redundant computations in STMT. If any are found, then
1433 replace them with the variable holding the result of the computation.
1435 If safe, record this expression into the available expression hash
1439 eliminate_redundant_computations (tree stmt
)
1441 tree
*expr_p
, def
= NULL_TREE
;
1444 bool retval
= false;
1445 bool modify_expr_p
= false;
1447 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
1448 def
= TREE_OPERAND (stmt
, 0);
1450 /* Certain expressions on the RHS can be optimized away, but can not
1451 themselves be entered into the hash tables. */
1453 || TREE_CODE (def
) != SSA_NAME
1454 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def
)
1455 || !ZERO_SSA_OPERANDS (stmt
, SSA_OP_VMAYDEF
)
1456 /* Do not record equivalences for increments of ivs. This would create
1457 overlapping live ranges for a very questionable gain. */
1458 || simple_iv_increment_p (stmt
))
1461 /* Check if the expression has been computed before. */
1462 cached_lhs
= lookup_avail_expr (stmt
, insert
);
1464 opt_stats
.num_exprs_considered
++;
1466 /* Get a pointer to the expression we are trying to optimize. */
1467 if (TREE_CODE (stmt
) == COND_EXPR
)
1468 expr_p
= &COND_EXPR_COND (stmt
);
1469 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1470 expr_p
= &SWITCH_COND (stmt
);
1471 else if (TREE_CODE (stmt
) == RETURN_EXPR
&& TREE_OPERAND (stmt
, 0))
1473 expr_p
= &TREE_OPERAND (TREE_OPERAND (stmt
, 0), 1);
1474 modify_expr_p
= true;
1478 expr_p
= &TREE_OPERAND (stmt
, 1);
1479 modify_expr_p
= true;
1482 /* It is safe to ignore types here since we have already done
1483 type checking in the hashing and equality routines. In fact
1484 type checking here merely gets in the way of constant
1485 propagation. Also, make sure that it is safe to propagate
1486 CACHED_LHS into *EXPR_P. */
1488 && ((TREE_CODE (cached_lhs
) != SSA_NAME
1490 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p
),
1491 TREE_TYPE (cached_lhs
))))
1492 || may_propagate_copy (*expr_p
, cached_lhs
)))
1494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1496 fprintf (dump_file
, " Replaced redundant expr '");
1497 print_generic_expr (dump_file
, *expr_p
, dump_flags
);
1498 fprintf (dump_file
, "' with '");
1499 print_generic_expr (dump_file
, cached_lhs
, dump_flags
);
1500 fprintf (dump_file
, "'\n");
1505 #if defined ENABLE_CHECKING
1506 gcc_assert (TREE_CODE (cached_lhs
) == SSA_NAME
1507 || is_gimple_min_invariant (cached_lhs
));
1510 if (TREE_CODE (cached_lhs
) == ADDR_EXPR
1511 || (POINTER_TYPE_P (TREE_TYPE (*expr_p
))
1512 && is_gimple_min_invariant (cached_lhs
)))
1516 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p
),
1517 TREE_TYPE (cached_lhs
)))
1518 cached_lhs
= fold_convert (TREE_TYPE (*expr_p
), cached_lhs
);
1520 propagate_tree_value (expr_p
, cached_lhs
);
1521 mark_stmt_modified (stmt
);
1526 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
1527 the available expressions table or the const_and_copies table.
1528 Detect and record those equivalences. */
1531 record_equivalences_from_stmt (tree stmt
,
1535 tree lhs
= TREE_OPERAND (stmt
, 0);
1536 enum tree_code lhs_code
= TREE_CODE (lhs
);
1538 if (lhs_code
== SSA_NAME
)
1540 tree rhs
= TREE_OPERAND (stmt
, 1);
1542 /* Strip away any useless type conversions. */
1543 STRIP_USELESS_TYPE_CONVERSION (rhs
);
1545 /* If the RHS of the assignment is a constant or another variable that
1546 may be propagated, register it in the CONST_AND_COPIES table. We
1547 do not need to record unwind data for this, since this is a true
1548 assignment and not an equivalence inferred from a comparison. All
1549 uses of this ssa name are dominated by this assignment, so unwinding
1550 just costs time and space. */
1552 && (TREE_CODE (rhs
) == SSA_NAME
1553 || is_gimple_min_invariant (rhs
)))
1554 SSA_NAME_VALUE (lhs
) = rhs
;
1557 /* A memory store, even an aliased store, creates a useful
1558 equivalence. By exchanging the LHS and RHS, creating suitable
1559 vops and recording the result in the available expression table,
1560 we may be able to expose more redundant loads. */
1561 if (!ann
->has_volatile_ops
1562 && (TREE_CODE (TREE_OPERAND (stmt
, 1)) == SSA_NAME
1563 || is_gimple_min_invariant (TREE_OPERAND (stmt
, 1)))
1564 && !is_gimple_reg (lhs
))
1566 tree rhs
= TREE_OPERAND (stmt
, 1);
1569 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
1570 is a constant, we need to adjust the constant to fit into the
1571 type of the LHS. If the LHS is a bitfield and the RHS is not
1572 a constant, then we can not record any equivalences for this
1573 statement since we would need to represent the widening or
1574 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
1575 and should not be necessary if GCC represented bitfields
1577 if (lhs_code
== COMPONENT_REF
1578 && DECL_BIT_FIELD (TREE_OPERAND (lhs
, 1)))
1580 if (TREE_CONSTANT (rhs
))
1581 rhs
= widen_bitfield (rhs
, TREE_OPERAND (lhs
, 1), lhs
);
1585 /* If the value overflowed, then we can not use this equivalence. */
1586 if (rhs
&& ! is_gimple_min_invariant (rhs
))
1592 /* Build a new statement with the RHS and LHS exchanged. */
1593 new = build2 (MODIFY_EXPR
, TREE_TYPE (stmt
), rhs
, lhs
);
1595 create_ssa_artficial_load_stmt (new, stmt
);
1597 /* Finally enter the statement into the available expression
1599 lookup_avail_expr (new, true);
1604 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1605 CONST_AND_COPIES. */
1608 cprop_operand (tree stmt
, use_operand_p op_p
)
1610 bool may_have_exposed_new_symbols
= false;
1612 tree op
= USE_FROM_PTR (op_p
);
1614 /* If the operand has a known constant value or it is known to be a
1615 copy of some other variable, use the value or copy stored in
1616 CONST_AND_COPIES. */
1617 val
= SSA_NAME_VALUE (op
);
1618 if (val
&& val
!= op
&& TREE_CODE (val
) != VALUE_HANDLE
)
1620 tree op_type
, val_type
;
1622 /* Do not change the base variable in the virtual operand
1623 tables. That would make it impossible to reconstruct
1624 the renamed virtual operand if we later modify this
1625 statement. Also only allow the new value to be an SSA_NAME
1626 for propagation into virtual operands. */
1627 if (!is_gimple_reg (op
)
1628 && (TREE_CODE (val
) != SSA_NAME
1629 || is_gimple_reg (val
)
1630 || get_virtual_var (val
) != get_virtual_var (op
)))
1633 /* Do not replace hard register operands in asm statements. */
1634 if (TREE_CODE (stmt
) == ASM_EXPR
1635 && !may_propagate_copy_into_asm (op
))
1638 /* Get the toplevel type of each operand. */
1639 op_type
= TREE_TYPE (op
);
1640 val_type
= TREE_TYPE (val
);
1642 /* While both types are pointers, get the type of the object
1644 while (POINTER_TYPE_P (op_type
) && POINTER_TYPE_P (val_type
))
1646 op_type
= TREE_TYPE (op_type
);
1647 val_type
= TREE_TYPE (val_type
);
1650 /* Make sure underlying types match before propagating a constant by
1651 converting the constant to the proper type. Note that convert may
1652 return a non-gimple expression, in which case we ignore this
1653 propagation opportunity. */
1654 if (TREE_CODE (val
) != SSA_NAME
)
1656 if (!lang_hooks
.types_compatible_p (op_type
, val_type
))
1658 val
= fold_convert (TREE_TYPE (op
), val
);
1659 if (!is_gimple_min_invariant (val
))
1664 /* Certain operands are not allowed to be copy propagated due
1665 to their interaction with exception handling and some GCC
1667 else if (!may_propagate_copy (op
, val
))
1670 /* Do not propagate copies if the propagated value is at a deeper loop
1671 depth than the propagatee. Otherwise, this may move loop variant
1672 variables outside of their loops and prevent coalescing
1673 opportunities. If the value was loop invariant, it will be hoisted
1674 by LICM and exposed for copy propagation. */
1675 if (loop_depth_of_name (val
) > loop_depth_of_name (op
))
1679 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1681 fprintf (dump_file
, " Replaced '");
1682 print_generic_expr (dump_file
, op
, dump_flags
);
1683 fprintf (dump_file
, "' with %s '",
1684 (TREE_CODE (val
) != SSA_NAME
? "constant" : "variable"));
1685 print_generic_expr (dump_file
, val
, dump_flags
);
1686 fprintf (dump_file
, "'\n");
1689 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
1690 that we may have exposed a new symbol for SSA renaming. */
1691 if (TREE_CODE (val
) == ADDR_EXPR
1692 || (POINTER_TYPE_P (TREE_TYPE (op
))
1693 && is_gimple_min_invariant (val
)))
1694 may_have_exposed_new_symbols
= true;
1696 if (TREE_CODE (val
) != SSA_NAME
)
1697 opt_stats
.num_const_prop
++;
1699 opt_stats
.num_copy_prop
++;
1701 propagate_value (op_p
, val
);
1703 /* And note that we modified this statement. This is now
1704 safe, even if we changed virtual operands since we will
1705 rescan the statement and rewrite its operands again. */
1706 mark_stmt_modified (stmt
);
1708 return may_have_exposed_new_symbols
;
1711 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1712 known value for that SSA_NAME (or NULL if no value is known).
1714 Propagate values from CONST_AND_COPIES into the uses, vuses and
1715 v_may_def_ops of STMT. */
1718 cprop_into_stmt (tree stmt
)
1720 bool may_have_exposed_new_symbols
= false;
1724 FOR_EACH_SSA_USE_OPERAND (op_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1726 if (TREE_CODE (USE_FROM_PTR (op_p
)) == SSA_NAME
)
1727 may_have_exposed_new_symbols
|= cprop_operand (stmt
, op_p
);
1730 return may_have_exposed_new_symbols
;
1734 /* Optimize the statement pointed to by iterator SI.
1736 We try to perform some simplistic global redundancy elimination and
1737 constant propagation:
1739 1- To detect global redundancy, we keep track of expressions that have
1740 been computed in this block and its dominators. If we find that the
1741 same expression is computed more than once, we eliminate repeated
1742 computations by using the target of the first one.
1744 2- Constant values and copy assignments. This is used to do very
1745 simplistic constant and copy propagation. When a constant or copy
1746 assignment is found, we map the value on the RHS of the assignment to
1747 the variable in the LHS in the CONST_AND_COPIES table. */
1750 optimize_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1751 basic_block bb
, block_stmt_iterator si
)
1754 tree stmt
, old_stmt
;
1755 bool may_optimize_p
;
1756 bool may_have_exposed_new_symbols
= false;
1758 old_stmt
= stmt
= bsi_stmt (si
);
1760 if (TREE_CODE (stmt
) == COND_EXPR
)
1761 canonicalize_comparison (stmt
);
1763 update_stmt_if_modified (stmt
);
1764 ann
= stmt_ann (stmt
);
1765 opt_stats
.num_stmts
++;
1766 may_have_exposed_new_symbols
= false;
1768 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1770 fprintf (dump_file
, "Optimizing statement ");
1771 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1774 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
1775 may_have_exposed_new_symbols
= cprop_into_stmt (stmt
);
1777 /* If the statement has been modified with constant replacements,
1778 fold its RHS before checking for redundant computations. */
1783 /* Try to fold the statement making sure that STMT is kept
1785 if (fold_stmt (bsi_stmt_ptr (si
)))
1787 stmt
= bsi_stmt (si
);
1788 ann
= stmt_ann (stmt
);
1790 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1792 fprintf (dump_file
, " Folded to: ");
1793 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1797 rhs
= get_rhs (stmt
);
1798 if (rhs
&& TREE_CODE (rhs
) == ADDR_EXPR
)
1799 recompute_tree_invariant_for_addr_expr (rhs
);
1801 /* Constant/copy propagation above may change the set of
1802 virtual operands associated with this statement. Folding
1803 may remove the need for some virtual operands.
1805 Indicate we will need to rescan and rewrite the statement. */
1806 may_have_exposed_new_symbols
= true;
1809 /* Check for redundant computations. Do this optimization only
1810 for assignments that have no volatile ops and conditionals. */
1811 may_optimize_p
= (!ann
->has_volatile_ops
1812 && ((TREE_CODE (stmt
) == RETURN_EXPR
1813 && TREE_OPERAND (stmt
, 0)
1814 && TREE_CODE (TREE_OPERAND (stmt
, 0)) == MODIFY_EXPR
1815 && ! (TREE_SIDE_EFFECTS
1816 (TREE_OPERAND (TREE_OPERAND (stmt
, 0), 1))))
1817 || (TREE_CODE (stmt
) == MODIFY_EXPR
1818 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt
, 1)))
1819 || TREE_CODE (stmt
) == COND_EXPR
1820 || TREE_CODE (stmt
) == SWITCH_EXPR
));
1823 may_have_exposed_new_symbols
|= eliminate_redundant_computations (stmt
);
1825 /* Record any additional equivalences created by this statement. */
1826 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
1827 record_equivalences_from_stmt (stmt
,
1831 /* If STMT is a COND_EXPR and it was modified, then we may know
1832 where it goes. If that is the case, then mark the CFG as altered.
1834 This will cause us to later call remove_unreachable_blocks and
1835 cleanup_tree_cfg when it is safe to do so. It is not safe to
1836 clean things up here since removal of edges and such can trigger
1837 the removal of PHI nodes, which in turn can release SSA_NAMEs to
1840 That's all fine and good, except that once SSA_NAMEs are released
1841 to the manager, we must not call create_ssa_name until all references
1842 to released SSA_NAMEs have been eliminated.
1844 All references to the deleted SSA_NAMEs can not be eliminated until
1845 we remove unreachable blocks.
1847 We can not remove unreachable blocks until after we have completed
1848 any queued jump threading.
1850 We can not complete any queued jump threads until we have taken
1851 appropriate variables out of SSA form. Taking variables out of
1852 SSA form can call create_ssa_name and thus we lose.
1854 Ultimately I suspect we're going to need to change the interface
1855 into the SSA_NAME manager. */
1861 if (TREE_CODE (stmt
) == COND_EXPR
)
1862 val
= COND_EXPR_COND (stmt
);
1863 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1864 val
= SWITCH_COND (stmt
);
1866 if (val
&& TREE_CODE (val
) == INTEGER_CST
&& find_taken_edge (bb
, val
))
1869 /* If we simplified a statement in such a way as to be shown that it
1870 cannot trap, update the eh information and the cfg to match. */
1871 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1873 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
1874 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1875 fprintf (dump_file
, " Flagged to clear EH edges.\n");
1879 if (may_have_exposed_new_symbols
)
1880 VEC_safe_push (tree
, heap
, stmts_to_rescan
, bsi_stmt (si
));
1883 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
1884 found, return its LHS. Otherwise insert STMT in the table and return
1887 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
1888 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
1889 can be removed when we finish processing this block and its children.
1891 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
1892 contains no CALL_EXPR on its RHS and makes no volatile nor
1893 aliased references. */
1896 lookup_avail_expr (tree stmt
, bool insert
)
1901 struct expr_hash_elt
*element
= XNEW (struct expr_hash_elt
);
1903 lhs
= TREE_CODE (stmt
) == MODIFY_EXPR
? TREE_OPERAND (stmt
, 0) : NULL
;
1905 initialize_hash_element (stmt
, lhs
, element
);
1907 /* Don't bother remembering constant assignments and copy operations.
1908 Constants and copy operations are handled by the constant/copy propagator
1909 in optimize_stmt. */
1910 if (TREE_CODE (element
->rhs
) == SSA_NAME
1911 || is_gimple_min_invariant (element
->rhs
))
1917 /* Finally try to find the expression in the main expression hash table. */
1918 slot
= htab_find_slot_with_hash (avail_exprs
, element
, element
->hash
,
1919 (insert
? INSERT
: NO_INSERT
));
1928 *slot
= (void *) element
;
1929 VEC_safe_push (tree
, heap
, avail_exprs_stack
,
1930 stmt
? stmt
: element
->rhs
);
1934 /* Extract the LHS of the assignment so that it can be used as the current
1935 definition of another variable. */
1936 lhs
= ((struct expr_hash_elt
*)*slot
)->lhs
;
1938 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
1939 use the value from the const_and_copies table. */
1940 if (TREE_CODE (lhs
) == SSA_NAME
)
1942 temp
= SSA_NAME_VALUE (lhs
);
1943 if (temp
&& TREE_CODE (temp
) != VALUE_HANDLE
)
1951 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
1952 MODIFY_EXPR statements. We compute a value number for expressions using
1953 the code of the expression and the SSA numbers of its operands. */
1956 avail_expr_hash (const void *p
)
1958 tree stmt
= ((struct expr_hash_elt
*)p
)->stmt
;
1959 tree rhs
= ((struct expr_hash_elt
*)p
)->rhs
;
1964 /* iterative_hash_expr knows how to deal with any expression and
1965 deals with commutative operators as well, so just use it instead
1966 of duplicating such complexities here. */
1967 val
= iterative_hash_expr (rhs
, val
);
1969 /* If the hash table entry is not associated with a statement, then we
1970 can just hash the expression and not worry about virtual operands
1972 if (!stmt
|| !stmt_ann (stmt
))
1975 /* Add the SSA version numbers of every vuse operand. This is important
1976 because compound variables like arrays are not renamed in the
1977 operands. Rather, the rename is done on the virtual variable
1978 representing all the elements of the array. */
1979 FOR_EACH_SSA_TREE_OPERAND (vuse
, stmt
, iter
, SSA_OP_VUSE
)
1980 val
= iterative_hash_expr (vuse
, val
);
1986 real_avail_expr_hash (const void *p
)
1988 return ((const struct expr_hash_elt
*)p
)->hash
;
1992 avail_expr_eq (const void *p1
, const void *p2
)
1994 tree stmt1
= ((struct expr_hash_elt
*)p1
)->stmt
;
1995 tree rhs1
= ((struct expr_hash_elt
*)p1
)->rhs
;
1996 tree stmt2
= ((struct expr_hash_elt
*)p2
)->stmt
;
1997 tree rhs2
= ((struct expr_hash_elt
*)p2
)->rhs
;
1999 /* If they are the same physical expression, return true. */
2000 if (rhs1
== rhs2
&& stmt1
== stmt2
)
2003 /* If their codes are not equal, then quit now. */
2004 if (TREE_CODE (rhs1
) != TREE_CODE (rhs2
))
2007 /* In case of a collision, both RHS have to be identical and have the
2008 same VUSE operands. */
2009 if ((TREE_TYPE (rhs1
) == TREE_TYPE (rhs2
)
2010 || lang_hooks
.types_compatible_p (TREE_TYPE (rhs1
), TREE_TYPE (rhs2
)))
2011 && operand_equal_p (rhs1
, rhs2
, OEP_PURE_SAME
))
2013 bool ret
= compare_ssa_operands_equal (stmt1
, stmt2
, SSA_OP_VUSE
);
2014 gcc_assert (!ret
|| ((struct expr_hash_elt
*)p1
)->hash
2015 == ((struct expr_hash_elt
*)p2
)->hash
);