1 /* Routines for discovering and unpropagating edge equivalences.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "basic-block.h"
33 #include "diagnostic.h"
35 #include "tree-dump.h"
36 #include "tree-flow.h"
39 #include "tree-pass.h"
40 #include "tree-ssa-propagate.h"
41 #include "langhooks.h"
43 /* The basic structure describing an equivalency created by traversing
44 an edge. Traversing the edge effectively means that we can assume
45 that we've seen an assignment LHS = RHS. */
46 struct edge_equivalency
52 /* This routine finds and records edge equivalences for every edge
55 When complete, each edge that creates an equivalency will have an
56 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
57 The caller is responsible for freeing the AUX fields. */
60 associate_equivalences_with_edges (void)
64 /* Walk over each block. If the block ends with a control statement,
65 then it might create a useful equivalence. */
68 block_stmt_iterator bsi
= bsi_last (bb
);
71 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
72 then there is nothing to do. */
76 stmt
= bsi_stmt (bsi
);
81 /* A COND_EXPR may create an equivalency in a variety of different
83 if (TREE_CODE (stmt
) == COND_EXPR
)
85 tree cond
= COND_EXPR_COND (stmt
);
88 struct edge_equivalency
*equivalency
;
90 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
92 /* If the conditional is a single variable 'X', record 'X = 1'
93 for the true edge and 'X = 0' on the false edge. */
94 if (TREE_CODE (cond
) == SSA_NAME
95 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond
))
97 equivalency
= XNEW (struct edge_equivalency
);
98 equivalency
->rhs
= constant_boolean_node (1, TREE_TYPE (cond
));
99 equivalency
->lhs
= cond
;
100 true_edge
->aux
= equivalency
;
102 equivalency
= XNEW (struct edge_equivalency
);
103 equivalency
->rhs
= constant_boolean_node (0, TREE_TYPE (cond
));
104 equivalency
->lhs
= cond
;
105 false_edge
->aux
= equivalency
;
107 /* Equality tests may create one or two equivalences. */
108 else if (TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
110 tree op0
= TREE_OPERAND (cond
, 0);
111 tree op1
= TREE_OPERAND (cond
, 1);
113 /* Special case comparing booleans against a constant as we
114 know the value of OP0 on both arms of the branch. i.e., we
115 can record an equivalence for OP0 rather than COND. */
116 if (TREE_CODE (op0
) == SSA_NAME
117 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
)
118 && TREE_CODE (TREE_TYPE (op0
)) == BOOLEAN_TYPE
119 && is_gimple_min_invariant (op1
))
121 if (TREE_CODE (cond
) == EQ_EXPR
)
123 equivalency
= XNEW (struct edge_equivalency
);
124 equivalency
->lhs
= op0
;
125 equivalency
->rhs
= (integer_zerop (op1
)
127 : boolean_true_node
);
128 true_edge
->aux
= equivalency
;
130 equivalency
= XNEW (struct edge_equivalency
);
131 equivalency
->lhs
= op0
;
132 equivalency
->rhs
= (integer_zerop (op1
)
134 : boolean_false_node
);
135 false_edge
->aux
= equivalency
;
139 equivalency
= XNEW (struct edge_equivalency
);
140 equivalency
->lhs
= op0
;
141 equivalency
->rhs
= (integer_zerop (op1
)
143 : boolean_false_node
);
144 true_edge
->aux
= equivalency
;
146 equivalency
= XNEW (struct edge_equivalency
);
147 equivalency
->lhs
= op0
;
148 equivalency
->rhs
= (integer_zerop (op1
)
150 : boolean_true_node
);
151 false_edge
->aux
= equivalency
;
155 if (TREE_CODE (op0
) == SSA_NAME
156 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
)
157 && (is_gimple_min_invariant (op1
)
158 || (TREE_CODE (op1
) == SSA_NAME
159 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1
))))
161 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
162 the sign of a variable compared against zero. If
163 we're honoring signed zeros, then we cannot record
164 this value unless we know that the value is nonzero. */
165 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0
)))
166 && (TREE_CODE (op1
) != REAL_CST
167 || REAL_VALUES_EQUAL (dconst0
, TREE_REAL_CST (op1
))))
170 equivalency
= XNEW (struct edge_equivalency
);
171 equivalency
->lhs
= op0
;
172 equivalency
->rhs
= op1
;
173 if (TREE_CODE (cond
) == EQ_EXPR
)
174 true_edge
->aux
= equivalency
;
176 false_edge
->aux
= equivalency
;
181 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
184 /* For a SWITCH_EXPR, a case label which represents a single
185 value and which is the only case label which reaches the
186 target block creates an equivalence. */
187 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
189 tree cond
= SWITCH_COND (stmt
);
191 if (TREE_CODE (cond
) == SSA_NAME
192 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond
))
194 tree labels
= SWITCH_LABELS (stmt
);
195 int i
, n_labels
= TREE_VEC_LENGTH (labels
);
196 tree
*info
= XCNEWVEC (tree
, n_basic_blocks
);
198 /* Walk over the case label vector. Record blocks
199 which are reached by a single case label which represents
201 for (i
= 0; i
< n_labels
; i
++)
203 tree label
= TREE_VEC_ELT (labels
, i
);
204 basic_block bb
= label_to_block (CASE_LABEL (label
));
207 if (CASE_HIGH (label
)
210 info
[bb
->index
] = error_mark_node
;
212 info
[bb
->index
] = label
;
215 /* Now walk over the blocks to determine which ones were
216 marked as being reached by a useful case label. */
217 for (i
= 0; i
< n_basic_blocks
; i
++)
222 && node
!= error_mark_node
)
224 tree x
= fold_convert (TREE_TYPE (cond
), CASE_LOW (node
));
225 struct edge_equivalency
*equivalency
;
227 /* Record an equivalency on the edge from BB to basic
229 equivalency
= XNEW (struct edge_equivalency
);
230 equivalency
->rhs
= x
;
231 equivalency
->lhs
= cond
;
232 find_edge (bb
, BASIC_BLOCK (i
))->aux
= equivalency
;
243 /* Translating out of SSA sometimes requires inserting copies and
244 constant initializations on edges to eliminate PHI nodes.
246 In some cases those copies and constant initializations are
247 redundant because the target already has the value on the
248 RHS of the assignment.
250 We previously tried to catch these cases after translating
251 out of SSA form. However, that code often missed cases. Worse
252 yet, the cases it missed were also often missed by the RTL
253 optimizers. Thus the resulting code had redundant instructions.
255 This pass attempts to detect these situations before translating
258 The key concept that this pass is built upon is that these
259 redundant copies and constant initializations often occur
260 due to constant/copy propagating equivalences resulting from
261 COND_EXPRs and SWITCH_EXPRs.
263 We want to do those propagations as they can sometimes allow
264 the SSA optimizers to do a better job. However, in the cases
265 where such propagations do not result in further optimization,
266 we would like to "undo" the propagation to avoid the redundant
267 copies and constant initializations.
269 This pass works by first associating equivalences with edges in
270 the CFG. For example, the edge leading from a SWITCH_EXPR to
271 its associated CASE_LABEL will have an equivalency between
272 SWITCH_COND and the value in the case label.
274 Once we have found the edge equivalences, we proceed to walk
275 the CFG in dominator order. As we traverse edges we record
276 equivalences associated with those edges we traverse.
278 When we encounter a PHI node, we walk its arguments to see if we
279 have an equivalence for the PHI argument. If so, then we replace
282 Equivalences are looked up based on their value (think of it as
283 the RHS of an assignment). A value may be an SSA_NAME or an
284 invariant. We may have several SSA_NAMEs with the same value,
285 so with each value we have a list of SSA_NAMEs that have the
288 /* As we enter each block we record the value for any edge equivalency
289 leading to this block. If no such edge equivalency exists, then we
290 record NULL. These equivalences are live until we leave the dominator
291 subtree rooted at the block where we record the equivalency. */
292 static VEC(tree
,heap
) *equiv_stack
;
294 /* Global hash table implementing a mapping from invariant values
295 to a list of SSA_NAMEs which have the same value. We might be
296 able to reuse tree-vn for this code. */
299 /* Main structure for recording equivalences into our hash table. */
300 struct equiv_hash_elt
302 /* The value/key of this entry. */
305 /* List of SSA_NAMEs which have the same value/key. */
306 VEC(tree
,heap
) *equivalences
;
309 static void uncprop_initialize_block (struct dom_walk_data
*, basic_block
);
310 static void uncprop_finalize_block (struct dom_walk_data
*, basic_block
);
311 static void uncprop_into_successor_phis (struct dom_walk_data
*, basic_block
);
313 /* Hashing and equality routines for the hash table. */
316 equiv_hash (const void *p
)
318 tree
const value
= ((const struct equiv_hash_elt
*)p
)->value
;
319 return iterative_hash_expr (value
, 0);
323 equiv_eq (const void *p1
, const void *p2
)
325 tree value1
= ((const struct equiv_hash_elt
*)p1
)->value
;
326 tree value2
= ((const struct equiv_hash_elt
*)p2
)->value
;
328 return operand_equal_p (value1
, value2
, 0);
331 /* Free an instance of equiv_hash_elt. */
336 struct equiv_hash_elt
*elt
= (struct equiv_hash_elt
*) p
;
337 VEC_free (tree
, heap
, elt
->equivalences
);
341 /* Remove the most recently recorded equivalency for VALUE. */
344 remove_equivalence (tree value
)
346 struct equiv_hash_elt equiv_hash_elt
, *equiv_hash_elt_p
;
349 equiv_hash_elt
.value
= value
;
350 equiv_hash_elt
.equivalences
= NULL
;
352 slot
= htab_find_slot (equiv
, &equiv_hash_elt
, NO_INSERT
);
354 equiv_hash_elt_p
= (struct equiv_hash_elt
*) *slot
;
355 VEC_pop (tree
, equiv_hash_elt_p
->equivalences
);
358 /* Record EQUIVALENCE = VALUE into our hash table. */
361 record_equiv (tree value
, tree equivalence
)
363 struct equiv_hash_elt
*equiv_hash_elt
;
366 equiv_hash_elt
= XNEW (struct equiv_hash_elt
);
367 equiv_hash_elt
->value
= value
;
368 equiv_hash_elt
->equivalences
= NULL
;
370 slot
= htab_find_slot (equiv
, equiv_hash_elt
, INSERT
);
373 *slot
= (void *) equiv_hash_elt
;
375 free (equiv_hash_elt
);
377 equiv_hash_elt
= (struct equiv_hash_elt
*) *slot
;
379 VEC_safe_push (tree
, heap
, equiv_hash_elt
->equivalences
, equivalence
);
382 /* Main driver for un-cprop. */
385 tree_ssa_uncprop (void)
387 struct dom_walk_data walk_data
;
390 associate_equivalences_with_edges ();
392 /* Create our global data structures. */
393 equiv
= htab_create (1024, equiv_hash
, equiv_eq
, equiv_free
);
394 equiv_stack
= VEC_alloc (tree
, heap
, 2);
396 /* We're going to do a dominator walk, so ensure that we have
397 dominance information. */
398 calculate_dominance_info (CDI_DOMINATORS
);
400 /* Setup callbacks for the generic dominator tree walker. */
401 walk_data
.walk_stmts_backward
= false;
402 walk_data
.dom_direction
= CDI_DOMINATORS
;
403 walk_data
.initialize_block_local_data
= NULL
;
404 walk_data
.before_dom_children_before_stmts
= uncprop_initialize_block
;
405 walk_data
.before_dom_children_walk_stmts
= NULL
;
406 walk_data
.before_dom_children_after_stmts
= uncprop_into_successor_phis
;
407 walk_data
.after_dom_children_before_stmts
= NULL
;
408 walk_data
.after_dom_children_walk_stmts
= NULL
;
409 walk_data
.after_dom_children_after_stmts
= uncprop_finalize_block
;
410 walk_data
.global_data
= NULL
;
411 walk_data
.block_local_data_size
= 0;
412 walk_data
.interesting_blocks
= NULL
;
414 /* Now initialize the dominator walker. */
415 init_walk_dominator_tree (&walk_data
);
417 /* Recursively walk the dominator tree undoing unprofitable
418 constant/copy propagations. */
419 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
421 /* Finalize and clean up. */
422 fini_walk_dominator_tree (&walk_data
);
424 /* EQUIV_STACK should already be empty at this point, so we just
425 need to empty elements out of the hash table, free EQUIV_STACK,
426 and cleanup the AUX field on the edges. */
428 VEC_free (tree
, heap
, equiv_stack
);
434 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
447 /* We have finished processing the dominator children of BB, perform
448 any finalization actions in preparation for leaving this node in
449 the dominator tree. */
452 uncprop_finalize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
453 basic_block bb ATTRIBUTE_UNUSED
)
455 /* Pop the topmost value off the equiv stack. */
456 tree value
= VEC_pop (tree
, equiv_stack
);
458 /* If that value was non-null, then pop the topmost equivalency off
459 its equivalency stack. */
461 remove_equivalence (value
);
464 /* Unpropagate values from PHI nodes in successor blocks of BB. */
467 uncprop_into_successor_phis (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
473 /* For each successor edge, first temporarily record any equivalence
474 on that edge. Then unpropagate values in any PHI nodes at the
475 destination of the edge. Then remove the temporary equivalence. */
476 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
478 tree phi
= phi_nodes (e
->dest
);
480 /* If there are no PHI nodes in this destination, then there is
481 no sense in recording any equivalences. */
485 /* Record any equivalency associated with E. */
488 struct edge_equivalency
*equiv
= (struct edge_equivalency
*) e
->aux
;
489 record_equiv (equiv
->rhs
, equiv
->lhs
);
492 /* Walk over the PHI nodes, unpropagating values. */
493 for ( ; phi
; phi
= PHI_CHAIN (phi
))
495 /* Sigh. We'll have more efficient access to this one day. */
496 tree arg
= PHI_ARG_DEF (phi
, e
->dest_idx
);
497 struct equiv_hash_elt equiv_hash_elt
;
500 /* If the argument is not an invariant, or refers to the same
501 underlying variable as the PHI result, then there's no
502 point in un-propagating the argument. */
503 if (!is_gimple_min_invariant (arg
)
504 && SSA_NAME_VAR (arg
) != SSA_NAME_VAR (PHI_RESULT (phi
)))
507 /* Lookup this argument's value in the hash table. */
508 equiv_hash_elt
.value
= arg
;
509 equiv_hash_elt
.equivalences
= NULL
;
510 slot
= htab_find_slot (equiv
, &equiv_hash_elt
, NO_INSERT
);
514 struct equiv_hash_elt
*elt
= (struct equiv_hash_elt
*) *slot
;
517 /* Walk every equivalence with the same value. If we find
518 one with the same underlying variable as the PHI result,
519 then replace the value in the argument with its equivalent
520 SSA_NAME. Use the most recent equivalence as hopefully
521 that results in shortest lifetimes. */
522 for (j
= VEC_length (tree
, elt
->equivalences
) - 1; j
>= 0; j
--)
524 tree equiv
= VEC_index (tree
, elt
->equivalences
, j
);
526 if (SSA_NAME_VAR (equiv
) == SSA_NAME_VAR (PHI_RESULT (phi
)))
528 SET_PHI_ARG_DEF (phi
, e
->dest_idx
, equiv
);
535 /* If we had an equivalence associated with this edge, remove it. */
538 struct edge_equivalency
*equiv
= (struct edge_equivalency
*) e
->aux
;
539 remove_equivalence (equiv
->rhs
);
544 /* Ignoring loop backedges, if BB has precisely one incoming edge then
545 return that edge. Otherwise return NULL. */
547 single_incoming_edge_ignoring_loop_edges (basic_block bb
)
553 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
555 /* A loop back edge can be identified by the destination of
556 the edge dominating the source of the edge. */
557 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
560 /* If we have already seen a non-loop edge, then we must have
561 multiple incoming non-loop edges and thus we return NULL. */
565 /* This is the first non-loop incoming edge we have found. Record
574 uncprop_initialize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
579 bool recorded
= false;
581 /* If this block is dominated by a single incoming edge and that edge
582 has an equivalency, then record the equivalency and push the
583 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
584 parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
587 e
= single_incoming_edge_ignoring_loop_edges (bb
);
589 if (e
&& e
->src
== parent
&& e
->aux
)
591 struct edge_equivalency
*equiv
= (struct edge_equivalency
*) e
->aux
;
593 record_equiv (equiv
->rhs
, equiv
->lhs
);
594 VEC_safe_push (tree
, heap
, equiv_stack
, equiv
->rhs
);
600 VEC_safe_push (tree
, heap
, equiv_stack
, NULL_TREE
);
606 return flag_tree_dom
!= 0;
609 struct tree_opt_pass pass_uncprop
=
611 "uncprop", /* name */
612 gate_uncprop
, /* gate */
613 tree_ssa_uncprop
, /* execute */
616 0, /* static_pass_number */
617 TV_TREE_SSA_UNCPROP
, /* tv_id */
618 PROP_cfg
| PROP_ssa
, /* properties_required */
619 0, /* properties_provided */
620 0, /* properties_destroyed */
621 0, /* todo_flags_start */
622 TODO_dump_func
| TODO_verify_ssa
, /* todo_flags_finish */