1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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/>. */
23 #include "coretypes.h"
24 #include "hash-table.h"
29 #include "double-int.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
42 #include "hard-reg-set.h"
45 #include "dominance.h"
48 #include "basic-block.h"
51 #include "gimple-pretty-print.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-fold.h"
56 #include "gimple-expr.h"
59 #include "gimple-iterator.h"
60 #include "gimple-ssa.h"
62 #include "tree-phinodes.h"
63 #include "ssa-iterators.h"
64 #include "stringpool.h"
65 #include "tree-ssanames.h"
66 #include "tree-into-ssa.h"
68 #include "tree-pass.h"
69 #include "tree-ssa-propagate.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "langhooks.h"
73 #include "tree-ssa-scopedtables.h"
74 #include "tree-ssa-threadedge.h"
75 #include "tree-ssa-dom.h"
78 #include "tree-cfgcleanup.h"
80 /* This file implements optimizations on the dominator tree. */
82 /* Representation of a "naked" right-hand-side expression, to be used
83 in recording available expressions in the expression hash table. */
100 struct { tree rhs
; } single
;
101 struct { enum tree_code op
; tree opnd
; } unary
;
102 struct { enum tree_code op
; tree opnd0
, opnd1
; } binary
;
103 struct { enum tree_code op
; tree opnd0
, opnd1
, opnd2
; } ternary
;
104 struct { gcall
*fn_from
; bool pure
; size_t nargs
; tree
*args
; } call
;
105 struct { size_t nargs
; tree
*args
; } phi
;
109 /* Structure for recording known values of a conditional expression
110 at the exits from its block. */
112 typedef struct cond_equivalence_s
114 struct hashable_expr cond
;
119 /* Structure for recording edge equivalences as well as any pending
120 edge redirections during the dominator optimizer.
122 Computing and storing the edge equivalences instead of creating
123 them on-demand can save significant amounts of time, particularly
124 for pathological cases involving switch statements.
126 These structures live for a single iteration of the dominator
127 optimizer in the edge's AUX field. At the end of an iteration we
128 free each of these structures and update the AUX field to point
129 to any requested redirection target (the code for updating the
130 CFG and SSA graph for edge redirection expects redirection edge
131 targets to be in the AUX field for each edge. */
135 /* If this edge creates a simple equivalence, the LHS and RHS of
136 the equivalence will be stored here. */
140 /* Traversing an edge may also indicate one or more particular conditions
141 are true or false. */
142 vec
<cond_equivalence
> cond_equivalences
;
145 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
146 expressions it enters into the hash table along with a marker entry
147 (null). When we finish processing the block, we pop off entries and
148 remove the expressions from the global hash table until we hit the
150 typedef struct expr_hash_elt
* expr_hash_elt_t
;
152 static vec
<std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> > avail_exprs_stack
;
154 /* Structure for entries in the expression hash table. */
158 /* The value (lhs) of this expression. */
161 /* The expression (rhs) we want to record. */
162 struct hashable_expr expr
;
164 /* The virtual operand associated with the nearest dominating stmt
165 loading from or storing to expr. */
168 /* The hash value for RHS. */
171 /* A unique stamp, typically the address of the hash
172 element itself, used in removing entries from the table. */
173 struct expr_hash_elt
*stamp
;
176 /* Hashtable helpers. */
178 static bool hashable_expr_equal_p (const struct hashable_expr
*,
179 const struct hashable_expr
*);
180 static void free_expr_hash_elt (void *);
182 struct expr_elt_hasher
184 typedef expr_hash_elt
*value_type
;
185 typedef expr_hash_elt
*compare_type
;
186 static inline hashval_t
hash (const value_type
&);
187 static inline bool equal (const value_type
&, const compare_type
&);
188 static inline void remove (value_type
&);
192 expr_elt_hasher::hash (const value_type
&p
)
198 expr_elt_hasher::equal (const value_type
&p1
, const compare_type
&p2
)
200 const struct hashable_expr
*expr1
= &p1
->expr
;
201 const struct expr_hash_elt
*stamp1
= p1
->stamp
;
202 const struct hashable_expr
*expr2
= &p2
->expr
;
203 const struct expr_hash_elt
*stamp2
= p2
->stamp
;
205 /* This case should apply only when removing entries from the table. */
206 if (stamp1
== stamp2
)
209 if (p1
->hash
!= p2
->hash
)
212 /* In case of a collision, both RHS have to be identical and have the
213 same VUSE operands. */
214 if (hashable_expr_equal_p (expr1
, expr2
)
215 && types_compatible_p (expr1
->type
, expr2
->type
))
221 /* Delete an expr_hash_elt and reclaim its storage. */
224 expr_elt_hasher::remove (value_type
&element
)
226 free_expr_hash_elt (element
);
229 /* Hash table with expressions made available during the renaming process.
230 When an assignment of the form X_i = EXPR is found, the statement is
231 stored in this table. If the same expression EXPR is later found on the
232 RHS of another statement, it is replaced with X_i (thus performing
233 global redundancy elimination). Similarly as we pass through conditionals
234 we record the conditional itself as having either a true or false value
236 static hash_table
<expr_elt_hasher
> *avail_exprs
;
238 /* Unwindable const/copy equivalences. */
239 static const_and_copies
*const_and_copies
;
241 /* Track whether or not we have changed the control flow graph. */
242 static bool cfg_altered
;
244 /* Bitmap of blocks that have had EH statements cleaned. We should
245 remove their dead edges eventually. */
246 static bitmap need_eh_cleanup
;
247 static vec
<gimple
> need_noreturn_fixup
;
249 /* Statistics for dominator optimizations. */
253 long num_exprs_considered
;
259 static struct opt_stats_d opt_stats
;
261 /* Local functions. */
262 static void optimize_stmt (basic_block
, gimple_stmt_iterator
);
263 static tree
lookup_avail_expr (gimple
, bool);
264 static hashval_t
avail_expr_hash (const void *);
265 static void htab_statistics (FILE *,
266 const hash_table
<expr_elt_hasher
> &);
267 static void record_cond (cond_equivalence
*);
268 static void record_equality (tree
, tree
);
269 static void record_equivalences_from_phis (basic_block
);
270 static void record_equivalences_from_incoming_edge (basic_block
);
271 static void eliminate_redundant_computations (gimple_stmt_iterator
*);
272 static void record_equivalences_from_stmt (gimple
, int);
273 static void remove_local_expressions_from_table (void);
274 static edge
single_incoming_edge_ignoring_loop_edges (basic_block
);
277 /* Given a statement STMT, initialize the hash table element pointed to
281 initialize_hash_element (gimple stmt
, tree lhs
,
282 struct expr_hash_elt
*element
)
284 enum gimple_code code
= gimple_code (stmt
);
285 struct hashable_expr
*expr
= &element
->expr
;
287 if (code
== GIMPLE_ASSIGN
)
289 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
291 switch (get_gimple_rhs_class (subcode
))
293 case GIMPLE_SINGLE_RHS
:
294 expr
->kind
= EXPR_SINGLE
;
295 expr
->type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
296 expr
->ops
.single
.rhs
= gimple_assign_rhs1 (stmt
);
298 case GIMPLE_UNARY_RHS
:
299 expr
->kind
= EXPR_UNARY
;
300 expr
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
301 if (CONVERT_EXPR_CODE_P (subcode
))
303 expr
->ops
.unary
.op
= subcode
;
304 expr
->ops
.unary
.opnd
= gimple_assign_rhs1 (stmt
);
306 case GIMPLE_BINARY_RHS
:
307 expr
->kind
= EXPR_BINARY
;
308 expr
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
309 expr
->ops
.binary
.op
= subcode
;
310 expr
->ops
.binary
.opnd0
= gimple_assign_rhs1 (stmt
);
311 expr
->ops
.binary
.opnd1
= gimple_assign_rhs2 (stmt
);
313 case GIMPLE_TERNARY_RHS
:
314 expr
->kind
= EXPR_TERNARY
;
315 expr
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
316 expr
->ops
.ternary
.op
= subcode
;
317 expr
->ops
.ternary
.opnd0
= gimple_assign_rhs1 (stmt
);
318 expr
->ops
.ternary
.opnd1
= gimple_assign_rhs2 (stmt
);
319 expr
->ops
.ternary
.opnd2
= gimple_assign_rhs3 (stmt
);
325 else if (code
== GIMPLE_COND
)
327 expr
->type
= boolean_type_node
;
328 expr
->kind
= EXPR_BINARY
;
329 expr
->ops
.binary
.op
= gimple_cond_code (stmt
);
330 expr
->ops
.binary
.opnd0
= gimple_cond_lhs (stmt
);
331 expr
->ops
.binary
.opnd1
= gimple_cond_rhs (stmt
);
333 else if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
335 size_t nargs
= gimple_call_num_args (call_stmt
);
338 gcc_assert (gimple_call_lhs (call_stmt
));
340 expr
->type
= TREE_TYPE (gimple_call_lhs (call_stmt
));
341 expr
->kind
= EXPR_CALL
;
342 expr
->ops
.call
.fn_from
= call_stmt
;
344 if (gimple_call_flags (call_stmt
) & (ECF_CONST
| ECF_PURE
))
345 expr
->ops
.call
.pure
= true;
347 expr
->ops
.call
.pure
= false;
349 expr
->ops
.call
.nargs
= nargs
;
350 expr
->ops
.call
.args
= XCNEWVEC (tree
, nargs
);
351 for (i
= 0; i
< nargs
; i
++)
352 expr
->ops
.call
.args
[i
] = gimple_call_arg (call_stmt
, i
);
354 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
356 expr
->type
= TREE_TYPE (gimple_switch_index (swtch_stmt
));
357 expr
->kind
= EXPR_SINGLE
;
358 expr
->ops
.single
.rhs
= gimple_switch_index (swtch_stmt
);
360 else if (code
== GIMPLE_GOTO
)
362 expr
->type
= TREE_TYPE (gimple_goto_dest (stmt
));
363 expr
->kind
= EXPR_SINGLE
;
364 expr
->ops
.single
.rhs
= gimple_goto_dest (stmt
);
366 else if (code
== GIMPLE_PHI
)
368 size_t nargs
= gimple_phi_num_args (stmt
);
371 expr
->type
= TREE_TYPE (gimple_phi_result (stmt
));
372 expr
->kind
= EXPR_PHI
;
373 expr
->ops
.phi
.nargs
= nargs
;
374 expr
->ops
.phi
.args
= XCNEWVEC (tree
, nargs
);
376 for (i
= 0; i
< nargs
; i
++)
377 expr
->ops
.phi
.args
[i
] = gimple_phi_arg_def (stmt
, i
);
383 element
->vop
= gimple_vuse (stmt
);
384 element
->hash
= avail_expr_hash (element
);
385 element
->stamp
= element
;
388 /* Given a conditional expression COND as a tree, initialize
389 a hashable_expr expression EXPR. The conditional must be a
390 comparison or logical negation. A constant or a variable is
394 initialize_expr_from_cond (tree cond
, struct hashable_expr
*expr
)
396 expr
->type
= boolean_type_node
;
398 if (COMPARISON_CLASS_P (cond
))
400 expr
->kind
= EXPR_BINARY
;
401 expr
->ops
.binary
.op
= TREE_CODE (cond
);
402 expr
->ops
.binary
.opnd0
= TREE_OPERAND (cond
, 0);
403 expr
->ops
.binary
.opnd1
= TREE_OPERAND (cond
, 1);
405 else if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
)
407 expr
->kind
= EXPR_UNARY
;
408 expr
->ops
.unary
.op
= TRUTH_NOT_EXPR
;
409 expr
->ops
.unary
.opnd
= TREE_OPERAND (cond
, 0);
415 /* Given a hashable_expr expression EXPR and an LHS,
416 initialize the hash table element pointed to by ELEMENT. */
419 initialize_hash_element_from_expr (struct hashable_expr
*expr
,
421 struct expr_hash_elt
*element
)
423 element
->expr
= *expr
;
425 element
->vop
= NULL_TREE
;
426 element
->hash
= avail_expr_hash (element
);
427 element
->stamp
= element
;
430 /* Compare two hashable_expr structures for equivalence.
431 They are considered equivalent when the the expressions
432 they denote must necessarily be equal. The logic is intended
433 to follow that of operand_equal_p in fold-const.c */
436 hashable_expr_equal_p (const struct hashable_expr
*expr0
,
437 const struct hashable_expr
*expr1
)
439 tree type0
= expr0
->type
;
440 tree type1
= expr1
->type
;
442 /* If either type is NULL, there is nothing to check. */
443 if ((type0
== NULL_TREE
) ^ (type1
== NULL_TREE
))
446 /* If both types don't have the same signedness, precision, and mode,
447 then we can't consider them equal. */
449 && (TREE_CODE (type0
) == ERROR_MARK
450 || TREE_CODE (type1
) == ERROR_MARK
451 || TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
)
452 || TYPE_PRECISION (type0
) != TYPE_PRECISION (type1
)
453 || TYPE_MODE (type0
) != TYPE_MODE (type1
)))
456 if (expr0
->kind
!= expr1
->kind
)
462 return operand_equal_p (expr0
->ops
.single
.rhs
,
463 expr1
->ops
.single
.rhs
, 0);
466 if (expr0
->ops
.unary
.op
!= expr1
->ops
.unary
.op
)
469 if ((CONVERT_EXPR_CODE_P (expr0
->ops
.unary
.op
)
470 || expr0
->ops
.unary
.op
== NON_LVALUE_EXPR
)
471 && TYPE_UNSIGNED (expr0
->type
) != TYPE_UNSIGNED (expr1
->type
))
474 return operand_equal_p (expr0
->ops
.unary
.opnd
,
475 expr1
->ops
.unary
.opnd
, 0);
478 if (expr0
->ops
.binary
.op
!= expr1
->ops
.binary
.op
)
481 if (operand_equal_p (expr0
->ops
.binary
.opnd0
,
482 expr1
->ops
.binary
.opnd0
, 0)
483 && operand_equal_p (expr0
->ops
.binary
.opnd1
,
484 expr1
->ops
.binary
.opnd1
, 0))
487 /* For commutative ops, allow the other order. */
488 return (commutative_tree_code (expr0
->ops
.binary
.op
)
489 && operand_equal_p (expr0
->ops
.binary
.opnd0
,
490 expr1
->ops
.binary
.opnd1
, 0)
491 && operand_equal_p (expr0
->ops
.binary
.opnd1
,
492 expr1
->ops
.binary
.opnd0
, 0));
495 if (expr0
->ops
.ternary
.op
!= expr1
->ops
.ternary
.op
496 || !operand_equal_p (expr0
->ops
.ternary
.opnd2
,
497 expr1
->ops
.ternary
.opnd2
, 0))
500 if (operand_equal_p (expr0
->ops
.ternary
.opnd0
,
501 expr1
->ops
.ternary
.opnd0
, 0)
502 && operand_equal_p (expr0
->ops
.ternary
.opnd1
,
503 expr1
->ops
.ternary
.opnd1
, 0))
506 /* For commutative ops, allow the other order. */
507 return (commutative_ternary_tree_code (expr0
->ops
.ternary
.op
)
508 && operand_equal_p (expr0
->ops
.ternary
.opnd0
,
509 expr1
->ops
.ternary
.opnd1
, 0)
510 && operand_equal_p (expr0
->ops
.ternary
.opnd1
,
511 expr1
->ops
.ternary
.opnd0
, 0));
517 /* If the calls are to different functions, then they
518 clearly cannot be equal. */
519 if (!gimple_call_same_target_p (expr0
->ops
.call
.fn_from
,
520 expr1
->ops
.call
.fn_from
))
523 if (! expr0
->ops
.call
.pure
)
526 if (expr0
->ops
.call
.nargs
!= expr1
->ops
.call
.nargs
)
529 for (i
= 0; i
< expr0
->ops
.call
.nargs
; i
++)
530 if (! operand_equal_p (expr0
->ops
.call
.args
[i
],
531 expr1
->ops
.call
.args
[i
], 0))
534 if (stmt_could_throw_p (expr0
->ops
.call
.fn_from
))
536 int lp0
= lookup_stmt_eh_lp (expr0
->ops
.call
.fn_from
);
537 int lp1
= lookup_stmt_eh_lp (expr1
->ops
.call
.fn_from
);
538 if ((lp0
> 0 || lp1
> 0) && lp0
!= lp1
)
549 if (expr0
->ops
.phi
.nargs
!= expr1
->ops
.phi
.nargs
)
552 for (i
= 0; i
< expr0
->ops
.phi
.nargs
; i
++)
553 if (! operand_equal_p (expr0
->ops
.phi
.args
[i
],
554 expr1
->ops
.phi
.args
[i
], 0))
565 /* Generate a hash value for a pair of expressions. This can be used
566 iteratively by passing a previous result in HSTATE.
568 The same hash value is always returned for a given pair of expressions,
569 regardless of the order in which they are presented. This is useful in
570 hashing the operands of commutative functions. */
576 add_expr_commutative (const_tree t1
, const_tree t2
, hash
&hstate
)
580 inchash::add_expr (t1
, one
);
581 inchash::add_expr (t2
, two
);
582 hstate
.add_commutative (one
, two
);
585 /* Compute a hash value for a hashable_expr value EXPR and a
586 previously accumulated hash value VAL. If two hashable_expr
587 values compare equal with hashable_expr_equal_p, they must
588 hash to the same value, given an identical value of VAL.
589 The logic is intended to follow inchash::add_expr in tree.c. */
592 add_hashable_expr (const struct hashable_expr
*expr
, hash
&hstate
)
597 inchash::add_expr (expr
->ops
.single
.rhs
, hstate
);
601 hstate
.add_object (expr
->ops
.unary
.op
);
603 /* Make sure to include signedness in the hash computation.
604 Don't hash the type, that can lead to having nodes which
605 compare equal according to operand_equal_p, but which
606 have different hash codes. */
607 if (CONVERT_EXPR_CODE_P (expr
->ops
.unary
.op
)
608 || expr
->ops
.unary
.op
== NON_LVALUE_EXPR
)
609 hstate
.add_int (TYPE_UNSIGNED (expr
->type
));
611 inchash::add_expr (expr
->ops
.unary
.opnd
, hstate
);
615 hstate
.add_object (expr
->ops
.binary
.op
);
616 if (commutative_tree_code (expr
->ops
.binary
.op
))
617 inchash::add_expr_commutative (expr
->ops
.binary
.opnd0
,
618 expr
->ops
.binary
.opnd1
, hstate
);
621 inchash::add_expr (expr
->ops
.binary
.opnd0
, hstate
);
622 inchash::add_expr (expr
->ops
.binary
.opnd1
, hstate
);
627 hstate
.add_object (expr
->ops
.ternary
.op
);
628 if (commutative_ternary_tree_code (expr
->ops
.ternary
.op
))
629 inchash::add_expr_commutative (expr
->ops
.ternary
.opnd0
,
630 expr
->ops
.ternary
.opnd1
, hstate
);
633 inchash::add_expr (expr
->ops
.ternary
.opnd0
, hstate
);
634 inchash::add_expr (expr
->ops
.ternary
.opnd1
, hstate
);
636 inchash::add_expr (expr
->ops
.ternary
.opnd2
, hstate
);
642 enum tree_code code
= CALL_EXPR
;
645 hstate
.add_object (code
);
646 fn_from
= expr
->ops
.call
.fn_from
;
647 if (gimple_call_internal_p (fn_from
))
648 hstate
.merge_hash ((hashval_t
) gimple_call_internal_fn (fn_from
));
650 inchash::add_expr (gimple_call_fn (fn_from
), hstate
);
651 for (i
= 0; i
< expr
->ops
.call
.nargs
; i
++)
652 inchash::add_expr (expr
->ops
.call
.args
[i
], hstate
);
660 for (i
= 0; i
< expr
->ops
.phi
.nargs
; i
++)
661 inchash::add_expr (expr
->ops
.phi
.args
[i
], hstate
);
672 /* Print a diagnostic dump of an expression hash table entry. */
675 print_expr_hash_elt (FILE * stream
, const struct expr_hash_elt
*element
)
677 fprintf (stream
, "STMT ");
681 print_generic_expr (stream
, element
->lhs
, 0);
682 fprintf (stream
, " = ");
685 switch (element
->expr
.kind
)
688 print_generic_expr (stream
, element
->expr
.ops
.single
.rhs
, 0);
692 fprintf (stream
, "%s ", get_tree_code_name (element
->expr
.ops
.unary
.op
));
693 print_generic_expr (stream
, element
->expr
.ops
.unary
.opnd
, 0);
697 print_generic_expr (stream
, element
->expr
.ops
.binary
.opnd0
, 0);
698 fprintf (stream
, " %s ", get_tree_code_name (element
->expr
.ops
.binary
.op
));
699 print_generic_expr (stream
, element
->expr
.ops
.binary
.opnd1
, 0);
703 fprintf (stream
, " %s <", get_tree_code_name (element
->expr
.ops
.ternary
.op
));
704 print_generic_expr (stream
, element
->expr
.ops
.ternary
.opnd0
, 0);
705 fputs (", ", stream
);
706 print_generic_expr (stream
, element
->expr
.ops
.ternary
.opnd1
, 0);
707 fputs (", ", stream
);
708 print_generic_expr (stream
, element
->expr
.ops
.ternary
.opnd2
, 0);
715 size_t nargs
= element
->expr
.ops
.call
.nargs
;
718 fn_from
= element
->expr
.ops
.call
.fn_from
;
719 if (gimple_call_internal_p (fn_from
))
720 fputs (internal_fn_name (gimple_call_internal_fn (fn_from
)),
723 print_generic_expr (stream
, gimple_call_fn (fn_from
), 0);
724 fprintf (stream
, " (");
725 for (i
= 0; i
< nargs
; i
++)
727 print_generic_expr (stream
, element
->expr
.ops
.call
.args
[i
], 0);
729 fprintf (stream
, ", ");
731 fprintf (stream
, ")");
738 size_t nargs
= element
->expr
.ops
.phi
.nargs
;
740 fprintf (stream
, "PHI <");
741 for (i
= 0; i
< nargs
; i
++)
743 print_generic_expr (stream
, element
->expr
.ops
.phi
.args
[i
], 0);
745 fprintf (stream
, ", ");
747 fprintf (stream
, ">");
754 fprintf (stream
, " with ");
755 print_generic_expr (stream
, element
->vop
, 0);
758 fprintf (stream
, "\n");
761 /* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
764 free_expr_hash_elt_contents (struct expr_hash_elt
*element
)
766 if (element
->expr
.kind
== EXPR_CALL
)
767 free (element
->expr
.ops
.call
.args
);
768 else if (element
->expr
.kind
== EXPR_PHI
)
769 free (element
->expr
.ops
.phi
.args
);
772 /* Delete an expr_hash_elt and reclaim its storage. */
775 free_expr_hash_elt (void *elt
)
777 struct expr_hash_elt
*element
= ((struct expr_hash_elt
*)elt
);
778 free_expr_hash_elt_contents (element
);
782 /* Allocate an EDGE_INFO for edge E and attach it to E.
783 Return the new EDGE_INFO structure. */
785 static struct edge_info
*
786 allocate_edge_info (edge e
)
788 struct edge_info
*edge_info
;
790 edge_info
= XCNEW (struct edge_info
);
796 /* Free all EDGE_INFO structures associated with edges in the CFG.
797 If a particular edge can be threaded, copy the redirection
798 target from the EDGE_INFO structure into the edge's AUX field
799 as required by code to update the CFG and SSA graph for
803 free_all_edge_infos (void)
809 FOR_EACH_BB_FN (bb
, cfun
)
811 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
813 struct edge_info
*edge_info
= (struct edge_info
*) e
->aux
;
817 edge_info
->cond_equivalences
.release ();
825 /* Build a cond_equivalence record indicating that the comparison
826 CODE holds between operands OP0 and OP1 and push it to **P. */
829 build_and_record_new_cond (enum tree_code code
,
831 vec
<cond_equivalence
> *p
)
834 struct hashable_expr
*cond
= &c
.cond
;
836 gcc_assert (TREE_CODE_CLASS (code
) == tcc_comparison
);
838 cond
->type
= boolean_type_node
;
839 cond
->kind
= EXPR_BINARY
;
840 cond
->ops
.binary
.op
= code
;
841 cond
->ops
.binary
.opnd0
= op0
;
842 cond
->ops
.binary
.opnd1
= op1
;
844 c
.value
= boolean_true_node
;
848 /* Record that COND is true and INVERTED is false into the edge information
849 structure. Also record that any conditions dominated by COND are true
852 For example, if a < b is true, then a <= b must also be true. */
855 record_conditions (struct edge_info
*edge_info
, tree cond
, tree inverted
)
860 if (!COMPARISON_CLASS_P (cond
))
863 op0
= TREE_OPERAND (cond
, 0);
864 op1
= TREE_OPERAND (cond
, 1);
866 switch (TREE_CODE (cond
))
870 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
872 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
873 &edge_info
->cond_equivalences
);
874 build_and_record_new_cond (LTGT_EXPR
, op0
, op1
,
875 &edge_info
->cond_equivalences
);
878 build_and_record_new_cond ((TREE_CODE (cond
) == LT_EXPR
879 ? LE_EXPR
: GE_EXPR
),
880 op0
, op1
, &edge_info
->cond_equivalences
);
881 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
882 &edge_info
->cond_equivalences
);
887 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
889 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
890 &edge_info
->cond_equivalences
);
895 if (FLOAT_TYPE_P (TREE_TYPE (op0
)))
897 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
898 &edge_info
->cond_equivalences
);
900 build_and_record_new_cond (LE_EXPR
, op0
, op1
,
901 &edge_info
->cond_equivalences
);
902 build_and_record_new_cond (GE_EXPR
, op0
, op1
,
903 &edge_info
->cond_equivalences
);
907 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
908 &edge_info
->cond_equivalences
);
909 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
910 &edge_info
->cond_equivalences
);
911 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
912 &edge_info
->cond_equivalences
);
913 build_and_record_new_cond (UNEQ_EXPR
, op0
, op1
,
914 &edge_info
->cond_equivalences
);
915 build_and_record_new_cond (UNLT_EXPR
, op0
, op1
,
916 &edge_info
->cond_equivalences
);
917 build_and_record_new_cond (UNGT_EXPR
, op0
, op1
,
918 &edge_info
->cond_equivalences
);
923 build_and_record_new_cond ((TREE_CODE (cond
) == UNLT_EXPR
924 ? UNLE_EXPR
: UNGE_EXPR
),
925 op0
, op1
, &edge_info
->cond_equivalences
);
926 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
927 &edge_info
->cond_equivalences
);
931 build_and_record_new_cond (UNLE_EXPR
, op0
, op1
,
932 &edge_info
->cond_equivalences
);
933 build_and_record_new_cond (UNGE_EXPR
, op0
, op1
,
934 &edge_info
->cond_equivalences
);
938 build_and_record_new_cond (NE_EXPR
, op0
, op1
,
939 &edge_info
->cond_equivalences
);
940 build_and_record_new_cond (ORDERED_EXPR
, op0
, op1
,
941 &edge_info
->cond_equivalences
);
948 /* Now store the original true and false conditions into the first
950 initialize_expr_from_cond (cond
, &c
.cond
);
951 c
.value
= boolean_true_node
;
952 edge_info
->cond_equivalences
.safe_push (c
);
954 /* It is possible for INVERTED to be the negation of a comparison,
955 and not a valid RHS or GIMPLE_COND condition. This happens because
956 invert_truthvalue may return such an expression when asked to invert
957 a floating-point comparison. These comparisons are not assumed to
958 obey the trichotomy law. */
959 initialize_expr_from_cond (inverted
, &c
.cond
);
960 c
.value
= boolean_false_node
;
961 edge_info
->cond_equivalences
.safe_push (c
);
964 /* We have finished optimizing BB, record any information implied by
965 taking a specific outgoing edge from BB. */
968 record_edge_info (basic_block bb
)
970 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
971 struct edge_info
*edge_info
;
973 if (! gsi_end_p (gsi
))
975 gimple stmt
= gsi_stmt (gsi
);
976 location_t loc
= gimple_location (stmt
);
978 if (gimple_code (stmt
) == GIMPLE_SWITCH
)
980 gswitch
*switch_stmt
= as_a
<gswitch
*> (stmt
);
981 tree index
= gimple_switch_index (switch_stmt
);
983 if (TREE_CODE (index
) == SSA_NAME
)
986 int n_labels
= gimple_switch_num_labels (switch_stmt
);
987 tree
*info
= XCNEWVEC (tree
, last_basic_block_for_fn (cfun
));
991 for (i
= 0; i
< n_labels
; i
++)
993 tree label
= gimple_switch_label (switch_stmt
, i
);
994 basic_block target_bb
= label_to_block (CASE_LABEL (label
));
995 if (CASE_HIGH (label
)
997 || info
[target_bb
->index
])
998 info
[target_bb
->index
] = error_mark_node
;
1000 info
[target_bb
->index
] = label
;
1003 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1005 basic_block target_bb
= e
->dest
;
1006 tree label
= info
[target_bb
->index
];
1008 if (label
!= NULL
&& label
!= error_mark_node
)
1010 tree x
= fold_convert_loc (loc
, TREE_TYPE (index
),
1012 edge_info
= allocate_edge_info (e
);
1013 edge_info
->lhs
= index
;
1021 /* A COND_EXPR may create equivalences too. */
1022 if (gimple_code (stmt
) == GIMPLE_COND
)
1027 tree op0
= gimple_cond_lhs (stmt
);
1028 tree op1
= gimple_cond_rhs (stmt
);
1029 enum tree_code code
= gimple_cond_code (stmt
);
1031 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
1033 /* Special case comparing booleans against a constant as we
1034 know the value of OP0 on both arms of the branch. i.e., we
1035 can record an equivalence for OP0 rather than COND. */
1036 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
1037 && TREE_CODE (op0
) == SSA_NAME
1038 && TREE_CODE (TREE_TYPE (op0
)) == BOOLEAN_TYPE
1039 && is_gimple_min_invariant (op1
))
1041 if (code
== EQ_EXPR
)
1043 edge_info
= allocate_edge_info (true_edge
);
1044 edge_info
->lhs
= op0
;
1045 edge_info
->rhs
= (integer_zerop (op1
)
1046 ? boolean_false_node
1047 : boolean_true_node
);
1049 edge_info
= allocate_edge_info (false_edge
);
1050 edge_info
->lhs
= op0
;
1051 edge_info
->rhs
= (integer_zerop (op1
)
1053 : boolean_false_node
);
1057 edge_info
= allocate_edge_info (true_edge
);
1058 edge_info
->lhs
= op0
;
1059 edge_info
->rhs
= (integer_zerop (op1
)
1061 : boolean_false_node
);
1063 edge_info
= allocate_edge_info (false_edge
);
1064 edge_info
->lhs
= op0
;
1065 edge_info
->rhs
= (integer_zerop (op1
)
1066 ? boolean_false_node
1067 : boolean_true_node
);
1070 else if (is_gimple_min_invariant (op0
)
1071 && (TREE_CODE (op1
) == SSA_NAME
1072 || is_gimple_min_invariant (op1
)))
1074 tree cond
= build2 (code
, boolean_type_node
, op0
, op1
);
1075 tree inverted
= invert_truthvalue_loc (loc
, cond
);
1076 bool can_infer_simple_equiv
1077 = !(HONOR_SIGNED_ZEROS (op0
)
1078 && real_zerop (op0
));
1079 struct edge_info
*edge_info
;
1081 edge_info
= allocate_edge_info (true_edge
);
1082 record_conditions (edge_info
, cond
, inverted
);
1084 if (can_infer_simple_equiv
&& code
== EQ_EXPR
)
1086 edge_info
->lhs
= op1
;
1087 edge_info
->rhs
= op0
;
1090 edge_info
= allocate_edge_info (false_edge
);
1091 record_conditions (edge_info
, inverted
, cond
);
1093 if (can_infer_simple_equiv
&& TREE_CODE (inverted
) == EQ_EXPR
)
1095 edge_info
->lhs
= op1
;
1096 edge_info
->rhs
= op0
;
1100 else if (TREE_CODE (op0
) == SSA_NAME
1101 && (TREE_CODE (op1
) == SSA_NAME
1102 || is_gimple_min_invariant (op1
)))
1104 tree cond
= build2 (code
, boolean_type_node
, op0
, op1
);
1105 tree inverted
= invert_truthvalue_loc (loc
, cond
);
1106 bool can_infer_simple_equiv
1107 = !(HONOR_SIGNED_ZEROS (op1
)
1108 && (TREE_CODE (op1
) == SSA_NAME
|| real_zerop (op1
)));
1109 struct edge_info
*edge_info
;
1111 edge_info
= allocate_edge_info (true_edge
);
1112 record_conditions (edge_info
, cond
, inverted
);
1114 if (can_infer_simple_equiv
&& code
== EQ_EXPR
)
1116 edge_info
->lhs
= op0
;
1117 edge_info
->rhs
= op1
;
1120 edge_info
= allocate_edge_info (false_edge
);
1121 record_conditions (edge_info
, inverted
, cond
);
1123 if (can_infer_simple_equiv
&& TREE_CODE (inverted
) == EQ_EXPR
)
1125 edge_info
->lhs
= op0
;
1126 edge_info
->rhs
= op1
;
1131 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1136 class dom_opt_dom_walker
: public dom_walker
1139 dom_opt_dom_walker (cdi_direction direction
)
1140 : dom_walker (direction
), m_dummy_cond (NULL
) {}
1142 virtual void before_dom_children (basic_block
);
1143 virtual void after_dom_children (basic_block
);
1146 void thread_across_edge (edge
);
1148 gcond
*m_dummy_cond
;
1151 /* Jump threading, redundancy elimination and const/copy propagation.
1153 This pass may expose new symbols that need to be renamed into SSA. For
1154 every new symbol exposed, its corresponding bit will be set in
1159 const pass_data pass_data_dominator
=
1161 GIMPLE_PASS
, /* type */
1163 OPTGROUP_NONE
, /* optinfo_flags */
1164 TV_TREE_SSA_DOMINATOR_OPTS
, /* tv_id */
1165 ( PROP_cfg
| PROP_ssa
), /* properties_required */
1166 0, /* properties_provided */
1167 0, /* properties_destroyed */
1168 0, /* todo_flags_start */
1169 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
1172 class pass_dominator
: public gimple_opt_pass
1175 pass_dominator (gcc::context
*ctxt
)
1176 : gimple_opt_pass (pass_data_dominator
, ctxt
)
1179 /* opt_pass methods: */
1180 opt_pass
* clone () { return new pass_dominator (m_ctxt
); }
1181 virtual bool gate (function
*) { return flag_tree_dom
!= 0; }
1182 virtual unsigned int execute (function
*);
1184 }; // class pass_dominator
1187 pass_dominator::execute (function
*fun
)
1189 memset (&opt_stats
, 0, sizeof (opt_stats
));
1191 /* Create our hash tables. */
1192 avail_exprs
= new hash_table
<expr_elt_hasher
> (1024);
1193 avail_exprs_stack
.create (20);
1194 const_and_copies
= new class const_and_copies (dump_file
, dump_flags
);
1195 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1196 need_noreturn_fixup
.create (0);
1198 calculate_dominance_info (CDI_DOMINATORS
);
1199 cfg_altered
= false;
1201 /* We need to know loop structures in order to avoid destroying them
1202 in jump threading. Note that we still can e.g. thread through loop
1203 headers to an exit edge, or through loop header to the loop body, assuming
1204 that we update the loop info.
1206 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
1207 to several overly conservative bail-outs in jump threading, case
1208 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
1209 missing. We should improve jump threading in future then
1210 LOOPS_HAVE_PREHEADERS won't be needed here. */
1211 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
| LOOPS_HAVE_SIMPLE_LATCHES
);
1213 /* Initialize the value-handle array. */
1214 threadedge_initialize_values ();
1216 /* We need accurate information regarding back edges in the CFG
1217 for jump threading; this may include back edges that are not part of
1219 mark_dfs_back_edges ();
1221 /* Recursively walk the dominator tree optimizing statements. */
1222 dom_opt_dom_walker (CDI_DOMINATORS
).walk (fun
->cfg
->x_entry_block_ptr
);
1225 gimple_stmt_iterator gsi
;
1227 FOR_EACH_BB_FN (bb
, fun
)
1229 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1230 update_stmt_if_modified (gsi_stmt (gsi
));
1234 /* If we exposed any new variables, go ahead and put them into
1235 SSA form now, before we handle jump threading. This simplifies
1236 interactions between rewriting of _DECL nodes into SSA form
1237 and rewriting SSA_NAME nodes into SSA form after block
1238 duplication and CFG manipulation. */
1239 update_ssa (TODO_update_ssa
);
1241 free_all_edge_infos ();
1243 /* Thread jumps, creating duplicate blocks as needed. */
1244 cfg_altered
|= thread_through_all_blocks (first_pass_instance
);
1247 free_dominance_info (CDI_DOMINATORS
);
1249 /* Removal of statements may make some EH edges dead. Purge
1250 such edges from the CFG as needed. */
1251 if (!bitmap_empty_p (need_eh_cleanup
))
1256 /* Jump threading may have created forwarder blocks from blocks
1257 needing EH cleanup; the new successor of these blocks, which
1258 has inherited from the original block, needs the cleanup.
1259 Don't clear bits in the bitmap, as that can break the bitmap
1261 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup
, 0, i
, bi
)
1263 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, i
);
1266 while (single_succ_p (bb
)
1267 && (single_succ_edge (bb
)->flags
& EDGE_EH
) == 0)
1268 bb
= single_succ (bb
);
1269 if (bb
== EXIT_BLOCK_PTR_FOR_FN (fun
))
1271 if ((unsigned) bb
->index
!= i
)
1272 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
1275 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
1276 bitmap_clear (need_eh_cleanup
);
1279 /* Fixup stmts that became noreturn calls. This may require splitting
1280 blocks and thus isn't possible during the dominator walk or before
1281 jump threading finished. Do this in reverse order so we don't
1282 inadvertedly remove a stmt we want to fixup by visiting a dominating
1283 now noreturn call first. */
1284 while (!need_noreturn_fixup
.is_empty ())
1286 gimple stmt
= need_noreturn_fixup
.pop ();
1287 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1289 fprintf (dump_file
, "Fixing up noreturn call ");
1290 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1291 fprintf (dump_file
, "\n");
1293 fixup_noreturn_call (stmt
);
1296 statistics_counter_event (fun
, "Redundant expressions eliminated",
1298 statistics_counter_event (fun
, "Constants propagated",
1299 opt_stats
.num_const_prop
);
1300 statistics_counter_event (fun
, "Copies propagated",
1301 opt_stats
.num_copy_prop
);
1303 /* Debugging dumps. */
1304 if (dump_file
&& (dump_flags
& TDF_STATS
))
1305 dump_dominator_optimization_stats (dump_file
);
1307 loop_optimizer_finalize ();
1309 /* Delete our main hashtable. */
1313 /* Free asserted bitmaps and stacks. */
1314 BITMAP_FREE (need_eh_cleanup
);
1315 need_noreturn_fixup
.release ();
1316 avail_exprs_stack
.release ();
1317 delete const_and_copies
;
1319 /* Free the value-handle array. */
1320 threadedge_finalize_values ();
1328 make_pass_dominator (gcc::context
*ctxt
)
1330 return new pass_dominator (ctxt
);
1334 /* Given a conditional statement CONDSTMT, convert the
1335 condition to a canonical form. */
1338 canonicalize_comparison (gcond
*condstmt
)
1342 enum tree_code code
;
1344 gcc_assert (gimple_code (condstmt
) == GIMPLE_COND
);
1346 op0
= gimple_cond_lhs (condstmt
);
1347 op1
= gimple_cond_rhs (condstmt
);
1349 code
= gimple_cond_code (condstmt
);
1351 /* If it would be profitable to swap the operands, then do so to
1352 canonicalize the statement, enabling better optimization.
1354 By placing canonicalization of such expressions here we
1355 transparently keep statements in canonical form, even
1356 when the statement is modified. */
1357 if (tree_swap_operands_p (op0
, op1
, false))
1359 /* For relationals we need to swap the operands
1360 and change the code. */
1366 code
= swap_tree_comparison (code
);
1368 gimple_cond_set_code (condstmt
, code
);
1369 gimple_cond_set_lhs (condstmt
, op1
);
1370 gimple_cond_set_rhs (condstmt
, op0
);
1372 update_stmt (condstmt
);
1377 /* Initialize local stacks for this optimizer and record equivalences
1378 upon entry to BB. Equivalences can come from the edge traversed to
1379 reach BB or they may come from PHI nodes at the start of BB. */
1381 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
1382 LIMIT entries left in LOCALs. */
1385 remove_local_expressions_from_table (void)
1387 /* Remove all the expressions made available in this block. */
1388 while (avail_exprs_stack
.length () > 0)
1390 std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> victim
1391 = avail_exprs_stack
.pop ();
1392 expr_hash_elt
**slot
;
1394 if (victim
.first
== NULL
)
1397 /* This must precede the actual removal from the hash table,
1398 as ELEMENT and the table entry may share a call argument
1399 vector which will be freed during removal. */
1400 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1402 fprintf (dump_file
, "<<<< ");
1403 print_expr_hash_elt (dump_file
, victim
.first
);
1406 slot
= avail_exprs
->find_slot (victim
.first
, NO_INSERT
);
1407 gcc_assert (slot
&& *slot
== victim
.first
);
1408 if (victim
.second
!= NULL
)
1410 free_expr_hash_elt (*slot
);
1411 *slot
= victim
.second
;
1414 avail_exprs
->clear_slot (slot
);
1418 /* A trivial wrapper so that we can present the generic jump
1419 threading code with a simple API for simplifying statements. */
1421 simplify_stmt_for_jump_threading (gimple stmt
,
1422 gimple within_stmt ATTRIBUTE_UNUSED
)
1424 return lookup_avail_expr (stmt
, false);
1427 /* Record into the equivalence tables any equivalences implied by
1428 traversing edge E (which are cached in E->aux).
1430 Callers are responsible for managing the unwinding markers. */
1432 record_temporary_equivalences (edge e
)
1435 struct edge_info
*edge_info
= (struct edge_info
*) e
->aux
;
1437 /* If we have info associated with this edge, record it into
1438 our equivalence tables. */
1441 cond_equivalence
*eq
;
1442 tree lhs
= edge_info
->lhs
;
1443 tree rhs
= edge_info
->rhs
;
1445 /* If we have a simple NAME = VALUE equivalence, record it. */
1446 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1447 const_and_copies
->record_const_or_copy (lhs
, rhs
);
1449 /* If we have 0 = COND or 1 = COND equivalences, record them
1450 into our expression hash tables. */
1451 for (i
= 0; edge_info
->cond_equivalences
.iterate (i
, &eq
); ++i
)
1456 /* Wrapper for common code to attempt to thread an edge. For example,
1457 it handles lazily building the dummy condition and the bookkeeping
1458 when jump threading is successful. */
1461 dom_opt_dom_walker::thread_across_edge (edge e
)
1465 gimple_build_cond (NE_EXPR
,
1466 integer_zero_node
, integer_zero_node
,
1469 /* Push a marker on both stacks so we can unwind the tables back to their
1471 avail_exprs_stack
.safe_push
1472 (std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> (NULL
, NULL
));
1473 const_and_copies
->push_marker ();
1475 /* Traversing E may result in equivalences we can utilize. */
1476 record_temporary_equivalences (e
);
1478 /* With all the edge equivalences in the tables, go ahead and attempt
1479 to thread through E->dest. */
1480 ::thread_across_edge (m_dummy_cond
, e
, false,
1482 simplify_stmt_for_jump_threading
);
1484 /* And restore the various tables to their state before
1485 we threaded this edge.
1487 XXX The code in tree-ssa-threadedge.c will restore the state of
1488 the const_and_copies table. We we just have to restore the expression
1490 remove_local_expressions_from_table ();
1493 /* PHI nodes can create equivalences too.
1495 Ignoring any alternatives which are the same as the result, if
1496 all the alternatives are equal, then the PHI node creates an
1500 record_equivalences_from_phis (basic_block bb
)
1504 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1506 gphi
*phi
= gsi
.phi ();
1508 tree lhs
= gimple_phi_result (phi
);
1512 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1514 tree t
= gimple_phi_arg_def (phi
, i
);
1516 /* Ignore alternatives which are the same as our LHS. Since
1517 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1518 can simply compare pointers. */
1523 if (TREE_CODE (t
) == SSA_NAME
)
1525 tree tmp
= SSA_NAME_VALUE (t
);
1529 /* If we have not processed an alternative yet, then set
1530 RHS to this alternative. */
1533 /* If we have processed an alternative (stored in RHS), then
1534 see if it is equal to this one. If it isn't, then stop
1536 else if (! operand_equal_for_phi_arg_p (rhs
, t
))
1540 /* If we had no interesting alternatives, then all the RHS alternatives
1541 must have been the same as LHS. */
1545 /* If we managed to iterate through each PHI alternative without
1546 breaking out of the loop, then we have a PHI which may create
1547 a useful equivalence. We do not need to record unwind data for
1548 this, since this is a true assignment and not an equivalence
1549 inferred from a comparison. All uses of this ssa name are dominated
1550 by this assignment, so unwinding just costs time and space. */
1551 if (i
== gimple_phi_num_args (phi
)
1552 && may_propagate_copy (lhs
, rhs
))
1553 set_ssa_name_value (lhs
, rhs
);
1557 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1558 return that edge. Otherwise return NULL. */
1560 single_incoming_edge_ignoring_loop_edges (basic_block bb
)
1566 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1568 /* A loop back edge can be identified by the destination of
1569 the edge dominating the source of the edge. */
1570 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
1573 /* If we have already seen a non-loop edge, then we must have
1574 multiple incoming non-loop edges and thus we return NULL. */
1578 /* This is the first non-loop incoming edge we have found. Record
1586 /* Record any equivalences created by the incoming edge to BB. If BB
1587 has more than one incoming edge, then no equivalence is created. */
1590 record_equivalences_from_incoming_edge (basic_block bb
)
1594 struct edge_info
*edge_info
;
1596 /* If our parent block ended with a control statement, then we may be
1597 able to record some equivalences based on which outgoing edge from
1598 the parent was followed. */
1599 parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1601 e
= single_incoming_edge_ignoring_loop_edges (bb
);
1603 /* If we had a single incoming edge from our parent block, then enter
1604 any data associated with the edge into our tables. */
1605 if (e
&& e
->src
== parent
)
1609 edge_info
= (struct edge_info
*) e
->aux
;
1613 tree lhs
= edge_info
->lhs
;
1614 tree rhs
= edge_info
->rhs
;
1615 cond_equivalence
*eq
;
1618 record_equality (lhs
, rhs
);
1620 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1621 set via a widening type conversion, then we may be able to record
1622 additional equivalences. */
1624 && TREE_CODE (lhs
) == SSA_NAME
1625 && is_gimple_constant (rhs
)
1626 && TREE_CODE (rhs
) == INTEGER_CST
)
1628 gimple defstmt
= SSA_NAME_DEF_STMT (lhs
);
1631 && is_gimple_assign (defstmt
)
1632 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt
)))
1634 tree old_rhs
= gimple_assign_rhs1 (defstmt
);
1636 /* If the conversion widens the original value and
1637 the constant is in the range of the type of OLD_RHS,
1638 then convert the constant and record the equivalence.
1640 Note that int_fits_type_p does not check the precision
1641 if the upper and lower bounds are OK. */
1642 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs
))
1643 && (TYPE_PRECISION (TREE_TYPE (lhs
))
1644 > TYPE_PRECISION (TREE_TYPE (old_rhs
)))
1645 && int_fits_type_p (rhs
, TREE_TYPE (old_rhs
)))
1647 tree newval
= fold_convert (TREE_TYPE (old_rhs
), rhs
);
1648 record_equality (old_rhs
, newval
);
1653 for (i
= 0; edge_info
->cond_equivalences
.iterate (i
, &eq
); ++i
)
1659 /* Dump SSA statistics on FILE. */
1662 dump_dominator_optimization_stats (FILE *file
)
1664 fprintf (file
, "Total number of statements: %6ld\n\n",
1665 opt_stats
.num_stmts
);
1666 fprintf (file
, "Exprs considered for dominator optimizations: %6ld\n",
1667 opt_stats
.num_exprs_considered
);
1669 fprintf (file
, "\nHash table statistics:\n");
1671 fprintf (file
, " avail_exprs: ");
1672 htab_statistics (file
, *avail_exprs
);
1676 /* Dump SSA statistics on stderr. */
1679 debug_dominator_optimization_stats (void)
1681 dump_dominator_optimization_stats (stderr
);
1685 /* Dump statistics for the hash table HTAB. */
1688 htab_statistics (FILE *file
, const hash_table
<expr_elt_hasher
> &htab
)
1690 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1691 (long) htab
.size (),
1692 (long) htab
.elements (),
1693 htab
.collisions ());
1697 /* Enter condition equivalence into the expression hash table.
1698 This indicates that a conditional expression has a known
1702 record_cond (cond_equivalence
*p
)
1704 struct expr_hash_elt
*element
= XCNEW (struct expr_hash_elt
);
1705 expr_hash_elt
**slot
;
1707 initialize_hash_element_from_expr (&p
->cond
, p
->value
, element
);
1709 slot
= avail_exprs
->find_slot_with_hash (element
, element
->hash
, INSERT
);
1714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1716 fprintf (dump_file
, "1>>> ");
1717 print_expr_hash_elt (dump_file
, element
);
1720 avail_exprs_stack
.safe_push
1721 (std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> (element
, NULL
));
1724 free_expr_hash_elt (element
);
1727 /* Return the loop depth of the basic block of the defining statement of X.
1728 This number should not be treated as absolutely correct because the loop
1729 information may not be completely up-to-date when dom runs. However, it
1730 will be relatively correct, and as more passes are taught to keep loop info
1731 up to date, the result will become more and more accurate. */
1734 loop_depth_of_name (tree x
)
1739 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1740 if (TREE_CODE (x
) != SSA_NAME
)
1743 /* Otherwise return the loop depth of the defining statement's bb.
1744 Note that there may not actually be a bb for this statement, if the
1745 ssa_name is live on entry. */
1746 defstmt
= SSA_NAME_DEF_STMT (x
);
1747 defbb
= gimple_bb (defstmt
);
1751 return bb_loop_depth (defbb
);
1754 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1755 This constrains the cases in which we may treat this as assignment. */
1758 record_equality (tree x
, tree y
)
1760 tree prev_x
= NULL
, prev_y
= NULL
;
1762 if (tree_swap_operands_p (x
, y
, false))
1765 /* Most of the time tree_swap_operands_p does what we want. But there
1766 are cases where we know one operand is better for copy propagation than
1767 the other. Given no other code cares about ordering of equality
1768 comparison operators for that purpose, we just handle the special cases
1770 if (TREE_CODE (x
) == SSA_NAME
&& TREE_CODE (y
) == SSA_NAME
)
1772 /* If one operand is a single use operand, then make it
1773 X. This will preserve its single use properly and if this
1774 conditional is eliminated, the computation of X can be
1775 eliminated as well. */
1776 if (has_single_use (y
) && ! has_single_use (x
))
1779 if (TREE_CODE (x
) == SSA_NAME
)
1780 prev_x
= SSA_NAME_VALUE (x
);
1781 if (TREE_CODE (y
) == SSA_NAME
)
1782 prev_y
= SSA_NAME_VALUE (y
);
1784 /* If one of the previous values is invariant, or invariant in more loops
1785 (by depth), then use that.
1786 Otherwise it doesn't matter which value we choose, just so
1787 long as we canonicalize on one value. */
1788 if (is_gimple_min_invariant (y
))
1790 else if (is_gimple_min_invariant (x
)
1791 /* ??? When threading over backedges the following is important
1792 for correctness. See PR61757. */
1793 || (loop_depth_of_name (x
) < loop_depth_of_name (y
)))
1794 prev_x
= x
, x
= y
, y
= prev_x
, prev_x
= prev_y
;
1795 else if (prev_x
&& is_gimple_min_invariant (prev_x
))
1796 x
= y
, y
= prev_x
, prev_x
= prev_y
;
1800 /* After the swapping, we must have one SSA_NAME. */
1801 if (TREE_CODE (x
) != SSA_NAME
)
1804 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1805 variable compared against zero. If we're honoring signed zeros,
1806 then we cannot record this value unless we know that the value is
1808 if (HONOR_SIGNED_ZEROS (x
)
1809 && (TREE_CODE (y
) != REAL_CST
1810 || REAL_VALUES_EQUAL (dconst0
, TREE_REAL_CST (y
))))
1813 const_and_copies
->record_const_or_copy (x
, y
, prev_x
);
1816 /* Returns true when STMT is a simple iv increment. It detects the
1817 following situation:
1819 i_1 = phi (..., i_2)
1820 i_2 = i_1 +/- ... */
1823 simple_iv_increment_p (gimple stmt
)
1825 enum tree_code code
;
1830 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1833 lhs
= gimple_assign_lhs (stmt
);
1834 if (TREE_CODE (lhs
) != SSA_NAME
)
1837 code
= gimple_assign_rhs_code (stmt
);
1838 if (code
!= PLUS_EXPR
1839 && code
!= MINUS_EXPR
1840 && code
!= POINTER_PLUS_EXPR
)
1843 preinc
= gimple_assign_rhs1 (stmt
);
1844 if (TREE_CODE (preinc
) != SSA_NAME
)
1847 phi
= SSA_NAME_DEF_STMT (preinc
);
1848 if (gimple_code (phi
) != GIMPLE_PHI
)
1851 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1852 if (gimple_phi_arg_def (phi
, i
) == lhs
)
1858 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1859 known value for that SSA_NAME (or NULL if no value is known).
1861 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1862 successors of BB. */
1865 cprop_into_successor_phis (basic_block bb
)
1870 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1875 /* If this is an abnormal edge, then we do not want to copy propagate
1876 into the PHI alternative associated with this edge. */
1877 if (e
->flags
& EDGE_ABNORMAL
)
1880 gsi
= gsi_start_phis (e
->dest
);
1881 if (gsi_end_p (gsi
))
1884 /* We may have an equivalence associated with this edge. While
1885 we can not propagate it into non-dominated blocks, we can
1886 propagate them into PHIs in non-dominated blocks. */
1888 /* Push the unwind marker so we can reset the const and copies
1889 table back to its original state after processing this edge. */
1890 const_and_copies
->push_marker ();
1892 /* Extract and record any simple NAME = VALUE equivalences.
1894 Don't bother with [01] = COND equivalences, they're not useful
1896 struct edge_info
*edge_info
= (struct edge_info
*) e
->aux
;
1899 tree lhs
= edge_info
->lhs
;
1900 tree rhs
= edge_info
->rhs
;
1902 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1903 const_and_copies
->record_const_or_copy (lhs
, rhs
);
1907 for ( ; !gsi_end_p (gsi
); gsi_next (&gsi
))
1910 use_operand_p orig_p
;
1912 gphi
*phi
= gsi
.phi ();
1914 /* The alternative may be associated with a constant, so verify
1915 it is an SSA_NAME before doing anything with it. */
1916 orig_p
= gimple_phi_arg_imm_use_ptr (phi
, indx
);
1917 orig_val
= get_use_from_ptr (orig_p
);
1918 if (TREE_CODE (orig_val
) != SSA_NAME
)
1921 /* If we have *ORIG_P in our constant/copy table, then replace
1922 ORIG_P with its value in our constant/copy table. */
1923 new_val
= SSA_NAME_VALUE (orig_val
);
1925 && new_val
!= orig_val
1926 && (TREE_CODE (new_val
) == SSA_NAME
1927 || is_gimple_min_invariant (new_val
))
1928 && may_propagate_copy (orig_val
, new_val
))
1929 propagate_value (orig_p
, new_val
);
1932 const_and_copies
->pop_to_marker ();
1937 dom_opt_dom_walker::before_dom_children (basic_block bb
)
1939 gimple_stmt_iterator gsi
;
1941 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1942 fprintf (dump_file
, "\n\nOptimizing block #%d\n\n", bb
->index
);
1944 /* Push a marker on the stacks of local information so that we know how
1945 far to unwind when we finalize this block. */
1946 avail_exprs_stack
.safe_push
1947 (std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> (NULL
, NULL
));
1948 const_and_copies
->push_marker ();
1950 record_equivalences_from_incoming_edge (bb
);
1952 /* PHI nodes can create equivalences too. */
1953 record_equivalences_from_phis (bb
);
1955 /* Create equivalences from redundant PHIs. PHIs are only truly
1956 redundant when they exist in the same block, so push another
1957 marker and unwind right afterwards. */
1958 avail_exprs_stack
.safe_push
1959 (std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> (NULL
, NULL
));
1960 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1961 eliminate_redundant_computations (&gsi
);
1962 remove_local_expressions_from_table ();
1964 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1965 optimize_stmt (bb
, gsi
);
1967 /* Now prepare to process dominated blocks. */
1968 record_edge_info (bb
);
1969 cprop_into_successor_phis (bb
);
1972 /* We have finished processing the dominator children of BB, perform
1973 any finalization actions in preparation for leaving this node in
1974 the dominator tree. */
1977 dom_opt_dom_walker::after_dom_children (basic_block bb
)
1981 /* If we have an outgoing edge to a block with multiple incoming and
1982 outgoing edges, then we may be able to thread the edge, i.e., we
1983 may be able to statically determine which of the outgoing edges
1984 will be traversed when the incoming edge from BB is traversed. */
1985 if (single_succ_p (bb
)
1986 && (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
) == 0
1987 && potentially_threadable_block (single_succ (bb
)))
1989 thread_across_edge (single_succ_edge (bb
));
1991 else if ((last
= last_stmt (bb
))
1992 && gimple_code (last
) == GIMPLE_COND
1993 && EDGE_COUNT (bb
->succs
) == 2
1994 && (EDGE_SUCC (bb
, 0)->flags
& EDGE_ABNORMAL
) == 0
1995 && (EDGE_SUCC (bb
, 1)->flags
& EDGE_ABNORMAL
) == 0)
1997 edge true_edge
, false_edge
;
1999 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
2001 /* Only try to thread the edge if it reaches a target block with
2002 more than one predecessor and more than one successor. */
2003 if (potentially_threadable_block (true_edge
->dest
))
2004 thread_across_edge (true_edge
);
2006 /* Similarly for the ELSE arm. */
2007 if (potentially_threadable_block (false_edge
->dest
))
2008 thread_across_edge (false_edge
);
2012 /* These remove expressions local to BB from the tables. */
2013 remove_local_expressions_from_table ();
2014 const_and_copies
->pop_to_marker ();
2017 /* Search for redundant computations in STMT. If any are found, then
2018 replace them with the variable holding the result of the computation.
2020 If safe, record this expression into the available expression hash
2024 eliminate_redundant_computations (gimple_stmt_iterator
* gsi
)
2030 bool assigns_var_p
= false;
2032 gimple stmt
= gsi_stmt (*gsi
);
2034 if (gimple_code (stmt
) == GIMPLE_PHI
)
2035 def
= gimple_phi_result (stmt
);
2037 def
= gimple_get_lhs (stmt
);
2039 /* Certain expressions on the RHS can be optimized away, but can not
2040 themselves be entered into the hash tables. */
2042 || TREE_CODE (def
) != SSA_NAME
2043 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def
)
2044 || gimple_vdef (stmt
)
2045 /* Do not record equivalences for increments of ivs. This would create
2046 overlapping live ranges for a very questionable gain. */
2047 || simple_iv_increment_p (stmt
))
2050 /* Check if the expression has been computed before. */
2051 cached_lhs
= lookup_avail_expr (stmt
, insert
);
2053 opt_stats
.num_exprs_considered
++;
2055 /* Get the type of the expression we are trying to optimize. */
2056 if (is_gimple_assign (stmt
))
2058 expr_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
2059 assigns_var_p
= true;
2061 else if (gimple_code (stmt
) == GIMPLE_COND
)
2062 expr_type
= boolean_type_node
;
2063 else if (is_gimple_call (stmt
))
2065 gcc_assert (gimple_call_lhs (stmt
));
2066 expr_type
= TREE_TYPE (gimple_call_lhs (stmt
));
2067 assigns_var_p
= true;
2069 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
2070 expr_type
= TREE_TYPE (gimple_switch_index (swtch_stmt
));
2071 else if (gimple_code (stmt
) == GIMPLE_PHI
)
2072 /* We can't propagate into a phi, so the logic below doesn't apply.
2073 Instead record an equivalence between the cached LHS and the
2074 PHI result of this statement, provided they are in the same block.
2075 This should be sufficient to kill the redundant phi. */
2077 if (def
&& cached_lhs
)
2078 const_and_copies
->record_const_or_copy (def
, cached_lhs
);
2087 /* It is safe to ignore types here since we have already done
2088 type checking in the hashing and equality routines. In fact
2089 type checking here merely gets in the way of constant
2090 propagation. Also, make sure that it is safe to propagate
2091 CACHED_LHS into the expression in STMT. */
2092 if ((TREE_CODE (cached_lhs
) != SSA_NAME
2094 || useless_type_conversion_p (expr_type
, TREE_TYPE (cached_lhs
))))
2095 || may_propagate_copy_into_stmt (stmt
, cached_lhs
))
2097 gcc_checking_assert (TREE_CODE (cached_lhs
) == SSA_NAME
2098 || is_gimple_min_invariant (cached_lhs
));
2100 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2102 fprintf (dump_file
, " Replaced redundant expr '");
2103 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
2104 fprintf (dump_file
, "' with '");
2105 print_generic_expr (dump_file
, cached_lhs
, dump_flags
);
2106 fprintf (dump_file
, "'\n");
2112 && !useless_type_conversion_p (expr_type
, TREE_TYPE (cached_lhs
)))
2113 cached_lhs
= fold_convert (expr_type
, cached_lhs
);
2115 propagate_tree_value_into_stmt (gsi
, cached_lhs
);
2117 /* Since it is always necessary to mark the result as modified,
2118 perhaps we should move this into propagate_tree_value_into_stmt
2120 gimple_set_modified (gsi_stmt (*gsi
), true);
2124 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
2125 the available expressions table or the const_and_copies table.
2126 Detect and record those equivalences. */
2127 /* We handle only very simple copy equivalences here. The heavy
2128 lifing is done by eliminate_redundant_computations. */
2131 record_equivalences_from_stmt (gimple stmt
, int may_optimize_p
)
2134 enum tree_code lhs_code
;
2136 gcc_assert (is_gimple_assign (stmt
));
2138 lhs
= gimple_assign_lhs (stmt
);
2139 lhs_code
= TREE_CODE (lhs
);
2141 if (lhs_code
== SSA_NAME
2142 && gimple_assign_single_p (stmt
))
2144 tree rhs
= gimple_assign_rhs1 (stmt
);
2146 /* If the RHS of the assignment is a constant or another variable that
2147 may be propagated, register it in the CONST_AND_COPIES table. We
2148 do not need to record unwind data for this, since this is a true
2149 assignment and not an equivalence inferred from a comparison. All
2150 uses of this ssa name are dominated by this assignment, so unwinding
2151 just costs time and space. */
2153 && (TREE_CODE (rhs
) == SSA_NAME
2154 || is_gimple_min_invariant (rhs
)))
2157 if (TREE_CODE (rhs
) == SSA_NAME
)
2159 tree tmp
= SSA_NAME_VALUE (rhs
);
2160 rhs
= tmp
? tmp
: rhs
;
2163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2165 fprintf (dump_file
, "==== ASGN ");
2166 print_generic_expr (dump_file
, lhs
, 0);
2167 fprintf (dump_file
, " = ");
2168 print_generic_expr (dump_file
, rhs
, 0);
2169 fprintf (dump_file
, "\n");
2172 set_ssa_name_value (lhs
, rhs
);
2176 /* Make sure we can propagate &x + CST. */
2177 if (lhs_code
== SSA_NAME
2178 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
2179 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
2180 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
)
2182 tree op0
= gimple_assign_rhs1 (stmt
);
2183 tree op1
= gimple_assign_rhs2 (stmt
);
2185 = build_fold_addr_expr (fold_build2 (MEM_REF
,
2186 TREE_TYPE (TREE_TYPE (op0
)),
2188 fold_convert (ptr_type_node
,
2190 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2192 fprintf (dump_file
, "==== ASGN ");
2193 print_generic_expr (dump_file
, lhs
, 0);
2194 fprintf (dump_file
, " = ");
2195 print_generic_expr (dump_file
, new_rhs
, 0);
2196 fprintf (dump_file
, "\n");
2199 set_ssa_name_value (lhs
, new_rhs
);
2202 /* A memory store, even an aliased store, creates a useful
2203 equivalence. By exchanging the LHS and RHS, creating suitable
2204 vops and recording the result in the available expression table,
2205 we may be able to expose more redundant loads. */
2206 if (!gimple_has_volatile_ops (stmt
)
2207 && gimple_references_memory_p (stmt
)
2208 && gimple_assign_single_p (stmt
)
2209 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
2210 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
2211 && !is_gimple_reg (lhs
))
2213 tree rhs
= gimple_assign_rhs1 (stmt
);
2216 /* Build a new statement with the RHS and LHS exchanged. */
2217 if (TREE_CODE (rhs
) == SSA_NAME
)
2219 /* NOTE tuples. The call to gimple_build_assign below replaced
2220 a call to build_gimple_modify_stmt, which did not set the
2221 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2222 may cause an SSA validation failure, as the LHS may be a
2223 default-initialized name and should have no definition. I'm
2224 a bit dubious of this, as the artificial statement that we
2225 generate here may in fact be ill-formed, but it is simply
2226 used as an internal device in this pass, and never becomes
2228 gimple defstmt
= SSA_NAME_DEF_STMT (rhs
);
2229 new_stmt
= gimple_build_assign (rhs
, lhs
);
2230 SSA_NAME_DEF_STMT (rhs
) = defstmt
;
2233 new_stmt
= gimple_build_assign (rhs
, lhs
);
2235 gimple_set_vuse (new_stmt
, gimple_vdef (stmt
));
2237 /* Finally enter the statement into the available expression
2239 lookup_avail_expr (new_stmt
, true);
2243 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2244 CONST_AND_COPIES. */
2247 cprop_operand (gimple stmt
, use_operand_p op_p
)
2250 tree op
= USE_FROM_PTR (op_p
);
2252 /* If the operand has a known constant value or it is known to be a
2253 copy of some other variable, use the value or copy stored in
2254 CONST_AND_COPIES. */
2255 val
= SSA_NAME_VALUE (op
);
2256 if (val
&& val
!= op
)
2258 /* Do not replace hard register operands in asm statements. */
2259 if (gimple_code (stmt
) == GIMPLE_ASM
2260 && !may_propagate_copy_into_asm (op
))
2263 /* Certain operands are not allowed to be copy propagated due
2264 to their interaction with exception handling and some GCC
2266 if (!may_propagate_copy (op
, val
))
2269 /* Do not propagate copies into BIVs.
2270 See PR23821 and PR62217 for how this can disturb IV and
2271 number of iteration analysis. */
2272 if (TREE_CODE (val
) != INTEGER_CST
)
2274 gimple def
= SSA_NAME_DEF_STMT (op
);
2275 if (gimple_code (def
) == GIMPLE_PHI
2276 && gimple_bb (def
)->loop_father
->header
== gimple_bb (def
))
2281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2283 fprintf (dump_file
, " Replaced '");
2284 print_generic_expr (dump_file
, op
, dump_flags
);
2285 fprintf (dump_file
, "' with %s '",
2286 (TREE_CODE (val
) != SSA_NAME
? "constant" : "variable"));
2287 print_generic_expr (dump_file
, val
, dump_flags
);
2288 fprintf (dump_file
, "'\n");
2291 if (TREE_CODE (val
) != SSA_NAME
)
2292 opt_stats
.num_const_prop
++;
2294 opt_stats
.num_copy_prop
++;
2296 propagate_value (op_p
, val
);
2298 /* And note that we modified this statement. This is now
2299 safe, even if we changed virtual operands since we will
2300 rescan the statement and rewrite its operands again. */
2301 gimple_set_modified (stmt
, true);
2305 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2306 known value for that SSA_NAME (or NULL if no value is known).
2308 Propagate values from CONST_AND_COPIES into the uses, vuses and
2309 vdef_ops of STMT. */
2312 cprop_into_stmt (gimple stmt
)
2317 FOR_EACH_SSA_USE_OPERAND (op_p
, stmt
, iter
, SSA_OP_USE
)
2318 cprop_operand (stmt
, op_p
);
2321 /* Optimize the statement pointed to by iterator SI.
2323 We try to perform some simplistic global redundancy elimination and
2324 constant propagation:
2326 1- To detect global redundancy, we keep track of expressions that have
2327 been computed in this block and its dominators. If we find that the
2328 same expression is computed more than once, we eliminate repeated
2329 computations by using the target of the first one.
2331 2- Constant values and copy assignments. This is used to do very
2332 simplistic constant and copy propagation. When a constant or copy
2333 assignment is found, we map the value on the RHS of the assignment to
2334 the variable in the LHS in the CONST_AND_COPIES table. */
2337 optimize_stmt (basic_block bb
, gimple_stmt_iterator si
)
2339 gimple stmt
, old_stmt
;
2340 bool may_optimize_p
;
2341 bool modified_p
= false;
2344 old_stmt
= stmt
= gsi_stmt (si
);
2345 was_noreturn
= is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
);
2347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2349 fprintf (dump_file
, "Optimizing statement ");
2350 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2353 if (gimple_code (stmt
) == GIMPLE_COND
)
2354 canonicalize_comparison (as_a
<gcond
*> (stmt
));
2356 update_stmt_if_modified (stmt
);
2357 opt_stats
.num_stmts
++;
2359 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2360 cprop_into_stmt (stmt
);
2362 /* If the statement has been modified with constant replacements,
2363 fold its RHS before checking for redundant computations. */
2364 if (gimple_modified_p (stmt
))
2368 /* Try to fold the statement making sure that STMT is kept
2370 if (fold_stmt (&si
))
2372 stmt
= gsi_stmt (si
);
2373 gimple_set_modified (stmt
, true);
2375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2377 fprintf (dump_file
, " Folded to: ");
2378 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2382 /* We only need to consider cases that can yield a gimple operand. */
2383 if (gimple_assign_single_p (stmt
))
2384 rhs
= gimple_assign_rhs1 (stmt
);
2385 else if (gimple_code (stmt
) == GIMPLE_GOTO
)
2386 rhs
= gimple_goto_dest (stmt
);
2387 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
2388 /* This should never be an ADDR_EXPR. */
2389 rhs
= gimple_switch_index (swtch_stmt
);
2391 if (rhs
&& TREE_CODE (rhs
) == ADDR_EXPR
)
2392 recompute_tree_invariant_for_addr_expr (rhs
);
2394 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2395 even if fold_stmt updated the stmt already and thus cleared
2396 gimple_modified_p flag on it. */
2400 /* Check for redundant computations. Do this optimization only
2401 for assignments that have no volatile ops and conditionals. */
2402 may_optimize_p
= (!gimple_has_side_effects (stmt
)
2403 && (is_gimple_assign (stmt
)
2404 || (is_gimple_call (stmt
)
2405 && gimple_call_lhs (stmt
) != NULL_TREE
)
2406 || gimple_code (stmt
) == GIMPLE_COND
2407 || gimple_code (stmt
) == GIMPLE_SWITCH
));
2411 if (gimple_code (stmt
) == GIMPLE_CALL
)
2413 /* Resolve __builtin_constant_p. If it hasn't been
2414 folded to integer_one_node by now, it's fairly
2415 certain that the value simply isn't constant. */
2416 tree callee
= gimple_call_fndecl (stmt
);
2418 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
2419 && DECL_FUNCTION_CODE (callee
) == BUILT_IN_CONSTANT_P
)
2421 propagate_tree_value_into_stmt (&si
, integer_zero_node
);
2422 stmt
= gsi_stmt (si
);
2426 update_stmt_if_modified (stmt
);
2427 eliminate_redundant_computations (&si
);
2428 stmt
= gsi_stmt (si
);
2430 /* Perform simple redundant store elimination. */
2431 if (gimple_assign_single_p (stmt
)
2432 && TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
2434 tree lhs
= gimple_assign_lhs (stmt
);
2435 tree rhs
= gimple_assign_rhs1 (stmt
);
2438 if (TREE_CODE (rhs
) == SSA_NAME
)
2440 tree tem
= SSA_NAME_VALUE (rhs
);
2444 /* Build a new statement with the RHS and LHS exchanged. */
2445 if (TREE_CODE (rhs
) == SSA_NAME
)
2447 gimple defstmt
= SSA_NAME_DEF_STMT (rhs
);
2448 new_stmt
= gimple_build_assign (rhs
, lhs
);
2449 SSA_NAME_DEF_STMT (rhs
) = defstmt
;
2452 new_stmt
= gimple_build_assign (rhs
, lhs
);
2453 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
2454 cached_lhs
= lookup_avail_expr (new_stmt
, false);
2456 && rhs
== cached_lhs
)
2458 basic_block bb
= gimple_bb (stmt
);
2459 unlink_stmt_vdef (stmt
);
2460 if (gsi_remove (&si
, true))
2462 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
2463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2464 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2466 release_defs (stmt
);
2472 /* Record any additional equivalences created by this statement. */
2473 if (is_gimple_assign (stmt
))
2474 record_equivalences_from_stmt (stmt
, may_optimize_p
);
2476 /* If STMT is a COND_EXPR and it was modified, then we may know
2477 where it goes. If that is the case, then mark the CFG as altered.
2479 This will cause us to later call remove_unreachable_blocks and
2480 cleanup_tree_cfg when it is safe to do so. It is not safe to
2481 clean things up here since removal of edges and such can trigger
2482 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2485 That's all fine and good, except that once SSA_NAMEs are released
2486 to the manager, we must not call create_ssa_name until all references
2487 to released SSA_NAMEs have been eliminated.
2489 All references to the deleted SSA_NAMEs can not be eliminated until
2490 we remove unreachable blocks.
2492 We can not remove unreachable blocks until after we have completed
2493 any queued jump threading.
2495 We can not complete any queued jump threads until we have taken
2496 appropriate variables out of SSA form. Taking variables out of
2497 SSA form can call create_ssa_name and thus we lose.
2499 Ultimately I suspect we're going to need to change the interface
2500 into the SSA_NAME manager. */
2501 if (gimple_modified_p (stmt
) || modified_p
)
2505 update_stmt_if_modified (stmt
);
2507 if (gimple_code (stmt
) == GIMPLE_COND
)
2508 val
= fold_binary_loc (gimple_location (stmt
),
2509 gimple_cond_code (stmt
), boolean_type_node
,
2510 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
2511 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
2512 val
= gimple_switch_index (swtch_stmt
);
2514 if (val
&& TREE_CODE (val
) == INTEGER_CST
&& find_taken_edge (bb
, val
))
2517 /* If we simplified a statement in such a way as to be shown that it
2518 cannot trap, update the eh information and the cfg to match. */
2519 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
2521 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
2522 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2523 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2527 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
2528 need_noreturn_fixup
.safe_push (stmt
);
2532 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
2533 the desired memory state. */
2536 vuse_eq (ao_ref
*, tree vuse1
, unsigned int cnt
, void *data
)
2538 tree vuse2
= (tree
) data
;
2542 /* This bounds the stmt walks we perform on reference lookups
2543 to O(1) instead of O(N) where N is the number of dominating
2544 stores leading to a candidate. We re-use the SCCVN param
2545 for this as it is basically the same complexity. */
2546 if (cnt
> (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS
))
2552 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2553 If found, return its LHS. Otherwise insert STMT in the table and
2556 Also, when an expression is first inserted in the table, it is also
2557 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2558 we finish processing this block and its children. */
2561 lookup_avail_expr (gimple stmt
, bool insert
)
2563 expr_hash_elt
**slot
;
2566 struct expr_hash_elt element
;
2568 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2569 if (gimple_code (stmt
) == GIMPLE_PHI
)
2570 lhs
= gimple_phi_result (stmt
);
2572 lhs
= gimple_get_lhs (stmt
);
2574 initialize_hash_element (stmt
, lhs
, &element
);
2576 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2578 fprintf (dump_file
, "LKUP ");
2579 print_expr_hash_elt (dump_file
, &element
);
2582 /* Don't bother remembering constant assignments and copy operations.
2583 Constants and copy operations are handled by the constant/copy propagator
2584 in optimize_stmt. */
2585 if (element
.expr
.kind
== EXPR_SINGLE
2586 && (TREE_CODE (element
.expr
.ops
.single
.rhs
) == SSA_NAME
2587 || is_gimple_min_invariant (element
.expr
.ops
.single
.rhs
)))
2590 /* Finally try to find the expression in the main expression hash table. */
2591 slot
= avail_exprs
->find_slot (&element
, (insert
? INSERT
: NO_INSERT
));
2594 free_expr_hash_elt_contents (&element
);
2597 else if (*slot
== NULL
)
2599 struct expr_hash_elt
*element2
= XNEW (struct expr_hash_elt
);
2600 *element2
= element
;
2601 element2
->stamp
= element2
;
2604 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2606 fprintf (dump_file
, "2>>> ");
2607 print_expr_hash_elt (dump_file
, element2
);
2610 avail_exprs_stack
.safe_push
2611 (std::pair
<expr_hash_elt_t
, expr_hash_elt_t
> (element2
, NULL
));
2615 /* If we found a redundant memory operation do an alias walk to
2616 check if we can re-use it. */
2617 if (gimple_vuse (stmt
) != (*slot
)->vop
)
2619 tree vuse1
= (*slot
)->vop
;
2620 tree vuse2
= gimple_vuse (stmt
);
2621 /* If we have a load of a register and a candidate in the
2622 hash with vuse1 then try to reach its stmt by walking
2623 up the virtual use-def chain using walk_non_aliased_vuses.
2624 But don't do this when removing expressions from the hash. */
2626 if (!(vuse1
&& vuse2
2627 && gimple_assign_single_p (stmt
)
2628 && TREE_CODE (gimple_assign_lhs (stmt
)) == SSA_NAME
2629 && (ao_ref_init (&ref
, gimple_assign_rhs1 (stmt
)), true)
2630 && walk_non_aliased_vuses (&ref
, vuse2
,
2631 vuse_eq
, NULL
, NULL
, vuse1
) != NULL
))
2635 struct expr_hash_elt
*element2
= XNEW (struct expr_hash_elt
);
2636 *element2
= element
;
2637 element2
->stamp
= element2
;
2639 /* Insert the expr into the hash by replacing the current
2640 entry and recording the value to restore in the
2641 avail_exprs_stack. */
2642 avail_exprs_stack
.safe_push (std::make_pair (element2
, *slot
));
2644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2646 fprintf (dump_file
, "2>>> ");
2647 print_expr_hash_elt (dump_file
, *slot
);
2654 free_expr_hash_elt_contents (&element
);
2656 /* Extract the LHS of the assignment so that it can be used as the current
2657 definition of another variable. */
2660 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2661 use the value from the const_and_copies table. */
2662 if (TREE_CODE (lhs
) == SSA_NAME
)
2664 temp
= SSA_NAME_VALUE (lhs
);
2669 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2671 fprintf (dump_file
, "FIND: ");
2672 print_generic_expr (dump_file
, lhs
, 0);
2673 fprintf (dump_file
, "\n");
2679 /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2680 for expressions using the code of the expression and the SSA numbers of
2684 avail_expr_hash (const void *p
)
2686 const struct hashable_expr
*expr
= &((const struct expr_hash_elt
*)p
)->expr
;
2687 inchash::hash hstate
;
2689 inchash::add_hashable_expr (expr
, hstate
);
2691 return hstate
.end ();
2694 /* PHI-ONLY copy and constant propagation. This pass is meant to clean
2695 up degenerate PHIs created by or exposed by jump threading. */
2697 /* Given a statement STMT, which is either a PHI node or an assignment,
2698 remove it from the IL. */
2701 remove_stmt_or_phi (gimple stmt
)
2703 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
2705 if (gimple_code (stmt
) == GIMPLE_PHI
)
2706 remove_phi_node (&gsi
, true);
2709 gsi_remove (&gsi
, true);
2710 release_defs (stmt
);
2714 /* Given a statement STMT, which is either a PHI node or an assignment,
2715 return the "rhs" of the node, in the case of a non-degenerate
2716 phi, NULL is returned. */
2719 get_rhs_or_phi_arg (gimple stmt
)
2721 if (gimple_code (stmt
) == GIMPLE_PHI
)
2722 return degenerate_phi_result (as_a
<gphi
*> (stmt
));
2723 else if (gimple_assign_single_p (stmt
))
2724 return gimple_assign_rhs1 (stmt
);
2730 /* Given a statement STMT, which is either a PHI node or an assignment,
2731 return the "lhs" of the node. */
2734 get_lhs_or_phi_result (gimple stmt
)
2736 if (gimple_code (stmt
) == GIMPLE_PHI
)
2737 return gimple_phi_result (stmt
);
2738 else if (is_gimple_assign (stmt
))
2739 return gimple_assign_lhs (stmt
);
2744 /* Propagate RHS into all uses of LHS (when possible).
2746 RHS and LHS are derived from STMT, which is passed in solely so
2747 that we can remove it if propagation is successful.
2749 When propagating into a PHI node or into a statement which turns
2750 into a trivial copy or constant initialization, set the
2751 appropriate bit in INTERESTING_NAMEs so that we will visit those
2752 nodes as well in an effort to pick up secondary optimization
2756 propagate_rhs_into_lhs (gimple stmt
, tree lhs
, tree rhs
, bitmap interesting_names
)
2758 /* First verify that propagation is valid. */
2759 if (may_propagate_copy (lhs
, rhs
))
2761 use_operand_p use_p
;
2762 imm_use_iterator iter
;
2767 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2769 fprintf (dump_file
, " Replacing '");
2770 print_generic_expr (dump_file
, lhs
, dump_flags
);
2771 fprintf (dump_file
, "' with %s '",
2772 (TREE_CODE (rhs
) != SSA_NAME
? "constant" : "variable"));
2773 print_generic_expr (dump_file
, rhs
, dump_flags
);
2774 fprintf (dump_file
, "'\n");
2777 /* Walk over every use of LHS and try to replace the use with RHS.
2778 At this point the only reason why such a propagation would not
2779 be successful would be if the use occurs in an ASM_EXPR. */
2780 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
2782 /* Leave debug stmts alone. If we succeed in propagating
2783 all non-debug uses, we'll drop the DEF, and propagation
2784 into debug stmts will occur then. */
2785 if (gimple_debug_bind_p (use_stmt
))
2788 /* It's not always safe to propagate into an ASM_EXPR. */
2789 if (gimple_code (use_stmt
) == GIMPLE_ASM
2790 && ! may_propagate_copy_into_asm (lhs
))
2796 /* It's not ok to propagate into the definition stmt of RHS.
2798 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2799 g_67.1_6 = prephitmp.12_36;
2801 While this is strictly all dead code we do not want to
2802 deal with this here. */
2803 if (TREE_CODE (rhs
) == SSA_NAME
2804 && SSA_NAME_DEF_STMT (rhs
) == use_stmt
)
2811 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2813 fprintf (dump_file
, " Original statement:");
2814 print_gimple_stmt (dump_file
, use_stmt
, 0, dump_flags
);
2817 /* Propagate the RHS into this use of the LHS. */
2818 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
2819 propagate_value (use_p
, rhs
);
2821 /* Special cases to avoid useless calls into the folding
2822 routines, operand scanning, etc.
2824 Propagation into a PHI may cause the PHI to become
2825 a degenerate, so mark the PHI as interesting. No other
2826 actions are necessary. */
2827 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
2832 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2834 fprintf (dump_file
, " Updated statement:");
2835 print_gimple_stmt (dump_file
, use_stmt
, 0, dump_flags
);
2838 result
= get_lhs_or_phi_result (use_stmt
);
2839 bitmap_set_bit (interesting_names
, SSA_NAME_VERSION (result
));
2843 /* From this point onward we are propagating into a
2844 real statement. Folding may (or may not) be possible,
2845 we may expose new operands, expose dead EH edges,
2847 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2848 cannot fold a call that simplifies to a constant,
2849 because the GIMPLE_CALL must be replaced by a
2850 GIMPLE_ASSIGN, and there is no way to effect such a
2851 transformation in-place. We might want to consider
2852 using the more general fold_stmt here. */
2854 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
2855 fold_stmt_inplace (&gsi
);
2858 /* Sometimes propagation can expose new operands to the
2860 update_stmt (use_stmt
);
2863 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2865 fprintf (dump_file
, " Updated statement:");
2866 print_gimple_stmt (dump_file
, use_stmt
, 0, dump_flags
);
2869 /* If we replaced a variable index with a constant, then
2870 we would need to update the invariant flag for ADDR_EXPRs. */
2871 if (gimple_assign_single_p (use_stmt
)
2872 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ADDR_EXPR
)
2873 recompute_tree_invariant_for_addr_expr
2874 (gimple_assign_rhs1 (use_stmt
));
2876 /* If we cleaned up EH information from the statement,
2877 mark its containing block as needing EH cleanups. */
2878 if (maybe_clean_or_replace_eh_stmt (use_stmt
, use_stmt
))
2880 bitmap_set_bit (need_eh_cleanup
, gimple_bb (use_stmt
)->index
);
2881 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2882 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2885 /* Propagation may expose new trivial copy/constant propagation
2887 if (gimple_assign_single_p (use_stmt
)
2888 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
2889 && (TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == SSA_NAME
2890 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt
))))
2892 tree result
= get_lhs_or_phi_result (use_stmt
);
2893 bitmap_set_bit (interesting_names
, SSA_NAME_VERSION (result
));
2896 /* Propagation into these nodes may make certain edges in
2897 the CFG unexecutable. We want to identify them as PHI nodes
2898 at the destination of those unexecutable edges may become
2900 else if (gimple_code (use_stmt
) == GIMPLE_COND
2901 || gimple_code (use_stmt
) == GIMPLE_SWITCH
2902 || gimple_code (use_stmt
) == GIMPLE_GOTO
)
2906 if (gimple_code (use_stmt
) == GIMPLE_COND
)
2907 val
= fold_binary_loc (gimple_location (use_stmt
),
2908 gimple_cond_code (use_stmt
),
2910 gimple_cond_lhs (use_stmt
),
2911 gimple_cond_rhs (use_stmt
));
2912 else if (gimple_code (use_stmt
) == GIMPLE_SWITCH
)
2913 val
= gimple_switch_index (as_a
<gswitch
*> (use_stmt
));
2915 val
= gimple_goto_dest (use_stmt
);
2917 if (val
&& is_gimple_min_invariant (val
))
2919 basic_block bb
= gimple_bb (use_stmt
);
2920 edge te
= find_taken_edge (bb
, val
);
2923 gimple_stmt_iterator gsi
;
2926 /* Remove all outgoing edges except TE. */
2927 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
));)
2931 /* Mark all the PHI nodes at the destination of
2932 the unexecutable edge as interesting. */
2933 for (psi
= gsi_start_phis (e
->dest
);
2937 gphi
*phi
= psi
.phi ();
2939 tree result
= gimple_phi_result (phi
);
2940 int version
= SSA_NAME_VERSION (result
);
2942 bitmap_set_bit (interesting_names
, version
);
2945 te
->probability
+= e
->probability
;
2947 te
->count
+= e
->count
;
2955 gsi
= gsi_last_bb (gimple_bb (use_stmt
));
2956 gsi_remove (&gsi
, true);
2958 /* And fixup the flags on the single remaining edge. */
2959 te
->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
2960 te
->flags
&= ~EDGE_ABNORMAL
;
2961 te
->flags
|= EDGE_FALLTHRU
;
2962 if (te
->probability
> REG_BR_PROB_BASE
)
2963 te
->probability
= REG_BR_PROB_BASE
;
2968 /* Ensure there is nothing else to do. */
2969 gcc_assert (!all
|| has_zero_uses (lhs
));
2971 /* If we were able to propagate away all uses of LHS, then
2972 we can remove STMT. */
2974 remove_stmt_or_phi (stmt
);
2978 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2979 a statement that is a trivial copy or constant initialization.
2981 Attempt to eliminate T by propagating its RHS into all uses of
2982 its LHS. This may in turn set new bits in INTERESTING_NAMES
2983 for nodes we want to revisit later.
2985 All exit paths should clear INTERESTING_NAMES for the result
2989 eliminate_const_or_copy (gimple stmt
, bitmap interesting_names
)
2991 tree lhs
= get_lhs_or_phi_result (stmt
);
2993 int version
= SSA_NAME_VERSION (lhs
);
2995 /* If the LHS of this statement or PHI has no uses, then we can
2996 just eliminate it. This can occur if, for example, the PHI
2997 was created by block duplication due to threading and its only
2998 use was in the conditional at the end of the block which was
3000 if (has_zero_uses (lhs
))
3002 bitmap_clear_bit (interesting_names
, version
);
3003 remove_stmt_or_phi (stmt
);
3007 /* Get the RHS of the assignment or PHI node if the PHI is a
3009 rhs
= get_rhs_or_phi_arg (stmt
);
3012 bitmap_clear_bit (interesting_names
, version
);
3016 if (!virtual_operand_p (lhs
))
3017 propagate_rhs_into_lhs (stmt
, lhs
, rhs
, interesting_names
);
3021 imm_use_iterator iter
;
3022 use_operand_p use_p
;
3023 /* For virtual operands we have to propagate into all uses as
3024 otherwise we will create overlapping life-ranges. */
3025 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
3026 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3027 SET_USE (use_p
, rhs
);
3028 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
3029 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
3030 remove_stmt_or_phi (stmt
);
3033 /* Note that STMT may well have been deleted by now, so do
3034 not access it, instead use the saved version # to clear
3035 T's entry in the worklist. */
3036 bitmap_clear_bit (interesting_names
, version
);
3039 /* The first phase in degenerate PHI elimination.
3041 Eliminate the degenerate PHIs in BB, then recurse on the
3042 dominator children of BB. */
3045 eliminate_degenerate_phis_1 (basic_block bb
, bitmap interesting_names
)
3050 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3052 gphi
*phi
= gsi
.phi ();
3054 eliminate_const_or_copy (phi
, interesting_names
);
3057 /* Recurse into the dominator children of BB. */
3058 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
3060 son
= next_dom_son (CDI_DOMINATORS
, son
))
3061 eliminate_degenerate_phis_1 (son
, interesting_names
);
3065 /* A very simple pass to eliminate degenerate PHI nodes from the
3066 IL. This is meant to be fast enough to be able to be run several
3067 times in the optimization pipeline.
3069 Certain optimizations, particularly those which duplicate blocks
3070 or remove edges from the CFG can create or expose PHIs which are
3071 trivial copies or constant initializations.
3073 While we could pick up these optimizations in DOM or with the
3074 combination of copy-prop and CCP, those solutions are far too
3075 heavy-weight for our needs.
3077 This implementation has two phases so that we can efficiently
3078 eliminate the first order degenerate PHIs and second order
3081 The first phase performs a dominator walk to identify and eliminate
3082 the vast majority of the degenerate PHIs. When a degenerate PHI
3083 is identified and eliminated any affected statements or PHIs
3084 are put on a worklist.
3086 The second phase eliminates degenerate PHIs and trivial copies
3087 or constant initializations using the worklist. This is how we
3088 pick up the secondary optimization opportunities with minimal
3093 const pass_data pass_data_phi_only_cprop
=
3095 GIMPLE_PASS
, /* type */
3096 "phicprop", /* name */
3097 OPTGROUP_NONE
, /* optinfo_flags */
3098 TV_TREE_PHI_CPROP
, /* tv_id */
3099 ( PROP_cfg
| PROP_ssa
), /* properties_required */
3100 0, /* properties_provided */
3101 0, /* properties_destroyed */
3102 0, /* todo_flags_start */
3103 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
3106 class pass_phi_only_cprop
: public gimple_opt_pass
3109 pass_phi_only_cprop (gcc::context
*ctxt
)
3110 : gimple_opt_pass (pass_data_phi_only_cprop
, ctxt
)
3113 /* opt_pass methods: */
3114 opt_pass
* clone () { return new pass_phi_only_cprop (m_ctxt
); }
3115 virtual bool gate (function
*) { return flag_tree_dom
!= 0; }
3116 virtual unsigned int execute (function
*);
3118 }; // class pass_phi_only_cprop
3121 pass_phi_only_cprop::execute (function
*fun
)
3123 bitmap interesting_names
;
3124 bitmap interesting_names1
;
3126 /* Bitmap of blocks which need EH information updated. We can not
3127 update it on-the-fly as doing so invalidates the dominator tree. */
3128 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
3130 /* INTERESTING_NAMES is effectively our worklist, indexed by
3133 A set bit indicates that the statement or PHI node which
3134 defines the SSA_NAME should be (re)examined to determine if
3135 it has become a degenerate PHI or trivial const/copy propagation
3138 Experiments have show we generally get better compilation
3139 time behavior with bitmaps rather than sbitmaps. */
3140 interesting_names
= BITMAP_ALLOC (NULL
);
3141 interesting_names1
= BITMAP_ALLOC (NULL
);
3143 calculate_dominance_info (CDI_DOMINATORS
);
3144 cfg_altered
= false;
3146 /* First phase. Eliminate degenerate PHIs via a dominator
3149 Experiments have indicated that we generally get better
3150 compile-time behavior by visiting blocks in the first
3151 phase in dominator order. Presumably this is because walking
3152 in dominator order leaves fewer PHIs for later examination
3153 by the worklist phase. */
3154 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR_FOR_FN (fun
),
3157 /* Second phase. Eliminate second order degenerate PHIs as well
3158 as trivial copies or constant initializations identified by
3159 the first phase or this phase. Basically we keep iterating
3160 until our set of INTERESTING_NAMEs is empty. */
3161 while (!bitmap_empty_p (interesting_names
))
3166 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3167 changed during the loop. Copy it to another bitmap and
3169 bitmap_copy (interesting_names1
, interesting_names
);
3171 EXECUTE_IF_SET_IN_BITMAP (interesting_names1
, 0, i
, bi
)
3173 tree name
= ssa_name (i
);
3175 /* Ignore SSA_NAMEs that have been released because
3176 their defining statement was deleted (unreachable). */
3178 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i
)),
3185 free_dominance_info (CDI_DOMINATORS
);
3186 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
3187 loops_state_set (LOOPS_NEED_FIXUP
);
3190 /* Propagation of const and copies may make some EH edges dead. Purge
3191 such edges from the CFG as needed. */
3192 if (!bitmap_empty_p (need_eh_cleanup
))
3194 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
3195 BITMAP_FREE (need_eh_cleanup
);
3198 BITMAP_FREE (interesting_names
);
3199 BITMAP_FREE (interesting_names1
);
3206 make_pass_phi_only_cprop (gcc::context
*ctxt
)
3208 return new pass_phi_only_cprop (ctxt
);