gcc/upc/
[official-gcc.git] / gcc / tree-ssa-dom.c
blob393aa26d270b50edea7b51a1937520fb31dbb7f2
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.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 "tree-flow.h"
34 #include "domwalk.h"
35 #include "tree-pass.h"
36 #include "tree-ssa-propagate.h"
37 #include "langhooks.h"
38 #include "params.h"
40 /* This file implements optimizations on the dominator tree. */
42 /* Representation of a "naked" right-hand-side expression, to be used
43 in recording available expressions in the expression hash table. */
45 enum expr_kind
47 EXPR_SINGLE,
48 EXPR_UNARY,
49 EXPR_BINARY,
50 EXPR_TERNARY,
51 EXPR_CALL,
52 EXPR_PHI
55 struct hashable_expr
57 tree type;
58 enum expr_kind kind;
59 union {
60 struct { tree rhs; } single;
61 struct { enum tree_code op; tree opnd; } unary;
62 struct { enum tree_code op; tree opnd0, opnd1; } binary;
63 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
64 struct { gimple fn_from; bool pure; size_t nargs; tree *args; } call;
65 struct { size_t nargs; tree *args; } phi;
66 } ops;
69 /* Structure for recording known values of a conditional expression
70 at the exits from its block. */
72 typedef struct cond_equivalence_s
74 struct hashable_expr cond;
75 tree value;
76 } cond_equivalence;
78 DEF_VEC_O(cond_equivalence);
79 DEF_VEC_ALLOC_O(cond_equivalence,heap);
81 /* Structure for recording edge equivalences as well as any pending
82 edge redirections during the dominator optimizer.
84 Computing and storing the edge equivalences instead of creating
85 them on-demand can save significant amounts of time, particularly
86 for pathological cases involving switch statements.
88 These structures live for a single iteration of the dominator
89 optimizer in the edge's AUX field. At the end of an iteration we
90 free each of these structures and update the AUX field to point
91 to any requested redirection target (the code for updating the
92 CFG and SSA graph for edge redirection expects redirection edge
93 targets to be in the AUX field for each edge. */
95 struct edge_info
97 /* If this edge creates a simple equivalence, the LHS and RHS of
98 the equivalence will be stored here. */
99 tree lhs;
100 tree rhs;
102 /* Traversing an edge may also indicate one or more particular conditions
103 are true or false. */
104 VEC(cond_equivalence, heap) *cond_equivalences;
107 /* Hash table with expressions made available during the renaming process.
108 When an assignment of the form X_i = EXPR is found, the statement is
109 stored in this table. If the same expression EXPR is later found on the
110 RHS of another statement, it is replaced with X_i (thus performing
111 global redundancy elimination). Similarly as we pass through conditionals
112 we record the conditional itself as having either a true or false value
113 in this table. */
114 static htab_t avail_exprs;
116 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
117 expressions it enters into the hash table along with a marker entry
118 (null). When we finish processing the block, we pop off entries and
119 remove the expressions from the global hash table until we hit the
120 marker. */
121 typedef struct expr_hash_elt * expr_hash_elt_t;
122 DEF_VEC_P(expr_hash_elt_t);
123 DEF_VEC_ALLOC_P(expr_hash_elt_t,heap);
125 static VEC(expr_hash_elt_t,heap) *avail_exprs_stack;
127 /* Structure for entries in the expression hash table. */
129 struct expr_hash_elt
131 /* The value (lhs) of this expression. */
132 tree lhs;
134 /* The expression (rhs) we want to record. */
135 struct hashable_expr expr;
137 /* The stmt pointer if this element corresponds to a statement. */
138 gimple stmt;
140 /* The hash value for RHS. */
141 hashval_t hash;
143 /* A unique stamp, typically the address of the hash
144 element itself, used in removing entries from the table. */
145 struct expr_hash_elt *stamp;
148 /* Stack of dest,src pairs that need to be restored during finalization.
150 A NULL entry is used to mark the end of pairs which need to be
151 restored during finalization of this block. */
152 static VEC(tree,heap) *const_and_copies_stack;
154 /* Track whether or not we have changed the control flow graph. */
155 static bool cfg_altered;
157 /* Bitmap of blocks that have had EH statements cleaned. We should
158 remove their dead edges eventually. */
159 static bitmap need_eh_cleanup;
161 /* Statistics for dominator optimizations. */
162 struct opt_stats_d
164 long num_stmts;
165 long num_exprs_considered;
166 long num_re;
167 long num_const_prop;
168 long num_copy_prop;
171 static struct opt_stats_d opt_stats;
173 /* Local functions. */
174 static void optimize_stmt (basic_block, gimple_stmt_iterator);
175 static tree lookup_avail_expr (gimple, bool);
176 static hashval_t avail_expr_hash (const void *);
177 static hashval_t real_avail_expr_hash (const void *);
178 static int avail_expr_eq (const void *, const void *);
179 static void htab_statistics (FILE *, htab_t);
180 static void record_cond (cond_equivalence *);
181 static void record_const_or_copy (tree, tree);
182 static void record_equality (tree, tree);
183 static void record_equivalences_from_phis (basic_block);
184 static void record_equivalences_from_incoming_edge (basic_block);
185 static void eliminate_redundant_computations (gimple_stmt_iterator *);
186 static void record_equivalences_from_stmt (gimple, int);
187 static void dom_thread_across_edge (struct dom_walk_data *, edge);
188 static void dom_opt_leave_block (struct dom_walk_data *, basic_block);
189 static void dom_opt_enter_block (struct dom_walk_data *, basic_block);
190 static void remove_local_expressions_from_table (void);
191 static void restore_vars_to_original_value (void);
192 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
195 /* Given a statement STMT, initialize the hash table element pointed to
196 by ELEMENT. */
198 static void
199 initialize_hash_element (gimple stmt, tree lhs,
200 struct expr_hash_elt *element)
202 enum gimple_code code = gimple_code (stmt);
203 struct hashable_expr *expr = &element->expr;
205 if (code == GIMPLE_ASSIGN)
207 enum tree_code subcode = gimple_assign_rhs_code (stmt);
209 switch (get_gimple_rhs_class (subcode))
211 case GIMPLE_SINGLE_RHS:
212 expr->kind = EXPR_SINGLE;
213 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
214 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
215 break;
216 case GIMPLE_UNARY_RHS:
217 expr->kind = EXPR_UNARY;
218 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
219 expr->ops.unary.op = subcode;
220 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
221 break;
222 case GIMPLE_BINARY_RHS:
223 expr->kind = EXPR_BINARY;
224 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
225 expr->ops.binary.op = subcode;
226 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
227 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
228 break;
229 case GIMPLE_TERNARY_RHS:
230 expr->kind = EXPR_TERNARY;
231 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
232 expr->ops.ternary.op = subcode;
233 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
234 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
235 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
236 break;
237 default:
238 gcc_unreachable ();
241 else if (code == GIMPLE_COND)
243 expr->type = boolean_type_node;
244 expr->kind = EXPR_BINARY;
245 expr->ops.binary.op = gimple_cond_code (stmt);
246 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
247 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
249 else if (code == GIMPLE_CALL)
251 size_t nargs = gimple_call_num_args (stmt);
252 size_t i;
254 gcc_assert (gimple_call_lhs (stmt));
256 expr->type = TREE_TYPE (gimple_call_lhs (stmt));
257 expr->kind = EXPR_CALL;
258 expr->ops.call.fn_from = stmt;
260 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
261 expr->ops.call.pure = true;
262 else
263 expr->ops.call.pure = false;
265 expr->ops.call.nargs = nargs;
266 expr->ops.call.args = XCNEWVEC (tree, nargs);
267 for (i = 0; i < nargs; i++)
268 expr->ops.call.args[i] = gimple_call_arg (stmt, i);
270 else if (code == GIMPLE_SWITCH)
272 expr->type = TREE_TYPE (gimple_switch_index (stmt));
273 expr->kind = EXPR_SINGLE;
274 expr->ops.single.rhs = gimple_switch_index (stmt);
276 else if (code == GIMPLE_GOTO)
278 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
279 expr->kind = EXPR_SINGLE;
280 expr->ops.single.rhs = gimple_goto_dest (stmt);
282 else if (code == GIMPLE_PHI)
284 size_t nargs = gimple_phi_num_args (stmt);
285 size_t i;
287 expr->type = TREE_TYPE (gimple_phi_result (stmt));
288 expr->kind = EXPR_PHI;
289 expr->ops.phi.nargs = nargs;
290 expr->ops.phi.args = XCNEWVEC (tree, nargs);
292 for (i = 0; i < nargs; i++)
293 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
295 else
296 gcc_unreachable ();
298 element->lhs = lhs;
299 element->stmt = stmt;
300 element->hash = avail_expr_hash (element);
301 element->stamp = element;
304 /* Given a conditional expression COND as a tree, initialize
305 a hashable_expr expression EXPR. The conditional must be a
306 comparison or logical negation. A constant or a variable is
307 not permitted. */
309 static void
310 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
312 expr->type = boolean_type_node;
314 if (COMPARISON_CLASS_P (cond))
316 expr->kind = EXPR_BINARY;
317 expr->ops.binary.op = TREE_CODE (cond);
318 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
319 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
321 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
323 expr->kind = EXPR_UNARY;
324 expr->ops.unary.op = TRUTH_NOT_EXPR;
325 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
327 else
328 gcc_unreachable ();
331 /* Given a hashable_expr expression EXPR and an LHS,
332 initialize the hash table element pointed to by ELEMENT. */
334 static void
335 initialize_hash_element_from_expr (struct hashable_expr *expr,
336 tree lhs,
337 struct expr_hash_elt *element)
339 element->expr = *expr;
340 element->lhs = lhs;
341 element->stmt = NULL;
342 element->hash = avail_expr_hash (element);
343 element->stamp = element;
346 /* Compare two hashable_expr structures for equivalence.
347 They are considered equivalent when the the expressions
348 they denote must necessarily be equal. The logic is intended
349 to follow that of operand_equal_p in fold-const.c */
351 static bool
352 hashable_expr_equal_p (const struct hashable_expr *expr0,
353 const struct hashable_expr *expr1)
355 tree type0 = expr0->type;
356 tree type1 = expr1->type;
358 /* If either type is NULL, there is nothing to check. */
359 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
360 return false;
362 /* If both types don't have the same signedness, precision, and mode,
363 then we can't consider them equal. */
364 if (type0 != type1
365 && (TREE_CODE (type0) == ERROR_MARK
366 || TREE_CODE (type1) == ERROR_MARK
367 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
368 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
369 || TYPE_MODE (type0) != TYPE_MODE (type1)))
370 return false;
372 if (expr0->kind != expr1->kind)
373 return false;
375 switch (expr0->kind)
377 case EXPR_SINGLE:
378 return operand_equal_p (expr0->ops.single.rhs,
379 expr1->ops.single.rhs, 0);
381 case EXPR_UNARY:
382 if (expr0->ops.unary.op != expr1->ops.unary.op)
383 return false;
385 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
386 || expr0->ops.unary.op == NON_LVALUE_EXPR)
387 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
388 return false;
390 return operand_equal_p (expr0->ops.unary.opnd,
391 expr1->ops.unary.opnd, 0);
393 case EXPR_BINARY:
394 if (expr0->ops.binary.op != expr1->ops.binary.op)
395 return false;
397 if (operand_equal_p (expr0->ops.binary.opnd0,
398 expr1->ops.binary.opnd0, 0)
399 && operand_equal_p (expr0->ops.binary.opnd1,
400 expr1->ops.binary.opnd1, 0))
401 return true;
403 /* For commutative ops, allow the other order. */
404 return (commutative_tree_code (expr0->ops.binary.op)
405 && operand_equal_p (expr0->ops.binary.opnd0,
406 expr1->ops.binary.opnd1, 0)
407 && operand_equal_p (expr0->ops.binary.opnd1,
408 expr1->ops.binary.opnd0, 0));
410 case EXPR_TERNARY:
411 if (expr0->ops.ternary.op != expr1->ops.ternary.op
412 || !operand_equal_p (expr0->ops.ternary.opnd2,
413 expr1->ops.ternary.opnd2, 0))
414 return false;
416 if (operand_equal_p (expr0->ops.ternary.opnd0,
417 expr1->ops.ternary.opnd0, 0)
418 && operand_equal_p (expr0->ops.ternary.opnd1,
419 expr1->ops.ternary.opnd1, 0))
420 return true;
422 /* For commutative ops, allow the other order. */
423 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
424 && operand_equal_p (expr0->ops.ternary.opnd0,
425 expr1->ops.ternary.opnd1, 0)
426 && operand_equal_p (expr0->ops.ternary.opnd1,
427 expr1->ops.ternary.opnd0, 0));
429 case EXPR_CALL:
431 size_t i;
433 /* If the calls are to different functions, then they
434 clearly cannot be equal. */
435 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
436 expr1->ops.call.fn_from))
437 return false;
439 if (! expr0->ops.call.pure)
440 return false;
442 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
443 return false;
445 for (i = 0; i < expr0->ops.call.nargs; i++)
446 if (! operand_equal_p (expr0->ops.call.args[i],
447 expr1->ops.call.args[i], 0))
448 return false;
450 return true;
453 case EXPR_PHI:
455 size_t i;
457 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
458 return false;
460 for (i = 0; i < expr0->ops.phi.nargs; i++)
461 if (! operand_equal_p (expr0->ops.phi.args[i],
462 expr1->ops.phi.args[i], 0))
463 return false;
465 return true;
468 default:
469 gcc_unreachable ();
473 /* Compute a hash value for a hashable_expr value EXPR and a
474 previously accumulated hash value VAL. If two hashable_expr
475 values compare equal with hashable_expr_equal_p, they must
476 hash to the same value, given an identical value of VAL.
477 The logic is intended to follow iterative_hash_expr in tree.c. */
479 static hashval_t
480 iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val)
482 switch (expr->kind)
484 case EXPR_SINGLE:
485 val = iterative_hash_expr (expr->ops.single.rhs, val);
486 break;
488 case EXPR_UNARY:
489 val = iterative_hash_object (expr->ops.unary.op, val);
491 /* Make sure to include signedness in the hash computation.
492 Don't hash the type, that can lead to having nodes which
493 compare equal according to operand_equal_p, but which
494 have different hash codes. */
495 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
496 || expr->ops.unary.op == NON_LVALUE_EXPR)
497 val += TYPE_UNSIGNED (expr->type);
499 val = iterative_hash_expr (expr->ops.unary.opnd, val);
500 break;
502 case EXPR_BINARY:
503 val = iterative_hash_object (expr->ops.binary.op, val);
504 if (commutative_tree_code (expr->ops.binary.op))
505 val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0,
506 expr->ops.binary.opnd1, val);
507 else
509 val = iterative_hash_expr (expr->ops.binary.opnd0, val);
510 val = iterative_hash_expr (expr->ops.binary.opnd1, val);
512 break;
514 case EXPR_TERNARY:
515 val = iterative_hash_object (expr->ops.ternary.op, val);
516 if (commutative_ternary_tree_code (expr->ops.ternary.op))
517 val = iterative_hash_exprs_commutative (expr->ops.ternary.opnd0,
518 expr->ops.ternary.opnd1, val);
519 else
521 val = iterative_hash_expr (expr->ops.ternary.opnd0, val);
522 val = iterative_hash_expr (expr->ops.ternary.opnd1, val);
524 val = iterative_hash_expr (expr->ops.ternary.opnd2, val);
525 break;
527 case EXPR_CALL:
529 size_t i;
530 enum tree_code code = CALL_EXPR;
531 gimple fn_from;
533 val = iterative_hash_object (code, val);
534 fn_from = expr->ops.call.fn_from;
535 if (gimple_call_internal_p (fn_from))
536 val = iterative_hash_hashval_t
537 ((hashval_t) gimple_call_internal_fn (fn_from), val);
538 else
539 val = iterative_hash_expr (gimple_call_fn (fn_from), val);
540 for (i = 0; i < expr->ops.call.nargs; i++)
541 val = iterative_hash_expr (expr->ops.call.args[i], val);
543 break;
545 case EXPR_PHI:
547 size_t i;
549 for (i = 0; i < expr->ops.phi.nargs; i++)
550 val = iterative_hash_expr (expr->ops.phi.args[i], val);
552 break;
554 default:
555 gcc_unreachable ();
558 return val;
561 /* Print a diagnostic dump of an expression hash table entry. */
563 static void
564 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
566 if (element->stmt)
567 fprintf (stream, "STMT ");
568 else
569 fprintf (stream, "COND ");
571 if (element->lhs)
573 print_generic_expr (stream, element->lhs, 0);
574 fprintf (stream, " = ");
577 switch (element->expr.kind)
579 case EXPR_SINGLE:
580 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
581 break;
583 case EXPR_UNARY:
584 fprintf (stream, "%s ", tree_code_name[element->expr.ops.unary.op]);
585 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
586 break;
588 case EXPR_BINARY:
589 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
590 fprintf (stream, " %s ", tree_code_name[element->expr.ops.binary.op]);
591 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
592 break;
594 case EXPR_TERNARY:
595 fprintf (stream, " %s <", tree_code_name[element->expr.ops.ternary.op]);
596 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
597 fputs (", ", stream);
598 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
599 fputs (", ", stream);
600 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
601 fputs (">", stream);
602 break;
604 case EXPR_CALL:
606 size_t i;
607 size_t nargs = element->expr.ops.call.nargs;
608 gimple fn_from;
610 fn_from = element->expr.ops.call.fn_from;
611 if (gimple_call_internal_p (fn_from))
612 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
613 stream);
614 else
615 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
616 fprintf (stream, " (");
617 for (i = 0; i < nargs; i++)
619 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
620 if (i + 1 < nargs)
621 fprintf (stream, ", ");
623 fprintf (stream, ")");
625 break;
627 case EXPR_PHI:
629 size_t i;
630 size_t nargs = element->expr.ops.phi.nargs;
632 fprintf (stream, "PHI <");
633 for (i = 0; i < nargs; i++)
635 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
636 if (i + 1 < nargs)
637 fprintf (stream, ", ");
639 fprintf (stream, ">");
641 break;
643 fprintf (stream, "\n");
645 if (element->stmt)
647 fprintf (stream, " ");
648 print_gimple_stmt (stream, element->stmt, 0, 0);
652 /* Delete an expr_hash_elt and reclaim its storage. */
654 static void
655 free_expr_hash_elt (void *elt)
657 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
659 if (element->expr.kind == EXPR_CALL)
660 free (element->expr.ops.call.args);
662 if (element->expr.kind == EXPR_PHI)
663 free (element->expr.ops.phi.args);
665 free (element);
668 /* Allocate an EDGE_INFO for edge E and attach it to E.
669 Return the new EDGE_INFO structure. */
671 static struct edge_info *
672 allocate_edge_info (edge e)
674 struct edge_info *edge_info;
676 edge_info = XCNEW (struct edge_info);
678 e->aux = edge_info;
679 return edge_info;
682 /* Free all EDGE_INFO structures associated with edges in the CFG.
683 If a particular edge can be threaded, copy the redirection
684 target from the EDGE_INFO structure into the edge's AUX field
685 as required by code to update the CFG and SSA graph for
686 jump threading. */
688 static void
689 free_all_edge_infos (void)
691 basic_block bb;
692 edge_iterator ei;
693 edge e;
695 FOR_EACH_BB (bb)
697 FOR_EACH_EDGE (e, ei, bb->preds)
699 struct edge_info *edge_info = (struct edge_info *) e->aux;
701 if (edge_info)
703 if (edge_info->cond_equivalences)
704 VEC_free (cond_equivalence, heap, edge_info->cond_equivalences);
705 free (edge_info);
706 e->aux = NULL;
712 /* Jump threading, redundancy elimination and const/copy propagation.
714 This pass may expose new symbols that need to be renamed into SSA. For
715 every new symbol exposed, its corresponding bit will be set in
716 VARS_TO_RENAME. */
718 static unsigned int
719 tree_ssa_dominator_optimize (void)
721 struct dom_walk_data walk_data;
723 memset (&opt_stats, 0, sizeof (opt_stats));
725 /* Create our hash tables. */
726 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free_expr_hash_elt);
727 avail_exprs_stack = VEC_alloc (expr_hash_elt_t, heap, 20);
728 const_and_copies_stack = VEC_alloc (tree, heap, 20);
729 need_eh_cleanup = BITMAP_ALLOC (NULL);
731 /* Setup callbacks for the generic dominator tree walker. */
732 walk_data.dom_direction = CDI_DOMINATORS;
733 walk_data.initialize_block_local_data = NULL;
734 walk_data.before_dom_children = dom_opt_enter_block;
735 walk_data.after_dom_children = dom_opt_leave_block;
736 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
737 When we attach more stuff we'll need to fill this out with a real
738 structure. */
739 walk_data.global_data = NULL;
740 walk_data.block_local_data_size = 0;
742 /* Now initialize the dominator walker. */
743 init_walk_dominator_tree (&walk_data);
745 calculate_dominance_info (CDI_DOMINATORS);
746 cfg_altered = false;
748 /* We need to know loop structures in order to avoid destroying them
749 in jump threading. Note that we still can e.g. thread through loop
750 headers to an exit edge, or through loop header to the loop body, assuming
751 that we update the loop info. */
752 loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
754 /* Initialize the value-handle array. */
755 threadedge_initialize_values ();
757 /* We need accurate information regarding back edges in the CFG
758 for jump threading; this may include back edges that are not part of
759 a single loop. */
760 mark_dfs_back_edges ();
762 /* Recursively walk the dominator tree optimizing statements. */
763 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
766 gimple_stmt_iterator gsi;
767 basic_block bb;
768 FOR_EACH_BB (bb)
770 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
771 update_stmt_if_modified (gsi_stmt (gsi));
775 /* If we exposed any new variables, go ahead and put them into
776 SSA form now, before we handle jump threading. This simplifies
777 interactions between rewriting of _DECL nodes into SSA form
778 and rewriting SSA_NAME nodes into SSA form after block
779 duplication and CFG manipulation. */
780 update_ssa (TODO_update_ssa);
782 free_all_edge_infos ();
784 /* Thread jumps, creating duplicate blocks as needed. */
785 cfg_altered |= thread_through_all_blocks (first_pass_instance);
787 if (cfg_altered)
788 free_dominance_info (CDI_DOMINATORS);
790 /* Removal of statements may make some EH edges dead. Purge
791 such edges from the CFG as needed. */
792 if (!bitmap_empty_p (need_eh_cleanup))
794 unsigned i;
795 bitmap_iterator bi;
797 /* Jump threading may have created forwarder blocks from blocks
798 needing EH cleanup; the new successor of these blocks, which
799 has inherited from the original block, needs the cleanup. */
800 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
802 basic_block bb = BASIC_BLOCK (i);
803 if (bb
804 && single_succ_p (bb)
805 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
807 bitmap_clear_bit (need_eh_cleanup, i);
808 bitmap_set_bit (need_eh_cleanup, single_succ (bb)->index);
812 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
813 bitmap_zero (need_eh_cleanup);
816 statistics_counter_event (cfun, "Redundant expressions eliminated",
817 opt_stats.num_re);
818 statistics_counter_event (cfun, "Constants propagated",
819 opt_stats.num_const_prop);
820 statistics_counter_event (cfun, "Copies propagated",
821 opt_stats.num_copy_prop);
823 /* Debugging dumps. */
824 if (dump_file && (dump_flags & TDF_STATS))
825 dump_dominator_optimization_stats (dump_file);
827 loop_optimizer_finalize ();
829 /* Delete our main hashtable. */
830 htab_delete (avail_exprs);
832 /* And finalize the dominator walker. */
833 fini_walk_dominator_tree (&walk_data);
835 /* Free asserted bitmaps and stacks. */
836 BITMAP_FREE (need_eh_cleanup);
838 VEC_free (expr_hash_elt_t, heap, avail_exprs_stack);
839 VEC_free (tree, heap, const_and_copies_stack);
841 /* Free the value-handle array. */
842 threadedge_finalize_values ();
843 ssa_name_values = NULL;
845 return 0;
848 static bool
849 gate_dominator (void)
851 return flag_tree_dom != 0;
854 struct gimple_opt_pass pass_dominator =
857 GIMPLE_PASS,
858 "dom", /* name */
859 gate_dominator, /* gate */
860 tree_ssa_dominator_optimize, /* execute */
861 NULL, /* sub */
862 NULL, /* next */
863 0, /* static_pass_number */
864 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
865 PROP_cfg | PROP_ssa, /* properties_required */
866 0, /* properties_provided */
867 0, /* properties_destroyed */
868 0, /* todo_flags_start */
869 TODO_cleanup_cfg
870 | TODO_update_ssa
871 | TODO_verify_ssa
872 | TODO_verify_flow /* todo_flags_finish */
877 /* Given a conditional statement CONDSTMT, convert the
878 condition to a canonical form. */
880 static void
881 canonicalize_comparison (gimple condstmt)
883 tree op0;
884 tree op1;
885 enum tree_code code;
887 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
889 op0 = gimple_cond_lhs (condstmt);
890 op1 = gimple_cond_rhs (condstmt);
892 code = gimple_cond_code (condstmt);
894 /* If it would be profitable to swap the operands, then do so to
895 canonicalize the statement, enabling better optimization.
897 By placing canonicalization of such expressions here we
898 transparently keep statements in canonical form, even
899 when the statement is modified. */
900 if (tree_swap_operands_p (op0, op1, false))
902 /* For relationals we need to swap the operands
903 and change the code. */
904 if (code == LT_EXPR
905 || code == GT_EXPR
906 || code == LE_EXPR
907 || code == GE_EXPR)
909 code = swap_tree_comparison (code);
911 gimple_cond_set_code (condstmt, code);
912 gimple_cond_set_lhs (condstmt, op1);
913 gimple_cond_set_rhs (condstmt, op0);
915 update_stmt (condstmt);
920 /* Initialize local stacks for this optimizer and record equivalences
921 upon entry to BB. Equivalences can come from the edge traversed to
922 reach BB or they may come from PHI nodes at the start of BB. */
924 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
925 LIMIT entries left in LOCALs. */
927 static void
928 remove_local_expressions_from_table (void)
930 /* Remove all the expressions made available in this block. */
931 while (VEC_length (expr_hash_elt_t, avail_exprs_stack) > 0)
933 expr_hash_elt_t victim = VEC_pop (expr_hash_elt_t, avail_exprs_stack);
934 void **slot;
936 if (victim == NULL)
937 break;
939 /* This must precede the actual removal from the hash table,
940 as ELEMENT and the table entry may share a call argument
941 vector which will be freed during removal. */
942 if (dump_file && (dump_flags & TDF_DETAILS))
944 fprintf (dump_file, "<<<< ");
945 print_expr_hash_elt (dump_file, victim);
948 slot = htab_find_slot_with_hash (avail_exprs,
949 victim, victim->hash, NO_INSERT);
950 gcc_assert (slot && *slot == (void *) victim);
951 htab_clear_slot (avail_exprs, slot);
955 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
956 CONST_AND_COPIES to its original state, stopping when we hit a
957 NULL marker. */
959 static void
960 restore_vars_to_original_value (void)
962 while (VEC_length (tree, const_and_copies_stack) > 0)
964 tree prev_value, dest;
966 dest = VEC_pop (tree, const_and_copies_stack);
968 if (dest == NULL)
969 break;
971 if (dump_file && (dump_flags & TDF_DETAILS))
973 fprintf (dump_file, "<<<< COPY ");
974 print_generic_expr (dump_file, dest, 0);
975 fprintf (dump_file, " = ");
976 print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
977 fprintf (dump_file, "\n");
980 prev_value = VEC_pop (tree, const_and_copies_stack);
981 set_ssa_name_value (dest, prev_value);
985 /* A trivial wrapper so that we can present the generic jump
986 threading code with a simple API for simplifying statements. */
987 static tree
988 simplify_stmt_for_jump_threading (gimple stmt,
989 gimple within_stmt ATTRIBUTE_UNUSED)
991 return lookup_avail_expr (stmt, false);
994 /* Wrapper for common code to attempt to thread an edge. For example,
995 it handles lazily building the dummy condition and the bookkeeping
996 when jump threading is successful. */
998 static void
999 dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
1001 if (! walk_data->global_data)
1003 gimple dummy_cond =
1004 gimple_build_cond (NE_EXPR,
1005 integer_zero_node, integer_zero_node,
1006 NULL, NULL);
1007 walk_data->global_data = dummy_cond;
1010 thread_across_edge ((gimple) walk_data->global_data, e, false,
1011 &const_and_copies_stack,
1012 simplify_stmt_for_jump_threading);
1015 /* PHI nodes can create equivalences too.
1017 Ignoring any alternatives which are the same as the result, if
1018 all the alternatives are equal, then the PHI node creates an
1019 equivalence. */
1021 static void
1022 record_equivalences_from_phis (basic_block bb)
1024 gimple_stmt_iterator gsi;
1026 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1028 gimple phi = gsi_stmt (gsi);
1030 tree lhs = gimple_phi_result (phi);
1031 tree rhs = NULL;
1032 size_t i;
1034 for (i = 0; i < gimple_phi_num_args (phi); i++)
1036 tree t = gimple_phi_arg_def (phi, i);
1038 /* Ignore alternatives which are the same as our LHS. Since
1039 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1040 can simply compare pointers. */
1041 if (lhs == t)
1042 continue;
1044 /* If we have not processed an alternative yet, then set
1045 RHS to this alternative. */
1046 if (rhs == NULL)
1047 rhs = t;
1048 /* If we have processed an alternative (stored in RHS), then
1049 see if it is equal to this one. If it isn't, then stop
1050 the search. */
1051 else if (! operand_equal_for_phi_arg_p (rhs, t))
1052 break;
1055 /* If we had no interesting alternatives, then all the RHS alternatives
1056 must have been the same as LHS. */
1057 if (!rhs)
1058 rhs = lhs;
1060 /* If we managed to iterate through each PHI alternative without
1061 breaking out of the loop, then we have a PHI which may create
1062 a useful equivalence. We do not need to record unwind data for
1063 this, since this is a true assignment and not an equivalence
1064 inferred from a comparison. All uses of this ssa name are dominated
1065 by this assignment, so unwinding just costs time and space. */
1066 if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs))
1067 set_ssa_name_value (lhs, rhs);
1071 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1072 return that edge. Otherwise return NULL. */
1073 static edge
1074 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1076 edge retval = NULL;
1077 edge e;
1078 edge_iterator ei;
1080 FOR_EACH_EDGE (e, ei, bb->preds)
1082 /* A loop back edge can be identified by the destination of
1083 the edge dominating the source of the edge. */
1084 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1085 continue;
1087 /* If we have already seen a non-loop edge, then we must have
1088 multiple incoming non-loop edges and thus we return NULL. */
1089 if (retval)
1090 return NULL;
1092 /* This is the first non-loop incoming edge we have found. Record
1093 it. */
1094 retval = e;
1097 return retval;
1100 /* Record any equivalences created by the incoming edge to BB. If BB
1101 has more than one incoming edge, then no equivalence is created. */
1103 static void
1104 record_equivalences_from_incoming_edge (basic_block bb)
1106 edge e;
1107 basic_block parent;
1108 struct edge_info *edge_info;
1110 /* If our parent block ended with a control statement, then we may be
1111 able to record some equivalences based on which outgoing edge from
1112 the parent was followed. */
1113 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1115 e = single_incoming_edge_ignoring_loop_edges (bb);
1117 /* If we had a single incoming edge from our parent block, then enter
1118 any data associated with the edge into our tables. */
1119 if (e && e->src == parent)
1121 unsigned int i;
1123 edge_info = (struct edge_info *) e->aux;
1125 if (edge_info)
1127 tree lhs = edge_info->lhs;
1128 tree rhs = edge_info->rhs;
1129 cond_equivalence *eq;
1131 if (lhs)
1132 record_equality (lhs, rhs);
1134 for (i = 0; VEC_iterate (cond_equivalence,
1135 edge_info->cond_equivalences, i, eq); ++i)
1136 record_cond (eq);
1141 /* Dump SSA statistics on FILE. */
1143 void
1144 dump_dominator_optimization_stats (FILE *file)
1146 fprintf (file, "Total number of statements: %6ld\n\n",
1147 opt_stats.num_stmts);
1148 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1149 opt_stats.num_exprs_considered);
1151 fprintf (file, "\nHash table statistics:\n");
1153 fprintf (file, " avail_exprs: ");
1154 htab_statistics (file, avail_exprs);
1158 /* Dump SSA statistics on stderr. */
1160 DEBUG_FUNCTION void
1161 debug_dominator_optimization_stats (void)
1163 dump_dominator_optimization_stats (stderr);
1167 /* Dump statistics for the hash table HTAB. */
1169 static void
1170 htab_statistics (FILE *file, htab_t htab)
1172 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1173 (long) htab_size (htab),
1174 (long) htab_elements (htab),
1175 htab_collisions (htab));
1179 /* Enter condition equivalence into the expression hash table.
1180 This indicates that a conditional expression has a known
1181 boolean value. */
1183 static void
1184 record_cond (cond_equivalence *p)
1186 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1187 void **slot;
1189 initialize_hash_element_from_expr (&p->cond, p->value, element);
1191 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1192 element->hash, INSERT);
1193 if (*slot == NULL)
1195 *slot = (void *) element;
1197 if (dump_file && (dump_flags & TDF_DETAILS))
1199 fprintf (dump_file, "1>>> ");
1200 print_expr_hash_elt (dump_file, element);
1203 VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element);
1205 else
1206 free (element);
1209 /* Build a cond_equivalence record indicating that the comparison
1210 CODE holds between operands OP0 and OP1 and push it to **P. */
1212 static void
1213 build_and_record_new_cond (enum tree_code code,
1214 tree op0, tree op1,
1215 VEC(cond_equivalence, heap) **p)
1217 cond_equivalence c;
1218 struct hashable_expr *cond = &c.cond;
1220 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1222 cond->type = boolean_type_node;
1223 cond->kind = EXPR_BINARY;
1224 cond->ops.binary.op = code;
1225 cond->ops.binary.opnd0 = op0;
1226 cond->ops.binary.opnd1 = op1;
1228 c.value = boolean_true_node;
1229 VEC_safe_push (cond_equivalence, heap, *p, &c);
1232 /* Record that COND is true and INVERTED is false into the edge information
1233 structure. Also record that any conditions dominated by COND are true
1234 as well.
1236 For example, if a < b is true, then a <= b must also be true. */
1238 static void
1239 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1241 tree op0, op1;
1242 cond_equivalence c;
1244 if (!COMPARISON_CLASS_P (cond))
1245 return;
1247 op0 = TREE_OPERAND (cond, 0);
1248 op1 = TREE_OPERAND (cond, 1);
1250 switch (TREE_CODE (cond))
1252 case LT_EXPR:
1253 case GT_EXPR:
1254 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1256 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1257 &edge_info->cond_equivalences);
1258 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1259 &edge_info->cond_equivalences);
1262 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1263 ? LE_EXPR : GE_EXPR),
1264 op0, op1, &edge_info->cond_equivalences);
1265 build_and_record_new_cond (NE_EXPR, op0, op1,
1266 &edge_info->cond_equivalences);
1267 break;
1269 case GE_EXPR:
1270 case LE_EXPR:
1271 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1273 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1274 &edge_info->cond_equivalences);
1276 break;
1278 case EQ_EXPR:
1279 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1281 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1282 &edge_info->cond_equivalences);
1284 build_and_record_new_cond (LE_EXPR, op0, op1,
1285 &edge_info->cond_equivalences);
1286 build_and_record_new_cond (GE_EXPR, op0, op1,
1287 &edge_info->cond_equivalences);
1288 break;
1290 case UNORDERED_EXPR:
1291 build_and_record_new_cond (NE_EXPR, op0, op1,
1292 &edge_info->cond_equivalences);
1293 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1294 &edge_info->cond_equivalences);
1295 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1296 &edge_info->cond_equivalences);
1297 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1298 &edge_info->cond_equivalences);
1299 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1300 &edge_info->cond_equivalences);
1301 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1302 &edge_info->cond_equivalences);
1303 break;
1305 case UNLT_EXPR:
1306 case UNGT_EXPR:
1307 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1308 ? UNLE_EXPR : UNGE_EXPR),
1309 op0, op1, &edge_info->cond_equivalences);
1310 build_and_record_new_cond (NE_EXPR, op0, op1,
1311 &edge_info->cond_equivalences);
1312 break;
1314 case UNEQ_EXPR:
1315 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1316 &edge_info->cond_equivalences);
1317 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1318 &edge_info->cond_equivalences);
1319 break;
1321 case LTGT_EXPR:
1322 build_and_record_new_cond (NE_EXPR, op0, op1,
1323 &edge_info->cond_equivalences);
1324 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1325 &edge_info->cond_equivalences);
1326 break;
1328 default:
1329 break;
1332 /* Now store the original true and false conditions into the first
1333 two slots. */
1334 initialize_expr_from_cond (cond, &c.cond);
1335 c.value = boolean_true_node;
1336 VEC_safe_push (cond_equivalence, heap, edge_info->cond_equivalences, &c);
1338 /* It is possible for INVERTED to be the negation of a comparison,
1339 and not a valid RHS or GIMPLE_COND condition. This happens because
1340 invert_truthvalue may return such an expression when asked to invert
1341 a floating-point comparison. These comparisons are not assumed to
1342 obey the trichotomy law. */
1343 initialize_expr_from_cond (inverted, &c.cond);
1344 c.value = boolean_false_node;
1345 VEC_safe_push (cond_equivalence, heap, edge_info->cond_equivalences, &c);
1348 /* A helper function for record_const_or_copy and record_equality.
1349 Do the work of recording the value and undo info. */
1351 static void
1352 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1354 set_ssa_name_value (x, y);
1356 if (dump_file && (dump_flags & TDF_DETAILS))
1358 fprintf (dump_file, "0>>> COPY ");
1359 print_generic_expr (dump_file, x, 0);
1360 fprintf (dump_file, " = ");
1361 print_generic_expr (dump_file, y, 0);
1362 fprintf (dump_file, "\n");
1365 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1366 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1367 VEC_quick_push (tree, const_and_copies_stack, x);
1370 /* Return the loop depth of the basic block of the defining statement of X.
1371 This number should not be treated as absolutely correct because the loop
1372 information may not be completely up-to-date when dom runs. However, it
1373 will be relatively correct, and as more passes are taught to keep loop info
1374 up to date, the result will become more and more accurate. */
1377 loop_depth_of_name (tree x)
1379 gimple defstmt;
1380 basic_block defbb;
1382 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1383 if (TREE_CODE (x) != SSA_NAME)
1384 return 0;
1386 /* Otherwise return the loop depth of the defining statement's bb.
1387 Note that there may not actually be a bb for this statement, if the
1388 ssa_name is live on entry. */
1389 defstmt = SSA_NAME_DEF_STMT (x);
1390 defbb = gimple_bb (defstmt);
1391 if (!defbb)
1392 return 0;
1394 return defbb->loop_depth;
1397 /* Record that X is equal to Y in const_and_copies. Record undo
1398 information in the block-local vector. */
1400 static void
1401 record_const_or_copy (tree x, tree y)
1403 tree prev_x = SSA_NAME_VALUE (x);
1405 gcc_assert (TREE_CODE (x) == SSA_NAME);
1407 if (TREE_CODE (y) == SSA_NAME)
1409 tree tmp = SSA_NAME_VALUE (y);
1410 if (tmp)
1411 y = tmp;
1414 record_const_or_copy_1 (x, y, prev_x);
1417 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1418 This constrains the cases in which we may treat this as assignment. */
1420 static void
1421 record_equality (tree x, tree y)
1423 tree prev_x = NULL, prev_y = NULL;
1425 if (TREE_CODE (x) == SSA_NAME)
1426 prev_x = SSA_NAME_VALUE (x);
1427 if (TREE_CODE (y) == SSA_NAME)
1428 prev_y = SSA_NAME_VALUE (y);
1430 /* If one of the previous values is invariant, or invariant in more loops
1431 (by depth), then use that.
1432 Otherwise it doesn't matter which value we choose, just so
1433 long as we canonicalize on one value. */
1434 if (is_gimple_min_invariant (y))
1436 else if (is_gimple_min_invariant (x)
1437 || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1438 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1439 else if (prev_x && is_gimple_min_invariant (prev_x))
1440 x = y, y = prev_x, prev_x = prev_y;
1441 else if (prev_y)
1442 y = prev_y;
1444 /* After the swapping, we must have one SSA_NAME. */
1445 if (TREE_CODE (x) != SSA_NAME)
1446 return;
1448 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1449 variable compared against zero. If we're honoring signed zeros,
1450 then we cannot record this value unless we know that the value is
1451 nonzero. */
1452 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1453 && (TREE_CODE (y) != REAL_CST
1454 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1455 return;
1457 record_const_or_copy_1 (x, y, prev_x);
1460 /* Returns true when STMT is a simple iv increment. It detects the
1461 following situation:
1463 i_1 = phi (..., i_2)
1464 i_2 = i_1 +/- ... */
1466 bool
1467 simple_iv_increment_p (gimple stmt)
1469 enum tree_code code;
1470 tree lhs, preinc;
1471 gimple phi;
1472 size_t i;
1474 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1475 return false;
1477 lhs = gimple_assign_lhs (stmt);
1478 if (TREE_CODE (lhs) != SSA_NAME)
1479 return false;
1481 code = gimple_assign_rhs_code (stmt);
1482 if (code != PLUS_EXPR
1483 && code != MINUS_EXPR
1484 && code != POINTER_PLUS_EXPR)
1485 return false;
1487 preinc = gimple_assign_rhs1 (stmt);
1488 if (TREE_CODE (preinc) != SSA_NAME)
1489 return false;
1491 phi = SSA_NAME_DEF_STMT (preinc);
1492 if (gimple_code (phi) != GIMPLE_PHI)
1493 return false;
1495 for (i = 0; i < gimple_phi_num_args (phi); i++)
1496 if (gimple_phi_arg_def (phi, i) == lhs)
1497 return true;
1499 return false;
1502 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1503 known value for that SSA_NAME (or NULL if no value is known).
1505 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1506 successors of BB. */
1508 static void
1509 cprop_into_successor_phis (basic_block bb)
1511 edge e;
1512 edge_iterator ei;
1514 FOR_EACH_EDGE (e, ei, bb->succs)
1516 int indx;
1517 gimple_stmt_iterator gsi;
1519 /* If this is an abnormal edge, then we do not want to copy propagate
1520 into the PHI alternative associated with this edge. */
1521 if (e->flags & EDGE_ABNORMAL)
1522 continue;
1524 gsi = gsi_start_phis (e->dest);
1525 if (gsi_end_p (gsi))
1526 continue;
1528 indx = e->dest_idx;
1529 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1531 tree new_val;
1532 use_operand_p orig_p;
1533 tree orig_val;
1534 gimple phi = gsi_stmt (gsi);
1536 /* The alternative may be associated with a constant, so verify
1537 it is an SSA_NAME before doing anything with it. */
1538 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1539 orig_val = get_use_from_ptr (orig_p);
1540 if (TREE_CODE (orig_val) != SSA_NAME)
1541 continue;
1543 /* If we have *ORIG_P in our constant/copy table, then replace
1544 ORIG_P with its value in our constant/copy table. */
1545 new_val = SSA_NAME_VALUE (orig_val);
1546 if (new_val
1547 && new_val != orig_val
1548 && (TREE_CODE (new_val) == SSA_NAME
1549 || is_gimple_min_invariant (new_val))
1550 && may_propagate_copy (orig_val, new_val))
1551 propagate_value (orig_p, new_val);
1556 /* We have finished optimizing BB, record any information implied by
1557 taking a specific outgoing edge from BB. */
1559 static void
1560 record_edge_info (basic_block bb)
1562 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1563 struct edge_info *edge_info;
1565 if (! gsi_end_p (gsi))
1567 gimple stmt = gsi_stmt (gsi);
1568 location_t loc = gimple_location (stmt);
1570 if (gimple_code (stmt) == GIMPLE_SWITCH)
1572 tree index = gimple_switch_index (stmt);
1574 if (TREE_CODE (index) == SSA_NAME)
1576 int i;
1577 int n_labels = gimple_switch_num_labels (stmt);
1578 tree *info = XCNEWVEC (tree, last_basic_block);
1579 edge e;
1580 edge_iterator ei;
1582 for (i = 0; i < n_labels; i++)
1584 tree label = gimple_switch_label (stmt, i);
1585 basic_block target_bb = label_to_block (CASE_LABEL (label));
1586 if (CASE_HIGH (label)
1587 || !CASE_LOW (label)
1588 || info[target_bb->index])
1589 info[target_bb->index] = error_mark_node;
1590 else
1591 info[target_bb->index] = label;
1594 FOR_EACH_EDGE (e, ei, bb->succs)
1596 basic_block target_bb = e->dest;
1597 tree label = info[target_bb->index];
1599 if (label != NULL && label != error_mark_node)
1601 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1602 CASE_LOW (label));
1603 edge_info = allocate_edge_info (e);
1604 edge_info->lhs = index;
1605 edge_info->rhs = x;
1608 free (info);
1612 /* A COND_EXPR may create equivalences too. */
1613 if (gimple_code (stmt) == GIMPLE_COND)
1615 edge true_edge;
1616 edge false_edge;
1618 tree op0 = gimple_cond_lhs (stmt);
1619 tree op1 = gimple_cond_rhs (stmt);
1620 enum tree_code code = gimple_cond_code (stmt);
1622 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1624 /* Special case comparing booleans against a constant as we
1625 know the value of OP0 on both arms of the branch. i.e., we
1626 can record an equivalence for OP0 rather than COND. */
1627 if ((code == EQ_EXPR || code == NE_EXPR)
1628 && TREE_CODE (op0) == SSA_NAME
1629 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1630 && is_gimple_min_invariant (op1))
1632 if (code == EQ_EXPR)
1634 edge_info = allocate_edge_info (true_edge);
1635 edge_info->lhs = op0;
1636 edge_info->rhs = (integer_zerop (op1)
1637 ? boolean_false_node
1638 : boolean_true_node);
1640 edge_info = allocate_edge_info (false_edge);
1641 edge_info->lhs = op0;
1642 edge_info->rhs = (integer_zerop (op1)
1643 ? boolean_true_node
1644 : boolean_false_node);
1646 else
1648 edge_info = allocate_edge_info (true_edge);
1649 edge_info->lhs = op0;
1650 edge_info->rhs = (integer_zerop (op1)
1651 ? boolean_true_node
1652 : boolean_false_node);
1654 edge_info = allocate_edge_info (false_edge);
1655 edge_info->lhs = op0;
1656 edge_info->rhs = (integer_zerop (op1)
1657 ? boolean_false_node
1658 : boolean_true_node);
1661 else if (is_gimple_min_invariant (op0)
1662 && (TREE_CODE (op1) == SSA_NAME
1663 || is_gimple_min_invariant (op1)))
1665 tree cond = build2 (code, boolean_type_node, op0, op1);
1666 tree inverted = invert_truthvalue_loc (loc, cond);
1667 bool can_infer_simple_equiv
1668 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
1669 && real_zerop (op0));
1670 struct edge_info *edge_info;
1672 edge_info = allocate_edge_info (true_edge);
1673 record_conditions (edge_info, cond, inverted);
1675 if (can_infer_simple_equiv && code == EQ_EXPR)
1677 edge_info->lhs = op1;
1678 edge_info->rhs = op0;
1681 edge_info = allocate_edge_info (false_edge);
1682 record_conditions (edge_info, inverted, cond);
1684 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1686 edge_info->lhs = op1;
1687 edge_info->rhs = op0;
1691 else if (TREE_CODE (op0) == SSA_NAME
1692 && (TREE_CODE (op1) == SSA_NAME
1693 || is_gimple_min_invariant (op1)))
1695 tree cond = build2 (code, boolean_type_node, op0, op1);
1696 tree inverted = invert_truthvalue_loc (loc, cond);
1697 bool can_infer_simple_equiv
1698 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op1)))
1699 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1700 struct edge_info *edge_info;
1702 edge_info = allocate_edge_info (true_edge);
1703 record_conditions (edge_info, cond, inverted);
1705 if (can_infer_simple_equiv && code == EQ_EXPR)
1707 edge_info->lhs = op0;
1708 edge_info->rhs = op1;
1711 edge_info = allocate_edge_info (false_edge);
1712 record_conditions (edge_info, inverted, cond);
1714 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1716 edge_info->lhs = op0;
1717 edge_info->rhs = op1;
1722 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1726 static void
1727 dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1728 basic_block bb)
1730 gimple_stmt_iterator gsi;
1732 if (dump_file && (dump_flags & TDF_DETAILS))
1733 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1735 /* Push a marker on the stacks of local information so that we know how
1736 far to unwind when we finalize this block. */
1737 VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
1738 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1740 record_equivalences_from_incoming_edge (bb);
1742 /* PHI nodes can create equivalences too. */
1743 record_equivalences_from_phis (bb);
1745 /* Create equivalences from redundant PHIs. PHIs are only truly
1746 redundant when they exist in the same block, so push another
1747 marker and unwind right afterwards. */
1748 VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
1749 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1750 eliminate_redundant_computations (&gsi);
1751 remove_local_expressions_from_table ();
1753 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1754 optimize_stmt (bb, gsi);
1756 /* Now prepare to process dominated blocks. */
1757 record_edge_info (bb);
1758 cprop_into_successor_phis (bb);
1761 /* We have finished processing the dominator children of BB, perform
1762 any finalization actions in preparation for leaving this node in
1763 the dominator tree. */
1765 static void
1766 dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb)
1768 gimple last;
1770 /* If we have an outgoing edge to a block with multiple incoming and
1771 outgoing edges, then we may be able to thread the edge, i.e., we
1772 may be able to statically determine which of the outgoing edges
1773 will be traversed when the incoming edge from BB is traversed. */
1774 if (single_succ_p (bb)
1775 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1776 && potentially_threadable_block (single_succ (bb)))
1778 /* Push a marker on the stack, which thread_across_edge expects
1779 and will remove. */
1780 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1781 dom_thread_across_edge (walk_data, single_succ_edge (bb));
1783 else if ((last = last_stmt (bb))
1784 && gimple_code (last) == GIMPLE_COND
1785 && EDGE_COUNT (bb->succs) == 2
1786 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1787 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1789 edge true_edge, false_edge;
1791 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1793 /* Only try to thread the edge if it reaches a target block with
1794 more than one predecessor and more than one successor. */
1795 if (potentially_threadable_block (true_edge->dest))
1797 struct edge_info *edge_info;
1798 unsigned int i;
1800 /* Push a marker onto the available expression stack so that we
1801 unwind any expressions related to the TRUE arm before processing
1802 the false arm below. */
1803 VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
1804 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1806 edge_info = (struct edge_info *) true_edge->aux;
1808 /* If we have info associated with this edge, record it into
1809 our equivalence tables. */
1810 if (edge_info)
1812 cond_equivalence *eq;
1813 tree lhs = edge_info->lhs;
1814 tree rhs = edge_info->rhs;
1816 /* If we have a simple NAME = VALUE equivalence, record it. */
1817 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1818 record_const_or_copy (lhs, rhs);
1820 /* If we have 0 = COND or 1 = COND equivalences, record them
1821 into our expression hash tables. */
1822 for (i = 0; VEC_iterate (cond_equivalence,
1823 edge_info->cond_equivalences, i, eq); ++i)
1824 record_cond (eq);
1827 dom_thread_across_edge (walk_data, true_edge);
1829 /* And restore the various tables to their state before
1830 we threaded this edge. */
1831 remove_local_expressions_from_table ();
1834 /* Similarly for the ELSE arm. */
1835 if (potentially_threadable_block (false_edge->dest))
1837 struct edge_info *edge_info;
1838 unsigned int i;
1840 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1841 edge_info = (struct edge_info *) false_edge->aux;
1843 /* If we have info associated with this edge, record it into
1844 our equivalence tables. */
1845 if (edge_info)
1847 cond_equivalence *eq;
1848 tree lhs = edge_info->lhs;
1849 tree rhs = edge_info->rhs;
1851 /* If we have a simple NAME = VALUE equivalence, record it. */
1852 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1853 record_const_or_copy (lhs, rhs);
1855 /* If we have 0 = COND or 1 = COND equivalences, record them
1856 into our expression hash tables. */
1857 for (i = 0; VEC_iterate (cond_equivalence,
1858 edge_info->cond_equivalences, i, eq); ++i)
1859 record_cond (eq);
1862 /* Now thread the edge. */
1863 dom_thread_across_edge (walk_data, false_edge);
1865 /* No need to remove local expressions from our tables
1866 or restore vars to their original value as that will
1867 be done immediately below. */
1871 remove_local_expressions_from_table ();
1872 restore_vars_to_original_value ();
1875 /* Search for redundant computations in STMT. If any are found, then
1876 replace them with the variable holding the result of the computation.
1878 If safe, record this expression into the available expression hash
1879 table. */
1881 static void
1882 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
1884 tree expr_type;
1885 tree cached_lhs;
1886 tree def;
1887 bool insert = true;
1888 bool assigns_var_p = false;
1890 gimple stmt = gsi_stmt (*gsi);
1892 if (gimple_code (stmt) == GIMPLE_PHI)
1893 def = gimple_phi_result (stmt);
1894 else
1895 def = gimple_get_lhs (stmt);
1897 /* Certain expressions on the RHS can be optimized away, but can not
1898 themselves be entered into the hash tables. */
1899 if (! def
1900 || TREE_CODE (def) != SSA_NAME
1901 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1902 || gimple_vdef (stmt)
1903 /* Do not record equivalences for increments of ivs. This would create
1904 overlapping live ranges for a very questionable gain. */
1905 || simple_iv_increment_p (stmt))
1906 insert = false;
1908 /* Check if the expression has been computed before. */
1909 cached_lhs = lookup_avail_expr (stmt, insert);
1911 opt_stats.num_exprs_considered++;
1913 /* Get the type of the expression we are trying to optimize. */
1914 if (is_gimple_assign (stmt))
1916 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1917 assigns_var_p = true;
1919 else if (gimple_code (stmt) == GIMPLE_COND)
1920 expr_type = boolean_type_node;
1921 else if (is_gimple_call (stmt))
1923 gcc_assert (gimple_call_lhs (stmt));
1924 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1925 assigns_var_p = true;
1927 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1928 expr_type = TREE_TYPE (gimple_switch_index (stmt));
1929 else if (gimple_code (stmt) == GIMPLE_PHI)
1930 /* We can't propagate into a phi, so the logic below doesn't apply.
1931 Instead record an equivalence between the cached LHS and the
1932 PHI result of this statement, provided they are in the same block.
1933 This should be sufficient to kill the redundant phi. */
1935 if (def && cached_lhs)
1936 record_const_or_copy (def, cached_lhs);
1937 return;
1939 else
1940 gcc_unreachable ();
1942 if (!cached_lhs)
1943 return;
1945 /* It is safe to ignore types here since we have already done
1946 type checking in the hashing and equality routines. In fact
1947 type checking here merely gets in the way of constant
1948 propagation. Also, make sure that it is safe to propagate
1949 CACHED_LHS into the expression in STMT. */
1950 if ((TREE_CODE (cached_lhs) != SSA_NAME
1951 && (assigns_var_p
1952 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1953 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1955 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1956 || is_gimple_min_invariant (cached_lhs));
1958 if (dump_file && (dump_flags & TDF_DETAILS))
1960 fprintf (dump_file, " Replaced redundant expr '");
1961 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1962 fprintf (dump_file, "' with '");
1963 print_generic_expr (dump_file, cached_lhs, dump_flags);
1964 fprintf (dump_file, "'\n");
1967 opt_stats.num_re++;
1969 if (assigns_var_p
1970 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1971 cached_lhs = fold_convert (expr_type, cached_lhs);
1973 propagate_tree_value_into_stmt (gsi, cached_lhs);
1975 /* Since it is always necessary to mark the result as modified,
1976 perhaps we should move this into propagate_tree_value_into_stmt
1977 itself. */
1978 gimple_set_modified (gsi_stmt (*gsi), true);
1982 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1983 the available expressions table or the const_and_copies table.
1984 Detect and record those equivalences. */
1985 /* We handle only very simple copy equivalences here. The heavy
1986 lifing is done by eliminate_redundant_computations. */
1988 static void
1989 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
1991 tree lhs;
1992 enum tree_code lhs_code;
1994 gcc_assert (is_gimple_assign (stmt));
1996 lhs = gimple_assign_lhs (stmt);
1997 lhs_code = TREE_CODE (lhs);
1999 if (lhs_code == SSA_NAME
2000 && gimple_assign_single_p (stmt))
2002 tree rhs = gimple_assign_rhs1 (stmt);
2004 /* If the RHS of the assignment is a constant or another variable that
2005 may be propagated, register it in the CONST_AND_COPIES table. We
2006 do not need to record unwind data for this, since this is a true
2007 assignment and not an equivalence inferred from a comparison. All
2008 uses of this ssa name are dominated by this assignment, so unwinding
2009 just costs time and space. */
2010 if (may_optimize_p
2011 && (TREE_CODE (rhs) == SSA_NAME
2012 || is_gimple_min_invariant (rhs)))
2014 if (dump_file && (dump_flags & TDF_DETAILS))
2016 fprintf (dump_file, "==== ASGN ");
2017 print_generic_expr (dump_file, lhs, 0);
2018 fprintf (dump_file, " = ");
2019 print_generic_expr (dump_file, rhs, 0);
2020 fprintf (dump_file, "\n");
2023 set_ssa_name_value (lhs, rhs);
2027 /* A memory store, even an aliased store, creates a useful
2028 equivalence. By exchanging the LHS and RHS, creating suitable
2029 vops and recording the result in the available expression table,
2030 we may be able to expose more redundant loads. */
2031 if (!gimple_has_volatile_ops (stmt)
2032 && gimple_references_memory_p (stmt)
2033 && gimple_assign_single_p (stmt)
2034 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2035 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2036 && !is_gimple_reg (lhs))
2038 tree rhs = gimple_assign_rhs1 (stmt);
2039 gimple new_stmt;
2041 /* Build a new statement with the RHS and LHS exchanged. */
2042 if (TREE_CODE (rhs) == SSA_NAME)
2044 /* NOTE tuples. The call to gimple_build_assign below replaced
2045 a call to build_gimple_modify_stmt, which did not set the
2046 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2047 may cause an SSA validation failure, as the LHS may be a
2048 default-initialized name and should have no definition. I'm
2049 a bit dubious of this, as the artificial statement that we
2050 generate here may in fact be ill-formed, but it is simply
2051 used as an internal device in this pass, and never becomes
2052 part of the CFG. */
2053 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2054 new_stmt = gimple_build_assign (rhs, lhs);
2055 SSA_NAME_DEF_STMT (rhs) = defstmt;
2057 else
2058 new_stmt = gimple_build_assign (rhs, lhs);
2060 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2062 /* Finally enter the statement into the available expression
2063 table. */
2064 lookup_avail_expr (new_stmt, true);
2068 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2069 CONST_AND_COPIES. */
2071 static void
2072 cprop_operand (gimple stmt, use_operand_p op_p)
2074 tree val;
2075 tree op = USE_FROM_PTR (op_p);
2077 /* If the operand has a known constant value or it is known to be a
2078 copy of some other variable, use the value or copy stored in
2079 CONST_AND_COPIES. */
2080 val = SSA_NAME_VALUE (op);
2081 if (val && val != op)
2083 /* Do not replace hard register operands in asm statements. */
2084 if (gimple_code (stmt) == GIMPLE_ASM
2085 && !may_propagate_copy_into_asm (op))
2086 return;
2088 /* Certain operands are not allowed to be copy propagated due
2089 to their interaction with exception handling and some GCC
2090 extensions. */
2091 if (!may_propagate_copy (op, val))
2092 return;
2094 /* Do not propagate addresses that point to volatiles into memory
2095 stmts without volatile operands. */
2096 if (POINTER_TYPE_P (TREE_TYPE (val))
2097 && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
2098 && gimple_has_mem_ops (stmt)
2099 && !gimple_has_volatile_ops (stmt))
2100 return;
2102 /* Do not propagate copies if the propagated value is at a deeper loop
2103 depth than the propagatee. Otherwise, this may move loop variant
2104 variables outside of their loops and prevent coalescing
2105 opportunities. If the value was loop invariant, it will be hoisted
2106 by LICM and exposed for copy propagation. */
2107 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2108 return;
2110 /* Do not propagate copies into simple IV increment statements.
2111 See PR23821 for how this can disturb IV analysis. */
2112 if (TREE_CODE (val) != INTEGER_CST
2113 && simple_iv_increment_p (stmt))
2114 return;
2116 /* Dump details. */
2117 if (dump_file && (dump_flags & TDF_DETAILS))
2119 fprintf (dump_file, " Replaced '");
2120 print_generic_expr (dump_file, op, dump_flags);
2121 fprintf (dump_file, "' with %s '",
2122 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2123 print_generic_expr (dump_file, val, dump_flags);
2124 fprintf (dump_file, "'\n");
2127 if (TREE_CODE (val) != SSA_NAME)
2128 opt_stats.num_const_prop++;
2129 else
2130 opt_stats.num_copy_prop++;
2132 propagate_value (op_p, val);
2134 /* And note that we modified this statement. This is now
2135 safe, even if we changed virtual operands since we will
2136 rescan the statement and rewrite its operands again. */
2137 gimple_set_modified (stmt, true);
2141 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2142 known value for that SSA_NAME (or NULL if no value is known).
2144 Propagate values from CONST_AND_COPIES into the uses, vuses and
2145 vdef_ops of STMT. */
2147 static void
2148 cprop_into_stmt (gimple stmt)
2150 use_operand_p op_p;
2151 ssa_op_iter iter;
2153 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2154 cprop_operand (stmt, op_p);
2157 /* Optimize the statement pointed to by iterator SI.
2159 We try to perform some simplistic global redundancy elimination and
2160 constant propagation:
2162 1- To detect global redundancy, we keep track of expressions that have
2163 been computed in this block and its dominators. If we find that the
2164 same expression is computed more than once, we eliminate repeated
2165 computations by using the target of the first one.
2167 2- Constant values and copy assignments. This is used to do very
2168 simplistic constant and copy propagation. When a constant or copy
2169 assignment is found, we map the value on the RHS of the assignment to
2170 the variable in the LHS in the CONST_AND_COPIES table. */
2172 static void
2173 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2175 gimple stmt, old_stmt;
2176 bool may_optimize_p;
2177 bool modified_p = false;
2179 old_stmt = stmt = gsi_stmt (si);
2181 if (dump_file && (dump_flags & TDF_DETAILS))
2183 fprintf (dump_file, "Optimizing statement ");
2184 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2187 if (gimple_code (stmt) == GIMPLE_COND)
2188 canonicalize_comparison (stmt);
2190 update_stmt_if_modified (stmt);
2191 opt_stats.num_stmts++;
2193 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2194 cprop_into_stmt (stmt);
2196 /* If the statement has been modified with constant replacements,
2197 fold its RHS before checking for redundant computations. */
2198 if (gimple_modified_p (stmt))
2200 tree rhs = NULL;
2202 /* Try to fold the statement making sure that STMT is kept
2203 up to date. */
2204 if (fold_stmt (&si))
2206 stmt = gsi_stmt (si);
2207 gimple_set_modified (stmt, true);
2209 if (dump_file && (dump_flags & TDF_DETAILS))
2211 fprintf (dump_file, " Folded to: ");
2212 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2216 /* We only need to consider cases that can yield a gimple operand. */
2217 if (gimple_assign_single_p (stmt))
2218 rhs = gimple_assign_rhs1 (stmt);
2219 else if (gimple_code (stmt) == GIMPLE_GOTO)
2220 rhs = gimple_goto_dest (stmt);
2221 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2222 /* This should never be an ADDR_EXPR. */
2223 rhs = gimple_switch_index (stmt);
2225 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2226 recompute_tree_invariant_for_addr_expr (rhs);
2228 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2229 even if fold_stmt updated the stmt already and thus cleared
2230 gimple_modified_p flag on it. */
2231 modified_p = true;
2234 /* Check for redundant computations. Do this optimization only
2235 for assignments that have no volatile ops and conditionals. */
2236 may_optimize_p = (!gimple_has_side_effects (stmt)
2237 && (is_gimple_assign (stmt)
2238 || (is_gimple_call (stmt)
2239 && gimple_call_lhs (stmt) != NULL_TREE)
2240 || gimple_code (stmt) == GIMPLE_COND
2241 || gimple_code (stmt) == GIMPLE_SWITCH));
2243 if (may_optimize_p)
2245 if (gimple_code (stmt) == GIMPLE_CALL)
2247 /* Resolve __builtin_constant_p. If it hasn't been
2248 folded to integer_one_node by now, it's fairly
2249 certain that the value simply isn't constant. */
2250 tree callee = gimple_call_fndecl (stmt);
2251 if (callee
2252 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2253 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2255 propagate_tree_value_into_stmt (&si, integer_zero_node);
2256 stmt = gsi_stmt (si);
2260 update_stmt_if_modified (stmt);
2261 eliminate_redundant_computations (&si);
2262 stmt = gsi_stmt (si);
2264 /* Perform simple redundant store elimination. */
2265 if (gimple_assign_single_p (stmt)
2266 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2268 tree lhs = gimple_assign_lhs (stmt);
2269 tree rhs = gimple_assign_rhs1 (stmt);
2270 tree cached_lhs;
2271 gimple new_stmt;
2272 if (TREE_CODE (rhs) == SSA_NAME)
2274 tree tem = SSA_NAME_VALUE (rhs);
2275 if (tem)
2276 rhs = tem;
2278 /* Build a new statement with the RHS and LHS exchanged. */
2279 if (TREE_CODE (rhs) == SSA_NAME)
2281 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2282 new_stmt = gimple_build_assign (rhs, lhs);
2283 SSA_NAME_DEF_STMT (rhs) = defstmt;
2285 else
2286 new_stmt = gimple_build_assign (rhs, lhs);
2287 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2288 cached_lhs = lookup_avail_expr (new_stmt, false);
2289 if (cached_lhs
2290 && rhs == cached_lhs)
2292 basic_block bb = gimple_bb (stmt);
2293 unlink_stmt_vdef (stmt);
2294 if (gsi_remove (&si, true))
2296 bitmap_set_bit (need_eh_cleanup, bb->index);
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2298 fprintf (dump_file, " Flagged to clear EH edges.\n");
2300 release_defs (stmt);
2301 return;
2306 /* Record any additional equivalences created by this statement. */
2307 if (is_gimple_assign (stmt))
2308 record_equivalences_from_stmt (stmt, may_optimize_p);
2310 /* If STMT is a COND_EXPR and it was modified, then we may know
2311 where it goes. If that is the case, then mark the CFG as altered.
2313 This will cause us to later call remove_unreachable_blocks and
2314 cleanup_tree_cfg when it is safe to do so. It is not safe to
2315 clean things up here since removal of edges and such can trigger
2316 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2317 the manager.
2319 That's all fine and good, except that once SSA_NAMEs are released
2320 to the manager, we must not call create_ssa_name until all references
2321 to released SSA_NAMEs have been eliminated.
2323 All references to the deleted SSA_NAMEs can not be eliminated until
2324 we remove unreachable blocks.
2326 We can not remove unreachable blocks until after we have completed
2327 any queued jump threading.
2329 We can not complete any queued jump threads until we have taken
2330 appropriate variables out of SSA form. Taking variables out of
2331 SSA form can call create_ssa_name and thus we lose.
2333 Ultimately I suspect we're going to need to change the interface
2334 into the SSA_NAME manager. */
2335 if (gimple_modified_p (stmt) || modified_p)
2337 tree val = NULL;
2339 update_stmt_if_modified (stmt);
2341 if (gimple_code (stmt) == GIMPLE_COND)
2342 val = fold_binary_loc (gimple_location (stmt),
2343 gimple_cond_code (stmt), boolean_type_node,
2344 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2345 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2346 val = gimple_switch_index (stmt);
2348 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2349 cfg_altered = true;
2351 /* If we simplified a statement in such a way as to be shown that it
2352 cannot trap, update the eh information and the cfg to match. */
2353 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2355 bitmap_set_bit (need_eh_cleanup, bb->index);
2356 if (dump_file && (dump_flags & TDF_DETAILS))
2357 fprintf (dump_file, " Flagged to clear EH edges.\n");
2362 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2363 If found, return its LHS. Otherwise insert STMT in the table and
2364 return NULL_TREE.
2366 Also, when an expression is first inserted in the table, it is also
2367 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2368 we finish processing this block and its children. */
2370 static tree
2371 lookup_avail_expr (gimple stmt, bool insert)
2373 void **slot;
2374 tree lhs;
2375 tree temp;
2376 struct expr_hash_elt element;
2378 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2379 if (gimple_code (stmt) == GIMPLE_PHI)
2380 lhs = gimple_phi_result (stmt);
2381 else
2382 lhs = gimple_get_lhs (stmt);
2384 initialize_hash_element (stmt, lhs, &element);
2386 if (dump_file && (dump_flags & TDF_DETAILS))
2388 fprintf (dump_file, "LKUP ");
2389 print_expr_hash_elt (dump_file, &element);
2392 /* Don't bother remembering constant assignments and copy operations.
2393 Constants and copy operations are handled by the constant/copy propagator
2394 in optimize_stmt. */
2395 if (element.expr.kind == EXPR_SINGLE
2396 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2397 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2398 return NULL_TREE;
2400 /* Finally try to find the expression in the main expression hash table. */
2401 slot = htab_find_slot_with_hash (avail_exprs, &element, element.hash,
2402 (insert ? INSERT : NO_INSERT));
2403 if (slot == NULL)
2404 return NULL_TREE;
2406 if (*slot == NULL)
2408 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2409 *element2 = element;
2410 element2->stamp = element2;
2411 *slot = (void *) element2;
2413 if (dump_file && (dump_flags & TDF_DETAILS))
2415 fprintf (dump_file, "2>>> ");
2416 print_expr_hash_elt (dump_file, element2);
2419 VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element2);
2420 return NULL_TREE;
2423 /* Extract the LHS of the assignment so that it can be used as the current
2424 definition of another variable. */
2425 lhs = ((struct expr_hash_elt *)*slot)->lhs;
2427 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2428 use the value from the const_and_copies table. */
2429 if (TREE_CODE (lhs) == SSA_NAME)
2431 temp = SSA_NAME_VALUE (lhs);
2432 if (temp)
2433 lhs = temp;
2436 if (dump_file && (dump_flags & TDF_DETAILS))
2438 fprintf (dump_file, "FIND: ");
2439 print_generic_expr (dump_file, lhs, 0);
2440 fprintf (dump_file, "\n");
2443 return lhs;
2446 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2447 for expressions using the code of the expression and the SSA numbers of
2448 its operands. */
2450 static hashval_t
2451 avail_expr_hash (const void *p)
2453 gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
2454 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2455 tree vuse;
2456 hashval_t val = 0;
2458 val = iterative_hash_hashable_expr (expr, val);
2460 /* If the hash table entry is not associated with a statement, then we
2461 can just hash the expression and not worry about virtual operands
2462 and such. */
2463 if (!stmt)
2464 return val;
2466 /* Add the SSA version numbers of the vuse operand. This is important
2467 because compound variables like arrays are not renamed in the
2468 operands. Rather, the rename is done on the virtual variable
2469 representing all the elements of the array. */
2470 if ((vuse = gimple_vuse (stmt)))
2471 val = iterative_hash_expr (vuse, val);
2473 return val;
2476 static hashval_t
2477 real_avail_expr_hash (const void *p)
2479 return ((const struct expr_hash_elt *)p)->hash;
2482 static int
2483 avail_expr_eq (const void *p1, const void *p2)
2485 gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt;
2486 const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr;
2487 const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp;
2488 gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt;
2489 const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr;
2490 const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp;
2492 /* This case should apply only when removing entries from the table. */
2493 if (stamp1 == stamp2)
2494 return true;
2496 /* FIXME tuples:
2497 We add stmts to a hash table and them modify them. To detect the case
2498 that we modify a stmt and then search for it, we assume that the hash
2499 is always modified by that change.
2500 We have to fully check why this doesn't happen on trunk or rewrite
2501 this in a more reliable (and easier to understand) way. */
2502 if (((const struct expr_hash_elt *)p1)->hash
2503 != ((const struct expr_hash_elt *)p2)->hash)
2504 return false;
2506 /* In case of a collision, both RHS have to be identical and have the
2507 same VUSE operands. */
2508 if (hashable_expr_equal_p (expr1, expr2)
2509 && types_compatible_p (expr1->type, expr2->type))
2511 /* Note that STMT1 and/or STMT2 may be NULL. */
2512 return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
2513 == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
2516 return false;
2519 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2520 up degenerate PHIs created by or exposed by jump threading. */
2522 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2523 NULL. */
2525 tree
2526 degenerate_phi_result (gimple phi)
2528 tree lhs = gimple_phi_result (phi);
2529 tree val = NULL;
2530 size_t i;
2532 /* Ignoring arguments which are the same as LHS, if all the remaining
2533 arguments are the same, then the PHI is a degenerate and has the
2534 value of that common argument. */
2535 for (i = 0; i < gimple_phi_num_args (phi); i++)
2537 tree arg = gimple_phi_arg_def (phi, i);
2539 if (arg == lhs)
2540 continue;
2541 else if (!arg)
2542 break;
2543 else if (!val)
2544 val = arg;
2545 else if (arg == val)
2546 continue;
2547 /* We bring in some of operand_equal_p not only to speed things
2548 up, but also to avoid crashing when dereferencing the type of
2549 a released SSA name. */
2550 else if (TREE_CODE (val) != TREE_CODE (arg)
2551 || TREE_CODE (val) == SSA_NAME
2552 || !operand_equal_p (arg, val, 0))
2553 break;
2555 return (i == gimple_phi_num_args (phi) ? val : NULL);
2558 /* Given a statement STMT, which is either a PHI node or an assignment,
2559 remove it from the IL. */
2561 static void
2562 remove_stmt_or_phi (gimple stmt)
2564 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2566 if (gimple_code (stmt) == GIMPLE_PHI)
2567 remove_phi_node (&gsi, true);
2568 else
2570 gsi_remove (&gsi, true);
2571 release_defs (stmt);
2575 /* Given a statement STMT, which is either a PHI node or an assignment,
2576 return the "rhs" of the node, in the case of a non-degenerate
2577 phi, NULL is returned. */
2579 static tree
2580 get_rhs_or_phi_arg (gimple stmt)
2582 if (gimple_code (stmt) == GIMPLE_PHI)
2583 return degenerate_phi_result (stmt);
2584 else if (gimple_assign_single_p (stmt))
2585 return gimple_assign_rhs1 (stmt);
2586 else
2587 gcc_unreachable ();
2591 /* Given a statement STMT, which is either a PHI node or an assignment,
2592 return the "lhs" of the node. */
2594 static tree
2595 get_lhs_or_phi_result (gimple stmt)
2597 if (gimple_code (stmt) == GIMPLE_PHI)
2598 return gimple_phi_result (stmt);
2599 else if (is_gimple_assign (stmt))
2600 return gimple_assign_lhs (stmt);
2601 else
2602 gcc_unreachable ();
2605 /* Propagate RHS into all uses of LHS (when possible).
2607 RHS and LHS are derived from STMT, which is passed in solely so
2608 that we can remove it if propagation is successful.
2610 When propagating into a PHI node or into a statement which turns
2611 into a trivial copy or constant initialization, set the
2612 appropriate bit in INTERESTING_NAMEs so that we will visit those
2613 nodes as well in an effort to pick up secondary optimization
2614 opportunities. */
2616 static void
2617 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2619 /* First verify that propagation is valid and isn't going to move a
2620 loop variant variable outside its loop. */
2621 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2622 && (TREE_CODE (rhs) != SSA_NAME
2623 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2624 && may_propagate_copy (lhs, rhs)
2625 && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2627 use_operand_p use_p;
2628 imm_use_iterator iter;
2629 gimple use_stmt;
2630 bool all = true;
2632 /* Dump details. */
2633 if (dump_file && (dump_flags & TDF_DETAILS))
2635 fprintf (dump_file, " Replacing '");
2636 print_generic_expr (dump_file, lhs, dump_flags);
2637 fprintf (dump_file, "' with %s '",
2638 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2639 print_generic_expr (dump_file, rhs, dump_flags);
2640 fprintf (dump_file, "'\n");
2643 /* Walk over every use of LHS and try to replace the use with RHS.
2644 At this point the only reason why such a propagation would not
2645 be successful would be if the use occurs in an ASM_EXPR. */
2646 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2648 /* Leave debug stmts alone. If we succeed in propagating
2649 all non-debug uses, we'll drop the DEF, and propagation
2650 into debug stmts will occur then. */
2651 if (gimple_debug_bind_p (use_stmt))
2652 continue;
2654 /* It's not always safe to propagate into an ASM_EXPR. */
2655 if (gimple_code (use_stmt) == GIMPLE_ASM
2656 && ! may_propagate_copy_into_asm (lhs))
2658 all = false;
2659 continue;
2662 /* It's not ok to propagate into the definition stmt of RHS.
2663 <bb 9>:
2664 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2665 g_67.1_6 = prephitmp.12_36;
2666 goto <bb 9>;
2667 While this is strictly all dead code we do not want to
2668 deal with this here. */
2669 if (TREE_CODE (rhs) == SSA_NAME
2670 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2672 all = false;
2673 continue;
2676 /* Dump details. */
2677 if (dump_file && (dump_flags & TDF_DETAILS))
2679 fprintf (dump_file, " Original statement:");
2680 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2683 /* Propagate the RHS into this use of the LHS. */
2684 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2685 propagate_value (use_p, rhs);
2687 /* Special cases to avoid useless calls into the folding
2688 routines, operand scanning, etc.
2690 First, propagation into a PHI may cause the PHI to become
2691 a degenerate, so mark the PHI as interesting. No other
2692 actions are necessary.
2694 Second, if we're propagating a virtual operand and the
2695 propagation does not change the underlying _DECL node for
2696 the virtual operand, then no further actions are necessary. */
2697 if (gimple_code (use_stmt) == GIMPLE_PHI
2698 || (! is_gimple_reg (lhs)
2699 && TREE_CODE (rhs) == SSA_NAME
2700 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs)))
2702 /* Dump details. */
2703 if (dump_file && (dump_flags & TDF_DETAILS))
2705 fprintf (dump_file, " Updated statement:");
2706 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2709 /* Propagation into a PHI may expose new degenerate PHIs,
2710 so mark the result of the PHI as interesting. */
2711 if (gimple_code (use_stmt) == GIMPLE_PHI)
2713 tree result = get_lhs_or_phi_result (use_stmt);
2714 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2717 continue;
2720 /* From this point onward we are propagating into a
2721 real statement. Folding may (or may not) be possible,
2722 we may expose new operands, expose dead EH edges,
2723 etc. */
2724 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2725 cannot fold a call that simplifies to a constant,
2726 because the GIMPLE_CALL must be replaced by a
2727 GIMPLE_ASSIGN, and there is no way to effect such a
2728 transformation in-place. We might want to consider
2729 using the more general fold_stmt here. */
2731 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2732 fold_stmt_inplace (&gsi);
2735 /* Sometimes propagation can expose new operands to the
2736 renamer. */
2737 update_stmt (use_stmt);
2739 /* Dump details. */
2740 if (dump_file && (dump_flags & TDF_DETAILS))
2742 fprintf (dump_file, " Updated statement:");
2743 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2746 /* If we replaced a variable index with a constant, then
2747 we would need to update the invariant flag for ADDR_EXPRs. */
2748 if (gimple_assign_single_p (use_stmt)
2749 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2750 recompute_tree_invariant_for_addr_expr
2751 (gimple_assign_rhs1 (use_stmt));
2753 /* If we cleaned up EH information from the statement,
2754 mark its containing block as needing EH cleanups. */
2755 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2757 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2758 if (dump_file && (dump_flags & TDF_DETAILS))
2759 fprintf (dump_file, " Flagged to clear EH edges.\n");
2762 /* Propagation may expose new trivial copy/constant propagation
2763 opportunities. */
2764 if (gimple_assign_single_p (use_stmt)
2765 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2766 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2767 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2769 tree result = get_lhs_or_phi_result (use_stmt);
2770 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2773 /* Propagation into these nodes may make certain edges in
2774 the CFG unexecutable. We want to identify them as PHI nodes
2775 at the destination of those unexecutable edges may become
2776 degenerates. */
2777 else if (gimple_code (use_stmt) == GIMPLE_COND
2778 || gimple_code (use_stmt) == GIMPLE_SWITCH
2779 || gimple_code (use_stmt) == GIMPLE_GOTO)
2781 tree val;
2783 if (gimple_code (use_stmt) == GIMPLE_COND)
2784 val = fold_binary_loc (gimple_location (use_stmt),
2785 gimple_cond_code (use_stmt),
2786 boolean_type_node,
2787 gimple_cond_lhs (use_stmt),
2788 gimple_cond_rhs (use_stmt));
2789 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2790 val = gimple_switch_index (use_stmt);
2791 else
2792 val = gimple_goto_dest (use_stmt);
2794 if (val && is_gimple_min_invariant (val))
2796 basic_block bb = gimple_bb (use_stmt);
2797 edge te = find_taken_edge (bb, val);
2798 edge_iterator ei;
2799 edge e;
2800 gimple_stmt_iterator gsi, psi;
2802 /* Remove all outgoing edges except TE. */
2803 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2805 if (e != te)
2807 /* Mark all the PHI nodes at the destination of
2808 the unexecutable edge as interesting. */
2809 for (psi = gsi_start_phis (e->dest);
2810 !gsi_end_p (psi);
2811 gsi_next (&psi))
2813 gimple phi = gsi_stmt (psi);
2815 tree result = gimple_phi_result (phi);
2816 int version = SSA_NAME_VERSION (result);
2818 bitmap_set_bit (interesting_names, version);
2821 te->probability += e->probability;
2823 te->count += e->count;
2824 remove_edge (e);
2825 cfg_altered = true;
2827 else
2828 ei_next (&ei);
2831 gsi = gsi_last_bb (gimple_bb (use_stmt));
2832 gsi_remove (&gsi, true);
2834 /* And fixup the flags on the single remaining edge. */
2835 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2836 te->flags &= ~EDGE_ABNORMAL;
2837 te->flags |= EDGE_FALLTHRU;
2838 if (te->probability > REG_BR_PROB_BASE)
2839 te->probability = REG_BR_PROB_BASE;
2844 /* Ensure there is nothing else to do. */
2845 gcc_assert (!all || has_zero_uses (lhs));
2847 /* If we were able to propagate away all uses of LHS, then
2848 we can remove STMT. */
2849 if (all)
2850 remove_stmt_or_phi (stmt);
2854 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2855 a statement that is a trivial copy or constant initialization.
2857 Attempt to eliminate T by propagating its RHS into all uses of
2858 its LHS. This may in turn set new bits in INTERESTING_NAMES
2859 for nodes we want to revisit later.
2861 All exit paths should clear INTERESTING_NAMES for the result
2862 of STMT. */
2864 static void
2865 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2867 tree lhs = get_lhs_or_phi_result (stmt);
2868 tree rhs;
2869 int version = SSA_NAME_VERSION (lhs);
2871 /* If the LHS of this statement or PHI has no uses, then we can
2872 just eliminate it. This can occur if, for example, the PHI
2873 was created by block duplication due to threading and its only
2874 use was in the conditional at the end of the block which was
2875 deleted. */
2876 if (has_zero_uses (lhs))
2878 bitmap_clear_bit (interesting_names, version);
2879 remove_stmt_or_phi (stmt);
2880 return;
2883 /* Get the RHS of the assignment or PHI node if the PHI is a
2884 degenerate. */
2885 rhs = get_rhs_or_phi_arg (stmt);
2886 if (!rhs)
2888 bitmap_clear_bit (interesting_names, version);
2889 return;
2892 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
2894 /* Note that STMT may well have been deleted by now, so do
2895 not access it, instead use the saved version # to clear
2896 T's entry in the worklist. */
2897 bitmap_clear_bit (interesting_names, version);
2900 /* The first phase in degenerate PHI elimination.
2902 Eliminate the degenerate PHIs in BB, then recurse on the
2903 dominator children of BB. */
2905 static void
2906 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2908 gimple_stmt_iterator gsi;
2909 basic_block son;
2911 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2913 gimple phi = gsi_stmt (gsi);
2915 eliminate_const_or_copy (phi, interesting_names);
2918 /* Recurse into the dominator children of BB. */
2919 for (son = first_dom_son (CDI_DOMINATORS, bb);
2920 son;
2921 son = next_dom_son (CDI_DOMINATORS, son))
2922 eliminate_degenerate_phis_1 (son, interesting_names);
2926 /* A very simple pass to eliminate degenerate PHI nodes from the
2927 IL. This is meant to be fast enough to be able to be run several
2928 times in the optimization pipeline.
2930 Certain optimizations, particularly those which duplicate blocks
2931 or remove edges from the CFG can create or expose PHIs which are
2932 trivial copies or constant initializations.
2934 While we could pick up these optimizations in DOM or with the
2935 combination of copy-prop and CCP, those solutions are far too
2936 heavy-weight for our needs.
2938 This implementation has two phases so that we can efficiently
2939 eliminate the first order degenerate PHIs and second order
2940 degenerate PHIs.
2942 The first phase performs a dominator walk to identify and eliminate
2943 the vast majority of the degenerate PHIs. When a degenerate PHI
2944 is identified and eliminated any affected statements or PHIs
2945 are put on a worklist.
2947 The second phase eliminates degenerate PHIs and trivial copies
2948 or constant initializations using the worklist. This is how we
2949 pick up the secondary optimization opportunities with minimal
2950 cost. */
2952 static unsigned int
2953 eliminate_degenerate_phis (void)
2955 bitmap interesting_names;
2956 bitmap interesting_names1;
2958 /* Bitmap of blocks which need EH information updated. We can not
2959 update it on-the-fly as doing so invalidates the dominator tree. */
2960 need_eh_cleanup = BITMAP_ALLOC (NULL);
2962 /* INTERESTING_NAMES is effectively our worklist, indexed by
2963 SSA_NAME_VERSION.
2965 A set bit indicates that the statement or PHI node which
2966 defines the SSA_NAME should be (re)examined to determine if
2967 it has become a degenerate PHI or trivial const/copy propagation
2968 opportunity.
2970 Experiments have show we generally get better compilation
2971 time behavior with bitmaps rather than sbitmaps. */
2972 interesting_names = BITMAP_ALLOC (NULL);
2973 interesting_names1 = BITMAP_ALLOC (NULL);
2975 calculate_dominance_info (CDI_DOMINATORS);
2976 cfg_altered = false;
2978 /* First phase. Eliminate degenerate PHIs via a dominator
2979 walk of the CFG.
2981 Experiments have indicated that we generally get better
2982 compile-time behavior by visiting blocks in the first
2983 phase in dominator order. Presumably this is because walking
2984 in dominator order leaves fewer PHIs for later examination
2985 by the worklist phase. */
2986 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
2988 /* Second phase. Eliminate second order degenerate PHIs as well
2989 as trivial copies or constant initializations identified by
2990 the first phase or this phase. Basically we keep iterating
2991 until our set of INTERESTING_NAMEs is empty. */
2992 while (!bitmap_empty_p (interesting_names))
2994 unsigned int i;
2995 bitmap_iterator bi;
2997 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
2998 changed during the loop. Copy it to another bitmap and
2999 use that. */
3000 bitmap_copy (interesting_names1, interesting_names);
3002 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3004 tree name = ssa_name (i);
3006 /* Ignore SSA_NAMEs that have been released because
3007 their defining statement was deleted (unreachable). */
3008 if (name)
3009 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3010 interesting_names);
3014 if (cfg_altered)
3015 free_dominance_info (CDI_DOMINATORS);
3017 /* Propagation of const and copies may make some EH edges dead. Purge
3018 such edges from the CFG as needed. */
3019 if (!bitmap_empty_p (need_eh_cleanup))
3021 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3022 BITMAP_FREE (need_eh_cleanup);
3025 BITMAP_FREE (interesting_names);
3026 BITMAP_FREE (interesting_names1);
3027 return 0;
3030 struct gimple_opt_pass pass_phi_only_cprop =
3033 GIMPLE_PASS,
3034 "phicprop", /* name */
3035 gate_dominator, /* gate */
3036 eliminate_degenerate_phis, /* execute */
3037 NULL, /* sub */
3038 NULL, /* next */
3039 0, /* static_pass_number */
3040 TV_TREE_PHI_CPROP, /* tv_id */
3041 PROP_cfg | PROP_ssa, /* properties_required */
3042 0, /* properties_provided */
3043 0, /* properties_destroyed */
3044 0, /* todo_flags_start */
3045 TODO_cleanup_cfg
3046 | TODO_ggc_collect
3047 | TODO_verify_ssa
3048 | TODO_verify_stmts
3049 | TODO_update_ssa /* todo_flags_finish */