* config/epiphany/epiphany.md (GPR_1): New constant.
[official-gcc.git] / gcc / tree-ssa-dom.c
blobd98a646aa4ffbc9d69a5baa393644798a722a9c5
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 conversion widens the original value and
1155 the constant is in the range of the type of OLD_RHS,
1156 then convert the constant and record the equivalence.
1158 Note that int_fits_type_p does not check the precision
1159 if the upper and lower bounds are OK. */
1160 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
1161 && (TYPE_PRECISION (TREE_TYPE (lhs))
1162 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
1163 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
1165 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1166 record_equality (old_rhs, newval);
1171 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1172 record_cond (eq);
1177 /* Dump SSA statistics on FILE. */
1179 void
1180 dump_dominator_optimization_stats (FILE *file)
1182 fprintf (file, "Total number of statements: %6ld\n\n",
1183 opt_stats.num_stmts);
1184 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1185 opt_stats.num_exprs_considered);
1187 fprintf (file, "\nHash table statistics:\n");
1189 fprintf (file, " avail_exprs: ");
1190 htab_statistics (file, avail_exprs);
1194 /* Dump SSA statistics on stderr. */
1196 DEBUG_FUNCTION void
1197 debug_dominator_optimization_stats (void)
1199 dump_dominator_optimization_stats (stderr);
1203 /* Dump statistics for the hash table HTAB. */
1205 static void
1206 htab_statistics (FILE *file, htab_t htab)
1208 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1209 (long) htab_size (htab),
1210 (long) htab_elements (htab),
1211 htab_collisions (htab));
1215 /* Enter condition equivalence into the expression hash table.
1216 This indicates that a conditional expression has a known
1217 boolean value. */
1219 static void
1220 record_cond (cond_equivalence *p)
1222 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1223 void **slot;
1225 initialize_hash_element_from_expr (&p->cond, p->value, element);
1227 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1228 element->hash, INSERT);
1229 if (*slot == NULL)
1231 *slot = (void *) element;
1233 if (dump_file && (dump_flags & TDF_DETAILS))
1235 fprintf (dump_file, "1>>> ");
1236 print_expr_hash_elt (dump_file, element);
1239 avail_exprs_stack.safe_push (element);
1241 else
1242 free_expr_hash_elt (element);
1245 /* Build a cond_equivalence record indicating that the comparison
1246 CODE holds between operands OP0 and OP1 and push it to **P. */
1248 static void
1249 build_and_record_new_cond (enum tree_code code,
1250 tree op0, tree op1,
1251 vec<cond_equivalence> *p)
1253 cond_equivalence c;
1254 struct hashable_expr *cond = &c.cond;
1256 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1258 cond->type = boolean_type_node;
1259 cond->kind = EXPR_BINARY;
1260 cond->ops.binary.op = code;
1261 cond->ops.binary.opnd0 = op0;
1262 cond->ops.binary.opnd1 = op1;
1264 c.value = boolean_true_node;
1265 p->safe_push (c);
1268 /* Record that COND is true and INVERTED is false into the edge information
1269 structure. Also record that any conditions dominated by COND are true
1270 as well.
1272 For example, if a < b is true, then a <= b must also be true. */
1274 static void
1275 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1277 tree op0, op1;
1278 cond_equivalence c;
1280 if (!COMPARISON_CLASS_P (cond))
1281 return;
1283 op0 = TREE_OPERAND (cond, 0);
1284 op1 = TREE_OPERAND (cond, 1);
1286 switch (TREE_CODE (cond))
1288 case LT_EXPR:
1289 case GT_EXPR:
1290 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1292 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1293 &edge_info->cond_equivalences);
1294 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1295 &edge_info->cond_equivalences);
1298 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1299 ? LE_EXPR : GE_EXPR),
1300 op0, op1, &edge_info->cond_equivalences);
1301 build_and_record_new_cond (NE_EXPR, op0, op1,
1302 &edge_info->cond_equivalences);
1303 break;
1305 case GE_EXPR:
1306 case LE_EXPR:
1307 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1309 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1310 &edge_info->cond_equivalences);
1312 break;
1314 case EQ_EXPR:
1315 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1317 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1318 &edge_info->cond_equivalences);
1320 build_and_record_new_cond (LE_EXPR, op0, op1,
1321 &edge_info->cond_equivalences);
1322 build_and_record_new_cond (GE_EXPR, op0, op1,
1323 &edge_info->cond_equivalences);
1324 break;
1326 case UNORDERED_EXPR:
1327 build_and_record_new_cond (NE_EXPR, op0, op1,
1328 &edge_info->cond_equivalences);
1329 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1330 &edge_info->cond_equivalences);
1331 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1332 &edge_info->cond_equivalences);
1333 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1334 &edge_info->cond_equivalences);
1335 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1336 &edge_info->cond_equivalences);
1337 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1338 &edge_info->cond_equivalences);
1339 break;
1341 case UNLT_EXPR:
1342 case UNGT_EXPR:
1343 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1344 ? UNLE_EXPR : UNGE_EXPR),
1345 op0, op1, &edge_info->cond_equivalences);
1346 build_and_record_new_cond (NE_EXPR, op0, op1,
1347 &edge_info->cond_equivalences);
1348 break;
1350 case UNEQ_EXPR:
1351 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1352 &edge_info->cond_equivalences);
1353 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1354 &edge_info->cond_equivalences);
1355 break;
1357 case LTGT_EXPR:
1358 build_and_record_new_cond (NE_EXPR, op0, op1,
1359 &edge_info->cond_equivalences);
1360 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1361 &edge_info->cond_equivalences);
1362 break;
1364 default:
1365 break;
1368 /* Now store the original true and false conditions into the first
1369 two slots. */
1370 initialize_expr_from_cond (cond, &c.cond);
1371 c.value = boolean_true_node;
1372 edge_info->cond_equivalences.safe_push (c);
1374 /* It is possible for INVERTED to be the negation of a comparison,
1375 and not a valid RHS or GIMPLE_COND condition. This happens because
1376 invert_truthvalue may return such an expression when asked to invert
1377 a floating-point comparison. These comparisons are not assumed to
1378 obey the trichotomy law. */
1379 initialize_expr_from_cond (inverted, &c.cond);
1380 c.value = boolean_false_node;
1381 edge_info->cond_equivalences.safe_push (c);
1384 /* A helper function for record_const_or_copy and record_equality.
1385 Do the work of recording the value and undo info. */
1387 static void
1388 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1390 set_ssa_name_value (x, y);
1392 if (dump_file && (dump_flags & TDF_DETAILS))
1394 fprintf (dump_file, "0>>> COPY ");
1395 print_generic_expr (dump_file, x, 0);
1396 fprintf (dump_file, " = ");
1397 print_generic_expr (dump_file, y, 0);
1398 fprintf (dump_file, "\n");
1401 const_and_copies_stack.reserve (2);
1402 const_and_copies_stack.quick_push (prev_x);
1403 const_and_copies_stack.quick_push (x);
1406 /* Return the loop depth of the basic block of the defining statement of X.
1407 This number should not be treated as absolutely correct because the loop
1408 information may not be completely up-to-date when dom runs. However, it
1409 will be relatively correct, and as more passes are taught to keep loop info
1410 up to date, the result will become more and more accurate. */
1413 loop_depth_of_name (tree x)
1415 gimple defstmt;
1416 basic_block defbb;
1418 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1419 if (TREE_CODE (x) != SSA_NAME)
1420 return 0;
1422 /* Otherwise return the loop depth of the defining statement's bb.
1423 Note that there may not actually be a bb for this statement, if the
1424 ssa_name is live on entry. */
1425 defstmt = SSA_NAME_DEF_STMT (x);
1426 defbb = gimple_bb (defstmt);
1427 if (!defbb)
1428 return 0;
1430 return bb_loop_depth (defbb);
1433 /* Record that X is equal to Y in const_and_copies. Record undo
1434 information in the block-local vector. */
1436 static void
1437 record_const_or_copy (tree x, tree y)
1439 tree prev_x = SSA_NAME_VALUE (x);
1441 gcc_assert (TREE_CODE (x) == SSA_NAME);
1443 if (TREE_CODE (y) == SSA_NAME)
1445 tree tmp = SSA_NAME_VALUE (y);
1446 if (tmp)
1447 y = tmp;
1450 record_const_or_copy_1 (x, y, prev_x);
1453 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1454 This constrains the cases in which we may treat this as assignment. */
1456 static void
1457 record_equality (tree x, tree y)
1459 tree prev_x = NULL, prev_y = NULL;
1461 if (TREE_CODE (x) == SSA_NAME)
1462 prev_x = SSA_NAME_VALUE (x);
1463 if (TREE_CODE (y) == SSA_NAME)
1464 prev_y = SSA_NAME_VALUE (y);
1466 /* If one of the previous values is invariant, or invariant in more loops
1467 (by depth), then use that.
1468 Otherwise it doesn't matter which value we choose, just so
1469 long as we canonicalize on one value. */
1470 if (is_gimple_min_invariant (y))
1472 else if (is_gimple_min_invariant (x)
1473 || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1474 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1475 else if (prev_x && is_gimple_min_invariant (prev_x))
1476 x = y, y = prev_x, prev_x = prev_y;
1477 else if (prev_y)
1478 y = prev_y;
1480 /* After the swapping, we must have one SSA_NAME. */
1481 if (TREE_CODE (x) != SSA_NAME)
1482 return;
1484 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1485 variable compared against zero. If we're honoring signed zeros,
1486 then we cannot record this value unless we know that the value is
1487 nonzero. */
1488 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1489 && (TREE_CODE (y) != REAL_CST
1490 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1491 return;
1493 record_const_or_copy_1 (x, y, prev_x);
1496 /* Returns true when STMT is a simple iv increment. It detects the
1497 following situation:
1499 i_1 = phi (..., i_2)
1500 i_2 = i_1 +/- ... */
1502 bool
1503 simple_iv_increment_p (gimple stmt)
1505 enum tree_code code;
1506 tree lhs, preinc;
1507 gimple phi;
1508 size_t i;
1510 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1511 return false;
1513 lhs = gimple_assign_lhs (stmt);
1514 if (TREE_CODE (lhs) != SSA_NAME)
1515 return false;
1517 code = gimple_assign_rhs_code (stmt);
1518 if (code != PLUS_EXPR
1519 && code != MINUS_EXPR
1520 && code != POINTER_PLUS_EXPR)
1521 return false;
1523 preinc = gimple_assign_rhs1 (stmt);
1524 if (TREE_CODE (preinc) != SSA_NAME)
1525 return false;
1527 phi = SSA_NAME_DEF_STMT (preinc);
1528 if (gimple_code (phi) != GIMPLE_PHI)
1529 return false;
1531 for (i = 0; i < gimple_phi_num_args (phi); i++)
1532 if (gimple_phi_arg_def (phi, i) == lhs)
1533 return true;
1535 return false;
1538 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1539 known value for that SSA_NAME (or NULL if no value is known).
1541 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1542 successors of BB. */
1544 static void
1545 cprop_into_successor_phis (basic_block bb)
1547 edge e;
1548 edge_iterator ei;
1550 FOR_EACH_EDGE (e, ei, bb->succs)
1552 int indx;
1553 gimple_stmt_iterator gsi;
1555 /* If this is an abnormal edge, then we do not want to copy propagate
1556 into the PHI alternative associated with this edge. */
1557 if (e->flags & EDGE_ABNORMAL)
1558 continue;
1560 gsi = gsi_start_phis (e->dest);
1561 if (gsi_end_p (gsi))
1562 continue;
1564 indx = e->dest_idx;
1565 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1567 tree new_val;
1568 use_operand_p orig_p;
1569 tree orig_val;
1570 gimple phi = gsi_stmt (gsi);
1572 /* The alternative may be associated with a constant, so verify
1573 it is an SSA_NAME before doing anything with it. */
1574 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1575 orig_val = get_use_from_ptr (orig_p);
1576 if (TREE_CODE (orig_val) != SSA_NAME)
1577 continue;
1579 /* If we have *ORIG_P in our constant/copy table, then replace
1580 ORIG_P with its value in our constant/copy table. */
1581 new_val = SSA_NAME_VALUE (orig_val);
1582 if (new_val
1583 && new_val != orig_val
1584 && (TREE_CODE (new_val) == SSA_NAME
1585 || is_gimple_min_invariant (new_val))
1586 && may_propagate_copy (orig_val, new_val))
1587 propagate_value (orig_p, new_val);
1592 /* We have finished optimizing BB, record any information implied by
1593 taking a specific outgoing edge from BB. */
1595 static void
1596 record_edge_info (basic_block bb)
1598 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1599 struct edge_info *edge_info;
1601 if (! gsi_end_p (gsi))
1603 gimple stmt = gsi_stmt (gsi);
1604 location_t loc = gimple_location (stmt);
1606 if (gimple_code (stmt) == GIMPLE_SWITCH)
1608 tree index = gimple_switch_index (stmt);
1610 if (TREE_CODE (index) == SSA_NAME)
1612 int i;
1613 int n_labels = gimple_switch_num_labels (stmt);
1614 tree *info = XCNEWVEC (tree, last_basic_block);
1615 edge e;
1616 edge_iterator ei;
1618 for (i = 0; i < n_labels; i++)
1620 tree label = gimple_switch_label (stmt, i);
1621 basic_block target_bb = label_to_block (CASE_LABEL (label));
1622 if (CASE_HIGH (label)
1623 || !CASE_LOW (label)
1624 || info[target_bb->index])
1625 info[target_bb->index] = error_mark_node;
1626 else
1627 info[target_bb->index] = label;
1630 FOR_EACH_EDGE (e, ei, bb->succs)
1632 basic_block target_bb = e->dest;
1633 tree label = info[target_bb->index];
1635 if (label != NULL && label != error_mark_node)
1637 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1638 CASE_LOW (label));
1639 edge_info = allocate_edge_info (e);
1640 edge_info->lhs = index;
1641 edge_info->rhs = x;
1644 free (info);
1648 /* A COND_EXPR may create equivalences too. */
1649 if (gimple_code (stmt) == GIMPLE_COND)
1651 edge true_edge;
1652 edge false_edge;
1654 tree op0 = gimple_cond_lhs (stmt);
1655 tree op1 = gimple_cond_rhs (stmt);
1656 enum tree_code code = gimple_cond_code (stmt);
1658 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1660 /* Special case comparing booleans against a constant as we
1661 know the value of OP0 on both arms of the branch. i.e., we
1662 can record an equivalence for OP0 rather than COND. */
1663 if ((code == EQ_EXPR || code == NE_EXPR)
1664 && TREE_CODE (op0) == SSA_NAME
1665 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1666 && is_gimple_min_invariant (op1))
1668 if (code == EQ_EXPR)
1670 edge_info = allocate_edge_info (true_edge);
1671 edge_info->lhs = op0;
1672 edge_info->rhs = (integer_zerop (op1)
1673 ? boolean_false_node
1674 : boolean_true_node);
1676 edge_info = allocate_edge_info (false_edge);
1677 edge_info->lhs = op0;
1678 edge_info->rhs = (integer_zerop (op1)
1679 ? boolean_true_node
1680 : boolean_false_node);
1682 else
1684 edge_info = allocate_edge_info (true_edge);
1685 edge_info->lhs = op0;
1686 edge_info->rhs = (integer_zerop (op1)
1687 ? boolean_true_node
1688 : boolean_false_node);
1690 edge_info = allocate_edge_info (false_edge);
1691 edge_info->lhs = op0;
1692 edge_info->rhs = (integer_zerop (op1)
1693 ? boolean_false_node
1694 : boolean_true_node);
1697 else if (is_gimple_min_invariant (op0)
1698 && (TREE_CODE (op1) == SSA_NAME
1699 || is_gimple_min_invariant (op1)))
1701 tree cond = build2 (code, boolean_type_node, op0, op1);
1702 tree inverted = invert_truthvalue_loc (loc, cond);
1703 bool can_infer_simple_equiv
1704 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
1705 && real_zerop (op0));
1706 struct edge_info *edge_info;
1708 edge_info = allocate_edge_info (true_edge);
1709 record_conditions (edge_info, cond, inverted);
1711 if (can_infer_simple_equiv && code == EQ_EXPR)
1713 edge_info->lhs = op1;
1714 edge_info->rhs = op0;
1717 edge_info = allocate_edge_info (false_edge);
1718 record_conditions (edge_info, inverted, cond);
1720 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1722 edge_info->lhs = op1;
1723 edge_info->rhs = op0;
1727 else if (TREE_CODE (op0) == SSA_NAME
1728 && (TREE_CODE (op1) == SSA_NAME
1729 || is_gimple_min_invariant (op1)))
1731 tree cond = build2 (code, boolean_type_node, op0, op1);
1732 tree inverted = invert_truthvalue_loc (loc, cond);
1733 bool can_infer_simple_equiv
1734 = !(HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op1)))
1735 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
1736 struct edge_info *edge_info;
1738 edge_info = allocate_edge_info (true_edge);
1739 record_conditions (edge_info, cond, inverted);
1741 if (can_infer_simple_equiv && code == EQ_EXPR)
1743 edge_info->lhs = op0;
1744 edge_info->rhs = op1;
1747 edge_info = allocate_edge_info (false_edge);
1748 record_conditions (edge_info, inverted, cond);
1750 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
1752 edge_info->lhs = op0;
1753 edge_info->rhs = op1;
1758 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1762 static void
1763 dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1764 basic_block bb)
1766 gimple_stmt_iterator gsi;
1768 if (dump_file && (dump_flags & TDF_DETAILS))
1769 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1771 /* Push a marker on the stacks of local information so that we know how
1772 far to unwind when we finalize this block. */
1773 avail_exprs_stack.safe_push (NULL);
1774 const_and_copies_stack.safe_push (NULL_TREE);
1776 record_equivalences_from_incoming_edge (bb);
1778 /* PHI nodes can create equivalences too. */
1779 record_equivalences_from_phis (bb);
1781 /* Create equivalences from redundant PHIs. PHIs are only truly
1782 redundant when they exist in the same block, so push another
1783 marker and unwind right afterwards. */
1784 avail_exprs_stack.safe_push (NULL);
1785 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1786 eliminate_redundant_computations (&gsi);
1787 remove_local_expressions_from_table ();
1789 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1790 optimize_stmt (bb, gsi);
1792 /* Now prepare to process dominated blocks. */
1793 record_edge_info (bb);
1794 cprop_into_successor_phis (bb);
1797 /* We have finished processing the dominator children of BB, perform
1798 any finalization actions in preparation for leaving this node in
1799 the dominator tree. */
1801 static void
1802 dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb)
1804 gimple last;
1806 /* If we have an outgoing edge to a block with multiple incoming and
1807 outgoing edges, then we may be able to thread the edge, i.e., we
1808 may be able to statically determine which of the outgoing edges
1809 will be traversed when the incoming edge from BB is traversed. */
1810 if (single_succ_p (bb)
1811 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1812 && potentially_threadable_block (single_succ (bb)))
1814 /* Push a marker on the stack, which thread_across_edge expects
1815 and will remove. */
1816 const_and_copies_stack.safe_push (NULL_TREE);
1817 dom_thread_across_edge (walk_data, single_succ_edge (bb));
1819 else if ((last = last_stmt (bb))
1820 && gimple_code (last) == GIMPLE_COND
1821 && EDGE_COUNT (bb->succs) == 2
1822 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1823 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1825 edge true_edge, false_edge;
1827 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1829 /* Only try to thread the edge if it reaches a target block with
1830 more than one predecessor and more than one successor. */
1831 if (potentially_threadable_block (true_edge->dest))
1833 struct edge_info *edge_info;
1834 unsigned int i;
1836 /* Push a marker onto the available expression stack so that we
1837 unwind any expressions related to the TRUE arm before processing
1838 the false arm below. */
1839 avail_exprs_stack.safe_push (NULL);
1840 const_and_copies_stack.safe_push (NULL_TREE);
1842 edge_info = (struct edge_info *) true_edge->aux;
1844 /* If we have info associated with this edge, record it into
1845 our equivalence tables. */
1846 if (edge_info)
1848 cond_equivalence *eq;
1849 tree lhs = edge_info->lhs;
1850 tree rhs = edge_info->rhs;
1852 /* If we have a simple NAME = VALUE equivalence, record it. */
1853 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1854 record_const_or_copy (lhs, rhs);
1856 /* If we have 0 = COND or 1 = COND equivalences, record them
1857 into our expression hash tables. */
1858 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1859 record_cond (eq);
1862 dom_thread_across_edge (walk_data, true_edge);
1864 /* And restore the various tables to their state before
1865 we threaded this edge. */
1866 remove_local_expressions_from_table ();
1869 /* Similarly for the ELSE arm. */
1870 if (potentially_threadable_block (false_edge->dest))
1872 struct edge_info *edge_info;
1873 unsigned int i;
1875 const_and_copies_stack.safe_push (NULL_TREE);
1876 edge_info = (struct edge_info *) false_edge->aux;
1878 /* If we have info associated with this edge, record it into
1879 our equivalence tables. */
1880 if (edge_info)
1882 cond_equivalence *eq;
1883 tree lhs = edge_info->lhs;
1884 tree rhs = edge_info->rhs;
1886 /* If we have a simple NAME = VALUE equivalence, record it. */
1887 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1888 record_const_or_copy (lhs, rhs);
1890 /* If we have 0 = COND or 1 = COND equivalences, record them
1891 into our expression hash tables. */
1892 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1893 record_cond (eq);
1896 /* Now thread the edge. */
1897 dom_thread_across_edge (walk_data, false_edge);
1899 /* No need to remove local expressions from our tables
1900 or restore vars to their original value as that will
1901 be done immediately below. */
1905 remove_local_expressions_from_table ();
1906 restore_vars_to_original_value ();
1909 /* Search for redundant computations in STMT. If any are found, then
1910 replace them with the variable holding the result of the computation.
1912 If safe, record this expression into the available expression hash
1913 table. */
1915 static void
1916 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
1918 tree expr_type;
1919 tree cached_lhs;
1920 tree def;
1921 bool insert = true;
1922 bool assigns_var_p = false;
1924 gimple stmt = gsi_stmt (*gsi);
1926 if (gimple_code (stmt) == GIMPLE_PHI)
1927 def = gimple_phi_result (stmt);
1928 else
1929 def = gimple_get_lhs (stmt);
1931 /* Certain expressions on the RHS can be optimized away, but can not
1932 themselves be entered into the hash tables. */
1933 if (! def
1934 || TREE_CODE (def) != SSA_NAME
1935 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1936 || gimple_vdef (stmt)
1937 /* Do not record equivalences for increments of ivs. This would create
1938 overlapping live ranges for a very questionable gain. */
1939 || simple_iv_increment_p (stmt))
1940 insert = false;
1942 /* Check if the expression has been computed before. */
1943 cached_lhs = lookup_avail_expr (stmt, insert);
1945 opt_stats.num_exprs_considered++;
1947 /* Get the type of the expression we are trying to optimize. */
1948 if (is_gimple_assign (stmt))
1950 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1951 assigns_var_p = true;
1953 else if (gimple_code (stmt) == GIMPLE_COND)
1954 expr_type = boolean_type_node;
1955 else if (is_gimple_call (stmt))
1957 gcc_assert (gimple_call_lhs (stmt));
1958 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1959 assigns_var_p = true;
1961 else if (gimple_code (stmt) == GIMPLE_SWITCH)
1962 expr_type = TREE_TYPE (gimple_switch_index (stmt));
1963 else if (gimple_code (stmt) == GIMPLE_PHI)
1964 /* We can't propagate into a phi, so the logic below doesn't apply.
1965 Instead record an equivalence between the cached LHS and the
1966 PHI result of this statement, provided they are in the same block.
1967 This should be sufficient to kill the redundant phi. */
1969 if (def && cached_lhs)
1970 record_const_or_copy (def, cached_lhs);
1971 return;
1973 else
1974 gcc_unreachable ();
1976 if (!cached_lhs)
1977 return;
1979 /* It is safe to ignore types here since we have already done
1980 type checking in the hashing and equality routines. In fact
1981 type checking here merely gets in the way of constant
1982 propagation. Also, make sure that it is safe to propagate
1983 CACHED_LHS into the expression in STMT. */
1984 if ((TREE_CODE (cached_lhs) != SSA_NAME
1985 && (assigns_var_p
1986 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1987 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1989 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1990 || is_gimple_min_invariant (cached_lhs));
1992 if (dump_file && (dump_flags & TDF_DETAILS))
1994 fprintf (dump_file, " Replaced redundant expr '");
1995 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1996 fprintf (dump_file, "' with '");
1997 print_generic_expr (dump_file, cached_lhs, dump_flags);
1998 fprintf (dump_file, "'\n");
2001 opt_stats.num_re++;
2003 if (assigns_var_p
2004 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
2005 cached_lhs = fold_convert (expr_type, cached_lhs);
2007 propagate_tree_value_into_stmt (gsi, cached_lhs);
2009 /* Since it is always necessary to mark the result as modified,
2010 perhaps we should move this into propagate_tree_value_into_stmt
2011 itself. */
2012 gimple_set_modified (gsi_stmt (*gsi), true);
2016 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2017 the available expressions table or the const_and_copies table.
2018 Detect and record those equivalences. */
2019 /* We handle only very simple copy equivalences here. The heavy
2020 lifing is done by eliminate_redundant_computations. */
2022 static void
2023 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
2025 tree lhs;
2026 enum tree_code lhs_code;
2028 gcc_assert (is_gimple_assign (stmt));
2030 lhs = gimple_assign_lhs (stmt);
2031 lhs_code = TREE_CODE (lhs);
2033 if (lhs_code == SSA_NAME
2034 && gimple_assign_single_p (stmt))
2036 tree rhs = gimple_assign_rhs1 (stmt);
2038 /* If the RHS of the assignment is a constant or another variable that
2039 may be propagated, register it in the CONST_AND_COPIES table. We
2040 do not need to record unwind data for this, since this is a true
2041 assignment and not an equivalence inferred from a comparison. All
2042 uses of this ssa name are dominated by this assignment, so unwinding
2043 just costs time and space. */
2044 if (may_optimize_p
2045 && (TREE_CODE (rhs) == SSA_NAME
2046 || is_gimple_min_invariant (rhs)))
2048 if (dump_file && (dump_flags & TDF_DETAILS))
2050 fprintf (dump_file, "==== ASGN ");
2051 print_generic_expr (dump_file, lhs, 0);
2052 fprintf (dump_file, " = ");
2053 print_generic_expr (dump_file, rhs, 0);
2054 fprintf (dump_file, "\n");
2057 set_ssa_name_value (lhs, rhs);
2061 /* A memory store, even an aliased store, creates a useful
2062 equivalence. By exchanging the LHS and RHS, creating suitable
2063 vops and recording the result in the available expression table,
2064 we may be able to expose more redundant loads. */
2065 if (!gimple_has_volatile_ops (stmt)
2066 && gimple_references_memory_p (stmt)
2067 && gimple_assign_single_p (stmt)
2068 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2069 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2070 && !is_gimple_reg (lhs))
2072 tree rhs = gimple_assign_rhs1 (stmt);
2073 gimple new_stmt;
2075 /* Build a new statement with the RHS and LHS exchanged. */
2076 if (TREE_CODE (rhs) == SSA_NAME)
2078 /* NOTE tuples. The call to gimple_build_assign below replaced
2079 a call to build_gimple_modify_stmt, which did not set the
2080 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2081 may cause an SSA validation failure, as the LHS may be a
2082 default-initialized name and should have no definition. I'm
2083 a bit dubious of this, as the artificial statement that we
2084 generate here may in fact be ill-formed, but it is simply
2085 used as an internal device in this pass, and never becomes
2086 part of the CFG. */
2087 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2088 new_stmt = gimple_build_assign (rhs, lhs);
2089 SSA_NAME_DEF_STMT (rhs) = defstmt;
2091 else
2092 new_stmt = gimple_build_assign (rhs, lhs);
2094 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
2096 /* Finally enter the statement into the available expression
2097 table. */
2098 lookup_avail_expr (new_stmt, true);
2102 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2103 CONST_AND_COPIES. */
2105 static void
2106 cprop_operand (gimple stmt, use_operand_p op_p)
2108 tree val;
2109 tree op = USE_FROM_PTR (op_p);
2111 /* If the operand has a known constant value or it is known to be a
2112 copy of some other variable, use the value or copy stored in
2113 CONST_AND_COPIES. */
2114 val = SSA_NAME_VALUE (op);
2115 if (val && val != op)
2117 /* Do not replace hard register operands in asm statements. */
2118 if (gimple_code (stmt) == GIMPLE_ASM
2119 && !may_propagate_copy_into_asm (op))
2120 return;
2122 /* Certain operands are not allowed to be copy propagated due
2123 to their interaction with exception handling and some GCC
2124 extensions. */
2125 if (!may_propagate_copy (op, val))
2126 return;
2128 /* Do not propagate addresses that point to volatiles into memory
2129 stmts without volatile operands. */
2130 if (POINTER_TYPE_P (TREE_TYPE (val))
2131 && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
2132 && gimple_has_mem_ops (stmt)
2133 && !gimple_has_volatile_ops (stmt))
2134 return;
2136 /* Do not propagate copies if the propagated value is at a deeper loop
2137 depth than the propagatee. Otherwise, this may move loop variant
2138 variables outside of their loops and prevent coalescing
2139 opportunities. If the value was loop invariant, it will be hoisted
2140 by LICM and exposed for copy propagation. */
2141 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2142 return;
2144 /* Do not propagate copies into simple IV increment statements.
2145 See PR23821 for how this can disturb IV analysis. */
2146 if (TREE_CODE (val) != INTEGER_CST
2147 && simple_iv_increment_p (stmt))
2148 return;
2150 /* Dump details. */
2151 if (dump_file && (dump_flags & TDF_DETAILS))
2153 fprintf (dump_file, " Replaced '");
2154 print_generic_expr (dump_file, op, dump_flags);
2155 fprintf (dump_file, "' with %s '",
2156 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2157 print_generic_expr (dump_file, val, dump_flags);
2158 fprintf (dump_file, "'\n");
2161 if (TREE_CODE (val) != SSA_NAME)
2162 opt_stats.num_const_prop++;
2163 else
2164 opt_stats.num_copy_prop++;
2166 propagate_value (op_p, val);
2168 /* And note that we modified this statement. This is now
2169 safe, even if we changed virtual operands since we will
2170 rescan the statement and rewrite its operands again. */
2171 gimple_set_modified (stmt, true);
2175 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2176 known value for that SSA_NAME (or NULL if no value is known).
2178 Propagate values from CONST_AND_COPIES into the uses, vuses and
2179 vdef_ops of STMT. */
2181 static void
2182 cprop_into_stmt (gimple stmt)
2184 use_operand_p op_p;
2185 ssa_op_iter iter;
2187 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2188 cprop_operand (stmt, op_p);
2191 /* Optimize the statement pointed to by iterator SI.
2193 We try to perform some simplistic global redundancy elimination and
2194 constant propagation:
2196 1- To detect global redundancy, we keep track of expressions that have
2197 been computed in this block and its dominators. If we find that the
2198 same expression is computed more than once, we eliminate repeated
2199 computations by using the target of the first one.
2201 2- Constant values and copy assignments. This is used to do very
2202 simplistic constant and copy propagation. When a constant or copy
2203 assignment is found, we map the value on the RHS of the assignment to
2204 the variable in the LHS in the CONST_AND_COPIES table. */
2206 static void
2207 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2209 gimple stmt, old_stmt;
2210 bool may_optimize_p;
2211 bool modified_p = false;
2213 old_stmt = stmt = gsi_stmt (si);
2215 if (dump_file && (dump_flags & TDF_DETAILS))
2217 fprintf (dump_file, "Optimizing statement ");
2218 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2221 if (gimple_code (stmt) == GIMPLE_COND)
2222 canonicalize_comparison (stmt);
2224 update_stmt_if_modified (stmt);
2225 opt_stats.num_stmts++;
2227 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2228 cprop_into_stmt (stmt);
2230 /* If the statement has been modified with constant replacements,
2231 fold its RHS before checking for redundant computations. */
2232 if (gimple_modified_p (stmt))
2234 tree rhs = NULL;
2236 /* Try to fold the statement making sure that STMT is kept
2237 up to date. */
2238 if (fold_stmt (&si))
2240 stmt = gsi_stmt (si);
2241 gimple_set_modified (stmt, true);
2243 if (dump_file && (dump_flags & TDF_DETAILS))
2245 fprintf (dump_file, " Folded to: ");
2246 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2250 /* We only need to consider cases that can yield a gimple operand. */
2251 if (gimple_assign_single_p (stmt))
2252 rhs = gimple_assign_rhs1 (stmt);
2253 else if (gimple_code (stmt) == GIMPLE_GOTO)
2254 rhs = gimple_goto_dest (stmt);
2255 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2256 /* This should never be an ADDR_EXPR. */
2257 rhs = gimple_switch_index (stmt);
2259 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2260 recompute_tree_invariant_for_addr_expr (rhs);
2262 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2263 even if fold_stmt updated the stmt already and thus cleared
2264 gimple_modified_p flag on it. */
2265 modified_p = true;
2268 /* Check for redundant computations. Do this optimization only
2269 for assignments that have no volatile ops and conditionals. */
2270 may_optimize_p = (!gimple_has_side_effects (stmt)
2271 && (is_gimple_assign (stmt)
2272 || (is_gimple_call (stmt)
2273 && gimple_call_lhs (stmt) != NULL_TREE)
2274 || gimple_code (stmt) == GIMPLE_COND
2275 || gimple_code (stmt) == GIMPLE_SWITCH));
2277 if (may_optimize_p)
2279 if (gimple_code (stmt) == GIMPLE_CALL)
2281 /* Resolve __builtin_constant_p. If it hasn't been
2282 folded to integer_one_node by now, it's fairly
2283 certain that the value simply isn't constant. */
2284 tree callee = gimple_call_fndecl (stmt);
2285 if (callee
2286 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2287 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2289 propagate_tree_value_into_stmt (&si, integer_zero_node);
2290 stmt = gsi_stmt (si);
2294 update_stmt_if_modified (stmt);
2295 eliminate_redundant_computations (&si);
2296 stmt = gsi_stmt (si);
2298 /* Perform simple redundant store elimination. */
2299 if (gimple_assign_single_p (stmt)
2300 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2302 tree lhs = gimple_assign_lhs (stmt);
2303 tree rhs = gimple_assign_rhs1 (stmt);
2304 tree cached_lhs;
2305 gimple new_stmt;
2306 if (TREE_CODE (rhs) == SSA_NAME)
2308 tree tem = SSA_NAME_VALUE (rhs);
2309 if (tem)
2310 rhs = tem;
2312 /* Build a new statement with the RHS and LHS exchanged. */
2313 if (TREE_CODE (rhs) == SSA_NAME)
2315 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2316 new_stmt = gimple_build_assign (rhs, lhs);
2317 SSA_NAME_DEF_STMT (rhs) = defstmt;
2319 else
2320 new_stmt = gimple_build_assign (rhs, lhs);
2321 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2322 cached_lhs = lookup_avail_expr (new_stmt, false);
2323 if (cached_lhs
2324 && rhs == cached_lhs)
2326 basic_block bb = gimple_bb (stmt);
2327 unlink_stmt_vdef (stmt);
2328 if (gsi_remove (&si, true))
2330 bitmap_set_bit (need_eh_cleanup, bb->index);
2331 if (dump_file && (dump_flags & TDF_DETAILS))
2332 fprintf (dump_file, " Flagged to clear EH edges.\n");
2334 release_defs (stmt);
2335 return;
2340 /* Record any additional equivalences created by this statement. */
2341 if (is_gimple_assign (stmt))
2342 record_equivalences_from_stmt (stmt, may_optimize_p);
2344 /* If STMT is a COND_EXPR and it was modified, then we may know
2345 where it goes. If that is the case, then mark the CFG as altered.
2347 This will cause us to later call remove_unreachable_blocks and
2348 cleanup_tree_cfg when it is safe to do so. It is not safe to
2349 clean things up here since removal of edges and such can trigger
2350 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2351 the manager.
2353 That's all fine and good, except that once SSA_NAMEs are released
2354 to the manager, we must not call create_ssa_name until all references
2355 to released SSA_NAMEs have been eliminated.
2357 All references to the deleted SSA_NAMEs can not be eliminated until
2358 we remove unreachable blocks.
2360 We can not remove unreachable blocks until after we have completed
2361 any queued jump threading.
2363 We can not complete any queued jump threads until we have taken
2364 appropriate variables out of SSA form. Taking variables out of
2365 SSA form can call create_ssa_name and thus we lose.
2367 Ultimately I suspect we're going to need to change the interface
2368 into the SSA_NAME manager. */
2369 if (gimple_modified_p (stmt) || modified_p)
2371 tree val = NULL;
2373 update_stmt_if_modified (stmt);
2375 if (gimple_code (stmt) == GIMPLE_COND)
2376 val = fold_binary_loc (gimple_location (stmt),
2377 gimple_cond_code (stmt), boolean_type_node,
2378 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
2379 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2380 val = gimple_switch_index (stmt);
2382 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2383 cfg_altered = true;
2385 /* If we simplified a statement in such a way as to be shown that it
2386 cannot trap, update the eh information and the cfg to match. */
2387 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2389 bitmap_set_bit (need_eh_cleanup, bb->index);
2390 if (dump_file && (dump_flags & TDF_DETAILS))
2391 fprintf (dump_file, " Flagged to clear EH edges.\n");
2396 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2397 If found, return its LHS. Otherwise insert STMT in the table and
2398 return NULL_TREE.
2400 Also, when an expression is first inserted in the table, it is also
2401 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2402 we finish processing this block and its children. */
2404 static tree
2405 lookup_avail_expr (gimple stmt, bool insert)
2407 void **slot;
2408 tree lhs;
2409 tree temp;
2410 struct expr_hash_elt element;
2412 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2413 if (gimple_code (stmt) == GIMPLE_PHI)
2414 lhs = gimple_phi_result (stmt);
2415 else
2416 lhs = gimple_get_lhs (stmt);
2418 initialize_hash_element (stmt, lhs, &element);
2420 if (dump_file && (dump_flags & TDF_DETAILS))
2422 fprintf (dump_file, "LKUP ");
2423 print_expr_hash_elt (dump_file, &element);
2426 /* Don't bother remembering constant assignments and copy operations.
2427 Constants and copy operations are handled by the constant/copy propagator
2428 in optimize_stmt. */
2429 if (element.expr.kind == EXPR_SINGLE
2430 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2431 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2432 return NULL_TREE;
2434 /* Finally try to find the expression in the main expression hash table. */
2435 slot = htab_find_slot_with_hash (avail_exprs, &element, element.hash,
2436 (insert ? INSERT : NO_INSERT));
2437 if (slot == NULL)
2439 free_expr_hash_elt_contents (&element);
2440 return NULL_TREE;
2442 else if (*slot == NULL)
2444 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2445 *element2 = element;
2446 element2->stamp = element2;
2447 *slot = (void *) element2;
2449 if (dump_file && (dump_flags & TDF_DETAILS))
2451 fprintf (dump_file, "2>>> ");
2452 print_expr_hash_elt (dump_file, element2);
2455 avail_exprs_stack.safe_push (element2);
2456 return NULL_TREE;
2458 else
2459 free_expr_hash_elt_contents (&element);
2461 /* Extract the LHS of the assignment so that it can be used as the current
2462 definition of another variable. */
2463 lhs = ((struct expr_hash_elt *)*slot)->lhs;
2465 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2466 use the value from the const_and_copies table. */
2467 if (TREE_CODE (lhs) == SSA_NAME)
2469 temp = SSA_NAME_VALUE (lhs);
2470 if (temp)
2471 lhs = temp;
2474 if (dump_file && (dump_flags & TDF_DETAILS))
2476 fprintf (dump_file, "FIND: ");
2477 print_generic_expr (dump_file, lhs, 0);
2478 fprintf (dump_file, "\n");
2481 return lhs;
2484 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2485 for expressions using the code of the expression and the SSA numbers of
2486 its operands. */
2488 static hashval_t
2489 avail_expr_hash (const void *p)
2491 gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
2492 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2493 tree vuse;
2494 hashval_t val = 0;
2496 val = iterative_hash_hashable_expr (expr, val);
2498 /* If the hash table entry is not associated with a statement, then we
2499 can just hash the expression and not worry about virtual operands
2500 and such. */
2501 if (!stmt)
2502 return val;
2504 /* Add the SSA version numbers of the vuse operand. This is important
2505 because compound variables like arrays are not renamed in the
2506 operands. Rather, the rename is done on the virtual variable
2507 representing all the elements of the array. */
2508 if ((vuse = gimple_vuse (stmt)))
2509 val = iterative_hash_expr (vuse, val);
2511 return val;
2514 static hashval_t
2515 real_avail_expr_hash (const void *p)
2517 return ((const struct expr_hash_elt *)p)->hash;
2520 static int
2521 avail_expr_eq (const void *p1, const void *p2)
2523 gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt;
2524 const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr;
2525 const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp;
2526 gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt;
2527 const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr;
2528 const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp;
2530 /* This case should apply only when removing entries from the table. */
2531 if (stamp1 == stamp2)
2532 return true;
2534 /* FIXME tuples:
2535 We add stmts to a hash table and them modify them. To detect the case
2536 that we modify a stmt and then search for it, we assume that the hash
2537 is always modified by that change.
2538 We have to fully check why this doesn't happen on trunk or rewrite
2539 this in a more reliable (and easier to understand) way. */
2540 if (((const struct expr_hash_elt *)p1)->hash
2541 != ((const struct expr_hash_elt *)p2)->hash)
2542 return false;
2544 /* In case of a collision, both RHS have to be identical and have the
2545 same VUSE operands. */
2546 if (hashable_expr_equal_p (expr1, expr2)
2547 && types_compatible_p (expr1->type, expr2->type))
2549 /* Note that STMT1 and/or STMT2 may be NULL. */
2550 return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
2551 == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
2554 return false;
2557 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2558 up degenerate PHIs created by or exposed by jump threading. */
2560 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2561 NULL. */
2563 tree
2564 degenerate_phi_result (gimple phi)
2566 tree lhs = gimple_phi_result (phi);
2567 tree val = NULL;
2568 size_t i;
2570 /* Ignoring arguments which are the same as LHS, if all the remaining
2571 arguments are the same, then the PHI is a degenerate and has the
2572 value of that common argument. */
2573 for (i = 0; i < gimple_phi_num_args (phi); i++)
2575 tree arg = gimple_phi_arg_def (phi, i);
2577 if (arg == lhs)
2578 continue;
2579 else if (!arg)
2580 break;
2581 else if (!val)
2582 val = arg;
2583 else if (arg == val)
2584 continue;
2585 /* We bring in some of operand_equal_p not only to speed things
2586 up, but also to avoid crashing when dereferencing the type of
2587 a released SSA name. */
2588 else if (TREE_CODE (val) != TREE_CODE (arg)
2589 || TREE_CODE (val) == SSA_NAME
2590 || !operand_equal_p (arg, val, 0))
2591 break;
2593 return (i == gimple_phi_num_args (phi) ? val : NULL);
2596 /* Given a statement STMT, which is either a PHI node or an assignment,
2597 remove it from the IL. */
2599 static void
2600 remove_stmt_or_phi (gimple stmt)
2602 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2604 if (gimple_code (stmt) == GIMPLE_PHI)
2605 remove_phi_node (&gsi, true);
2606 else
2608 gsi_remove (&gsi, true);
2609 release_defs (stmt);
2613 /* Given a statement STMT, which is either a PHI node or an assignment,
2614 return the "rhs" of the node, in the case of a non-degenerate
2615 phi, NULL is returned. */
2617 static tree
2618 get_rhs_or_phi_arg (gimple stmt)
2620 if (gimple_code (stmt) == GIMPLE_PHI)
2621 return degenerate_phi_result (stmt);
2622 else if (gimple_assign_single_p (stmt))
2623 return gimple_assign_rhs1 (stmt);
2624 else
2625 gcc_unreachable ();
2629 /* Given a statement STMT, which is either a PHI node or an assignment,
2630 return the "lhs" of the node. */
2632 static tree
2633 get_lhs_or_phi_result (gimple stmt)
2635 if (gimple_code (stmt) == GIMPLE_PHI)
2636 return gimple_phi_result (stmt);
2637 else if (is_gimple_assign (stmt))
2638 return gimple_assign_lhs (stmt);
2639 else
2640 gcc_unreachable ();
2643 /* Propagate RHS into all uses of LHS (when possible).
2645 RHS and LHS are derived from STMT, which is passed in solely so
2646 that we can remove it if propagation is successful.
2648 When propagating into a PHI node or into a statement which turns
2649 into a trivial copy or constant initialization, set the
2650 appropriate bit in INTERESTING_NAMEs so that we will visit those
2651 nodes as well in an effort to pick up secondary optimization
2652 opportunities. */
2654 static void
2655 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2657 /* First verify that propagation is valid and isn't going to move a
2658 loop variant variable outside its loop. */
2659 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2660 && (TREE_CODE (rhs) != SSA_NAME
2661 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2662 && may_propagate_copy (lhs, rhs)
2663 && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2665 use_operand_p use_p;
2666 imm_use_iterator iter;
2667 gimple use_stmt;
2668 bool all = true;
2670 /* Dump details. */
2671 if (dump_file && (dump_flags & TDF_DETAILS))
2673 fprintf (dump_file, " Replacing '");
2674 print_generic_expr (dump_file, lhs, dump_flags);
2675 fprintf (dump_file, "' with %s '",
2676 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2677 print_generic_expr (dump_file, rhs, dump_flags);
2678 fprintf (dump_file, "'\n");
2681 /* Walk over every use of LHS and try to replace the use with RHS.
2682 At this point the only reason why such a propagation would not
2683 be successful would be if the use occurs in an ASM_EXPR. */
2684 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2686 /* Leave debug stmts alone. If we succeed in propagating
2687 all non-debug uses, we'll drop the DEF, and propagation
2688 into debug stmts will occur then. */
2689 if (gimple_debug_bind_p (use_stmt))
2690 continue;
2692 /* It's not always safe to propagate into an ASM_EXPR. */
2693 if (gimple_code (use_stmt) == GIMPLE_ASM
2694 && ! may_propagate_copy_into_asm (lhs))
2696 all = false;
2697 continue;
2700 /* It's not ok to propagate into the definition stmt of RHS.
2701 <bb 9>:
2702 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2703 g_67.1_6 = prephitmp.12_36;
2704 goto <bb 9>;
2705 While this is strictly all dead code we do not want to
2706 deal with this here. */
2707 if (TREE_CODE (rhs) == SSA_NAME
2708 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2710 all = false;
2711 continue;
2714 /* Dump details. */
2715 if (dump_file && (dump_flags & TDF_DETAILS))
2717 fprintf (dump_file, " Original statement:");
2718 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2721 /* Propagate the RHS into this use of the LHS. */
2722 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2723 propagate_value (use_p, rhs);
2725 /* Special cases to avoid useless calls into the folding
2726 routines, operand scanning, etc.
2728 Propagation into a PHI may cause the PHI to become
2729 a degenerate, so mark the PHI as interesting. No other
2730 actions are necessary. */
2731 if (gimple_code (use_stmt) == GIMPLE_PHI)
2733 tree result;
2735 /* Dump details. */
2736 if (dump_file && (dump_flags & TDF_DETAILS))
2738 fprintf (dump_file, " Updated statement:");
2739 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2742 result = get_lhs_or_phi_result (use_stmt);
2743 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2744 continue;
2747 /* From this point onward we are propagating into a
2748 real statement. Folding may (or may not) be possible,
2749 we may expose new operands, expose dead EH edges,
2750 etc. */
2751 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2752 cannot fold a call that simplifies to a constant,
2753 because the GIMPLE_CALL must be replaced by a
2754 GIMPLE_ASSIGN, and there is no way to effect such a
2755 transformation in-place. We might want to consider
2756 using the more general fold_stmt here. */
2758 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2759 fold_stmt_inplace (&gsi);
2762 /* Sometimes propagation can expose new operands to the
2763 renamer. */
2764 update_stmt (use_stmt);
2766 /* Dump details. */
2767 if (dump_file && (dump_flags & TDF_DETAILS))
2769 fprintf (dump_file, " Updated statement:");
2770 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2773 /* If we replaced a variable index with a constant, then
2774 we would need to update the invariant flag for ADDR_EXPRs. */
2775 if (gimple_assign_single_p (use_stmt)
2776 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2777 recompute_tree_invariant_for_addr_expr
2778 (gimple_assign_rhs1 (use_stmt));
2780 /* If we cleaned up EH information from the statement,
2781 mark its containing block as needing EH cleanups. */
2782 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2784 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2785 if (dump_file && (dump_flags & TDF_DETAILS))
2786 fprintf (dump_file, " Flagged to clear EH edges.\n");
2789 /* Propagation may expose new trivial copy/constant propagation
2790 opportunities. */
2791 if (gimple_assign_single_p (use_stmt)
2792 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2793 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2794 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2796 tree result = get_lhs_or_phi_result (use_stmt);
2797 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2800 /* Propagation into these nodes may make certain edges in
2801 the CFG unexecutable. We want to identify them as PHI nodes
2802 at the destination of those unexecutable edges may become
2803 degenerates. */
2804 else if (gimple_code (use_stmt) == GIMPLE_COND
2805 || gimple_code (use_stmt) == GIMPLE_SWITCH
2806 || gimple_code (use_stmt) == GIMPLE_GOTO)
2808 tree val;
2810 if (gimple_code (use_stmt) == GIMPLE_COND)
2811 val = fold_binary_loc (gimple_location (use_stmt),
2812 gimple_cond_code (use_stmt),
2813 boolean_type_node,
2814 gimple_cond_lhs (use_stmt),
2815 gimple_cond_rhs (use_stmt));
2816 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2817 val = gimple_switch_index (use_stmt);
2818 else
2819 val = gimple_goto_dest (use_stmt);
2821 if (val && is_gimple_min_invariant (val))
2823 basic_block bb = gimple_bb (use_stmt);
2824 edge te = find_taken_edge (bb, val);
2825 edge_iterator ei;
2826 edge e;
2827 gimple_stmt_iterator gsi, psi;
2829 /* Remove all outgoing edges except TE. */
2830 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2832 if (e != te)
2834 /* Mark all the PHI nodes at the destination of
2835 the unexecutable edge as interesting. */
2836 for (psi = gsi_start_phis (e->dest);
2837 !gsi_end_p (psi);
2838 gsi_next (&psi))
2840 gimple phi = gsi_stmt (psi);
2842 tree result = gimple_phi_result (phi);
2843 int version = SSA_NAME_VERSION (result);
2845 bitmap_set_bit (interesting_names, version);
2848 te->probability += e->probability;
2850 te->count += e->count;
2851 remove_edge (e);
2852 cfg_altered = true;
2854 else
2855 ei_next (&ei);
2858 gsi = gsi_last_bb (gimple_bb (use_stmt));
2859 gsi_remove (&gsi, true);
2861 /* And fixup the flags on the single remaining edge. */
2862 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2863 te->flags &= ~EDGE_ABNORMAL;
2864 te->flags |= EDGE_FALLTHRU;
2865 if (te->probability > REG_BR_PROB_BASE)
2866 te->probability = REG_BR_PROB_BASE;
2871 /* Ensure there is nothing else to do. */
2872 gcc_assert (!all || has_zero_uses (lhs));
2874 /* If we were able to propagate away all uses of LHS, then
2875 we can remove STMT. */
2876 if (all)
2877 remove_stmt_or_phi (stmt);
2881 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2882 a statement that is a trivial copy or constant initialization.
2884 Attempt to eliminate T by propagating its RHS into all uses of
2885 its LHS. This may in turn set new bits in INTERESTING_NAMES
2886 for nodes we want to revisit later.
2888 All exit paths should clear INTERESTING_NAMES for the result
2889 of STMT. */
2891 static void
2892 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2894 tree lhs = get_lhs_or_phi_result (stmt);
2895 tree rhs;
2896 int version = SSA_NAME_VERSION (lhs);
2898 /* If the LHS of this statement or PHI has no uses, then we can
2899 just eliminate it. This can occur if, for example, the PHI
2900 was created by block duplication due to threading and its only
2901 use was in the conditional at the end of the block which was
2902 deleted. */
2903 if (has_zero_uses (lhs))
2905 bitmap_clear_bit (interesting_names, version);
2906 remove_stmt_or_phi (stmt);
2907 return;
2910 /* Get the RHS of the assignment or PHI node if the PHI is a
2911 degenerate. */
2912 rhs = get_rhs_or_phi_arg (stmt);
2913 if (!rhs)
2915 bitmap_clear_bit (interesting_names, version);
2916 return;
2919 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
2921 /* Note that STMT may well have been deleted by now, so do
2922 not access it, instead use the saved version # to clear
2923 T's entry in the worklist. */
2924 bitmap_clear_bit (interesting_names, version);
2927 /* The first phase in degenerate PHI elimination.
2929 Eliminate the degenerate PHIs in BB, then recurse on the
2930 dominator children of BB. */
2932 static void
2933 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2935 gimple_stmt_iterator gsi;
2936 basic_block son;
2938 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2940 gimple phi = gsi_stmt (gsi);
2942 eliminate_const_or_copy (phi, interesting_names);
2945 /* Recurse into the dominator children of BB. */
2946 for (son = first_dom_son (CDI_DOMINATORS, bb);
2947 son;
2948 son = next_dom_son (CDI_DOMINATORS, son))
2949 eliminate_degenerate_phis_1 (son, interesting_names);
2953 /* A very simple pass to eliminate degenerate PHI nodes from the
2954 IL. This is meant to be fast enough to be able to be run several
2955 times in the optimization pipeline.
2957 Certain optimizations, particularly those which duplicate blocks
2958 or remove edges from the CFG can create or expose PHIs which are
2959 trivial copies or constant initializations.
2961 While we could pick up these optimizations in DOM or with the
2962 combination of copy-prop and CCP, those solutions are far too
2963 heavy-weight for our needs.
2965 This implementation has two phases so that we can efficiently
2966 eliminate the first order degenerate PHIs and second order
2967 degenerate PHIs.
2969 The first phase performs a dominator walk to identify and eliminate
2970 the vast majority of the degenerate PHIs. When a degenerate PHI
2971 is identified and eliminated any affected statements or PHIs
2972 are put on a worklist.
2974 The second phase eliminates degenerate PHIs and trivial copies
2975 or constant initializations using the worklist. This is how we
2976 pick up the secondary optimization opportunities with minimal
2977 cost. */
2979 static unsigned int
2980 eliminate_degenerate_phis (void)
2982 bitmap interesting_names;
2983 bitmap interesting_names1;
2985 /* Bitmap of blocks which need EH information updated. We can not
2986 update it on-the-fly as doing so invalidates the dominator tree. */
2987 need_eh_cleanup = BITMAP_ALLOC (NULL);
2989 /* INTERESTING_NAMES is effectively our worklist, indexed by
2990 SSA_NAME_VERSION.
2992 A set bit indicates that the statement or PHI node which
2993 defines the SSA_NAME should be (re)examined to determine if
2994 it has become a degenerate PHI or trivial const/copy propagation
2995 opportunity.
2997 Experiments have show we generally get better compilation
2998 time behavior with bitmaps rather than sbitmaps. */
2999 interesting_names = BITMAP_ALLOC (NULL);
3000 interesting_names1 = BITMAP_ALLOC (NULL);
3002 calculate_dominance_info (CDI_DOMINATORS);
3003 cfg_altered = false;
3005 /* First phase. Eliminate degenerate PHIs via a dominator
3006 walk of the CFG.
3008 Experiments have indicated that we generally get better
3009 compile-time behavior by visiting blocks in the first
3010 phase in dominator order. Presumably this is because walking
3011 in dominator order leaves fewer PHIs for later examination
3012 by the worklist phase. */
3013 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
3015 /* Second phase. Eliminate second order degenerate PHIs as well
3016 as trivial copies or constant initializations identified by
3017 the first phase or this phase. Basically we keep iterating
3018 until our set of INTERESTING_NAMEs is empty. */
3019 while (!bitmap_empty_p (interesting_names))
3021 unsigned int i;
3022 bitmap_iterator bi;
3024 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3025 changed during the loop. Copy it to another bitmap and
3026 use that. */
3027 bitmap_copy (interesting_names1, interesting_names);
3029 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
3031 tree name = ssa_name (i);
3033 /* Ignore SSA_NAMEs that have been released because
3034 their defining statement was deleted (unreachable). */
3035 if (name)
3036 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3037 interesting_names);
3041 if (cfg_altered)
3043 free_dominance_info (CDI_DOMINATORS);
3044 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3045 if (current_loops)
3046 loops_state_set (LOOPS_NEED_FIXUP);
3049 /* Propagation of const and copies may make some EH edges dead. Purge
3050 such edges from the CFG as needed. */
3051 if (!bitmap_empty_p (need_eh_cleanup))
3053 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
3054 BITMAP_FREE (need_eh_cleanup);
3057 BITMAP_FREE (interesting_names);
3058 BITMAP_FREE (interesting_names1);
3059 return 0;
3062 struct gimple_opt_pass pass_phi_only_cprop =
3065 GIMPLE_PASS,
3066 "phicprop", /* name */
3067 OPTGROUP_NONE, /* optinfo_flags */
3068 gate_dominator, /* gate */
3069 eliminate_degenerate_phis, /* execute */
3070 NULL, /* sub */
3071 NULL, /* next */
3072 0, /* static_pass_number */
3073 TV_TREE_PHI_CPROP, /* tv_id */
3074 PROP_cfg | PROP_ssa, /* properties_required */
3075 0, /* properties_provided */
3076 0, /* properties_destroyed */
3077 0, /* todo_flags_start */
3078 TODO_cleanup_cfg
3079 | TODO_ggc_collect
3080 | TODO_verify_ssa
3081 | TODO_verify_stmts
3082 | TODO_update_ssa /* todo_flags_finish */