* tree-ssa-loop-ivopts.c (strip_offset_1): Change parameter type.
[official-gcc.git] / gcc / tree-ssa-dom.c
blob211bfcf5cc4e7787af154643e0f30ebe82cdeed4
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hash-table.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "cfgloop.h"
31 #include "function.h"
32 #include "gimple-pretty-print.h"
33 #include "gimple.h"
34 #include "gimple-ssa.h"
35 #include "tree-cfg.h"
36 #include "tree-phinodes.h"
37 #include "ssa-iterators.h"
38 #include "tree-ssanames.h"
39 #include "tree-into-ssa.h"
40 #include "domwalk.h"
41 #include "tree-pass.h"
42 #include "tree-ssa-propagate.h"
43 #include "tree-ssa-threadupdate.h"
44 #include "langhooks.h"
45 #include "params.h"
46 #include "tree-ssa-threadedge.h"
47 #include "tree-ssa-dom.h"
49 /* This file implements optimizations on the dominator tree. */
51 /* Representation of a "naked" right-hand-side expression, to be used
52 in recording available expressions in the expression hash table. */
54 enum expr_kind
56 EXPR_SINGLE,
57 EXPR_UNARY,
58 EXPR_BINARY,
59 EXPR_TERNARY,
60 EXPR_CALL,
61 EXPR_PHI
64 struct hashable_expr
66 tree type;
67 enum expr_kind kind;
68 union {
69 struct { tree rhs; } single;
70 struct { enum tree_code op; tree opnd; } unary;
71 struct { enum tree_code op; tree opnd0, opnd1; } binary;
72 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
73 struct { gimple fn_from; bool pure; size_t nargs; tree *args; } call;
74 struct { size_t nargs; tree *args; } phi;
75 } ops;
78 /* Structure for recording known values of a conditional expression
79 at the exits from its block. */
81 typedef struct cond_equivalence_s
83 struct hashable_expr cond;
84 tree value;
85 } cond_equivalence;
88 /* Structure for recording edge equivalences as well as any pending
89 edge redirections during the dominator optimizer.
91 Computing and storing the edge equivalences instead of creating
92 them on-demand can save significant amounts of time, particularly
93 for pathological cases involving switch statements.
95 These structures live for a single iteration of the dominator
96 optimizer in the edge's AUX field. At the end of an iteration we
97 free each of these structures and update the AUX field to point
98 to any requested redirection target (the code for updating the
99 CFG and SSA graph for edge redirection expects redirection edge
100 targets to be in the AUX field for each edge. */
102 struct edge_info
104 /* If this edge creates a simple equivalence, the LHS and RHS of
105 the equivalence will be stored here. */
106 tree lhs;
107 tree rhs;
109 /* Traversing an edge may also indicate one or more particular conditions
110 are true or false. */
111 vec<cond_equivalence> cond_equivalences;
114 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
115 expressions it enters into the hash table along with a marker entry
116 (null). When we finish processing the block, we pop off entries and
117 remove the expressions from the global hash table until we hit the
118 marker. */
119 typedef struct expr_hash_elt * expr_hash_elt_t;
121 static vec<expr_hash_elt_t> avail_exprs_stack;
123 /* Structure for entries in the expression hash table. */
125 struct expr_hash_elt
127 /* The value (lhs) of this expression. */
128 tree lhs;
130 /* The expression (rhs) we want to record. */
131 struct hashable_expr expr;
133 /* The stmt pointer if this element corresponds to a statement. */
134 gimple stmt;
136 /* The hash value for RHS. */
137 hashval_t hash;
139 /* A unique stamp, typically the address of the hash
140 element itself, used in removing entries from the table. */
141 struct expr_hash_elt *stamp;
144 /* Hashtable helpers. */
146 static bool hashable_expr_equal_p (const struct hashable_expr *,
147 const struct hashable_expr *);
148 static void free_expr_hash_elt (void *);
150 struct expr_elt_hasher
152 typedef expr_hash_elt value_type;
153 typedef expr_hash_elt compare_type;
154 static inline hashval_t hash (const value_type *);
155 static inline bool equal (const value_type *, const compare_type *);
156 static inline void remove (value_type *);
159 inline hashval_t
160 expr_elt_hasher::hash (const value_type *p)
162 return p->hash;
165 inline bool
166 expr_elt_hasher::equal (const value_type *p1, const compare_type *p2)
168 gimple stmt1 = p1->stmt;
169 const struct hashable_expr *expr1 = &p1->expr;
170 const struct expr_hash_elt *stamp1 = p1->stamp;
171 gimple stmt2 = p2->stmt;
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 /* FIXME tuples:
180 We add stmts to a hash table and them modify them. To detect the case
181 that we modify a stmt and then search for it, we assume that the hash
182 is always modified by that change.
183 We have to fully check why this doesn't happen on trunk or rewrite
184 this in a more reliable (and easier to understand) way. */
185 if (((const struct expr_hash_elt *)p1)->hash
186 != ((const struct expr_hash_elt *)p2)->hash)
187 return false;
189 /* In case of a collision, both RHS have to be identical and have the
190 same VUSE operands. */
191 if (hashable_expr_equal_p (expr1, expr2)
192 && types_compatible_p (expr1->type, expr2->type))
194 /* Note that STMT1 and/or STMT2 may be NULL. */
195 return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
196 == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
199 return false;
202 /* Delete an expr_hash_elt and reclaim its storage. */
204 inline void
205 expr_elt_hasher::remove (value_type *element)
207 free_expr_hash_elt (element);
210 /* Hash table with expressions made available during the renaming process.
211 When an assignment of the form X_i = EXPR is found, the statement is
212 stored in this table. If the same expression EXPR is later found on the
213 RHS of another statement, it is replaced with X_i (thus performing
214 global redundancy elimination). Similarly as we pass through conditionals
215 we record the conditional itself as having either a true or false value
216 in this table. */
217 static hash_table <expr_elt_hasher> avail_exprs;
219 /* Stack of dest,src pairs that need to be restored during finalization.
221 A NULL entry is used to mark the end of pairs which need to be
222 restored during finalization of this block. */
223 static vec<tree> const_and_copies_stack;
225 /* Track whether or not we have changed the control flow graph. */
226 static bool cfg_altered;
228 /* Bitmap of blocks that have had EH statements cleaned. We should
229 remove their dead edges eventually. */
230 static bitmap need_eh_cleanup;
232 /* Statistics for dominator optimizations. */
233 struct opt_stats_d
235 long num_stmts;
236 long num_exprs_considered;
237 long num_re;
238 long num_const_prop;
239 long num_copy_prop;
242 static struct opt_stats_d opt_stats;
244 /* Local functions. */
245 static void optimize_stmt (basic_block, gimple_stmt_iterator);
246 static tree lookup_avail_expr (gimple, bool);
247 static hashval_t avail_expr_hash (const void *);
248 static void htab_statistics (FILE *, hash_table <expr_elt_hasher>);
249 static void record_cond (cond_equivalence *);
250 static void record_const_or_copy (tree, tree);
251 static void record_equality (tree, tree);
252 static void record_equivalences_from_phis (basic_block);
253 static void record_equivalences_from_incoming_edge (basic_block);
254 static void eliminate_redundant_computations (gimple_stmt_iterator *);
255 static void record_equivalences_from_stmt (gimple, int);
256 static void remove_local_expressions_from_table (void);
257 static void restore_vars_to_original_value (void);
258 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
261 /* Given a statement STMT, initialize the hash table element pointed to
262 by ELEMENT. */
264 static void
265 initialize_hash_element (gimple stmt, tree lhs,
266 struct expr_hash_elt *element)
268 enum gimple_code code = gimple_code (stmt);
269 struct hashable_expr *expr = &element->expr;
271 if (code == GIMPLE_ASSIGN)
273 enum tree_code subcode = gimple_assign_rhs_code (stmt);
275 switch (get_gimple_rhs_class (subcode))
277 case GIMPLE_SINGLE_RHS:
278 expr->kind = EXPR_SINGLE;
279 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
280 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
281 break;
282 case GIMPLE_UNARY_RHS:
283 expr->kind = EXPR_UNARY;
284 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
285 expr->ops.unary.op = subcode;
286 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
287 break;
288 case GIMPLE_BINARY_RHS:
289 expr->kind = EXPR_BINARY;
290 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
291 expr->ops.binary.op = subcode;
292 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
293 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
294 break;
295 case GIMPLE_TERNARY_RHS:
296 expr->kind = EXPR_TERNARY;
297 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
298 expr->ops.ternary.op = subcode;
299 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
300 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
301 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
302 break;
303 default:
304 gcc_unreachable ();
307 else if (code == GIMPLE_COND)
309 expr->type = boolean_type_node;
310 expr->kind = EXPR_BINARY;
311 expr->ops.binary.op = gimple_cond_code (stmt);
312 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
313 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
315 else if (code == GIMPLE_CALL)
317 size_t nargs = gimple_call_num_args (stmt);
318 size_t i;
320 gcc_assert (gimple_call_lhs (stmt));
322 expr->type = TREE_TYPE (gimple_call_lhs (stmt));
323 expr->kind = EXPR_CALL;
324 expr->ops.call.fn_from = stmt;
326 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
327 expr->ops.call.pure = true;
328 else
329 expr->ops.call.pure = false;
331 expr->ops.call.nargs = nargs;
332 expr->ops.call.args = XCNEWVEC (tree, nargs);
333 for (i = 0; i < nargs; i++)
334 expr->ops.call.args[i] = gimple_call_arg (stmt, i);
336 else if (code == GIMPLE_SWITCH)
338 expr->type = TREE_TYPE (gimple_switch_index (stmt));
339 expr->kind = EXPR_SINGLE;
340 expr->ops.single.rhs = gimple_switch_index (stmt);
342 else if (code == GIMPLE_GOTO)
344 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
345 expr->kind = EXPR_SINGLE;
346 expr->ops.single.rhs = gimple_goto_dest (stmt);
348 else if (code == GIMPLE_PHI)
350 size_t nargs = gimple_phi_num_args (stmt);
351 size_t i;
353 expr->type = TREE_TYPE (gimple_phi_result (stmt));
354 expr->kind = EXPR_PHI;
355 expr->ops.phi.nargs = nargs;
356 expr->ops.phi.args = XCNEWVEC (tree, nargs);
358 for (i = 0; i < nargs; i++)
359 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
361 else
362 gcc_unreachable ();
364 element->lhs = lhs;
365 element->stmt = stmt;
366 element->hash = avail_expr_hash (element);
367 element->stamp = element;
370 /* Given a conditional expression COND as a tree, initialize
371 a hashable_expr expression EXPR. The conditional must be a
372 comparison or logical negation. A constant or a variable is
373 not permitted. */
375 static void
376 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
378 expr->type = boolean_type_node;
380 if (COMPARISON_CLASS_P (cond))
382 expr->kind = EXPR_BINARY;
383 expr->ops.binary.op = TREE_CODE (cond);
384 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
385 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
387 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
389 expr->kind = EXPR_UNARY;
390 expr->ops.unary.op = TRUTH_NOT_EXPR;
391 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
393 else
394 gcc_unreachable ();
397 /* Given a hashable_expr expression EXPR and an LHS,
398 initialize the hash table element pointed to by ELEMENT. */
400 static void
401 initialize_hash_element_from_expr (struct hashable_expr *expr,
402 tree lhs,
403 struct expr_hash_elt *element)
405 element->expr = *expr;
406 element->lhs = lhs;
407 element->stmt = NULL;
408 element->hash = avail_expr_hash (element);
409 element->stamp = element;
412 /* Compare two hashable_expr structures for equivalence.
413 They are considered equivalent when the the expressions
414 they denote must necessarily be equal. The logic is intended
415 to follow that of operand_equal_p in fold-const.c */
417 static bool
418 hashable_expr_equal_p (const struct hashable_expr *expr0,
419 const struct hashable_expr *expr1)
421 tree type0 = expr0->type;
422 tree type1 = expr1->type;
424 /* If either type is NULL, there is nothing to check. */
425 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
426 return false;
428 /* If both types don't have the same signedness, precision, and mode,
429 then we can't consider them equal. */
430 if (type0 != type1
431 && (TREE_CODE (type0) == ERROR_MARK
432 || TREE_CODE (type1) == ERROR_MARK
433 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
434 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
435 || TYPE_MODE (type0) != TYPE_MODE (type1)))
436 return false;
438 if (expr0->kind != expr1->kind)
439 return false;
441 switch (expr0->kind)
443 case EXPR_SINGLE:
444 return operand_equal_p (expr0->ops.single.rhs,
445 expr1->ops.single.rhs, 0);
447 case EXPR_UNARY:
448 if (expr0->ops.unary.op != expr1->ops.unary.op)
449 return false;
451 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
452 || expr0->ops.unary.op == NON_LVALUE_EXPR)
453 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
454 return false;
456 return operand_equal_p (expr0->ops.unary.opnd,
457 expr1->ops.unary.opnd, 0);
459 case EXPR_BINARY:
460 if (expr0->ops.binary.op != expr1->ops.binary.op)
461 return false;
463 if (operand_equal_p (expr0->ops.binary.opnd0,
464 expr1->ops.binary.opnd0, 0)
465 && operand_equal_p (expr0->ops.binary.opnd1,
466 expr1->ops.binary.opnd1, 0))
467 return true;
469 /* For commutative ops, allow the other order. */
470 return (commutative_tree_code (expr0->ops.binary.op)
471 && operand_equal_p (expr0->ops.binary.opnd0,
472 expr1->ops.binary.opnd1, 0)
473 && operand_equal_p (expr0->ops.binary.opnd1,
474 expr1->ops.binary.opnd0, 0));
476 case EXPR_TERNARY:
477 if (expr0->ops.ternary.op != expr1->ops.ternary.op
478 || !operand_equal_p (expr0->ops.ternary.opnd2,
479 expr1->ops.ternary.opnd2, 0))
480 return false;
482 if (operand_equal_p (expr0->ops.ternary.opnd0,
483 expr1->ops.ternary.opnd0, 0)
484 && operand_equal_p (expr0->ops.ternary.opnd1,
485 expr1->ops.ternary.opnd1, 0))
486 return true;
488 /* For commutative ops, allow the other order. */
489 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
490 && operand_equal_p (expr0->ops.ternary.opnd0,
491 expr1->ops.ternary.opnd1, 0)
492 && operand_equal_p (expr0->ops.ternary.opnd1,
493 expr1->ops.ternary.opnd0, 0));
495 case EXPR_CALL:
497 size_t i;
499 /* If the calls are to different functions, then they
500 clearly cannot be equal. */
501 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
502 expr1->ops.call.fn_from))
503 return false;
505 if (! expr0->ops.call.pure)
506 return false;
508 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
509 return false;
511 for (i = 0; i < expr0->ops.call.nargs; i++)
512 if (! operand_equal_p (expr0->ops.call.args[i],
513 expr1->ops.call.args[i], 0))
514 return false;
516 return true;
519 case EXPR_PHI:
521 size_t i;
523 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
524 return false;
526 for (i = 0; i < expr0->ops.phi.nargs; i++)
527 if (! operand_equal_p (expr0->ops.phi.args[i],
528 expr1->ops.phi.args[i], 0))
529 return false;
531 return true;
534 default:
535 gcc_unreachable ();
539 /* Compute a hash value for a hashable_expr value EXPR and a
540 previously accumulated hash value VAL. If two hashable_expr
541 values compare equal with hashable_expr_equal_p, they must
542 hash to the same value, given an identical value of VAL.
543 The logic is intended to follow iterative_hash_expr in tree.c. */
545 static hashval_t
546 iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val)
548 switch (expr->kind)
550 case EXPR_SINGLE:
551 val = iterative_hash_expr (expr->ops.single.rhs, val);
552 break;
554 case EXPR_UNARY:
555 val = iterative_hash_object (expr->ops.unary.op, val);
557 /* Make sure to include signedness in the hash computation.
558 Don't hash the type, that can lead to having nodes which
559 compare equal according to operand_equal_p, but which
560 have different hash codes. */
561 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
562 || expr->ops.unary.op == NON_LVALUE_EXPR)
563 val += TYPE_UNSIGNED (expr->type);
565 val = iterative_hash_expr (expr->ops.unary.opnd, val);
566 break;
568 case EXPR_BINARY:
569 val = iterative_hash_object (expr->ops.binary.op, val);
570 if (commutative_tree_code (expr->ops.binary.op))
571 val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0,
572 expr->ops.binary.opnd1, val);
573 else
575 val = iterative_hash_expr (expr->ops.binary.opnd0, val);
576 val = iterative_hash_expr (expr->ops.binary.opnd1, val);
578 break;
580 case EXPR_TERNARY:
581 val = iterative_hash_object (expr->ops.ternary.op, val);
582 if (commutative_ternary_tree_code (expr->ops.ternary.op))
583 val = iterative_hash_exprs_commutative (expr->ops.ternary.opnd0,
584 expr->ops.ternary.opnd1, val);
585 else
587 val = iterative_hash_expr (expr->ops.ternary.opnd0, val);
588 val = iterative_hash_expr (expr->ops.ternary.opnd1, val);
590 val = iterative_hash_expr (expr->ops.ternary.opnd2, val);
591 break;
593 case EXPR_CALL:
595 size_t i;
596 enum tree_code code = CALL_EXPR;
597 gimple fn_from;
599 val = iterative_hash_object (code, val);
600 fn_from = expr->ops.call.fn_from;
601 if (gimple_call_internal_p (fn_from))
602 val = iterative_hash_hashval_t
603 ((hashval_t) gimple_call_internal_fn (fn_from), val);
604 else
605 val = iterative_hash_expr (gimple_call_fn (fn_from), val);
606 for (i = 0; i < expr->ops.call.nargs; i++)
607 val = iterative_hash_expr (expr->ops.call.args[i], val);
609 break;
611 case EXPR_PHI:
613 size_t i;
615 for (i = 0; i < expr->ops.phi.nargs; i++)
616 val = iterative_hash_expr (expr->ops.phi.args[i], val);
618 break;
620 default:
621 gcc_unreachable ();
624 return val;
627 /* Print a diagnostic dump of an expression hash table entry. */
629 static void
630 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
632 if (element->stmt)
633 fprintf (stream, "STMT ");
634 else
635 fprintf (stream, "COND ");
637 if (element->lhs)
639 print_generic_expr (stream, element->lhs, 0);
640 fprintf (stream, " = ");
643 switch (element->expr.kind)
645 case EXPR_SINGLE:
646 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
647 break;
649 case EXPR_UNARY:
650 fprintf (stream, "%s ", get_tree_code_name (element->expr.ops.unary.op));
651 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
652 break;
654 case EXPR_BINARY:
655 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
656 fprintf (stream, " %s ", get_tree_code_name (element->expr.ops.binary.op));
657 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
658 break;
660 case EXPR_TERNARY:
661 fprintf (stream, " %s <", get_tree_code_name (element->expr.ops.ternary.op));
662 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
663 fputs (", ", stream);
664 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
665 fputs (", ", stream);
666 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
667 fputs (">", stream);
668 break;
670 case EXPR_CALL:
672 size_t i;
673 size_t nargs = element->expr.ops.call.nargs;
674 gimple fn_from;
676 fn_from = element->expr.ops.call.fn_from;
677 if (gimple_call_internal_p (fn_from))
678 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
679 stream);
680 else
681 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
682 fprintf (stream, " (");
683 for (i = 0; i < nargs; i++)
685 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
686 if (i + 1 < nargs)
687 fprintf (stream, ", ");
689 fprintf (stream, ")");
691 break;
693 case EXPR_PHI:
695 size_t i;
696 size_t nargs = element->expr.ops.phi.nargs;
698 fprintf (stream, "PHI <");
699 for (i = 0; i < nargs; i++)
701 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
702 if (i + 1 < nargs)
703 fprintf (stream, ", ");
705 fprintf (stream, ">");
707 break;
709 fprintf (stream, "\n");
711 if (element->stmt)
713 fprintf (stream, " ");
714 print_gimple_stmt (stream, element->stmt, 0, 0);
718 /* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
720 static void
721 free_expr_hash_elt_contents (struct expr_hash_elt *element)
723 if (element->expr.kind == EXPR_CALL)
724 free (element->expr.ops.call.args);
725 else if (element->expr.kind == EXPR_PHI)
726 free (element->expr.ops.phi.args);
729 /* Delete an expr_hash_elt and reclaim its storage. */
731 static void
732 free_expr_hash_elt (void *elt)
734 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
735 free_expr_hash_elt_contents (element);
736 free (element);
739 /* Allocate an EDGE_INFO for edge E and attach it to E.
740 Return the new EDGE_INFO structure. */
742 static struct edge_info *
743 allocate_edge_info (edge e)
745 struct edge_info *edge_info;
747 edge_info = XCNEW (struct edge_info);
749 e->aux = edge_info;
750 return edge_info;
753 /* Free all EDGE_INFO structures associated with edges in the CFG.
754 If a particular edge can be threaded, copy the redirection
755 target from the EDGE_INFO structure into the edge's AUX field
756 as required by code to update the CFG and SSA graph for
757 jump threading. */
759 static void
760 free_all_edge_infos (void)
762 basic_block bb;
763 edge_iterator ei;
764 edge e;
766 FOR_EACH_BB (bb)
768 FOR_EACH_EDGE (e, ei, bb->preds)
770 struct edge_info *edge_info = (struct edge_info *) e->aux;
772 if (edge_info)
774 edge_info->cond_equivalences.release ();
775 free (edge_info);
776 e->aux = NULL;
782 class dom_opt_dom_walker : public dom_walker
784 public:
785 dom_opt_dom_walker (cdi_direction direction)
786 : dom_walker (direction), m_dummy_cond (NULL) {}
788 virtual void before_dom_children (basic_block);
789 virtual void after_dom_children (basic_block);
791 private:
792 void thread_across_edge (edge);
794 gimple m_dummy_cond;
797 /* Jump threading, redundancy elimination and const/copy propagation.
799 This pass may expose new symbols that need to be renamed into SSA. For
800 every new symbol exposed, its corresponding bit will be set in
801 VARS_TO_RENAME. */
803 static unsigned int
804 tree_ssa_dominator_optimize (void)
806 memset (&opt_stats, 0, sizeof (opt_stats));
808 /* Create our hash tables. */
809 avail_exprs.create (1024);
810 avail_exprs_stack.create (20);
811 const_and_copies_stack.create (20);
812 need_eh_cleanup = BITMAP_ALLOC (NULL);
814 calculate_dominance_info (CDI_DOMINATORS);
815 cfg_altered = false;
817 /* We need to know loop structures in order to avoid destroying them
818 in jump threading. Note that we still can e.g. thread through loop
819 headers to an exit edge, or through loop header to the loop body, assuming
820 that we update the loop info. */
821 loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
823 /* Initialize the value-handle array. */
824 threadedge_initialize_values ();
826 /* We need accurate information regarding back edges in the CFG
827 for jump threading; this may include back edges that are not part of
828 a single loop. */
829 mark_dfs_back_edges ();
831 /* Recursively walk the dominator tree optimizing statements. */
832 dom_opt_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
835 gimple_stmt_iterator gsi;
836 basic_block bb;
837 FOR_EACH_BB (bb)
839 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
840 update_stmt_if_modified (gsi_stmt (gsi));
844 /* If we exposed any new variables, go ahead and put them into
845 SSA form now, before we handle jump threading. This simplifies
846 interactions between rewriting of _DECL nodes into SSA form
847 and rewriting SSA_NAME nodes into SSA form after block
848 duplication and CFG manipulation. */
849 update_ssa (TODO_update_ssa);
851 free_all_edge_infos ();
853 /* Thread jumps, creating duplicate blocks as needed. */
854 cfg_altered |= thread_through_all_blocks (first_pass_instance);
856 if (cfg_altered)
857 free_dominance_info (CDI_DOMINATORS);
859 /* Removal of statements may make some EH edges dead. Purge
860 such edges from the CFG as needed. */
861 if (!bitmap_empty_p (need_eh_cleanup))
863 unsigned i;
864 bitmap_iterator bi;
866 /* Jump threading may have created forwarder blocks from blocks
867 needing EH cleanup; the new successor of these blocks, which
868 has inherited from the original block, needs the cleanup.
869 Don't clear bits in the bitmap, as that can break the bitmap
870 iterator. */
871 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
873 basic_block bb = BASIC_BLOCK (i);
874 if (bb == NULL)
875 continue;
876 while (single_succ_p (bb)
877 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
878 bb = single_succ (bb);
879 if (bb == EXIT_BLOCK_PTR)
880 continue;
881 if ((unsigned) bb->index != i)
882 bitmap_set_bit (need_eh_cleanup, bb->index);
885 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
886 bitmap_clear (need_eh_cleanup);
889 statistics_counter_event (cfun, "Redundant expressions eliminated",
890 opt_stats.num_re);
891 statistics_counter_event (cfun, "Constants propagated",
892 opt_stats.num_const_prop);
893 statistics_counter_event (cfun, "Copies propagated",
894 opt_stats.num_copy_prop);
896 /* Debugging dumps. */
897 if (dump_file && (dump_flags & TDF_STATS))
898 dump_dominator_optimization_stats (dump_file);
900 loop_optimizer_finalize ();
902 /* Delete our main hashtable. */
903 avail_exprs.dispose ();
905 /* Free asserted bitmaps and stacks. */
906 BITMAP_FREE (need_eh_cleanup);
908 avail_exprs_stack.release ();
909 const_and_copies_stack.release ();
911 /* Free the value-handle array. */
912 threadedge_finalize_values ();
914 return 0;
917 static bool
918 gate_dominator (void)
920 return flag_tree_dom != 0;
923 namespace {
925 const pass_data pass_data_dominator =
927 GIMPLE_PASS, /* type */
928 "dom", /* name */
929 OPTGROUP_NONE, /* optinfo_flags */
930 true, /* has_gate */
931 true, /* has_execute */
932 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
933 ( PROP_cfg | PROP_ssa ), /* properties_required */
934 0, /* properties_provided */
935 0, /* properties_destroyed */
936 0, /* todo_flags_start */
937 ( TODO_cleanup_cfg | TODO_update_ssa
938 | TODO_verify_ssa
939 | TODO_verify_flow ), /* todo_flags_finish */
942 class pass_dominator : public gimple_opt_pass
944 public:
945 pass_dominator (gcc::context *ctxt)
946 : gimple_opt_pass (pass_data_dominator, ctxt)
949 /* opt_pass methods: */
950 opt_pass * clone () { return new pass_dominator (m_ctxt); }
951 bool gate () { return gate_dominator (); }
952 unsigned int execute () { return tree_ssa_dominator_optimize (); }
954 }; // class pass_dominator
956 } // anon namespace
958 gimple_opt_pass *
959 make_pass_dominator (gcc::context *ctxt)
961 return new pass_dominator (ctxt);
965 /* Given a conditional statement CONDSTMT, convert the
966 condition to a canonical form. */
968 static void
969 canonicalize_comparison (gimple condstmt)
971 tree op0;
972 tree op1;
973 enum tree_code code;
975 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
977 op0 = gimple_cond_lhs (condstmt);
978 op1 = gimple_cond_rhs (condstmt);
980 code = gimple_cond_code (condstmt);
982 /* If it would be profitable to swap the operands, then do so to
983 canonicalize the statement, enabling better optimization.
985 By placing canonicalization of such expressions here we
986 transparently keep statements in canonical form, even
987 when the statement is modified. */
988 if (tree_swap_operands_p (op0, op1, false))
990 /* For relationals we need to swap the operands
991 and change the code. */
992 if (code == LT_EXPR
993 || code == GT_EXPR
994 || code == LE_EXPR
995 || code == GE_EXPR)
997 code = swap_tree_comparison (code);
999 gimple_cond_set_code (condstmt, code);
1000 gimple_cond_set_lhs (condstmt, op1);
1001 gimple_cond_set_rhs (condstmt, op0);
1003 update_stmt (condstmt);
1008 /* Initialize local stacks for this optimizer and record equivalences
1009 upon entry to BB. Equivalences can come from the edge traversed to
1010 reach BB or they may come from PHI nodes at the start of BB. */
1012 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
1013 LIMIT entries left in LOCALs. */
1015 static void
1016 remove_local_expressions_from_table (void)
1018 /* Remove all the expressions made available in this block. */
1019 while (avail_exprs_stack.length () > 0)
1021 expr_hash_elt_t victim = avail_exprs_stack.pop ();
1022 expr_hash_elt **slot;
1024 if (victim == NULL)
1025 break;
1027 /* This must precede the actual removal from the hash table,
1028 as ELEMENT and the table entry may share a call argument
1029 vector which will be freed during removal. */
1030 if (dump_file && (dump_flags & TDF_DETAILS))
1032 fprintf (dump_file, "<<<< ");
1033 print_expr_hash_elt (dump_file, victim);
1036 slot = avail_exprs.find_slot_with_hash (victim, victim->hash, NO_INSERT);
1037 gcc_assert (slot && *slot == victim);
1038 avail_exprs.clear_slot (slot);
1042 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
1043 CONST_AND_COPIES to its original state, stopping when we hit a
1044 NULL marker. */
1046 static void
1047 restore_vars_to_original_value (void)
1049 while (const_and_copies_stack.length () > 0)
1051 tree prev_value, dest;
1053 dest = const_and_copies_stack.pop ();
1055 if (dest == NULL)
1056 break;
1058 if (dump_file && (dump_flags & TDF_DETAILS))
1060 fprintf (dump_file, "<<<< COPY ");
1061 print_generic_expr (dump_file, dest, 0);
1062 fprintf (dump_file, " = ");
1063 print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
1064 fprintf (dump_file, "\n");
1067 prev_value = const_and_copies_stack.pop ();
1068 set_ssa_name_value (dest, prev_value);
1072 /* A trivial wrapper so that we can present the generic jump
1073 threading code with a simple API for simplifying statements. */
1074 static tree
1075 simplify_stmt_for_jump_threading (gimple stmt,
1076 gimple within_stmt ATTRIBUTE_UNUSED)
1078 return lookup_avail_expr (stmt, false);
1081 /* Record into the equivalence tables any equivalences implied by
1082 traversing edge E (which are cached in E->aux).
1084 Callers are responsible for managing the unwinding markers. */
1085 static void
1086 record_temporary_equivalences (edge e)
1088 int i;
1089 struct edge_info *edge_info = (struct edge_info *) e->aux;
1091 /* If we have info associated with this edge, record it into
1092 our equivalence tables. */
1093 if (edge_info)
1095 cond_equivalence *eq;
1096 tree lhs = edge_info->lhs;
1097 tree rhs = edge_info->rhs;
1099 /* If we have a simple NAME = VALUE equivalence, record it. */
1100 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1101 record_const_or_copy (lhs, rhs);
1103 /* If we have 0 = COND or 1 = COND equivalences, record them
1104 into our expression hash tables. */
1105 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1106 record_cond (eq);
1110 /* Wrapper for common code to attempt to thread an edge. For example,
1111 it handles lazily building the dummy condition and the bookkeeping
1112 when jump threading is successful. */
1114 void
1115 dom_opt_dom_walker::thread_across_edge (edge e)
1117 if (! m_dummy_cond)
1118 m_dummy_cond =
1119 gimple_build_cond (NE_EXPR,
1120 integer_zero_node, integer_zero_node,
1121 NULL, NULL);
1123 /* Push a marker on both stacks so we can unwind the tables back to their
1124 current state. */
1125 avail_exprs_stack.safe_push (NULL);
1126 const_and_copies_stack.safe_push (NULL_TREE);
1128 /* Traversing E may result in equivalences we can utilize. */
1129 record_temporary_equivalences (e);
1131 /* With all the edge equivalences in the tables, go ahead and attempt
1132 to thread through E->dest. */
1133 ::thread_across_edge (m_dummy_cond, e, false,
1134 &const_and_copies_stack,
1135 simplify_stmt_for_jump_threading);
1137 /* And restore the various tables to their state before
1138 we threaded this edge.
1140 XXX The code in tree-ssa-threadedge.c will restore the state of
1141 the const_and_copies table. We we just have to restore the expression
1142 table. */
1143 remove_local_expressions_from_table ();
1146 /* PHI nodes can create equivalences too.
1148 Ignoring any alternatives which are the same as the result, if
1149 all the alternatives are equal, then the PHI node creates an
1150 equivalence. */
1152 static void
1153 record_equivalences_from_phis (basic_block bb)
1155 gimple_stmt_iterator gsi;
1157 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1159 gimple phi = gsi_stmt (gsi);
1161 tree lhs = gimple_phi_result (phi);
1162 tree rhs = NULL;
1163 size_t i;
1165 for (i = 0; i < gimple_phi_num_args (phi); i++)
1167 tree t = gimple_phi_arg_def (phi, i);
1169 /* Ignore alternatives which are the same as our LHS. Since
1170 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1171 can simply compare pointers. */
1172 if (lhs == t)
1173 continue;
1175 /* If we have not processed an alternative yet, then set
1176 RHS to this alternative. */
1177 if (rhs == NULL)
1178 rhs = t;
1179 /* If we have processed an alternative (stored in RHS), then
1180 see if it is equal to this one. If it isn't, then stop
1181 the search. */
1182 else if (! operand_equal_for_phi_arg_p (rhs, t))
1183 break;
1186 /* If we had no interesting alternatives, then all the RHS alternatives
1187 must have been the same as LHS. */
1188 if (!rhs)
1189 rhs = lhs;
1191 /* If we managed to iterate through each PHI alternative without
1192 breaking out of the loop, then we have a PHI which may create
1193 a useful equivalence. We do not need to record unwind data for
1194 this, since this is a true assignment and not an equivalence
1195 inferred from a comparison. All uses of this ssa name are dominated
1196 by this assignment, so unwinding just costs time and space. */
1197 if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs))
1198 set_ssa_name_value (lhs, rhs);
1202 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1203 return that edge. Otherwise return NULL. */
1204 static edge
1205 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1207 edge retval = NULL;
1208 edge e;
1209 edge_iterator ei;
1211 FOR_EACH_EDGE (e, ei, bb->preds)
1213 /* A loop back edge can be identified by the destination of
1214 the edge dominating the source of the edge. */
1215 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1216 continue;
1218 /* If we have already seen a non-loop edge, then we must have
1219 multiple incoming non-loop edges and thus we return NULL. */
1220 if (retval)
1221 return NULL;
1223 /* This is the first non-loop incoming edge we have found. Record
1224 it. */
1225 retval = e;
1228 return retval;
1231 /* Record any equivalences created by the incoming edge to BB. If BB
1232 has more than one incoming edge, then no equivalence is created. */
1234 static void
1235 record_equivalences_from_incoming_edge (basic_block bb)
1237 edge e;
1238 basic_block parent;
1239 struct edge_info *edge_info;
1241 /* If our parent block ended with a control statement, then we may be
1242 able to record some equivalences based on which outgoing edge from
1243 the parent was followed. */
1244 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1246 e = single_incoming_edge_ignoring_loop_edges (bb);
1248 /* If we had a single incoming edge from our parent block, then enter
1249 any data associated with the edge into our tables. */
1250 if (e && e->src == parent)
1252 unsigned int i;
1254 edge_info = (struct edge_info *) e->aux;
1256 if (edge_info)
1258 tree lhs = edge_info->lhs;
1259 tree rhs = edge_info->rhs;
1260 cond_equivalence *eq;
1262 if (lhs)
1263 record_equality (lhs, rhs);
1265 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1266 set via a widening type conversion, then we may be able to record
1267 additional equivalences. */
1268 if (lhs
1269 && TREE_CODE (lhs) == SSA_NAME
1270 && is_gimple_constant (rhs)
1271 && TREE_CODE (rhs) == INTEGER_CST)
1273 gimple defstmt = SSA_NAME_DEF_STMT (lhs);
1275 if (defstmt
1276 && is_gimple_assign (defstmt)
1277 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
1279 tree old_rhs = gimple_assign_rhs1 (defstmt);
1281 /* If the conversion widens the original value and
1282 the constant is in the range of the type of OLD_RHS,
1283 then convert the constant and record the equivalence.
1285 Note that int_fits_type_p does not check the precision
1286 if the upper and lower bounds are OK. */
1287 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
1288 && (TYPE_PRECISION (TREE_TYPE (lhs))
1289 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
1290 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
1292 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1293 record_equality (old_rhs, newval);
1298 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1299 record_cond (eq);
1304 /* Dump SSA statistics on FILE. */
1306 void
1307 dump_dominator_optimization_stats (FILE *file)
1309 fprintf (file, "Total number of statements: %6ld\n\n",
1310 opt_stats.num_stmts);
1311 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1312 opt_stats.num_exprs_considered);
1314 fprintf (file, "\nHash table statistics:\n");
1316 fprintf (file, " avail_exprs: ");
1317 htab_statistics (file, avail_exprs);
1321 /* Dump SSA statistics on stderr. */
1323 DEBUG_FUNCTION void
1324 debug_dominator_optimization_stats (void)
1326 dump_dominator_optimization_stats (stderr);
1330 /* Dump statistics for the hash table HTAB. */
1332 static void
1333 htab_statistics (FILE *file, hash_table <expr_elt_hasher> htab)
1335 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1336 (long) htab.size (),
1337 (long) htab.elements (),
1338 htab.collisions ());
1342 /* Enter condition equivalence into the expression hash table.
1343 This indicates that a conditional expression has a known
1344 boolean value. */
1346 static void
1347 record_cond (cond_equivalence *p)
1349 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1350 expr_hash_elt **slot;
1352 initialize_hash_element_from_expr (&p->cond, p->value, element);
1354 slot = avail_exprs.find_slot_with_hash (element, element->hash, INSERT);
1355 if (*slot == NULL)
1357 *slot = element;
1359 if (dump_file && (dump_flags & TDF_DETAILS))
1361 fprintf (dump_file, "1>>> ");
1362 print_expr_hash_elt (dump_file, element);
1365 avail_exprs_stack.safe_push (element);
1367 else
1368 free_expr_hash_elt (element);
1371 /* Build a cond_equivalence record indicating that the comparison
1372 CODE holds between operands OP0 and OP1 and push it to **P. */
1374 static void
1375 build_and_record_new_cond (enum tree_code code,
1376 tree op0, tree op1,
1377 vec<cond_equivalence> *p)
1379 cond_equivalence c;
1380 struct hashable_expr *cond = &c.cond;
1382 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1384 cond->type = boolean_type_node;
1385 cond->kind = EXPR_BINARY;
1386 cond->ops.binary.op = code;
1387 cond->ops.binary.opnd0 = op0;
1388 cond->ops.binary.opnd1 = op1;
1390 c.value = boolean_true_node;
1391 p->safe_push (c);
1394 /* Record that COND is true and INVERTED is false into the edge information
1395 structure. Also record that any conditions dominated by COND are true
1396 as well.
1398 For example, if a < b is true, then a <= b must also be true. */
1400 static void
1401 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1403 tree op0, op1;
1404 cond_equivalence c;
1406 if (!COMPARISON_CLASS_P (cond))
1407 return;
1409 op0 = TREE_OPERAND (cond, 0);
1410 op1 = TREE_OPERAND (cond, 1);
1412 switch (TREE_CODE (cond))
1414 case LT_EXPR:
1415 case GT_EXPR:
1416 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1418 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1419 &edge_info->cond_equivalences);
1420 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1421 &edge_info->cond_equivalences);
1424 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1425 ? LE_EXPR : GE_EXPR),
1426 op0, op1, &edge_info->cond_equivalences);
1427 build_and_record_new_cond (NE_EXPR, op0, op1,
1428 &edge_info->cond_equivalences);
1429 break;
1431 case GE_EXPR:
1432 case LE_EXPR:
1433 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1435 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1436 &edge_info->cond_equivalences);
1438 break;
1440 case EQ_EXPR:
1441 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1443 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1444 &edge_info->cond_equivalences);
1446 build_and_record_new_cond (LE_EXPR, op0, op1,
1447 &edge_info->cond_equivalences);
1448 build_and_record_new_cond (GE_EXPR, op0, op1,
1449 &edge_info->cond_equivalences);
1450 break;
1452 case UNORDERED_EXPR:
1453 build_and_record_new_cond (NE_EXPR, op0, op1,
1454 &edge_info->cond_equivalences);
1455 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1456 &edge_info->cond_equivalences);
1457 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1458 &edge_info->cond_equivalences);
1459 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1460 &edge_info->cond_equivalences);
1461 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1462 &edge_info->cond_equivalences);
1463 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1464 &edge_info->cond_equivalences);
1465 break;
1467 case UNLT_EXPR:
1468 case UNGT_EXPR:
1469 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1470 ? UNLE_EXPR : UNGE_EXPR),
1471 op0, op1, &edge_info->cond_equivalences);
1472 build_and_record_new_cond (NE_EXPR, op0, op1,
1473 &edge_info->cond_equivalences);
1474 break;
1476 case UNEQ_EXPR:
1477 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1478 &edge_info->cond_equivalences);
1479 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1480 &edge_info->cond_equivalences);
1481 break;
1483 case LTGT_EXPR:
1484 build_and_record_new_cond (NE_EXPR, op0, op1,
1485 &edge_info->cond_equivalences);
1486 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1487 &edge_info->cond_equivalences);
1488 break;
1490 default:
1491 break;
1494 /* Now store the original true and false conditions into the first
1495 two slots. */
1496 initialize_expr_from_cond (cond, &c.cond);
1497 c.value = boolean_true_node;
1498 edge_info->cond_equivalences.safe_push (c);
1500 /* It is possible for INVERTED to be the negation of a comparison,
1501 and not a valid RHS or GIMPLE_COND condition. This happens because
1502 invert_truthvalue may return such an expression when asked to invert
1503 a floating-point comparison. These comparisons are not assumed to
1504 obey the trichotomy law. */
1505 initialize_expr_from_cond (inverted, &c.cond);
1506 c.value = boolean_false_node;
1507 edge_info->cond_equivalences.safe_push (c);
1510 /* A helper function for record_const_or_copy and record_equality.
1511 Do the work of recording the value and undo info. */
1513 static void
1514 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1516 set_ssa_name_value (x, y);
1518 if (dump_file && (dump_flags & TDF_DETAILS))
1520 fprintf (dump_file, "0>>> COPY ");
1521 print_generic_expr (dump_file, x, 0);
1522 fprintf (dump_file, " = ");
1523 print_generic_expr (dump_file, y, 0);
1524 fprintf (dump_file, "\n");
1527 const_and_copies_stack.reserve (2);
1528 const_and_copies_stack.quick_push (prev_x);
1529 const_and_copies_stack.quick_push (x);
1532 /* Return the loop depth of the basic block of the defining statement of X.
1533 This number should not be treated as absolutely correct because the loop
1534 information may not be completely up-to-date when dom runs. However, it
1535 will be relatively correct, and as more passes are taught to keep loop info
1536 up to date, the result will become more and more accurate. */
1539 loop_depth_of_name (tree x)
1541 gimple defstmt;
1542 basic_block defbb;
1544 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1545 if (TREE_CODE (x) != SSA_NAME)
1546 return 0;
1548 /* Otherwise return the loop depth of the defining statement's bb.
1549 Note that there may not actually be a bb for this statement, if the
1550 ssa_name is live on entry. */
1551 defstmt = SSA_NAME_DEF_STMT (x);
1552 defbb = gimple_bb (defstmt);
1553 if (!defbb)
1554 return 0;
1556 return bb_loop_depth (defbb);
1559 /* Record that X is equal to Y in const_and_copies. Record undo
1560 information in the block-local vector. */
1562 static void
1563 record_const_or_copy (tree x, tree y)
1565 tree prev_x = SSA_NAME_VALUE (x);
1567 gcc_assert (TREE_CODE (x) == SSA_NAME);
1569 if (TREE_CODE (y) == SSA_NAME)
1571 tree tmp = SSA_NAME_VALUE (y);
1572 if (tmp)
1573 y = tmp;
1576 record_const_or_copy_1 (x, y, prev_x);
1579 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1580 This constrains the cases in which we may treat this as assignment. */
1582 static void
1583 record_equality (tree x, tree y)
1585 tree prev_x = NULL, prev_y = NULL;
1587 if (TREE_CODE (x) == SSA_NAME)
1588 prev_x = SSA_NAME_VALUE (x);
1589 if (TREE_CODE (y) == SSA_NAME)
1590 prev_y = SSA_NAME_VALUE (y);
1592 /* If one of the previous values is invariant, or invariant in more loops
1593 (by depth), then use that.
1594 Otherwise it doesn't matter which value we choose, just so
1595 long as we canonicalize on one value. */
1596 if (is_gimple_min_invariant (y))
1598 else if (is_gimple_min_invariant (x)
1599 || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1600 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1601 else if (prev_x && is_gimple_min_invariant (prev_x))
1602 x = y, y = prev_x, prev_x = prev_y;
1603 else if (prev_y)
1604 y = prev_y;
1606 /* After the swapping, we must have one SSA_NAME. */
1607 if (TREE_CODE (x) != SSA_NAME)
1608 return;
1610 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1611 variable compared against zero. If we're honoring signed zeros,
1612 then we cannot record this value unless we know that the value is
1613 nonzero. */
1614 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1615 && (TREE_CODE (y) != REAL_CST
1616 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1617 return;
1619 record_const_or_copy_1 (x, y, prev_x);
1622 /* Returns true when STMT is a simple iv increment. It detects the
1623 following situation:
1625 i_1 = phi (..., i_2)
1626 i_2 = i_1 +/- ... */
1628 bool
1629 simple_iv_increment_p (gimple stmt)
1631 enum tree_code code;
1632 tree lhs, preinc;
1633 gimple phi;
1634 size_t i;
1636 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1637 return false;
1639 lhs = gimple_assign_lhs (stmt);
1640 if (TREE_CODE (lhs) != SSA_NAME)
1641 return false;
1643 code = gimple_assign_rhs_code (stmt);
1644 if (code != PLUS_EXPR
1645 && code != MINUS_EXPR
1646 && code != POINTER_PLUS_EXPR)
1647 return false;
1649 preinc = gimple_assign_rhs1 (stmt);
1650 if (TREE_CODE (preinc) != SSA_NAME)
1651 return false;
1653 phi = SSA_NAME_DEF_STMT (preinc);
1654 if (gimple_code (phi) != GIMPLE_PHI)
1655 return false;
1657 for (i = 0; i < gimple_phi_num_args (phi); i++)
1658 if (gimple_phi_arg_def (phi, i) == lhs)
1659 return true;
1661 return false;
1664 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1665 known value for that SSA_NAME (or NULL if no value is known).
1667 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1668 successors of BB. */
1670 static void
1671 cprop_into_successor_phis (basic_block bb)
1673 edge e;
1674 edge_iterator ei;
1676 FOR_EACH_EDGE (e, ei, bb->succs)
1678 int indx;
1679 gimple_stmt_iterator gsi;
1681 /* If this is an abnormal edge, then we do not want to copy propagate
1682 into the PHI alternative associated with this edge. */
1683 if (e->flags & EDGE_ABNORMAL)
1684 continue;
1686 gsi = gsi_start_phis (e->dest);
1687 if (gsi_end_p (gsi))
1688 continue;
1690 /* We may have an equivalence associated with this edge. While
1691 we can not propagate it into non-dominated blocks, we can
1692 propagate them into PHIs in non-dominated blocks. */
1694 /* Push the unwind marker so we can reset the const and copies
1695 table back to its original state after processing this edge. */
1696 const_and_copies_stack.safe_push (NULL_TREE);
1698 /* Extract and record any simple NAME = VALUE equivalences.
1700 Don't bother with [01] = COND equivalences, they're not useful
1701 here. */
1702 struct edge_info *edge_info = (struct edge_info *) e->aux;
1703 if (edge_info)
1705 tree lhs = edge_info->lhs;
1706 tree rhs = edge_info->rhs;
1708 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1709 record_const_or_copy (lhs, rhs);
1712 indx = e->dest_idx;
1713 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1715 tree new_val;
1716 use_operand_p orig_p;
1717 tree orig_val;
1718 gimple phi = gsi_stmt (gsi);
1720 /* The alternative may be associated with a constant, so verify
1721 it is an SSA_NAME before doing anything with it. */
1722 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1723 orig_val = get_use_from_ptr (orig_p);
1724 if (TREE_CODE (orig_val) != SSA_NAME)
1725 continue;
1727 /* If we have *ORIG_P in our constant/copy table, then replace
1728 ORIG_P with its value in our constant/copy table. */
1729 new_val = SSA_NAME_VALUE (orig_val);
1730 if (new_val
1731 && new_val != orig_val
1732 && (TREE_CODE (new_val) == SSA_NAME
1733 || is_gimple_min_invariant (new_val))
1734 && may_propagate_copy (orig_val, new_val))
1735 propagate_value (orig_p, new_val);
1738 restore_vars_to_original_value ();
1742 /* We have finished optimizing BB, record any information implied by
1743 taking a specific outgoing edge from BB. */
1745 static void
1746 record_edge_info (basic_block bb)
1748 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1749 struct edge_info *edge_info;
1751 if (! gsi_end_p (gsi))
1753 gimple stmt = gsi_stmt (gsi);
1754 location_t loc = gimple_location (stmt);
1756 if (gimple_code (stmt) == GIMPLE_SWITCH)
1758 tree index = gimple_switch_index (stmt);
1760 if (TREE_CODE (index) == SSA_NAME)
1762 int i;
1763 int n_labels = gimple_switch_num_labels (stmt);
1764 tree *info = XCNEWVEC (tree, last_basic_block);
1765 edge e;
1766 edge_iterator ei;
1768 for (i = 0; i < n_labels; i++)
1770 tree label = gimple_switch_label (stmt, i);
1771 basic_block target_bb = label_to_block (CASE_LABEL (label));
1772 if (CASE_HIGH (label)
1773 || !CASE_LOW (label)
1774 || info[target_bb->index])
1775 info[target_bb->index] = error_mark_node;
1776 else
1777 info[target_bb->index] = label;
1780 FOR_EACH_EDGE (e, ei, bb->succs)
1782 basic_block target_bb = e->dest;
1783 tree label = info[target_bb->index];
1785 if (label != NULL && label != error_mark_node)
1787 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1788 CASE_LOW (label));
1789 edge_info = allocate_edge_info (e);
1790 edge_info->lhs = index;
1791 edge_info->rhs = x;
1794 free (info);
1798 /* A COND_EXPR may create equivalences too. */
1799 if (gimple_code (stmt) == GIMPLE_COND)
1801 edge true_edge;
1802 edge false_edge;
1804 tree op0 = gimple_cond_lhs (stmt);
1805 tree op1 = gimple_cond_rhs (stmt);
1806 enum tree_code code = gimple_cond_code (stmt);
1808 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1810 /* Special case comparing booleans against a constant as we
1811 know the value of OP0 on both arms of the branch. i.e., we
1812 can record an equivalence for OP0 rather than COND. */
1813 if ((code == EQ_EXPR || code == NE_EXPR)
1814 && TREE_CODE (op0) == SSA_NAME
1815 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1816 && is_gimple_min_invariant (op1))
1818 if (code == EQ_EXPR)
1820 edge_info = allocate_edge_info (true_edge);
1821 edge_info->lhs = op0;
1822 edge_info->rhs = (integer_zerop (op1)
1823 ? boolean_false_node
1824 : boolean_true_node);
1826 edge_info = allocate_edge_info (false_edge);
1827 edge_info->lhs = op0;
1828 edge_info->rhs = (integer_zerop (op1)
1829 ? boolean_true_node
1830 : boolean_false_node);
1832 else
1834 edge_info = allocate_edge_info (true_edge);
1835 edge_info->lhs = op0;
1836 edge_info->rhs = (integer_zerop (op1)
1837 ? boolean_true_node
1838 : boolean_false_node);
1840 edge_info = allocate_edge_info (false_edge);
1841 edge_info->lhs = op0;
1842 edge_info->rhs = (integer_zerop (op1)
1843 ? boolean_false_node
1844 : boolean_true_node);
1847 else if (is_gimple_min_invariant (op0)
1848 && (TREE_CODE (op1) == SSA_NAME
1849 || is_gimple_min_invariant (op1)))
1851 tree cond = build2 (code, boolean_type_node, op0, op1);
1852 tree inverted = invert_truthvalue_loc (loc, cond);
1853 bool can_infer_simple_equiv
1854 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
1855 && real_zerop (op0));
1856 struct edge_info *edge_info;
1858 edge_info = allocate_edge_info (true_edge);
1859 record_conditions (edge_info, cond, inverted);
1861 if (can_infer_simple_equiv && code == EQ_EXPR)
1863 edge_info->lhs = op1;
1864 edge_info->rhs = op0;
1867 edge_info = allocate_edge_info (false_edge);
1868 record_conditions (edge_info, inverted, cond);
1870 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1872 edge_info->lhs = op1;
1873 edge_info->rhs = op0;
1877 else if (TREE_CODE (op0) == SSA_NAME
1878 && (TREE_CODE (op1) == SSA_NAME
1879 || is_gimple_min_invariant (op1)))
1881 tree cond = build2 (code, boolean_type_node, op0, op1);
1882 tree inverted = invert_truthvalue_loc (loc, cond);
1883 bool can_infer_simple_equiv
1884 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op1)))
1885 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1886 struct edge_info *edge_info;
1888 edge_info = allocate_edge_info (true_edge);
1889 record_conditions (edge_info, cond, inverted);
1891 if (can_infer_simple_equiv && code == EQ_EXPR)
1893 edge_info->lhs = op0;
1894 edge_info->rhs = op1;
1897 edge_info = allocate_edge_info (false_edge);
1898 record_conditions (edge_info, inverted, cond);
1900 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1902 edge_info->lhs = op0;
1903 edge_info->rhs = op1;
1908 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1912 void
1913 dom_opt_dom_walker::before_dom_children (basic_block bb)
1915 gimple_stmt_iterator gsi;
1917 if (dump_file && (dump_flags & TDF_DETAILS))
1918 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1920 /* Push a marker on the stacks of local information so that we know how
1921 far to unwind when we finalize this block. */
1922 avail_exprs_stack.safe_push (NULL);
1923 const_and_copies_stack.safe_push (NULL_TREE);
1925 record_equivalences_from_incoming_edge (bb);
1927 /* PHI nodes can create equivalences too. */
1928 record_equivalences_from_phis (bb);
1930 /* Create equivalences from redundant PHIs. PHIs are only truly
1931 redundant when they exist in the same block, so push another
1932 marker and unwind right afterwards. */
1933 avail_exprs_stack.safe_push (NULL);
1934 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1935 eliminate_redundant_computations (&gsi);
1936 remove_local_expressions_from_table ();
1938 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1939 optimize_stmt (bb, gsi);
1941 /* Now prepare to process dominated blocks. */
1942 record_edge_info (bb);
1943 cprop_into_successor_phis (bb);
1946 /* We have finished processing the dominator children of BB, perform
1947 any finalization actions in preparation for leaving this node in
1948 the dominator tree. */
1950 void
1951 dom_opt_dom_walker::after_dom_children (basic_block bb)
1953 gimple last;
1955 /* If we have an outgoing edge to a block with multiple incoming and
1956 outgoing edges, then we may be able to thread the edge, i.e., we
1957 may be able to statically determine which of the outgoing edges
1958 will be traversed when the incoming edge from BB is traversed. */
1959 if (single_succ_p (bb)
1960 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1961 && potentially_threadable_block (single_succ (bb)))
1963 thread_across_edge (single_succ_edge (bb));
1965 else if ((last = last_stmt (bb))
1966 && gimple_code (last) == GIMPLE_COND
1967 && EDGE_COUNT (bb->succs) == 2
1968 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1969 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1971 edge true_edge, false_edge;
1973 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1975 /* Only try to thread the edge if it reaches a target block with
1976 more than one predecessor and more than one successor. */
1977 if (potentially_threadable_block (true_edge->dest))
1978 thread_across_edge (true_edge);
1980 /* Similarly for the ELSE arm. */
1981 if (potentially_threadable_block (false_edge->dest))
1982 thread_across_edge (false_edge);
1986 /* These remove expressions local to BB from the tables. */
1987 remove_local_expressions_from_table ();
1988 restore_vars_to_original_value ();
1991 /* Search for redundant computations in STMT. If any are found, then
1992 replace them with the variable holding the result of the computation.
1994 If safe, record this expression into the available expression hash
1995 table. */
1997 static void
1998 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
2000 tree expr_type;
2001 tree cached_lhs;
2002 tree def;
2003 bool insert = true;
2004 bool assigns_var_p = false;
2006 gimple stmt = gsi_stmt (*gsi);
2008 if (gimple_code (stmt) == GIMPLE_PHI)
2009 def = gimple_phi_result (stmt);
2010 else
2011 def = gimple_get_lhs (stmt);
2013 /* Certain expressions on the RHS can be optimized away, but can not
2014 themselves be entered into the hash tables. */
2015 if (! def
2016 || TREE_CODE (def) != SSA_NAME
2017 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2018 || gimple_vdef (stmt)
2019 /* Do not record equivalences for increments of ivs. This would create
2020 overlapping live ranges for a very questionable gain. */
2021 || simple_iv_increment_p (stmt))
2022 insert = false;
2024 /* Check if the expression has been computed before. */
2025 cached_lhs = lookup_avail_expr (stmt, insert);
2027 opt_stats.num_exprs_considered++;
2029 /* Get the type of the expression we are trying to optimize. */
2030 if (is_gimple_assign (stmt))
2032 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
2033 assigns_var_p = true;
2035 else if (gimple_code (stmt) == GIMPLE_COND)
2036 expr_type = boolean_type_node;
2037 else if (is_gimple_call (stmt))
2039 gcc_assert (gimple_call_lhs (stmt));
2040 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
2041 assigns_var_p = true;
2043 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2044 expr_type = TREE_TYPE (gimple_switch_index (stmt));
2045 else if (gimple_code (stmt) == GIMPLE_PHI)
2046 /* We can't propagate into a phi, so the logic below doesn't apply.
2047 Instead record an equivalence between the cached LHS and the
2048 PHI result of this statement, provided they are in the same block.
2049 This should be sufficient to kill the redundant phi. */
2051 if (def && cached_lhs)
2052 record_const_or_copy (def, cached_lhs);
2053 return;
2055 else
2056 gcc_unreachable ();
2058 if (!cached_lhs)
2059 return;
2061 /* It is safe to ignore types here since we have already done
2062 type checking in the hashing and equality routines. In fact
2063 type checking here merely gets in the way of constant
2064 propagation. Also, make sure that it is safe to propagate
2065 CACHED_LHS into the expression in STMT. */
2066 if ((TREE_CODE (cached_lhs) != SSA_NAME
2067 && (assigns_var_p
2068 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
2069 || may_propagate_copy_into_stmt (stmt, cached_lhs))
2071 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
2072 || is_gimple_min_invariant (cached_lhs));
2074 if (dump_file && (dump_flags & TDF_DETAILS))
2076 fprintf (dump_file, " Replaced redundant expr '");
2077 print_gimple_expr (dump_file, stmt, 0, dump_flags);
2078 fprintf (dump_file, "' with '");
2079 print_generic_expr (dump_file, cached_lhs, dump_flags);
2080 fprintf (dump_file, "'\n");
2083 opt_stats.num_re++;
2085 if (assigns_var_p
2086 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
2087 cached_lhs = fold_convert (expr_type, cached_lhs);
2089 propagate_tree_value_into_stmt (gsi, cached_lhs);
2091 /* Since it is always necessary to mark the result as modified,
2092 perhaps we should move this into propagate_tree_value_into_stmt
2093 itself. */
2094 gimple_set_modified (gsi_stmt (*gsi), true);
2098 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2099 the available expressions table or the const_and_copies table.
2100 Detect and record those equivalences. */
2101 /* We handle only very simple copy equivalences here. The heavy
2102 lifing is done by eliminate_redundant_computations. */
2104 static void
2105 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
2107 tree lhs;
2108 enum tree_code lhs_code;
2110 gcc_assert (is_gimple_assign (stmt));
2112 lhs = gimple_assign_lhs (stmt);
2113 lhs_code = TREE_CODE (lhs);
2115 if (lhs_code == SSA_NAME
2116 && gimple_assign_single_p (stmt))
2118 tree rhs = gimple_assign_rhs1 (stmt);
2120 /* If the RHS of the assignment is a constant or another variable that
2121 may be propagated, register it in the CONST_AND_COPIES table. We
2122 do not need to record unwind data for this, since this is a true
2123 assignment and not an equivalence inferred from a comparison. All
2124 uses of this ssa name are dominated by this assignment, so unwinding
2125 just costs time and space. */
2126 if (may_optimize_p
2127 && (TREE_CODE (rhs) == SSA_NAME
2128 || is_gimple_min_invariant (rhs)))
2130 if (dump_file && (dump_flags & TDF_DETAILS))
2132 fprintf (dump_file, "==== ASGN ");
2133 print_generic_expr (dump_file, lhs, 0);
2134 fprintf (dump_file, " = ");
2135 print_generic_expr (dump_file, rhs, 0);
2136 fprintf (dump_file, "\n");
2139 set_ssa_name_value (lhs, rhs);
2143 /* A memory store, even an aliased store, creates a useful
2144 equivalence. By exchanging the LHS and RHS, creating suitable
2145 vops and recording the result in the available expression table,
2146 we may be able to expose more redundant loads. */
2147 if (!gimple_has_volatile_ops (stmt)
2148 && gimple_references_memory_p (stmt)
2149 && gimple_assign_single_p (stmt)
2150 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2151 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2152 && !is_gimple_reg (lhs))
2154 tree rhs = gimple_assign_rhs1 (stmt);
2155 gimple new_stmt;
2157 /* Build a new statement with the RHS and LHS exchanged. */
2158 if (TREE_CODE (rhs) == SSA_NAME)
2160 /* NOTE tuples. The call to gimple_build_assign below replaced
2161 a call to build_gimple_modify_stmt, which did not set the
2162 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2163 may cause an SSA validation failure, as the LHS may be a
2164 default-initialized name and should have no definition. I'm
2165 a bit dubious of this, as the artificial statement that we
2166 generate here may in fact be ill-formed, but it is simply
2167 used as an internal device in this pass, and never becomes
2168 part of the CFG. */
2169 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2170 new_stmt = gimple_build_assign (rhs, lhs);
2171 SSA_NAME_DEF_STMT (rhs) = defstmt;
2173 else
2174 new_stmt = gimple_build_assign (rhs, lhs);
2176 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2178 /* Finally enter the statement into the available expression
2179 table. */
2180 lookup_avail_expr (new_stmt, true);
2184 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2185 CONST_AND_COPIES. */
2187 static void
2188 cprop_operand (gimple stmt, use_operand_p op_p)
2190 tree val;
2191 tree op = USE_FROM_PTR (op_p);
2193 /* If the operand has a known constant value or it is known to be a
2194 copy of some other variable, use the value or copy stored in
2195 CONST_AND_COPIES. */
2196 val = SSA_NAME_VALUE (op);
2197 if (val && val != op)
2199 /* Do not replace hard register operands in asm statements. */
2200 if (gimple_code (stmt) == GIMPLE_ASM
2201 && !may_propagate_copy_into_asm (op))
2202 return;
2204 /* Certain operands are not allowed to be copy propagated due
2205 to their interaction with exception handling and some GCC
2206 extensions. */
2207 if (!may_propagate_copy (op, val))
2208 return;
2210 /* Do not propagate addresses that point to volatiles into memory
2211 stmts without volatile operands. */
2212 if (POINTER_TYPE_P (TREE_TYPE (val))
2213 && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
2214 && gimple_has_mem_ops (stmt)
2215 && !gimple_has_volatile_ops (stmt))
2216 return;
2218 /* Do not propagate copies if the propagated value is at a deeper loop
2219 depth than the propagatee. Otherwise, this may move loop variant
2220 variables outside of their loops and prevent coalescing
2221 opportunities. If the value was loop invariant, it will be hoisted
2222 by LICM and exposed for copy propagation. */
2223 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2224 return;
2226 /* Do not propagate copies into simple IV increment statements.
2227 See PR23821 for how this can disturb IV analysis. */
2228 if (TREE_CODE (val) != INTEGER_CST
2229 && simple_iv_increment_p (stmt))
2230 return;
2232 /* Dump details. */
2233 if (dump_file && (dump_flags & TDF_DETAILS))
2235 fprintf (dump_file, " Replaced '");
2236 print_generic_expr (dump_file, op, dump_flags);
2237 fprintf (dump_file, "' with %s '",
2238 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2239 print_generic_expr (dump_file, val, dump_flags);
2240 fprintf (dump_file, "'\n");
2243 if (TREE_CODE (val) != SSA_NAME)
2244 opt_stats.num_const_prop++;
2245 else
2246 opt_stats.num_copy_prop++;
2248 propagate_value (op_p, val);
2250 /* And note that we modified this statement. This is now
2251 safe, even if we changed virtual operands since we will
2252 rescan the statement and rewrite its operands again. */
2253 gimple_set_modified (stmt, true);
2257 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2258 known value for that SSA_NAME (or NULL if no value is known).
2260 Propagate values from CONST_AND_COPIES into the uses, vuses and
2261 vdef_ops of STMT. */
2263 static void
2264 cprop_into_stmt (gimple stmt)
2266 use_operand_p op_p;
2267 ssa_op_iter iter;
2269 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2270 cprop_operand (stmt, op_p);
2273 /* Optimize the statement pointed to by iterator SI.
2275 We try to perform some simplistic global redundancy elimination and
2276 constant propagation:
2278 1- To detect global redundancy, we keep track of expressions that have
2279 been computed in this block and its dominators. If we find that the
2280 same expression is computed more than once, we eliminate repeated
2281 computations by using the target of the first one.
2283 2- Constant values and copy assignments. This is used to do very
2284 simplistic constant and copy propagation. When a constant or copy
2285 assignment is found, we map the value on the RHS of the assignment to
2286 the variable in the LHS in the CONST_AND_COPIES table. */
2288 static void
2289 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2291 gimple stmt, old_stmt;
2292 bool may_optimize_p;
2293 bool modified_p = false;
2295 old_stmt = stmt = gsi_stmt (si);
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2299 fprintf (dump_file, "Optimizing statement ");
2300 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2303 if (gimple_code (stmt) == GIMPLE_COND)
2304 canonicalize_comparison (stmt);
2306 update_stmt_if_modified (stmt);
2307 opt_stats.num_stmts++;
2309 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2310 cprop_into_stmt (stmt);
2312 /* If the statement has been modified with constant replacements,
2313 fold its RHS before checking for redundant computations. */
2314 if (gimple_modified_p (stmt))
2316 tree rhs = NULL;
2318 /* Try to fold the statement making sure that STMT is kept
2319 up to date. */
2320 if (fold_stmt (&si))
2322 stmt = gsi_stmt (si);
2323 gimple_set_modified (stmt, true);
2325 if (dump_file && (dump_flags & TDF_DETAILS))
2327 fprintf (dump_file, " Folded to: ");
2328 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2332 /* We only need to consider cases that can yield a gimple operand. */
2333 if (gimple_assign_single_p (stmt))
2334 rhs = gimple_assign_rhs1 (stmt);
2335 else if (gimple_code (stmt) == GIMPLE_GOTO)
2336 rhs = gimple_goto_dest (stmt);
2337 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2338 /* This should never be an ADDR_EXPR. */
2339 rhs = gimple_switch_index (stmt);
2341 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2342 recompute_tree_invariant_for_addr_expr (rhs);
2344 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2345 even if fold_stmt updated the stmt already and thus cleared
2346 gimple_modified_p flag on it. */
2347 modified_p = true;
2350 /* Check for redundant computations. Do this optimization only
2351 for assignments that have no volatile ops and conditionals. */
2352 may_optimize_p = (!gimple_has_side_effects (stmt)
2353 && (is_gimple_assign (stmt)
2354 || (is_gimple_call (stmt)
2355 && gimple_call_lhs (stmt) != NULL_TREE)
2356 || gimple_code (stmt) == GIMPLE_COND
2357 || gimple_code (stmt) == GIMPLE_SWITCH));
2359 if (may_optimize_p)
2361 if (gimple_code (stmt) == GIMPLE_CALL)
2363 /* Resolve __builtin_constant_p. If it hasn't been
2364 folded to integer_one_node by now, it's fairly
2365 certain that the value simply isn't constant. */
2366 tree callee = gimple_call_fndecl (stmt);
2367 if (callee
2368 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2369 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2371 propagate_tree_value_into_stmt (&si, integer_zero_node);
2372 stmt = gsi_stmt (si);
2376 update_stmt_if_modified (stmt);
2377 eliminate_redundant_computations (&si);
2378 stmt = gsi_stmt (si);
2380 /* Perform simple redundant store elimination. */
2381 if (gimple_assign_single_p (stmt)
2382 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2384 tree lhs = gimple_assign_lhs (stmt);
2385 tree rhs = gimple_assign_rhs1 (stmt);
2386 tree cached_lhs;
2387 gimple new_stmt;
2388 if (TREE_CODE (rhs) == SSA_NAME)
2390 tree tem = SSA_NAME_VALUE (rhs);
2391 if (tem)
2392 rhs = tem;
2394 /* Build a new statement with the RHS and LHS exchanged. */
2395 if (TREE_CODE (rhs) == SSA_NAME)
2397 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2398 new_stmt = gimple_build_assign (rhs, lhs);
2399 SSA_NAME_DEF_STMT (rhs) = defstmt;
2401 else
2402 new_stmt = gimple_build_assign (rhs, lhs);
2403 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2404 cached_lhs = lookup_avail_expr (new_stmt, false);
2405 if (cached_lhs
2406 && rhs == cached_lhs)
2408 basic_block bb = gimple_bb (stmt);
2409 unlink_stmt_vdef (stmt);
2410 if (gsi_remove (&si, true))
2412 bitmap_set_bit (need_eh_cleanup, bb->index);
2413 if (dump_file && (dump_flags & TDF_DETAILS))
2414 fprintf (dump_file, " Flagged to clear EH edges.\n");
2416 release_defs (stmt);
2417 return;
2422 /* Record any additional equivalences created by this statement. */
2423 if (is_gimple_assign (stmt))
2424 record_equivalences_from_stmt (stmt, may_optimize_p);
2426 /* If STMT is a COND_EXPR and it was modified, then we may know
2427 where it goes. If that is the case, then mark the CFG as altered.
2429 This will cause us to later call remove_unreachable_blocks and
2430 cleanup_tree_cfg when it is safe to do so. It is not safe to
2431 clean things up here since removal of edges and such can trigger
2432 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2433 the manager.
2435 That's all fine and good, except that once SSA_NAMEs are released
2436 to the manager, we must not call create_ssa_name until all references
2437 to released SSA_NAMEs have been eliminated.
2439 All references to the deleted SSA_NAMEs can not be eliminated until
2440 we remove unreachable blocks.
2442 We can not remove unreachable blocks until after we have completed
2443 any queued jump threading.
2445 We can not complete any queued jump threads until we have taken
2446 appropriate variables out of SSA form. Taking variables out of
2447 SSA form can call create_ssa_name and thus we lose.
2449 Ultimately I suspect we're going to need to change the interface
2450 into the SSA_NAME manager. */
2451 if (gimple_modified_p (stmt) || modified_p)
2453 tree val = NULL;
2455 update_stmt_if_modified (stmt);
2457 if (gimple_code (stmt) == GIMPLE_COND)
2458 val = fold_binary_loc (gimple_location (stmt),
2459 gimple_cond_code (stmt), boolean_type_node,
2460 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2461 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2462 val = gimple_switch_index (stmt);
2464 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2465 cfg_altered = true;
2467 /* If we simplified a statement in such a way as to be shown that it
2468 cannot trap, update the eh information and the cfg to match. */
2469 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2471 bitmap_set_bit (need_eh_cleanup, bb->index);
2472 if (dump_file && (dump_flags & TDF_DETAILS))
2473 fprintf (dump_file, " Flagged to clear EH edges.\n");
2478 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2479 If found, return its LHS. Otherwise insert STMT in the table and
2480 return NULL_TREE.
2482 Also, when an expression is first inserted in the table, it is also
2483 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2484 we finish processing this block and its children. */
2486 static tree
2487 lookup_avail_expr (gimple stmt, bool insert)
2489 expr_hash_elt **slot;
2490 tree lhs;
2491 tree temp;
2492 struct expr_hash_elt element;
2494 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2495 if (gimple_code (stmt) == GIMPLE_PHI)
2496 lhs = gimple_phi_result (stmt);
2497 else
2498 lhs = gimple_get_lhs (stmt);
2500 initialize_hash_element (stmt, lhs, &element);
2502 if (dump_file && (dump_flags & TDF_DETAILS))
2504 fprintf (dump_file, "LKUP ");
2505 print_expr_hash_elt (dump_file, &element);
2508 /* Don't bother remembering constant assignments and copy operations.
2509 Constants and copy operations are handled by the constant/copy propagator
2510 in optimize_stmt. */
2511 if (element.expr.kind == EXPR_SINGLE
2512 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2513 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2514 return NULL_TREE;
2516 /* Finally try to find the expression in the main expression hash table. */
2517 slot = avail_exprs.find_slot_with_hash (&element, element.hash,
2518 (insert ? INSERT : NO_INSERT));
2519 if (slot == NULL)
2521 free_expr_hash_elt_contents (&element);
2522 return NULL_TREE;
2524 else if (*slot == NULL)
2526 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2527 *element2 = element;
2528 element2->stamp = element2;
2529 *slot = element2;
2531 if (dump_file && (dump_flags & TDF_DETAILS))
2533 fprintf (dump_file, "2>>> ");
2534 print_expr_hash_elt (dump_file, element2);
2537 avail_exprs_stack.safe_push (element2);
2538 return NULL_TREE;
2540 else
2541 free_expr_hash_elt_contents (&element);
2543 /* Extract the LHS of the assignment so that it can be used as the current
2544 definition of another variable. */
2545 lhs = ((struct expr_hash_elt *)*slot)->lhs;
2547 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2548 use the value from the const_and_copies table. */
2549 if (TREE_CODE (lhs) == SSA_NAME)
2551 temp = SSA_NAME_VALUE (lhs);
2552 if (temp)
2553 lhs = temp;
2556 if (dump_file && (dump_flags & TDF_DETAILS))
2558 fprintf (dump_file, "FIND: ");
2559 print_generic_expr (dump_file, lhs, 0);
2560 fprintf (dump_file, "\n");
2563 return lhs;
2566 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2567 for expressions using the code of the expression and the SSA numbers of
2568 its operands. */
2570 static hashval_t
2571 avail_expr_hash (const void *p)
2573 gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
2574 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2575 tree vuse;
2576 hashval_t val = 0;
2578 val = iterative_hash_hashable_expr (expr, val);
2580 /* If the hash table entry is not associated with a statement, then we
2581 can just hash the expression and not worry about virtual operands
2582 and such. */
2583 if (!stmt)
2584 return val;
2586 /* Add the SSA version numbers of the vuse operand. This is important
2587 because compound variables like arrays are not renamed in the
2588 operands. Rather, the rename is done on the virtual variable
2589 representing all the elements of the array. */
2590 if ((vuse = gimple_vuse (stmt)))
2591 val = iterative_hash_expr (vuse, val);
2593 return val;
2596 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2597 up degenerate PHIs created by or exposed by jump threading. */
2599 /* Given a statement STMT, which is either a PHI node or an assignment,
2600 remove it from the IL. */
2602 static void
2603 remove_stmt_or_phi (gimple stmt)
2605 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2607 if (gimple_code (stmt) == GIMPLE_PHI)
2608 remove_phi_node (&gsi, true);
2609 else
2611 gsi_remove (&gsi, true);
2612 release_defs (stmt);
2616 /* Given a statement STMT, which is either a PHI node or an assignment,
2617 return the "rhs" of the node, in the case of a non-degenerate
2618 phi, NULL is returned. */
2620 static tree
2621 get_rhs_or_phi_arg (gimple stmt)
2623 if (gimple_code (stmt) == GIMPLE_PHI)
2624 return degenerate_phi_result (stmt);
2625 else if (gimple_assign_single_p (stmt))
2626 return gimple_assign_rhs1 (stmt);
2627 else
2628 gcc_unreachable ();
2632 /* Given a statement STMT, which is either a PHI node or an assignment,
2633 return the "lhs" of the node. */
2635 static tree
2636 get_lhs_or_phi_result (gimple stmt)
2638 if (gimple_code (stmt) == GIMPLE_PHI)
2639 return gimple_phi_result (stmt);
2640 else if (is_gimple_assign (stmt))
2641 return gimple_assign_lhs (stmt);
2642 else
2643 gcc_unreachable ();
2646 /* Propagate RHS into all uses of LHS (when possible).
2648 RHS and LHS are derived from STMT, which is passed in solely so
2649 that we can remove it if propagation is successful.
2651 When propagating into a PHI node or into a statement which turns
2652 into a trivial copy or constant initialization, set the
2653 appropriate bit in INTERESTING_NAMEs so that we will visit those
2654 nodes as well in an effort to pick up secondary optimization
2655 opportunities. */
2657 static void
2658 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2660 /* First verify that propagation is valid and isn't going to move a
2661 loop variant variable outside its loop. */
2662 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2663 && (TREE_CODE (rhs) != SSA_NAME
2664 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2665 && may_propagate_copy (lhs, rhs)
2666 && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2668 use_operand_p use_p;
2669 imm_use_iterator iter;
2670 gimple use_stmt;
2671 bool all = true;
2673 /* Dump details. */
2674 if (dump_file && (dump_flags & TDF_DETAILS))
2676 fprintf (dump_file, " Replacing '");
2677 print_generic_expr (dump_file, lhs, dump_flags);
2678 fprintf (dump_file, "' with %s '",
2679 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2680 print_generic_expr (dump_file, rhs, dump_flags);
2681 fprintf (dump_file, "'\n");
2684 /* Walk over every use of LHS and try to replace the use with RHS.
2685 At this point the only reason why such a propagation would not
2686 be successful would be if the use occurs in an ASM_EXPR. */
2687 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2689 /* Leave debug stmts alone. If we succeed in propagating
2690 all non-debug uses, we'll drop the DEF, and propagation
2691 into debug stmts will occur then. */
2692 if (gimple_debug_bind_p (use_stmt))
2693 continue;
2695 /* It's not always safe to propagate into an ASM_EXPR. */
2696 if (gimple_code (use_stmt) == GIMPLE_ASM
2697 && ! may_propagate_copy_into_asm (lhs))
2699 all = false;
2700 continue;
2703 /* It's not ok to propagate into the definition stmt of RHS.
2704 <bb 9>:
2705 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2706 g_67.1_6 = prephitmp.12_36;
2707 goto <bb 9>;
2708 While this is strictly all dead code we do not want to
2709 deal with this here. */
2710 if (TREE_CODE (rhs) == SSA_NAME
2711 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2713 all = false;
2714 continue;
2717 /* Dump details. */
2718 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, " Original statement:");
2721 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2724 /* Propagate the RHS into this use of the LHS. */
2725 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2726 propagate_value (use_p, rhs);
2728 /* Special cases to avoid useless calls into the folding
2729 routines, operand scanning, etc.
2731 Propagation into a PHI may cause the PHI to become
2732 a degenerate, so mark the PHI as interesting. No other
2733 actions are necessary. */
2734 if (gimple_code (use_stmt) == GIMPLE_PHI)
2736 tree result;
2738 /* Dump details. */
2739 if (dump_file && (dump_flags & TDF_DETAILS))
2741 fprintf (dump_file, " Updated statement:");
2742 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2745 result = get_lhs_or_phi_result (use_stmt);
2746 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2747 continue;
2750 /* From this point onward we are propagating into a
2751 real statement. Folding may (or may not) be possible,
2752 we may expose new operands, expose dead EH edges,
2753 etc. */
2754 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2755 cannot fold a call that simplifies to a constant,
2756 because the GIMPLE_CALL must be replaced by a
2757 GIMPLE_ASSIGN, and there is no way to effect such a
2758 transformation in-place. We might want to consider
2759 using the more general fold_stmt here. */
2761 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2762 fold_stmt_inplace (&gsi);
2765 /* Sometimes propagation can expose new operands to the
2766 renamer. */
2767 update_stmt (use_stmt);
2769 /* Dump details. */
2770 if (dump_file && (dump_flags & TDF_DETAILS))
2772 fprintf (dump_file, " Updated statement:");
2773 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2776 /* If we replaced a variable index with a constant, then
2777 we would need to update the invariant flag for ADDR_EXPRs. */
2778 if (gimple_assign_single_p (use_stmt)
2779 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2780 recompute_tree_invariant_for_addr_expr
2781 (gimple_assign_rhs1 (use_stmt));
2783 /* If we cleaned up EH information from the statement,
2784 mark its containing block as needing EH cleanups. */
2785 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2787 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2788 if (dump_file && (dump_flags & TDF_DETAILS))
2789 fprintf (dump_file, " Flagged to clear EH edges.\n");
2792 /* Propagation may expose new trivial copy/constant propagation
2793 opportunities. */
2794 if (gimple_assign_single_p (use_stmt)
2795 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2796 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2797 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2799 tree result = get_lhs_or_phi_result (use_stmt);
2800 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2803 /* Propagation into these nodes may make certain edges in
2804 the CFG unexecutable. We want to identify them as PHI nodes
2805 at the destination of those unexecutable edges may become
2806 degenerates. */
2807 else if (gimple_code (use_stmt) == GIMPLE_COND
2808 || gimple_code (use_stmt) == GIMPLE_SWITCH
2809 || gimple_code (use_stmt) == GIMPLE_GOTO)
2811 tree val;
2813 if (gimple_code (use_stmt) == GIMPLE_COND)
2814 val = fold_binary_loc (gimple_location (use_stmt),
2815 gimple_cond_code (use_stmt),
2816 boolean_type_node,
2817 gimple_cond_lhs (use_stmt),
2818 gimple_cond_rhs (use_stmt));
2819 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2820 val = gimple_switch_index (use_stmt);
2821 else
2822 val = gimple_goto_dest (use_stmt);
2824 if (val && is_gimple_min_invariant (val))
2826 basic_block bb = gimple_bb (use_stmt);
2827 edge te = find_taken_edge (bb, val);
2828 edge_iterator ei;
2829 edge e;
2830 gimple_stmt_iterator gsi, psi;
2832 /* Remove all outgoing edges except TE. */
2833 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2835 if (e != te)
2837 /* Mark all the PHI nodes at the destination of
2838 the unexecutable edge as interesting. */
2839 for (psi = gsi_start_phis (e->dest);
2840 !gsi_end_p (psi);
2841 gsi_next (&psi))
2843 gimple phi = gsi_stmt (psi);
2845 tree result = gimple_phi_result (phi);
2846 int version = SSA_NAME_VERSION (result);
2848 bitmap_set_bit (interesting_names, version);
2851 te->probability += e->probability;
2853 te->count += e->count;
2854 remove_edge (e);
2855 cfg_altered = true;
2857 else
2858 ei_next (&ei);
2861 gsi = gsi_last_bb (gimple_bb (use_stmt));
2862 gsi_remove (&gsi, true);
2864 /* And fixup the flags on the single remaining edge. */
2865 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2866 te->flags &= ~EDGE_ABNORMAL;
2867 te->flags |= EDGE_FALLTHRU;
2868 if (te->probability > REG_BR_PROB_BASE)
2869 te->probability = REG_BR_PROB_BASE;
2874 /* Ensure there is nothing else to do. */
2875 gcc_assert (!all || has_zero_uses (lhs));
2877 /* If we were able to propagate away all uses of LHS, then
2878 we can remove STMT. */
2879 if (all)
2880 remove_stmt_or_phi (stmt);
2884 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2885 a statement that is a trivial copy or constant initialization.
2887 Attempt to eliminate T by propagating its RHS into all uses of
2888 its LHS. This may in turn set new bits in INTERESTING_NAMES
2889 for nodes we want to revisit later.
2891 All exit paths should clear INTERESTING_NAMES for the result
2892 of STMT. */
2894 static void
2895 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2897 tree lhs = get_lhs_or_phi_result (stmt);
2898 tree rhs;
2899 int version = SSA_NAME_VERSION (lhs);
2901 /* If the LHS of this statement or PHI has no uses, then we can
2902 just eliminate it. This can occur if, for example, the PHI
2903 was created by block duplication due to threading and its only
2904 use was in the conditional at the end of the block which was
2905 deleted. */
2906 if (has_zero_uses (lhs))
2908 bitmap_clear_bit (interesting_names, version);
2909 remove_stmt_or_phi (stmt);
2910 return;
2913 /* Get the RHS of the assignment or PHI node if the PHI is a
2914 degenerate. */
2915 rhs = get_rhs_or_phi_arg (stmt);
2916 if (!rhs)
2918 bitmap_clear_bit (interesting_names, version);
2919 return;
2922 if (!virtual_operand_p (lhs))
2923 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
2924 else
2926 gimple use_stmt;
2927 imm_use_iterator iter;
2928 use_operand_p use_p;
2929 /* For virtual operands we have to propagate into all uses as
2930 otherwise we will create overlapping life-ranges. */
2931 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2932 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2933 SET_USE (use_p, rhs);
2934 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2935 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
2936 remove_stmt_or_phi (stmt);
2939 /* Note that STMT may well have been deleted by now, so do
2940 not access it, instead use the saved version # to clear
2941 T's entry in the worklist. */
2942 bitmap_clear_bit (interesting_names, version);
2945 /* The first phase in degenerate PHI elimination.
2947 Eliminate the degenerate PHIs in BB, then recurse on the
2948 dominator children of BB. */
2950 static void
2951 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2953 gimple_stmt_iterator gsi;
2954 basic_block son;
2956 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2958 gimple phi = gsi_stmt (gsi);
2960 eliminate_const_or_copy (phi, interesting_names);
2963 /* Recurse into the dominator children of BB. */
2964 for (son = first_dom_son (CDI_DOMINATORS, bb);
2965 son;
2966 son = next_dom_son (CDI_DOMINATORS, son))
2967 eliminate_degenerate_phis_1 (son, interesting_names);
2971 /* A very simple pass to eliminate degenerate PHI nodes from the
2972 IL. This is meant to be fast enough to be able to be run several
2973 times in the optimization pipeline.
2975 Certain optimizations, particularly those which duplicate blocks
2976 or remove edges from the CFG can create or expose PHIs which are
2977 trivial copies or constant initializations.
2979 While we could pick up these optimizations in DOM or with the
2980 combination of copy-prop and CCP, those solutions are far too
2981 heavy-weight for our needs.
2983 This implementation has two phases so that we can efficiently
2984 eliminate the first order degenerate PHIs and second order
2985 degenerate PHIs.
2987 The first phase performs a dominator walk to identify and eliminate
2988 the vast majority of the degenerate PHIs. When a degenerate PHI
2989 is identified and eliminated any affected statements or PHIs
2990 are put on a worklist.
2992 The second phase eliminates degenerate PHIs and trivial copies
2993 or constant initializations using the worklist. This is how we
2994 pick up the secondary optimization opportunities with minimal
2995 cost. */
2997 static unsigned int
2998 eliminate_degenerate_phis (void)
3000 bitmap interesting_names;
3001 bitmap interesting_names1;
3003 /* Bitmap of blocks which need EH information updated. We can not
3004 update it on-the-fly as doing so invalidates the dominator tree. */
3005 need_eh_cleanup = BITMAP_ALLOC (NULL);
3007 /* INTERESTING_NAMES is effectively our worklist, indexed by
3008 SSA_NAME_VERSION.
3010 A set bit indicates that the statement or PHI node which
3011 defines the SSA_NAME should be (re)examined to determine if
3012 it has become a degenerate PHI or trivial const/copy propagation
3013 opportunity.
3015 Experiments have show we generally get better compilation
3016 time behavior with bitmaps rather than sbitmaps. */
3017 interesting_names = BITMAP_ALLOC (NULL);
3018 interesting_names1 = BITMAP_ALLOC (NULL);
3020 calculate_dominance_info (CDI_DOMINATORS);
3021 cfg_altered = false;
3023 /* First phase. Eliminate degenerate PHIs via a dominator
3024 walk of the CFG.
3026 Experiments have indicated that we generally get better
3027 compile-time behavior by visiting blocks in the first
3028 phase in dominator order. Presumably this is because walking
3029 in dominator order leaves fewer PHIs for later examination
3030 by the worklist phase. */
3031 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
3033 /* Second phase. Eliminate second order degenerate PHIs as well
3034 as trivial copies or constant initializations identified by
3035 the first phase or this phase. Basically we keep iterating
3036 until our set of INTERESTING_NAMEs is empty. */
3037 while (!bitmap_empty_p (interesting_names))
3039 unsigned int i;
3040 bitmap_iterator bi;
3042 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3043 changed during the loop. Copy it to another bitmap and
3044 use that. */
3045 bitmap_copy (interesting_names1, interesting_names);
3047 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3049 tree name = ssa_name (i);
3051 /* Ignore SSA_NAMEs that have been released because
3052 their defining statement was deleted (unreachable). */
3053 if (name)
3054 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3055 interesting_names);
3059 if (cfg_altered)
3061 free_dominance_info (CDI_DOMINATORS);
3062 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3063 if (current_loops)
3064 loops_state_set (LOOPS_NEED_FIXUP);
3067 /* Propagation of const and copies may make some EH edges dead. Purge
3068 such edges from the CFG as needed. */
3069 if (!bitmap_empty_p (need_eh_cleanup))
3071 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3072 BITMAP_FREE (need_eh_cleanup);
3075 BITMAP_FREE (interesting_names);
3076 BITMAP_FREE (interesting_names1);
3077 return 0;
3080 namespace {
3082 const pass_data pass_data_phi_only_cprop =
3084 GIMPLE_PASS, /* type */
3085 "phicprop", /* name */
3086 OPTGROUP_NONE, /* optinfo_flags */
3087 true, /* has_gate */
3088 true, /* has_execute */
3089 TV_TREE_PHI_CPROP, /* tv_id */
3090 ( PROP_cfg | PROP_ssa ), /* properties_required */
3091 0, /* properties_provided */
3092 0, /* properties_destroyed */
3093 0, /* todo_flags_start */
3094 ( TODO_cleanup_cfg | TODO_verify_ssa
3095 | TODO_verify_stmts
3096 | TODO_update_ssa ), /* todo_flags_finish */
3099 class pass_phi_only_cprop : public gimple_opt_pass
3101 public:
3102 pass_phi_only_cprop (gcc::context *ctxt)
3103 : gimple_opt_pass (pass_data_phi_only_cprop, ctxt)
3106 /* opt_pass methods: */
3107 opt_pass * clone () { return new pass_phi_only_cprop (m_ctxt); }
3108 bool gate () { return gate_dominator (); }
3109 unsigned int execute () { return eliminate_degenerate_phis (); }
3111 }; // class pass_phi_only_cprop
3113 } // anon namespace
3115 gimple_opt_pass *
3116 make_pass_phi_only_cprop (gcc::context *ctxt)
3118 return new pass_phi_only_cprop (ctxt);