2012-10-15 Paolo Carlini <paolo.carlini@oracle.com>
[gcc.git] / gcc / tree-ssa-uncprop.c
1 /* Routines for discovering and unpropagating edge equivalences.
2 Copyright (C) 2005, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "tree-flow.h"
31 #include "domwalk.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
34
35 /* The basic structure describing an equivalency created by traversing
36 an edge. Traversing the edge effectively means that we can assume
37 that we've seen an assignment LHS = RHS. */
38 struct edge_equivalency
39 {
40 tree rhs;
41 tree lhs;
42 };
43
44 /* This routine finds and records edge equivalences for every edge
45 in the CFG.
46
47 When complete, each edge that creates an equivalency will have an
48 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
49 The caller is responsible for freeing the AUX fields. */
50
51 static void
52 associate_equivalences_with_edges (void)
53 {
54 basic_block bb;
55
56 /* Walk over each block. If the block ends with a control statement,
57 then it might create a useful equivalence. */
58 FOR_EACH_BB (bb)
59 {
60 gimple_stmt_iterator gsi = gsi_last_bb (bb);
61 gimple stmt;
62
63 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
64 then there is nothing to do. */
65 if (gsi_end_p (gsi))
66 continue;
67
68 stmt = gsi_stmt (gsi);
69
70 if (!stmt)
71 continue;
72
73 /* A COND_EXPR may create an equivalency in a variety of different
74 ways. */
75 if (gimple_code (stmt) == GIMPLE_COND)
76 {
77 edge true_edge;
78 edge false_edge;
79 struct edge_equivalency *equivalency;
80 enum tree_code code = gimple_cond_code (stmt);
81
82 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
83
84 /* Equality tests may create one or two equivalences. */
85 if (code == EQ_EXPR || code == NE_EXPR)
86 {
87 tree op0 = gimple_cond_lhs (stmt);
88 tree op1 = gimple_cond_rhs (stmt);
89
90 /* Special case comparing booleans against a constant as we
91 know the value of OP0 on both arms of the branch. i.e., we
92 can record an equivalence for OP0 rather than COND. */
93 if (TREE_CODE (op0) == SSA_NAME
94 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
95 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
96 && is_gimple_min_invariant (op1))
97 {
98 if (code == EQ_EXPR)
99 {
100 equivalency = XNEW (struct edge_equivalency);
101 equivalency->lhs = op0;
102 equivalency->rhs = (integer_zerop (op1)
103 ? boolean_false_node
104 : boolean_true_node);
105 true_edge->aux = equivalency;
106
107 equivalency = XNEW (struct edge_equivalency);
108 equivalency->lhs = op0;
109 equivalency->rhs = (integer_zerop (op1)
110 ? boolean_true_node
111 : boolean_false_node);
112 false_edge->aux = equivalency;
113 }
114 else
115 {
116 equivalency = XNEW (struct edge_equivalency);
117 equivalency->lhs = op0;
118 equivalency->rhs = (integer_zerop (op1)
119 ? boolean_true_node
120 : boolean_false_node);
121 true_edge->aux = equivalency;
122
123 equivalency = XNEW (struct edge_equivalency);
124 equivalency->lhs = op0;
125 equivalency->rhs = (integer_zerop (op1)
126 ? boolean_false_node
127 : boolean_true_node);
128 false_edge->aux = equivalency;
129 }
130 }
131
132 else if (TREE_CODE (op0) == SSA_NAME
133 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
134 && (is_gimple_min_invariant (op1)
135 || (TREE_CODE (op1) == SSA_NAME
136 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
137 {
138 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
139 the sign of a variable compared against zero. If
140 we're honoring signed zeros, then we cannot record
141 this value unless we know that the value is nonzero. */
142 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
143 && (TREE_CODE (op1) != REAL_CST
144 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
145 continue;
146
147 equivalency = XNEW (struct edge_equivalency);
148 equivalency->lhs = op0;
149 equivalency->rhs = op1;
150 if (code == EQ_EXPR)
151 true_edge->aux = equivalency;
152 else
153 false_edge->aux = equivalency;
154
155 }
156 }
157
158 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
159 }
160
161 /* For a SWITCH_EXPR, a case label which represents a single
162 value and which is the only case label which reaches the
163 target block creates an equivalence. */
164 else if (gimple_code (stmt) == GIMPLE_SWITCH)
165 {
166 tree cond = gimple_switch_index (stmt);
167
168 if (TREE_CODE (cond) == SSA_NAME
169 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
170 {
171 int i, n_labels = gimple_switch_num_labels (stmt);
172 tree *info = XCNEWVEC (tree, last_basic_block);
173
174 /* Walk over the case label vector. Record blocks
175 which are reached by a single case label which represents
176 a single value. */
177 for (i = 0; i < n_labels; i++)
178 {
179 tree label = gimple_switch_label (stmt, i);
180 basic_block bb = label_to_block (CASE_LABEL (label));
181
182 if (CASE_HIGH (label)
183 || !CASE_LOW (label)
184 || info[bb->index])
185 info[bb->index] = error_mark_node;
186 else
187 info[bb->index] = label;
188 }
189
190 /* Now walk over the blocks to determine which ones were
191 marked as being reached by a useful case label. */
192 for (i = 0; i < n_basic_blocks; i++)
193 {
194 tree node = info[i];
195
196 if (node != NULL
197 && node != error_mark_node)
198 {
199 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
200 struct edge_equivalency *equivalency;
201
202 /* Record an equivalency on the edge from BB to basic
203 block I. */
204 equivalency = XNEW (struct edge_equivalency);
205 equivalency->rhs = x;
206 equivalency->lhs = cond;
207 find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
208 }
209 }
210 free (info);
211 }
212 }
213
214 }
215 }
216
217
218 /* Translating out of SSA sometimes requires inserting copies and
219 constant initializations on edges to eliminate PHI nodes.
220
221 In some cases those copies and constant initializations are
222 redundant because the target already has the value on the
223 RHS of the assignment.
224
225 We previously tried to catch these cases after translating
226 out of SSA form. However, that code often missed cases. Worse
227 yet, the cases it missed were also often missed by the RTL
228 optimizers. Thus the resulting code had redundant instructions.
229
230 This pass attempts to detect these situations before translating
231 out of SSA form.
232
233 The key concept that this pass is built upon is that these
234 redundant copies and constant initializations often occur
235 due to constant/copy propagating equivalences resulting from
236 COND_EXPRs and SWITCH_EXPRs.
237
238 We want to do those propagations as they can sometimes allow
239 the SSA optimizers to do a better job. However, in the cases
240 where such propagations do not result in further optimization,
241 we would like to "undo" the propagation to avoid the redundant
242 copies and constant initializations.
243
244 This pass works by first associating equivalences with edges in
245 the CFG. For example, the edge leading from a SWITCH_EXPR to
246 its associated CASE_LABEL will have an equivalency between
247 SWITCH_COND and the value in the case label.
248
249 Once we have found the edge equivalences, we proceed to walk
250 the CFG in dominator order. As we traverse edges we record
251 equivalences associated with those edges we traverse.
252
253 When we encounter a PHI node, we walk its arguments to see if we
254 have an equivalence for the PHI argument. If so, then we replace
255 the argument.
256
257 Equivalences are looked up based on their value (think of it as
258 the RHS of an assignment). A value may be an SSA_NAME or an
259 invariant. We may have several SSA_NAMEs with the same value,
260 so with each value we have a list of SSA_NAMEs that have the
261 same value. */
262
263 /* As we enter each block we record the value for any edge equivalency
264 leading to this block. If no such edge equivalency exists, then we
265 record NULL. These equivalences are live until we leave the dominator
266 subtree rooted at the block where we record the equivalency. */
267 static VEC(tree,heap) *equiv_stack;
268
269 /* Global hash table implementing a mapping from invariant values
270 to a list of SSA_NAMEs which have the same value. We might be
271 able to reuse tree-vn for this code. */
272 static htab_t equiv;
273
274 /* Main structure for recording equivalences into our hash table. */
275 struct equiv_hash_elt
276 {
277 /* The value/key of this entry. */
278 tree value;
279
280 /* List of SSA_NAMEs which have the same value/key. */
281 VEC(tree,heap) *equivalences;
282 };
283
284 static void uncprop_enter_block (struct dom_walk_data *, basic_block);
285 static void uncprop_leave_block (struct dom_walk_data *, basic_block);
286 static void uncprop_into_successor_phis (basic_block);
287
288 /* Hashing and equality routines for the hash table. */
289
290 static hashval_t
291 equiv_hash (const void *p)
292 {
293 tree const value = ((const struct equiv_hash_elt *)p)->value;
294 return iterative_hash_expr (value, 0);
295 }
296
297 static int
298 equiv_eq (const void *p1, const void *p2)
299 {
300 tree value1 = ((const struct equiv_hash_elt *)p1)->value;
301 tree value2 = ((const struct equiv_hash_elt *)p2)->value;
302
303 return operand_equal_p (value1, value2, 0);
304 }
305
306 /* Free an instance of equiv_hash_elt. */
307
308 static void
309 equiv_free (void *p)
310 {
311 struct equiv_hash_elt *elt = (struct equiv_hash_elt *) p;
312 VEC_free (tree, heap, elt->equivalences);
313 free (elt);
314 }
315
316 /* Remove the most recently recorded equivalency for VALUE. */
317
318 static void
319 remove_equivalence (tree value)
320 {
321 struct equiv_hash_elt equiv_hash_elt, *equiv_hash_elt_p;
322 void **slot;
323
324 equiv_hash_elt.value = value;
325 equiv_hash_elt.equivalences = NULL;
326
327 slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
328
329 equiv_hash_elt_p = (struct equiv_hash_elt *) *slot;
330 VEC_pop (tree, equiv_hash_elt_p->equivalences);
331 }
332
333 /* Record EQUIVALENCE = VALUE into our hash table. */
334
335 static void
336 record_equiv (tree value, tree equivalence)
337 {
338 struct equiv_hash_elt *equiv_hash_elt;
339 void **slot;
340
341 equiv_hash_elt = XNEW (struct equiv_hash_elt);
342 equiv_hash_elt->value = value;
343 equiv_hash_elt->equivalences = NULL;
344
345 slot = htab_find_slot (equiv, equiv_hash_elt, INSERT);
346
347 if (*slot == NULL)
348 *slot = (void *) equiv_hash_elt;
349 else
350 free (equiv_hash_elt);
351
352 equiv_hash_elt = (struct equiv_hash_elt *) *slot;
353
354 VEC_safe_push (tree, heap, equiv_hash_elt->equivalences, equivalence);
355 }
356
357 /* Main driver for un-cprop. */
358
359 static unsigned int
360 tree_ssa_uncprop (void)
361 {
362 struct dom_walk_data walk_data;
363 basic_block bb;
364
365 associate_equivalences_with_edges ();
366
367 /* Create our global data structures. */
368 equiv = htab_create (1024, equiv_hash, equiv_eq, equiv_free);
369 equiv_stack = VEC_alloc (tree, heap, 2);
370
371 /* We're going to do a dominator walk, so ensure that we have
372 dominance information. */
373 calculate_dominance_info (CDI_DOMINATORS);
374
375 /* Setup callbacks for the generic dominator tree walker. */
376 walk_data.dom_direction = CDI_DOMINATORS;
377 walk_data.initialize_block_local_data = NULL;
378 walk_data.before_dom_children = uncprop_enter_block;
379 walk_data.after_dom_children = uncprop_leave_block;
380 walk_data.global_data = NULL;
381 walk_data.block_local_data_size = 0;
382
383 /* Now initialize the dominator walker. */
384 init_walk_dominator_tree (&walk_data);
385
386 /* Recursively walk the dominator tree undoing unprofitable
387 constant/copy propagations. */
388 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
389
390 /* Finalize and clean up. */
391 fini_walk_dominator_tree (&walk_data);
392
393 /* EQUIV_STACK should already be empty at this point, so we just
394 need to empty elements out of the hash table, free EQUIV_STACK,
395 and cleanup the AUX field on the edges. */
396 htab_delete (equiv);
397 VEC_free (tree, heap, equiv_stack);
398 FOR_EACH_BB (bb)
399 {
400 edge e;
401 edge_iterator ei;
402
403 FOR_EACH_EDGE (e, ei, bb->succs)
404 {
405 if (e->aux)
406 {
407 free (e->aux);
408 e->aux = NULL;
409 }
410 }
411 }
412 return 0;
413 }
414
415
416 /* We have finished processing the dominator children of BB, perform
417 any finalization actions in preparation for leaving this node in
418 the dominator tree. */
419
420 static void
421 uncprop_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
422 basic_block bb ATTRIBUTE_UNUSED)
423 {
424 /* Pop the topmost value off the equiv stack. */
425 tree value = VEC_pop (tree, equiv_stack);
426
427 /* If that value was non-null, then pop the topmost equivalency off
428 its equivalency stack. */
429 if (value != NULL)
430 remove_equivalence (value);
431 }
432
433 /* Unpropagate values from PHI nodes in successor blocks of BB. */
434
435 static void
436 uncprop_into_successor_phis (basic_block bb)
437 {
438 edge e;
439 edge_iterator ei;
440
441 /* For each successor edge, first temporarily record any equivalence
442 on that edge. Then unpropagate values in any PHI nodes at the
443 destination of the edge. Then remove the temporary equivalence. */
444 FOR_EACH_EDGE (e, ei, bb->succs)
445 {
446 gimple_seq phis = phi_nodes (e->dest);
447 gimple_stmt_iterator gsi;
448
449 /* If there are no PHI nodes in this destination, then there is
450 no sense in recording any equivalences. */
451 if (gimple_seq_empty_p (phis))
452 continue;
453
454 /* Record any equivalency associated with E. */
455 if (e->aux)
456 {
457 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
458 record_equiv (equiv->rhs, equiv->lhs);
459 }
460
461 /* Walk over the PHI nodes, unpropagating values. */
462 for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
463 {
464 gimple phi = gsi_stmt (gsi);
465 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
466 tree res = PHI_RESULT (phi);
467 struct equiv_hash_elt equiv_hash_elt;
468 void **slot;
469
470 /* If the argument is not an invariant, and refers to the same
471 underlying variable as the PHI result, then there's no
472 point in un-propagating the argument. */
473 if (!is_gimple_min_invariant (arg)
474 && (SSA_NAME_VAR (arg) == SSA_NAME_VAR (res)
475 && TREE_TYPE (arg) == TREE_TYPE (res)))
476 continue;
477
478 /* Lookup this argument's value in the hash table. */
479 equiv_hash_elt.value = arg;
480 equiv_hash_elt.equivalences = NULL;
481 slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
482
483 if (slot)
484 {
485 struct equiv_hash_elt *elt = (struct equiv_hash_elt *) *slot;
486 int j;
487
488 /* Walk every equivalence with the same value. If we find
489 one with the same underlying variable as the PHI result,
490 then replace the value in the argument with its equivalent
491 SSA_NAME. Use the most recent equivalence as hopefully
492 that results in shortest lifetimes. */
493 for (j = VEC_length (tree, elt->equivalences) - 1; j >= 0; j--)
494 {
495 tree equiv = VEC_index (tree, elt->equivalences, j);
496
497 if (SSA_NAME_VAR (equiv) == SSA_NAME_VAR (res)
498 && TREE_TYPE (equiv) == TREE_TYPE (res))
499 {
500 SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
501 break;
502 }
503 }
504 }
505 }
506
507 /* If we had an equivalence associated with this edge, remove it. */
508 if (e->aux)
509 {
510 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
511 remove_equivalence (equiv->rhs);
512 }
513 }
514 }
515
516 /* Ignoring loop backedges, if BB has precisely one incoming edge then
517 return that edge. Otherwise return NULL. */
518 static edge
519 single_incoming_edge_ignoring_loop_edges (basic_block bb)
520 {
521 edge retval = NULL;
522 edge e;
523 edge_iterator ei;
524
525 FOR_EACH_EDGE (e, ei, bb->preds)
526 {
527 /* A loop back edge can be identified by the destination of
528 the edge dominating the source of the edge. */
529 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
530 continue;
531
532 /* If we have already seen a non-loop edge, then we must have
533 multiple incoming non-loop edges and thus we return NULL. */
534 if (retval)
535 return NULL;
536
537 /* This is the first non-loop incoming edge we have found. Record
538 it. */
539 retval = e;
540 }
541
542 return retval;
543 }
544
545 static void
546 uncprop_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
547 basic_block bb)
548 {
549 basic_block parent;
550 edge e;
551 bool recorded = false;
552
553 /* If this block is dominated by a single incoming edge and that edge
554 has an equivalency, then record the equivalency and push the
555 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
556 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
557 if (parent)
558 {
559 e = single_incoming_edge_ignoring_loop_edges (bb);
560
561 if (e && e->src == parent && e->aux)
562 {
563 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
564
565 record_equiv (equiv->rhs, equiv->lhs);
566 VEC_safe_push (tree, heap, equiv_stack, equiv->rhs);
567 recorded = true;
568 }
569 }
570
571 if (!recorded)
572 VEC_safe_push (tree, heap, equiv_stack, NULL_TREE);
573
574 uncprop_into_successor_phis (bb);
575 }
576
577 static bool
578 gate_uncprop (void)
579 {
580 return flag_tree_dom != 0;
581 }
582
583 struct gimple_opt_pass pass_uncprop =
584 {
585 {
586 GIMPLE_PASS,
587 "uncprop", /* name */
588 gate_uncprop, /* gate */
589 tree_ssa_uncprop, /* execute */
590 NULL, /* sub */
591 NULL, /* next */
592 0, /* static_pass_number */
593 TV_TREE_SSA_UNCPROP, /* tv_id */
594 PROP_cfg | PROP_ssa, /* properties_required */
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
598 TODO_verify_ssa /* todo_flags_finish */
599 }
600 };