]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-uncprop.c
configure.ac: Check for support of __atomic extensions.
[thirdparty/gcc.git] / gcc / tree-ssa-uncprop.c
CommitLineData
fef0657c 1/* Routines for discovering and unpropagating edge equivalences.
d1e082c2 2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
fef0657c
JL
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
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
fef0657c
JL
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
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
fef0657c
JL
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"
fef0657c 26#include "tm_p.h"
fef0657c 27#include "basic-block.h"
fef0657c 28#include "function.h"
442b4905 29#include "gimple.h"
5be5c238 30#include "gimple-iterator.h"
442b4905
AM
31#include "gimple-ssa.h"
32#include "tree-cfg.h"
33#include "tree-phinodes.h"
34#include "ssa-iterators.h"
fef0657c 35#include "domwalk.h"
fef0657c
JL
36#include "tree-pass.h"
37#include "tree-ssa-propagate.h"
fef0657c
JL
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
b8698a0f 52 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
fef0657c
JL
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 {
726a989a
RB
64 gimple_stmt_iterator gsi = gsi_last_bb (bb);
65 gimple stmt;
fef0657c
JL
66
67 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
68 then there is nothing to do. */
726a989a 69 if (gsi_end_p (gsi))
fef0657c
JL
70 continue;
71
726a989a 72 stmt = gsi_stmt (gsi);
fef0657c
JL
73
74 if (!stmt)
75 continue;
76
77 /* A COND_EXPR may create an equivalency in a variety of different
78 ways. */
726a989a 79 if (gimple_code (stmt) == GIMPLE_COND)
fef0657c 80 {
fef0657c
JL
81 edge true_edge;
82 edge false_edge;
83 struct edge_equivalency *equivalency;
726a989a 84 enum tree_code code = gimple_cond_code (stmt);
fef0657c
JL
85
86 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
87
fef0657c 88 /* Equality tests may create one or two equivalences. */
726a989a 89 if (code == EQ_EXPR || code == NE_EXPR)
fef0657c 90 {
726a989a
RB
91 tree op0 = gimple_cond_lhs (stmt);
92 tree op1 = gimple_cond_rhs (stmt);
fef0657c
JL
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
224b4faf 98 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
fef0657c
JL
99 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
100 && is_gimple_min_invariant (op1))
101 {
726a989a 102 if (code == EQ_EXPR)
fef0657c 103 {
e1111e8e 104 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
105 equivalency->lhs = op0;
106 equivalency->rhs = (integer_zerop (op1)
107 ? boolean_false_node
108 : boolean_true_node);
109 true_edge->aux = equivalency;
110
e1111e8e 111 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
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 {
e1111e8e 120 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
121 equivalency->lhs = op0;
122 equivalency->rhs = (integer_zerop (op1)
123 ? boolean_true_node
124 : boolean_false_node);
125 true_edge->aux = equivalency;
126
e1111e8e 127 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
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
e49a540c
RG
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))))
fef0657c
JL
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
e1111e8e 151 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
152 equivalency->lhs = op0;
153 equivalency->rhs = op1;
726a989a 154 if (code == EQ_EXPR)
fef0657c 155 true_edge->aux = equivalency;
b8698a0f 156 else
fef0657c
JL
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. */
726a989a 168 else if (gimple_code (stmt) == GIMPLE_SWITCH)
fef0657c 169 {
726a989a 170 tree cond = gimple_switch_index (stmt);
fef0657c 171
224b4faf
JL
172 if (TREE_CODE (cond) == SSA_NAME
173 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
fef0657c 174 {
726a989a 175 int i, n_labels = gimple_switch_num_labels (stmt);
1ff7d06b 176 tree *info = XCNEWVEC (tree, last_basic_block);
fef0657c
JL
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 {
726a989a 183 tree label = gimple_switch_label (stmt, i);
fef0657c
JL
184 basic_block bb = label_to_block (CASE_LABEL (label));
185
fef0657c
JL
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. */
196 for (i = 0; i < n_basic_blocks; i++)
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. */
e1111e8e 208 equivalency = XNEW (struct edge_equivalency);
fef0657c
JL
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
f0e4ea10 243 the SSA optimizers to do a better job. However, in the cases
fef0657c
JL
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
fef0657c 267
fef0657c
JL
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. */
9771b263 275 vec<tree> equivalences;
fef0657c
JL
276};
277
4a8fb1a1 278/* Value to ssa name equivalence hashtable helpers. */
fef0657c 279
4a8fb1a1
LC
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};
fef0657c 288
4a8fb1a1
LC
289inline hashval_t
290val_ssa_equiv_hasher::hash (const value_type *p)
fef0657c 291{
4a8fb1a1 292 tree const value = p->value;
fef0657c
JL
293 return iterative_hash_expr (value, 0);
294}
295
4a8fb1a1
LC
296inline bool
297val_ssa_equiv_hasher::equal (const value_type *p1, const compare_type *p2)
fef0657c 298{
4a8fb1a1
LC
299 tree value1 = p1->value;
300 tree value2 = p2->value;
fef0657c
JL
301
302 return operand_equal_p (value1, value2, 0);
303}
304
075a0d54
KH
305/* Free an instance of equiv_hash_elt. */
306
4a8fb1a1
LC
307inline void
308val_ssa_equiv_hasher::remove (value_type *elt)
075a0d54 309{
9771b263 310 elt->equivalences.release ();
075a0d54
KH
311 free (elt);
312}
313
4a8fb1a1
LC
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
4a8fb1a1
LC
319static void uncprop_into_successor_phis (basic_block);
320
fef0657c
JL
321/* Remove the most recently recorded equivalency for VALUE. */
322
323static void
324remove_equivalence (tree value)
325{
4a8fb1a1
LC
326 struct equiv_hash_elt an_equiv_elt, *an_equiv_elt_p;
327 equiv_hash_elt **slot;
fef0657c 328
4a8fb1a1
LC
329 an_equiv_elt.value = value;
330 an_equiv_elt.equivalences.create (0);
fef0657c 331
4a8fb1a1 332 slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
fef0657c 333
4a8fb1a1
LC
334 an_equiv_elt_p = *slot;
335 an_equiv_elt_p->equivalences.pop ();
fef0657c
JL
336}
337
338/* Record EQUIVALENCE = VALUE into our hash table. */
339
340static void
341record_equiv (tree value, tree equivalence)
342{
4a8fb1a1
LC
343 equiv_hash_elt *an_equiv_elt_p;
344 equiv_hash_elt **slot;
fef0657c 345
4a8fb1a1
LC
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);
fef0657c 349
4a8fb1a1 350 slot = val_ssa_equiv.find_slot (an_equiv_elt_p, INSERT);
fef0657c
JL
351
352 if (*slot == NULL)
4a8fb1a1 353 *slot = an_equiv_elt_p;
fef0657c 354 else
4a8fb1a1 355 free (an_equiv_elt_p);
fef0657c 356
4a8fb1a1 357 an_equiv_elt_p = *slot;
b8698a0f 358
4a8fb1a1 359 an_equiv_elt_p->equivalences.safe_push (equivalence);
fef0657c
JL
360}
361
4d9192b5
TS
362class uncprop_dom_walker : public dom_walker
363{
364public:
07687835 365 uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
4d9192b5
TS
366
367 virtual void before_dom_children (basic_block);
368 virtual void after_dom_children (basic_block);
369
370private:
371
65d3284b
RS
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. */
07687835 376 stack_vec<tree, 2> m_equiv_stack;
4d9192b5
TS
377};
378
fef0657c
JL
379/* Main driver for un-cprop. */
380
c2924966 381static unsigned int
fef0657c
JL
382tree_ssa_uncprop (void)
383{
fef0657c
JL
384 basic_block bb;
385
386 associate_equivalences_with_edges ();
387
388 /* Create our global data structures. */
4a8fb1a1 389 val_ssa_equiv.create (1024);
fef0657c
JL
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
fef0657c
JL
395 /* Recursively walk the dominator tree undoing unprofitable
396 constant/copy propagations. */
4d9192b5 397 uncprop_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
fef0657c 398
4d9192b5
TS
399 /* we just need to empty elements out of the hash table, and cleanup the
400 AUX field on the edges. */
4a8fb1a1 401 val_ssa_equiv.dispose ();
fef0657c
JL
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 }
c2924966 416 return 0;
fef0657c
JL
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
4d9192b5
TS
424void
425uncprop_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
fef0657c 426{
fef0657c 427 /* Pop the topmost value off the equiv stack. */
65d3284b 428 tree value = m_equiv_stack.pop ();
fef0657c
JL
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
ccf5c864 439uncprop_into_successor_phis (basic_block bb)
fef0657c
JL
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 {
726a989a
RB
449 gimple_seq phis = phi_nodes (e->dest);
450 gimple_stmt_iterator gsi;
fef0657c
JL
451
452 /* If there are no PHI nodes in this destination, then there is
453 no sense in recording any equivalences. */
8eacd016 454 if (gimple_seq_empty_p (phis))
fef0657c
JL
455 continue;
456
457 /* Record any equivalency associated with E. */
458 if (e->aux)
459 {
e1111e8e 460 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
fef0657c
JL
461 record_equiv (equiv->rhs, equiv->lhs);
462 }
463
464 /* Walk over the PHI nodes, unpropagating values. */
726a989a 465 for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
fef0657c 466 {
726a989a 467 gimple phi = gsi_stmt (gsi);
fef0657c 468 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
70b5e7dc 469 tree res = PHI_RESULT (phi);
4a8fb1a1
LC
470 equiv_hash_elt an_equiv_elt;
471 equiv_hash_elt **slot;
fef0657c 472
e91d0adb
JL
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. */
fef0657c 476 if (!is_gimple_min_invariant (arg)
e91d0adb 477 && gimple_can_coalesce_p (arg, res))
fef0657c
JL
478 continue;
479
480 /* Lookup this argument's value in the hash table. */
4a8fb1a1
LC
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);
fef0657c
JL
484
485 if (slot)
486 {
4a8fb1a1 487 struct equiv_hash_elt *elt = *slot;
fef0657c
JL
488 int j;
489
490 /* Walk every equivalence with the same value. If we find
e91d0adb 491 one that can potentially coalesce with the PHI rsult,
fef0657c 492 then replace the value in the argument with its equivalent
f0e4ea10 493 SSA_NAME. Use the most recent equivalence as hopefully
fef0657c 494 that results in shortest lifetimes. */
9771b263 495 for (j = elt->equivalences.length () - 1; j >= 0; j--)
fef0657c 496 {
9771b263 497 tree equiv = elt->equivalences[j];
fef0657c 498
e91d0adb 499 if (gimple_can_coalesce_p (equiv, res))
fef0657c
JL
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 {
e1111e8e 511 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
fef0657c
JL
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
4d9192b5
TS
546void
547uncprop_dom_walker::before_dom_children (basic_block bb)
fef0657c
JL
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 {
e1111e8e 563 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
fef0657c
JL
564
565 record_equiv (equiv->rhs, equiv->lhs);
65d3284b 566 m_equiv_stack.safe_push (equiv->rhs);
fef0657c
JL
567 recorded = true;
568 }
569 }
570
571 if (!recorded)
65d3284b 572 m_equiv_stack.safe_push (NULL_TREE);
ccf5c864
PB
573
574 uncprop_into_successor_phis (bb);
fef0657c
JL
575}
576
577static bool
578gate_uncprop (void)
579{
580 return flag_tree_dom != 0;
581}
582
27a4cd48
DM
583namespace {
584
585const pass_data pass_data_uncprop =
fef0657c 586{
27a4cd48
DM
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 */
fef0657c 598};
27a4cd48
DM
599
600class pass_uncprop : public gimple_opt_pass
601{
602public:
c3284718
RS
603 pass_uncprop (gcc::context *ctxt)
604 : gimple_opt_pass (pass_data_uncprop, ctxt)
27a4cd48
DM
605 {}
606
607 /* opt_pass methods: */
65d3284b 608 opt_pass * clone () { return new pass_uncprop (m_ctxt); }
27a4cd48
DM
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}