[PATCH] Another small cleanup to the const_and_copies stack
[official-gcc.git] / gcc / tree-ssa-dom.c
blobe3eb0dbd54bb5822b0422405080adfbad3c44a71
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 "backend.h"
25 #include "cfghooks.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "hard-reg-set.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "flags.h"
34 #include "tm_p.h"
35 #include "cfganal.h"
36 #include "cfgloop.h"
37 #include "gimple-pretty-print.h"
38 #include "internal-fn.h"
39 #include "gimple-fold.h"
40 #include "tree-eh.h"
41 #include "gimple-iterator.h"
42 #include "tree-cfg.h"
43 #include "tree-into-ssa.h"
44 #include "domwalk.h"
45 #include "tree-pass.h"
46 #include "tree-ssa-propagate.h"
47 #include "tree-ssa-threadupdate.h"
48 #include "langhooks.h"
49 #include "params.h"
50 #include "tree-ssa-scopedtables.h"
51 #include "tree-ssa-threadedge.h"
52 #include "tree-ssa-dom.h"
53 #include "gimplify.h"
54 #include "tree-cfgcleanup.h"
56 /* This file implements optimizations on the dominator tree. */
58 /* Representation of a "naked" right-hand-side expression, to be used
59 in recording available expressions in the expression hash table. */
61 enum expr_kind
63 EXPR_SINGLE,
64 EXPR_UNARY,
65 EXPR_BINARY,
66 EXPR_TERNARY,
67 EXPR_CALL,
68 EXPR_PHI
71 struct hashable_expr
73 tree type;
74 enum expr_kind kind;
75 union {
76 struct { tree rhs; } single;
77 struct { enum tree_code op; tree opnd; } unary;
78 struct { enum tree_code op; tree opnd0, opnd1; } binary;
79 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
80 struct { gcall *fn_from; bool pure; size_t nargs; tree *args; } call;
81 struct { size_t nargs; tree *args; } phi;
82 } ops;
85 /* Structure for recording known values of a conditional expression
86 at the exits from its block. */
88 struct cond_equivalence
90 struct hashable_expr cond;
91 tree value;
95 /* Structure for recording edge equivalences.
97 Computing and storing the edge equivalences instead of creating
98 them on-demand can save significant amounts of time, particularly
99 for pathological cases involving switch statements.
101 These structures live for a single iteration of the dominator
102 optimizer in the edge's AUX field. At the end of an iteration we
103 free each of these structures. */
105 struct edge_info
107 /* If this edge creates a simple equivalence, the LHS and RHS of
108 the equivalence will be stored here. */
109 tree lhs;
110 tree rhs;
112 /* Traversing an edge may also indicate one or more particular conditions
113 are true or false. */
114 vec<cond_equivalence> cond_equivalences;
117 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
118 expressions it enters into the hash table along with a marker entry
119 (null). When we finish processing the block, we pop off entries and
120 remove the expressions from the global hash table until we hit the
121 marker. */
122 typedef struct expr_hash_elt * expr_hash_elt_t;
124 static vec<std::pair<expr_hash_elt_t, expr_hash_elt_t> > avail_exprs_stack;
126 /* Structure for entries in the expression hash table. */
128 struct expr_hash_elt
130 /* The value (lhs) of this expression. */
131 tree lhs;
133 /* The expression (rhs) we want to record. */
134 struct hashable_expr expr;
136 /* The virtual operand associated with the nearest dominating stmt
137 loading from or storing to expr. */
138 tree vop;
140 /* The hash value for RHS. */
141 hashval_t hash;
143 /* A unique stamp, typically the address of the hash
144 element itself, used in removing entries from the table. */
145 struct expr_hash_elt *stamp;
148 /* Hashtable helpers. */
150 static bool hashable_expr_equal_p (const struct hashable_expr *,
151 const struct hashable_expr *);
152 static void free_expr_hash_elt (void *);
154 struct expr_elt_hasher : pointer_hash <expr_hash_elt>
156 static inline hashval_t hash (const value_type &);
157 static inline bool equal (const value_type &, const compare_type &);
158 static inline void remove (value_type &);
161 inline hashval_t
162 expr_elt_hasher::hash (const value_type &p)
164 return p->hash;
167 inline bool
168 expr_elt_hasher::equal (const value_type &p1, const compare_type &p2)
170 const struct hashable_expr *expr1 = &p1->expr;
171 const struct expr_hash_elt *stamp1 = p1->stamp;
172 const struct hashable_expr *expr2 = &p2->expr;
173 const struct expr_hash_elt *stamp2 = p2->stamp;
175 /* This case should apply only when removing entries from the table. */
176 if (stamp1 == stamp2)
177 return true;
179 if (p1->hash != p2->hash)
180 return false;
182 /* In case of a collision, both RHS have to be identical and have the
183 same VUSE operands. */
184 if (hashable_expr_equal_p (expr1, expr2)
185 && types_compatible_p (expr1->type, expr2->type))
186 return true;
188 return false;
191 /* Delete an expr_hash_elt and reclaim its storage. */
193 inline void
194 expr_elt_hasher::remove (value_type &element)
196 free_expr_hash_elt (element);
199 /* Hash table with expressions made available during the renaming process.
200 When an assignment of the form X_i = EXPR is found, the statement is
201 stored in this table. If the same expression EXPR is later found on the
202 RHS of another statement, it is replaced with X_i (thus performing
203 global redundancy elimination). Similarly as we pass through conditionals
204 we record the conditional itself as having either a true or false value
205 in this table. */
206 static hash_table<expr_elt_hasher> *avail_exprs;
208 /* Unwindable const/copy equivalences. */
209 static const_and_copies *const_and_copies;
211 /* Track whether or not we have changed the control flow graph. */
212 static bool cfg_altered;
214 /* Bitmap of blocks that have had EH statements cleaned. We should
215 remove their dead edges eventually. */
216 static bitmap need_eh_cleanup;
217 static vec<gimple> need_noreturn_fixup;
219 /* Statistics for dominator optimizations. */
220 struct opt_stats_d
222 long num_stmts;
223 long num_exprs_considered;
224 long num_re;
225 long num_const_prop;
226 long num_copy_prop;
229 static struct opt_stats_d opt_stats;
231 /* Local functions. */
232 static void optimize_stmt (basic_block, gimple_stmt_iterator);
233 static tree lookup_avail_expr (gimple, bool);
234 static hashval_t avail_expr_hash (const void *);
235 static void htab_statistics (FILE *,
236 const hash_table<expr_elt_hasher> &);
237 static void record_cond (cond_equivalence *);
238 static void record_equality (tree, tree);
239 static void record_equivalences_from_phis (basic_block);
240 static void record_equivalences_from_incoming_edge (basic_block);
241 static void eliminate_redundant_computations (gimple_stmt_iterator *);
242 static void record_equivalences_from_stmt (gimple, int);
243 static void remove_local_expressions_from_table (void);
244 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
247 /* Given a statement STMT, initialize the hash table element pointed to
248 by ELEMENT. */
250 static void
251 initialize_hash_element (gimple stmt, tree lhs,
252 struct expr_hash_elt *element)
254 enum gimple_code code = gimple_code (stmt);
255 struct hashable_expr *expr = &element->expr;
257 if (code == GIMPLE_ASSIGN)
259 enum tree_code subcode = gimple_assign_rhs_code (stmt);
261 switch (get_gimple_rhs_class (subcode))
263 case GIMPLE_SINGLE_RHS:
264 expr->kind = EXPR_SINGLE;
265 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
266 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
267 break;
268 case GIMPLE_UNARY_RHS:
269 expr->kind = EXPR_UNARY;
270 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
271 if (CONVERT_EXPR_CODE_P (subcode))
272 subcode = NOP_EXPR;
273 expr->ops.unary.op = subcode;
274 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
275 break;
276 case GIMPLE_BINARY_RHS:
277 expr->kind = EXPR_BINARY;
278 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
279 expr->ops.binary.op = subcode;
280 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
281 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
282 break;
283 case GIMPLE_TERNARY_RHS:
284 expr->kind = EXPR_TERNARY;
285 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
286 expr->ops.ternary.op = subcode;
287 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
288 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
289 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
290 break;
291 default:
292 gcc_unreachable ();
295 else if (code == GIMPLE_COND)
297 expr->type = boolean_type_node;
298 expr->kind = EXPR_BINARY;
299 expr->ops.binary.op = gimple_cond_code (stmt);
300 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
301 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
303 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
305 size_t nargs = gimple_call_num_args (call_stmt);
306 size_t i;
308 gcc_assert (gimple_call_lhs (call_stmt));
310 expr->type = TREE_TYPE (gimple_call_lhs (call_stmt));
311 expr->kind = EXPR_CALL;
312 expr->ops.call.fn_from = call_stmt;
314 if (gimple_call_flags (call_stmt) & (ECF_CONST | ECF_PURE))
315 expr->ops.call.pure = true;
316 else
317 expr->ops.call.pure = false;
319 expr->ops.call.nargs = nargs;
320 expr->ops.call.args = XCNEWVEC (tree, nargs);
321 for (i = 0; i < nargs; i++)
322 expr->ops.call.args[i] = gimple_call_arg (call_stmt, i);
324 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
326 expr->type = TREE_TYPE (gimple_switch_index (swtch_stmt));
327 expr->kind = EXPR_SINGLE;
328 expr->ops.single.rhs = gimple_switch_index (swtch_stmt);
330 else if (code == GIMPLE_GOTO)
332 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
333 expr->kind = EXPR_SINGLE;
334 expr->ops.single.rhs = gimple_goto_dest (stmt);
336 else if (code == GIMPLE_PHI)
338 size_t nargs = gimple_phi_num_args (stmt);
339 size_t i;
341 expr->type = TREE_TYPE (gimple_phi_result (stmt));
342 expr->kind = EXPR_PHI;
343 expr->ops.phi.nargs = nargs;
344 expr->ops.phi.args = XCNEWVEC (tree, nargs);
346 for (i = 0; i < nargs; i++)
347 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
349 else
350 gcc_unreachable ();
352 element->lhs = lhs;
353 element->vop = gimple_vuse (stmt);
354 element->hash = avail_expr_hash (element);
355 element->stamp = element;
358 /* Given a conditional expression COND as a tree, initialize
359 a hashable_expr expression EXPR. The conditional must be a
360 comparison or logical negation. A constant or a variable is
361 not permitted. */
363 static void
364 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
366 expr->type = boolean_type_node;
368 if (COMPARISON_CLASS_P (cond))
370 expr->kind = EXPR_BINARY;
371 expr->ops.binary.op = TREE_CODE (cond);
372 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
373 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
375 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
377 expr->kind = EXPR_UNARY;
378 expr->ops.unary.op = TRUTH_NOT_EXPR;
379 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
381 else
382 gcc_unreachable ();
385 /* Given a hashable_expr expression EXPR and an LHS,
386 initialize the hash table element pointed to by ELEMENT. */
388 static void
389 initialize_hash_element_from_expr (struct hashable_expr *expr,
390 tree lhs,
391 struct expr_hash_elt *element)
393 element->expr = *expr;
394 element->lhs = lhs;
395 element->vop = NULL_TREE;
396 element->hash = avail_expr_hash (element);
397 element->stamp = element;
400 /* Compare two hashable_expr structures for equivalence. They are
401 considered equivalent when the expressions they denote must
402 necessarily be equal. The logic is intended to follow that of
403 operand_equal_p in fold-const.c */
405 static bool
406 hashable_expr_equal_p (const struct hashable_expr *expr0,
407 const struct hashable_expr *expr1)
409 tree type0 = expr0->type;
410 tree type1 = expr1->type;
412 /* If either type is NULL, there is nothing to check. */
413 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
414 return false;
416 /* If both types don't have the same signedness, precision, and mode,
417 then we can't consider them equal. */
418 if (type0 != type1
419 && (TREE_CODE (type0) == ERROR_MARK
420 || TREE_CODE (type1) == ERROR_MARK
421 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
422 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
423 || TYPE_MODE (type0) != TYPE_MODE (type1)))
424 return false;
426 if (expr0->kind != expr1->kind)
427 return false;
429 switch (expr0->kind)
431 case EXPR_SINGLE:
432 return operand_equal_p (expr0->ops.single.rhs,
433 expr1->ops.single.rhs, 0);
435 case EXPR_UNARY:
436 if (expr0->ops.unary.op != expr1->ops.unary.op)
437 return false;
439 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
440 || expr0->ops.unary.op == NON_LVALUE_EXPR)
441 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
442 return false;
444 return operand_equal_p (expr0->ops.unary.opnd,
445 expr1->ops.unary.opnd, 0);
447 case EXPR_BINARY:
448 if (expr0->ops.binary.op != expr1->ops.binary.op)
449 return false;
451 if (operand_equal_p (expr0->ops.binary.opnd0,
452 expr1->ops.binary.opnd0, 0)
453 && operand_equal_p (expr0->ops.binary.opnd1,
454 expr1->ops.binary.opnd1, 0))
455 return true;
457 /* For commutative ops, allow the other order. */
458 return (commutative_tree_code (expr0->ops.binary.op)
459 && operand_equal_p (expr0->ops.binary.opnd0,
460 expr1->ops.binary.opnd1, 0)
461 && operand_equal_p (expr0->ops.binary.opnd1,
462 expr1->ops.binary.opnd0, 0));
464 case EXPR_TERNARY:
465 if (expr0->ops.ternary.op != expr1->ops.ternary.op
466 || !operand_equal_p (expr0->ops.ternary.opnd2,
467 expr1->ops.ternary.opnd2, 0))
468 return false;
470 if (operand_equal_p (expr0->ops.ternary.opnd0,
471 expr1->ops.ternary.opnd0, 0)
472 && operand_equal_p (expr0->ops.ternary.opnd1,
473 expr1->ops.ternary.opnd1, 0))
474 return true;
476 /* For commutative ops, allow the other order. */
477 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
478 && operand_equal_p (expr0->ops.ternary.opnd0,
479 expr1->ops.ternary.opnd1, 0)
480 && operand_equal_p (expr0->ops.ternary.opnd1,
481 expr1->ops.ternary.opnd0, 0));
483 case EXPR_CALL:
485 size_t i;
487 /* If the calls are to different functions, then they
488 clearly cannot be equal. */
489 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
490 expr1->ops.call.fn_from))
491 return false;
493 if (! expr0->ops.call.pure)
494 return false;
496 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
497 return false;
499 for (i = 0; i < expr0->ops.call.nargs; i++)
500 if (! operand_equal_p (expr0->ops.call.args[i],
501 expr1->ops.call.args[i], 0))
502 return false;
504 if (stmt_could_throw_p (expr0->ops.call.fn_from))
506 int lp0 = lookup_stmt_eh_lp (expr0->ops.call.fn_from);
507 int lp1 = lookup_stmt_eh_lp (expr1->ops.call.fn_from);
508 if ((lp0 > 0 || lp1 > 0) && lp0 != lp1)
509 return false;
512 return true;
515 case EXPR_PHI:
517 size_t i;
519 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
520 return false;
522 for (i = 0; i < expr0->ops.phi.nargs; i++)
523 if (! operand_equal_p (expr0->ops.phi.args[i],
524 expr1->ops.phi.args[i], 0))
525 return false;
527 return true;
530 default:
531 gcc_unreachable ();
535 /* Generate a hash value for a pair of expressions. This can be used
536 iteratively by passing a previous result in HSTATE.
538 The same hash value is always returned for a given pair of expressions,
539 regardless of the order in which they are presented. This is useful in
540 hashing the operands of commutative functions. */
542 namespace inchash
545 static void
546 add_expr_commutative (const_tree t1, const_tree t2, hash &hstate)
548 hash one, two;
550 inchash::add_expr (t1, one);
551 inchash::add_expr (t2, two);
552 hstate.add_commutative (one, two);
555 /* Compute a hash value for a hashable_expr value EXPR and a
556 previously accumulated hash value VAL. If two hashable_expr
557 values compare equal with hashable_expr_equal_p, they must
558 hash to the same value, given an identical value of VAL.
559 The logic is intended to follow inchash::add_expr in tree.c. */
561 static void
562 add_hashable_expr (const struct hashable_expr *expr, hash &hstate)
564 switch (expr->kind)
566 case EXPR_SINGLE:
567 inchash::add_expr (expr->ops.single.rhs, hstate);
568 break;
570 case EXPR_UNARY:
571 hstate.add_object (expr->ops.unary.op);
573 /* Make sure to include signedness in the hash computation.
574 Don't hash the type, that can lead to having nodes which
575 compare equal according to operand_equal_p, but which
576 have different hash codes. */
577 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
578 || expr->ops.unary.op == NON_LVALUE_EXPR)
579 hstate.add_int (TYPE_UNSIGNED (expr->type));
581 inchash::add_expr (expr->ops.unary.opnd, hstate);
582 break;
584 case EXPR_BINARY:
585 hstate.add_object (expr->ops.binary.op);
586 if (commutative_tree_code (expr->ops.binary.op))
587 inchash::add_expr_commutative (expr->ops.binary.opnd0,
588 expr->ops.binary.opnd1, hstate);
589 else
591 inchash::add_expr (expr->ops.binary.opnd0, hstate);
592 inchash::add_expr (expr->ops.binary.opnd1, hstate);
594 break;
596 case EXPR_TERNARY:
597 hstate.add_object (expr->ops.ternary.op);
598 if (commutative_ternary_tree_code (expr->ops.ternary.op))
599 inchash::add_expr_commutative (expr->ops.ternary.opnd0,
600 expr->ops.ternary.opnd1, hstate);
601 else
603 inchash::add_expr (expr->ops.ternary.opnd0, hstate);
604 inchash::add_expr (expr->ops.ternary.opnd1, hstate);
606 inchash::add_expr (expr->ops.ternary.opnd2, hstate);
607 break;
609 case EXPR_CALL:
611 size_t i;
612 enum tree_code code = CALL_EXPR;
613 gcall *fn_from;
615 hstate.add_object (code);
616 fn_from = expr->ops.call.fn_from;
617 if (gimple_call_internal_p (fn_from))
618 hstate.merge_hash ((hashval_t) gimple_call_internal_fn (fn_from));
619 else
620 inchash::add_expr (gimple_call_fn (fn_from), hstate);
621 for (i = 0; i < expr->ops.call.nargs; i++)
622 inchash::add_expr (expr->ops.call.args[i], hstate);
624 break;
626 case EXPR_PHI:
628 size_t i;
630 for (i = 0; i < expr->ops.phi.nargs; i++)
631 inchash::add_expr (expr->ops.phi.args[i], hstate);
633 break;
635 default:
636 gcc_unreachable ();
642 /* Print a diagnostic dump of an expression hash table entry. */
644 static void
645 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
647 fprintf (stream, "STMT ");
649 if (element->lhs)
651 print_generic_expr (stream, element->lhs, 0);
652 fprintf (stream, " = ");
655 switch (element->expr.kind)
657 case EXPR_SINGLE:
658 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
659 break;
661 case EXPR_UNARY:
662 fprintf (stream, "%s ", get_tree_code_name (element->expr.ops.unary.op));
663 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
664 break;
666 case EXPR_BINARY:
667 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
668 fprintf (stream, " %s ", get_tree_code_name (element->expr.ops.binary.op));
669 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
670 break;
672 case EXPR_TERNARY:
673 fprintf (stream, " %s <", get_tree_code_name (element->expr.ops.ternary.op));
674 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
675 fputs (", ", stream);
676 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
677 fputs (", ", stream);
678 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
679 fputs (">", stream);
680 break;
682 case EXPR_CALL:
684 size_t i;
685 size_t nargs = element->expr.ops.call.nargs;
686 gcall *fn_from;
688 fn_from = element->expr.ops.call.fn_from;
689 if (gimple_call_internal_p (fn_from))
690 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
691 stream);
692 else
693 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
694 fprintf (stream, " (");
695 for (i = 0; i < nargs; i++)
697 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
698 if (i + 1 < nargs)
699 fprintf (stream, ", ");
701 fprintf (stream, ")");
703 break;
705 case EXPR_PHI:
707 size_t i;
708 size_t nargs = element->expr.ops.phi.nargs;
710 fprintf (stream, "PHI <");
711 for (i = 0; i < nargs; i++)
713 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
714 if (i + 1 < nargs)
715 fprintf (stream, ", ");
717 fprintf (stream, ">");
719 break;
722 if (element->vop)
724 fprintf (stream, " with ");
725 print_generic_expr (stream, element->vop, 0);
728 fprintf (stream, "\n");
731 /* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
733 static void
734 free_expr_hash_elt_contents (struct expr_hash_elt *element)
736 if (element->expr.kind == EXPR_CALL)
737 free (element->expr.ops.call.args);
738 else if (element->expr.kind == EXPR_PHI)
739 free (element->expr.ops.phi.args);
742 /* Delete an expr_hash_elt and reclaim its storage. */
744 static void
745 free_expr_hash_elt (void *elt)
747 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
748 free_expr_hash_elt_contents (element);
749 free (element);
752 /* Allocate an EDGE_INFO for edge E and attach it to E.
753 Return the new EDGE_INFO structure. */
755 static struct edge_info *
756 allocate_edge_info (edge e)
758 struct edge_info *edge_info;
760 edge_info = XCNEW (struct edge_info);
762 e->aux = edge_info;
763 return edge_info;
766 /* Free all EDGE_INFO structures associated with edges in the CFG.
767 If a particular edge can be threaded, copy the redirection
768 target from the EDGE_INFO structure into the edge's AUX field
769 as required by code to update the CFG and SSA graph for
770 jump threading. */
772 static void
773 free_all_edge_infos (void)
775 basic_block bb;
776 edge_iterator ei;
777 edge e;
779 FOR_EACH_BB_FN (bb, cfun)
781 FOR_EACH_EDGE (e, ei, bb->preds)
783 struct edge_info *edge_info = (struct edge_info *) e->aux;
785 if (edge_info)
787 edge_info->cond_equivalences.release ();
788 free (edge_info);
789 e->aux = NULL;
795 /* Build a cond_equivalence record indicating that the comparison
796 CODE holds between operands OP0 and OP1 and push it to **P. */
798 static void
799 build_and_record_new_cond (enum tree_code code,
800 tree op0, tree op1,
801 vec<cond_equivalence> *p,
802 bool val = true)
804 cond_equivalence c;
805 struct hashable_expr *cond = &c.cond;
807 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
809 cond->type = boolean_type_node;
810 cond->kind = EXPR_BINARY;
811 cond->ops.binary.op = code;
812 cond->ops.binary.opnd0 = op0;
813 cond->ops.binary.opnd1 = op1;
815 c.value = val ? boolean_true_node : boolean_false_node;
816 p->safe_push (c);
819 /* Record that COND is true and INVERTED is false into the edge information
820 structure. Also record that any conditions dominated by COND are true
821 as well.
823 For example, if a < b is true, then a <= b must also be true. */
825 static void
826 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
828 tree op0, op1;
829 cond_equivalence c;
831 if (!COMPARISON_CLASS_P (cond))
832 return;
834 op0 = TREE_OPERAND (cond, 0);
835 op1 = TREE_OPERAND (cond, 1);
837 switch (TREE_CODE (cond))
839 case LT_EXPR:
840 case GT_EXPR:
841 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
843 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
844 &edge_info->cond_equivalences);
845 build_and_record_new_cond (LTGT_EXPR, op0, op1,
846 &edge_info->cond_equivalences);
849 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
850 ? LE_EXPR : GE_EXPR),
851 op0, op1, &edge_info->cond_equivalences);
852 build_and_record_new_cond (NE_EXPR, op0, op1,
853 &edge_info->cond_equivalences);
854 build_and_record_new_cond (EQ_EXPR, op0, op1,
855 &edge_info->cond_equivalences, false);
856 break;
858 case GE_EXPR:
859 case LE_EXPR:
860 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
862 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
863 &edge_info->cond_equivalences);
865 break;
867 case EQ_EXPR:
868 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
870 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
871 &edge_info->cond_equivalences);
873 build_and_record_new_cond (LE_EXPR, op0, op1,
874 &edge_info->cond_equivalences);
875 build_and_record_new_cond (GE_EXPR, op0, op1,
876 &edge_info->cond_equivalences);
877 break;
879 case UNORDERED_EXPR:
880 build_and_record_new_cond (NE_EXPR, op0, op1,
881 &edge_info->cond_equivalences);
882 build_and_record_new_cond (UNLE_EXPR, op0, op1,
883 &edge_info->cond_equivalences);
884 build_and_record_new_cond (UNGE_EXPR, op0, op1,
885 &edge_info->cond_equivalences);
886 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
887 &edge_info->cond_equivalences);
888 build_and_record_new_cond (UNLT_EXPR, op0, op1,
889 &edge_info->cond_equivalences);
890 build_and_record_new_cond (UNGT_EXPR, op0, op1,
891 &edge_info->cond_equivalences);
892 break;
894 case UNLT_EXPR:
895 case UNGT_EXPR:
896 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
897 ? UNLE_EXPR : UNGE_EXPR),
898 op0, op1, &edge_info->cond_equivalences);
899 build_and_record_new_cond (NE_EXPR, op0, op1,
900 &edge_info->cond_equivalences);
901 break;
903 case UNEQ_EXPR:
904 build_and_record_new_cond (UNLE_EXPR, op0, op1,
905 &edge_info->cond_equivalences);
906 build_and_record_new_cond (UNGE_EXPR, op0, op1,
907 &edge_info->cond_equivalences);
908 break;
910 case LTGT_EXPR:
911 build_and_record_new_cond (NE_EXPR, op0, op1,
912 &edge_info->cond_equivalences);
913 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
914 &edge_info->cond_equivalences);
915 break;
917 default:
918 break;
921 /* Now store the original true and false conditions into the first
922 two slots. */
923 initialize_expr_from_cond (cond, &c.cond);
924 c.value = boolean_true_node;
925 edge_info->cond_equivalences.safe_push (c);
927 /* It is possible for INVERTED to be the negation of a comparison,
928 and not a valid RHS or GIMPLE_COND condition. This happens because
929 invert_truthvalue may return such an expression when asked to invert
930 a floating-point comparison. These comparisons are not assumed to
931 obey the trichotomy law. */
932 initialize_expr_from_cond (inverted, &c.cond);
933 c.value = boolean_false_node;
934 edge_info->cond_equivalences.safe_push (c);
937 /* We have finished optimizing BB, record any information implied by
938 taking a specific outgoing edge from BB. */
940 static void
941 record_edge_info (basic_block bb)
943 gimple_stmt_iterator gsi = gsi_last_bb (bb);
944 struct edge_info *edge_info;
946 if (! gsi_end_p (gsi))
948 gimple stmt = gsi_stmt (gsi);
949 location_t loc = gimple_location (stmt);
951 if (gimple_code (stmt) == GIMPLE_SWITCH)
953 gswitch *switch_stmt = as_a <gswitch *> (stmt);
954 tree index = gimple_switch_index (switch_stmt);
956 if (TREE_CODE (index) == SSA_NAME)
958 int i;
959 int n_labels = gimple_switch_num_labels (switch_stmt);
960 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
961 edge e;
962 edge_iterator ei;
964 for (i = 0; i < n_labels; i++)
966 tree label = gimple_switch_label (switch_stmt, i);
967 basic_block target_bb = label_to_block (CASE_LABEL (label));
968 if (CASE_HIGH (label)
969 || !CASE_LOW (label)
970 || info[target_bb->index])
971 info[target_bb->index] = error_mark_node;
972 else
973 info[target_bb->index] = label;
976 FOR_EACH_EDGE (e, ei, bb->succs)
978 basic_block target_bb = e->dest;
979 tree label = info[target_bb->index];
981 if (label != NULL && label != error_mark_node)
983 tree x = fold_convert_loc (loc, TREE_TYPE (index),
984 CASE_LOW (label));
985 edge_info = allocate_edge_info (e);
986 edge_info->lhs = index;
987 edge_info->rhs = x;
990 free (info);
994 /* A COND_EXPR may create equivalences too. */
995 if (gimple_code (stmt) == GIMPLE_COND)
997 edge true_edge;
998 edge false_edge;
1000 tree op0 = gimple_cond_lhs (stmt);
1001 tree op1 = gimple_cond_rhs (stmt);
1002 enum tree_code code = gimple_cond_code (stmt);
1004 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1006 /* Special case comparing booleans against a constant as we
1007 know the value of OP0 on both arms of the branch. i.e., we
1008 can record an equivalence for OP0 rather than COND. */
1009 if ((code == EQ_EXPR || code == NE_EXPR)
1010 && TREE_CODE (op0) == SSA_NAME
1011 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1012 && is_gimple_min_invariant (op1))
1014 if (code == EQ_EXPR)
1016 edge_info = allocate_edge_info (true_edge);
1017 edge_info->lhs = op0;
1018 edge_info->rhs = (integer_zerop (op1)
1019 ? boolean_false_node
1020 : boolean_true_node);
1022 edge_info = allocate_edge_info (false_edge);
1023 edge_info->lhs = op0;
1024 edge_info->rhs = (integer_zerop (op1)
1025 ? boolean_true_node
1026 : boolean_false_node);
1028 else
1030 edge_info = allocate_edge_info (true_edge);
1031 edge_info->lhs = op0;
1032 edge_info->rhs = (integer_zerop (op1)
1033 ? boolean_true_node
1034 : boolean_false_node);
1036 edge_info = allocate_edge_info (false_edge);
1037 edge_info->lhs = op0;
1038 edge_info->rhs = (integer_zerop (op1)
1039 ? boolean_false_node
1040 : boolean_true_node);
1043 else if (is_gimple_min_invariant (op0)
1044 && (TREE_CODE (op1) == SSA_NAME
1045 || is_gimple_min_invariant (op1)))
1047 tree cond = build2 (code, boolean_type_node, op0, op1);
1048 tree inverted = invert_truthvalue_loc (loc, cond);
1049 bool can_infer_simple_equiv
1050 = !(HONOR_SIGNED_ZEROS (op0)
1051 && real_zerop (op0));
1052 struct edge_info *edge_info;
1054 edge_info = allocate_edge_info (true_edge);
1055 record_conditions (edge_info, cond, inverted);
1057 if (can_infer_simple_equiv && code == EQ_EXPR)
1059 edge_info->lhs = op1;
1060 edge_info->rhs = op0;
1063 edge_info = allocate_edge_info (false_edge);
1064 record_conditions (edge_info, inverted, cond);
1066 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1068 edge_info->lhs = op1;
1069 edge_info->rhs = op0;
1073 else if (TREE_CODE (op0) == SSA_NAME
1074 && (TREE_CODE (op1) == SSA_NAME
1075 || is_gimple_min_invariant (op1)))
1077 tree cond = build2 (code, boolean_type_node, op0, op1);
1078 tree inverted = invert_truthvalue_loc (loc, cond);
1079 bool can_infer_simple_equiv
1080 = !(HONOR_SIGNED_ZEROS (op1)
1081 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1082 struct edge_info *edge_info;
1084 edge_info = allocate_edge_info (true_edge);
1085 record_conditions (edge_info, cond, inverted);
1087 if (can_infer_simple_equiv && code == EQ_EXPR)
1089 edge_info->lhs = op0;
1090 edge_info->rhs = op1;
1093 edge_info = allocate_edge_info (false_edge);
1094 record_conditions (edge_info, inverted, cond);
1096 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1098 edge_info->lhs = op0;
1099 edge_info->rhs = op1;
1104 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1109 class dom_opt_dom_walker : public dom_walker
1111 public:
1112 dom_opt_dom_walker (cdi_direction direction)
1113 : dom_walker (direction), m_dummy_cond (NULL) {}
1115 virtual void before_dom_children (basic_block);
1116 virtual void after_dom_children (basic_block);
1118 private:
1119 void thread_across_edge (edge);
1121 gcond *m_dummy_cond;
1124 /* Jump threading, redundancy elimination and const/copy propagation.
1126 This pass may expose new symbols that need to be renamed into SSA. For
1127 every new symbol exposed, its corresponding bit will be set in
1128 VARS_TO_RENAME. */
1130 namespace {
1132 const pass_data pass_data_dominator =
1134 GIMPLE_PASS, /* type */
1135 "dom", /* name */
1136 OPTGROUP_NONE, /* optinfo_flags */
1137 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
1138 ( PROP_cfg | PROP_ssa ), /* properties_required */
1139 0, /* properties_provided */
1140 0, /* properties_destroyed */
1141 0, /* todo_flags_start */
1142 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
1145 class pass_dominator : public gimple_opt_pass
1147 public:
1148 pass_dominator (gcc::context *ctxt)
1149 : gimple_opt_pass (pass_data_dominator, ctxt)
1152 /* opt_pass methods: */
1153 opt_pass * clone () { return new pass_dominator (m_ctxt); }
1154 virtual bool gate (function *) { return flag_tree_dom != 0; }
1155 virtual unsigned int execute (function *);
1157 }; // class pass_dominator
1159 unsigned int
1160 pass_dominator::execute (function *fun)
1162 memset (&opt_stats, 0, sizeof (opt_stats));
1164 /* Create our hash tables. */
1165 avail_exprs = new hash_table<expr_elt_hasher> (1024);
1166 avail_exprs_stack.create (20);
1167 const_and_copies = new class const_and_copies ();
1168 need_eh_cleanup = BITMAP_ALLOC (NULL);
1169 need_noreturn_fixup.create (0);
1171 calculate_dominance_info (CDI_DOMINATORS);
1172 cfg_altered = false;
1174 /* We need to know loop structures in order to avoid destroying them
1175 in jump threading. Note that we still can e.g. thread through loop
1176 headers to an exit edge, or through loop header to the loop body, assuming
1177 that we update the loop info.
1179 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
1180 to several overly conservative bail-outs in jump threading, case
1181 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
1182 missing. We should improve jump threading in future then
1183 LOOPS_HAVE_PREHEADERS won't be needed here. */
1184 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
1186 /* Initialize the value-handle array. */
1187 threadedge_initialize_values ();
1189 /* We need accurate information regarding back edges in the CFG
1190 for jump threading; this may include back edges that are not part of
1191 a single loop. */
1192 mark_dfs_back_edges ();
1194 /* Recursively walk the dominator tree optimizing statements. */
1195 dom_opt_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
1198 gimple_stmt_iterator gsi;
1199 basic_block bb;
1200 FOR_EACH_BB_FN (bb, fun)
1202 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1203 update_stmt_if_modified (gsi_stmt (gsi));
1207 /* If we exposed any new variables, go ahead and put them into
1208 SSA form now, before we handle jump threading. This simplifies
1209 interactions between rewriting of _DECL nodes into SSA form
1210 and rewriting SSA_NAME nodes into SSA form after block
1211 duplication and CFG manipulation. */
1212 update_ssa (TODO_update_ssa);
1214 free_all_edge_infos ();
1216 /* Thread jumps, creating duplicate blocks as needed. */
1217 cfg_altered |= thread_through_all_blocks (first_pass_instance);
1219 if (cfg_altered)
1220 free_dominance_info (CDI_DOMINATORS);
1222 /* Removal of statements may make some EH edges dead. Purge
1223 such edges from the CFG as needed. */
1224 if (!bitmap_empty_p (need_eh_cleanup))
1226 unsigned i;
1227 bitmap_iterator bi;
1229 /* Jump threading may have created forwarder blocks from blocks
1230 needing EH cleanup; the new successor of these blocks, which
1231 has inherited from the original block, needs the cleanup.
1232 Don't clear bits in the bitmap, as that can break the bitmap
1233 iterator. */
1234 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
1236 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
1237 if (bb == NULL)
1238 continue;
1239 while (single_succ_p (bb)
1240 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
1241 bb = single_succ (bb);
1242 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
1243 continue;
1244 if ((unsigned) bb->index != i)
1245 bitmap_set_bit (need_eh_cleanup, bb->index);
1248 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1249 bitmap_clear (need_eh_cleanup);
1252 /* Fixup stmts that became noreturn calls. This may require splitting
1253 blocks and thus isn't possible during the dominator walk or before
1254 jump threading finished. Do this in reverse order so we don't
1255 inadvertedly remove a stmt we want to fixup by visiting a dominating
1256 now noreturn call first. */
1257 while (!need_noreturn_fixup.is_empty ())
1259 gimple stmt = need_noreturn_fixup.pop ();
1260 if (dump_file && dump_flags & TDF_DETAILS)
1262 fprintf (dump_file, "Fixing up noreturn call ");
1263 print_gimple_stmt (dump_file, stmt, 0, 0);
1264 fprintf (dump_file, "\n");
1266 fixup_noreturn_call (stmt);
1269 statistics_counter_event (fun, "Redundant expressions eliminated",
1270 opt_stats.num_re);
1271 statistics_counter_event (fun, "Constants propagated",
1272 opt_stats.num_const_prop);
1273 statistics_counter_event (fun, "Copies propagated",
1274 opt_stats.num_copy_prop);
1276 /* Debugging dumps. */
1277 if (dump_file && (dump_flags & TDF_STATS))
1278 dump_dominator_optimization_stats (dump_file);
1280 loop_optimizer_finalize ();
1282 /* Delete our main hashtable. */
1283 delete avail_exprs;
1284 avail_exprs = NULL;
1286 /* Free asserted bitmaps and stacks. */
1287 BITMAP_FREE (need_eh_cleanup);
1288 need_noreturn_fixup.release ();
1289 avail_exprs_stack.release ();
1290 delete const_and_copies;
1292 /* Free the value-handle array. */
1293 threadedge_finalize_values ();
1295 return 0;
1298 } // anon namespace
1300 gimple_opt_pass *
1301 make_pass_dominator (gcc::context *ctxt)
1303 return new pass_dominator (ctxt);
1307 /* Given a conditional statement CONDSTMT, convert the
1308 condition to a canonical form. */
1310 static void
1311 canonicalize_comparison (gcond *condstmt)
1313 tree op0;
1314 tree op1;
1315 enum tree_code code;
1317 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
1319 op0 = gimple_cond_lhs (condstmt);
1320 op1 = gimple_cond_rhs (condstmt);
1322 code = gimple_cond_code (condstmt);
1324 /* If it would be profitable to swap the operands, then do so to
1325 canonicalize the statement, enabling better optimization.
1327 By placing canonicalization of such expressions here we
1328 transparently keep statements in canonical form, even
1329 when the statement is modified. */
1330 if (tree_swap_operands_p (op0, op1, false))
1332 /* For relationals we need to swap the operands
1333 and change the code. */
1334 if (code == LT_EXPR
1335 || code == GT_EXPR
1336 || code == LE_EXPR
1337 || code == GE_EXPR)
1339 code = swap_tree_comparison (code);
1341 gimple_cond_set_code (condstmt, code);
1342 gimple_cond_set_lhs (condstmt, op1);
1343 gimple_cond_set_rhs (condstmt, op0);
1345 update_stmt (condstmt);
1350 /* Initialize local stacks for this optimizer and record equivalences
1351 upon entry to BB. Equivalences can come from the edge traversed to
1352 reach BB or they may come from PHI nodes at the start of BB. */
1354 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
1355 LIMIT entries left in LOCALs. */
1357 static void
1358 remove_local_expressions_from_table (void)
1360 /* Remove all the expressions made available in this block. */
1361 while (avail_exprs_stack.length () > 0)
1363 std::pair<expr_hash_elt_t, expr_hash_elt_t> victim
1364 = avail_exprs_stack.pop ();
1365 expr_hash_elt **slot;
1367 if (victim.first == NULL)
1368 break;
1370 /* This must precede the actual removal from the hash table,
1371 as ELEMENT and the table entry may share a call argument
1372 vector which will be freed during removal. */
1373 if (dump_file && (dump_flags & TDF_DETAILS))
1375 fprintf (dump_file, "<<<< ");
1376 print_expr_hash_elt (dump_file, victim.first);
1379 slot = avail_exprs->find_slot (victim.first, NO_INSERT);
1380 gcc_assert (slot && *slot == victim.first);
1381 if (victim.second != NULL)
1383 free_expr_hash_elt (*slot);
1384 *slot = victim.second;
1386 else
1387 avail_exprs->clear_slot (slot);
1391 /* A trivial wrapper so that we can present the generic jump
1392 threading code with a simple API for simplifying statements. */
1393 static tree
1394 simplify_stmt_for_jump_threading (gimple stmt,
1395 gimple within_stmt ATTRIBUTE_UNUSED)
1397 return lookup_avail_expr (stmt, false);
1400 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
1402 static tree
1403 dom_valueize (tree t)
1405 if (TREE_CODE (t) == SSA_NAME)
1407 tree tem = SSA_NAME_VALUE (t);
1408 if (tem)
1409 return tem;
1411 return t;
1414 /* Record into the equivalence tables any equivalences implied by
1415 traversing edge E (which are cached in E->aux).
1417 Callers are responsible for managing the unwinding markers. */
1418 static void
1419 record_temporary_equivalences (edge e)
1421 int i;
1422 struct edge_info *edge_info = (struct edge_info *) e->aux;
1424 /* If we have info associated with this edge, record it into
1425 our equivalence tables. */
1426 if (edge_info)
1428 cond_equivalence *eq;
1429 tree lhs = edge_info->lhs;
1430 tree rhs = edge_info->rhs;
1432 /* If we have a simple NAME = VALUE equivalence, record it. */
1433 if (lhs)
1434 record_equality (lhs, rhs);
1436 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1437 set via a widening type conversion, then we may be able to record
1438 additional equivalences. */
1439 if (lhs
1440 && TREE_CODE (lhs) == SSA_NAME
1441 && TREE_CODE (rhs) == INTEGER_CST)
1443 gimple defstmt = SSA_NAME_DEF_STMT (lhs);
1445 if (defstmt
1446 && is_gimple_assign (defstmt)
1447 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
1449 tree old_rhs = gimple_assign_rhs1 (defstmt);
1451 /* If the conversion widens the original value and
1452 the constant is in the range of the type of OLD_RHS,
1453 then convert the constant and record the equivalence.
1455 Note that int_fits_type_p does not check the precision
1456 if the upper and lower bounds are OK. */
1457 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
1458 && (TYPE_PRECISION (TREE_TYPE (lhs))
1459 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
1460 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
1462 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1463 record_equality (old_rhs, newval);
1468 /* If LHS is an SSA_NAME with a new equivalency then try if
1469 stmts with uses of that LHS that dominate the edge destination
1470 simplify and allow further equivalences to be recorded. */
1471 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1473 use_operand_p use_p;
1474 imm_use_iterator iter;
1475 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
1477 gimple use_stmt = USE_STMT (use_p);
1479 /* Only bother to record more equivalences for lhs that
1480 can be directly used by e->dest.
1481 ??? If the code gets re-organized to a worklist to
1482 catch more indirect opportunities and it is made to
1483 handle PHIs then this should only consider use_stmts
1484 in basic-blocks we have already visited. */
1485 if (e->dest == gimple_bb (use_stmt)
1486 || !dominated_by_p (CDI_DOMINATORS,
1487 e->dest, gimple_bb (use_stmt)))
1488 continue;
1489 tree lhs2 = gimple_get_lhs (use_stmt);
1490 if (lhs2 && TREE_CODE (lhs2) == SSA_NAME)
1492 tree res
1493 = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
1494 no_follow_ssa_edges);
1495 if (res
1496 && (TREE_CODE (res) == SSA_NAME
1497 || is_gimple_min_invariant (res)))
1498 record_equality (lhs2, res);
1503 /* If we have 0 = COND or 1 = COND equivalences, record them
1504 into our expression hash tables. */
1505 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1506 record_cond (eq);
1510 /* Wrapper for common code to attempt to thread an edge. For example,
1511 it handles lazily building the dummy condition and the bookkeeping
1512 when jump threading is successful. */
1514 void
1515 dom_opt_dom_walker::thread_across_edge (edge e)
1517 if (! m_dummy_cond)
1518 m_dummy_cond =
1519 gimple_build_cond (NE_EXPR,
1520 integer_zero_node, integer_zero_node,
1521 NULL, NULL);
1523 /* Push a marker on both stacks so we can unwind the tables back to their
1524 current state. */
1525 avail_exprs_stack.safe_push
1526 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1527 const_and_copies->push_marker ();
1529 /* Traversing E may result in equivalences we can utilize. */
1530 record_temporary_equivalences (e);
1532 /* With all the edge equivalences in the tables, go ahead and attempt
1533 to thread through E->dest. */
1534 ::thread_across_edge (m_dummy_cond, e, false,
1535 const_and_copies,
1536 simplify_stmt_for_jump_threading);
1538 /* And restore the various tables to their state before
1539 we threaded this edge.
1541 XXX The code in tree-ssa-threadedge.c will restore the state of
1542 the const_and_copies table. We we just have to restore the expression
1543 table. */
1544 remove_local_expressions_from_table ();
1547 /* PHI nodes can create equivalences too.
1549 Ignoring any alternatives which are the same as the result, if
1550 all the alternatives are equal, then the PHI node creates an
1551 equivalence. */
1553 static void
1554 record_equivalences_from_phis (basic_block bb)
1556 gphi_iterator gsi;
1558 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1560 gphi *phi = gsi.phi ();
1562 tree lhs = gimple_phi_result (phi);
1563 tree rhs = NULL;
1564 size_t i;
1566 for (i = 0; i < gimple_phi_num_args (phi); i++)
1568 tree t = gimple_phi_arg_def (phi, i);
1570 /* Ignore alternatives which are the same as our LHS. Since
1571 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1572 can simply compare pointers. */
1573 if (lhs == t)
1574 continue;
1576 t = dom_valueize (t);
1578 /* If we have not processed an alternative yet, then set
1579 RHS to this alternative. */
1580 if (rhs == NULL)
1581 rhs = t;
1582 /* If we have processed an alternative (stored in RHS), then
1583 see if it is equal to this one. If it isn't, then stop
1584 the search. */
1585 else if (! operand_equal_for_phi_arg_p (rhs, t))
1586 break;
1589 /* If we had no interesting alternatives, then all the RHS alternatives
1590 must have been the same as LHS. */
1591 if (!rhs)
1592 rhs = lhs;
1594 /* If we managed to iterate through each PHI alternative without
1595 breaking out of the loop, then we have a PHI which may create
1596 a useful equivalence. We do not need to record unwind data for
1597 this, since this is a true assignment and not an equivalence
1598 inferred from a comparison. All uses of this ssa name are dominated
1599 by this assignment, so unwinding just costs time and space. */
1600 if (i == gimple_phi_num_args (phi)
1601 && may_propagate_copy (lhs, rhs))
1602 set_ssa_name_value (lhs, rhs);
1606 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1607 return that edge. Otherwise return NULL. */
1608 static edge
1609 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1611 edge retval = NULL;
1612 edge e;
1613 edge_iterator ei;
1615 FOR_EACH_EDGE (e, ei, bb->preds)
1617 /* A loop back edge can be identified by the destination of
1618 the edge dominating the source of the edge. */
1619 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1620 continue;
1622 /* If we have already seen a non-loop edge, then we must have
1623 multiple incoming non-loop edges and thus we return NULL. */
1624 if (retval)
1625 return NULL;
1627 /* This is the first non-loop incoming edge we have found. Record
1628 it. */
1629 retval = e;
1632 return retval;
1635 /* Record any equivalences created by the incoming edge to BB. If BB
1636 has more than one incoming edge, then no equivalence is created. */
1638 static void
1639 record_equivalences_from_incoming_edge (basic_block bb)
1641 edge e;
1642 basic_block parent;
1644 /* If our parent block ended with a control statement, then we may be
1645 able to record some equivalences based on which outgoing edge from
1646 the parent was followed. */
1647 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1649 e = single_incoming_edge_ignoring_loop_edges (bb);
1651 /* If we had a single incoming edge from our parent block, then enter
1652 any data associated with the edge into our tables. */
1653 if (e && e->src == parent)
1654 record_temporary_equivalences (e);
1657 /* Dump SSA statistics on FILE. */
1659 void
1660 dump_dominator_optimization_stats (FILE *file)
1662 fprintf (file, "Total number of statements: %6ld\n\n",
1663 opt_stats.num_stmts);
1664 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1665 opt_stats.num_exprs_considered);
1667 fprintf (file, "\nHash table statistics:\n");
1669 fprintf (file, " avail_exprs: ");
1670 htab_statistics (file, *avail_exprs);
1674 /* Dump SSA statistics on stderr. */
1676 DEBUG_FUNCTION void
1677 debug_dominator_optimization_stats (void)
1679 dump_dominator_optimization_stats (stderr);
1683 /* Dump statistics for the hash table HTAB. */
1685 static void
1686 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1688 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1689 (long) htab.size (),
1690 (long) htab.elements (),
1691 htab.collisions ());
1695 /* Enter condition equivalence into the expression hash table.
1696 This indicates that a conditional expression has a known
1697 boolean value. */
1699 static void
1700 record_cond (cond_equivalence *p)
1702 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1703 expr_hash_elt **slot;
1705 initialize_hash_element_from_expr (&p->cond, p->value, element);
1707 slot = avail_exprs->find_slot_with_hash (element, element->hash, INSERT);
1708 if (*slot == NULL)
1710 *slot = element;
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1714 fprintf (dump_file, "1>>> ");
1715 print_expr_hash_elt (dump_file, element);
1718 avail_exprs_stack.safe_push
1719 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element, NULL));
1721 else
1722 free_expr_hash_elt (element);
1725 /* Return the loop depth of the basic block of the defining statement of X.
1726 This number should not be treated as absolutely correct because the loop
1727 information may not be completely up-to-date when dom runs. However, it
1728 will be relatively correct, and as more passes are taught to keep loop info
1729 up to date, the result will become more and more accurate. */
1731 static int
1732 loop_depth_of_name (tree x)
1734 gimple defstmt;
1735 basic_block defbb;
1737 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1738 if (TREE_CODE (x) != SSA_NAME)
1739 return 0;
1741 /* Otherwise return the loop depth of the defining statement's bb.
1742 Note that there may not actually be a bb for this statement, if the
1743 ssa_name is live on entry. */
1744 defstmt = SSA_NAME_DEF_STMT (x);
1745 defbb = gimple_bb (defstmt);
1746 if (!defbb)
1747 return 0;
1749 return bb_loop_depth (defbb);
1752 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1753 This constrains the cases in which we may treat this as assignment. */
1755 static void
1756 record_equality (tree x, tree y)
1758 tree prev_x = NULL, prev_y = NULL;
1760 if (tree_swap_operands_p (x, y, false))
1761 std::swap (x, y);
1763 /* Most of the time tree_swap_operands_p does what we want. But there
1764 are cases where we know one operand is better for copy propagation than
1765 the other. Given no other code cares about ordering of equality
1766 comparison operators for that purpose, we just handle the special cases
1767 here. */
1768 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1770 /* If one operand is a single use operand, then make it
1771 X. This will preserve its single use properly and if this
1772 conditional is eliminated, the computation of X can be
1773 eliminated as well. */
1774 if (has_single_use (y) && ! has_single_use (x))
1775 std::swap (x, y);
1777 if (TREE_CODE (x) == SSA_NAME)
1778 prev_x = SSA_NAME_VALUE (x);
1779 if (TREE_CODE (y) == SSA_NAME)
1780 prev_y = SSA_NAME_VALUE (y);
1782 /* If one of the previous values is invariant, or invariant in more loops
1783 (by depth), then use that.
1784 Otherwise it doesn't matter which value we choose, just so
1785 long as we canonicalize on one value. */
1786 if (is_gimple_min_invariant (y))
1788 else if (is_gimple_min_invariant (x)
1789 /* ??? When threading over backedges the following is important
1790 for correctness. See PR61757. */
1791 || (loop_depth_of_name (x) < loop_depth_of_name (y)))
1792 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1793 else if (prev_x && is_gimple_min_invariant (prev_x))
1794 x = y, y = prev_x, prev_x = prev_y;
1795 else if (prev_y)
1796 y = prev_y;
1798 /* After the swapping, we must have one SSA_NAME. */
1799 if (TREE_CODE (x) != SSA_NAME)
1800 return;
1802 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1803 variable compared against zero. If we're honoring signed zeros,
1804 then we cannot record this value unless we know that the value is
1805 nonzero. */
1806 if (HONOR_SIGNED_ZEROS (x)
1807 && (TREE_CODE (y) != REAL_CST
1808 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1809 return;
1811 const_and_copies->record_const_or_copy (x, y, prev_x);
1814 /* Returns true when STMT is a simple iv increment. It detects the
1815 following situation:
1817 i_1 = phi (..., i_2)
1818 i_2 = i_1 +/- ... */
1820 bool
1821 simple_iv_increment_p (gimple stmt)
1823 enum tree_code code;
1824 tree lhs, preinc;
1825 gimple phi;
1826 size_t i;
1828 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1829 return false;
1831 lhs = gimple_assign_lhs (stmt);
1832 if (TREE_CODE (lhs) != SSA_NAME)
1833 return false;
1835 code = gimple_assign_rhs_code (stmt);
1836 if (code != PLUS_EXPR
1837 && code != MINUS_EXPR
1838 && code != POINTER_PLUS_EXPR)
1839 return false;
1841 preinc = gimple_assign_rhs1 (stmt);
1842 if (TREE_CODE (preinc) != SSA_NAME)
1843 return false;
1845 phi = SSA_NAME_DEF_STMT (preinc);
1846 if (gimple_code (phi) != GIMPLE_PHI)
1847 return false;
1849 for (i = 0; i < gimple_phi_num_args (phi); i++)
1850 if (gimple_phi_arg_def (phi, i) == lhs)
1851 return true;
1853 return false;
1856 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1857 known value for that SSA_NAME (or NULL if no value is known).
1859 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1860 successors of BB. */
1862 static void
1863 cprop_into_successor_phis (basic_block bb)
1865 edge e;
1866 edge_iterator ei;
1868 FOR_EACH_EDGE (e, ei, bb->succs)
1870 int indx;
1871 gphi_iterator gsi;
1873 /* If this is an abnormal edge, then we do not want to copy propagate
1874 into the PHI alternative associated with this edge. */
1875 if (e->flags & EDGE_ABNORMAL)
1876 continue;
1878 gsi = gsi_start_phis (e->dest);
1879 if (gsi_end_p (gsi))
1880 continue;
1882 /* We may have an equivalence associated with this edge. While
1883 we can not propagate it into non-dominated blocks, we can
1884 propagate them into PHIs in non-dominated blocks. */
1886 /* Push the unwind marker so we can reset the const and copies
1887 table back to its original state after processing this edge. */
1888 const_and_copies->push_marker ();
1890 /* Extract and record any simple NAME = VALUE equivalences.
1892 Don't bother with [01] = COND equivalences, they're not useful
1893 here. */
1894 struct edge_info *edge_info = (struct edge_info *) e->aux;
1895 if (edge_info)
1897 tree lhs = edge_info->lhs;
1898 tree rhs = edge_info->rhs;
1900 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1901 const_and_copies->record_const_or_copy (lhs, rhs);
1904 indx = e->dest_idx;
1905 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1907 tree new_val;
1908 use_operand_p orig_p;
1909 tree orig_val;
1910 gphi *phi = gsi.phi ();
1912 /* The alternative may be associated with a constant, so verify
1913 it is an SSA_NAME before doing anything with it. */
1914 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1915 orig_val = get_use_from_ptr (orig_p);
1916 if (TREE_CODE (orig_val) != SSA_NAME)
1917 continue;
1919 /* If we have *ORIG_P in our constant/copy table, then replace
1920 ORIG_P with its value in our constant/copy table. */
1921 new_val = SSA_NAME_VALUE (orig_val);
1922 if (new_val
1923 && new_val != orig_val
1924 && (TREE_CODE (new_val) == SSA_NAME
1925 || is_gimple_min_invariant (new_val))
1926 && may_propagate_copy (orig_val, new_val))
1927 propagate_value (orig_p, new_val);
1930 const_and_copies->pop_to_marker ();
1934 void
1935 dom_opt_dom_walker::before_dom_children (basic_block bb)
1937 gimple_stmt_iterator gsi;
1939 if (dump_file && (dump_flags & TDF_DETAILS))
1940 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1942 /* Push a marker on the stacks of local information so that we know how
1943 far to unwind when we finalize this block. */
1944 avail_exprs_stack.safe_push
1945 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1946 const_and_copies->push_marker ();
1948 record_equivalences_from_incoming_edge (bb);
1950 /* PHI nodes can create equivalences too. */
1951 record_equivalences_from_phis (bb);
1953 /* Create equivalences from redundant PHIs. PHIs are only truly
1954 redundant when they exist in the same block, so push another
1955 marker and unwind right afterwards. */
1956 avail_exprs_stack.safe_push
1957 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
1958 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1959 eliminate_redundant_computations (&gsi);
1960 remove_local_expressions_from_table ();
1962 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1963 optimize_stmt (bb, gsi);
1965 /* Now prepare to process dominated blocks. */
1966 record_edge_info (bb);
1967 cprop_into_successor_phis (bb);
1970 /* We have finished processing the dominator children of BB, perform
1971 any finalization actions in preparation for leaving this node in
1972 the dominator tree. */
1974 void
1975 dom_opt_dom_walker::after_dom_children (basic_block bb)
1977 gimple last;
1979 /* If we have an outgoing edge to a block with multiple incoming and
1980 outgoing edges, then we may be able to thread the edge, i.e., we
1981 may be able to statically determine which of the outgoing edges
1982 will be traversed when the incoming edge from BB is traversed. */
1983 if (single_succ_p (bb)
1984 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1985 && potentially_threadable_block (single_succ (bb)))
1987 thread_across_edge (single_succ_edge (bb));
1989 else if ((last = last_stmt (bb))
1990 && gimple_code (last) == GIMPLE_COND
1991 && EDGE_COUNT (bb->succs) == 2
1992 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1993 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1995 edge true_edge, false_edge;
1997 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1999 /* Only try to thread the edge if it reaches a target block with
2000 more than one predecessor and more than one successor. */
2001 if (potentially_threadable_block (true_edge->dest))
2002 thread_across_edge (true_edge);
2004 /* Similarly for the ELSE arm. */
2005 if (potentially_threadable_block (false_edge->dest))
2006 thread_across_edge (false_edge);
2010 /* These remove expressions local to BB from the tables. */
2011 remove_local_expressions_from_table ();
2012 const_and_copies->pop_to_marker ();
2015 /* Search for redundant computations in STMT. If any are found, then
2016 replace them with the variable holding the result of the computation.
2018 If safe, record this expression into the available expression hash
2019 table. */
2021 static void
2022 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
2024 tree expr_type;
2025 tree cached_lhs;
2026 tree def;
2027 bool insert = true;
2028 bool assigns_var_p = false;
2030 gimple stmt = gsi_stmt (*gsi);
2032 if (gimple_code (stmt) == GIMPLE_PHI)
2033 def = gimple_phi_result (stmt);
2034 else
2035 def = gimple_get_lhs (stmt);
2037 /* Certain expressions on the RHS can be optimized away, but can not
2038 themselves be entered into the hash tables. */
2039 if (! def
2040 || TREE_CODE (def) != SSA_NAME
2041 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2042 || gimple_vdef (stmt)
2043 /* Do not record equivalences for increments of ivs. This would create
2044 overlapping live ranges for a very questionable gain. */
2045 || simple_iv_increment_p (stmt))
2046 insert = false;
2048 /* Check if the expression has been computed before. */
2049 cached_lhs = lookup_avail_expr (stmt, insert);
2051 opt_stats.num_exprs_considered++;
2053 /* Get the type of the expression we are trying to optimize. */
2054 if (is_gimple_assign (stmt))
2056 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
2057 assigns_var_p = true;
2059 else if (gimple_code (stmt) == GIMPLE_COND)
2060 expr_type = boolean_type_node;
2061 else if (is_gimple_call (stmt))
2063 gcc_assert (gimple_call_lhs (stmt));
2064 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
2065 assigns_var_p = true;
2067 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2068 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
2069 else if (gimple_code (stmt) == GIMPLE_PHI)
2070 /* We can't propagate into a phi, so the logic below doesn't apply.
2071 Instead record an equivalence between the cached LHS and the
2072 PHI result of this statement, provided they are in the same block.
2073 This should be sufficient to kill the redundant phi. */
2075 if (def && cached_lhs)
2076 const_and_copies->record_const_or_copy (def, cached_lhs);
2077 return;
2079 else
2080 gcc_unreachable ();
2082 if (!cached_lhs)
2083 return;
2085 /* It is safe to ignore types here since we have already done
2086 type checking in the hashing and equality routines. In fact
2087 type checking here merely gets in the way of constant
2088 propagation. Also, make sure that it is safe to propagate
2089 CACHED_LHS into the expression in STMT. */
2090 if ((TREE_CODE (cached_lhs) != SSA_NAME
2091 && (assigns_var_p
2092 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
2093 || may_propagate_copy_into_stmt (stmt, cached_lhs))
2095 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
2096 || is_gimple_min_invariant (cached_lhs));
2098 if (dump_file && (dump_flags & TDF_DETAILS))
2100 fprintf (dump_file, " Replaced redundant expr '");
2101 print_gimple_expr (dump_file, stmt, 0, dump_flags);
2102 fprintf (dump_file, "' with '");
2103 print_generic_expr (dump_file, cached_lhs, dump_flags);
2104 fprintf (dump_file, "'\n");
2107 opt_stats.num_re++;
2109 if (assigns_var_p
2110 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
2111 cached_lhs = fold_convert (expr_type, cached_lhs);
2113 propagate_tree_value_into_stmt (gsi, cached_lhs);
2115 /* Since it is always necessary to mark the result as modified,
2116 perhaps we should move this into propagate_tree_value_into_stmt
2117 itself. */
2118 gimple_set_modified (gsi_stmt (*gsi), true);
2122 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2123 the available expressions table or the const_and_copies table.
2124 Detect and record those equivalences. */
2125 /* We handle only very simple copy equivalences here. The heavy
2126 lifing is done by eliminate_redundant_computations. */
2128 static void
2129 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
2131 tree lhs;
2132 enum tree_code lhs_code;
2134 gcc_assert (is_gimple_assign (stmt));
2136 lhs = gimple_assign_lhs (stmt);
2137 lhs_code = TREE_CODE (lhs);
2139 if (lhs_code == SSA_NAME
2140 && gimple_assign_single_p (stmt))
2142 tree rhs = gimple_assign_rhs1 (stmt);
2144 /* If the RHS of the assignment is a constant or another variable that
2145 may be propagated, register it in the CONST_AND_COPIES table. We
2146 do not need to record unwind data for this, since this is a true
2147 assignment and not an equivalence inferred from a comparison. All
2148 uses of this ssa name are dominated by this assignment, so unwinding
2149 just costs time and space. */
2150 if (may_optimize_p
2151 && (TREE_CODE (rhs) == SSA_NAME
2152 || is_gimple_min_invariant (rhs)))
2154 rhs = dom_valueize (rhs);
2156 if (dump_file && (dump_flags & TDF_DETAILS))
2158 fprintf (dump_file, "==== ASGN ");
2159 print_generic_expr (dump_file, lhs, 0);
2160 fprintf (dump_file, " = ");
2161 print_generic_expr (dump_file, rhs, 0);
2162 fprintf (dump_file, "\n");
2165 set_ssa_name_value (lhs, rhs);
2169 /* Make sure we can propagate &x + CST. */
2170 if (lhs_code == SSA_NAME
2171 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2172 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
2173 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
2175 tree op0 = gimple_assign_rhs1 (stmt);
2176 tree op1 = gimple_assign_rhs2 (stmt);
2177 tree new_rhs
2178 = build_fold_addr_expr (fold_build2 (MEM_REF,
2179 TREE_TYPE (TREE_TYPE (op0)),
2180 unshare_expr (op0),
2181 fold_convert (ptr_type_node,
2182 op1)));
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2185 fprintf (dump_file, "==== ASGN ");
2186 print_generic_expr (dump_file, lhs, 0);
2187 fprintf (dump_file, " = ");
2188 print_generic_expr (dump_file, new_rhs, 0);
2189 fprintf (dump_file, "\n");
2192 set_ssa_name_value (lhs, new_rhs);
2195 /* A memory store, even an aliased store, creates a useful
2196 equivalence. By exchanging the LHS and RHS, creating suitable
2197 vops and recording the result in the available expression table,
2198 we may be able to expose more redundant loads. */
2199 if (!gimple_has_volatile_ops (stmt)
2200 && gimple_references_memory_p (stmt)
2201 && gimple_assign_single_p (stmt)
2202 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2203 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2204 && !is_gimple_reg (lhs))
2206 tree rhs = gimple_assign_rhs1 (stmt);
2207 gassign *new_stmt;
2209 /* Build a new statement with the RHS and LHS exchanged. */
2210 if (TREE_CODE (rhs) == SSA_NAME)
2212 /* NOTE tuples. The call to gimple_build_assign below replaced
2213 a call to build_gimple_modify_stmt, which did not set the
2214 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2215 may cause an SSA validation failure, as the LHS may be a
2216 default-initialized name and should have no definition. I'm
2217 a bit dubious of this, as the artificial statement that we
2218 generate here may in fact be ill-formed, but it is simply
2219 used as an internal device in this pass, and never becomes
2220 part of the CFG. */
2221 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2222 new_stmt = gimple_build_assign (rhs, lhs);
2223 SSA_NAME_DEF_STMT (rhs) = defstmt;
2225 else
2226 new_stmt = gimple_build_assign (rhs, lhs);
2228 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2230 /* Finally enter the statement into the available expression
2231 table. */
2232 lookup_avail_expr (new_stmt, true);
2236 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2237 CONST_AND_COPIES. */
2239 static void
2240 cprop_operand (gimple stmt, use_operand_p op_p)
2242 tree val;
2243 tree op = USE_FROM_PTR (op_p);
2245 /* If the operand has a known constant value or it is known to be a
2246 copy of some other variable, use the value or copy stored in
2247 CONST_AND_COPIES. */
2248 val = SSA_NAME_VALUE (op);
2249 if (val && val != op)
2251 /* Do not replace hard register operands in asm statements. */
2252 if (gimple_code (stmt) == GIMPLE_ASM
2253 && !may_propagate_copy_into_asm (op))
2254 return;
2256 /* Certain operands are not allowed to be copy propagated due
2257 to their interaction with exception handling and some GCC
2258 extensions. */
2259 if (!may_propagate_copy (op, val))
2260 return;
2262 /* Do not propagate copies into BIVs.
2263 See PR23821 and PR62217 for how this can disturb IV and
2264 number of iteration analysis. */
2265 if (TREE_CODE (val) != INTEGER_CST)
2267 gimple def = SSA_NAME_DEF_STMT (op);
2268 if (gimple_code (def) == GIMPLE_PHI
2269 && gimple_bb (def)->loop_father->header == gimple_bb (def))
2270 return;
2273 /* Dump details. */
2274 if (dump_file && (dump_flags & TDF_DETAILS))
2276 fprintf (dump_file, " Replaced '");
2277 print_generic_expr (dump_file, op, dump_flags);
2278 fprintf (dump_file, "' with %s '",
2279 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2280 print_generic_expr (dump_file, val, dump_flags);
2281 fprintf (dump_file, "'\n");
2284 if (TREE_CODE (val) != SSA_NAME)
2285 opt_stats.num_const_prop++;
2286 else
2287 opt_stats.num_copy_prop++;
2289 propagate_value (op_p, val);
2291 /* And note that we modified this statement. This is now
2292 safe, even if we changed virtual operands since we will
2293 rescan the statement and rewrite its operands again. */
2294 gimple_set_modified (stmt, true);
2298 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2299 known value for that SSA_NAME (or NULL if no value is known).
2301 Propagate values from CONST_AND_COPIES into the uses, vuses and
2302 vdef_ops of STMT. */
2304 static void
2305 cprop_into_stmt (gimple stmt)
2307 use_operand_p op_p;
2308 ssa_op_iter iter;
2310 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2311 cprop_operand (stmt, op_p);
2314 /* Optimize the statement pointed to by iterator SI.
2316 We try to perform some simplistic global redundancy elimination and
2317 constant propagation:
2319 1- To detect global redundancy, we keep track of expressions that have
2320 been computed in this block and its dominators. If we find that the
2321 same expression is computed more than once, we eliminate repeated
2322 computations by using the target of the first one.
2324 2- Constant values and copy assignments. This is used to do very
2325 simplistic constant and copy propagation. When a constant or copy
2326 assignment is found, we map the value on the RHS of the assignment to
2327 the variable in the LHS in the CONST_AND_COPIES table. */
2329 static void
2330 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2332 gimple stmt, old_stmt;
2333 bool may_optimize_p;
2334 bool modified_p = false;
2335 bool was_noreturn;
2337 old_stmt = stmt = gsi_stmt (si);
2338 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
2340 if (dump_file && (dump_flags & TDF_DETAILS))
2342 fprintf (dump_file, "Optimizing statement ");
2343 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2346 if (gimple_code (stmt) == GIMPLE_COND)
2347 canonicalize_comparison (as_a <gcond *> (stmt));
2349 update_stmt_if_modified (stmt);
2350 opt_stats.num_stmts++;
2352 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2353 cprop_into_stmt (stmt);
2355 /* If the statement has been modified with constant replacements,
2356 fold its RHS before checking for redundant computations. */
2357 if (gimple_modified_p (stmt))
2359 tree rhs = NULL;
2361 /* Try to fold the statement making sure that STMT is kept
2362 up to date. */
2363 if (fold_stmt (&si))
2365 stmt = gsi_stmt (si);
2366 gimple_set_modified (stmt, true);
2368 if (dump_file && (dump_flags & TDF_DETAILS))
2370 fprintf (dump_file, " Folded to: ");
2371 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2375 /* We only need to consider cases that can yield a gimple operand. */
2376 if (gimple_assign_single_p (stmt))
2377 rhs = gimple_assign_rhs1 (stmt);
2378 else if (gimple_code (stmt) == GIMPLE_GOTO)
2379 rhs = gimple_goto_dest (stmt);
2380 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2381 /* This should never be an ADDR_EXPR. */
2382 rhs = gimple_switch_index (swtch_stmt);
2384 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2385 recompute_tree_invariant_for_addr_expr (rhs);
2387 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2388 even if fold_stmt updated the stmt already and thus cleared
2389 gimple_modified_p flag on it. */
2390 modified_p = true;
2393 /* Check for redundant computations. Do this optimization only
2394 for assignments that have no volatile ops and conditionals. */
2395 may_optimize_p = (!gimple_has_side_effects (stmt)
2396 && (is_gimple_assign (stmt)
2397 || (is_gimple_call (stmt)
2398 && gimple_call_lhs (stmt) != NULL_TREE)
2399 || gimple_code (stmt) == GIMPLE_COND
2400 || gimple_code (stmt) == GIMPLE_SWITCH));
2402 if (may_optimize_p)
2404 if (gimple_code (stmt) == GIMPLE_CALL)
2406 /* Resolve __builtin_constant_p. If it hasn't been
2407 folded to integer_one_node by now, it's fairly
2408 certain that the value simply isn't constant. */
2409 tree callee = gimple_call_fndecl (stmt);
2410 if (callee
2411 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2412 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2414 propagate_tree_value_into_stmt (&si, integer_zero_node);
2415 stmt = gsi_stmt (si);
2419 update_stmt_if_modified (stmt);
2420 eliminate_redundant_computations (&si);
2421 stmt = gsi_stmt (si);
2423 /* Perform simple redundant store elimination. */
2424 if (gimple_assign_single_p (stmt)
2425 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2427 tree lhs = gimple_assign_lhs (stmt);
2428 tree rhs = gimple_assign_rhs1 (stmt);
2429 tree cached_lhs;
2430 gassign *new_stmt;
2431 rhs = dom_valueize (rhs);
2432 /* Build a new statement with the RHS and LHS exchanged. */
2433 if (TREE_CODE (rhs) == SSA_NAME)
2435 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2436 new_stmt = gimple_build_assign (rhs, lhs);
2437 SSA_NAME_DEF_STMT (rhs) = defstmt;
2439 else
2440 new_stmt = gimple_build_assign (rhs, lhs);
2441 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2442 cached_lhs = lookup_avail_expr (new_stmt, false);
2443 if (cached_lhs
2444 && rhs == cached_lhs)
2446 basic_block bb = gimple_bb (stmt);
2447 unlink_stmt_vdef (stmt);
2448 if (gsi_remove (&si, true))
2450 bitmap_set_bit (need_eh_cleanup, bb->index);
2451 if (dump_file && (dump_flags & TDF_DETAILS))
2452 fprintf (dump_file, " Flagged to clear EH edges.\n");
2454 release_defs (stmt);
2455 return;
2460 /* Record any additional equivalences created by this statement. */
2461 if (is_gimple_assign (stmt))
2462 record_equivalences_from_stmt (stmt, may_optimize_p);
2464 /* If STMT is a COND_EXPR and it was modified, then we may know
2465 where it goes. If that is the case, then mark the CFG as altered.
2467 This will cause us to later call remove_unreachable_blocks and
2468 cleanup_tree_cfg when it is safe to do so. It is not safe to
2469 clean things up here since removal of edges and such can trigger
2470 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2471 the manager.
2473 That's all fine and good, except that once SSA_NAMEs are released
2474 to the manager, we must not call create_ssa_name until all references
2475 to released SSA_NAMEs have been eliminated.
2477 All references to the deleted SSA_NAMEs can not be eliminated until
2478 we remove unreachable blocks.
2480 We can not remove unreachable blocks until after we have completed
2481 any queued jump threading.
2483 We can not complete any queued jump threads until we have taken
2484 appropriate variables out of SSA form. Taking variables out of
2485 SSA form can call create_ssa_name and thus we lose.
2487 Ultimately I suspect we're going to need to change the interface
2488 into the SSA_NAME manager. */
2489 if (gimple_modified_p (stmt) || modified_p)
2491 tree val = NULL;
2493 update_stmt_if_modified (stmt);
2495 if (gimple_code (stmt) == GIMPLE_COND)
2496 val = fold_binary_loc (gimple_location (stmt),
2497 gimple_cond_code (stmt), boolean_type_node,
2498 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2499 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2500 val = gimple_switch_index (swtch_stmt);
2502 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2503 cfg_altered = true;
2505 /* If we simplified a statement in such a way as to be shown that it
2506 cannot trap, update the eh information and the cfg to match. */
2507 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2509 bitmap_set_bit (need_eh_cleanup, bb->index);
2510 if (dump_file && (dump_flags & TDF_DETAILS))
2511 fprintf (dump_file, " Flagged to clear EH edges.\n");
2514 if (!was_noreturn
2515 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
2516 need_noreturn_fixup.safe_push (stmt);
2520 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
2521 the desired memory state. */
2523 static void *
2524 vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
2526 tree vuse2 = (tree) data;
2527 if (vuse1 == vuse2)
2528 return data;
2530 /* This bounds the stmt walks we perform on reference lookups
2531 to O(1) instead of O(N) where N is the number of dominating
2532 stores leading to a candidate. We re-use the SCCVN param
2533 for this as it is basically the same complexity. */
2534 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
2535 return (void *)-1;
2537 return NULL;
2540 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2541 If found, return its LHS. Otherwise insert STMT in the table and
2542 return NULL_TREE.
2544 Also, when an expression is first inserted in the table, it is also
2545 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2546 we finish processing this block and its children. */
2548 static tree
2549 lookup_avail_expr (gimple stmt, bool insert)
2551 expr_hash_elt **slot;
2552 tree lhs;
2553 struct expr_hash_elt element;
2555 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2556 if (gimple_code (stmt) == GIMPLE_PHI)
2557 lhs = gimple_phi_result (stmt);
2558 else
2559 lhs = gimple_get_lhs (stmt);
2561 initialize_hash_element (stmt, lhs, &element);
2563 if (dump_file && (dump_flags & TDF_DETAILS))
2565 fprintf (dump_file, "LKUP ");
2566 print_expr_hash_elt (dump_file, &element);
2569 /* Don't bother remembering constant assignments and copy operations.
2570 Constants and copy operations are handled by the constant/copy propagator
2571 in optimize_stmt. */
2572 if (element.expr.kind == EXPR_SINGLE
2573 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2574 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2575 return NULL_TREE;
2577 /* Finally try to find the expression in the main expression hash table. */
2578 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
2579 if (slot == NULL)
2581 free_expr_hash_elt_contents (&element);
2582 return NULL_TREE;
2584 else if (*slot == NULL)
2586 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2587 *element2 = element;
2588 element2->stamp = element2;
2589 *slot = element2;
2591 if (dump_file && (dump_flags & TDF_DETAILS))
2593 fprintf (dump_file, "2>>> ");
2594 print_expr_hash_elt (dump_file, element2);
2597 avail_exprs_stack.safe_push
2598 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element2, NULL));
2599 return NULL_TREE;
2602 /* If we found a redundant memory operation do an alias walk to
2603 check if we can re-use it. */
2604 if (gimple_vuse (stmt) != (*slot)->vop)
2606 tree vuse1 = (*slot)->vop;
2607 tree vuse2 = gimple_vuse (stmt);
2608 /* If we have a load of a register and a candidate in the
2609 hash with vuse1 then try to reach its stmt by walking
2610 up the virtual use-def chain using walk_non_aliased_vuses.
2611 But don't do this when removing expressions from the hash. */
2612 ao_ref ref;
2613 if (!(vuse1 && vuse2
2614 && gimple_assign_single_p (stmt)
2615 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
2616 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)), true)
2617 && walk_non_aliased_vuses (&ref, vuse2,
2618 vuse_eq, NULL, NULL, vuse1) != NULL))
2620 if (insert)
2622 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2623 *element2 = element;
2624 element2->stamp = element2;
2626 /* Insert the expr into the hash by replacing the current
2627 entry and recording the value to restore in the
2628 avail_exprs_stack. */
2629 avail_exprs_stack.safe_push (std::make_pair (element2, *slot));
2630 *slot = element2;
2631 if (dump_file && (dump_flags & TDF_DETAILS))
2633 fprintf (dump_file, "2>>> ");
2634 print_expr_hash_elt (dump_file, *slot);
2637 return NULL_TREE;
2641 free_expr_hash_elt_contents (&element);
2643 /* Extract the LHS of the assignment so that it can be used as the current
2644 definition of another variable. */
2645 lhs = (*slot)->lhs;
2647 lhs = dom_valueize (lhs);
2649 if (dump_file && (dump_flags & TDF_DETAILS))
2651 fprintf (dump_file, "FIND: ");
2652 print_generic_expr (dump_file, lhs, 0);
2653 fprintf (dump_file, "\n");
2656 return lhs;
2659 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2660 for expressions using the code of the expression and the SSA numbers of
2661 its operands. */
2663 static hashval_t
2664 avail_expr_hash (const void *p)
2666 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2667 inchash::hash hstate;
2669 inchash::add_hashable_expr (expr, hstate);
2671 return hstate.end ();
2674 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2675 up degenerate PHIs created by or exposed by jump threading. */
2677 /* Given a statement STMT, which is either a PHI node or an assignment,
2678 remove it from the IL. */
2680 static void
2681 remove_stmt_or_phi (gimple stmt)
2683 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2685 if (gimple_code (stmt) == GIMPLE_PHI)
2686 remove_phi_node (&gsi, true);
2687 else
2689 gsi_remove (&gsi, true);
2690 release_defs (stmt);
2694 /* Given a statement STMT, which is either a PHI node or an assignment,
2695 return the "rhs" of the node, in the case of a non-degenerate
2696 phi, NULL is returned. */
2698 static tree
2699 get_rhs_or_phi_arg (gimple stmt)
2701 if (gimple_code (stmt) == GIMPLE_PHI)
2702 return degenerate_phi_result (as_a <gphi *> (stmt));
2703 else if (gimple_assign_single_p (stmt))
2704 return gimple_assign_rhs1 (stmt);
2705 else
2706 gcc_unreachable ();
2710 /* Given a statement STMT, which is either a PHI node or an assignment,
2711 return the "lhs" of the node. */
2713 static tree
2714 get_lhs_or_phi_result (gimple stmt)
2716 if (gimple_code (stmt) == GIMPLE_PHI)
2717 return gimple_phi_result (stmt);
2718 else if (is_gimple_assign (stmt))
2719 return gimple_assign_lhs (stmt);
2720 else
2721 gcc_unreachable ();
2724 /* Propagate RHS into all uses of LHS (when possible).
2726 RHS and LHS are derived from STMT, which is passed in solely so
2727 that we can remove it if propagation is successful.
2729 When propagating into a PHI node or into a statement which turns
2730 into a trivial copy or constant initialization, set the
2731 appropriate bit in INTERESTING_NAMEs so that we will visit those
2732 nodes as well in an effort to pick up secondary optimization
2733 opportunities. */
2735 static void
2736 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2738 /* First verify that propagation is valid. */
2739 if (may_propagate_copy (lhs, rhs))
2741 use_operand_p use_p;
2742 imm_use_iterator iter;
2743 gimple use_stmt;
2744 bool all = true;
2746 /* Dump details. */
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2749 fprintf (dump_file, " Replacing '");
2750 print_generic_expr (dump_file, lhs, dump_flags);
2751 fprintf (dump_file, "' with %s '",
2752 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2753 print_generic_expr (dump_file, rhs, dump_flags);
2754 fprintf (dump_file, "'\n");
2757 /* Walk over every use of LHS and try to replace the use with RHS.
2758 At this point the only reason why such a propagation would not
2759 be successful would be if the use occurs in an ASM_EXPR. */
2760 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2762 /* Leave debug stmts alone. If we succeed in propagating
2763 all non-debug uses, we'll drop the DEF, and propagation
2764 into debug stmts will occur then. */
2765 if (gimple_debug_bind_p (use_stmt))
2766 continue;
2768 /* It's not always safe to propagate into an ASM_EXPR. */
2769 if (gimple_code (use_stmt) == GIMPLE_ASM
2770 && ! may_propagate_copy_into_asm (lhs))
2772 all = false;
2773 continue;
2776 /* It's not ok to propagate into the definition stmt of RHS.
2777 <bb 9>:
2778 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2779 g_67.1_6 = prephitmp.12_36;
2780 goto <bb 9>;
2781 While this is strictly all dead code we do not want to
2782 deal with this here. */
2783 if (TREE_CODE (rhs) == SSA_NAME
2784 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2786 all = false;
2787 continue;
2790 /* Dump details. */
2791 if (dump_file && (dump_flags & TDF_DETAILS))
2793 fprintf (dump_file, " Original statement:");
2794 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2797 /* Propagate the RHS into this use of the LHS. */
2798 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2799 propagate_value (use_p, rhs);
2801 /* Special cases to avoid useless calls into the folding
2802 routines, operand scanning, etc.
2804 Propagation into a PHI may cause the PHI to become
2805 a degenerate, so mark the PHI as interesting. No other
2806 actions are necessary. */
2807 if (gimple_code (use_stmt) == GIMPLE_PHI)
2809 tree result;
2811 /* Dump details. */
2812 if (dump_file && (dump_flags & TDF_DETAILS))
2814 fprintf (dump_file, " Updated statement:");
2815 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2818 result = get_lhs_or_phi_result (use_stmt);
2819 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2820 continue;
2823 /* From this point onward we are propagating into a
2824 real statement. Folding may (or may not) be possible,
2825 we may expose new operands, expose dead EH edges,
2826 etc. */
2827 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2828 cannot fold a call that simplifies to a constant,
2829 because the GIMPLE_CALL must be replaced by a
2830 GIMPLE_ASSIGN, and there is no way to effect such a
2831 transformation in-place. We might want to consider
2832 using the more general fold_stmt here. */
2834 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2835 fold_stmt_inplace (&gsi);
2838 /* Sometimes propagation can expose new operands to the
2839 renamer. */
2840 update_stmt (use_stmt);
2842 /* Dump details. */
2843 if (dump_file && (dump_flags & TDF_DETAILS))
2845 fprintf (dump_file, " Updated statement:");
2846 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2849 /* If we replaced a variable index with a constant, then
2850 we would need to update the invariant flag for ADDR_EXPRs. */
2851 if (gimple_assign_single_p (use_stmt)
2852 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2853 recompute_tree_invariant_for_addr_expr
2854 (gimple_assign_rhs1 (use_stmt));
2856 /* If we cleaned up EH information from the statement,
2857 mark its containing block as needing EH cleanups. */
2858 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2860 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2861 if (dump_file && (dump_flags & TDF_DETAILS))
2862 fprintf (dump_file, " Flagged to clear EH edges.\n");
2865 /* Propagation may expose new trivial copy/constant propagation
2866 opportunities. */
2867 if (gimple_assign_single_p (use_stmt)
2868 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2869 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2870 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2872 tree result = get_lhs_or_phi_result (use_stmt);
2873 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2876 /* Propagation into these nodes may make certain edges in
2877 the CFG unexecutable. We want to identify them as PHI nodes
2878 at the destination of those unexecutable edges may become
2879 degenerates. */
2880 else if (gimple_code (use_stmt) == GIMPLE_COND
2881 || gimple_code (use_stmt) == GIMPLE_SWITCH
2882 || gimple_code (use_stmt) == GIMPLE_GOTO)
2884 tree val;
2886 if (gimple_code (use_stmt) == GIMPLE_COND)
2887 val = fold_binary_loc (gimple_location (use_stmt),
2888 gimple_cond_code (use_stmt),
2889 boolean_type_node,
2890 gimple_cond_lhs (use_stmt),
2891 gimple_cond_rhs (use_stmt));
2892 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2893 val = gimple_switch_index (as_a <gswitch *> (use_stmt));
2894 else
2895 val = gimple_goto_dest (use_stmt);
2897 if (val && is_gimple_min_invariant (val))
2899 basic_block bb = gimple_bb (use_stmt);
2900 edge te = find_taken_edge (bb, val);
2901 if (!te)
2902 continue;
2904 edge_iterator ei;
2905 edge e;
2906 gimple_stmt_iterator gsi;
2907 gphi_iterator psi;
2909 /* Remove all outgoing edges except TE. */
2910 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2912 if (e != te)
2914 /* Mark all the PHI nodes at the destination of
2915 the unexecutable edge as interesting. */
2916 for (psi = gsi_start_phis (e->dest);
2917 !gsi_end_p (psi);
2918 gsi_next (&psi))
2920 gphi *phi = psi.phi ();
2922 tree result = gimple_phi_result (phi);
2923 int version = SSA_NAME_VERSION (result);
2925 bitmap_set_bit (interesting_names, version);
2928 te->probability += e->probability;
2930 te->count += e->count;
2931 remove_edge (e);
2932 cfg_altered = true;
2934 else
2935 ei_next (&ei);
2938 gsi = gsi_last_bb (gimple_bb (use_stmt));
2939 gsi_remove (&gsi, true);
2941 /* And fixup the flags on the single remaining edge. */
2942 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2943 te->flags &= ~EDGE_ABNORMAL;
2944 te->flags |= EDGE_FALLTHRU;
2945 if (te->probability > REG_BR_PROB_BASE)
2946 te->probability = REG_BR_PROB_BASE;
2951 /* Ensure there is nothing else to do. */
2952 gcc_assert (!all || has_zero_uses (lhs));
2954 /* If we were able to propagate away all uses of LHS, then
2955 we can remove STMT. */
2956 if (all)
2957 remove_stmt_or_phi (stmt);
2961 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2962 a statement that is a trivial copy or constant initialization.
2964 Attempt to eliminate T by propagating its RHS into all uses of
2965 its LHS. This may in turn set new bits in INTERESTING_NAMES
2966 for nodes we want to revisit later.
2968 All exit paths should clear INTERESTING_NAMES for the result
2969 of STMT. */
2971 static void
2972 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2974 tree lhs = get_lhs_or_phi_result (stmt);
2975 tree rhs;
2976 int version = SSA_NAME_VERSION (lhs);
2978 /* If the LHS of this statement or PHI has no uses, then we can
2979 just eliminate it. This can occur if, for example, the PHI
2980 was created by block duplication due to threading and its only
2981 use was in the conditional at the end of the block which was
2982 deleted. */
2983 if (has_zero_uses (lhs))
2985 bitmap_clear_bit (interesting_names, version);
2986 remove_stmt_or_phi (stmt);
2987 return;
2990 /* Get the RHS of the assignment or PHI node if the PHI is a
2991 degenerate. */
2992 rhs = get_rhs_or_phi_arg (stmt);
2993 if (!rhs)
2995 bitmap_clear_bit (interesting_names, version);
2996 return;
2999 if (!virtual_operand_p (lhs))
3000 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
3001 else
3003 gimple use_stmt;
3004 imm_use_iterator iter;
3005 use_operand_p use_p;
3006 /* For virtual operands we have to propagate into all uses as
3007 otherwise we will create overlapping life-ranges. */
3008 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3009 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3010 SET_USE (use_p, rhs);
3011 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3012 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
3013 remove_stmt_or_phi (stmt);
3016 /* Note that STMT may well have been deleted by now, so do
3017 not access it, instead use the saved version # to clear
3018 T's entry in the worklist. */
3019 bitmap_clear_bit (interesting_names, version);
3022 /* The first phase in degenerate PHI elimination.
3024 Eliminate the degenerate PHIs in BB, then recurse on the
3025 dominator children of BB. */
3027 static void
3028 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
3030 gphi_iterator gsi;
3031 basic_block son;
3033 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3035 gphi *phi = gsi.phi ();
3037 eliminate_const_or_copy (phi, interesting_names);
3040 /* Recurse into the dominator children of BB. */
3041 for (son = first_dom_son (CDI_DOMINATORS, bb);
3042 son;
3043 son = next_dom_son (CDI_DOMINATORS, son))
3044 eliminate_degenerate_phis_1 (son, interesting_names);
3048 /* A very simple pass to eliminate degenerate PHI nodes from the
3049 IL. This is meant to be fast enough to be able to be run several
3050 times in the optimization pipeline.
3052 Certain optimizations, particularly those which duplicate blocks
3053 or remove edges from the CFG can create or expose PHIs which are
3054 trivial copies or constant initializations.
3056 While we could pick up these optimizations in DOM or with the
3057 combination of copy-prop and CCP, those solutions are far too
3058 heavy-weight for our needs.
3060 This implementation has two phases so that we can efficiently
3061 eliminate the first order degenerate PHIs and second order
3062 degenerate PHIs.
3064 The first phase performs a dominator walk to identify and eliminate
3065 the vast majority of the degenerate PHIs. When a degenerate PHI
3066 is identified and eliminated any affected statements or PHIs
3067 are put on a worklist.
3069 The second phase eliminates degenerate PHIs and trivial copies
3070 or constant initializations using the worklist. This is how we
3071 pick up the secondary optimization opportunities with minimal
3072 cost. */
3074 namespace {
3076 const pass_data pass_data_phi_only_cprop =
3078 GIMPLE_PASS, /* type */
3079 "phicprop", /* name */
3080 OPTGROUP_NONE, /* optinfo_flags */
3081 TV_TREE_PHI_CPROP, /* tv_id */
3082 ( PROP_cfg | PROP_ssa ), /* properties_required */
3083 0, /* properties_provided */
3084 0, /* properties_destroyed */
3085 0, /* todo_flags_start */
3086 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
3089 class pass_phi_only_cprop : public gimple_opt_pass
3091 public:
3092 pass_phi_only_cprop (gcc::context *ctxt)
3093 : gimple_opt_pass (pass_data_phi_only_cprop, ctxt)
3096 /* opt_pass methods: */
3097 opt_pass * clone () { return new pass_phi_only_cprop (m_ctxt); }
3098 virtual bool gate (function *) { return flag_tree_dom != 0; }
3099 virtual unsigned int execute (function *);
3101 }; // class pass_phi_only_cprop
3103 unsigned int
3104 pass_phi_only_cprop::execute (function *fun)
3106 bitmap interesting_names;
3107 bitmap interesting_names1;
3109 /* Bitmap of blocks which need EH information updated. We can not
3110 update it on-the-fly as doing so invalidates the dominator tree. */
3111 need_eh_cleanup = BITMAP_ALLOC (NULL);
3113 /* INTERESTING_NAMES is effectively our worklist, indexed by
3114 SSA_NAME_VERSION.
3116 A set bit indicates that the statement or PHI node which
3117 defines the SSA_NAME should be (re)examined to determine if
3118 it has become a degenerate PHI or trivial const/copy propagation
3119 opportunity.
3121 Experiments have show we generally get better compilation
3122 time behavior with bitmaps rather than sbitmaps. */
3123 interesting_names = BITMAP_ALLOC (NULL);
3124 interesting_names1 = BITMAP_ALLOC (NULL);
3126 calculate_dominance_info (CDI_DOMINATORS);
3127 cfg_altered = false;
3129 /* First phase. Eliminate degenerate PHIs via a dominator
3130 walk of the CFG.
3132 Experiments have indicated that we generally get better
3133 compile-time behavior by visiting blocks in the first
3134 phase in dominator order. Presumably this is because walking
3135 in dominator order leaves fewer PHIs for later examination
3136 by the worklist phase. */
3137 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR_FOR_FN (fun),
3138 interesting_names);
3140 /* Second phase. Eliminate second order degenerate PHIs as well
3141 as trivial copies or constant initializations identified by
3142 the first phase or this phase. Basically we keep iterating
3143 until our set of INTERESTING_NAMEs is empty. */
3144 while (!bitmap_empty_p (interesting_names))
3146 unsigned int i;
3147 bitmap_iterator bi;
3149 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3150 changed during the loop. Copy it to another bitmap and
3151 use that. */
3152 bitmap_copy (interesting_names1, interesting_names);
3154 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3156 tree name = ssa_name (i);
3158 /* Ignore SSA_NAMEs that have been released because
3159 their defining statement was deleted (unreachable). */
3160 if (name)
3161 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3162 interesting_names);
3166 if (cfg_altered)
3168 free_dominance_info (CDI_DOMINATORS);
3169 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3170 loops_state_set (LOOPS_NEED_FIXUP);
3173 /* Propagation of const and copies may make some EH edges dead. Purge
3174 such edges from the CFG as needed. */
3175 if (!bitmap_empty_p (need_eh_cleanup))
3177 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3178 BITMAP_FREE (need_eh_cleanup);
3181 BITMAP_FREE (interesting_names);
3182 BITMAP_FREE (interesting_names1);
3183 return 0;
3186 } // anon namespace
3188 gimple_opt_pass *
3189 make_pass_phi_only_cprop (gcc::context *ctxt)
3191 return new pass_phi_only_cprop (ctxt);