* config/msp430/msp430.c (msp430_asm_integer): Support addition
[official-gcc.git] / gcc / tree-ssa-dom.c
blob7f7d70de596bcd030e8b35b5ee2d095490f5b6eb
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hash-table.h"
25 #include "tm.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stor-layout.h"
35 #include "flags.h"
36 #include "tm_p.h"
37 #include "predict.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "dominance.h"
42 #include "cfg.h"
43 #include "cfganal.h"
44 #include "basic-block.h"
45 #include "cfgloop.h"
46 #include "inchash.h"
47 #include "gimple-pretty-print.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-fold.h"
51 #include "tree-eh.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "gimple-ssa.h"
57 #include "tree-cfg.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-into-ssa.h"
63 #include "domwalk.h"
64 #include "tree-pass.h"
65 #include "tree-ssa-propagate.h"
66 #include "tree-ssa-threadupdate.h"
67 #include "langhooks.h"
68 #include "params.h"
69 #include "tree-ssa-scopedtables.h"
70 #include "tree-ssa-threadedge.h"
71 #include "tree-ssa-dom.h"
72 #include "inchash.h"
73 #include "gimplify.h"
74 #include "tree-cfgcleanup.h"
76 /* This file implements optimizations on the dominator tree. */
78 /* Representation of a "naked" right-hand-side expression, to be used
79 in recording available expressions in the expression hash table. */
81 enum expr_kind
83 EXPR_SINGLE,
84 EXPR_UNARY,
85 EXPR_BINARY,
86 EXPR_TERNARY,
87 EXPR_CALL,
88 EXPR_PHI
91 struct hashable_expr
93 tree type;
94 enum expr_kind kind;
95 union {
96 struct { tree rhs; } single;
97 struct { enum tree_code op; tree opnd; } unary;
98 struct { enum tree_code op; tree opnd0, opnd1; } binary;
99 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
100 struct { gcall *fn_from; bool pure; size_t nargs; tree *args; } call;
101 struct { size_t nargs; tree *args; } phi;
102 } ops;
105 /* Structure for recording known values of a conditional expression
106 at the exits from its block. */
108 typedef struct cond_equivalence_s
110 struct hashable_expr cond;
111 tree value;
112 } cond_equivalence;
115 /* Structure for recording edge equivalences as well as any pending
116 edge redirections during the dominator optimizer.
118 Computing and storing the edge equivalences instead of creating
119 them on-demand can save significant amounts of time, particularly
120 for pathological cases involving switch statements.
122 These structures live for a single iteration of the dominator
123 optimizer in the edge's AUX field. At the end of an iteration we
124 free each of these structures and update the AUX field to point
125 to any requested redirection target (the code for updating the
126 CFG and SSA graph for edge redirection expects redirection edge
127 targets to be in the AUX field for each edge. */
129 struct edge_info
131 /* If this edge creates a simple equivalence, the LHS and RHS of
132 the equivalence will be stored here. */
133 tree lhs;
134 tree rhs;
136 /* Traversing an edge may also indicate one or more particular conditions
137 are true or false. */
138 vec<cond_equivalence> cond_equivalences;
141 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
142 expressions it enters into the hash table along with a marker entry
143 (null). When we finish processing the block, we pop off entries and
144 remove the expressions from the global hash table until we hit the
145 marker. */
146 typedef struct expr_hash_elt * expr_hash_elt_t;
148 static vec<std::pair<expr_hash_elt_t, expr_hash_elt_t> > avail_exprs_stack;
150 /* Structure for entries in the expression hash table. */
152 struct expr_hash_elt
154 /* The value (lhs) of this expression. */
155 tree lhs;
157 /* The expression (rhs) we want to record. */
158 struct hashable_expr expr;
160 /* The virtual operand associated with the nearest dominating stmt
161 loading from or storing to expr. */
162 tree vop;
164 /* The hash value for RHS. */
165 hashval_t hash;
167 /* A unique stamp, typically the address of the hash
168 element itself, used in removing entries from the table. */
169 struct expr_hash_elt *stamp;
172 /* Hashtable helpers. */
174 static bool hashable_expr_equal_p (const struct hashable_expr *,
175 const struct hashable_expr *);
176 static void free_expr_hash_elt (void *);
178 struct expr_elt_hasher
180 typedef expr_hash_elt *value_type;
181 typedef expr_hash_elt *compare_type;
182 static inline hashval_t hash (const value_type &);
183 static inline bool equal (const value_type &, const compare_type &);
184 static inline void remove (value_type &);
187 inline hashval_t
188 expr_elt_hasher::hash (const value_type &p)
190 return p->hash;
193 inline bool
194 expr_elt_hasher::equal (const value_type &p1, const compare_type &p2)
196 const struct hashable_expr *expr1 = &p1->expr;
197 const struct expr_hash_elt *stamp1 = p1->stamp;
198 const struct hashable_expr *expr2 = &p2->expr;
199 const struct expr_hash_elt *stamp2 = p2->stamp;
201 /* This case should apply only when removing entries from the table. */
202 if (stamp1 == stamp2)
203 return true;
205 if (p1->hash != p2->hash)
206 return false;
208 /* In case of a collision, both RHS have to be identical and have the
209 same VUSE operands. */
210 if (hashable_expr_equal_p (expr1, expr2)
211 && types_compatible_p (expr1->type, expr2->type))
212 return true;
214 return false;
217 /* Delete an expr_hash_elt and reclaim its storage. */
219 inline void
220 expr_elt_hasher::remove (value_type &element)
222 free_expr_hash_elt (element);
225 /* Hash table with expressions made available during the renaming process.
226 When an assignment of the form X_i = EXPR is found, the statement is
227 stored in this table. If the same expression EXPR is later found on the
228 RHS of another statement, it is replaced with X_i (thus performing
229 global redundancy elimination). Similarly as we pass through conditionals
230 we record the conditional itself as having either a true or false value
231 in this table. */
232 static hash_table<expr_elt_hasher> *avail_exprs;
234 /* Unwindable const/copy equivalences. */
235 static const_and_copies *const_and_copies;
237 /* Track whether or not we have changed the control flow graph. */
238 static bool cfg_altered;
240 /* Bitmap of blocks that have had EH statements cleaned. We should
241 remove their dead edges eventually. */
242 static bitmap need_eh_cleanup;
243 static vec<gimple> need_noreturn_fixup;
245 /* Statistics for dominator optimizations. */
246 struct opt_stats_d
248 long num_stmts;
249 long num_exprs_considered;
250 long num_re;
251 long num_const_prop;
252 long num_copy_prop;
255 static struct opt_stats_d opt_stats;
257 /* Local functions. */
258 static void optimize_stmt (basic_block, gimple_stmt_iterator);
259 static tree lookup_avail_expr (gimple, bool);
260 static hashval_t avail_expr_hash (const void *);
261 static void htab_statistics (FILE *,
262 const hash_table<expr_elt_hasher> &);
263 static void record_cond (cond_equivalence *);
264 static void record_equality (tree, tree);
265 static void record_equivalences_from_phis (basic_block);
266 static void record_equivalences_from_incoming_edge (basic_block);
267 static void eliminate_redundant_computations (gimple_stmt_iterator *);
268 static void record_equivalences_from_stmt (gimple, int);
269 static void remove_local_expressions_from_table (void);
270 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
273 /* Given a statement STMT, initialize the hash table element pointed to
274 by ELEMENT. */
276 static void
277 initialize_hash_element (gimple stmt, tree lhs,
278 struct expr_hash_elt *element)
280 enum gimple_code code = gimple_code (stmt);
281 struct hashable_expr *expr = &element->expr;
283 if (code == GIMPLE_ASSIGN)
285 enum tree_code subcode = gimple_assign_rhs_code (stmt);
287 switch (get_gimple_rhs_class (subcode))
289 case GIMPLE_SINGLE_RHS:
290 expr->kind = EXPR_SINGLE;
291 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
292 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
293 break;
294 case GIMPLE_UNARY_RHS:
295 expr->kind = EXPR_UNARY;
296 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
297 if (CONVERT_EXPR_CODE_P (subcode))
298 subcode = NOP_EXPR;
299 expr->ops.unary.op = subcode;
300 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
301 break;
302 case GIMPLE_BINARY_RHS:
303 expr->kind = EXPR_BINARY;
304 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
305 expr->ops.binary.op = subcode;
306 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
307 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
308 break;
309 case GIMPLE_TERNARY_RHS:
310 expr->kind = EXPR_TERNARY;
311 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
312 expr->ops.ternary.op = subcode;
313 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
314 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
315 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
316 break;
317 default:
318 gcc_unreachable ();
321 else if (code == GIMPLE_COND)
323 expr->type = boolean_type_node;
324 expr->kind = EXPR_BINARY;
325 expr->ops.binary.op = gimple_cond_code (stmt);
326 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
327 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
329 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
331 size_t nargs = gimple_call_num_args (call_stmt);
332 size_t i;
334 gcc_assert (gimple_call_lhs (call_stmt));
336 expr->type = TREE_TYPE (gimple_call_lhs (call_stmt));
337 expr->kind = EXPR_CALL;
338 expr->ops.call.fn_from = call_stmt;
340 if (gimple_call_flags (call_stmt) & (ECF_CONST | ECF_PURE))
341 expr->ops.call.pure = true;
342 else
343 expr->ops.call.pure = false;
345 expr->ops.call.nargs = nargs;
346 expr->ops.call.args = XCNEWVEC (tree, nargs);
347 for (i = 0; i < nargs; i++)
348 expr->ops.call.args[i] = gimple_call_arg (call_stmt, i);
350 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
352 expr->type = TREE_TYPE (gimple_switch_index (swtch_stmt));
353 expr->kind = EXPR_SINGLE;
354 expr->ops.single.rhs = gimple_switch_index (swtch_stmt);
356 else if (code == GIMPLE_GOTO)
358 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
359 expr->kind = EXPR_SINGLE;
360 expr->ops.single.rhs = gimple_goto_dest (stmt);
362 else if (code == GIMPLE_PHI)
364 size_t nargs = gimple_phi_num_args (stmt);
365 size_t i;
367 expr->type = TREE_TYPE (gimple_phi_result (stmt));
368 expr->kind = EXPR_PHI;
369 expr->ops.phi.nargs = nargs;
370 expr->ops.phi.args = XCNEWVEC (tree, nargs);
372 for (i = 0; i < nargs; i++)
373 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
375 else
376 gcc_unreachable ();
378 element->lhs = lhs;
379 element->vop = gimple_vuse (stmt);
380 element->hash = avail_expr_hash (element);
381 element->stamp = element;
384 /* Given a conditional expression COND as a tree, initialize
385 a hashable_expr expression EXPR. The conditional must be a
386 comparison or logical negation. A constant or a variable is
387 not permitted. */
389 static void
390 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
392 expr->type = boolean_type_node;
394 if (COMPARISON_CLASS_P (cond))
396 expr->kind = EXPR_BINARY;
397 expr->ops.binary.op = TREE_CODE (cond);
398 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
399 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
401 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
403 expr->kind = EXPR_UNARY;
404 expr->ops.unary.op = TRUTH_NOT_EXPR;
405 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
407 else
408 gcc_unreachable ();
411 /* Given a hashable_expr expression EXPR and an LHS,
412 initialize the hash table element pointed to by ELEMENT. */
414 static void
415 initialize_hash_element_from_expr (struct hashable_expr *expr,
416 tree lhs,
417 struct expr_hash_elt *element)
419 element->expr = *expr;
420 element->lhs = lhs;
421 element->vop = NULL_TREE;
422 element->hash = avail_expr_hash (element);
423 element->stamp = element;
426 /* Compare two hashable_expr structures for equivalence.
427 They are considered equivalent when the the expressions
428 they denote must necessarily be equal. The logic is intended
429 to follow that of operand_equal_p in fold-const.c */
431 static bool
432 hashable_expr_equal_p (const struct hashable_expr *expr0,
433 const struct hashable_expr *expr1)
435 tree type0 = expr0->type;
436 tree type1 = expr1->type;
438 /* If either type is NULL, there is nothing to check. */
439 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
440 return false;
442 /* If both types don't have the same signedness, precision, and mode,
443 then we can't consider them equal. */
444 if (type0 != type1
445 && (TREE_CODE (type0) == ERROR_MARK
446 || TREE_CODE (type1) == ERROR_MARK
447 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
448 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
449 || TYPE_MODE (type0) != TYPE_MODE (type1)))
450 return false;
452 if (expr0->kind != expr1->kind)
453 return false;
455 switch (expr0->kind)
457 case EXPR_SINGLE:
458 return operand_equal_p (expr0->ops.single.rhs,
459 expr1->ops.single.rhs, 0);
461 case EXPR_UNARY:
462 if (expr0->ops.unary.op != expr1->ops.unary.op)
463 return false;
465 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
466 || expr0->ops.unary.op == NON_LVALUE_EXPR)
467 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
468 return false;
470 return operand_equal_p (expr0->ops.unary.opnd,
471 expr1->ops.unary.opnd, 0);
473 case EXPR_BINARY:
474 if (expr0->ops.binary.op != expr1->ops.binary.op)
475 return false;
477 if (operand_equal_p (expr0->ops.binary.opnd0,
478 expr1->ops.binary.opnd0, 0)
479 && operand_equal_p (expr0->ops.binary.opnd1,
480 expr1->ops.binary.opnd1, 0))
481 return true;
483 /* For commutative ops, allow the other order. */
484 return (commutative_tree_code (expr0->ops.binary.op)
485 && operand_equal_p (expr0->ops.binary.opnd0,
486 expr1->ops.binary.opnd1, 0)
487 && operand_equal_p (expr0->ops.binary.opnd1,
488 expr1->ops.binary.opnd0, 0));
490 case EXPR_TERNARY:
491 if (expr0->ops.ternary.op != expr1->ops.ternary.op
492 || !operand_equal_p (expr0->ops.ternary.opnd2,
493 expr1->ops.ternary.opnd2, 0))
494 return false;
496 if (operand_equal_p (expr0->ops.ternary.opnd0,
497 expr1->ops.ternary.opnd0, 0)
498 && operand_equal_p (expr0->ops.ternary.opnd1,
499 expr1->ops.ternary.opnd1, 0))
500 return true;
502 /* For commutative ops, allow the other order. */
503 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
504 && operand_equal_p (expr0->ops.ternary.opnd0,
505 expr1->ops.ternary.opnd1, 0)
506 && operand_equal_p (expr0->ops.ternary.opnd1,
507 expr1->ops.ternary.opnd0, 0));
509 case EXPR_CALL:
511 size_t i;
513 /* If the calls are to different functions, then they
514 clearly cannot be equal. */
515 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
516 expr1->ops.call.fn_from))
517 return false;
519 if (! expr0->ops.call.pure)
520 return false;
522 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
523 return false;
525 for (i = 0; i < expr0->ops.call.nargs; i++)
526 if (! operand_equal_p (expr0->ops.call.args[i],
527 expr1->ops.call.args[i], 0))
528 return false;
530 if (stmt_could_throw_p (expr0->ops.call.fn_from))
532 int lp0 = lookup_stmt_eh_lp (expr0->ops.call.fn_from);
533 int lp1 = lookup_stmt_eh_lp (expr1->ops.call.fn_from);
534 if ((lp0 > 0 || lp1 > 0) && lp0 != lp1)
535 return false;
538 return true;
541 case EXPR_PHI:
543 size_t i;
545 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
546 return false;
548 for (i = 0; i < expr0->ops.phi.nargs; i++)
549 if (! operand_equal_p (expr0->ops.phi.args[i],
550 expr1->ops.phi.args[i], 0))
551 return false;
553 return true;
556 default:
557 gcc_unreachable ();
561 /* Generate a hash value for a pair of expressions. This can be used
562 iteratively by passing a previous result in HSTATE.
564 The same hash value is always returned for a given pair of expressions,
565 regardless of the order in which they are presented. This is useful in
566 hashing the operands of commutative functions. */
568 namespace inchash
571 static void
572 add_expr_commutative (const_tree t1, const_tree t2, hash &hstate)
574 hash one, two;
576 inchash::add_expr (t1, one);
577 inchash::add_expr (t2, two);
578 hstate.add_commutative (one, two);
581 /* Compute a hash value for a hashable_expr value EXPR and a
582 previously accumulated hash value VAL. If two hashable_expr
583 values compare equal with hashable_expr_equal_p, they must
584 hash to the same value, given an identical value of VAL.
585 The logic is intended to follow inchash::add_expr in tree.c. */
587 static void
588 add_hashable_expr (const struct hashable_expr *expr, hash &hstate)
590 switch (expr->kind)
592 case EXPR_SINGLE:
593 inchash::add_expr (expr->ops.single.rhs, hstate);
594 break;
596 case EXPR_UNARY:
597 hstate.add_object (expr->ops.unary.op);
599 /* Make sure to include signedness in the hash computation.
600 Don't hash the type, that can lead to having nodes which
601 compare equal according to operand_equal_p, but which
602 have different hash codes. */
603 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
604 || expr->ops.unary.op == NON_LVALUE_EXPR)
605 hstate.add_int (TYPE_UNSIGNED (expr->type));
607 inchash::add_expr (expr->ops.unary.opnd, hstate);
608 break;
610 case EXPR_BINARY:
611 hstate.add_object (expr->ops.binary.op);
612 if (commutative_tree_code (expr->ops.binary.op))
613 inchash::add_expr_commutative (expr->ops.binary.opnd0,
614 expr->ops.binary.opnd1, hstate);
615 else
617 inchash::add_expr (expr->ops.binary.opnd0, hstate);
618 inchash::add_expr (expr->ops.binary.opnd1, hstate);
620 break;
622 case EXPR_TERNARY:
623 hstate.add_object (expr->ops.ternary.op);
624 if (commutative_ternary_tree_code (expr->ops.ternary.op))
625 inchash::add_expr_commutative (expr->ops.ternary.opnd0,
626 expr->ops.ternary.opnd1, hstate);
627 else
629 inchash::add_expr (expr->ops.ternary.opnd0, hstate);
630 inchash::add_expr (expr->ops.ternary.opnd1, hstate);
632 inchash::add_expr (expr->ops.ternary.opnd2, hstate);
633 break;
635 case EXPR_CALL:
637 size_t i;
638 enum tree_code code = CALL_EXPR;
639 gcall *fn_from;
641 hstate.add_object (code);
642 fn_from = expr->ops.call.fn_from;
643 if (gimple_call_internal_p (fn_from))
644 hstate.merge_hash ((hashval_t) gimple_call_internal_fn (fn_from));
645 else
646 inchash::add_expr (gimple_call_fn (fn_from), hstate);
647 for (i = 0; i < expr->ops.call.nargs; i++)
648 inchash::add_expr (expr->ops.call.args[i], hstate);
650 break;
652 case EXPR_PHI:
654 size_t i;
656 for (i = 0; i < expr->ops.phi.nargs; i++)
657 inchash::add_expr (expr->ops.phi.args[i], hstate);
659 break;
661 default:
662 gcc_unreachable ();
668 /* Print a diagnostic dump of an expression hash table entry. */
670 static void
671 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
673 fprintf (stream, "STMT ");
675 if (element->lhs)
677 print_generic_expr (stream, element->lhs, 0);
678 fprintf (stream, " = ");
681 switch (element->expr.kind)
683 case EXPR_SINGLE:
684 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
685 break;
687 case EXPR_UNARY:
688 fprintf (stream, "%s ", get_tree_code_name (element->expr.ops.unary.op));
689 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
690 break;
692 case EXPR_BINARY:
693 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
694 fprintf (stream, " %s ", get_tree_code_name (element->expr.ops.binary.op));
695 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
696 break;
698 case EXPR_TERNARY:
699 fprintf (stream, " %s <", get_tree_code_name (element->expr.ops.ternary.op));
700 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
701 fputs (", ", stream);
702 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
703 fputs (", ", stream);
704 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
705 fputs (">", stream);
706 break;
708 case EXPR_CALL:
710 size_t i;
711 size_t nargs = element->expr.ops.call.nargs;
712 gcall *fn_from;
714 fn_from = element->expr.ops.call.fn_from;
715 if (gimple_call_internal_p (fn_from))
716 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
717 stream);
718 else
719 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
720 fprintf (stream, " (");
721 for (i = 0; i < nargs; i++)
723 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
724 if (i + 1 < nargs)
725 fprintf (stream, ", ");
727 fprintf (stream, ")");
729 break;
731 case EXPR_PHI:
733 size_t i;
734 size_t nargs = element->expr.ops.phi.nargs;
736 fprintf (stream, "PHI <");
737 for (i = 0; i < nargs; i++)
739 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
740 if (i + 1 < nargs)
741 fprintf (stream, ", ");
743 fprintf (stream, ">");
745 break;
748 if (element->vop)
750 fprintf (stream, " with ");
751 print_generic_expr (stream, element->vop, 0);
754 fprintf (stream, "\n");
757 /* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
759 static void
760 free_expr_hash_elt_contents (struct expr_hash_elt *element)
762 if (element->expr.kind == EXPR_CALL)
763 free (element->expr.ops.call.args);
764 else if (element->expr.kind == EXPR_PHI)
765 free (element->expr.ops.phi.args);
768 /* Delete an expr_hash_elt and reclaim its storage. */
770 static void
771 free_expr_hash_elt (void *elt)
773 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
774 free_expr_hash_elt_contents (element);
775 free (element);
778 /* Allocate an EDGE_INFO for edge E and attach it to E.
779 Return the new EDGE_INFO structure. */
781 static struct edge_info *
782 allocate_edge_info (edge e)
784 struct edge_info *edge_info;
786 edge_info = XCNEW (struct edge_info);
788 e->aux = edge_info;
789 return edge_info;
792 /* Free all EDGE_INFO structures associated with edges in the CFG.
793 If a particular edge can be threaded, copy the redirection
794 target from the EDGE_INFO structure into the edge's AUX field
795 as required by code to update the CFG and SSA graph for
796 jump threading. */
798 static void
799 free_all_edge_infos (void)
801 basic_block bb;
802 edge_iterator ei;
803 edge e;
805 FOR_EACH_BB_FN (bb, cfun)
807 FOR_EACH_EDGE (e, ei, bb->preds)
809 struct edge_info *edge_info = (struct edge_info *) e->aux;
811 if (edge_info)
813 edge_info->cond_equivalences.release ();
814 free (edge_info);
815 e->aux = NULL;
821 /* Build a cond_equivalence record indicating that the comparison
822 CODE holds between operands OP0 and OP1 and push it to **P. */
824 static void
825 build_and_record_new_cond (enum tree_code code,
826 tree op0, tree op1,
827 vec<cond_equivalence> *p)
829 cond_equivalence c;
830 struct hashable_expr *cond = &c.cond;
832 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
834 cond->type = boolean_type_node;
835 cond->kind = EXPR_BINARY;
836 cond->ops.binary.op = code;
837 cond->ops.binary.opnd0 = op0;
838 cond->ops.binary.opnd1 = op1;
840 c.value = boolean_true_node;
841 p->safe_push (c);
844 /* Record that COND is true and INVERTED is false into the edge information
845 structure. Also record that any conditions dominated by COND are true
846 as well.
848 For example, if a < b is true, then a <= b must also be true. */
850 static void
851 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
853 tree op0, op1;
854 cond_equivalence c;
856 if (!COMPARISON_CLASS_P (cond))
857 return;
859 op0 = TREE_OPERAND (cond, 0);
860 op1 = TREE_OPERAND (cond, 1);
862 switch (TREE_CODE (cond))
864 case LT_EXPR:
865 case GT_EXPR:
866 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
868 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
869 &edge_info->cond_equivalences);
870 build_and_record_new_cond (LTGT_EXPR, op0, op1,
871 &edge_info->cond_equivalences);
874 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
875 ? LE_EXPR : GE_EXPR),
876 op0, op1, &edge_info->cond_equivalences);
877 build_and_record_new_cond (NE_EXPR, op0, op1,
878 &edge_info->cond_equivalences);
879 break;
881 case GE_EXPR:
882 case LE_EXPR:
883 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
885 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
886 &edge_info->cond_equivalences);
888 break;
890 case EQ_EXPR:
891 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
893 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
894 &edge_info->cond_equivalences);
896 build_and_record_new_cond (LE_EXPR, op0, op1,
897 &edge_info->cond_equivalences);
898 build_and_record_new_cond (GE_EXPR, op0, op1,
899 &edge_info->cond_equivalences);
900 break;
902 case UNORDERED_EXPR:
903 build_and_record_new_cond (NE_EXPR, op0, op1,
904 &edge_info->cond_equivalences);
905 build_and_record_new_cond (UNLE_EXPR, op0, op1,
906 &edge_info->cond_equivalences);
907 build_and_record_new_cond (UNGE_EXPR, op0, op1,
908 &edge_info->cond_equivalences);
909 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
910 &edge_info->cond_equivalences);
911 build_and_record_new_cond (UNLT_EXPR, op0, op1,
912 &edge_info->cond_equivalences);
913 build_and_record_new_cond (UNGT_EXPR, op0, op1,
914 &edge_info->cond_equivalences);
915 break;
917 case UNLT_EXPR:
918 case UNGT_EXPR:
919 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
920 ? UNLE_EXPR : UNGE_EXPR),
921 op0, op1, &edge_info->cond_equivalences);
922 build_and_record_new_cond (NE_EXPR, op0, op1,
923 &edge_info->cond_equivalences);
924 break;
926 case UNEQ_EXPR:
927 build_and_record_new_cond (UNLE_EXPR, op0, op1,
928 &edge_info->cond_equivalences);
929 build_and_record_new_cond (UNGE_EXPR, op0, op1,
930 &edge_info->cond_equivalences);
931 break;
933 case LTGT_EXPR:
934 build_and_record_new_cond (NE_EXPR, op0, op1,
935 &edge_info->cond_equivalences);
936 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
937 &edge_info->cond_equivalences);
938 break;
940 default:
941 break;
944 /* Now store the original true and false conditions into the first
945 two slots. */
946 initialize_expr_from_cond (cond, &c.cond);
947 c.value = boolean_true_node;
948 edge_info->cond_equivalences.safe_push (c);
950 /* It is possible for INVERTED to be the negation of a comparison,
951 and not a valid RHS or GIMPLE_COND condition. This happens because
952 invert_truthvalue may return such an expression when asked to invert
953 a floating-point comparison. These comparisons are not assumed to
954 obey the trichotomy law. */
955 initialize_expr_from_cond (inverted, &c.cond);
956 c.value = boolean_false_node;
957 edge_info->cond_equivalences.safe_push (c);
960 /* We have finished optimizing BB, record any information implied by
961 taking a specific outgoing edge from BB. */
963 static void
964 record_edge_info (basic_block bb)
966 gimple_stmt_iterator gsi = gsi_last_bb (bb);
967 struct edge_info *edge_info;
969 if (! gsi_end_p (gsi))
971 gimple stmt = gsi_stmt (gsi);
972 location_t loc = gimple_location (stmt);
974 if (gimple_code (stmt) == GIMPLE_SWITCH)
976 gswitch *switch_stmt = as_a <gswitch *> (stmt);
977 tree index = gimple_switch_index (switch_stmt);
979 if (TREE_CODE (index) == SSA_NAME)
981 int i;
982 int n_labels = gimple_switch_num_labels (switch_stmt);
983 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
984 edge e;
985 edge_iterator ei;
987 for (i = 0; i < n_labels; i++)
989 tree label = gimple_switch_label (switch_stmt, i);
990 basic_block target_bb = label_to_block (CASE_LABEL (label));
991 if (CASE_HIGH (label)
992 || !CASE_LOW (label)
993 || info[target_bb->index])
994 info[target_bb->index] = error_mark_node;
995 else
996 info[target_bb->index] = label;
999 FOR_EACH_EDGE (e, ei, bb->succs)
1001 basic_block target_bb = e->dest;
1002 tree label = info[target_bb->index];
1004 if (label != NULL && label != error_mark_node)
1006 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1007 CASE_LOW (label));
1008 edge_info = allocate_edge_info (e);
1009 edge_info->lhs = index;
1010 edge_info->rhs = x;
1013 free (info);
1017 /* A COND_EXPR may create equivalences too. */
1018 if (gimple_code (stmt) == GIMPLE_COND)
1020 edge true_edge;
1021 edge false_edge;
1023 tree op0 = gimple_cond_lhs (stmt);
1024 tree op1 = gimple_cond_rhs (stmt);
1025 enum tree_code code = gimple_cond_code (stmt);
1027 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1029 /* Special case comparing booleans against a constant as we
1030 know the value of OP0 on both arms of the branch. i.e., we
1031 can record an equivalence for OP0 rather than COND. */
1032 if ((code == EQ_EXPR || code == NE_EXPR)
1033 && TREE_CODE (op0) == SSA_NAME
1034 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1035 && is_gimple_min_invariant (op1))
1037 if (code == EQ_EXPR)
1039 edge_info = allocate_edge_info (true_edge);
1040 edge_info->lhs = op0;
1041 edge_info->rhs = (integer_zerop (op1)
1042 ? boolean_false_node
1043 : boolean_true_node);
1045 edge_info = allocate_edge_info (false_edge);
1046 edge_info->lhs = op0;
1047 edge_info->rhs = (integer_zerop (op1)
1048 ? boolean_true_node
1049 : boolean_false_node);
1051 else
1053 edge_info = allocate_edge_info (true_edge);
1054 edge_info->lhs = op0;
1055 edge_info->rhs = (integer_zerop (op1)
1056 ? boolean_true_node
1057 : boolean_false_node);
1059 edge_info = allocate_edge_info (false_edge);
1060 edge_info->lhs = op0;
1061 edge_info->rhs = (integer_zerop (op1)
1062 ? boolean_false_node
1063 : boolean_true_node);
1066 else if (is_gimple_min_invariant (op0)
1067 && (TREE_CODE (op1) == SSA_NAME
1068 || is_gimple_min_invariant (op1)))
1070 tree cond = build2 (code, boolean_type_node, op0, op1);
1071 tree inverted = invert_truthvalue_loc (loc, cond);
1072 bool can_infer_simple_equiv
1073 = !(HONOR_SIGNED_ZEROS (op0)
1074 && real_zerop (op0));
1075 struct edge_info *edge_info;
1077 edge_info = allocate_edge_info (true_edge);
1078 record_conditions (edge_info, cond, inverted);
1080 if (can_infer_simple_equiv && code == EQ_EXPR)
1082 edge_info->lhs = op1;
1083 edge_info->rhs = op0;
1086 edge_info = allocate_edge_info (false_edge);
1087 record_conditions (edge_info, inverted, cond);
1089 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1091 edge_info->lhs = op1;
1092 edge_info->rhs = op0;
1096 else if (TREE_CODE (op0) == SSA_NAME
1097 && (TREE_CODE (op1) == SSA_NAME
1098 || is_gimple_min_invariant (op1)))
1100 tree cond = build2 (code, boolean_type_node, op0, op1);
1101 tree inverted = invert_truthvalue_loc (loc, cond);
1102 bool can_infer_simple_equiv
1103 = !(HONOR_SIGNED_ZEROS (op1)
1104 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1105 struct edge_info *edge_info;
1107 edge_info = allocate_edge_info (true_edge);
1108 record_conditions (edge_info, cond, inverted);
1110 if (can_infer_simple_equiv && code == EQ_EXPR)
1112 edge_info->lhs = op0;
1113 edge_info->rhs = op1;
1116 edge_info = allocate_edge_info (false_edge);
1117 record_conditions (edge_info, inverted, cond);
1119 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1121 edge_info->lhs = op0;
1122 edge_info->rhs = op1;
1127 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1132 class dom_opt_dom_walker : public dom_walker
1134 public:
1135 dom_opt_dom_walker (cdi_direction direction)
1136 : dom_walker (direction), m_dummy_cond (NULL) {}
1138 virtual void before_dom_children (basic_block);
1139 virtual void after_dom_children (basic_block);
1141 private:
1142 void thread_across_edge (edge);
1144 gcond *m_dummy_cond;
1147 /* Jump threading, redundancy elimination and const/copy propagation.
1149 This pass may expose new symbols that need to be renamed into SSA. For
1150 every new symbol exposed, its corresponding bit will be set in
1151 VARS_TO_RENAME. */
1153 namespace {
1155 const pass_data pass_data_dominator =
1157 GIMPLE_PASS, /* type */
1158 "dom", /* name */
1159 OPTGROUP_NONE, /* optinfo_flags */
1160 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
1161 ( PROP_cfg | PROP_ssa ), /* properties_required */
1162 0, /* properties_provided */
1163 0, /* properties_destroyed */
1164 0, /* todo_flags_start */
1165 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
1168 class pass_dominator : public gimple_opt_pass
1170 public:
1171 pass_dominator (gcc::context *ctxt)
1172 : gimple_opt_pass (pass_data_dominator, ctxt)
1175 /* opt_pass methods: */
1176 opt_pass * clone () { return new pass_dominator (m_ctxt); }
1177 virtual bool gate (function *) { return flag_tree_dom != 0; }
1178 virtual unsigned int execute (function *);
1180 }; // class pass_dominator
1182 unsigned int
1183 pass_dominator::execute (function *fun)
1185 memset (&opt_stats, 0, sizeof (opt_stats));
1187 /* Create our hash tables. */
1188 avail_exprs = new hash_table<expr_elt_hasher> (1024);
1189 avail_exprs_stack.create (20);
1190 const_and_copies = new class const_and_copies (dump_file, dump_flags);
1191 need_eh_cleanup = BITMAP_ALLOC (NULL);
1192 need_noreturn_fixup.create (0);
1194 calculate_dominance_info (CDI_DOMINATORS);
1195 cfg_altered = false;
1197 /* We need to know loop structures in order to avoid destroying them
1198 in jump threading. Note that we still can e.g. thread through loop
1199 headers to an exit edge, or through loop header to the loop body, assuming
1200 that we update the loop info.
1202 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
1203 to several overly conservative bail-outs in jump threading, case
1204 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
1205 missing. We should improve jump threading in future then
1206 LOOPS_HAVE_PREHEADERS won't be needed here. */
1207 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
1209 /* Initialize the value-handle array. */
1210 threadedge_initialize_values ();
1212 /* We need accurate information regarding back edges in the CFG
1213 for jump threading; this may include back edges that are not part of
1214 a single loop. */
1215 mark_dfs_back_edges ();
1217 /* Recursively walk the dominator tree optimizing statements. */
1218 dom_opt_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
1221 gimple_stmt_iterator gsi;
1222 basic_block bb;
1223 FOR_EACH_BB_FN (bb, fun)
1225 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1226 update_stmt_if_modified (gsi_stmt (gsi));
1230 /* If we exposed any new variables, go ahead and put them into
1231 SSA form now, before we handle jump threading. This simplifies
1232 interactions between rewriting of _DECL nodes into SSA form
1233 and rewriting SSA_NAME nodes into SSA form after block
1234 duplication and CFG manipulation. */
1235 update_ssa (TODO_update_ssa);
1237 free_all_edge_infos ();
1239 /* Thread jumps, creating duplicate blocks as needed. */
1240 cfg_altered |= thread_through_all_blocks (first_pass_instance);
1242 if (cfg_altered)
1243 free_dominance_info (CDI_DOMINATORS);
1245 /* Removal of statements may make some EH edges dead. Purge
1246 such edges from the CFG as needed. */
1247 if (!bitmap_empty_p (need_eh_cleanup))
1249 unsigned i;
1250 bitmap_iterator bi;
1252 /* Jump threading may have created forwarder blocks from blocks
1253 needing EH cleanup; the new successor of these blocks, which
1254 has inherited from the original block, needs the cleanup.
1255 Don't clear bits in the bitmap, as that can break the bitmap
1256 iterator. */
1257 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
1259 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
1260 if (bb == NULL)
1261 continue;
1262 while (single_succ_p (bb)
1263 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
1264 bb = single_succ (bb);
1265 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
1266 continue;
1267 if ((unsigned) bb->index != i)
1268 bitmap_set_bit (need_eh_cleanup, bb->index);
1271 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1272 bitmap_clear (need_eh_cleanup);
1275 /* Fixup stmts that became noreturn calls. This may require splitting
1276 blocks and thus isn't possible during the dominator walk or before
1277 jump threading finished. Do this in reverse order so we don't
1278 inadvertedly remove a stmt we want to fixup by visiting a dominating
1279 now noreturn call first. */
1280 while (!need_noreturn_fixup.is_empty ())
1282 gimple stmt = need_noreturn_fixup.pop ();
1283 if (dump_file && dump_flags & TDF_DETAILS)
1285 fprintf (dump_file, "Fixing up noreturn call ");
1286 print_gimple_stmt (dump_file, stmt, 0, 0);
1287 fprintf (dump_file, "\n");
1289 fixup_noreturn_call (stmt);
1292 statistics_counter_event (fun, "Redundant expressions eliminated",
1293 opt_stats.num_re);
1294 statistics_counter_event (fun, "Constants propagated",
1295 opt_stats.num_const_prop);
1296 statistics_counter_event (fun, "Copies propagated",
1297 opt_stats.num_copy_prop);
1299 /* Debugging dumps. */
1300 if (dump_file && (dump_flags & TDF_STATS))
1301 dump_dominator_optimization_stats (dump_file);
1303 loop_optimizer_finalize ();
1305 /* Delete our main hashtable. */
1306 delete avail_exprs;
1307 avail_exprs = NULL;
1309 /* Free asserted bitmaps and stacks. */
1310 BITMAP_FREE (need_eh_cleanup);
1311 need_noreturn_fixup.release ();
1312 avail_exprs_stack.release ();
1313 delete const_and_copies;
1315 /* Free the value-handle array. */
1316 threadedge_finalize_values ();
1318 return 0;
1321 } // anon namespace
1323 gimple_opt_pass *
1324 make_pass_dominator (gcc::context *ctxt)
1326 return new pass_dominator (ctxt);
1330 /* Given a conditional statement CONDSTMT, convert the
1331 condition to a canonical form. */
1333 static void
1334 canonicalize_comparison (gcond *condstmt)
1336 tree op0;
1337 tree op1;
1338 enum tree_code code;
1340 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
1342 op0 = gimple_cond_lhs (condstmt);
1343 op1 = gimple_cond_rhs (condstmt);
1345 code = gimple_cond_code (condstmt);
1347 /* If it would be profitable to swap the operands, then do so to
1348 canonicalize the statement, enabling better optimization.
1350 By placing canonicalization of such expressions here we
1351 transparently keep statements in canonical form, even
1352 when the statement is modified. */
1353 if (tree_swap_operands_p (op0, op1, false))
1355 /* For relationals we need to swap the operands
1356 and change the code. */
1357 if (code == LT_EXPR
1358 || code == GT_EXPR
1359 || code == LE_EXPR
1360 || code == GE_EXPR)
1362 code = swap_tree_comparison (code);
1364 gimple_cond_set_code (condstmt, code);
1365 gimple_cond_set_lhs (condstmt, op1);
1366 gimple_cond_set_rhs (condstmt, op0);
1368 update_stmt (condstmt);
1373 /* Initialize local stacks for this optimizer and record equivalences
1374 upon entry to BB. Equivalences can come from the edge traversed to
1375 reach BB or they may come from PHI nodes at the start of BB. */
1377 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
1378 LIMIT entries left in LOCALs. */
1380 static void
1381 remove_local_expressions_from_table (void)
1383 /* Remove all the expressions made available in this block. */
1384 while (avail_exprs_stack.length () > 0)
1386 std::pair<expr_hash_elt_t, expr_hash_elt_t> victim
1387 = avail_exprs_stack.pop ();
1388 expr_hash_elt **slot;
1390 if (victim.first == NULL)
1391 break;
1393 /* This must precede the actual removal from the hash table,
1394 as ELEMENT and the table entry may share a call argument
1395 vector which will be freed during removal. */
1396 if (dump_file && (dump_flags & TDF_DETAILS))
1398 fprintf (dump_file, "<<<< ");
1399 print_expr_hash_elt (dump_file, victim.first);
1402 slot = avail_exprs->find_slot (victim.first, NO_INSERT);
1403 gcc_assert (slot && *slot == victim.first);
1404 if (victim.second != NULL)
1406 free_expr_hash_elt (*slot);
1407 *slot = victim.second;
1409 else
1410 avail_exprs->clear_slot (slot);
1414 /* A trivial wrapper so that we can present the generic jump
1415 threading code with a simple API for simplifying statements. */
1416 static tree
1417 simplify_stmt_for_jump_threading (gimple stmt,
1418 gimple within_stmt ATTRIBUTE_UNUSED)
1420 return lookup_avail_expr (stmt, false);
1423 /* Record into the equivalence tables any equivalences implied by
1424 traversing edge E (which are cached in E->aux).
1426 Callers are responsible for managing the unwinding markers. */
1427 static void
1428 record_temporary_equivalences (edge e)
1430 int i;
1431 struct edge_info *edge_info = (struct edge_info *) e->aux;
1433 /* If we have info associated with this edge, record it into
1434 our equivalence tables. */
1435 if (edge_info)
1437 cond_equivalence *eq;
1438 tree lhs = edge_info->lhs;
1439 tree rhs = edge_info->rhs;
1441 /* If we have a simple NAME = VALUE equivalence, record it. */
1442 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1443 const_and_copies->record_const_or_copy (lhs, rhs);
1445 /* If we have 0 = COND or 1 = COND equivalences, record them
1446 into our expression hash tables. */
1447 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1448 record_cond (eq);
1452 /* Wrapper for common code to attempt to thread an edge. For example,
1453 it handles lazily building the dummy condition and the bookkeeping
1454 when jump threading is successful. */
1456 void
1457 dom_opt_dom_walker::thread_across_edge (edge e)
1459 if (! m_dummy_cond)
1460 m_dummy_cond =
1461 gimple_build_cond (NE_EXPR,
1462 integer_zero_node, integer_zero_node,
1463 NULL, NULL);
1465 /* Push a marker on both stacks so we can unwind the tables back to their
1466 current state. */
1467 avail_exprs_stack.safe_push
1468 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1469 const_and_copies->push_marker ();
1471 /* Traversing E may result in equivalences we can utilize. */
1472 record_temporary_equivalences (e);
1474 /* With all the edge equivalences in the tables, go ahead and attempt
1475 to thread through E->dest. */
1476 ::thread_across_edge (m_dummy_cond, e, false,
1477 const_and_copies,
1478 simplify_stmt_for_jump_threading);
1480 /* And restore the various tables to their state before
1481 we threaded this edge.
1483 XXX The code in tree-ssa-threadedge.c will restore the state of
1484 the const_and_copies table. We we just have to restore the expression
1485 table. */
1486 remove_local_expressions_from_table ();
1489 /* PHI nodes can create equivalences too.
1491 Ignoring any alternatives which are the same as the result, if
1492 all the alternatives are equal, then the PHI node creates an
1493 equivalence. */
1495 static void
1496 record_equivalences_from_phis (basic_block bb)
1498 gphi_iterator gsi;
1500 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1502 gphi *phi = gsi.phi ();
1504 tree lhs = gimple_phi_result (phi);
1505 tree rhs = NULL;
1506 size_t i;
1508 for (i = 0; i < gimple_phi_num_args (phi); i++)
1510 tree t = gimple_phi_arg_def (phi, i);
1512 /* Ignore alternatives which are the same as our LHS. Since
1513 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1514 can simply compare pointers. */
1515 if (lhs == t)
1516 continue;
1518 /* Valueize t. */
1519 if (TREE_CODE (t) == SSA_NAME)
1521 tree tmp = SSA_NAME_VALUE (t);
1522 t = tmp ? tmp : t;
1525 /* If we have not processed an alternative yet, then set
1526 RHS to this alternative. */
1527 if (rhs == NULL)
1528 rhs = t;
1529 /* If we have processed an alternative (stored in RHS), then
1530 see if it is equal to this one. If it isn't, then stop
1531 the search. */
1532 else if (! operand_equal_for_phi_arg_p (rhs, t))
1533 break;
1536 /* If we had no interesting alternatives, then all the RHS alternatives
1537 must have been the same as LHS. */
1538 if (!rhs)
1539 rhs = lhs;
1541 /* If we managed to iterate through each PHI alternative without
1542 breaking out of the loop, then we have a PHI which may create
1543 a useful equivalence. We do not need to record unwind data for
1544 this, since this is a true assignment and not an equivalence
1545 inferred from a comparison. All uses of this ssa name are dominated
1546 by this assignment, so unwinding just costs time and space. */
1547 if (i == gimple_phi_num_args (phi)
1548 && may_propagate_copy (lhs, rhs))
1549 set_ssa_name_value (lhs, rhs);
1553 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1554 return that edge. Otherwise return NULL. */
1555 static edge
1556 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1558 edge retval = NULL;
1559 edge e;
1560 edge_iterator ei;
1562 FOR_EACH_EDGE (e, ei, bb->preds)
1564 /* A loop back edge can be identified by the destination of
1565 the edge dominating the source of the edge. */
1566 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1567 continue;
1569 /* If we have already seen a non-loop edge, then we must have
1570 multiple incoming non-loop edges and thus we return NULL. */
1571 if (retval)
1572 return NULL;
1574 /* This is the first non-loop incoming edge we have found. Record
1575 it. */
1576 retval = e;
1579 return retval;
1582 /* Record any equivalences created by the incoming edge to BB. If BB
1583 has more than one incoming edge, then no equivalence is created. */
1585 static void
1586 record_equivalences_from_incoming_edge (basic_block bb)
1588 edge e;
1589 basic_block parent;
1590 struct edge_info *edge_info;
1592 /* If our parent block ended with a control statement, then we may be
1593 able to record some equivalences based on which outgoing edge from
1594 the parent was followed. */
1595 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1597 e = single_incoming_edge_ignoring_loop_edges (bb);
1599 /* If we had a single incoming edge from our parent block, then enter
1600 any data associated with the edge into our tables. */
1601 if (e && e->src == parent)
1603 unsigned int i;
1605 edge_info = (struct edge_info *) e->aux;
1607 if (edge_info)
1609 tree lhs = edge_info->lhs;
1610 tree rhs = edge_info->rhs;
1611 cond_equivalence *eq;
1613 if (lhs)
1614 record_equality (lhs, rhs);
1616 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1617 set via a widening type conversion, then we may be able to record
1618 additional equivalences. */
1619 if (lhs
1620 && TREE_CODE (lhs) == SSA_NAME
1621 && is_gimple_constant (rhs)
1622 && TREE_CODE (rhs) == INTEGER_CST)
1624 gimple defstmt = SSA_NAME_DEF_STMT (lhs);
1626 if (defstmt
1627 && is_gimple_assign (defstmt)
1628 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
1630 tree old_rhs = gimple_assign_rhs1 (defstmt);
1632 /* If the conversion widens the original value and
1633 the constant is in the range of the type of OLD_RHS,
1634 then convert the constant and record the equivalence.
1636 Note that int_fits_type_p does not check the precision
1637 if the upper and lower bounds are OK. */
1638 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
1639 && (TYPE_PRECISION (TREE_TYPE (lhs))
1640 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
1641 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
1643 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1644 record_equality (old_rhs, newval);
1649 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1650 record_cond (eq);
1655 /* Dump SSA statistics on FILE. */
1657 void
1658 dump_dominator_optimization_stats (FILE *file)
1660 fprintf (file, "Total number of statements: %6ld\n\n",
1661 opt_stats.num_stmts);
1662 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1663 opt_stats.num_exprs_considered);
1665 fprintf (file, "\nHash table statistics:\n");
1667 fprintf (file, " avail_exprs: ");
1668 htab_statistics (file, *avail_exprs);
1672 /* Dump SSA statistics on stderr. */
1674 DEBUG_FUNCTION void
1675 debug_dominator_optimization_stats (void)
1677 dump_dominator_optimization_stats (stderr);
1681 /* Dump statistics for the hash table HTAB. */
1683 static void
1684 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1686 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1687 (long) htab.size (),
1688 (long) htab.elements (),
1689 htab.collisions ());
1693 /* Enter condition equivalence into the expression hash table.
1694 This indicates that a conditional expression has a known
1695 boolean value. */
1697 static void
1698 record_cond (cond_equivalence *p)
1700 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1701 expr_hash_elt **slot;
1703 initialize_hash_element_from_expr (&p->cond, p->value, element);
1705 slot = avail_exprs->find_slot_with_hash (element, element->hash, INSERT);
1706 if (*slot == NULL)
1708 *slot = element;
1710 if (dump_file && (dump_flags & TDF_DETAILS))
1712 fprintf (dump_file, "1>>> ");
1713 print_expr_hash_elt (dump_file, element);
1716 avail_exprs_stack.safe_push
1717 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element, NULL));
1719 else
1720 free_expr_hash_elt (element);
1723 /* Return the loop depth of the basic block of the defining statement of X.
1724 This number should not be treated as absolutely correct because the loop
1725 information may not be completely up-to-date when dom runs. However, it
1726 will be relatively correct, and as more passes are taught to keep loop info
1727 up to date, the result will become more and more accurate. */
1729 static int
1730 loop_depth_of_name (tree x)
1732 gimple defstmt;
1733 basic_block defbb;
1735 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1736 if (TREE_CODE (x) != SSA_NAME)
1737 return 0;
1739 /* Otherwise return the loop depth of the defining statement's bb.
1740 Note that there may not actually be a bb for this statement, if the
1741 ssa_name is live on entry. */
1742 defstmt = SSA_NAME_DEF_STMT (x);
1743 defbb = gimple_bb (defstmt);
1744 if (!defbb)
1745 return 0;
1747 return bb_loop_depth (defbb);
1750 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1751 This constrains the cases in which we may treat this as assignment. */
1753 static void
1754 record_equality (tree x, tree y)
1756 tree prev_x = NULL, prev_y = NULL;
1758 if (tree_swap_operands_p (x, y, false))
1759 std::swap (x, y);
1761 /* Most of the time tree_swap_operands_p does what we want. But there
1762 are cases where we know one operand is better for copy propagation than
1763 the other. Given no other code cares about ordering of equality
1764 comparison operators for that purpose, we just handle the special cases
1765 here. */
1766 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1768 /* If one operand is a single use operand, then make it
1769 X. This will preserve its single use properly and if this
1770 conditional is eliminated, the computation of X can be
1771 eliminated as well. */
1772 if (has_single_use (y) && ! has_single_use (x))
1773 std::swap (x, y);
1775 if (TREE_CODE (x) == SSA_NAME)
1776 prev_x = SSA_NAME_VALUE (x);
1777 if (TREE_CODE (y) == SSA_NAME)
1778 prev_y = SSA_NAME_VALUE (y);
1780 /* If one of the previous values is invariant, or invariant in more loops
1781 (by depth), then use that.
1782 Otherwise it doesn't matter which value we choose, just so
1783 long as we canonicalize on one value. */
1784 if (is_gimple_min_invariant (y))
1786 else if (is_gimple_min_invariant (x)
1787 /* ??? When threading over backedges the following is important
1788 for correctness. See PR61757. */
1789 || (loop_depth_of_name (x) < loop_depth_of_name (y)))
1790 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1791 else if (prev_x && is_gimple_min_invariant (prev_x))
1792 x = y, y = prev_x, prev_x = prev_y;
1793 else if (prev_y)
1794 y = prev_y;
1796 /* After the swapping, we must have one SSA_NAME. */
1797 if (TREE_CODE (x) != SSA_NAME)
1798 return;
1800 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1801 variable compared against zero. If we're honoring signed zeros,
1802 then we cannot record this value unless we know that the value is
1803 nonzero. */
1804 if (HONOR_SIGNED_ZEROS (x)
1805 && (TREE_CODE (y) != REAL_CST
1806 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1807 return;
1809 const_and_copies->record_const_or_copy (x, y, prev_x);
1812 /* Returns true when STMT is a simple iv increment. It detects the
1813 following situation:
1815 i_1 = phi (..., i_2)
1816 i_2 = i_1 +/- ... */
1818 bool
1819 simple_iv_increment_p (gimple stmt)
1821 enum tree_code code;
1822 tree lhs, preinc;
1823 gimple phi;
1824 size_t i;
1826 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1827 return false;
1829 lhs = gimple_assign_lhs (stmt);
1830 if (TREE_CODE (lhs) != SSA_NAME)
1831 return false;
1833 code = gimple_assign_rhs_code (stmt);
1834 if (code != PLUS_EXPR
1835 && code != MINUS_EXPR
1836 && code != POINTER_PLUS_EXPR)
1837 return false;
1839 preinc = gimple_assign_rhs1 (stmt);
1840 if (TREE_CODE (preinc) != SSA_NAME)
1841 return false;
1843 phi = SSA_NAME_DEF_STMT (preinc);
1844 if (gimple_code (phi) != GIMPLE_PHI)
1845 return false;
1847 for (i = 0; i < gimple_phi_num_args (phi); i++)
1848 if (gimple_phi_arg_def (phi, i) == lhs)
1849 return true;
1851 return false;
1854 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1855 known value for that SSA_NAME (or NULL if no value is known).
1857 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1858 successors of BB. */
1860 static void
1861 cprop_into_successor_phis (basic_block bb)
1863 edge e;
1864 edge_iterator ei;
1866 FOR_EACH_EDGE (e, ei, bb->succs)
1868 int indx;
1869 gphi_iterator gsi;
1871 /* If this is an abnormal edge, then we do not want to copy propagate
1872 into the PHI alternative associated with this edge. */
1873 if (e->flags & EDGE_ABNORMAL)
1874 continue;
1876 gsi = gsi_start_phis (e->dest);
1877 if (gsi_end_p (gsi))
1878 continue;
1880 /* We may have an equivalence associated with this edge. While
1881 we can not propagate it into non-dominated blocks, we can
1882 propagate them into PHIs in non-dominated blocks. */
1884 /* Push the unwind marker so we can reset the const and copies
1885 table back to its original state after processing this edge. */
1886 const_and_copies->push_marker ();
1888 /* Extract and record any simple NAME = VALUE equivalences.
1890 Don't bother with [01] = COND equivalences, they're not useful
1891 here. */
1892 struct edge_info *edge_info = (struct edge_info *) e->aux;
1893 if (edge_info)
1895 tree lhs = edge_info->lhs;
1896 tree rhs = edge_info->rhs;
1898 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1899 const_and_copies->record_const_or_copy (lhs, rhs);
1902 indx = e->dest_idx;
1903 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1905 tree new_val;
1906 use_operand_p orig_p;
1907 tree orig_val;
1908 gphi *phi = gsi.phi ();
1910 /* The alternative may be associated with a constant, so verify
1911 it is an SSA_NAME before doing anything with it. */
1912 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1913 orig_val = get_use_from_ptr (orig_p);
1914 if (TREE_CODE (orig_val) != SSA_NAME)
1915 continue;
1917 /* If we have *ORIG_P in our constant/copy table, then replace
1918 ORIG_P with its value in our constant/copy table. */
1919 new_val = SSA_NAME_VALUE (orig_val);
1920 if (new_val
1921 && new_val != orig_val
1922 && (TREE_CODE (new_val) == SSA_NAME
1923 || is_gimple_min_invariant (new_val))
1924 && may_propagate_copy (orig_val, new_val))
1925 propagate_value (orig_p, new_val);
1928 const_and_copies->pop_to_marker ();
1932 void
1933 dom_opt_dom_walker::before_dom_children (basic_block bb)
1935 gimple_stmt_iterator gsi;
1937 if (dump_file && (dump_flags & TDF_DETAILS))
1938 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1940 /* Push a marker on the stacks of local information so that we know how
1941 far to unwind when we finalize this block. */
1942 avail_exprs_stack.safe_push
1943 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1944 const_and_copies->push_marker ();
1946 record_equivalences_from_incoming_edge (bb);
1948 /* PHI nodes can create equivalences too. */
1949 record_equivalences_from_phis (bb);
1951 /* Create equivalences from redundant PHIs. PHIs are only truly
1952 redundant when they exist in the same block, so push another
1953 marker and unwind right afterwards. */
1954 avail_exprs_stack.safe_push
1955 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1956 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1957 eliminate_redundant_computations (&gsi);
1958 remove_local_expressions_from_table ();
1960 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1961 optimize_stmt (bb, gsi);
1963 /* Now prepare to process dominated blocks. */
1964 record_edge_info (bb);
1965 cprop_into_successor_phis (bb);
1968 /* We have finished processing the dominator children of BB, perform
1969 any finalization actions in preparation for leaving this node in
1970 the dominator tree. */
1972 void
1973 dom_opt_dom_walker::after_dom_children (basic_block bb)
1975 gimple last;
1977 /* If we have an outgoing edge to a block with multiple incoming and
1978 outgoing edges, then we may be able to thread the edge, i.e., we
1979 may be able to statically determine which of the outgoing edges
1980 will be traversed when the incoming edge from BB is traversed. */
1981 if (single_succ_p (bb)
1982 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1983 && potentially_threadable_block (single_succ (bb)))
1985 thread_across_edge (single_succ_edge (bb));
1987 else if ((last = last_stmt (bb))
1988 && gimple_code (last) == GIMPLE_COND
1989 && EDGE_COUNT (bb->succs) == 2
1990 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1991 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1993 edge true_edge, false_edge;
1995 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1997 /* Only try to thread the edge if it reaches a target block with
1998 more than one predecessor and more than one successor. */
1999 if (potentially_threadable_block (true_edge->dest))
2000 thread_across_edge (true_edge);
2002 /* Similarly for the ELSE arm. */
2003 if (potentially_threadable_block (false_edge->dest))
2004 thread_across_edge (false_edge);
2008 /* These remove expressions local to BB from the tables. */
2009 remove_local_expressions_from_table ();
2010 const_and_copies->pop_to_marker ();
2013 /* Search for redundant computations in STMT. If any are found, then
2014 replace them with the variable holding the result of the computation.
2016 If safe, record this expression into the available expression hash
2017 table. */
2019 static void
2020 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
2022 tree expr_type;
2023 tree cached_lhs;
2024 tree def;
2025 bool insert = true;
2026 bool assigns_var_p = false;
2028 gimple stmt = gsi_stmt (*gsi);
2030 if (gimple_code (stmt) == GIMPLE_PHI)
2031 def = gimple_phi_result (stmt);
2032 else
2033 def = gimple_get_lhs (stmt);
2035 /* Certain expressions on the RHS can be optimized away, but can not
2036 themselves be entered into the hash tables. */
2037 if (! def
2038 || TREE_CODE (def) != SSA_NAME
2039 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2040 || gimple_vdef (stmt)
2041 /* Do not record equivalences for increments of ivs. This would create
2042 overlapping live ranges for a very questionable gain. */
2043 || simple_iv_increment_p (stmt))
2044 insert = false;
2046 /* Check if the expression has been computed before. */
2047 cached_lhs = lookup_avail_expr (stmt, insert);
2049 opt_stats.num_exprs_considered++;
2051 /* Get the type of the expression we are trying to optimize. */
2052 if (is_gimple_assign (stmt))
2054 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
2055 assigns_var_p = true;
2057 else if (gimple_code (stmt) == GIMPLE_COND)
2058 expr_type = boolean_type_node;
2059 else if (is_gimple_call (stmt))
2061 gcc_assert (gimple_call_lhs (stmt));
2062 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
2063 assigns_var_p = true;
2065 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2066 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
2067 else if (gimple_code (stmt) == GIMPLE_PHI)
2068 /* We can't propagate into a phi, so the logic below doesn't apply.
2069 Instead record an equivalence between the cached LHS and the
2070 PHI result of this statement, provided they are in the same block.
2071 This should be sufficient to kill the redundant phi. */
2073 if (def && cached_lhs)
2074 const_and_copies->record_const_or_copy (def, cached_lhs);
2075 return;
2077 else
2078 gcc_unreachable ();
2080 if (!cached_lhs)
2081 return;
2083 /* It is safe to ignore types here since we have already done
2084 type checking in the hashing and equality routines. In fact
2085 type checking here merely gets in the way of constant
2086 propagation. Also, make sure that it is safe to propagate
2087 CACHED_LHS into the expression in STMT. */
2088 if ((TREE_CODE (cached_lhs) != SSA_NAME
2089 && (assigns_var_p
2090 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
2091 || may_propagate_copy_into_stmt (stmt, cached_lhs))
2093 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
2094 || is_gimple_min_invariant (cached_lhs));
2096 if (dump_file && (dump_flags & TDF_DETAILS))
2098 fprintf (dump_file, " Replaced redundant expr '");
2099 print_gimple_expr (dump_file, stmt, 0, dump_flags);
2100 fprintf (dump_file, "' with '");
2101 print_generic_expr (dump_file, cached_lhs, dump_flags);
2102 fprintf (dump_file, "'\n");
2105 opt_stats.num_re++;
2107 if (assigns_var_p
2108 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
2109 cached_lhs = fold_convert (expr_type, cached_lhs);
2111 propagate_tree_value_into_stmt (gsi, cached_lhs);
2113 /* Since it is always necessary to mark the result as modified,
2114 perhaps we should move this into propagate_tree_value_into_stmt
2115 itself. */
2116 gimple_set_modified (gsi_stmt (*gsi), true);
2120 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2121 the available expressions table or the const_and_copies table.
2122 Detect and record those equivalences. */
2123 /* We handle only very simple copy equivalences here. The heavy
2124 lifing is done by eliminate_redundant_computations. */
2126 static void
2127 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
2129 tree lhs;
2130 enum tree_code lhs_code;
2132 gcc_assert (is_gimple_assign (stmt));
2134 lhs = gimple_assign_lhs (stmt);
2135 lhs_code = TREE_CODE (lhs);
2137 if (lhs_code == SSA_NAME
2138 && gimple_assign_single_p (stmt))
2140 tree rhs = gimple_assign_rhs1 (stmt);
2142 /* If the RHS of the assignment is a constant or another variable that
2143 may be propagated, register it in the CONST_AND_COPIES table. We
2144 do not need to record unwind data for this, since this is a true
2145 assignment and not an equivalence inferred from a comparison. All
2146 uses of this ssa name are dominated by this assignment, so unwinding
2147 just costs time and space. */
2148 if (may_optimize_p
2149 && (TREE_CODE (rhs) == SSA_NAME
2150 || is_gimple_min_invariant (rhs)))
2152 /* Valueize rhs. */
2153 if (TREE_CODE (rhs) == SSA_NAME)
2155 tree tmp = SSA_NAME_VALUE (rhs);
2156 rhs = tmp ? tmp : rhs;
2159 if (dump_file && (dump_flags & TDF_DETAILS))
2161 fprintf (dump_file, "==== ASGN ");
2162 print_generic_expr (dump_file, lhs, 0);
2163 fprintf (dump_file, " = ");
2164 print_generic_expr (dump_file, rhs, 0);
2165 fprintf (dump_file, "\n");
2168 set_ssa_name_value (lhs, rhs);
2172 /* Make sure we can propagate &x + CST. */
2173 if (lhs_code == SSA_NAME
2174 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2175 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
2176 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
2178 tree op0 = gimple_assign_rhs1 (stmt);
2179 tree op1 = gimple_assign_rhs2 (stmt);
2180 tree new_rhs
2181 = build_fold_addr_expr (fold_build2 (MEM_REF,
2182 TREE_TYPE (TREE_TYPE (op0)),
2183 unshare_expr (op0),
2184 fold_convert (ptr_type_node,
2185 op1)));
2186 if (dump_file && (dump_flags & TDF_DETAILS))
2188 fprintf (dump_file, "==== ASGN ");
2189 print_generic_expr (dump_file, lhs, 0);
2190 fprintf (dump_file, " = ");
2191 print_generic_expr (dump_file, new_rhs, 0);
2192 fprintf (dump_file, "\n");
2195 set_ssa_name_value (lhs, new_rhs);
2198 /* A memory store, even an aliased store, creates a useful
2199 equivalence. By exchanging the LHS and RHS, creating suitable
2200 vops and recording the result in the available expression table,
2201 we may be able to expose more redundant loads. */
2202 if (!gimple_has_volatile_ops (stmt)
2203 && gimple_references_memory_p (stmt)
2204 && gimple_assign_single_p (stmt)
2205 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2206 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2207 && !is_gimple_reg (lhs))
2209 tree rhs = gimple_assign_rhs1 (stmt);
2210 gassign *new_stmt;
2212 /* Build a new statement with the RHS and LHS exchanged. */
2213 if (TREE_CODE (rhs) == SSA_NAME)
2215 /* NOTE tuples. The call to gimple_build_assign below replaced
2216 a call to build_gimple_modify_stmt, which did not set the
2217 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2218 may cause an SSA validation failure, as the LHS may be a
2219 default-initialized name and should have no definition. I'm
2220 a bit dubious of this, as the artificial statement that we
2221 generate here may in fact be ill-formed, but it is simply
2222 used as an internal device in this pass, and never becomes
2223 part of the CFG. */
2224 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2225 new_stmt = gimple_build_assign (rhs, lhs);
2226 SSA_NAME_DEF_STMT (rhs) = defstmt;
2228 else
2229 new_stmt = gimple_build_assign (rhs, lhs);
2231 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2233 /* Finally enter the statement into the available expression
2234 table. */
2235 lookup_avail_expr (new_stmt, true);
2239 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2240 CONST_AND_COPIES. */
2242 static void
2243 cprop_operand (gimple stmt, use_operand_p op_p)
2245 tree val;
2246 tree op = USE_FROM_PTR (op_p);
2248 /* If the operand has a known constant value or it is known to be a
2249 copy of some other variable, use the value or copy stored in
2250 CONST_AND_COPIES. */
2251 val = SSA_NAME_VALUE (op);
2252 if (val && val != op)
2254 /* Do not replace hard register operands in asm statements. */
2255 if (gimple_code (stmt) == GIMPLE_ASM
2256 && !may_propagate_copy_into_asm (op))
2257 return;
2259 /* Certain operands are not allowed to be copy propagated due
2260 to their interaction with exception handling and some GCC
2261 extensions. */
2262 if (!may_propagate_copy (op, val))
2263 return;
2265 /* Do not propagate copies into BIVs.
2266 See PR23821 and PR62217 for how this can disturb IV and
2267 number of iteration analysis. */
2268 if (TREE_CODE (val) != INTEGER_CST)
2270 gimple def = SSA_NAME_DEF_STMT (op);
2271 if (gimple_code (def) == GIMPLE_PHI
2272 && gimple_bb (def)->loop_father->header == gimple_bb (def))
2273 return;
2276 /* Dump details. */
2277 if (dump_file && (dump_flags & TDF_DETAILS))
2279 fprintf (dump_file, " Replaced '");
2280 print_generic_expr (dump_file, op, dump_flags);
2281 fprintf (dump_file, "' with %s '",
2282 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2283 print_generic_expr (dump_file, val, dump_flags);
2284 fprintf (dump_file, "'\n");
2287 if (TREE_CODE (val) != SSA_NAME)
2288 opt_stats.num_const_prop++;
2289 else
2290 opt_stats.num_copy_prop++;
2292 propagate_value (op_p, val);
2294 /* And note that we modified this statement. This is now
2295 safe, even if we changed virtual operands since we will
2296 rescan the statement and rewrite its operands again. */
2297 gimple_set_modified (stmt, true);
2301 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2302 known value for that SSA_NAME (or NULL if no value is known).
2304 Propagate values from CONST_AND_COPIES into the uses, vuses and
2305 vdef_ops of STMT. */
2307 static void
2308 cprop_into_stmt (gimple stmt)
2310 use_operand_p op_p;
2311 ssa_op_iter iter;
2313 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2314 cprop_operand (stmt, op_p);
2317 /* Optimize the statement pointed to by iterator SI.
2319 We try to perform some simplistic global redundancy elimination and
2320 constant propagation:
2322 1- To detect global redundancy, we keep track of expressions that have
2323 been computed in this block and its dominators. If we find that the
2324 same expression is computed more than once, we eliminate repeated
2325 computations by using the target of the first one.
2327 2- Constant values and copy assignments. This is used to do very
2328 simplistic constant and copy propagation. When a constant or copy
2329 assignment is found, we map the value on the RHS of the assignment to
2330 the variable in the LHS in the CONST_AND_COPIES table. */
2332 static void
2333 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2335 gimple stmt, old_stmt;
2336 bool may_optimize_p;
2337 bool modified_p = false;
2338 bool was_noreturn;
2340 old_stmt = stmt = gsi_stmt (si);
2341 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
2343 if (dump_file && (dump_flags & TDF_DETAILS))
2345 fprintf (dump_file, "Optimizing statement ");
2346 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2349 if (gimple_code (stmt) == GIMPLE_COND)
2350 canonicalize_comparison (as_a <gcond *> (stmt));
2352 update_stmt_if_modified (stmt);
2353 opt_stats.num_stmts++;
2355 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2356 cprop_into_stmt (stmt);
2358 /* If the statement has been modified with constant replacements,
2359 fold its RHS before checking for redundant computations. */
2360 if (gimple_modified_p (stmt))
2362 tree rhs = NULL;
2364 /* Try to fold the statement making sure that STMT is kept
2365 up to date. */
2366 if (fold_stmt (&si))
2368 stmt = gsi_stmt (si);
2369 gimple_set_modified (stmt, true);
2371 if (dump_file && (dump_flags & TDF_DETAILS))
2373 fprintf (dump_file, " Folded to: ");
2374 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2378 /* We only need to consider cases that can yield a gimple operand. */
2379 if (gimple_assign_single_p (stmt))
2380 rhs = gimple_assign_rhs1 (stmt);
2381 else if (gimple_code (stmt) == GIMPLE_GOTO)
2382 rhs = gimple_goto_dest (stmt);
2383 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2384 /* This should never be an ADDR_EXPR. */
2385 rhs = gimple_switch_index (swtch_stmt);
2387 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2388 recompute_tree_invariant_for_addr_expr (rhs);
2390 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2391 even if fold_stmt updated the stmt already and thus cleared
2392 gimple_modified_p flag on it. */
2393 modified_p = true;
2396 /* Check for redundant computations. Do this optimization only
2397 for assignments that have no volatile ops and conditionals. */
2398 may_optimize_p = (!gimple_has_side_effects (stmt)
2399 && (is_gimple_assign (stmt)
2400 || (is_gimple_call (stmt)
2401 && gimple_call_lhs (stmt) != NULL_TREE)
2402 || gimple_code (stmt) == GIMPLE_COND
2403 || gimple_code (stmt) == GIMPLE_SWITCH));
2405 if (may_optimize_p)
2407 if (gimple_code (stmt) == GIMPLE_CALL)
2409 /* Resolve __builtin_constant_p. If it hasn't been
2410 folded to integer_one_node by now, it's fairly
2411 certain that the value simply isn't constant. */
2412 tree callee = gimple_call_fndecl (stmt);
2413 if (callee
2414 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2415 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2417 propagate_tree_value_into_stmt (&si, integer_zero_node);
2418 stmt = gsi_stmt (si);
2422 update_stmt_if_modified (stmt);
2423 eliminate_redundant_computations (&si);
2424 stmt = gsi_stmt (si);
2426 /* Perform simple redundant store elimination. */
2427 if (gimple_assign_single_p (stmt)
2428 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2430 tree lhs = gimple_assign_lhs (stmt);
2431 tree rhs = gimple_assign_rhs1 (stmt);
2432 tree cached_lhs;
2433 gassign *new_stmt;
2434 if (TREE_CODE (rhs) == SSA_NAME)
2436 tree tem = SSA_NAME_VALUE (rhs);
2437 if (tem)
2438 rhs = tem;
2440 /* Build a new statement with the RHS and LHS exchanged. */
2441 if (TREE_CODE (rhs) == SSA_NAME)
2443 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2444 new_stmt = gimple_build_assign (rhs, lhs);
2445 SSA_NAME_DEF_STMT (rhs) = defstmt;
2447 else
2448 new_stmt = gimple_build_assign (rhs, lhs);
2449 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2450 cached_lhs = lookup_avail_expr (new_stmt, false);
2451 if (cached_lhs
2452 && rhs == cached_lhs)
2454 basic_block bb = gimple_bb (stmt);
2455 unlink_stmt_vdef (stmt);
2456 if (gsi_remove (&si, true))
2458 bitmap_set_bit (need_eh_cleanup, bb->index);
2459 if (dump_file && (dump_flags & TDF_DETAILS))
2460 fprintf (dump_file, " Flagged to clear EH edges.\n");
2462 release_defs (stmt);
2463 return;
2468 /* Record any additional equivalences created by this statement. */
2469 if (is_gimple_assign (stmt))
2470 record_equivalences_from_stmt (stmt, may_optimize_p);
2472 /* If STMT is a COND_EXPR and it was modified, then we may know
2473 where it goes. If that is the case, then mark the CFG as altered.
2475 This will cause us to later call remove_unreachable_blocks and
2476 cleanup_tree_cfg when it is safe to do so. It is not safe to
2477 clean things up here since removal of edges and such can trigger
2478 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2479 the manager.
2481 That's all fine and good, except that once SSA_NAMEs are released
2482 to the manager, we must not call create_ssa_name until all references
2483 to released SSA_NAMEs have been eliminated.
2485 All references to the deleted SSA_NAMEs can not be eliminated until
2486 we remove unreachable blocks.
2488 We can not remove unreachable blocks until after we have completed
2489 any queued jump threading.
2491 We can not complete any queued jump threads until we have taken
2492 appropriate variables out of SSA form. Taking variables out of
2493 SSA form can call create_ssa_name and thus we lose.
2495 Ultimately I suspect we're going to need to change the interface
2496 into the SSA_NAME manager. */
2497 if (gimple_modified_p (stmt) || modified_p)
2499 tree val = NULL;
2501 update_stmt_if_modified (stmt);
2503 if (gimple_code (stmt) == GIMPLE_COND)
2504 val = fold_binary_loc (gimple_location (stmt),
2505 gimple_cond_code (stmt), boolean_type_node,
2506 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2507 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2508 val = gimple_switch_index (swtch_stmt);
2510 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2511 cfg_altered = true;
2513 /* If we simplified a statement in such a way as to be shown that it
2514 cannot trap, update the eh information and the cfg to match. */
2515 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2517 bitmap_set_bit (need_eh_cleanup, bb->index);
2518 if (dump_file && (dump_flags & TDF_DETAILS))
2519 fprintf (dump_file, " Flagged to clear EH edges.\n");
2522 if (!was_noreturn
2523 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
2524 need_noreturn_fixup.safe_push (stmt);
2528 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
2529 the desired memory state. */
2531 static void *
2532 vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
2534 tree vuse2 = (tree) data;
2535 if (vuse1 == vuse2)
2536 return data;
2538 /* This bounds the stmt walks we perform on reference lookups
2539 to O(1) instead of O(N) where N is the number of dominating
2540 stores leading to a candidate. We re-use the SCCVN param
2541 for this as it is basically the same complexity. */
2542 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
2543 return (void *)-1;
2545 return NULL;
2548 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2549 If found, return its LHS. Otherwise insert STMT in the table and
2550 return NULL_TREE.
2552 Also, when an expression is first inserted in the table, it is also
2553 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2554 we finish processing this block and its children. */
2556 static tree
2557 lookup_avail_expr (gimple stmt, bool insert)
2559 expr_hash_elt **slot;
2560 tree lhs;
2561 tree temp;
2562 struct expr_hash_elt element;
2564 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2565 if (gimple_code (stmt) == GIMPLE_PHI)
2566 lhs = gimple_phi_result (stmt);
2567 else
2568 lhs = gimple_get_lhs (stmt);
2570 initialize_hash_element (stmt, lhs, &element);
2572 if (dump_file && (dump_flags & TDF_DETAILS))
2574 fprintf (dump_file, "LKUP ");
2575 print_expr_hash_elt (dump_file, &element);
2578 /* Don't bother remembering constant assignments and copy operations.
2579 Constants and copy operations are handled by the constant/copy propagator
2580 in optimize_stmt. */
2581 if (element.expr.kind == EXPR_SINGLE
2582 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2583 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2584 return NULL_TREE;
2586 /* Finally try to find the expression in the main expression hash table. */
2587 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
2588 if (slot == NULL)
2590 free_expr_hash_elt_contents (&element);
2591 return NULL_TREE;
2593 else if (*slot == NULL)
2595 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2596 *element2 = element;
2597 element2->stamp = element2;
2598 *slot = element2;
2600 if (dump_file && (dump_flags & TDF_DETAILS))
2602 fprintf (dump_file, "2>>> ");
2603 print_expr_hash_elt (dump_file, element2);
2606 avail_exprs_stack.safe_push
2607 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element2, NULL));
2608 return NULL_TREE;
2611 /* If we found a redundant memory operation do an alias walk to
2612 check if we can re-use it. */
2613 if (gimple_vuse (stmt) != (*slot)->vop)
2615 tree vuse1 = (*slot)->vop;
2616 tree vuse2 = gimple_vuse (stmt);
2617 /* If we have a load of a register and a candidate in the
2618 hash with vuse1 then try to reach its stmt by walking
2619 up the virtual use-def chain using walk_non_aliased_vuses.
2620 But don't do this when removing expressions from the hash. */
2621 ao_ref ref;
2622 if (!(vuse1 && vuse2
2623 && gimple_assign_single_p (stmt)
2624 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
2625 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)), true)
2626 && walk_non_aliased_vuses (&ref, vuse2,
2627 vuse_eq, NULL, NULL, vuse1) != NULL))
2629 if (insert)
2631 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2632 *element2 = element;
2633 element2->stamp = element2;
2635 /* Insert the expr into the hash by replacing the current
2636 entry and recording the value to restore in the
2637 avail_exprs_stack. */
2638 avail_exprs_stack.safe_push (std::make_pair (element2, *slot));
2639 *slot = element2;
2640 if (dump_file && (dump_flags & TDF_DETAILS))
2642 fprintf (dump_file, "2>>> ");
2643 print_expr_hash_elt (dump_file, *slot);
2646 return NULL_TREE;
2650 free_expr_hash_elt_contents (&element);
2652 /* Extract the LHS of the assignment so that it can be used as the current
2653 definition of another variable. */
2654 lhs = (*slot)->lhs;
2656 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2657 use the value from the const_and_copies table. */
2658 if (TREE_CODE (lhs) == SSA_NAME)
2660 temp = SSA_NAME_VALUE (lhs);
2661 if (temp)
2662 lhs = temp;
2665 if (dump_file && (dump_flags & TDF_DETAILS))
2667 fprintf (dump_file, "FIND: ");
2668 print_generic_expr (dump_file, lhs, 0);
2669 fprintf (dump_file, "\n");
2672 return lhs;
2675 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2676 for expressions using the code of the expression and the SSA numbers of
2677 its operands. */
2679 static hashval_t
2680 avail_expr_hash (const void *p)
2682 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2683 inchash::hash hstate;
2685 inchash::add_hashable_expr (expr, hstate);
2687 return hstate.end ();
2690 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2691 up degenerate PHIs created by or exposed by jump threading. */
2693 /* Given a statement STMT, which is either a PHI node or an assignment,
2694 remove it from the IL. */
2696 static void
2697 remove_stmt_or_phi (gimple stmt)
2699 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2701 if (gimple_code (stmt) == GIMPLE_PHI)
2702 remove_phi_node (&gsi, true);
2703 else
2705 gsi_remove (&gsi, true);
2706 release_defs (stmt);
2710 /* Given a statement STMT, which is either a PHI node or an assignment,
2711 return the "rhs" of the node, in the case of a non-degenerate
2712 phi, NULL is returned. */
2714 static tree
2715 get_rhs_or_phi_arg (gimple stmt)
2717 if (gimple_code (stmt) == GIMPLE_PHI)
2718 return degenerate_phi_result (as_a <gphi *> (stmt));
2719 else if (gimple_assign_single_p (stmt))
2720 return gimple_assign_rhs1 (stmt);
2721 else
2722 gcc_unreachable ();
2726 /* Given a statement STMT, which is either a PHI node or an assignment,
2727 return the "lhs" of the node. */
2729 static tree
2730 get_lhs_or_phi_result (gimple stmt)
2732 if (gimple_code (stmt) == GIMPLE_PHI)
2733 return gimple_phi_result (stmt);
2734 else if (is_gimple_assign (stmt))
2735 return gimple_assign_lhs (stmt);
2736 else
2737 gcc_unreachable ();
2740 /* Propagate RHS into all uses of LHS (when possible).
2742 RHS and LHS are derived from STMT, which is passed in solely so
2743 that we can remove it if propagation is successful.
2745 When propagating into a PHI node or into a statement which turns
2746 into a trivial copy or constant initialization, set the
2747 appropriate bit in INTERESTING_NAMEs so that we will visit those
2748 nodes as well in an effort to pick up secondary optimization
2749 opportunities. */
2751 static void
2752 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2754 /* First verify that propagation is valid. */
2755 if (may_propagate_copy (lhs, rhs))
2757 use_operand_p use_p;
2758 imm_use_iterator iter;
2759 gimple use_stmt;
2760 bool all = true;
2762 /* Dump details. */
2763 if (dump_file && (dump_flags & TDF_DETAILS))
2765 fprintf (dump_file, " Replacing '");
2766 print_generic_expr (dump_file, lhs, dump_flags);
2767 fprintf (dump_file, "' with %s '",
2768 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2769 print_generic_expr (dump_file, rhs, dump_flags);
2770 fprintf (dump_file, "'\n");
2773 /* Walk over every use of LHS and try to replace the use with RHS.
2774 At this point the only reason why such a propagation would not
2775 be successful would be if the use occurs in an ASM_EXPR. */
2776 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2778 /* Leave debug stmts alone. If we succeed in propagating
2779 all non-debug uses, we'll drop the DEF, and propagation
2780 into debug stmts will occur then. */
2781 if (gimple_debug_bind_p (use_stmt))
2782 continue;
2784 /* It's not always safe to propagate into an ASM_EXPR. */
2785 if (gimple_code (use_stmt) == GIMPLE_ASM
2786 && ! may_propagate_copy_into_asm (lhs))
2788 all = false;
2789 continue;
2792 /* It's not ok to propagate into the definition stmt of RHS.
2793 <bb 9>:
2794 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2795 g_67.1_6 = prephitmp.12_36;
2796 goto <bb 9>;
2797 While this is strictly all dead code we do not want to
2798 deal with this here. */
2799 if (TREE_CODE (rhs) == SSA_NAME
2800 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2802 all = false;
2803 continue;
2806 /* Dump details. */
2807 if (dump_file && (dump_flags & TDF_DETAILS))
2809 fprintf (dump_file, " Original statement:");
2810 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2813 /* Propagate the RHS into this use of the LHS. */
2814 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2815 propagate_value (use_p, rhs);
2817 /* Special cases to avoid useless calls into the folding
2818 routines, operand scanning, etc.
2820 Propagation into a PHI may cause the PHI to become
2821 a degenerate, so mark the PHI as interesting. No other
2822 actions are necessary. */
2823 if (gimple_code (use_stmt) == GIMPLE_PHI)
2825 tree result;
2827 /* Dump details. */
2828 if (dump_file && (dump_flags & TDF_DETAILS))
2830 fprintf (dump_file, " Updated statement:");
2831 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2834 result = get_lhs_or_phi_result (use_stmt);
2835 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2836 continue;
2839 /* From this point onward we are propagating into a
2840 real statement. Folding may (or may not) be possible,
2841 we may expose new operands, expose dead EH edges,
2842 etc. */
2843 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2844 cannot fold a call that simplifies to a constant,
2845 because the GIMPLE_CALL must be replaced by a
2846 GIMPLE_ASSIGN, and there is no way to effect such a
2847 transformation in-place. We might want to consider
2848 using the more general fold_stmt here. */
2850 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2851 fold_stmt_inplace (&gsi);
2854 /* Sometimes propagation can expose new operands to the
2855 renamer. */
2856 update_stmt (use_stmt);
2858 /* Dump details. */
2859 if (dump_file && (dump_flags & TDF_DETAILS))
2861 fprintf (dump_file, " Updated statement:");
2862 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2865 /* If we replaced a variable index with a constant, then
2866 we would need to update the invariant flag for ADDR_EXPRs. */
2867 if (gimple_assign_single_p (use_stmt)
2868 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2869 recompute_tree_invariant_for_addr_expr
2870 (gimple_assign_rhs1 (use_stmt));
2872 /* If we cleaned up EH information from the statement,
2873 mark its containing block as needing EH cleanups. */
2874 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2876 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2877 if (dump_file && (dump_flags & TDF_DETAILS))
2878 fprintf (dump_file, " Flagged to clear EH edges.\n");
2881 /* Propagation may expose new trivial copy/constant propagation
2882 opportunities. */
2883 if (gimple_assign_single_p (use_stmt)
2884 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2885 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2886 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2888 tree result = get_lhs_or_phi_result (use_stmt);
2889 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2892 /* Propagation into these nodes may make certain edges in
2893 the CFG unexecutable. We want to identify them as PHI nodes
2894 at the destination of those unexecutable edges may become
2895 degenerates. */
2896 else if (gimple_code (use_stmt) == GIMPLE_COND
2897 || gimple_code (use_stmt) == GIMPLE_SWITCH
2898 || gimple_code (use_stmt) == GIMPLE_GOTO)
2900 tree val;
2902 if (gimple_code (use_stmt) == GIMPLE_COND)
2903 val = fold_binary_loc (gimple_location (use_stmt),
2904 gimple_cond_code (use_stmt),
2905 boolean_type_node,
2906 gimple_cond_lhs (use_stmt),
2907 gimple_cond_rhs (use_stmt));
2908 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2909 val = gimple_switch_index (as_a <gswitch *> (use_stmt));
2910 else
2911 val = gimple_goto_dest (use_stmt);
2913 if (val && is_gimple_min_invariant (val))
2915 basic_block bb = gimple_bb (use_stmt);
2916 edge te = find_taken_edge (bb, val);
2917 if (!te)
2918 continue;
2920 edge_iterator ei;
2921 edge e;
2922 gimple_stmt_iterator gsi;
2923 gphi_iterator psi;
2925 /* Remove all outgoing edges except TE. */
2926 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2928 if (e != te)
2930 /* Mark all the PHI nodes at the destination of
2931 the unexecutable edge as interesting. */
2932 for (psi = gsi_start_phis (e->dest);
2933 !gsi_end_p (psi);
2934 gsi_next (&psi))
2936 gphi *phi = psi.phi ();
2938 tree result = gimple_phi_result (phi);
2939 int version = SSA_NAME_VERSION (result);
2941 bitmap_set_bit (interesting_names, version);
2944 te->probability += e->probability;
2946 te->count += e->count;
2947 remove_edge (e);
2948 cfg_altered = true;
2950 else
2951 ei_next (&ei);
2954 gsi = gsi_last_bb (gimple_bb (use_stmt));
2955 gsi_remove (&gsi, true);
2957 /* And fixup the flags on the single remaining edge. */
2958 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2959 te->flags &= ~EDGE_ABNORMAL;
2960 te->flags |= EDGE_FALLTHRU;
2961 if (te->probability > REG_BR_PROB_BASE)
2962 te->probability = REG_BR_PROB_BASE;
2967 /* Ensure there is nothing else to do. */
2968 gcc_assert (!all || has_zero_uses (lhs));
2970 /* If we were able to propagate away all uses of LHS, then
2971 we can remove STMT. */
2972 if (all)
2973 remove_stmt_or_phi (stmt);
2977 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2978 a statement that is a trivial copy or constant initialization.
2980 Attempt to eliminate T by propagating its RHS into all uses of
2981 its LHS. This may in turn set new bits in INTERESTING_NAMES
2982 for nodes we want to revisit later.
2984 All exit paths should clear INTERESTING_NAMES for the result
2985 of STMT. */
2987 static void
2988 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2990 tree lhs = get_lhs_or_phi_result (stmt);
2991 tree rhs;
2992 int version = SSA_NAME_VERSION (lhs);
2994 /* If the LHS of this statement or PHI has no uses, then we can
2995 just eliminate it. This can occur if, for example, the PHI
2996 was created by block duplication due to threading and its only
2997 use was in the conditional at the end of the block which was
2998 deleted. */
2999 if (has_zero_uses (lhs))
3001 bitmap_clear_bit (interesting_names, version);
3002 remove_stmt_or_phi (stmt);
3003 return;
3006 /* Get the RHS of the assignment or PHI node if the PHI is a
3007 degenerate. */
3008 rhs = get_rhs_or_phi_arg (stmt);
3009 if (!rhs)
3011 bitmap_clear_bit (interesting_names, version);
3012 return;
3015 if (!virtual_operand_p (lhs))
3016 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
3017 else
3019 gimple use_stmt;
3020 imm_use_iterator iter;
3021 use_operand_p use_p;
3022 /* For virtual operands we have to propagate into all uses as
3023 otherwise we will create overlapping life-ranges. */
3024 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3025 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3026 SET_USE (use_p, rhs);
3027 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3028 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
3029 remove_stmt_or_phi (stmt);
3032 /* Note that STMT may well have been deleted by now, so do
3033 not access it, instead use the saved version # to clear
3034 T's entry in the worklist. */
3035 bitmap_clear_bit (interesting_names, version);
3038 /* The first phase in degenerate PHI elimination.
3040 Eliminate the degenerate PHIs in BB, then recurse on the
3041 dominator children of BB. */
3043 static void
3044 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
3046 gphi_iterator gsi;
3047 basic_block son;
3049 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3051 gphi *phi = gsi.phi ();
3053 eliminate_const_or_copy (phi, interesting_names);
3056 /* Recurse into the dominator children of BB. */
3057 for (son = first_dom_son (CDI_DOMINATORS, bb);
3058 son;
3059 son = next_dom_son (CDI_DOMINATORS, son))
3060 eliminate_degenerate_phis_1 (son, interesting_names);
3064 /* A very simple pass to eliminate degenerate PHI nodes from the
3065 IL. This is meant to be fast enough to be able to be run several
3066 times in the optimization pipeline.
3068 Certain optimizations, particularly those which duplicate blocks
3069 or remove edges from the CFG can create or expose PHIs which are
3070 trivial copies or constant initializations.
3072 While we could pick up these optimizations in DOM or with the
3073 combination of copy-prop and CCP, those solutions are far too
3074 heavy-weight for our needs.
3076 This implementation has two phases so that we can efficiently
3077 eliminate the first order degenerate PHIs and second order
3078 degenerate PHIs.
3080 The first phase performs a dominator walk to identify and eliminate
3081 the vast majority of the degenerate PHIs. When a degenerate PHI
3082 is identified and eliminated any affected statements or PHIs
3083 are put on a worklist.
3085 The second phase eliminates degenerate PHIs and trivial copies
3086 or constant initializations using the worklist. This is how we
3087 pick up the secondary optimization opportunities with minimal
3088 cost. */
3090 namespace {
3092 const pass_data pass_data_phi_only_cprop =
3094 GIMPLE_PASS, /* type */
3095 "phicprop", /* name */
3096 OPTGROUP_NONE, /* optinfo_flags */
3097 TV_TREE_PHI_CPROP, /* tv_id */
3098 ( PROP_cfg | PROP_ssa ), /* properties_required */
3099 0, /* properties_provided */
3100 0, /* properties_destroyed */
3101 0, /* todo_flags_start */
3102 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
3105 class pass_phi_only_cprop : public gimple_opt_pass
3107 public:
3108 pass_phi_only_cprop (gcc::context *ctxt)
3109 : gimple_opt_pass (pass_data_phi_only_cprop, ctxt)
3112 /* opt_pass methods: */
3113 opt_pass * clone () { return new pass_phi_only_cprop (m_ctxt); }
3114 virtual bool gate (function *) { return flag_tree_dom != 0; }
3115 virtual unsigned int execute (function *);
3117 }; // class pass_phi_only_cprop
3119 unsigned int
3120 pass_phi_only_cprop::execute (function *fun)
3122 bitmap interesting_names;
3123 bitmap interesting_names1;
3125 /* Bitmap of blocks which need EH information updated. We can not
3126 update it on-the-fly as doing so invalidates the dominator tree. */
3127 need_eh_cleanup = BITMAP_ALLOC (NULL);
3129 /* INTERESTING_NAMES is effectively our worklist, indexed by
3130 SSA_NAME_VERSION.
3132 A set bit indicates that the statement or PHI node which
3133 defines the SSA_NAME should be (re)examined to determine if
3134 it has become a degenerate PHI or trivial const/copy propagation
3135 opportunity.
3137 Experiments have show we generally get better compilation
3138 time behavior with bitmaps rather than sbitmaps. */
3139 interesting_names = BITMAP_ALLOC (NULL);
3140 interesting_names1 = BITMAP_ALLOC (NULL);
3142 calculate_dominance_info (CDI_DOMINATORS);
3143 cfg_altered = false;
3145 /* First phase. Eliminate degenerate PHIs via a dominator
3146 walk of the CFG.
3148 Experiments have indicated that we generally get better
3149 compile-time behavior by visiting blocks in the first
3150 phase in dominator order. Presumably this is because walking
3151 in dominator order leaves fewer PHIs for later examination
3152 by the worklist phase. */
3153 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR_FOR_FN (fun),
3154 interesting_names);
3156 /* Second phase. Eliminate second order degenerate PHIs as well
3157 as trivial copies or constant initializations identified by
3158 the first phase or this phase. Basically we keep iterating
3159 until our set of INTERESTING_NAMEs is empty. */
3160 while (!bitmap_empty_p (interesting_names))
3162 unsigned int i;
3163 bitmap_iterator bi;
3165 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3166 changed during the loop. Copy it to another bitmap and
3167 use that. */
3168 bitmap_copy (interesting_names1, interesting_names);
3170 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3172 tree name = ssa_name (i);
3174 /* Ignore SSA_NAMEs that have been released because
3175 their defining statement was deleted (unreachable). */
3176 if (name)
3177 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3178 interesting_names);
3182 if (cfg_altered)
3184 free_dominance_info (CDI_DOMINATORS);
3185 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3186 loops_state_set (LOOPS_NEED_FIXUP);
3189 /* Propagation of const and copies may make some EH edges dead. Purge
3190 such edges from the CFG as needed. */
3191 if (!bitmap_empty_p (need_eh_cleanup))
3193 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3194 BITMAP_FREE (need_eh_cleanup);
3197 BITMAP_FREE (interesting_names);
3198 BITMAP_FREE (interesting_names1);
3199 return 0;
3202 } // anon namespace
3204 gimple_opt_pass *
3205 make_pass_phi_only_cprop (gcc::context *ctxt)
3207 return new pass_phi_only_cprop (ctxt);