2013-04-03 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / tree-ssa-dom.c
blob29d2bb4f3e195deba52d91a5f2e871f72109ce72
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "function.h"
31 #include "gimple-pretty-print.h"
32 #include "tree-flow.h"
33 #include "domwalk.h"
34 #include "tree-pass.h"
35 #include "tree-ssa-propagate.h"
36 #include "langhooks.h"
37 #include "params.h"
39 /* This file implements optimizations on the dominator tree. */
41 /* Representation of a "naked" right-hand-side expression, to be used
42 in recording available expressions in the expression hash table. */
44 enum expr_kind
46 EXPR_SINGLE,
47 EXPR_UNARY,
48 EXPR_BINARY,
49 EXPR_TERNARY,
50 EXPR_CALL,
51 EXPR_PHI
54 struct hashable_expr
56 tree type;
57 enum expr_kind kind;
58 union {
59 struct { tree rhs; } single;
60 struct { enum tree_code op; tree opnd; } unary;
61 struct { enum tree_code op; tree opnd0, opnd1; } binary;
62 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
63 struct { gimple fn_from; bool pure; size_t nargs; tree *args; } call;
64 struct { size_t nargs; tree *args; } phi;
65 } ops;
68 /* Structure for recording known values of a conditional expression
69 at the exits from its block. */
71 typedef struct cond_equivalence_s
73 struct hashable_expr cond;
74 tree value;
75 } cond_equivalence;
78 /* Structure for recording edge equivalences as well as any pending
79 edge redirections during the dominator optimizer.
81 Computing and storing the edge equivalences instead of creating
82 them on-demand can save significant amounts of time, particularly
83 for pathological cases involving switch statements.
85 These structures live for a single iteration of the dominator
86 optimizer in the edge's AUX field. At the end of an iteration we
87 free each of these structures and update the AUX field to point
88 to any requested redirection target (the code for updating the
89 CFG and SSA graph for edge redirection expects redirection edge
90 targets to be in the AUX field for each edge. */
92 struct edge_info
94 /* If this edge creates a simple equivalence, the LHS and RHS of
95 the equivalence will be stored here. */
96 tree lhs;
97 tree rhs;
99 /* Traversing an edge may also indicate one or more particular conditions
100 are true or false. */
101 vec<cond_equivalence> cond_equivalences;
104 /* Hash table with expressions made available during the renaming process.
105 When an assignment of the form X_i = EXPR is found, the statement is
106 stored in this table. If the same expression EXPR is later found on the
107 RHS of another statement, it is replaced with X_i (thus performing
108 global redundancy elimination). Similarly as we pass through conditionals
109 we record the conditional itself as having either a true or false value
110 in this table. */
111 static htab_t avail_exprs;
113 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
114 expressions it enters into the hash table along with a marker entry
115 (null). When we finish processing the block, we pop off entries and
116 remove the expressions from the global hash table until we hit the
117 marker. */
118 typedef struct expr_hash_elt * expr_hash_elt_t;
120 static vec<expr_hash_elt_t> avail_exprs_stack;
122 /* Structure for entries in the expression hash table. */
124 struct expr_hash_elt
126 /* The value (lhs) of this expression. */
127 tree lhs;
129 /* The expression (rhs) we want to record. */
130 struct hashable_expr expr;
132 /* The stmt pointer if this element corresponds to a statement. */
133 gimple stmt;
135 /* The hash value for RHS. */
136 hashval_t hash;
138 /* A unique stamp, typically the address of the hash
139 element itself, used in removing entries from the table. */
140 struct expr_hash_elt *stamp;
143 /* Stack of dest,src pairs that need to be restored during finalization.
145 A NULL entry is used to mark the end of pairs which need to be
146 restored during finalization of this block. */
147 static vec<tree> const_and_copies_stack;
149 /* Track whether or not we have changed the control flow graph. */
150 static bool cfg_altered;
152 /* Bitmap of blocks that have had EH statements cleaned. We should
153 remove their dead edges eventually. */
154 static bitmap need_eh_cleanup;
156 /* Statistics for dominator optimizations. */
157 struct opt_stats_d
159 long num_stmts;
160 long num_exprs_considered;
161 long num_re;
162 long num_const_prop;
163 long num_copy_prop;
166 static struct opt_stats_d opt_stats;
168 /* Local functions. */
169 static void optimize_stmt (basic_block, gimple_stmt_iterator);
170 static tree lookup_avail_expr (gimple, bool);
171 static hashval_t avail_expr_hash (const void *);
172 static hashval_t real_avail_expr_hash (const void *);
173 static int avail_expr_eq (const void *, const void *);
174 static void htab_statistics (FILE *, htab_t);
175 static void record_cond (cond_equivalence *);
176 static void record_const_or_copy (tree, tree);
177 static void record_equality (tree, tree);
178 static void record_equivalences_from_phis (basic_block);
179 static void record_equivalences_from_incoming_edge (basic_block);
180 static void eliminate_redundant_computations (gimple_stmt_iterator *);
181 static void record_equivalences_from_stmt (gimple, int);
182 static void dom_thread_across_edge (struct dom_walk_data *, edge);
183 static void dom_opt_leave_block (struct dom_walk_data *, basic_block);
184 static void dom_opt_enter_block (struct dom_walk_data *, basic_block);
185 static void remove_local_expressions_from_table (void);
186 static void restore_vars_to_original_value (void);
187 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
190 /* Given a statement STMT, initialize the hash table element pointed to
191 by ELEMENT. */
193 static void
194 initialize_hash_element (gimple stmt, tree lhs,
195 struct expr_hash_elt *element)
197 enum gimple_code code = gimple_code (stmt);
198 struct hashable_expr *expr = &element->expr;
200 if (code == GIMPLE_ASSIGN)
202 enum tree_code subcode = gimple_assign_rhs_code (stmt);
204 switch (get_gimple_rhs_class (subcode))
206 case GIMPLE_SINGLE_RHS:
207 expr->kind = EXPR_SINGLE;
208 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
209 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
210 break;
211 case GIMPLE_UNARY_RHS:
212 expr->kind = EXPR_UNARY;
213 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
214 expr->ops.unary.op = subcode;
215 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
216 break;
217 case GIMPLE_BINARY_RHS:
218 expr->kind = EXPR_BINARY;
219 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
220 expr->ops.binary.op = subcode;
221 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
222 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
223 break;
224 case GIMPLE_TERNARY_RHS:
225 expr->kind = EXPR_TERNARY;
226 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
227 expr->ops.ternary.op = subcode;
228 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
229 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
230 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
231 break;
232 default:
233 gcc_unreachable ();
236 else if (code == GIMPLE_COND)
238 expr->type = boolean_type_node;
239 expr->kind = EXPR_BINARY;
240 expr->ops.binary.op = gimple_cond_code (stmt);
241 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
242 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
244 else if (code == GIMPLE_CALL)
246 size_t nargs = gimple_call_num_args (stmt);
247 size_t i;
249 gcc_assert (gimple_call_lhs (stmt));
251 expr->type = TREE_TYPE (gimple_call_lhs (stmt));
252 expr->kind = EXPR_CALL;
253 expr->ops.call.fn_from = stmt;
255 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
256 expr->ops.call.pure = true;
257 else
258 expr->ops.call.pure = false;
260 expr->ops.call.nargs = nargs;
261 expr->ops.call.args = XCNEWVEC (tree, nargs);
262 for (i = 0; i < nargs; i++)
263 expr->ops.call.args[i] = gimple_call_arg (stmt, i);
265 else if (code == GIMPLE_SWITCH)
267 expr->type = TREE_TYPE (gimple_switch_index (stmt));
268 expr->kind = EXPR_SINGLE;
269 expr->ops.single.rhs = gimple_switch_index (stmt);
271 else if (code == GIMPLE_GOTO)
273 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
274 expr->kind = EXPR_SINGLE;
275 expr->ops.single.rhs = gimple_goto_dest (stmt);
277 else if (code == GIMPLE_PHI)
279 size_t nargs = gimple_phi_num_args (stmt);
280 size_t i;
282 expr->type = TREE_TYPE (gimple_phi_result (stmt));
283 expr->kind = EXPR_PHI;
284 expr->ops.phi.nargs = nargs;
285 expr->ops.phi.args = XCNEWVEC (tree, nargs);
287 for (i = 0; i < nargs; i++)
288 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
290 else
291 gcc_unreachable ();
293 element->lhs = lhs;
294 element->stmt = stmt;
295 element->hash = avail_expr_hash (element);
296 element->stamp = element;
299 /* Given a conditional expression COND as a tree, initialize
300 a hashable_expr expression EXPR. The conditional must be a
301 comparison or logical negation. A constant or a variable is
302 not permitted. */
304 static void
305 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
307 expr->type = boolean_type_node;
309 if (COMPARISON_CLASS_P (cond))
311 expr->kind = EXPR_BINARY;
312 expr->ops.binary.op = TREE_CODE (cond);
313 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
314 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
316 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
318 expr->kind = EXPR_UNARY;
319 expr->ops.unary.op = TRUTH_NOT_EXPR;
320 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
322 else
323 gcc_unreachable ();
326 /* Given a hashable_expr expression EXPR and an LHS,
327 initialize the hash table element pointed to by ELEMENT. */
329 static void
330 initialize_hash_element_from_expr (struct hashable_expr *expr,
331 tree lhs,
332 struct expr_hash_elt *element)
334 element->expr = *expr;
335 element->lhs = lhs;
336 element->stmt = NULL;
337 element->hash = avail_expr_hash (element);
338 element->stamp = element;
341 /* Compare two hashable_expr structures for equivalence.
342 They are considered equivalent when the the expressions
343 they denote must necessarily be equal. The logic is intended
344 to follow that of operand_equal_p in fold-const.c */
346 static bool
347 hashable_expr_equal_p (const struct hashable_expr *expr0,
348 const struct hashable_expr *expr1)
350 tree type0 = expr0->type;
351 tree type1 = expr1->type;
353 /* If either type is NULL, there is nothing to check. */
354 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
355 return false;
357 /* If both types don't have the same signedness, precision, and mode,
358 then we can't consider them equal. */
359 if (type0 != type1
360 && (TREE_CODE (type0) == ERROR_MARK
361 || TREE_CODE (type1) == ERROR_MARK
362 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
363 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
364 || TYPE_MODE (type0) != TYPE_MODE (type1)))
365 return false;
367 if (expr0->kind != expr1->kind)
368 return false;
370 switch (expr0->kind)
372 case EXPR_SINGLE:
373 return operand_equal_p (expr0->ops.single.rhs,
374 expr1->ops.single.rhs, 0);
376 case EXPR_UNARY:
377 if (expr0->ops.unary.op != expr1->ops.unary.op)
378 return false;
380 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
381 || expr0->ops.unary.op == NON_LVALUE_EXPR)
382 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
383 return false;
385 return operand_equal_p (expr0->ops.unary.opnd,
386 expr1->ops.unary.opnd, 0);
388 case EXPR_BINARY:
389 if (expr0->ops.binary.op != expr1->ops.binary.op)
390 return false;
392 if (operand_equal_p (expr0->ops.binary.opnd0,
393 expr1->ops.binary.opnd0, 0)
394 && operand_equal_p (expr0->ops.binary.opnd1,
395 expr1->ops.binary.opnd1, 0))
396 return true;
398 /* For commutative ops, allow the other order. */
399 return (commutative_tree_code (expr0->ops.binary.op)
400 && operand_equal_p (expr0->ops.binary.opnd0,
401 expr1->ops.binary.opnd1, 0)
402 && operand_equal_p (expr0->ops.binary.opnd1,
403 expr1->ops.binary.opnd0, 0));
405 case EXPR_TERNARY:
406 if (expr0->ops.ternary.op != expr1->ops.ternary.op
407 || !operand_equal_p (expr0->ops.ternary.opnd2,
408 expr1->ops.ternary.opnd2, 0))
409 return false;
411 if (operand_equal_p (expr0->ops.ternary.opnd0,
412 expr1->ops.ternary.opnd0, 0)
413 && operand_equal_p (expr0->ops.ternary.opnd1,
414 expr1->ops.ternary.opnd1, 0))
415 return true;
417 /* For commutative ops, allow the other order. */
418 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
419 && operand_equal_p (expr0->ops.ternary.opnd0,
420 expr1->ops.ternary.opnd1, 0)
421 && operand_equal_p (expr0->ops.ternary.opnd1,
422 expr1->ops.ternary.opnd0, 0));
424 case EXPR_CALL:
426 size_t i;
428 /* If the calls are to different functions, then they
429 clearly cannot be equal. */
430 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
431 expr1->ops.call.fn_from))
432 return false;
434 if (! expr0->ops.call.pure)
435 return false;
437 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
438 return false;
440 for (i = 0; i < expr0->ops.call.nargs; i++)
441 if (! operand_equal_p (expr0->ops.call.args[i],
442 expr1->ops.call.args[i], 0))
443 return false;
445 return true;
448 case EXPR_PHI:
450 size_t i;
452 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
453 return false;
455 for (i = 0; i < expr0->ops.phi.nargs; i++)
456 if (! operand_equal_p (expr0->ops.phi.args[i],
457 expr1->ops.phi.args[i], 0))
458 return false;
460 return true;
463 default:
464 gcc_unreachable ();
468 /* Compute a hash value for a hashable_expr value EXPR and a
469 previously accumulated hash value VAL. If two hashable_expr
470 values compare equal with hashable_expr_equal_p, they must
471 hash to the same value, given an identical value of VAL.
472 The logic is intended to follow iterative_hash_expr in tree.c. */
474 static hashval_t
475 iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val)
477 switch (expr->kind)
479 case EXPR_SINGLE:
480 val = iterative_hash_expr (expr->ops.single.rhs, val);
481 break;
483 case EXPR_UNARY:
484 val = iterative_hash_object (expr->ops.unary.op, val);
486 /* Make sure to include signedness in the hash computation.
487 Don't hash the type, that can lead to having nodes which
488 compare equal according to operand_equal_p, but which
489 have different hash codes. */
490 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
491 || expr->ops.unary.op == NON_LVALUE_EXPR)
492 val += TYPE_UNSIGNED (expr->type);
494 val = iterative_hash_expr (expr->ops.unary.opnd, val);
495 break;
497 case EXPR_BINARY:
498 val = iterative_hash_object (expr->ops.binary.op, val);
499 if (commutative_tree_code (expr->ops.binary.op))
500 val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0,
501 expr->ops.binary.opnd1, val);
502 else
504 val = iterative_hash_expr (expr->ops.binary.opnd0, val);
505 val = iterative_hash_expr (expr->ops.binary.opnd1, val);
507 break;
509 case EXPR_TERNARY:
510 val = iterative_hash_object (expr->ops.ternary.op, val);
511 if (commutative_ternary_tree_code (expr->ops.ternary.op))
512 val = iterative_hash_exprs_commutative (expr->ops.ternary.opnd0,
513 expr->ops.ternary.opnd1, val);
514 else
516 val = iterative_hash_expr (expr->ops.ternary.opnd0, val);
517 val = iterative_hash_expr (expr->ops.ternary.opnd1, val);
519 val = iterative_hash_expr (expr->ops.ternary.opnd2, val);
520 break;
522 case EXPR_CALL:
524 size_t i;
525 enum tree_code code = CALL_EXPR;
526 gimple fn_from;
528 val = iterative_hash_object (code, val);
529 fn_from = expr->ops.call.fn_from;
530 if (gimple_call_internal_p (fn_from))
531 val = iterative_hash_hashval_t
532 ((hashval_t) gimple_call_internal_fn (fn_from), val);
533 else
534 val = iterative_hash_expr (gimple_call_fn (fn_from), val);
535 for (i = 0; i < expr->ops.call.nargs; i++)
536 val = iterative_hash_expr (expr->ops.call.args[i], val);
538 break;
540 case EXPR_PHI:
542 size_t i;
544 for (i = 0; i < expr->ops.phi.nargs; i++)
545 val = iterative_hash_expr (expr->ops.phi.args[i], val);
547 break;
549 default:
550 gcc_unreachable ();
553 return val;
556 /* Print a diagnostic dump of an expression hash table entry. */
558 static void
559 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
561 if (element->stmt)
562 fprintf (stream, "STMT ");
563 else
564 fprintf (stream, "COND ");
566 if (element->lhs)
568 print_generic_expr (stream, element->lhs, 0);
569 fprintf (stream, " = ");
572 switch (element->expr.kind)
574 case EXPR_SINGLE:
575 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
576 break;
578 case EXPR_UNARY:
579 fprintf (stream, "%s ", tree_code_name[element->expr.ops.unary.op]);
580 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
581 break;
583 case EXPR_BINARY:
584 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
585 fprintf (stream, " %s ", tree_code_name[element->expr.ops.binary.op]);
586 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
587 break;
589 case EXPR_TERNARY:
590 fprintf (stream, " %s <", tree_code_name[element->expr.ops.ternary.op]);
591 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
592 fputs (", ", stream);
593 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
594 fputs (", ", stream);
595 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
596 fputs (">", stream);
597 break;
599 case EXPR_CALL:
601 size_t i;
602 size_t nargs = element->expr.ops.call.nargs;
603 gimple fn_from;
605 fn_from = element->expr.ops.call.fn_from;
606 if (gimple_call_internal_p (fn_from))
607 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
608 stream);
609 else
610 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
611 fprintf (stream, " (");
612 for (i = 0; i < nargs; i++)
614 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
615 if (i + 1 < nargs)
616 fprintf (stream, ", ");
618 fprintf (stream, ")");
620 break;
622 case EXPR_PHI:
624 size_t i;
625 size_t nargs = element->expr.ops.phi.nargs;
627 fprintf (stream, "PHI <");
628 for (i = 0; i < nargs; i++)
630 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
631 if (i + 1 < nargs)
632 fprintf (stream, ", ");
634 fprintf (stream, ">");
636 break;
638 fprintf (stream, "\n");
640 if (element->stmt)
642 fprintf (stream, " ");
643 print_gimple_stmt (stream, element->stmt, 0, 0);
647 /* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
649 static void
650 free_expr_hash_elt_contents (struct expr_hash_elt *element)
652 if (element->expr.kind == EXPR_CALL)
653 free (element->expr.ops.call.args);
654 else if (element->expr.kind == EXPR_PHI)
655 free (element->expr.ops.phi.args);
658 /* Delete an expr_hash_elt and reclaim its storage. */
660 static void
661 free_expr_hash_elt (void *elt)
663 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
664 free_expr_hash_elt_contents (element);
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 edge_info->cond_equivalences.release ();
704 free (edge_info);
705 e->aux = NULL;
711 /* Jump threading, redundancy elimination and const/copy propagation.
713 This pass may expose new symbols that need to be renamed into SSA. For
714 every new symbol exposed, its corresponding bit will be set in
715 VARS_TO_RENAME. */
717 static unsigned int
718 tree_ssa_dominator_optimize (void)
720 struct dom_walk_data walk_data;
722 memset (&opt_stats, 0, sizeof (opt_stats));
724 /* Create our hash tables. */
725 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free_expr_hash_elt);
726 avail_exprs_stack.create (20);
727 const_and_copies_stack.create (20);
728 need_eh_cleanup = BITMAP_ALLOC (NULL);
730 /* Setup callbacks for the generic dominator tree walker. */
731 walk_data.dom_direction = CDI_DOMINATORS;
732 walk_data.initialize_block_local_data = NULL;
733 walk_data.before_dom_children = dom_opt_enter_block;
734 walk_data.after_dom_children = dom_opt_leave_block;
735 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
736 When we attach more stuff we'll need to fill this out with a real
737 structure. */
738 walk_data.global_data = NULL;
739 walk_data.block_local_data_size = 0;
741 /* Now initialize the dominator walker. */
742 init_walk_dominator_tree (&walk_data);
744 calculate_dominance_info (CDI_DOMINATORS);
745 cfg_altered = false;
747 /* We need to know loop structures in order to avoid destroying them
748 in jump threading. Note that we still can e.g. thread through loop
749 headers to an exit edge, or through loop header to the loop body, assuming
750 that we update the loop info. */
751 loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
753 /* Initialize the value-handle array. */
754 threadedge_initialize_values ();
756 /* We need accurate information regarding back edges in the CFG
757 for jump threading; this may include back edges that are not part of
758 a single loop. */
759 mark_dfs_back_edges ();
761 /* Recursively walk the dominator tree optimizing statements. */
762 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
765 gimple_stmt_iterator gsi;
766 basic_block bb;
767 FOR_EACH_BB (bb)
769 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
770 update_stmt_if_modified (gsi_stmt (gsi));
774 /* If we exposed any new variables, go ahead and put them into
775 SSA form now, before we handle jump threading. This simplifies
776 interactions between rewriting of _DECL nodes into SSA form
777 and rewriting SSA_NAME nodes into SSA form after block
778 duplication and CFG manipulation. */
779 update_ssa (TODO_update_ssa);
781 free_all_edge_infos ();
783 /* Thread jumps, creating duplicate blocks as needed. */
784 cfg_altered |= thread_through_all_blocks (first_pass_instance);
786 if (cfg_altered)
787 free_dominance_info (CDI_DOMINATORS);
789 /* Removal of statements may make some EH edges dead. Purge
790 such edges from the CFG as needed. */
791 if (!bitmap_empty_p (need_eh_cleanup))
793 unsigned i;
794 bitmap_iterator bi;
796 /* Jump threading may have created forwarder blocks from blocks
797 needing EH cleanup; the new successor of these blocks, which
798 has inherited from the original block, needs the cleanup.
799 Don't clear bits in the bitmap, as that can break the bitmap
800 iterator. */
801 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
803 basic_block bb = BASIC_BLOCK (i);
804 if (bb == NULL)
805 continue;
806 while (single_succ_p (bb)
807 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
808 bb = single_succ (bb);
809 if (bb == EXIT_BLOCK_PTR)
810 continue;
811 if ((unsigned) bb->index != i)
812 bitmap_set_bit (need_eh_cleanup, bb->index);
815 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
816 bitmap_clear (need_eh_cleanup);
819 statistics_counter_event (cfun, "Redundant expressions eliminated",
820 opt_stats.num_re);
821 statistics_counter_event (cfun, "Constants propagated",
822 opt_stats.num_const_prop);
823 statistics_counter_event (cfun, "Copies propagated",
824 opt_stats.num_copy_prop);
826 /* Debugging dumps. */
827 if (dump_file && (dump_flags & TDF_STATS))
828 dump_dominator_optimization_stats (dump_file);
830 loop_optimizer_finalize ();
832 /* Delete our main hashtable. */
833 htab_delete (avail_exprs);
835 /* And finalize the dominator walker. */
836 fini_walk_dominator_tree (&walk_data);
838 /* Free asserted bitmaps and stacks. */
839 BITMAP_FREE (need_eh_cleanup);
841 avail_exprs_stack.release ();
842 const_and_copies_stack.release ();
844 /* Free the value-handle array. */
845 threadedge_finalize_values ();
846 ssa_name_values.release ();
848 return 0;
851 static bool
852 gate_dominator (void)
854 return flag_tree_dom != 0;
857 struct gimple_opt_pass pass_dominator =
860 GIMPLE_PASS,
861 "dom", /* name */
862 OPTGROUP_NONE, /* optinfo_flags */
863 gate_dominator, /* gate */
864 tree_ssa_dominator_optimize, /* execute */
865 NULL, /* sub */
866 NULL, /* next */
867 0, /* static_pass_number */
868 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
869 PROP_cfg | PROP_ssa, /* properties_required */
870 0, /* properties_provided */
871 0, /* properties_destroyed */
872 0, /* todo_flags_start */
873 TODO_cleanup_cfg
874 | TODO_update_ssa
875 | TODO_verify_ssa
876 | TODO_verify_flow /* todo_flags_finish */
881 /* Given a conditional statement CONDSTMT, convert the
882 condition to a canonical form. */
884 static void
885 canonicalize_comparison (gimple condstmt)
887 tree op0;
888 tree op1;
889 enum tree_code code;
891 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
893 op0 = gimple_cond_lhs (condstmt);
894 op1 = gimple_cond_rhs (condstmt);
896 code = gimple_cond_code (condstmt);
898 /* If it would be profitable to swap the operands, then do so to
899 canonicalize the statement, enabling better optimization.
901 By placing canonicalization of such expressions here we
902 transparently keep statements in canonical form, even
903 when the statement is modified. */
904 if (tree_swap_operands_p (op0, op1, false))
906 /* For relationals we need to swap the operands
907 and change the code. */
908 if (code == LT_EXPR
909 || code == GT_EXPR
910 || code == LE_EXPR
911 || code == GE_EXPR)
913 code = swap_tree_comparison (code);
915 gimple_cond_set_code (condstmt, code);
916 gimple_cond_set_lhs (condstmt, op1);
917 gimple_cond_set_rhs (condstmt, op0);
919 update_stmt (condstmt);
924 /* Initialize local stacks for this optimizer and record equivalences
925 upon entry to BB. Equivalences can come from the edge traversed to
926 reach BB or they may come from PHI nodes at the start of BB. */
928 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
929 LIMIT entries left in LOCALs. */
931 static void
932 remove_local_expressions_from_table (void)
934 /* Remove all the expressions made available in this block. */
935 while (avail_exprs_stack.length () > 0)
937 expr_hash_elt_t victim = avail_exprs_stack.pop ();
938 void **slot;
940 if (victim == NULL)
941 break;
943 /* This must precede the actual removal from the hash table,
944 as ELEMENT and the table entry may share a call argument
945 vector which will be freed during removal. */
946 if (dump_file && (dump_flags & TDF_DETAILS))
948 fprintf (dump_file, "<<<< ");
949 print_expr_hash_elt (dump_file, victim);
952 slot = htab_find_slot_with_hash (avail_exprs,
953 victim, victim->hash, NO_INSERT);
954 gcc_assert (slot && *slot == (void *) victim);
955 htab_clear_slot (avail_exprs, slot);
959 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
960 CONST_AND_COPIES to its original state, stopping when we hit a
961 NULL marker. */
963 static void
964 restore_vars_to_original_value (void)
966 while (const_and_copies_stack.length () > 0)
968 tree prev_value, dest;
970 dest = const_and_copies_stack.pop ();
972 if (dest == NULL)
973 break;
975 if (dump_file && (dump_flags & TDF_DETAILS))
977 fprintf (dump_file, "<<<< COPY ");
978 print_generic_expr (dump_file, dest, 0);
979 fprintf (dump_file, " = ");
980 print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
981 fprintf (dump_file, "\n");
984 prev_value = const_and_copies_stack.pop ();
985 set_ssa_name_value (dest, prev_value);
989 /* A trivial wrapper so that we can present the generic jump
990 threading code with a simple API for simplifying statements. */
991 static tree
992 simplify_stmt_for_jump_threading (gimple stmt,
993 gimple within_stmt ATTRIBUTE_UNUSED)
995 return lookup_avail_expr (stmt, false);
998 /* Wrapper for common code to attempt to thread an edge. For example,
999 it handles lazily building the dummy condition and the bookkeeping
1000 when jump threading is successful. */
1002 static void
1003 dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
1005 if (! walk_data->global_data)
1007 gimple dummy_cond =
1008 gimple_build_cond (NE_EXPR,
1009 integer_zero_node, integer_zero_node,
1010 NULL, NULL);
1011 walk_data->global_data = dummy_cond;
1014 thread_across_edge ((gimple) walk_data->global_data, e, false,
1015 &const_and_copies_stack,
1016 simplify_stmt_for_jump_threading);
1019 /* PHI nodes can create equivalences too.
1021 Ignoring any alternatives which are the same as the result, if
1022 all the alternatives are equal, then the PHI node creates an
1023 equivalence. */
1025 static void
1026 record_equivalences_from_phis (basic_block bb)
1028 gimple_stmt_iterator gsi;
1030 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1032 gimple phi = gsi_stmt (gsi);
1034 tree lhs = gimple_phi_result (phi);
1035 tree rhs = NULL;
1036 size_t i;
1038 for (i = 0; i < gimple_phi_num_args (phi); i++)
1040 tree t = gimple_phi_arg_def (phi, i);
1042 /* Ignore alternatives which are the same as our LHS. Since
1043 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1044 can simply compare pointers. */
1045 if (lhs == t)
1046 continue;
1048 /* If we have not processed an alternative yet, then set
1049 RHS to this alternative. */
1050 if (rhs == NULL)
1051 rhs = t;
1052 /* If we have processed an alternative (stored in RHS), then
1053 see if it is equal to this one. If it isn't, then stop
1054 the search. */
1055 else if (! operand_equal_for_phi_arg_p (rhs, t))
1056 break;
1059 /* If we had no interesting alternatives, then all the RHS alternatives
1060 must have been the same as LHS. */
1061 if (!rhs)
1062 rhs = lhs;
1064 /* If we managed to iterate through each PHI alternative without
1065 breaking out of the loop, then we have a PHI which may create
1066 a useful equivalence. We do not need to record unwind data for
1067 this, since this is a true assignment and not an equivalence
1068 inferred from a comparison. All uses of this ssa name are dominated
1069 by this assignment, so unwinding just costs time and space. */
1070 if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs))
1071 set_ssa_name_value (lhs, rhs);
1075 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1076 return that edge. Otherwise return NULL. */
1077 static edge
1078 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1080 edge retval = NULL;
1081 edge e;
1082 edge_iterator ei;
1084 FOR_EACH_EDGE (e, ei, bb->preds)
1086 /* A loop back edge can be identified by the destination of
1087 the edge dominating the source of the edge. */
1088 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1089 continue;
1091 /* If we have already seen a non-loop edge, then we must have
1092 multiple incoming non-loop edges and thus we return NULL. */
1093 if (retval)
1094 return NULL;
1096 /* This is the first non-loop incoming edge we have found. Record
1097 it. */
1098 retval = e;
1101 return retval;
1104 /* Record any equivalences created by the incoming edge to BB. If BB
1105 has more than one incoming edge, then no equivalence is created. */
1107 static void
1108 record_equivalences_from_incoming_edge (basic_block bb)
1110 edge e;
1111 basic_block parent;
1112 struct edge_info *edge_info;
1114 /* If our parent block ended with a control statement, then we may be
1115 able to record some equivalences based on which outgoing edge from
1116 the parent was followed. */
1117 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1119 e = single_incoming_edge_ignoring_loop_edges (bb);
1121 /* If we had a single incoming edge from our parent block, then enter
1122 any data associated with the edge into our tables. */
1123 if (e && e->src == parent)
1125 unsigned int i;
1127 edge_info = (struct edge_info *) e->aux;
1129 if (edge_info)
1131 tree lhs = edge_info->lhs;
1132 tree rhs = edge_info->rhs;
1133 cond_equivalence *eq;
1135 if (lhs)
1136 record_equality (lhs, rhs);
1138 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1139 set via a widening type conversion, then we may be able to record
1140 additional equivalences. */
1141 if (lhs
1142 && TREE_CODE (lhs) == SSA_NAME
1143 && is_gimple_constant (rhs)
1144 && TREE_CODE (rhs) == INTEGER_CST)
1146 gimple defstmt = SSA_NAME_DEF_STMT (lhs);
1148 if (defstmt
1149 && is_gimple_assign (defstmt)
1150 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
1152 tree old_rhs = gimple_assign_rhs1 (defstmt);
1154 /* If the constant is in the range of the type of OLD_RHS,
1155 then convert the constant and record the equivalence. */
1156 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
1157 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
1159 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1160 record_equality (old_rhs, newval);
1165 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1166 record_cond (eq);
1171 /* Dump SSA statistics on FILE. */
1173 void
1174 dump_dominator_optimization_stats (FILE *file)
1176 fprintf (file, "Total number of statements: %6ld\n\n",
1177 opt_stats.num_stmts);
1178 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1179 opt_stats.num_exprs_considered);
1181 fprintf (file, "\nHash table statistics:\n");
1183 fprintf (file, " avail_exprs: ");
1184 htab_statistics (file, avail_exprs);
1188 /* Dump SSA statistics on stderr. */
1190 DEBUG_FUNCTION void
1191 debug_dominator_optimization_stats (void)
1193 dump_dominator_optimization_stats (stderr);
1197 /* Dump statistics for the hash table HTAB. */
1199 static void
1200 htab_statistics (FILE *file, htab_t htab)
1202 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1203 (long) htab_size (htab),
1204 (long) htab_elements (htab),
1205 htab_collisions (htab));
1209 /* Enter condition equivalence into the expression hash table.
1210 This indicates that a conditional expression has a known
1211 boolean value. */
1213 static void
1214 record_cond (cond_equivalence *p)
1216 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1217 void **slot;
1219 initialize_hash_element_from_expr (&p->cond, p->value, element);
1221 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1222 element->hash, INSERT);
1223 if (*slot == NULL)
1225 *slot = (void *) element;
1227 if (dump_file && (dump_flags & TDF_DETAILS))
1229 fprintf (dump_file, "1>>> ");
1230 print_expr_hash_elt (dump_file, element);
1233 avail_exprs_stack.safe_push (element);
1235 else
1236 free_expr_hash_elt (element);
1239 /* Build a cond_equivalence record indicating that the comparison
1240 CODE holds between operands OP0 and OP1 and push it to **P. */
1242 static void
1243 build_and_record_new_cond (enum tree_code code,
1244 tree op0, tree op1,
1245 vec<cond_equivalence> *p)
1247 cond_equivalence c;
1248 struct hashable_expr *cond = &c.cond;
1250 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1252 cond->type = boolean_type_node;
1253 cond->kind = EXPR_BINARY;
1254 cond->ops.binary.op = code;
1255 cond->ops.binary.opnd0 = op0;
1256 cond->ops.binary.opnd1 = op1;
1258 c.value = boolean_true_node;
1259 p->safe_push (c);
1262 /* Record that COND is true and INVERTED is false into the edge information
1263 structure. Also record that any conditions dominated by COND are true
1264 as well.
1266 For example, if a < b is true, then a <= b must also be true. */
1268 static void
1269 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1271 tree op0, op1;
1272 cond_equivalence c;
1274 if (!COMPARISON_CLASS_P (cond))
1275 return;
1277 op0 = TREE_OPERAND (cond, 0);
1278 op1 = TREE_OPERAND (cond, 1);
1280 switch (TREE_CODE (cond))
1282 case LT_EXPR:
1283 case GT_EXPR:
1284 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1286 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1287 &edge_info->cond_equivalences);
1288 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1289 &edge_info->cond_equivalences);
1292 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1293 ? LE_EXPR : GE_EXPR),
1294 op0, op1, &edge_info->cond_equivalences);
1295 build_and_record_new_cond (NE_EXPR, op0, op1,
1296 &edge_info->cond_equivalences);
1297 break;
1299 case GE_EXPR:
1300 case LE_EXPR:
1301 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1303 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1304 &edge_info->cond_equivalences);
1306 break;
1308 case EQ_EXPR:
1309 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1311 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1312 &edge_info->cond_equivalences);
1314 build_and_record_new_cond (LE_EXPR, op0, op1,
1315 &edge_info->cond_equivalences);
1316 build_and_record_new_cond (GE_EXPR, op0, op1,
1317 &edge_info->cond_equivalences);
1318 break;
1320 case UNORDERED_EXPR:
1321 build_and_record_new_cond (NE_EXPR, op0, op1,
1322 &edge_info->cond_equivalences);
1323 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1324 &edge_info->cond_equivalences);
1325 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1326 &edge_info->cond_equivalences);
1327 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1328 &edge_info->cond_equivalences);
1329 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1330 &edge_info->cond_equivalences);
1331 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1332 &edge_info->cond_equivalences);
1333 break;
1335 case UNLT_EXPR:
1336 case UNGT_EXPR:
1337 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1338 ? UNLE_EXPR : UNGE_EXPR),
1339 op0, op1, &edge_info->cond_equivalences);
1340 build_and_record_new_cond (NE_EXPR, op0, op1,
1341 &edge_info->cond_equivalences);
1342 break;
1344 case UNEQ_EXPR:
1345 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1346 &edge_info->cond_equivalences);
1347 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1348 &edge_info->cond_equivalences);
1349 break;
1351 case LTGT_EXPR:
1352 build_and_record_new_cond (NE_EXPR, op0, op1,
1353 &edge_info->cond_equivalences);
1354 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1355 &edge_info->cond_equivalences);
1356 break;
1358 default:
1359 break;
1362 /* Now store the original true and false conditions into the first
1363 two slots. */
1364 initialize_expr_from_cond (cond, &c.cond);
1365 c.value = boolean_true_node;
1366 edge_info->cond_equivalences.safe_push (c);
1368 /* It is possible for INVERTED to be the negation of a comparison,
1369 and not a valid RHS or GIMPLE_COND condition. This happens because
1370 invert_truthvalue may return such an expression when asked to invert
1371 a floating-point comparison. These comparisons are not assumed to
1372 obey the trichotomy law. */
1373 initialize_expr_from_cond (inverted, &c.cond);
1374 c.value = boolean_false_node;
1375 edge_info->cond_equivalences.safe_push (c);
1378 /* A helper function for record_const_or_copy and record_equality.
1379 Do the work of recording the value and undo info. */
1381 static void
1382 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1384 set_ssa_name_value (x, y);
1386 if (dump_file && (dump_flags & TDF_DETAILS))
1388 fprintf (dump_file, "0>>> COPY ");
1389 print_generic_expr (dump_file, x, 0);
1390 fprintf (dump_file, " = ");
1391 print_generic_expr (dump_file, y, 0);
1392 fprintf (dump_file, "\n");
1395 const_and_copies_stack.reserve (2);
1396 const_and_copies_stack.quick_push (prev_x);
1397 const_and_copies_stack.quick_push (x);
1400 /* Return the loop depth of the basic block of the defining statement of X.
1401 This number should not be treated as absolutely correct because the loop
1402 information may not be completely up-to-date when dom runs. However, it
1403 will be relatively correct, and as more passes are taught to keep loop info
1404 up to date, the result will become more and more accurate. */
1407 loop_depth_of_name (tree x)
1409 gimple defstmt;
1410 basic_block defbb;
1412 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1413 if (TREE_CODE (x) != SSA_NAME)
1414 return 0;
1416 /* Otherwise return the loop depth of the defining statement's bb.
1417 Note that there may not actually be a bb for this statement, if the
1418 ssa_name is live on entry. */
1419 defstmt = SSA_NAME_DEF_STMT (x);
1420 defbb = gimple_bb (defstmt);
1421 if (!defbb)
1422 return 0;
1424 return bb_loop_depth (defbb);
1427 /* Record that X is equal to Y in const_and_copies. Record undo
1428 information in the block-local vector. */
1430 static void
1431 record_const_or_copy (tree x, tree y)
1433 tree prev_x = SSA_NAME_VALUE (x);
1435 gcc_assert (TREE_CODE (x) == SSA_NAME);
1437 if (TREE_CODE (y) == SSA_NAME)
1439 tree tmp = SSA_NAME_VALUE (y);
1440 if (tmp)
1441 y = tmp;
1444 record_const_or_copy_1 (x, y, prev_x);
1447 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1448 This constrains the cases in which we may treat this as assignment. */
1450 static void
1451 record_equality (tree x, tree y)
1453 tree prev_x = NULL, prev_y = NULL;
1455 if (TREE_CODE (x) == SSA_NAME)
1456 prev_x = SSA_NAME_VALUE (x);
1457 if (TREE_CODE (y) == SSA_NAME)
1458 prev_y = SSA_NAME_VALUE (y);
1460 /* If one of the previous values is invariant, or invariant in more loops
1461 (by depth), then use that.
1462 Otherwise it doesn't matter which value we choose, just so
1463 long as we canonicalize on one value. */
1464 if (is_gimple_min_invariant (y))
1466 else if (is_gimple_min_invariant (x)
1467 || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1468 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1469 else if (prev_x && is_gimple_min_invariant (prev_x))
1470 x = y, y = prev_x, prev_x = prev_y;
1471 else if (prev_y)
1472 y = prev_y;
1474 /* After the swapping, we must have one SSA_NAME. */
1475 if (TREE_CODE (x) != SSA_NAME)
1476 return;
1478 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1479 variable compared against zero. If we're honoring signed zeros,
1480 then we cannot record this value unless we know that the value is
1481 nonzero. */
1482 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1483 && (TREE_CODE (y) != REAL_CST
1484 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1485 return;
1487 record_const_or_copy_1 (x, y, prev_x);
1490 /* Returns true when STMT is a simple iv increment. It detects the
1491 following situation:
1493 i_1 = phi (..., i_2)
1494 i_2 = i_1 +/- ... */
1496 bool
1497 simple_iv_increment_p (gimple stmt)
1499 enum tree_code code;
1500 tree lhs, preinc;
1501 gimple phi;
1502 size_t i;
1504 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1505 return false;
1507 lhs = gimple_assign_lhs (stmt);
1508 if (TREE_CODE (lhs) != SSA_NAME)
1509 return false;
1511 code = gimple_assign_rhs_code (stmt);
1512 if (code != PLUS_EXPR
1513 && code != MINUS_EXPR
1514 && code != POINTER_PLUS_EXPR)
1515 return false;
1517 preinc = gimple_assign_rhs1 (stmt);
1518 if (TREE_CODE (preinc) != SSA_NAME)
1519 return false;
1521 phi = SSA_NAME_DEF_STMT (preinc);
1522 if (gimple_code (phi) != GIMPLE_PHI)
1523 return false;
1525 for (i = 0; i < gimple_phi_num_args (phi); i++)
1526 if (gimple_phi_arg_def (phi, i) == lhs)
1527 return true;
1529 return false;
1532 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1533 known value for that SSA_NAME (or NULL if no value is known).
1535 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1536 successors of BB. */
1538 static void
1539 cprop_into_successor_phis (basic_block bb)
1541 edge e;
1542 edge_iterator ei;
1544 FOR_EACH_EDGE (e, ei, bb->succs)
1546 int indx;
1547 gimple_stmt_iterator gsi;
1549 /* If this is an abnormal edge, then we do not want to copy propagate
1550 into the PHI alternative associated with this edge. */
1551 if (e->flags & EDGE_ABNORMAL)
1552 continue;
1554 gsi = gsi_start_phis (e->dest);
1555 if (gsi_end_p (gsi))
1556 continue;
1558 indx = e->dest_idx;
1559 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1561 tree new_val;
1562 use_operand_p orig_p;
1563 tree orig_val;
1564 gimple phi = gsi_stmt (gsi);
1566 /* The alternative may be associated with a constant, so verify
1567 it is an SSA_NAME before doing anything with it. */
1568 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1569 orig_val = get_use_from_ptr (orig_p);
1570 if (TREE_CODE (orig_val) != SSA_NAME)
1571 continue;
1573 /* If we have *ORIG_P in our constant/copy table, then replace
1574 ORIG_P with its value in our constant/copy table. */
1575 new_val = SSA_NAME_VALUE (orig_val);
1576 if (new_val
1577 && new_val != orig_val
1578 && (TREE_CODE (new_val) == SSA_NAME
1579 || is_gimple_min_invariant (new_val))
1580 && may_propagate_copy (orig_val, new_val))
1581 propagate_value (orig_p, new_val);
1586 /* We have finished optimizing BB, record any information implied by
1587 taking a specific outgoing edge from BB. */
1589 static void
1590 record_edge_info (basic_block bb)
1592 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1593 struct edge_info *edge_info;
1595 if (! gsi_end_p (gsi))
1597 gimple stmt = gsi_stmt (gsi);
1598 location_t loc = gimple_location (stmt);
1600 if (gimple_code (stmt) == GIMPLE_SWITCH)
1602 tree index = gimple_switch_index (stmt);
1604 if (TREE_CODE (index) == SSA_NAME)
1606 int i;
1607 int n_labels = gimple_switch_num_labels (stmt);
1608 tree *info = XCNEWVEC (tree, last_basic_block);
1609 edge e;
1610 edge_iterator ei;
1612 for (i = 0; i < n_labels; i++)
1614 tree label = gimple_switch_label (stmt, i);
1615 basic_block target_bb = label_to_block (CASE_LABEL (label));
1616 if (CASE_HIGH (label)
1617 || !CASE_LOW (label)
1618 || info[target_bb->index])
1619 info[target_bb->index] = error_mark_node;
1620 else
1621 info[target_bb->index] = label;
1624 FOR_EACH_EDGE (e, ei, bb->succs)
1626 basic_block target_bb = e->dest;
1627 tree label = info[target_bb->index];
1629 if (label != NULL && label != error_mark_node)
1631 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1632 CASE_LOW (label));
1633 edge_info = allocate_edge_info (e);
1634 edge_info->lhs = index;
1635 edge_info->rhs = x;
1638 free (info);
1642 /* A COND_EXPR may create equivalences too. */
1643 if (gimple_code (stmt) == GIMPLE_COND)
1645 edge true_edge;
1646 edge false_edge;
1648 tree op0 = gimple_cond_lhs (stmt);
1649 tree op1 = gimple_cond_rhs (stmt);
1650 enum tree_code code = gimple_cond_code (stmt);
1652 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1654 /* Special case comparing booleans against a constant as we
1655 know the value of OP0 on both arms of the branch. i.e., we
1656 can record an equivalence for OP0 rather than COND. */
1657 if ((code == EQ_EXPR || code == NE_EXPR)
1658 && TREE_CODE (op0) == SSA_NAME
1659 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1660 && is_gimple_min_invariant (op1))
1662 if (code == EQ_EXPR)
1664 edge_info = allocate_edge_info (true_edge);
1665 edge_info->lhs = op0;
1666 edge_info->rhs = (integer_zerop (op1)
1667 ? boolean_false_node
1668 : boolean_true_node);
1670 edge_info = allocate_edge_info (false_edge);
1671 edge_info->lhs = op0;
1672 edge_info->rhs = (integer_zerop (op1)
1673 ? boolean_true_node
1674 : boolean_false_node);
1676 else
1678 edge_info = allocate_edge_info (true_edge);
1679 edge_info->lhs = op0;
1680 edge_info->rhs = (integer_zerop (op1)
1681 ? boolean_true_node
1682 : boolean_false_node);
1684 edge_info = allocate_edge_info (false_edge);
1685 edge_info->lhs = op0;
1686 edge_info->rhs = (integer_zerop (op1)
1687 ? boolean_false_node
1688 : boolean_true_node);
1691 else if (is_gimple_min_invariant (op0)
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 (op0)))
1699 && real_zerop (op0));
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 = op1;
1708 edge_info->rhs = op0;
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 = op1;
1717 edge_info->rhs = op0;
1721 else if (TREE_CODE (op0) == SSA_NAME
1722 && (TREE_CODE (op1) == SSA_NAME
1723 || is_gimple_min_invariant (op1)))
1725 tree cond = build2 (code, boolean_type_node, op0, op1);
1726 tree inverted = invert_truthvalue_loc (loc, cond);
1727 bool can_infer_simple_equiv
1728 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op1)))
1729 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1730 struct edge_info *edge_info;
1732 edge_info = allocate_edge_info (true_edge);
1733 record_conditions (edge_info, cond, inverted);
1735 if (can_infer_simple_equiv && code == EQ_EXPR)
1737 edge_info->lhs = op0;
1738 edge_info->rhs = op1;
1741 edge_info = allocate_edge_info (false_edge);
1742 record_conditions (edge_info, inverted, cond);
1744 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1746 edge_info->lhs = op0;
1747 edge_info->rhs = op1;
1752 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1756 static void
1757 dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1758 basic_block bb)
1760 gimple_stmt_iterator gsi;
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1763 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1765 /* Push a marker on the stacks of local information so that we know how
1766 far to unwind when we finalize this block. */
1767 avail_exprs_stack.safe_push (NULL);
1768 const_and_copies_stack.safe_push (NULL_TREE);
1770 record_equivalences_from_incoming_edge (bb);
1772 /* PHI nodes can create equivalences too. */
1773 record_equivalences_from_phis (bb);
1775 /* Create equivalences from redundant PHIs. PHIs are only truly
1776 redundant when they exist in the same block, so push another
1777 marker and unwind right afterwards. */
1778 avail_exprs_stack.safe_push (NULL);
1779 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1780 eliminate_redundant_computations (&gsi);
1781 remove_local_expressions_from_table ();
1783 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1784 optimize_stmt (bb, gsi);
1786 /* Now prepare to process dominated blocks. */
1787 record_edge_info (bb);
1788 cprop_into_successor_phis (bb);
1791 /* We have finished processing the dominator children of BB, perform
1792 any finalization actions in preparation for leaving this node in
1793 the dominator tree. */
1795 static void
1796 dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb)
1798 gimple last;
1800 /* If we have an outgoing edge to a block with multiple incoming and
1801 outgoing edges, then we may be able to thread the edge, i.e., we
1802 may be able to statically determine which of the outgoing edges
1803 will be traversed when the incoming edge from BB is traversed. */
1804 if (single_succ_p (bb)
1805 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1806 && potentially_threadable_block (single_succ (bb)))
1808 /* Push a marker on the stack, which thread_across_edge expects
1809 and will remove. */
1810 const_and_copies_stack.safe_push (NULL_TREE);
1811 dom_thread_across_edge (walk_data, single_succ_edge (bb));
1813 else if ((last = last_stmt (bb))
1814 && gimple_code (last) == GIMPLE_COND
1815 && EDGE_COUNT (bb->succs) == 2
1816 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1817 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1819 edge true_edge, false_edge;
1821 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1823 /* Only try to thread the edge if it reaches a target block with
1824 more than one predecessor and more than one successor. */
1825 if (potentially_threadable_block (true_edge->dest))
1827 struct edge_info *edge_info;
1828 unsigned int i;
1830 /* Push a marker onto the available expression stack so that we
1831 unwind any expressions related to the TRUE arm before processing
1832 the false arm below. */
1833 avail_exprs_stack.safe_push (NULL);
1834 const_and_copies_stack.safe_push (NULL_TREE);
1836 edge_info = (struct edge_info *) true_edge->aux;
1838 /* If we have info associated with this edge, record it into
1839 our equivalence tables. */
1840 if (edge_info)
1842 cond_equivalence *eq;
1843 tree lhs = edge_info->lhs;
1844 tree rhs = edge_info->rhs;
1846 /* If we have a simple NAME = VALUE equivalence, record it. */
1847 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1848 record_const_or_copy (lhs, rhs);
1850 /* If we have 0 = COND or 1 = COND equivalences, record them
1851 into our expression hash tables. */
1852 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1853 record_cond (eq);
1856 dom_thread_across_edge (walk_data, true_edge);
1858 /* And restore the various tables to their state before
1859 we threaded this edge. */
1860 remove_local_expressions_from_table ();
1863 /* Similarly for the ELSE arm. */
1864 if (potentially_threadable_block (false_edge->dest))
1866 struct edge_info *edge_info;
1867 unsigned int i;
1869 const_and_copies_stack.safe_push (NULL_TREE);
1870 edge_info = (struct edge_info *) false_edge->aux;
1872 /* If we have info associated with this edge, record it into
1873 our equivalence tables. */
1874 if (edge_info)
1876 cond_equivalence *eq;
1877 tree lhs = edge_info->lhs;
1878 tree rhs = edge_info->rhs;
1880 /* If we have a simple NAME = VALUE equivalence, record it. */
1881 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1882 record_const_or_copy (lhs, rhs);
1884 /* If we have 0 = COND or 1 = COND equivalences, record them
1885 into our expression hash tables. */
1886 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1887 record_cond (eq);
1890 /* Now thread the edge. */
1891 dom_thread_across_edge (walk_data, false_edge);
1893 /* No need to remove local expressions from our tables
1894 or restore vars to their original value as that will
1895 be done immediately below. */
1899 remove_local_expressions_from_table ();
1900 restore_vars_to_original_value ();
1903 /* Search for redundant computations in STMT. If any are found, then
1904 replace them with the variable holding the result of the computation.
1906 If safe, record this expression into the available expression hash
1907 table. */
1909 static void
1910 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
1912 tree expr_type;
1913 tree cached_lhs;
1914 tree def;
1915 bool insert = true;
1916 bool assigns_var_p = false;
1918 gimple stmt = gsi_stmt (*gsi);
1920 if (gimple_code (stmt) == GIMPLE_PHI)
1921 def = gimple_phi_result (stmt);
1922 else
1923 def = gimple_get_lhs (stmt);
1925 /* Certain expressions on the RHS can be optimized away, but can not
1926 themselves be entered into the hash tables. */
1927 if (! def
1928 || TREE_CODE (def) != SSA_NAME
1929 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1930 || gimple_vdef (stmt)
1931 /* Do not record equivalences for increments of ivs. This would create
1932 overlapping live ranges for a very questionable gain. */
1933 || simple_iv_increment_p (stmt))
1934 insert = false;
1936 /* Check if the expression has been computed before. */
1937 cached_lhs = lookup_avail_expr (stmt, insert);
1939 opt_stats.num_exprs_considered++;
1941 /* Get the type of the expression we are trying to optimize. */
1942 if (is_gimple_assign (stmt))
1944 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1945 assigns_var_p = true;
1947 else if (gimple_code (stmt) == GIMPLE_COND)
1948 expr_type = boolean_type_node;
1949 else if (is_gimple_call (stmt))
1951 gcc_assert (gimple_call_lhs (stmt));
1952 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1953 assigns_var_p = true;
1955 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1956 expr_type = TREE_TYPE (gimple_switch_index (stmt));
1957 else if (gimple_code (stmt) == GIMPLE_PHI)
1958 /* We can't propagate into a phi, so the logic below doesn't apply.
1959 Instead record an equivalence between the cached LHS and the
1960 PHI result of this statement, provided they are in the same block.
1961 This should be sufficient to kill the redundant phi. */
1963 if (def && cached_lhs)
1964 record_const_or_copy (def, cached_lhs);
1965 return;
1967 else
1968 gcc_unreachable ();
1970 if (!cached_lhs)
1971 return;
1973 /* It is safe to ignore types here since we have already done
1974 type checking in the hashing and equality routines. In fact
1975 type checking here merely gets in the way of constant
1976 propagation. Also, make sure that it is safe to propagate
1977 CACHED_LHS into the expression in STMT. */
1978 if ((TREE_CODE (cached_lhs) != SSA_NAME
1979 && (assigns_var_p
1980 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1981 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1983 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1984 || is_gimple_min_invariant (cached_lhs));
1986 if (dump_file && (dump_flags & TDF_DETAILS))
1988 fprintf (dump_file, " Replaced redundant expr '");
1989 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1990 fprintf (dump_file, "' with '");
1991 print_generic_expr (dump_file, cached_lhs, dump_flags);
1992 fprintf (dump_file, "'\n");
1995 opt_stats.num_re++;
1997 if (assigns_var_p
1998 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1999 cached_lhs = fold_convert (expr_type, cached_lhs);
2001 propagate_tree_value_into_stmt (gsi, cached_lhs);
2003 /* Since it is always necessary to mark the result as modified,
2004 perhaps we should move this into propagate_tree_value_into_stmt
2005 itself. */
2006 gimple_set_modified (gsi_stmt (*gsi), true);
2010 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2011 the available expressions table or the const_and_copies table.
2012 Detect and record those equivalences. */
2013 /* We handle only very simple copy equivalences here. The heavy
2014 lifing is done by eliminate_redundant_computations. */
2016 static void
2017 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
2019 tree lhs;
2020 enum tree_code lhs_code;
2022 gcc_assert (is_gimple_assign (stmt));
2024 lhs = gimple_assign_lhs (stmt);
2025 lhs_code = TREE_CODE (lhs);
2027 if (lhs_code == SSA_NAME
2028 && gimple_assign_single_p (stmt))
2030 tree rhs = gimple_assign_rhs1 (stmt);
2032 /* If the RHS of the assignment is a constant or another variable that
2033 may be propagated, register it in the CONST_AND_COPIES table. We
2034 do not need to record unwind data for this, since this is a true
2035 assignment and not an equivalence inferred from a comparison. All
2036 uses of this ssa name are dominated by this assignment, so unwinding
2037 just costs time and space. */
2038 if (may_optimize_p
2039 && (TREE_CODE (rhs) == SSA_NAME
2040 || is_gimple_min_invariant (rhs)))
2042 if (dump_file && (dump_flags & TDF_DETAILS))
2044 fprintf (dump_file, "==== ASGN ");
2045 print_generic_expr (dump_file, lhs, 0);
2046 fprintf (dump_file, " = ");
2047 print_generic_expr (dump_file, rhs, 0);
2048 fprintf (dump_file, "\n");
2051 set_ssa_name_value (lhs, rhs);
2055 /* A memory store, even an aliased store, creates a useful
2056 equivalence. By exchanging the LHS and RHS, creating suitable
2057 vops and recording the result in the available expression table,
2058 we may be able to expose more redundant loads. */
2059 if (!gimple_has_volatile_ops (stmt)
2060 && gimple_references_memory_p (stmt)
2061 && gimple_assign_single_p (stmt)
2062 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2063 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2064 && !is_gimple_reg (lhs))
2066 tree rhs = gimple_assign_rhs1 (stmt);
2067 gimple new_stmt;
2069 /* Build a new statement with the RHS and LHS exchanged. */
2070 if (TREE_CODE (rhs) == SSA_NAME)
2072 /* NOTE tuples. The call to gimple_build_assign below replaced
2073 a call to build_gimple_modify_stmt, which did not set the
2074 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2075 may cause an SSA validation failure, as the LHS may be a
2076 default-initialized name and should have no definition. I'm
2077 a bit dubious of this, as the artificial statement that we
2078 generate here may in fact be ill-formed, but it is simply
2079 used as an internal device in this pass, and never becomes
2080 part of the CFG. */
2081 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2082 new_stmt = gimple_build_assign (rhs, lhs);
2083 SSA_NAME_DEF_STMT (rhs) = defstmt;
2085 else
2086 new_stmt = gimple_build_assign (rhs, lhs);
2088 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2090 /* Finally enter the statement into the available expression
2091 table. */
2092 lookup_avail_expr (new_stmt, true);
2096 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2097 CONST_AND_COPIES. */
2099 static void
2100 cprop_operand (gimple stmt, use_operand_p op_p)
2102 tree val;
2103 tree op = USE_FROM_PTR (op_p);
2105 /* If the operand has a known constant value or it is known to be a
2106 copy of some other variable, use the value or copy stored in
2107 CONST_AND_COPIES. */
2108 val = SSA_NAME_VALUE (op);
2109 if (val && val != op)
2111 /* Do not replace hard register operands in asm statements. */
2112 if (gimple_code (stmt) == GIMPLE_ASM
2113 && !may_propagate_copy_into_asm (op))
2114 return;
2116 /* Certain operands are not allowed to be copy propagated due
2117 to their interaction with exception handling and some GCC
2118 extensions. */
2119 if (!may_propagate_copy (op, val))
2120 return;
2122 /* Do not propagate addresses that point to volatiles into memory
2123 stmts without volatile operands. */
2124 if (POINTER_TYPE_P (TREE_TYPE (val))
2125 && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
2126 && gimple_has_mem_ops (stmt)
2127 && !gimple_has_volatile_ops (stmt))
2128 return;
2130 /* Do not propagate copies if the propagated value is at a deeper loop
2131 depth than the propagatee. Otherwise, this may move loop variant
2132 variables outside of their loops and prevent coalescing
2133 opportunities. If the value was loop invariant, it will be hoisted
2134 by LICM and exposed for copy propagation. */
2135 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2136 return;
2138 /* Do not propagate copies into simple IV increment statements.
2139 See PR23821 for how this can disturb IV analysis. */
2140 if (TREE_CODE (val) != INTEGER_CST
2141 && simple_iv_increment_p (stmt))
2142 return;
2144 /* Dump details. */
2145 if (dump_file && (dump_flags & TDF_DETAILS))
2147 fprintf (dump_file, " Replaced '");
2148 print_generic_expr (dump_file, op, dump_flags);
2149 fprintf (dump_file, "' with %s '",
2150 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2151 print_generic_expr (dump_file, val, dump_flags);
2152 fprintf (dump_file, "'\n");
2155 if (TREE_CODE (val) != SSA_NAME)
2156 opt_stats.num_const_prop++;
2157 else
2158 opt_stats.num_copy_prop++;
2160 propagate_value (op_p, val);
2162 /* And note that we modified this statement. This is now
2163 safe, even if we changed virtual operands since we will
2164 rescan the statement and rewrite its operands again. */
2165 gimple_set_modified (stmt, true);
2169 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2170 known value for that SSA_NAME (or NULL if no value is known).
2172 Propagate values from CONST_AND_COPIES into the uses, vuses and
2173 vdef_ops of STMT. */
2175 static void
2176 cprop_into_stmt (gimple stmt)
2178 use_operand_p op_p;
2179 ssa_op_iter iter;
2181 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2182 cprop_operand (stmt, op_p);
2185 /* Optimize the statement pointed to by iterator SI.
2187 We try to perform some simplistic global redundancy elimination and
2188 constant propagation:
2190 1- To detect global redundancy, we keep track of expressions that have
2191 been computed in this block and its dominators. If we find that the
2192 same expression is computed more than once, we eliminate repeated
2193 computations by using the target of the first one.
2195 2- Constant values and copy assignments. This is used to do very
2196 simplistic constant and copy propagation. When a constant or copy
2197 assignment is found, we map the value on the RHS of the assignment to
2198 the variable in the LHS in the CONST_AND_COPIES table. */
2200 static void
2201 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2203 gimple stmt, old_stmt;
2204 bool may_optimize_p;
2205 bool modified_p = false;
2207 old_stmt = stmt = gsi_stmt (si);
2209 if (dump_file && (dump_flags & TDF_DETAILS))
2211 fprintf (dump_file, "Optimizing statement ");
2212 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2215 if (gimple_code (stmt) == GIMPLE_COND)
2216 canonicalize_comparison (stmt);
2218 update_stmt_if_modified (stmt);
2219 opt_stats.num_stmts++;
2221 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2222 cprop_into_stmt (stmt);
2224 /* If the statement has been modified with constant replacements,
2225 fold its RHS before checking for redundant computations. */
2226 if (gimple_modified_p (stmt))
2228 tree rhs = NULL;
2230 /* Try to fold the statement making sure that STMT is kept
2231 up to date. */
2232 if (fold_stmt (&si))
2234 stmt = gsi_stmt (si);
2235 gimple_set_modified (stmt, true);
2237 if (dump_file && (dump_flags & TDF_DETAILS))
2239 fprintf (dump_file, " Folded to: ");
2240 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2244 /* We only need to consider cases that can yield a gimple operand. */
2245 if (gimple_assign_single_p (stmt))
2246 rhs = gimple_assign_rhs1 (stmt);
2247 else if (gimple_code (stmt) == GIMPLE_GOTO)
2248 rhs = gimple_goto_dest (stmt);
2249 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2250 /* This should never be an ADDR_EXPR. */
2251 rhs = gimple_switch_index (stmt);
2253 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2254 recompute_tree_invariant_for_addr_expr (rhs);
2256 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2257 even if fold_stmt updated the stmt already and thus cleared
2258 gimple_modified_p flag on it. */
2259 modified_p = true;
2262 /* Check for redundant computations. Do this optimization only
2263 for assignments that have no volatile ops and conditionals. */
2264 may_optimize_p = (!gimple_has_side_effects (stmt)
2265 && (is_gimple_assign (stmt)
2266 || (is_gimple_call (stmt)
2267 && gimple_call_lhs (stmt) != NULL_TREE)
2268 || gimple_code (stmt) == GIMPLE_COND
2269 || gimple_code (stmt) == GIMPLE_SWITCH));
2271 if (may_optimize_p)
2273 if (gimple_code (stmt) == GIMPLE_CALL)
2275 /* Resolve __builtin_constant_p. If it hasn't been
2276 folded to integer_one_node by now, it's fairly
2277 certain that the value simply isn't constant. */
2278 tree callee = gimple_call_fndecl (stmt);
2279 if (callee
2280 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2281 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2283 propagate_tree_value_into_stmt (&si, integer_zero_node);
2284 stmt = gsi_stmt (si);
2288 update_stmt_if_modified (stmt);
2289 eliminate_redundant_computations (&si);
2290 stmt = gsi_stmt (si);
2292 /* Perform simple redundant store elimination. */
2293 if (gimple_assign_single_p (stmt)
2294 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2296 tree lhs = gimple_assign_lhs (stmt);
2297 tree rhs = gimple_assign_rhs1 (stmt);
2298 tree cached_lhs;
2299 gimple new_stmt;
2300 if (TREE_CODE (rhs) == SSA_NAME)
2302 tree tem = SSA_NAME_VALUE (rhs);
2303 if (tem)
2304 rhs = tem;
2306 /* Build a new statement with the RHS and LHS exchanged. */
2307 if (TREE_CODE (rhs) == SSA_NAME)
2309 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2310 new_stmt = gimple_build_assign (rhs, lhs);
2311 SSA_NAME_DEF_STMT (rhs) = defstmt;
2313 else
2314 new_stmt = gimple_build_assign (rhs, lhs);
2315 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2316 cached_lhs = lookup_avail_expr (new_stmt, false);
2317 if (cached_lhs
2318 && rhs == cached_lhs)
2320 basic_block bb = gimple_bb (stmt);
2321 unlink_stmt_vdef (stmt);
2322 if (gsi_remove (&si, true))
2324 bitmap_set_bit (need_eh_cleanup, bb->index);
2325 if (dump_file && (dump_flags & TDF_DETAILS))
2326 fprintf (dump_file, " Flagged to clear EH edges.\n");
2328 release_defs (stmt);
2329 return;
2334 /* Record any additional equivalences created by this statement. */
2335 if (is_gimple_assign (stmt))
2336 record_equivalences_from_stmt (stmt, may_optimize_p);
2338 /* If STMT is a COND_EXPR and it was modified, then we may know
2339 where it goes. If that is the case, then mark the CFG as altered.
2341 This will cause us to later call remove_unreachable_blocks and
2342 cleanup_tree_cfg when it is safe to do so. It is not safe to
2343 clean things up here since removal of edges and such can trigger
2344 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2345 the manager.
2347 That's all fine and good, except that once SSA_NAMEs are released
2348 to the manager, we must not call create_ssa_name until all references
2349 to released SSA_NAMEs have been eliminated.
2351 All references to the deleted SSA_NAMEs can not be eliminated until
2352 we remove unreachable blocks.
2354 We can not remove unreachable blocks until after we have completed
2355 any queued jump threading.
2357 We can not complete any queued jump threads until we have taken
2358 appropriate variables out of SSA form. Taking variables out of
2359 SSA form can call create_ssa_name and thus we lose.
2361 Ultimately I suspect we're going to need to change the interface
2362 into the SSA_NAME manager. */
2363 if (gimple_modified_p (stmt) || modified_p)
2365 tree val = NULL;
2367 update_stmt_if_modified (stmt);
2369 if (gimple_code (stmt) == GIMPLE_COND)
2370 val = fold_binary_loc (gimple_location (stmt),
2371 gimple_cond_code (stmt), boolean_type_node,
2372 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2373 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2374 val = gimple_switch_index (stmt);
2376 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2377 cfg_altered = true;
2379 /* If we simplified a statement in such a way as to be shown that it
2380 cannot trap, update the eh information and the cfg to match. */
2381 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2383 bitmap_set_bit (need_eh_cleanup, bb->index);
2384 if (dump_file && (dump_flags & TDF_DETAILS))
2385 fprintf (dump_file, " Flagged to clear EH edges.\n");
2390 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2391 If found, return its LHS. Otherwise insert STMT in the table and
2392 return NULL_TREE.
2394 Also, when an expression is first inserted in the table, it is also
2395 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2396 we finish processing this block and its children. */
2398 static tree
2399 lookup_avail_expr (gimple stmt, bool insert)
2401 void **slot;
2402 tree lhs;
2403 tree temp;
2404 struct expr_hash_elt element;
2406 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2407 if (gimple_code (stmt) == GIMPLE_PHI)
2408 lhs = gimple_phi_result (stmt);
2409 else
2410 lhs = gimple_get_lhs (stmt);
2412 initialize_hash_element (stmt, lhs, &element);
2414 if (dump_file && (dump_flags & TDF_DETAILS))
2416 fprintf (dump_file, "LKUP ");
2417 print_expr_hash_elt (dump_file, &element);
2420 /* Don't bother remembering constant assignments and copy operations.
2421 Constants and copy operations are handled by the constant/copy propagator
2422 in optimize_stmt. */
2423 if (element.expr.kind == EXPR_SINGLE
2424 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2425 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2426 return NULL_TREE;
2428 /* Finally try to find the expression in the main expression hash table. */
2429 slot = htab_find_slot_with_hash (avail_exprs, &element, element.hash,
2430 (insert ? INSERT : NO_INSERT));
2431 if (slot == NULL)
2433 free_expr_hash_elt_contents (&element);
2434 return NULL_TREE;
2436 else if (*slot == NULL)
2438 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2439 *element2 = element;
2440 element2->stamp = element2;
2441 *slot = (void *) element2;
2443 if (dump_file && (dump_flags & TDF_DETAILS))
2445 fprintf (dump_file, "2>>> ");
2446 print_expr_hash_elt (dump_file, element2);
2449 avail_exprs_stack.safe_push (element2);
2450 return NULL_TREE;
2452 else
2453 free_expr_hash_elt_contents (&element);
2455 /* Extract the LHS of the assignment so that it can be used as the current
2456 definition of another variable. */
2457 lhs = ((struct expr_hash_elt *)*slot)->lhs;
2459 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2460 use the value from the const_and_copies table. */
2461 if (TREE_CODE (lhs) == SSA_NAME)
2463 temp = SSA_NAME_VALUE (lhs);
2464 if (temp)
2465 lhs = temp;
2468 if (dump_file && (dump_flags & TDF_DETAILS))
2470 fprintf (dump_file, "FIND: ");
2471 print_generic_expr (dump_file, lhs, 0);
2472 fprintf (dump_file, "\n");
2475 return lhs;
2478 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2479 for expressions using the code of the expression and the SSA numbers of
2480 its operands. */
2482 static hashval_t
2483 avail_expr_hash (const void *p)
2485 gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
2486 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2487 tree vuse;
2488 hashval_t val = 0;
2490 val = iterative_hash_hashable_expr (expr, val);
2492 /* If the hash table entry is not associated with a statement, then we
2493 can just hash the expression and not worry about virtual operands
2494 and such. */
2495 if (!stmt)
2496 return val;
2498 /* Add the SSA version numbers of the vuse operand. This is important
2499 because compound variables like arrays are not renamed in the
2500 operands. Rather, the rename is done on the virtual variable
2501 representing all the elements of the array. */
2502 if ((vuse = gimple_vuse (stmt)))
2503 val = iterative_hash_expr (vuse, val);
2505 return val;
2508 static hashval_t
2509 real_avail_expr_hash (const void *p)
2511 return ((const struct expr_hash_elt *)p)->hash;
2514 static int
2515 avail_expr_eq (const void *p1, const void *p2)
2517 gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt;
2518 const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr;
2519 const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp;
2520 gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt;
2521 const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr;
2522 const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp;
2524 /* This case should apply only when removing entries from the table. */
2525 if (stamp1 == stamp2)
2526 return true;
2528 /* FIXME tuples:
2529 We add stmts to a hash table and them modify them. To detect the case
2530 that we modify a stmt and then search for it, we assume that the hash
2531 is always modified by that change.
2532 We have to fully check why this doesn't happen on trunk or rewrite
2533 this in a more reliable (and easier to understand) way. */
2534 if (((const struct expr_hash_elt *)p1)->hash
2535 != ((const struct expr_hash_elt *)p2)->hash)
2536 return false;
2538 /* In case of a collision, both RHS have to be identical and have the
2539 same VUSE operands. */
2540 if (hashable_expr_equal_p (expr1, expr2)
2541 && types_compatible_p (expr1->type, expr2->type))
2543 /* Note that STMT1 and/or STMT2 may be NULL. */
2544 return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
2545 == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
2548 return false;
2551 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2552 up degenerate PHIs created by or exposed by jump threading. */
2554 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2555 NULL. */
2557 tree
2558 degenerate_phi_result (gimple phi)
2560 tree lhs = gimple_phi_result (phi);
2561 tree val = NULL;
2562 size_t i;
2564 /* Ignoring arguments which are the same as LHS, if all the remaining
2565 arguments are the same, then the PHI is a degenerate and has the
2566 value of that common argument. */
2567 for (i = 0; i < gimple_phi_num_args (phi); i++)
2569 tree arg = gimple_phi_arg_def (phi, i);
2571 if (arg == lhs)
2572 continue;
2573 else if (!arg)
2574 break;
2575 else if (!val)
2576 val = arg;
2577 else if (arg == val)
2578 continue;
2579 /* We bring in some of operand_equal_p not only to speed things
2580 up, but also to avoid crashing when dereferencing the type of
2581 a released SSA name. */
2582 else if (TREE_CODE (val) != TREE_CODE (arg)
2583 || TREE_CODE (val) == SSA_NAME
2584 || !operand_equal_p (arg, val, 0))
2585 break;
2587 return (i == gimple_phi_num_args (phi) ? val : NULL);
2590 /* Given a statement STMT, which is either a PHI node or an assignment,
2591 remove it from the IL. */
2593 static void
2594 remove_stmt_or_phi (gimple stmt)
2596 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2598 if (gimple_code (stmt) == GIMPLE_PHI)
2599 remove_phi_node (&gsi, true);
2600 else
2602 gsi_remove (&gsi, true);
2603 release_defs (stmt);
2607 /* Given a statement STMT, which is either a PHI node or an assignment,
2608 return the "rhs" of the node, in the case of a non-degenerate
2609 phi, NULL is returned. */
2611 static tree
2612 get_rhs_or_phi_arg (gimple stmt)
2614 if (gimple_code (stmt) == GIMPLE_PHI)
2615 return degenerate_phi_result (stmt);
2616 else if (gimple_assign_single_p (stmt))
2617 return gimple_assign_rhs1 (stmt);
2618 else
2619 gcc_unreachable ();
2623 /* Given a statement STMT, which is either a PHI node or an assignment,
2624 return the "lhs" of the node. */
2626 static tree
2627 get_lhs_or_phi_result (gimple stmt)
2629 if (gimple_code (stmt) == GIMPLE_PHI)
2630 return gimple_phi_result (stmt);
2631 else if (is_gimple_assign (stmt))
2632 return gimple_assign_lhs (stmt);
2633 else
2634 gcc_unreachable ();
2637 /* Propagate RHS into all uses of LHS (when possible).
2639 RHS and LHS are derived from STMT, which is passed in solely so
2640 that we can remove it if propagation is successful.
2642 When propagating into a PHI node or into a statement which turns
2643 into a trivial copy or constant initialization, set the
2644 appropriate bit in INTERESTING_NAMEs so that we will visit those
2645 nodes as well in an effort to pick up secondary optimization
2646 opportunities. */
2648 static void
2649 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2651 /* First verify that propagation is valid and isn't going to move a
2652 loop variant variable outside its loop. */
2653 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2654 && (TREE_CODE (rhs) != SSA_NAME
2655 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2656 && may_propagate_copy (lhs, rhs)
2657 && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2659 use_operand_p use_p;
2660 imm_use_iterator iter;
2661 gimple use_stmt;
2662 bool all = true;
2664 /* Dump details. */
2665 if (dump_file && (dump_flags & TDF_DETAILS))
2667 fprintf (dump_file, " Replacing '");
2668 print_generic_expr (dump_file, lhs, dump_flags);
2669 fprintf (dump_file, "' with %s '",
2670 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2671 print_generic_expr (dump_file, rhs, dump_flags);
2672 fprintf (dump_file, "'\n");
2675 /* Walk over every use of LHS and try to replace the use with RHS.
2676 At this point the only reason why such a propagation would not
2677 be successful would be if the use occurs in an ASM_EXPR. */
2678 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2680 /* Leave debug stmts alone. If we succeed in propagating
2681 all non-debug uses, we'll drop the DEF, and propagation
2682 into debug stmts will occur then. */
2683 if (gimple_debug_bind_p (use_stmt))
2684 continue;
2686 /* It's not always safe to propagate into an ASM_EXPR. */
2687 if (gimple_code (use_stmt) == GIMPLE_ASM
2688 && ! may_propagate_copy_into_asm (lhs))
2690 all = false;
2691 continue;
2694 /* It's not ok to propagate into the definition stmt of RHS.
2695 <bb 9>:
2696 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2697 g_67.1_6 = prephitmp.12_36;
2698 goto <bb 9>;
2699 While this is strictly all dead code we do not want to
2700 deal with this here. */
2701 if (TREE_CODE (rhs) == SSA_NAME
2702 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2704 all = false;
2705 continue;
2708 /* Dump details. */
2709 if (dump_file && (dump_flags & TDF_DETAILS))
2711 fprintf (dump_file, " Original statement:");
2712 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2715 /* Propagate the RHS into this use of the LHS. */
2716 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2717 propagate_value (use_p, rhs);
2719 /* Special cases to avoid useless calls into the folding
2720 routines, operand scanning, etc.
2722 Propagation into a PHI may cause the PHI to become
2723 a degenerate, so mark the PHI as interesting. No other
2724 actions are necessary. */
2725 if (gimple_code (use_stmt) == GIMPLE_PHI)
2727 tree result;
2729 /* Dump details. */
2730 if (dump_file && (dump_flags & TDF_DETAILS))
2732 fprintf (dump_file, " Updated statement:");
2733 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2736 result = get_lhs_or_phi_result (use_stmt);
2737 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2738 continue;
2741 /* From this point onward we are propagating into a
2742 real statement. Folding may (or may not) be possible,
2743 we may expose new operands, expose dead EH edges,
2744 etc. */
2745 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2746 cannot fold a call that simplifies to a constant,
2747 because the GIMPLE_CALL must be replaced by a
2748 GIMPLE_ASSIGN, and there is no way to effect such a
2749 transformation in-place. We might want to consider
2750 using the more general fold_stmt here. */
2752 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2753 fold_stmt_inplace (&gsi);
2756 /* Sometimes propagation can expose new operands to the
2757 renamer. */
2758 update_stmt (use_stmt);
2760 /* Dump details. */
2761 if (dump_file && (dump_flags & TDF_DETAILS))
2763 fprintf (dump_file, " Updated statement:");
2764 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2767 /* If we replaced a variable index with a constant, then
2768 we would need to update the invariant flag for ADDR_EXPRs. */
2769 if (gimple_assign_single_p (use_stmt)
2770 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2771 recompute_tree_invariant_for_addr_expr
2772 (gimple_assign_rhs1 (use_stmt));
2774 /* If we cleaned up EH information from the statement,
2775 mark its containing block as needing EH cleanups. */
2776 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2778 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2779 if (dump_file && (dump_flags & TDF_DETAILS))
2780 fprintf (dump_file, " Flagged to clear EH edges.\n");
2783 /* Propagation may expose new trivial copy/constant propagation
2784 opportunities. */
2785 if (gimple_assign_single_p (use_stmt)
2786 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2787 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2788 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2790 tree result = get_lhs_or_phi_result (use_stmt);
2791 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2794 /* Propagation into these nodes may make certain edges in
2795 the CFG unexecutable. We want to identify them as PHI nodes
2796 at the destination of those unexecutable edges may become
2797 degenerates. */
2798 else if (gimple_code (use_stmt) == GIMPLE_COND
2799 || gimple_code (use_stmt) == GIMPLE_SWITCH
2800 || gimple_code (use_stmt) == GIMPLE_GOTO)
2802 tree val;
2804 if (gimple_code (use_stmt) == GIMPLE_COND)
2805 val = fold_binary_loc (gimple_location (use_stmt),
2806 gimple_cond_code (use_stmt),
2807 boolean_type_node,
2808 gimple_cond_lhs (use_stmt),
2809 gimple_cond_rhs (use_stmt));
2810 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2811 val = gimple_switch_index (use_stmt);
2812 else
2813 val = gimple_goto_dest (use_stmt);
2815 if (val && is_gimple_min_invariant (val))
2817 basic_block bb = gimple_bb (use_stmt);
2818 edge te = find_taken_edge (bb, val);
2819 edge_iterator ei;
2820 edge e;
2821 gimple_stmt_iterator gsi, psi;
2823 /* Remove all outgoing edges except TE. */
2824 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2826 if (e != te)
2828 /* Mark all the PHI nodes at the destination of
2829 the unexecutable edge as interesting. */
2830 for (psi = gsi_start_phis (e->dest);
2831 !gsi_end_p (psi);
2832 gsi_next (&psi))
2834 gimple phi = gsi_stmt (psi);
2836 tree result = gimple_phi_result (phi);
2837 int version = SSA_NAME_VERSION (result);
2839 bitmap_set_bit (interesting_names, version);
2842 te->probability += e->probability;
2844 te->count += e->count;
2845 remove_edge (e);
2846 cfg_altered = true;
2848 else
2849 ei_next (&ei);
2852 gsi = gsi_last_bb (gimple_bb (use_stmt));
2853 gsi_remove (&gsi, true);
2855 /* And fixup the flags on the single remaining edge. */
2856 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2857 te->flags &= ~EDGE_ABNORMAL;
2858 te->flags |= EDGE_FALLTHRU;
2859 if (te->probability > REG_BR_PROB_BASE)
2860 te->probability = REG_BR_PROB_BASE;
2865 /* Ensure there is nothing else to do. */
2866 gcc_assert (!all || has_zero_uses (lhs));
2868 /* If we were able to propagate away all uses of LHS, then
2869 we can remove STMT. */
2870 if (all)
2871 remove_stmt_or_phi (stmt);
2875 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2876 a statement that is a trivial copy or constant initialization.
2878 Attempt to eliminate T by propagating its RHS into all uses of
2879 its LHS. This may in turn set new bits in INTERESTING_NAMES
2880 for nodes we want to revisit later.
2882 All exit paths should clear INTERESTING_NAMES for the result
2883 of STMT. */
2885 static void
2886 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2888 tree lhs = get_lhs_or_phi_result (stmt);
2889 tree rhs;
2890 int version = SSA_NAME_VERSION (lhs);
2892 /* If the LHS of this statement or PHI has no uses, then we can
2893 just eliminate it. This can occur if, for example, the PHI
2894 was created by block duplication due to threading and its only
2895 use was in the conditional at the end of the block which was
2896 deleted. */
2897 if (has_zero_uses (lhs))
2899 bitmap_clear_bit (interesting_names, version);
2900 remove_stmt_or_phi (stmt);
2901 return;
2904 /* Get the RHS of the assignment or PHI node if the PHI is a
2905 degenerate. */
2906 rhs = get_rhs_or_phi_arg (stmt);
2907 if (!rhs)
2909 bitmap_clear_bit (interesting_names, version);
2910 return;
2913 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
2915 /* Note that STMT may well have been deleted by now, so do
2916 not access it, instead use the saved version # to clear
2917 T's entry in the worklist. */
2918 bitmap_clear_bit (interesting_names, version);
2921 /* The first phase in degenerate PHI elimination.
2923 Eliminate the degenerate PHIs in BB, then recurse on the
2924 dominator children of BB. */
2926 static void
2927 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2929 gimple_stmt_iterator gsi;
2930 basic_block son;
2932 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2934 gimple phi = gsi_stmt (gsi);
2936 eliminate_const_or_copy (phi, interesting_names);
2939 /* Recurse into the dominator children of BB. */
2940 for (son = first_dom_son (CDI_DOMINATORS, bb);
2941 son;
2942 son = next_dom_son (CDI_DOMINATORS, son))
2943 eliminate_degenerate_phis_1 (son, interesting_names);
2947 /* A very simple pass to eliminate degenerate PHI nodes from the
2948 IL. This is meant to be fast enough to be able to be run several
2949 times in the optimization pipeline.
2951 Certain optimizations, particularly those which duplicate blocks
2952 or remove edges from the CFG can create or expose PHIs which are
2953 trivial copies or constant initializations.
2955 While we could pick up these optimizations in DOM or with the
2956 combination of copy-prop and CCP, those solutions are far too
2957 heavy-weight for our needs.
2959 This implementation has two phases so that we can efficiently
2960 eliminate the first order degenerate PHIs and second order
2961 degenerate PHIs.
2963 The first phase performs a dominator walk to identify and eliminate
2964 the vast majority of the degenerate PHIs. When a degenerate PHI
2965 is identified and eliminated any affected statements or PHIs
2966 are put on a worklist.
2968 The second phase eliminates degenerate PHIs and trivial copies
2969 or constant initializations using the worklist. This is how we
2970 pick up the secondary optimization opportunities with minimal
2971 cost. */
2973 static unsigned int
2974 eliminate_degenerate_phis (void)
2976 bitmap interesting_names;
2977 bitmap interesting_names1;
2979 /* Bitmap of blocks which need EH information updated. We can not
2980 update it on-the-fly as doing so invalidates the dominator tree. */
2981 need_eh_cleanup = BITMAP_ALLOC (NULL);
2983 /* INTERESTING_NAMES is effectively our worklist, indexed by
2984 SSA_NAME_VERSION.
2986 A set bit indicates that the statement or PHI node which
2987 defines the SSA_NAME should be (re)examined to determine if
2988 it has become a degenerate PHI or trivial const/copy propagation
2989 opportunity.
2991 Experiments have show we generally get better compilation
2992 time behavior with bitmaps rather than sbitmaps. */
2993 interesting_names = BITMAP_ALLOC (NULL);
2994 interesting_names1 = BITMAP_ALLOC (NULL);
2996 calculate_dominance_info (CDI_DOMINATORS);
2997 cfg_altered = false;
2999 /* First phase. Eliminate degenerate PHIs via a dominator
3000 walk of the CFG.
3002 Experiments have indicated that we generally get better
3003 compile-time behavior by visiting blocks in the first
3004 phase in dominator order. Presumably this is because walking
3005 in dominator order leaves fewer PHIs for later examination
3006 by the worklist phase. */
3007 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
3009 /* Second phase. Eliminate second order degenerate PHIs as well
3010 as trivial copies or constant initializations identified by
3011 the first phase or this phase. Basically we keep iterating
3012 until our set of INTERESTING_NAMEs is empty. */
3013 while (!bitmap_empty_p (interesting_names))
3015 unsigned int i;
3016 bitmap_iterator bi;
3018 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3019 changed during the loop. Copy it to another bitmap and
3020 use that. */
3021 bitmap_copy (interesting_names1, interesting_names);
3023 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3025 tree name = ssa_name (i);
3027 /* Ignore SSA_NAMEs that have been released because
3028 their defining statement was deleted (unreachable). */
3029 if (name)
3030 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3031 interesting_names);
3035 if (cfg_altered)
3037 free_dominance_info (CDI_DOMINATORS);
3038 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3039 if (current_loops)
3040 loops_state_set (LOOPS_NEED_FIXUP);
3043 /* Propagation of const and copies may make some EH edges dead. Purge
3044 such edges from the CFG as needed. */
3045 if (!bitmap_empty_p (need_eh_cleanup))
3047 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3048 BITMAP_FREE (need_eh_cleanup);
3051 BITMAP_FREE (interesting_names);
3052 BITMAP_FREE (interesting_names1);
3053 return 0;
3056 struct gimple_opt_pass pass_phi_only_cprop =
3059 GIMPLE_PASS,
3060 "phicprop", /* name */
3061 OPTGROUP_NONE, /* optinfo_flags */
3062 gate_dominator, /* gate */
3063 eliminate_degenerate_phis, /* execute */
3064 NULL, /* sub */
3065 NULL, /* next */
3066 0, /* static_pass_number */
3067 TV_TREE_PHI_CPROP, /* tv_id */
3068 PROP_cfg | PROP_ssa, /* properties_required */
3069 0, /* properties_provided */
3070 0, /* properties_destroyed */
3071 0, /* todo_flags_start */
3072 TODO_cleanup_cfg
3073 | TODO_ggc_collect
3074 | TODO_verify_ssa
3075 | TODO_verify_stmts
3076 | TODO_update_ssa /* todo_flags_finish */