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