1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2021 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"
27 #include "tree-pass.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
33 #include "gimple-fold.h"
35 #include "tree-inline.h"
36 #include "gimple-iterator.h"
38 #include "tree-into-ssa.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-ssa-threadupdate.h"
42 #include "tree-ssa-scopedtables.h"
43 #include "tree-ssa-threadedge.h"
44 #include "tree-ssa-dom.h"
46 #include "tree-cfgcleanup.h"
48 #include "alloc-pool.h"
50 #include "vr-values.h"
51 #include "gimple-ssa-evrp-analyze.h"
54 /* This file implements optimizations on the dominator tree. */
56 /* Structure for recording edge equivalences.
58 Computing and storing the edge equivalences instead of creating
59 them on-demand can save significant amounts of time, particularly
60 for pathological cases involving switch statements.
62 These structures live for a single iteration of the dominator
63 optimizer in the edge's AUX field. At the end of an iteration we
64 free each of these structures. */
68 typedef std::pair
<tree
, tree
> equiv_pair
;
72 /* Record a simple LHS = RHS equivalence. This may trigger
73 calls to derive_equivalences. */
74 void record_simple_equiv (tree
, tree
);
76 /* If traversing this edge creates simple equivalences, we store
77 them as LHS/RHS pairs within this vector. */
78 vec
<equiv_pair
> simple_equivalences
;
80 /* Traversing an edge may also indicate one or more particular conditions
82 vec
<cond_equivalence
> cond_equivalences
;
85 /* Derive equivalences by walking the use-def chains. */
86 void derive_equivalences (tree
, tree
, int);
89 /* Track whether or not we have changed the control flow graph. */
90 static bool cfg_altered
;
92 /* Bitmap of blocks that have had EH statements cleaned. We should
93 remove their dead edges eventually. */
94 static bitmap need_eh_cleanup
;
95 static vec
<gimple
*> need_noreturn_fixup
;
97 /* Statistics for dominator optimizations. */
101 long num_exprs_considered
;
107 static struct opt_stats_d opt_stats
;
109 /* Local functions. */
110 static void record_equality (tree
, tree
, class const_and_copies
*);
111 static void record_equivalences_from_phis (basic_block
);
112 static void record_equivalences_from_incoming_edge (basic_block
,
113 class const_and_copies
*,
114 class avail_exprs_stack
*);
115 static void eliminate_redundant_computations (gimple_stmt_iterator
*,
116 class const_and_copies
*,
117 class avail_exprs_stack
*);
118 static void record_equivalences_from_stmt (gimple
*, int,
119 class avail_exprs_stack
*);
120 static void dump_dominator_optimization_stats (FILE *file
,
121 hash_table
<expr_elt_hasher
> *);
123 /* Constructor for EDGE_INFO. An EDGE_INFO instance is always
124 associated with an edge E. */
126 edge_info::edge_info (edge e
)
128 /* Free the old one associated with E, if it exists and
129 associate our new object with E. */
130 free_dom_edge_info (e
);
133 /* And initialize the embedded vectors. */
134 simple_equivalences
= vNULL
;
135 cond_equivalences
= vNULL
;
138 /* Destructor just needs to release the vectors. */
140 edge_info::~edge_info (void)
142 this->cond_equivalences
.release ();
143 this->simple_equivalences
.release ();
146 /* NAME is known to have the value VALUE, which must be a constant.
148 Walk through its use-def chain to see if there are other equivalences
149 we might be able to derive.
151 RECURSION_LIMIT controls how far back we recurse through the use-def
155 edge_info::derive_equivalences (tree name
, tree value
, int recursion_limit
)
157 if (TREE_CODE (name
) != SSA_NAME
|| TREE_CODE (value
) != INTEGER_CST
)
160 /* This records the equivalence for the toplevel object. Do
161 this before checking the recursion limit. */
162 simple_equivalences
.safe_push (equiv_pair (name
, value
));
164 /* Limit how far up the use-def chains we are willing to walk. */
165 if (recursion_limit
== 0)
168 /* We can walk up the use-def chains to potentially find more
170 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
171 if (is_gimple_assign (def_stmt
))
173 enum tree_code code
= gimple_assign_rhs_code (def_stmt
);
176 /* If the result of an OR is zero, then its operands are, too. */
178 if (integer_zerop (value
))
180 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
181 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
183 value
= build_zero_cst (TREE_TYPE (rhs1
));
184 derive_equivalences (rhs1
, value
, recursion_limit
- 1);
185 value
= build_zero_cst (TREE_TYPE (rhs2
));
186 derive_equivalences (rhs2
, value
, recursion_limit
- 1);
190 /* If the result of an AND is nonzero, then its operands are, too. */
192 if (!integer_zerop (value
))
194 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
195 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
197 /* If either operand has a boolean range, then we
198 know its value must be one, otherwise we just know it
199 is nonzero. The former is clearly useful, I haven't
200 seen cases where the latter is helpful yet. */
201 if (TREE_CODE (rhs1
) == SSA_NAME
)
203 if (ssa_name_has_boolean_range (rhs1
))
205 value
= build_one_cst (TREE_TYPE (rhs1
));
206 derive_equivalences (rhs1
, value
, recursion_limit
- 1);
209 if (TREE_CODE (rhs2
) == SSA_NAME
)
211 if (ssa_name_has_boolean_range (rhs2
))
213 value
= build_one_cst (TREE_TYPE (rhs2
));
214 derive_equivalences (rhs2
, value
, recursion_limit
- 1);
220 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
221 set via a widening type conversion, then we may be able to record
222 additional equivalences. */
226 tree rhs
= gimple_assign_rhs1 (def_stmt
);
227 tree rhs_type
= TREE_TYPE (rhs
);
228 if (INTEGRAL_TYPE_P (rhs_type
)
229 && (TYPE_PRECISION (TREE_TYPE (name
))
230 >= TYPE_PRECISION (rhs_type
))
231 && int_fits_type_p (value
, rhs_type
))
232 derive_equivalences (rhs
,
233 fold_convert (rhs_type
, value
),
234 recursion_limit
- 1);
238 /* We can invert the operation of these codes trivially if
239 one of the RHS operands is a constant to produce a known
240 value for the other RHS operand. */
241 case POINTER_PLUS_EXPR
:
244 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
245 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
247 /* If either argument is a constant, then we can compute
248 a constant value for the nonconstant argument. */
249 if (TREE_CODE (rhs1
) == INTEGER_CST
250 && TREE_CODE (rhs2
) == SSA_NAME
)
251 derive_equivalences (rhs2
,
252 fold_binary (MINUS_EXPR
, TREE_TYPE (rhs1
),
254 recursion_limit
- 1);
255 else if (TREE_CODE (rhs2
) == INTEGER_CST
256 && TREE_CODE (rhs1
) == SSA_NAME
)
257 derive_equivalences (rhs1
,
258 fold_binary (MINUS_EXPR
, TREE_TYPE (rhs1
),
260 recursion_limit
- 1);
264 /* If one of the operands is a constant, then we can compute
265 the value of the other operand. If both operands are
266 SSA_NAMEs, then they must be equal if the result is zero. */
269 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
270 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
272 /* If either argument is a constant, then we can compute
273 a constant value for the nonconstant argument. */
274 if (TREE_CODE (rhs1
) == INTEGER_CST
275 && TREE_CODE (rhs2
) == SSA_NAME
)
276 derive_equivalences (rhs2
,
277 fold_binary (MINUS_EXPR
, TREE_TYPE (rhs1
),
279 recursion_limit
- 1);
280 else if (TREE_CODE (rhs2
) == INTEGER_CST
281 && TREE_CODE (rhs1
) == SSA_NAME
)
282 derive_equivalences (rhs1
,
283 fold_binary (PLUS_EXPR
, TREE_TYPE (rhs1
),
285 recursion_limit
- 1);
286 else if (integer_zerop (value
))
288 tree cond
= build2 (EQ_EXPR
, boolean_type_node
,
289 gimple_assign_rhs1 (def_stmt
),
290 gimple_assign_rhs2 (def_stmt
));
291 tree inverted
= invert_truthvalue (cond
);
292 record_conditions (&this->cond_equivalences
, cond
, inverted
);
300 if ((code
== EQ_EXPR
&& integer_onep (value
))
301 || (code
== NE_EXPR
&& integer_zerop (value
)))
303 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
304 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
306 /* If either argument is a constant, then record the
307 other argument as being the same as that constant.
309 If neither operand is a constant, then we have a
310 conditional name == name equivalence. */
311 if (TREE_CODE (rhs1
) == INTEGER_CST
)
312 derive_equivalences (rhs2
, rhs1
, recursion_limit
- 1);
313 else if (TREE_CODE (rhs2
) == INTEGER_CST
)
314 derive_equivalences (rhs1
, rhs2
, recursion_limit
- 1);
318 tree cond
= build2 (code
, boolean_type_node
,
319 gimple_assign_rhs1 (def_stmt
),
320 gimple_assign_rhs2 (def_stmt
));
321 tree inverted
= invert_truthvalue (cond
);
322 if (integer_zerop (value
))
323 std::swap (cond
, inverted
);
324 record_conditions (&this->cond_equivalences
, cond
, inverted
);
329 /* For BIT_NOT and NEGATE, we can just apply the operation to the
330 VALUE to get the new equivalence. It will always be a constant
331 so we can recurse. */
335 tree rhs
= gimple_assign_rhs1 (def_stmt
);
337 /* If this is a NOT and the operand has a boolean range, then we
338 know its value must be zero or one. We are not supposed to
339 have a BIT_NOT_EXPR for boolean types with precision > 1 in
340 the general case, see e.g. the handling of TRUTH_NOT_EXPR in
341 the gimplifier, but it can be generated by match.pd out of
342 a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR. Now the handling
343 of BIT_AND_EXPR above already forces a specific semantics for
344 boolean types with precision > 1 so we must do the same here,
345 otherwise we could change the semantics of TRUTH_NOT_EXPR for
346 boolean types with precision > 1. */
347 if (code
== BIT_NOT_EXPR
348 && TREE_CODE (rhs
) == SSA_NAME
349 && ssa_name_has_boolean_range (rhs
))
351 if ((TREE_INT_CST_LOW (value
) & 1) == 0)
352 res
= build_one_cst (TREE_TYPE (rhs
));
354 res
= build_zero_cst (TREE_TYPE (rhs
));
357 res
= fold_build1 (code
, TREE_TYPE (rhs
), value
);
358 derive_equivalences (rhs
, res
, recursion_limit
- 1);
364 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
366 tree cond
= build2 (code
, boolean_type_node
,
367 gimple_assign_rhs1 (def_stmt
),
368 gimple_assign_rhs2 (def_stmt
));
369 tree inverted
= invert_truthvalue (cond
);
370 if (integer_zerop (value
))
371 std::swap (cond
, inverted
);
372 record_conditions (&this->cond_equivalences
, cond
, inverted
);
382 edge_info::record_simple_equiv (tree lhs
, tree rhs
)
384 /* If the RHS is a constant, then we may be able to derive
385 further equivalences. Else just record the name = name
387 if (TREE_CODE (rhs
) == INTEGER_CST
)
388 derive_equivalences (lhs
, rhs
, 4);
390 simple_equivalences
.safe_push (equiv_pair (lhs
, rhs
));
393 /* Free the edge_info data attached to E, if it exists. */
396 free_dom_edge_info (edge e
)
398 class edge_info
*edge_info
= (class edge_info
*)e
->aux
;
404 /* Free all EDGE_INFO structures associated with edges in the CFG.
405 If a particular edge can be threaded, copy the redirection
406 target from the EDGE_INFO structure into the edge's AUX field
407 as required by code to update the CFG and SSA graph for
411 free_all_edge_infos (void)
417 FOR_EACH_BB_FN (bb
, cfun
)
419 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
421 free_dom_edge_info (e
);
427 /* We have finished optimizing BB, record any information implied by
428 taking a specific outgoing edge from BB. */
431 record_edge_info (basic_block bb
)
433 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
434 class edge_info
*edge_info
;
436 if (! gsi_end_p (gsi
))
438 gimple
*stmt
= gsi_stmt (gsi
);
439 location_t loc
= gimple_location (stmt
);
441 if (gimple_code (stmt
) == GIMPLE_SWITCH
)
443 gswitch
*switch_stmt
= as_a
<gswitch
*> (stmt
);
444 tree index
= gimple_switch_index (switch_stmt
);
446 if (TREE_CODE (index
) == SSA_NAME
)
449 int n_labels
= gimple_switch_num_labels (switch_stmt
);
450 tree
*info
= XCNEWVEC (tree
, last_basic_block_for_fn (cfun
));
454 for (i
= 0; i
< n_labels
; i
++)
456 tree label
= gimple_switch_label (switch_stmt
, i
);
457 basic_block target_bb
458 = label_to_block (cfun
, CASE_LABEL (label
));
459 if (CASE_HIGH (label
)
461 || info
[target_bb
->index
])
462 info
[target_bb
->index
] = error_mark_node
;
464 info
[target_bb
->index
] = label
;
467 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
469 basic_block target_bb
= e
->dest
;
470 tree label
= info
[target_bb
->index
];
472 if (label
!= NULL
&& label
!= error_mark_node
)
474 tree x
= fold_convert_loc (loc
, TREE_TYPE (index
),
476 edge_info
= new class edge_info (e
);
477 edge_info
->record_simple_equiv (index
, x
);
484 /* A COND_EXPR may create equivalences too. */
485 if (gimple_code (stmt
) == GIMPLE_COND
)
490 tree op0
= gimple_cond_lhs (stmt
);
491 tree op1
= gimple_cond_rhs (stmt
);
492 enum tree_code code
= gimple_cond_code (stmt
);
494 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
496 /* Special case comparing booleans against a constant as we
497 know the value of OP0 on both arms of the branch. i.e., we
498 can record an equivalence for OP0 rather than COND.
500 However, don't do this if the constant isn't zero or one.
501 Such conditionals will get optimized more thoroughly during
503 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
504 && TREE_CODE (op0
) == SSA_NAME
505 && ssa_name_has_boolean_range (op0
)
506 && is_gimple_min_invariant (op1
)
507 && (integer_zerop (op1
) || integer_onep (op1
)))
509 tree true_val
= constant_boolean_node (true, TREE_TYPE (op0
));
510 tree false_val
= constant_boolean_node (false, TREE_TYPE (op0
));
514 edge_info
= new class edge_info (true_edge
);
515 edge_info
->record_simple_equiv (op0
,
517 ? false_val
: true_val
));
518 edge_info
= new class edge_info (false_edge
);
519 edge_info
->record_simple_equiv (op0
,
521 ? true_val
: false_val
));
525 edge_info
= new class edge_info (true_edge
);
526 edge_info
->record_simple_equiv (op0
,
528 ? true_val
: false_val
));
529 edge_info
= new class edge_info (false_edge
);
530 edge_info
->record_simple_equiv (op0
,
532 ? false_val
: true_val
));
535 /* This can show up in the IL as a result of copy propagation
536 it will eventually be canonicalized, but we have to cope
537 with this case within the pass. */
538 else if (is_gimple_min_invariant (op0
)
539 && TREE_CODE (op1
) == SSA_NAME
)
541 tree cond
= build2 (code
, boolean_type_node
, op0
, op1
);
542 tree inverted
= invert_truthvalue_loc (loc
, cond
);
543 bool can_infer_simple_equiv
544 = !(HONOR_SIGNED_ZEROS (op0
)
545 && real_zerop (op0
));
546 class edge_info
*edge_info
;
548 edge_info
= new class edge_info (true_edge
);
549 record_conditions (&edge_info
->cond_equivalences
, cond
, inverted
);
551 if (can_infer_simple_equiv
&& code
== EQ_EXPR
)
552 edge_info
->record_simple_equiv (op1
, op0
);
554 edge_info
= new class edge_info (false_edge
);
555 record_conditions (&edge_info
->cond_equivalences
, inverted
, cond
);
557 if (can_infer_simple_equiv
&& TREE_CODE (inverted
) == EQ_EXPR
)
558 edge_info
->record_simple_equiv (op1
, op0
);
561 else if (TREE_CODE (op0
) == SSA_NAME
562 && (TREE_CODE (op1
) == SSA_NAME
563 || is_gimple_min_invariant (op1
)))
565 tree cond
= build2 (code
, boolean_type_node
, op0
, op1
);
566 tree inverted
= invert_truthvalue_loc (loc
, cond
);
567 bool can_infer_simple_equiv
568 = !(HONOR_SIGNED_ZEROS (op1
)
569 && (TREE_CODE (op1
) == SSA_NAME
|| real_zerop (op1
)));
570 class edge_info
*edge_info
;
572 edge_info
= new class edge_info (true_edge
);
573 record_conditions (&edge_info
->cond_equivalences
, cond
, inverted
);
575 if (can_infer_simple_equiv
&& code
== EQ_EXPR
)
576 edge_info
->record_simple_equiv (op0
, op1
);
578 edge_info
= new class edge_info (false_edge
);
579 record_conditions (&edge_info
->cond_equivalences
, inverted
, cond
);
581 if (can_infer_simple_equiv
&& TREE_CODE (inverted
) == EQ_EXPR
)
582 edge_info
->record_simple_equiv (op0
, op1
);
588 class dom_jt_state
: public jt_state
591 dom_jt_state (const_and_copies
*copies
, avail_exprs_stack
*avails
,
592 evrp_range_analyzer
*evrp
)
593 : m_copies (copies
), m_avails (avails
), m_evrp (evrp
)
596 void push (edge e
) override
598 m_copies
->push_marker ();
599 m_avails
->push_marker ();
600 m_evrp
->push_marker ();
605 m_copies
->pop_to_marker ();
606 m_avails
->pop_to_marker ();
607 m_evrp
->pop_to_marker ();
610 void register_equivs_edge (edge e
) override
612 record_temporary_equivalences (e
, m_copies
, m_avails
);
614 void record_ranges_from_stmt (gimple
*stmt
, bool temporary
) override
616 m_evrp
->record_ranges_from_stmt (stmt
, temporary
);
618 void register_equiv (tree dest
, tree src
, bool update
) override
;
620 const_and_copies
*m_copies
;
621 avail_exprs_stack
*m_avails
;
622 evrp_range_analyzer
*m_evrp
;
626 dom_jt_state::register_equiv (tree dest
, tree src
, bool update
)
628 m_copies
->record_const_or_copy (dest
, src
);
630 /* If requested, update the value range associated with DST, using
631 the range from SRC. */
634 /* Get new VR we can pass to push_value_range. */
635 value_range_equiv
*new_vr
= m_evrp
->allocate_value_range_equiv ();
636 new (new_vr
) value_range_equiv ();
638 /* There are three cases to consider:
640 First if SRC is an SSA_NAME, then we can copy the value range
641 from SRC into NEW_VR.
643 Second if SRC is an INTEGER_CST, then we can just set NEW_VR
644 to a singleton range. Note that even if SRC is a constant we
645 need to set a suitable output range so that VR_UNDEFINED
646 ranges do not leak through.
648 Otherwise set NEW_VR to varying. This may be overly
650 if (TREE_CODE (src
) == SSA_NAME
)
651 new_vr
->deep_copy (m_evrp
->get_value_range (src
));
652 else if (TREE_CODE (src
) == INTEGER_CST
)
655 new_vr
->set_varying (TREE_TYPE (src
));
657 /* This is a temporary range for DST, so push it. */
658 m_evrp
->push_value_range (dest
, new_vr
);
662 class dom_jt_simplifier
: public jt_simplifier
665 dom_jt_simplifier (vr_values
*v
, avail_exprs_stack
*avails
)
666 : m_vr_values (v
), m_avails (avails
) { }
669 tree
simplify (gimple
*, gimple
*, basic_block
, jt_state
*) override
;
670 vr_values
*m_vr_values
;
671 avail_exprs_stack
*m_avails
;
675 dom_jt_simplifier::simplify (gimple
*stmt
, gimple
*within_stmt
,
676 basic_block
, jt_state
*)
678 /* First see if the conditional is in the hash table. */
679 tree cached_lhs
= m_avails
->lookup_avail_expr (stmt
, false, true);
683 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
685 simplify_using_ranges
simplifier (m_vr_values
);
686 return simplifier
.vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
687 gimple_cond_lhs (cond_stmt
),
688 gimple_cond_rhs (cond_stmt
),
691 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
693 tree op
= gimple_switch_index (switch_stmt
);
694 if (TREE_CODE (op
) != SSA_NAME
)
697 const value_range_equiv
*vr
= m_vr_values
->get_value_range (op
);
698 return find_case_label_range (switch_stmt
, vr
);
700 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
702 tree lhs
= gimple_assign_lhs (assign_stmt
);
703 if (TREE_CODE (lhs
) == SSA_NAME
704 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
705 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
706 && stmt_interesting_for_vrp (stmt
))
710 value_range_equiv new_vr
;
711 m_vr_values
->extract_range_from_stmt (stmt
, &dummy_e
, &dummy_tree
,
714 if (new_vr
.singleton_p (&singleton
))
721 class dom_opt_dom_walker
: public dom_walker
724 dom_opt_dom_walker (cdi_direction direction
,
725 jump_threader
*threader
,
727 evrp_range_analyzer
*analyzer
,
728 const_and_copies
*const_and_copies
,
729 avail_exprs_stack
*avail_exprs_stack
)
730 : dom_walker (direction
, REACHABLE_BLOCKS
)
732 m_evrp_range_analyzer
= analyzer
;
734 m_dummy_cond
= gimple_build_cond (NE_EXPR
, integer_zero_node
,
735 integer_zero_node
, NULL
, NULL
);
736 m_const_and_copies
= const_and_copies
;
737 m_avail_exprs_stack
= avail_exprs_stack
;
738 m_threader
= threader
;
741 virtual edge
before_dom_children (basic_block
);
742 virtual void after_dom_children (basic_block
);
746 /* Unwindable equivalences, both const/copy and expression varieties. */
747 class const_and_copies
*m_const_and_copies
;
748 class avail_exprs_stack
*m_avail_exprs_stack
;
750 /* Dummy condition to avoid creating lots of throw away statements. */
753 /* Optimize a single statement within a basic block using the
754 various tables mantained by DOM. Returns the taken edge if
755 the statement is a conditional with a statically determined
757 edge
optimize_stmt (basic_block
, gimple_stmt_iterator
*, bool *);
760 void test_for_singularity (gimple
*, avail_exprs_stack
*);
762 jump_threader
*m_threader
;
763 evrp_range_analyzer
*m_evrp_range_analyzer
;
767 /* Jump threading, redundancy elimination and const/copy propagation.
769 This pass may expose new symbols that need to be renamed into SSA. For
770 every new symbol exposed, its corresponding bit will be set in
775 const pass_data pass_data_dominator
=
777 GIMPLE_PASS
, /* type */
779 OPTGROUP_NONE
, /* optinfo_flags */
780 TV_TREE_SSA_DOMINATOR_OPTS
, /* tv_id */
781 ( PROP_cfg
| PROP_ssa
), /* properties_required */
782 0, /* properties_provided */
783 0, /* properties_destroyed */
784 0, /* todo_flags_start */
785 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
788 class pass_dominator
: public gimple_opt_pass
791 pass_dominator (gcc::context
*ctxt
)
792 : gimple_opt_pass (pass_data_dominator
, ctxt
),
793 may_peel_loop_headers_p (false)
796 /* opt_pass methods: */
797 opt_pass
* clone () { return new pass_dominator (m_ctxt
); }
798 void set_pass_param (unsigned int n
, bool param
)
801 may_peel_loop_headers_p
= param
;
803 virtual bool gate (function
*) { return flag_tree_dom
!= 0; }
804 virtual unsigned int execute (function
*);
807 /* This flag is used to prevent loops from being peeled repeatedly in jump
808 threading; it will be removed once we preserve loop structures throughout
809 the compilation -- we will be able to mark the affected loops directly in
810 jump threading, and avoid peeling them next time. */
811 bool may_peel_loop_headers_p
;
812 }; // class pass_dominator
815 pass_dominator::execute (function
*fun
)
817 memset (&opt_stats
, 0, sizeof (opt_stats
));
819 /* Create our hash tables. */
820 hash_table
<expr_elt_hasher
> *avail_exprs
821 = new hash_table
<expr_elt_hasher
> (1024);
822 class avail_exprs_stack
*avail_exprs_stack
823 = new class avail_exprs_stack (avail_exprs
);
824 class const_and_copies
*const_and_copies
= new class const_and_copies ();
825 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
826 need_noreturn_fixup
.create (0);
828 calculate_dominance_info (CDI_DOMINATORS
);
831 /* We need to know loop structures in order to avoid destroying them
832 in jump threading. Note that we still can e.g. thread through loop
833 headers to an exit edge, or through loop header to the loop body, assuming
834 that we update the loop info.
836 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
837 to several overly conservative bail-outs in jump threading, case
838 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
839 missing. We should improve jump threading in future then
840 LOOPS_HAVE_PREHEADERS won't be needed here. */
841 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
| LOOPS_HAVE_SIMPLE_LATCHES
842 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
);
844 /* We need accurate information regarding back edges in the CFG
845 for jump threading; this may include back edges that are not part of
847 mark_dfs_back_edges ();
849 /* We want to create the edge info structures before the dominator walk
850 so that they'll be in place for the jump threader, particularly when
851 threading through a join block.
853 The conditions will be lazily updated with global equivalences as
854 we reach them during the dominator walk. */
856 FOR_EACH_BB_FN (bb
, fun
)
857 record_edge_info (bb
);
859 /* Recursively walk the dominator tree optimizing statements. */
860 evrp_range_analyzer
analyzer (true);
861 dom_jt_simplifier
simplifier (&analyzer
, avail_exprs_stack
);
862 dom_jt_state
state (const_and_copies
, avail_exprs_stack
, &analyzer
);
863 jump_threader
threader (&simplifier
, &state
);
864 dom_opt_dom_walker
walker (CDI_DOMINATORS
,
870 walker
.walk (fun
->cfg
->x_entry_block_ptr
);
872 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
873 edge. When found, remove jump threads which contain any outgoing
874 edge from the affected block. */
877 FOR_EACH_BB_FN (bb
, fun
)
882 /* First see if there are any edges without EDGE_EXECUTABLE
885 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
887 if ((e
->flags
& EDGE_EXECUTABLE
) == 0)
894 /* If there were any such edges found, then remove jump threads
895 containing any edge leaving BB. */
897 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
898 threader
.remove_jump_threads_including (e
);
903 gimple_stmt_iterator gsi
;
905 FOR_EACH_BB_FN (bb
, fun
)
907 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
908 update_stmt_if_modified (gsi_stmt (gsi
));
912 /* If we exposed any new variables, go ahead and put them into
913 SSA form now, before we handle jump threading. This simplifies
914 interactions between rewriting of _DECL nodes into SSA form
915 and rewriting SSA_NAME nodes into SSA form after block
916 duplication and CFG manipulation. */
917 update_ssa (TODO_update_ssa
);
919 free_all_edge_infos ();
921 /* Thread jumps, creating duplicate blocks as needed. */
922 cfg_altered
|= threader
.thread_through_all_blocks (may_peel_loop_headers_p
);
925 free_dominance_info (CDI_DOMINATORS
);
927 /* Removal of statements may make some EH edges dead. Purge
928 such edges from the CFG as needed. */
929 if (!bitmap_empty_p (need_eh_cleanup
))
934 /* Jump threading may have created forwarder blocks from blocks
935 needing EH cleanup; the new successor of these blocks, which
936 has inherited from the original block, needs the cleanup.
937 Don't clear bits in the bitmap, as that can break the bitmap
939 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup
, 0, i
, bi
)
941 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, i
);
944 while (single_succ_p (bb
)
945 && (single_succ_edge (bb
)->flags
946 & (EDGE_EH
|EDGE_DFS_BACK
)) == 0)
947 bb
= single_succ (bb
);
948 if (bb
== EXIT_BLOCK_PTR_FOR_FN (fun
))
950 if ((unsigned) bb
->index
!= i
)
951 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
954 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
955 bitmap_clear (need_eh_cleanup
);
958 /* Fixup stmts that became noreturn calls. This may require splitting
959 blocks and thus isn't possible during the dominator walk or before
960 jump threading finished. Do this in reverse order so we don't
961 inadvertedly remove a stmt we want to fixup by visiting a dominating
962 now noreturn call first. */
963 while (!need_noreturn_fixup
.is_empty ())
965 gimple
*stmt
= need_noreturn_fixup
.pop ();
966 if (dump_file
&& dump_flags
& TDF_DETAILS
)
968 fprintf (dump_file
, "Fixing up noreturn call ");
969 print_gimple_stmt (dump_file
, stmt
, 0);
970 fprintf (dump_file
, "\n");
972 fixup_noreturn_call (stmt
);
975 statistics_counter_event (fun
, "Redundant expressions eliminated",
977 statistics_counter_event (fun
, "Constants propagated",
978 opt_stats
.num_const_prop
);
979 statistics_counter_event (fun
, "Copies propagated",
980 opt_stats
.num_copy_prop
);
982 /* Debugging dumps. */
983 if (dump_file
&& (dump_flags
& TDF_STATS
))
984 dump_dominator_optimization_stats (dump_file
, avail_exprs
);
986 loop_optimizer_finalize ();
988 /* Delete our main hashtable. */
992 /* Free asserted bitmaps and stacks. */
993 BITMAP_FREE (need_eh_cleanup
);
994 need_noreturn_fixup
.release ();
995 delete avail_exprs_stack
;
996 delete const_and_copies
;
1004 make_pass_dominator (gcc::context
*ctxt
)
1006 return new pass_dominator (ctxt
);
1009 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
1012 dom_valueize (tree t
)
1014 if (TREE_CODE (t
) == SSA_NAME
)
1016 tree tem
= SSA_NAME_VALUE (t
);
1023 /* We have just found an equivalence for LHS on an edge E.
1024 Look backwards to other uses of LHS and see if we can derive
1025 additional equivalences that are valid on edge E. */
1027 back_propagate_equivalences (tree lhs
, edge e
,
1028 class const_and_copies
*const_and_copies
)
1030 use_operand_p use_p
;
1031 imm_use_iterator iter
;
1032 bitmap domby
= NULL
;
1033 basic_block dest
= e
->dest
;
1035 /* Iterate over the uses of LHS to see if any dominate E->dest.
1036 If so, they may create useful equivalences too.
1038 ??? If the code gets re-organized to a worklist to catch more
1039 indirect opportunities and it is made to handle PHIs then this
1040 should only consider use_stmts in basic-blocks we have already visited. */
1041 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
1043 gimple
*use_stmt
= USE_STMT (use_p
);
1045 /* Often the use is in DEST, which we trivially know we can't use.
1046 This is cheaper than the dominator set tests below. */
1047 if (dest
== gimple_bb (use_stmt
))
1050 /* Filter out statements that can never produce a useful
1052 tree lhs2
= gimple_get_lhs (use_stmt
);
1053 if (!lhs2
|| TREE_CODE (lhs2
) != SSA_NAME
)
1056 /* Profiling has shown the domination tests here can be fairly
1057 expensive. We get significant improvements by building the
1058 set of blocks that dominate BB. We can then just test
1059 for set membership below.
1061 We also initialize the set lazily since often the only uses
1062 are going to be in the same block as DEST. */
1065 domby
= BITMAP_ALLOC (NULL
);
1066 basic_block bb
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
1069 bitmap_set_bit (domby
, bb
->index
);
1070 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1074 /* This tests if USE_STMT does not dominate DEST. */
1075 if (!bitmap_bit_p (domby
, gimple_bb (use_stmt
)->index
))
1078 /* At this point USE_STMT dominates DEST and may result in a
1079 useful equivalence. Try to simplify its RHS to a constant
1081 tree res
= gimple_fold_stmt_to_constant_1 (use_stmt
, dom_valueize
,
1082 no_follow_ssa_edges
);
1083 if (res
&& (TREE_CODE (res
) == SSA_NAME
|| is_gimple_min_invariant (res
)))
1084 record_equality (lhs2
, res
, const_and_copies
);
1088 BITMAP_FREE (domby
);
1091 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
1092 by traversing edge E (which are cached in E->aux).
1094 Callers are responsible for managing the unwinding markers. */
1096 record_temporary_equivalences (edge e
,
1097 class const_and_copies
*const_and_copies
,
1098 class avail_exprs_stack
*avail_exprs_stack
)
1101 class edge_info
*edge_info
= (class edge_info
*) e
->aux
;
1103 /* If we have info associated with this edge, record it into
1104 our equivalence tables. */
1107 cond_equivalence
*eq
;
1108 /* If we have 0 = COND or 1 = COND equivalences, record them
1109 into our expression hash tables. */
1110 for (i
= 0; edge_info
->cond_equivalences
.iterate (i
, &eq
); ++i
)
1111 avail_exprs_stack
->record_cond (eq
);
1113 edge_info::equiv_pair
*seq
;
1114 for (i
= 0; edge_info
->simple_equivalences
.iterate (i
, &seq
); ++i
)
1116 tree lhs
= seq
->first
;
1117 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
1120 /* Record the simple NAME = VALUE equivalence. */
1121 tree rhs
= seq
->second
;
1123 /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
1124 cheaper to compute than the other, then set up the equivalence
1125 such that we replace the expensive one with the cheap one.
1127 If they are the same cost to compute, then do not record
1129 if (TREE_CODE (lhs
) == SSA_NAME
&& TREE_CODE (rhs
) == SSA_NAME
)
1131 gimple
*rhs_def
= SSA_NAME_DEF_STMT (rhs
);
1132 int rhs_cost
= estimate_num_insns (rhs_def
, &eni_size_weights
);
1134 gimple
*lhs_def
= SSA_NAME_DEF_STMT (lhs
);
1135 int lhs_cost
= estimate_num_insns (lhs_def
, &eni_size_weights
);
1137 if (rhs_cost
> lhs_cost
)
1138 record_equality (rhs
, lhs
, const_and_copies
);
1139 else if (rhs_cost
< lhs_cost
)
1140 record_equality (lhs
, rhs
, const_and_copies
);
1143 record_equality (lhs
, rhs
, const_and_copies
);
1146 /* Any equivalence found for LHS may result in additional
1147 equivalences for other uses of LHS that we have already
1149 back_propagate_equivalences (lhs
, e
, const_and_copies
);
1154 /* PHI nodes can create equivalences too.
1156 Ignoring any alternatives which are the same as the result, if
1157 all the alternatives are equal, then the PHI node creates an
1161 record_equivalences_from_phis (basic_block bb
)
1165 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); )
1167 gphi
*phi
= gsi
.phi ();
1169 /* We might eliminate the PHI, so advance GSI now. */
1172 tree lhs
= gimple_phi_result (phi
);
1176 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1178 tree t
= gimple_phi_arg_def (phi
, i
);
1180 /* Ignore alternatives which are the same as our LHS. Since
1181 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1182 can simply compare pointers. */
1186 /* If the associated edge is not marked as executable, then it
1188 if ((gimple_phi_arg_edge (phi
, i
)->flags
& EDGE_EXECUTABLE
) == 0)
1191 t
= dom_valueize (t
);
1193 /* If T is an SSA_NAME and its associated edge is a backedge,
1194 then quit as we cannot utilize this equivalence. */
1195 if (TREE_CODE (t
) == SSA_NAME
1196 && (gimple_phi_arg_edge (phi
, i
)->flags
& EDGE_DFS_BACK
))
1199 /* If we have not processed an alternative yet, then set
1200 RHS to this alternative. */
1203 /* If we have processed an alternative (stored in RHS), then
1204 see if it is equal to this one. If it isn't, then stop
1206 else if (! operand_equal_for_phi_arg_p (rhs
, t
))
1210 /* If we had no interesting alternatives, then all the RHS alternatives
1211 must have been the same as LHS. */
1215 /* If we managed to iterate through each PHI alternative without
1216 breaking out of the loop, then we have a PHI which may create
1217 a useful equivalence. We do not need to record unwind data for
1218 this, since this is a true assignment and not an equivalence
1219 inferred from a comparison. All uses of this ssa name are dominated
1220 by this assignment, so unwinding just costs time and space. */
1221 if (i
== gimple_phi_num_args (phi
))
1223 if (may_propagate_copy (lhs
, rhs
))
1224 set_ssa_name_value (lhs
, rhs
);
1225 else if (virtual_operand_p (lhs
))
1228 imm_use_iterator iter
;
1229 use_operand_p use_p
;
1230 /* For virtual operands we have to propagate into all uses as
1231 otherwise we will create overlapping life-ranges. */
1232 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
1233 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1234 SET_USE (use_p
, rhs
);
1235 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
1236 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
1237 gimple_stmt_iterator tmp_gsi
= gsi_for_stmt (phi
);
1238 remove_phi_node (&tmp_gsi
, true);
1244 /* Record any equivalences created by the incoming edge to BB into
1245 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1246 incoming edge, then no equivalence is created. */
1249 record_equivalences_from_incoming_edge (basic_block bb
,
1250 class const_and_copies
*const_and_copies
,
1251 class avail_exprs_stack
*avail_exprs_stack
)
1256 /* If our parent block ended with a control statement, then we may be
1257 able to record some equivalences based on which outgoing edge from
1258 the parent was followed. */
1259 parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1261 e
= single_pred_edge_ignoring_loop_edges (bb
, true);
1263 /* If we had a single incoming edge from our parent block, then enter
1264 any data associated with the edge into our tables. */
1265 if (e
&& e
->src
== parent
)
1266 record_temporary_equivalences (e
, const_and_copies
, avail_exprs_stack
);
1269 /* Dump statistics for the hash table HTAB. */
1272 htab_statistics (FILE *file
, const hash_table
<expr_elt_hasher
> &htab
)
1274 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1275 (long) htab
.size (),
1276 (long) htab
.elements (),
1277 htab
.collisions ());
1280 /* Dump SSA statistics on FILE. */
1283 dump_dominator_optimization_stats (FILE *file
,
1284 hash_table
<expr_elt_hasher
> *avail_exprs
)
1286 fprintf (file
, "Total number of statements: %6ld\n\n",
1287 opt_stats
.num_stmts
);
1288 fprintf (file
, "Exprs considered for dominator optimizations: %6ld\n",
1289 opt_stats
.num_exprs_considered
);
1291 fprintf (file
, "\nHash table statistics:\n");
1293 fprintf (file
, " avail_exprs: ");
1294 htab_statistics (file
, *avail_exprs
);
1298 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1299 This constrains the cases in which we may treat this as assignment. */
1302 record_equality (tree x
, tree y
, class const_and_copies
*const_and_copies
)
1304 tree prev_x
= NULL
, prev_y
= NULL
;
1306 if (tree_swap_operands_p (x
, y
))
1309 /* Most of the time tree_swap_operands_p does what we want. But there
1310 are cases where we know one operand is better for copy propagation than
1311 the other. Given no other code cares about ordering of equality
1312 comparison operators for that purpose, we just handle the special cases
1314 if (TREE_CODE (x
) == SSA_NAME
&& TREE_CODE (y
) == SSA_NAME
)
1316 /* If one operand is a single use operand, then make it
1317 X. This will preserve its single use properly and if this
1318 conditional is eliminated, the computation of X can be
1319 eliminated as well. */
1320 if (has_single_use (y
) && ! has_single_use (x
))
1323 if (TREE_CODE (x
) == SSA_NAME
)
1324 prev_x
= SSA_NAME_VALUE (x
);
1325 if (TREE_CODE (y
) == SSA_NAME
)
1326 prev_y
= SSA_NAME_VALUE (y
);
1328 /* If one of the previous values is invariant, or invariant in more loops
1329 (by depth), then use that.
1330 Otherwise it doesn't matter which value we choose, just so
1331 long as we canonicalize on one value. */
1332 if (is_gimple_min_invariant (y
))
1334 else if (is_gimple_min_invariant (x
))
1335 prev_x
= x
, x
= y
, y
= prev_x
, prev_x
= prev_y
;
1336 else if (prev_x
&& is_gimple_min_invariant (prev_x
))
1337 x
= y
, y
= prev_x
, prev_x
= prev_y
;
1341 /* After the swapping, we must have one SSA_NAME. */
1342 if (TREE_CODE (x
) != SSA_NAME
)
1345 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1346 variable compared against zero. If we're honoring signed zeros,
1347 then we cannot record this value unless we know that the value is
1349 if (HONOR_SIGNED_ZEROS (x
)
1350 && (TREE_CODE (y
) != REAL_CST
1351 || real_equal (&dconst0
, &TREE_REAL_CST (y
))))
1354 const_and_copies
->record_const_or_copy (x
, y
, prev_x
);
1357 /* Returns true when STMT is a simple iv increment. It detects the
1358 following situation:
1360 i_1 = phi (..., i_k)
1362 i_j = i_{j-1} for each j : 2 <= j <= k-1
1364 i_k = i_{k-1} +/- ... */
1367 simple_iv_increment_p (gimple
*stmt
)
1369 enum tree_code code
;
1374 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1377 lhs
= gimple_assign_lhs (stmt
);
1378 if (TREE_CODE (lhs
) != SSA_NAME
)
1381 code
= gimple_assign_rhs_code (stmt
);
1382 if (code
!= PLUS_EXPR
1383 && code
!= MINUS_EXPR
1384 && code
!= POINTER_PLUS_EXPR
)
1387 preinc
= gimple_assign_rhs1 (stmt
);
1388 if (TREE_CODE (preinc
) != SSA_NAME
)
1391 phi
= SSA_NAME_DEF_STMT (preinc
);
1392 while (gimple_code (phi
) != GIMPLE_PHI
)
1394 /* Follow trivial copies, but not the DEF used in a back edge,
1395 so that we don't prevent coalescing. */
1396 if (!gimple_assign_ssa_name_copy_p (phi
))
1398 preinc
= gimple_assign_rhs1 (phi
);
1399 phi
= SSA_NAME_DEF_STMT (preinc
);
1402 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1403 if (gimple_phi_arg_def (phi
, i
) == lhs
)
1409 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1410 successors of BB. */
1413 cprop_into_successor_phis (basic_block bb
,
1414 class const_and_copies
*const_and_copies
)
1419 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1424 /* If this is an abnormal edge, then we do not want to copy propagate
1425 into the PHI alternative associated with this edge. */
1426 if (e
->flags
& EDGE_ABNORMAL
)
1429 gsi
= gsi_start_phis (e
->dest
);
1430 if (gsi_end_p (gsi
))
1433 /* We may have an equivalence associated with this edge. While
1434 we cannot propagate it into non-dominated blocks, we can
1435 propagate them into PHIs in non-dominated blocks. */
1437 /* Push the unwind marker so we can reset the const and copies
1438 table back to its original state after processing this edge. */
1439 const_and_copies
->push_marker ();
1441 /* Extract and record any simple NAME = VALUE equivalences.
1443 Don't bother with [01] = COND equivalences, they're not useful
1445 class edge_info
*edge_info
= (class edge_info
*) e
->aux
;
1449 edge_info::equiv_pair
*seq
;
1450 for (int i
= 0; edge_info
->simple_equivalences
.iterate (i
, &seq
); ++i
)
1452 tree lhs
= seq
->first
;
1453 tree rhs
= seq
->second
;
1455 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
1456 const_and_copies
->record_const_or_copy (lhs
, rhs
);
1462 for ( ; !gsi_end_p (gsi
); gsi_next (&gsi
))
1465 use_operand_p orig_p
;
1467 gphi
*phi
= gsi
.phi ();
1469 /* The alternative may be associated with a constant, so verify
1470 it is an SSA_NAME before doing anything with it. */
1471 orig_p
= gimple_phi_arg_imm_use_ptr (phi
, indx
);
1472 orig_val
= get_use_from_ptr (orig_p
);
1473 if (TREE_CODE (orig_val
) != SSA_NAME
)
1476 /* If we have *ORIG_P in our constant/copy table, then replace
1477 ORIG_P with its value in our constant/copy table. */
1478 new_val
= SSA_NAME_VALUE (orig_val
);
1480 && new_val
!= orig_val
1481 && may_propagate_copy (orig_val
, new_val
))
1482 propagate_value (orig_p
, new_val
);
1485 const_and_copies
->pop_to_marker ();
1490 dom_opt_dom_walker::before_dom_children (basic_block bb
)
1492 gimple_stmt_iterator gsi
;
1494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1495 fprintf (dump_file
, "\n\nOptimizing block #%d\n\n", bb
->index
);
1497 m_evrp_range_analyzer
->enter (bb
);
1499 /* Push a marker on the stacks of local information so that we know how
1500 far to unwind when we finalize this block. */
1501 m_avail_exprs_stack
->push_marker ();
1502 m_const_and_copies
->push_marker ();
1504 record_equivalences_from_incoming_edge (bb
, m_const_and_copies
,
1505 m_avail_exprs_stack
);
1507 /* PHI nodes can create equivalences too. */
1508 record_equivalences_from_phis (bb
);
1510 /* Create equivalences from redundant PHIs. PHIs are only truly
1511 redundant when they exist in the same block, so push another
1512 marker and unwind right afterwards. */
1513 m_avail_exprs_stack
->push_marker ();
1514 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1515 eliminate_redundant_computations (&gsi
, m_const_and_copies
,
1516 m_avail_exprs_stack
);
1517 m_avail_exprs_stack
->pop_to_marker ();
1519 edge taken_edge
= NULL
;
1520 /* Initialize visited flag ahead of us, it has undefined state on
1522 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1523 gimple_set_visited (gsi_stmt (gsi
), false);
1524 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
1526 /* Do not optimize a stmt twice, substitution might end up with
1527 _3 = _3 which is not valid. */
1528 if (gimple_visited_p (gsi_stmt (gsi
)))
1534 m_state
->record_ranges_from_stmt (gsi_stmt (gsi
), false);
1535 bool removed_p
= false;
1536 taken_edge
= this->optimize_stmt (bb
, &gsi
, &removed_p
);
1538 gimple_set_visited (gsi_stmt (gsi
), true);
1540 /* Go back and visit stmts inserted by folding after substituting
1541 into the stmt at gsi. */
1542 if (gsi_end_p (gsi
))
1544 gcc_checking_assert (removed_p
);
1545 gsi
= gsi_last_bb (bb
);
1546 while (!gsi_end_p (gsi
) && !gimple_visited_p (gsi_stmt (gsi
)))
1555 while (!gsi_end_p (gsi
) && !gimple_visited_p (gsi_stmt (gsi
)));
1557 if (gsi_end_p (gsi
))
1558 gsi
= gsi_start_bb (bb
);
1563 /* Now prepare to process dominated blocks. */
1564 record_edge_info (bb
);
1565 cprop_into_successor_phis (bb
, m_const_and_copies
);
1566 if (taken_edge
&& !dbg_cnt (dom_unreachable_edges
))
1572 /* We have finished processing the dominator children of BB, perform
1573 any finalization actions in preparation for leaving this node in
1574 the dominator tree. */
1577 dom_opt_dom_walker::after_dom_children (basic_block bb
)
1579 m_threader
->thread_outgoing_edges (bb
);
1580 m_avail_exprs_stack
->pop_to_marker ();
1581 m_const_and_copies
->pop_to_marker ();
1582 m_evrp_range_analyzer
->leave (bb
);
1585 /* Search for redundant computations in STMT. If any are found, then
1586 replace them with the variable holding the result of the computation.
1588 If safe, record this expression into AVAIL_EXPRS_STACK and
1589 CONST_AND_COPIES. */
1592 eliminate_redundant_computations (gimple_stmt_iterator
* gsi
,
1593 class const_and_copies
*const_and_copies
,
1594 class avail_exprs_stack
*avail_exprs_stack
)
1600 bool assigns_var_p
= false;
1602 gimple
*stmt
= gsi_stmt (*gsi
);
1604 if (gimple_code (stmt
) == GIMPLE_PHI
)
1605 def
= gimple_phi_result (stmt
);
1607 def
= gimple_get_lhs (stmt
);
1609 /* Certain expressions on the RHS can be optimized away, but cannot
1610 themselves be entered into the hash tables. */
1612 || TREE_CODE (def
) != SSA_NAME
1613 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def
)
1614 || gimple_vdef (stmt
)
1615 /* Do not record equivalences for increments of ivs. This would create
1616 overlapping live ranges for a very questionable gain. */
1617 || simple_iv_increment_p (stmt
))
1620 /* Check if the expression has been computed before. */
1621 cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, insert
, true);
1623 opt_stats
.num_exprs_considered
++;
1625 /* Get the type of the expression we are trying to optimize. */
1626 if (is_gimple_assign (stmt
))
1628 expr_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1629 assigns_var_p
= true;
1631 else if (gimple_code (stmt
) == GIMPLE_COND
)
1632 expr_type
= boolean_type_node
;
1633 else if (is_gimple_call (stmt
))
1635 gcc_assert (gimple_call_lhs (stmt
));
1636 expr_type
= TREE_TYPE (gimple_call_lhs (stmt
));
1637 assigns_var_p
= true;
1639 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
1640 expr_type
= TREE_TYPE (gimple_switch_index (swtch_stmt
));
1641 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1642 /* We can't propagate into a phi, so the logic below doesn't apply.
1643 Instead record an equivalence between the cached LHS and the
1644 PHI result of this statement, provided they are in the same block.
1645 This should be sufficient to kill the redundant phi. */
1647 if (def
&& cached_lhs
)
1648 const_and_copies
->record_const_or_copy (def
, cached_lhs
);
1657 /* It is safe to ignore types here since we have already done
1658 type checking in the hashing and equality routines. In fact
1659 type checking here merely gets in the way of constant
1660 propagation. Also, make sure that it is safe to propagate
1661 CACHED_LHS into the expression in STMT. */
1662 if ((TREE_CODE (cached_lhs
) != SSA_NAME
1664 || useless_type_conversion_p (expr_type
, TREE_TYPE (cached_lhs
))))
1665 || may_propagate_copy_into_stmt (stmt
, cached_lhs
))
1667 gcc_checking_assert (TREE_CODE (cached_lhs
) == SSA_NAME
1668 || is_gimple_min_invariant (cached_lhs
));
1670 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1672 fprintf (dump_file
, " Replaced redundant expr '");
1673 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
1674 fprintf (dump_file
, "' with '");
1675 print_generic_expr (dump_file
, cached_lhs
, dump_flags
);
1676 fprintf (dump_file
, "'\n");
1682 && !useless_type_conversion_p (expr_type
, TREE_TYPE (cached_lhs
)))
1683 cached_lhs
= fold_convert (expr_type
, cached_lhs
);
1685 propagate_tree_value_into_stmt (gsi
, cached_lhs
);
1687 /* Since it is always necessary to mark the result as modified,
1688 perhaps we should move this into propagate_tree_value_into_stmt
1690 gimple_set_modified (gsi_stmt (*gsi
), true);
1694 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1695 the available expressions table or the const_and_copies table.
1696 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1698 We handle only very simple copy equivalences here. The heavy
1699 lifing is done by eliminate_redundant_computations. */
1702 record_equivalences_from_stmt (gimple
*stmt
, int may_optimize_p
,
1703 class avail_exprs_stack
*avail_exprs_stack
)
1706 enum tree_code lhs_code
;
1708 gcc_assert (is_gimple_assign (stmt
));
1710 lhs
= gimple_assign_lhs (stmt
);
1711 lhs_code
= TREE_CODE (lhs
);
1713 if (lhs_code
== SSA_NAME
1714 && gimple_assign_single_p (stmt
))
1716 tree rhs
= gimple_assign_rhs1 (stmt
);
1718 /* If the RHS of the assignment is a constant or another variable that
1719 may be propagated, register it in the CONST_AND_COPIES table. We
1720 do not need to record unwind data for this, since this is a true
1721 assignment and not an equivalence inferred from a comparison. All
1722 uses of this ssa name are dominated by this assignment, so unwinding
1723 just costs time and space. */
1725 && (TREE_CODE (rhs
) == SSA_NAME
1726 || is_gimple_min_invariant (rhs
)))
1728 rhs
= dom_valueize (rhs
);
1730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1732 fprintf (dump_file
, "==== ASGN ");
1733 print_generic_expr (dump_file
, lhs
);
1734 fprintf (dump_file
, " = ");
1735 print_generic_expr (dump_file
, rhs
);
1736 fprintf (dump_file
, "\n");
1739 set_ssa_name_value (lhs
, rhs
);
1743 /* Make sure we can propagate &x + CST. */
1744 if (lhs_code
== SSA_NAME
1745 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
1746 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
1747 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
)
1749 tree op0
= gimple_assign_rhs1 (stmt
);
1750 tree op1
= gimple_assign_rhs2 (stmt
);
1752 = build1 (ADDR_EXPR
, TREE_TYPE (op0
),
1753 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (op0
)),
1754 unshare_expr (op0
), fold_convert (ptr_type_node
,
1756 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1758 fprintf (dump_file
, "==== ASGN ");
1759 print_generic_expr (dump_file
, lhs
);
1760 fprintf (dump_file
, " = ");
1761 print_generic_expr (dump_file
, new_rhs
);
1762 fprintf (dump_file
, "\n");
1765 set_ssa_name_value (lhs
, new_rhs
);
1768 /* A memory store, even an aliased store, creates a useful
1769 equivalence. By exchanging the LHS and RHS, creating suitable
1770 vops and recording the result in the available expression table,
1771 we may be able to expose more redundant loads. */
1772 if (!gimple_has_volatile_ops (stmt
)
1773 && gimple_references_memory_p (stmt
)
1774 && gimple_assign_single_p (stmt
)
1775 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
1776 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
1777 && !is_gimple_reg (lhs
))
1779 tree rhs
= gimple_assign_rhs1 (stmt
);
1782 /* Build a new statement with the RHS and LHS exchanged. */
1783 if (TREE_CODE (rhs
) == SSA_NAME
)
1785 /* NOTE tuples. The call to gimple_build_assign below replaced
1786 a call to build_gimple_modify_stmt, which did not set the
1787 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1788 may cause an SSA validation failure, as the LHS may be a
1789 default-initialized name and should have no definition. I'm
1790 a bit dubious of this, as the artificial statement that we
1791 generate here may in fact be ill-formed, but it is simply
1792 used as an internal device in this pass, and never becomes
1794 gimple
*defstmt
= SSA_NAME_DEF_STMT (rhs
);
1795 new_stmt
= gimple_build_assign (rhs
, lhs
);
1796 SSA_NAME_DEF_STMT (rhs
) = defstmt
;
1799 new_stmt
= gimple_build_assign (rhs
, lhs
);
1801 gimple_set_vuse (new_stmt
, gimple_vdef (stmt
));
1803 /* Finally enter the statement into the available expression
1805 avail_exprs_stack
->lookup_avail_expr (new_stmt
, true, true);
1809 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1810 CONST_AND_COPIES. */
1813 cprop_operand (gimple
*stmt
, use_operand_p op_p
, range_query
*query
)
1816 tree op
= USE_FROM_PTR (op_p
);
1818 /* If the operand has a known constant value or it is known to be a
1819 copy of some other variable, use the value or copy stored in
1820 CONST_AND_COPIES. */
1821 val
= SSA_NAME_VALUE (op
);
1826 if (query
->range_of_expr (r
, op
, stmt
) && r
.singleton_p (&single
))
1830 if (val
&& val
!= op
)
1832 /* Do not replace hard register operands in asm statements. */
1833 if (gimple_code (stmt
) == GIMPLE_ASM
1834 && !may_propagate_copy_into_asm (op
))
1837 /* Certain operands are not allowed to be copy propagated due
1838 to their interaction with exception handling and some GCC
1840 if (!may_propagate_copy (op
, val
))
1843 /* Do not propagate copies into BIVs.
1844 See PR23821 and PR62217 for how this can disturb IV and
1845 number of iteration analysis. */
1846 if (TREE_CODE (val
) != INTEGER_CST
)
1848 gimple
*def
= SSA_NAME_DEF_STMT (op
);
1849 if (gimple_code (def
) == GIMPLE_PHI
1850 && gimple_bb (def
)->loop_father
->header
== gimple_bb (def
))
1855 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1857 fprintf (dump_file
, " Replaced '");
1858 print_generic_expr (dump_file
, op
, dump_flags
);
1859 fprintf (dump_file
, "' with %s '",
1860 (TREE_CODE (val
) != SSA_NAME
? "constant" : "variable"));
1861 print_generic_expr (dump_file
, val
, dump_flags
);
1862 fprintf (dump_file
, "'\n");
1865 if (TREE_CODE (val
) != SSA_NAME
)
1866 opt_stats
.num_const_prop
++;
1868 opt_stats
.num_copy_prop
++;
1870 propagate_value (op_p
, val
);
1872 /* And note that we modified this statement. This is now
1873 safe, even if we changed virtual operands since we will
1874 rescan the statement and rewrite its operands again. */
1875 gimple_set_modified (stmt
, true);
1879 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1880 known value for that SSA_NAME (or NULL if no value is known).
1882 Propagate values from CONST_AND_COPIES into the uses, vuses and
1883 vdef_ops of STMT. */
1886 cprop_into_stmt (gimple
*stmt
, range_query
*query
)
1890 tree last_copy_propagated_op
= NULL
;
1892 FOR_EACH_SSA_USE_OPERAND (op_p
, stmt
, iter
, SSA_OP_USE
)
1894 tree old_op
= USE_FROM_PTR (op_p
);
1896 /* If we have A = B and B = A in the copy propagation tables
1897 (due to an equality comparison), avoid substituting B for A
1898 then A for B in the trivially discovered cases. This allows
1899 optimization of statements were A and B appear as input
1901 if (old_op
!= last_copy_propagated_op
)
1903 cprop_operand (stmt
, op_p
, query
);
1905 tree new_op
= USE_FROM_PTR (op_p
);
1906 if (new_op
!= old_op
&& TREE_CODE (new_op
) == SSA_NAME
)
1907 last_copy_propagated_op
= new_op
;
1912 /* If STMT contains a relational test, try to convert it into an
1913 equality test if there is only a single value which can ever
1916 For example, if the expression hash table contains:
1920 And we have a test within statement of i >= 1, then we can safely
1921 rewrite the test as i == 1 since there only a single value where
1924 This is similar to code in VRP. */
1927 dom_opt_dom_walker::test_for_singularity (gimple
*stmt
,
1928 avail_exprs_stack
*avail_exprs_stack
)
1930 /* We want to support gimple conditionals as well as assignments
1931 where the RHS contains a conditional. */
1932 if (is_gimple_assign (stmt
) || gimple_code (stmt
) == GIMPLE_COND
)
1934 enum tree_code code
= ERROR_MARK
;
1937 /* Extract the condition of interest from both forms we support. */
1938 if (is_gimple_assign (stmt
))
1940 code
= gimple_assign_rhs_code (stmt
);
1941 lhs
= gimple_assign_rhs1 (stmt
);
1942 rhs
= gimple_assign_rhs2 (stmt
);
1944 else if (gimple_code (stmt
) == GIMPLE_COND
)
1946 code
= gimple_cond_code (as_a
<gcond
*> (stmt
));
1947 lhs
= gimple_cond_lhs (as_a
<gcond
*> (stmt
));
1948 rhs
= gimple_cond_rhs (as_a
<gcond
*> (stmt
));
1951 /* We're looking for a relational test using LE/GE. Also note we can
1952 canonicalize LT/GT tests against constants into LE/GT tests. */
1953 if (code
== LE_EXPR
|| code
== GE_EXPR
1954 || ((code
== LT_EXPR
|| code
== GT_EXPR
)
1955 && TREE_CODE (rhs
) == INTEGER_CST
))
1957 /* For LT_EXPR and GT_EXPR, canonicalize to LE_EXPR and GE_EXPR. */
1958 if (code
== LT_EXPR
)
1959 rhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (rhs
),
1960 rhs
, build_int_cst (TREE_TYPE (rhs
), 1));
1962 if (code
== GT_EXPR
)
1963 rhs
= fold_build2 (PLUS_EXPR
, TREE_TYPE (rhs
),
1964 rhs
, build_int_cst (TREE_TYPE (rhs
), 1));
1966 /* Determine the code we want to check for in the hash table. */
1967 enum tree_code test_code
;
1968 if (code
== GE_EXPR
|| code
== GT_EXPR
)
1969 test_code
= LE_EXPR
;
1971 test_code
= GE_EXPR
;
1973 /* Update the dummy statement so we can query the hash tables. */
1974 gimple_cond_set_code (m_dummy_cond
, test_code
);
1975 gimple_cond_set_lhs (m_dummy_cond
, lhs
);
1976 gimple_cond_set_rhs (m_dummy_cond
, rhs
);
1978 = avail_exprs_stack
->lookup_avail_expr (m_dummy_cond
,
1981 /* If the lookup returned 1 (true), then the expression we
1982 queried was in the hash table. As a result there is only
1983 one value that makes the original conditional true. Update
1984 STMT accordingly. */
1985 if (cached_lhs
&& integer_onep (cached_lhs
))
1987 if (is_gimple_assign (stmt
))
1989 gimple_assign_set_rhs_code (stmt
, EQ_EXPR
);
1990 gimple_assign_set_rhs2 (stmt
, rhs
);
1991 gimple_set_modified (stmt
, true);
1995 gimple_set_modified (stmt
, true);
1996 gimple_cond_set_code (as_a
<gcond
*> (stmt
), EQ_EXPR
);
1997 gimple_cond_set_rhs (as_a
<gcond
*> (stmt
), rhs
);
1998 gimple_set_modified (stmt
, true);
2005 /* If STMT is a comparison of two uniform vectors reduce it to a comparison
2006 of scalar objects, otherwise leave STMT unchanged. */
2009 reduce_vector_comparison_to_scalar_comparison (gimple
*stmt
)
2011 if (gimple_code (stmt
) == GIMPLE_COND
)
2013 tree lhs
= gimple_cond_lhs (stmt
);
2014 tree rhs
= gimple_cond_rhs (stmt
);
2016 /* We may have a vector comparison where both arms are uniform
2017 vectors. If so, we can simplify the vector comparison down
2018 to a scalar comparison. */
2019 if (TREE_CODE (TREE_TYPE (lhs
)) == VECTOR_TYPE
2020 && TREE_CODE (TREE_TYPE (rhs
)) == VECTOR_TYPE
)
2022 /* If either operand is an SSA_NAME, then look back to its
2023 defining statement to try and get at a suitable source. */
2024 if (TREE_CODE (rhs
) == SSA_NAME
)
2026 gimple
*def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2027 if (gimple_assign_single_p (def_stmt
))
2028 rhs
= gimple_assign_rhs1 (def_stmt
);
2031 if (TREE_CODE (lhs
) == SSA_NAME
)
2033 gimple
*def_stmt
= SSA_NAME_DEF_STMT (lhs
);
2034 if (gimple_assign_single_p (def_stmt
))
2035 lhs
= gimple_assign_rhs1 (def_stmt
);
2038 /* Now see if they are both uniform vectors and if so replace
2039 the vector comparison with a scalar comparison. */
2040 tree rhs_elem
= rhs
? uniform_vector_p (rhs
) : NULL_TREE
;
2041 tree lhs_elem
= lhs
? uniform_vector_p (lhs
) : NULL_TREE
;
2042 if (rhs_elem
&& lhs_elem
)
2044 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2046 fprintf (dump_file
, "Reducing vector comparison: ");
2047 print_gimple_stmt (dump_file
, stmt
, 0);
2050 gimple_cond_set_rhs (as_a
<gcond
*>(stmt
), rhs_elem
);
2051 gimple_cond_set_lhs (as_a
<gcond
*>(stmt
), lhs_elem
);
2052 gimple_set_modified (stmt
, true);
2054 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2056 fprintf (dump_file
, "To scalar equivalent: ");
2057 print_gimple_stmt (dump_file
, stmt
, 0);
2058 fprintf (dump_file
, "\n");
2065 /* Optimize the statement in block BB pointed to by iterator SI.
2067 We try to perform some simplistic global redundancy elimination and
2068 constant propagation:
2070 1- To detect global redundancy, we keep track of expressions that have
2071 been computed in this block and its dominators. If we find that the
2072 same expression is computed more than once, we eliminate repeated
2073 computations by using the target of the first one.
2075 2- Constant values and copy assignments. This is used to do very
2076 simplistic constant and copy propagation. When a constant or copy
2077 assignment is found, we map the value on the RHS of the assignment to
2078 the variable in the LHS in the CONST_AND_COPIES table.
2080 3- Very simple redundant store elimination is performed.
2082 4- We can simplify a condition to a constant or from a relational
2083 condition to an equality condition. */
2086 dom_opt_dom_walker::optimize_stmt (basic_block bb
, gimple_stmt_iterator
*si
,
2089 gimple
*stmt
, *old_stmt
;
2090 bool may_optimize_p
;
2091 bool modified_p
= false;
2095 old_stmt
= stmt
= gsi_stmt (*si
);
2096 was_noreturn
= is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
);
2098 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2100 fprintf (dump_file
, "Optimizing statement ");
2101 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2104 /* STMT may be a comparison of uniform vectors that we can simplify
2105 down to a comparison of scalars. Do that transformation first
2106 so that all the scalar optimizations from here onward apply. */
2107 reduce_vector_comparison_to_scalar_comparison (stmt
);
2109 update_stmt_if_modified (stmt
);
2110 opt_stats
.num_stmts
++;
2112 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2113 cprop_into_stmt (stmt
, m_evrp_range_analyzer
);
2115 /* If the statement has been modified with constant replacements,
2116 fold its RHS before checking for redundant computations. */
2117 if (gimple_modified_p (stmt
))
2121 /* Try to fold the statement making sure that STMT is kept
2125 stmt
= gsi_stmt (*si
);
2126 gimple_set_modified (stmt
, true);
2128 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2130 fprintf (dump_file
, " Folded to: ");
2131 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2135 /* We only need to consider cases that can yield a gimple operand. */
2136 if (gimple_assign_single_p (stmt
))
2137 rhs
= gimple_assign_rhs1 (stmt
);
2138 else if (gimple_code (stmt
) == GIMPLE_GOTO
)
2139 rhs
= gimple_goto_dest (stmt
);
2140 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
2141 /* This should never be an ADDR_EXPR. */
2142 rhs
= gimple_switch_index (swtch_stmt
);
2144 if (rhs
&& TREE_CODE (rhs
) == ADDR_EXPR
)
2145 recompute_tree_invariant_for_addr_expr (rhs
);
2147 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2148 even if fold_stmt updated the stmt already and thus cleared
2149 gimple_modified_p flag on it. */
2153 /* Check for redundant computations. Do this optimization only
2154 for assignments that have no volatile ops and conditionals. */
2155 may_optimize_p
= (!gimple_has_side_effects (stmt
)
2156 && (is_gimple_assign (stmt
)
2157 || (is_gimple_call (stmt
)
2158 && gimple_call_lhs (stmt
) != NULL_TREE
)
2159 || gimple_code (stmt
) == GIMPLE_COND
2160 || gimple_code (stmt
) == GIMPLE_SWITCH
));
2164 if (gimple_code (stmt
) == GIMPLE_CALL
)
2166 /* Resolve __builtin_constant_p. If it hasn't been
2167 folded to integer_one_node by now, it's fairly
2168 certain that the value simply isn't constant. */
2169 tree callee
= gimple_call_fndecl (stmt
);
2171 && fndecl_built_in_p (callee
, BUILT_IN_CONSTANT_P
))
2173 propagate_tree_value_into_stmt (si
, integer_zero_node
);
2174 stmt
= gsi_stmt (*si
);
2178 if (gimple_code (stmt
) == GIMPLE_COND
)
2180 tree lhs
= gimple_cond_lhs (stmt
);
2181 tree rhs
= gimple_cond_rhs (stmt
);
2183 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
2184 then this conditional is computable at compile time. We can just
2185 shove either 0 or 1 into the LHS, mark the statement as modified
2186 and all the right things will just happen below.
2188 Note this would apply to any case where LHS has a range
2189 narrower than its type implies and RHS is outside that
2190 narrower range. Future work. */
2191 if (TREE_CODE (lhs
) == SSA_NAME
2192 && ssa_name_has_boolean_range (lhs
)
2193 && TREE_CODE (rhs
) == INTEGER_CST
2194 && ! (integer_zerop (rhs
) || integer_onep (rhs
)))
2196 gimple_cond_set_lhs (as_a
<gcond
*> (stmt
),
2197 fold_convert (TREE_TYPE (lhs
),
2198 integer_zero_node
));
2199 gimple_set_modified (stmt
, true);
2201 else if (TREE_CODE (lhs
) == SSA_NAME
)
2203 /* Exploiting EVRP data is not yet fully integrated into DOM
2204 but we need to do something for this case to avoid regressing
2205 udr4.f90 and new1.C which have unexecutable blocks with
2206 undefined behavior that get diagnosed if they're left in the
2207 IL because we've attached range information to new
2209 update_stmt_if_modified (stmt
);
2210 edge taken_edge
= NULL
;
2211 simplify_using_ranges
simpl (m_evrp_range_analyzer
);
2212 simpl
.vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), &taken_edge
);
2215 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
2216 gimple_cond_make_true (as_a
<gcond
*> (stmt
));
2217 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
2218 gimple_cond_make_false (as_a
<gcond
*> (stmt
));
2221 gimple_set_modified (stmt
, true);
2229 update_stmt_if_modified (stmt
);
2230 eliminate_redundant_computations (si
, m_const_and_copies
,
2231 m_avail_exprs_stack
);
2232 stmt
= gsi_stmt (*si
);
2234 /* Perform simple redundant store elimination. */
2235 if (gimple_assign_single_p (stmt
)
2236 && TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
2238 tree lhs
= gimple_assign_lhs (stmt
);
2239 tree rhs
= gimple_assign_rhs1 (stmt
);
2242 rhs
= dom_valueize (rhs
);
2243 /* Build a new statement with the RHS and LHS exchanged. */
2244 if (TREE_CODE (rhs
) == SSA_NAME
)
2246 gimple
*defstmt
= SSA_NAME_DEF_STMT (rhs
);
2247 new_stmt
= gimple_build_assign (rhs
, lhs
);
2248 SSA_NAME_DEF_STMT (rhs
) = defstmt
;
2251 new_stmt
= gimple_build_assign (rhs
, lhs
);
2252 gimple_set_vuse (new_stmt
, gimple_vuse (stmt
));
2253 expr_hash_elt
*elt
= NULL
;
2254 cached_lhs
= m_avail_exprs_stack
->lookup_avail_expr (new_stmt
, false,
2257 && operand_equal_p (rhs
, cached_lhs
, 0)
2258 && refs_same_for_tbaa_p (elt
->expr ()->kind
== EXPR_SINGLE
2259 ? elt
->expr ()->ops
.single
.rhs
2262 basic_block bb
= gimple_bb (stmt
);
2263 unlink_stmt_vdef (stmt
);
2264 if (gsi_remove (si
, true))
2266 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
2267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2268 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2270 release_defs (stmt
);
2276 /* If this statement was not redundant, we may still be able to simplify
2277 it, which may in turn allow other part of DOM or other passes to do
2279 test_for_singularity (stmt
, m_avail_exprs_stack
);
2282 /* Record any additional equivalences created by this statement. */
2283 if (is_gimple_assign (stmt
))
2284 record_equivalences_from_stmt (stmt
, may_optimize_p
, m_avail_exprs_stack
);
2286 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
2287 know where it goes. */
2288 if (gimple_modified_p (stmt
) || modified_p
)
2292 if (gimple_code (stmt
) == GIMPLE_COND
)
2293 val
= fold_binary_loc (gimple_location (stmt
),
2294 gimple_cond_code (stmt
), boolean_type_node
,
2295 gimple_cond_lhs (stmt
),
2296 gimple_cond_rhs (stmt
));
2297 else if (gswitch
*swtch_stmt
= dyn_cast
<gswitch
*> (stmt
))
2298 val
= gimple_switch_index (swtch_stmt
);
2300 if (val
&& TREE_CODE (val
) == INTEGER_CST
)
2302 retval
= find_taken_edge (bb
, val
);
2305 /* Fix the condition to be either true or false. */
2306 if (gimple_code (stmt
) == GIMPLE_COND
)
2308 if (integer_zerop (val
))
2309 gimple_cond_make_false (as_a
<gcond
*> (stmt
));
2310 else if (integer_onep (val
))
2311 gimple_cond_make_true (as_a
<gcond
*> (stmt
));
2315 gimple_set_modified (stmt
, true);
2318 /* Further simplifications may be possible. */
2323 update_stmt_if_modified (stmt
);
2325 /* If we simplified a statement in such a way as to be shown that it
2326 cannot trap, update the eh information and the cfg to match. */
2327 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
2329 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
2330 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2331 fprintf (dump_file
, " Flagged to clear EH edges.\n");
2335 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
2336 need_noreturn_fixup
.safe_push (stmt
);