[aarch64] Use op_mode instead of vmode in aarch64_vectorize_vec_perm_const.
[official-gcc.git] / gcc / tree-ssa-dom.cc
blobf5e8f57499760f04f07f4c85f00765899ec1329e
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "cfganal.h"
32 #include "cfgloop.h"
33 #include "gimple-iterator.h"
34 #include "gimple-fold.h"
35 #include "tree-eh.h"
36 #include "tree-inline.h"
37 #include "tree-cfg.h"
38 #include "tree-into-ssa.h"
39 #include "domwalk.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"
45 #include "gimplify.h"
46 #include "tree-cfgcleanup.h"
47 #include "dbgcnt.h"
48 #include "alloc-pool.h"
49 #include "tree-vrp.h"
50 #include "vr-values.h"
51 #include "gimple-range.h"
52 #include "gimple-range-path.h"
53 #include "alias.h"
55 /* This file implements optimizations on the dominator tree. */
57 /* Structure for recording edge equivalences.
59 Computing and storing the edge equivalences instead of creating
60 them on-demand can save significant amounts of time, particularly
61 for pathological cases involving switch statements.
63 These structures live for a single iteration of the dominator
64 optimizer in the edge's AUX field. At the end of an iteration we
65 free each of these structures. */
66 class edge_info
68 public:
69 typedef std::pair <tree, tree> equiv_pair;
70 edge_info (edge);
71 ~edge_info ();
73 /* Record a simple LHS = RHS equivalence. This may trigger
74 calls to derive_equivalences. */
75 void record_simple_equiv (tree, tree);
77 /* If traversing this edge creates simple equivalences, we store
78 them as LHS/RHS pairs within this vector. */
79 vec<equiv_pair> simple_equivalences;
81 /* Traversing an edge may also indicate one or more particular conditions
82 are true or false. */
83 vec<cond_equivalence> cond_equivalences;
85 private:
86 /* Derive equivalences by walking the use-def chains. */
87 void derive_equivalences (tree, tree, int);
90 /* Track whether or not we have changed the control flow graph. */
91 static bool cfg_altered;
93 /* Bitmap of blocks that have had EH statements cleaned. We should
94 remove their dead edges eventually. */
95 static bitmap need_eh_cleanup;
96 static vec<gimple *> need_noreturn_fixup;
98 /* Statistics for dominator optimizations. */
99 struct opt_stats_d
101 long num_stmts;
102 long num_exprs_considered;
103 long num_re;
104 long num_const_prop;
105 long num_copy_prop;
108 static struct opt_stats_d opt_stats;
110 /* Local functions. */
111 static void record_equality (tree, tree, class const_and_copies *);
112 static void record_equivalences_from_phis (basic_block);
113 static void record_equivalences_from_incoming_edge (basic_block,
114 class const_and_copies *,
115 class avail_exprs_stack *,
116 bitmap blocks_on_stack);
117 static void eliminate_redundant_computations (gimple_stmt_iterator *,
118 class const_and_copies *,
119 class avail_exprs_stack *);
120 static void record_equivalences_from_stmt (gimple *, int,
121 class avail_exprs_stack *);
122 static void dump_dominator_optimization_stats (FILE *file,
123 hash_table<expr_elt_hasher> *);
124 static void record_temporary_equivalences (edge, class const_and_copies *,
125 class avail_exprs_stack *, bitmap);
127 /* Constructor for EDGE_INFO. An EDGE_INFO instance is always
128 associated with an edge E. */
130 edge_info::edge_info (edge e)
132 /* Free the old one associated with E, if it exists and
133 associate our new object with E. */
134 free_dom_edge_info (e);
135 e->aux = this;
137 /* And initialize the embedded vectors. */
138 simple_equivalences = vNULL;
139 cond_equivalences = vNULL;
142 /* Destructor just needs to release the vectors. */
144 edge_info::~edge_info (void)
146 this->cond_equivalences.release ();
147 this->simple_equivalences.release ();
150 /* NAME is known to have the value VALUE, which must be a constant.
152 Walk through its use-def chain to see if there are other equivalences
153 we might be able to derive.
155 RECURSION_LIMIT controls how far back we recurse through the use-def
156 chains. */
158 void
159 edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
161 if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
162 return;
164 /* This records the equivalence for the toplevel object. Do
165 this before checking the recursion limit. */
166 simple_equivalences.safe_push (equiv_pair (name, value));
168 /* Limit how far up the use-def chains we are willing to walk. */
169 if (recursion_limit == 0)
170 return;
172 /* We can walk up the use-def chains to potentially find more
173 equivalences. */
174 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
175 if (is_gimple_assign (def_stmt))
177 enum tree_code code = gimple_assign_rhs_code (def_stmt);
178 switch (code)
180 /* If the result of an OR is zero, then its operands are, too. */
181 case BIT_IOR_EXPR:
182 if (integer_zerop (value))
184 tree rhs1 = gimple_assign_rhs1 (def_stmt);
185 tree rhs2 = gimple_assign_rhs2 (def_stmt);
187 value = build_zero_cst (TREE_TYPE (rhs1));
188 derive_equivalences (rhs1, value, recursion_limit - 1);
189 value = build_zero_cst (TREE_TYPE (rhs2));
190 derive_equivalences (rhs2, value, recursion_limit - 1);
192 break;
194 /* If the result of an AND is nonzero, then its operands are, too. */
195 case BIT_AND_EXPR:
196 if (!integer_zerop (value))
198 tree rhs1 = gimple_assign_rhs1 (def_stmt);
199 tree rhs2 = gimple_assign_rhs2 (def_stmt);
201 /* If either operand has a boolean range, then we
202 know its value must be one, otherwise we just know it
203 is nonzero. The former is clearly useful, I haven't
204 seen cases where the latter is helpful yet. */
205 if (TREE_CODE (rhs1) == SSA_NAME)
207 if (ssa_name_has_boolean_range (rhs1))
209 value = build_one_cst (TREE_TYPE (rhs1));
210 derive_equivalences (rhs1, value, recursion_limit - 1);
213 if (TREE_CODE (rhs2) == SSA_NAME)
215 if (ssa_name_has_boolean_range (rhs2))
217 value = build_one_cst (TREE_TYPE (rhs2));
218 derive_equivalences (rhs2, value, recursion_limit - 1);
222 break;
224 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
225 set via a widening type conversion, then we may be able to record
226 additional equivalences. */
227 CASE_CONVERT:
229 tree rhs = gimple_assign_rhs1 (def_stmt);
230 tree rhs_type = TREE_TYPE (rhs);
231 if (INTEGRAL_TYPE_P (rhs_type)
232 && (TYPE_PRECISION (TREE_TYPE (name))
233 >= TYPE_PRECISION (rhs_type))
234 && int_fits_type_p (value, rhs_type))
235 derive_equivalences (rhs,
236 fold_convert (rhs_type, value),
237 recursion_limit - 1);
238 break;
241 /* We can invert the operation of these codes trivially if
242 one of the RHS operands is a constant to produce a known
243 value for the other RHS operand. */
244 case POINTER_PLUS_EXPR:
245 case PLUS_EXPR:
247 tree rhs1 = gimple_assign_rhs1 (def_stmt);
248 tree rhs2 = gimple_assign_rhs2 (def_stmt);
250 /* If either argument is a constant, then we can compute
251 a constant value for the nonconstant argument. */
252 if (TREE_CODE (rhs1) == INTEGER_CST
253 && TREE_CODE (rhs2) == SSA_NAME)
254 derive_equivalences (rhs2,
255 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
256 value, rhs1),
257 recursion_limit - 1);
258 else if (TREE_CODE (rhs2) == INTEGER_CST
259 && TREE_CODE (rhs1) == SSA_NAME)
260 derive_equivalences (rhs1,
261 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
262 value, rhs2),
263 recursion_limit - 1);
264 break;
267 /* If one of the operands is a constant, then we can compute
268 the value of the other operand. If both operands are
269 SSA_NAMEs, then they must be equal if the result is zero. */
270 case MINUS_EXPR:
272 tree rhs1 = gimple_assign_rhs1 (def_stmt);
273 tree rhs2 = gimple_assign_rhs2 (def_stmt);
275 /* If either argument is a constant, then we can compute
276 a constant value for the nonconstant argument. */
277 if (TREE_CODE (rhs1) == INTEGER_CST
278 && TREE_CODE (rhs2) == SSA_NAME)
279 derive_equivalences (rhs2,
280 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
281 rhs1, value),
282 recursion_limit - 1);
283 else if (TREE_CODE (rhs2) == INTEGER_CST
284 && TREE_CODE (rhs1) == SSA_NAME)
285 derive_equivalences (rhs1,
286 fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
287 value, rhs2),
288 recursion_limit - 1);
289 else if (integer_zerop (value))
291 tree cond = build2 (EQ_EXPR, boolean_type_node,
292 gimple_assign_rhs1 (def_stmt),
293 gimple_assign_rhs2 (def_stmt));
294 tree inverted = invert_truthvalue (cond);
295 record_conditions (&this->cond_equivalences, cond, inverted);
297 break;
300 case EQ_EXPR:
301 case NE_EXPR:
303 if ((code == EQ_EXPR && integer_onep (value))
304 || (code == NE_EXPR && integer_zerop (value)))
306 tree rhs1 = gimple_assign_rhs1 (def_stmt);
307 tree rhs2 = gimple_assign_rhs2 (def_stmt);
309 /* If either argument is a constant, then record the
310 other argument as being the same as that constant.
312 If neither operand is a constant, then we have a
313 conditional name == name equivalence. */
314 if (TREE_CODE (rhs1) == INTEGER_CST)
315 derive_equivalences (rhs2, rhs1, recursion_limit - 1);
316 else if (TREE_CODE (rhs2) == INTEGER_CST)
317 derive_equivalences (rhs1, rhs2, recursion_limit - 1);
319 else
321 tree cond = build2 (code, boolean_type_node,
322 gimple_assign_rhs1 (def_stmt),
323 gimple_assign_rhs2 (def_stmt));
324 tree inverted = invert_truthvalue (cond);
325 if (integer_zerop (value))
326 std::swap (cond, inverted);
327 record_conditions (&this->cond_equivalences, cond, inverted);
329 break;
332 /* For BIT_NOT and NEGATE, we can just apply the operation to the
333 VALUE to get the new equivalence. It will always be a constant
334 so we can recurse. */
335 case BIT_NOT_EXPR:
336 case NEGATE_EXPR:
338 tree rhs = gimple_assign_rhs1 (def_stmt);
339 tree res;
340 /* If this is a NOT and the operand has a boolean range, then we
341 know its value must be zero or one. We are not supposed to
342 have a BIT_NOT_EXPR for boolean types with precision > 1 in
343 the general case, see e.g. the handling of TRUTH_NOT_EXPR in
344 the gimplifier, but it can be generated by match.pd out of
345 a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR. Now the handling
346 of BIT_AND_EXPR above already forces a specific semantics for
347 boolean types with precision > 1 so we must do the same here,
348 otherwise we could change the semantics of TRUTH_NOT_EXPR for
349 boolean types with precision > 1. */
350 if (code == BIT_NOT_EXPR
351 && TREE_CODE (rhs) == SSA_NAME
352 && ssa_name_has_boolean_range (rhs))
354 if ((TREE_INT_CST_LOW (value) & 1) == 0)
355 res = build_one_cst (TREE_TYPE (rhs));
356 else
357 res = build_zero_cst (TREE_TYPE (rhs));
359 else
360 res = fold_build1 (code, TREE_TYPE (rhs), value);
361 derive_equivalences (rhs, res, recursion_limit - 1);
362 break;
365 default:
367 if (TREE_CODE_CLASS (code) == tcc_comparison)
369 tree cond = build2 (code, boolean_type_node,
370 gimple_assign_rhs1 (def_stmt),
371 gimple_assign_rhs2 (def_stmt));
372 tree inverted = invert_truthvalue (cond);
373 if (integer_zerop (value))
374 std::swap (cond, inverted);
375 record_conditions (&this->cond_equivalences, cond, inverted);
376 break;
378 break;
384 void
385 edge_info::record_simple_equiv (tree lhs, tree rhs)
387 /* If the RHS is a constant, then we may be able to derive
388 further equivalences. Else just record the name = name
389 equivalence. */
390 if (TREE_CODE (rhs) == INTEGER_CST)
391 derive_equivalences (lhs, rhs, 4);
392 else
393 simple_equivalences.safe_push (equiv_pair (lhs, rhs));
396 /* Free the edge_info data attached to E, if it exists. */
398 void
399 free_dom_edge_info (edge e)
401 class edge_info *edge_info = (class edge_info *)e->aux;
403 if (edge_info)
404 delete edge_info;
407 /* Free all EDGE_INFO structures associated with edges in the CFG.
408 If a particular edge can be threaded, copy the redirection
409 target from the EDGE_INFO structure into the edge's AUX field
410 as required by code to update the CFG and SSA graph for
411 jump threading. */
413 static void
414 free_all_edge_infos (void)
416 basic_block bb;
417 edge_iterator ei;
418 edge e;
420 FOR_EACH_BB_FN (bb, cfun)
422 FOR_EACH_EDGE (e, ei, bb->preds)
424 free_dom_edge_info (e);
425 e->aux = NULL;
430 /* We have finished optimizing BB, record any information implied by
431 taking a specific outgoing edge from BB. */
433 static void
434 record_edge_info (basic_block bb)
436 gimple_stmt_iterator gsi = gsi_last_bb (bb);
437 class edge_info *edge_info;
439 if (! gsi_end_p (gsi))
441 gimple *stmt = gsi_stmt (gsi);
442 location_t loc = gimple_location (stmt);
444 if (gimple_code (stmt) == GIMPLE_SWITCH)
446 gswitch *switch_stmt = as_a <gswitch *> (stmt);
447 tree index = gimple_switch_index (switch_stmt);
449 if (TREE_CODE (index) == SSA_NAME)
451 int i;
452 int n_labels = gimple_switch_num_labels (switch_stmt);
453 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
454 edge e;
455 edge_iterator ei;
457 for (i = 0; i < n_labels; i++)
459 tree label = gimple_switch_label (switch_stmt, i);
460 basic_block target_bb
461 = label_to_block (cfun, CASE_LABEL (label));
462 if (CASE_HIGH (label)
463 || !CASE_LOW (label)
464 || info[target_bb->index])
465 info[target_bb->index] = error_mark_node;
466 else
467 info[target_bb->index] = label;
470 FOR_EACH_EDGE (e, ei, bb->succs)
472 basic_block target_bb = e->dest;
473 tree label = info[target_bb->index];
475 if (label != NULL && label != error_mark_node)
477 tree x = fold_convert_loc (loc, TREE_TYPE (index),
478 CASE_LOW (label));
479 edge_info = new class edge_info (e);
480 edge_info->record_simple_equiv (index, x);
483 free (info);
487 /* A COND_EXPR may create equivalences too. */
488 if (gimple_code (stmt) == GIMPLE_COND)
490 edge true_edge;
491 edge false_edge;
493 tree op0 = gimple_cond_lhs (stmt);
494 tree op1 = gimple_cond_rhs (stmt);
495 enum tree_code code = gimple_cond_code (stmt);
497 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
499 /* Special case comparing booleans against a constant as we
500 know the value of OP0 on both arms of the branch. i.e., we
501 can record an equivalence for OP0 rather than COND.
503 However, don't do this if the constant isn't zero or one.
504 Such conditionals will get optimized more thoroughly during
505 the domwalk. */
506 if ((code == EQ_EXPR || code == NE_EXPR)
507 && TREE_CODE (op0) == SSA_NAME
508 && ssa_name_has_boolean_range (op0)
509 && is_gimple_min_invariant (op1)
510 && (integer_zerop (op1) || integer_onep (op1)))
512 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
513 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
515 if (code == EQ_EXPR)
517 edge_info = new class edge_info (true_edge);
518 edge_info->record_simple_equiv (op0,
519 (integer_zerop (op1)
520 ? false_val : true_val));
521 edge_info = new class edge_info (false_edge);
522 edge_info->record_simple_equiv (op0,
523 (integer_zerop (op1)
524 ? true_val : false_val));
526 else
528 edge_info = new class edge_info (true_edge);
529 edge_info->record_simple_equiv (op0,
530 (integer_zerop (op1)
531 ? true_val : false_val));
532 edge_info = new class edge_info (false_edge);
533 edge_info->record_simple_equiv (op0,
534 (integer_zerop (op1)
535 ? false_val : true_val));
538 /* This can show up in the IL as a result of copy propagation
539 it will eventually be canonicalized, but we have to cope
540 with this case within the pass. */
541 else if (is_gimple_min_invariant (op0)
542 && TREE_CODE (op1) == SSA_NAME)
544 tree cond = build2 (code, boolean_type_node, op0, op1);
545 tree inverted = invert_truthvalue_loc (loc, cond);
546 bool can_infer_simple_equiv
547 = !(HONOR_SIGNED_ZEROS (op0)
548 && real_zerop (op0));
549 class edge_info *edge_info;
551 edge_info = new class edge_info (true_edge);
552 record_conditions (&edge_info->cond_equivalences, cond, inverted);
554 if (can_infer_simple_equiv && code == EQ_EXPR)
555 edge_info->record_simple_equiv (op1, op0);
557 edge_info = new class edge_info (false_edge);
558 record_conditions (&edge_info->cond_equivalences, inverted, cond);
560 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
561 edge_info->record_simple_equiv (op1, op0);
564 else if (TREE_CODE (op0) == SSA_NAME
565 && (TREE_CODE (op1) == SSA_NAME
566 || is_gimple_min_invariant (op1)))
568 tree cond = build2 (code, boolean_type_node, op0, op1);
569 tree inverted = invert_truthvalue_loc (loc, cond);
570 bool can_infer_simple_equiv
571 = !(HONOR_SIGNED_ZEROS (op1)
572 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
573 class edge_info *edge_info;
575 edge_info = new class edge_info (true_edge);
576 record_conditions (&edge_info->cond_equivalences, cond, inverted);
578 if (can_infer_simple_equiv && code == EQ_EXPR)
579 edge_info->record_simple_equiv (op0, op1);
581 edge_info = new class edge_info (false_edge);
582 record_conditions (&edge_info->cond_equivalences, inverted, cond);
584 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
585 edge_info->record_simple_equiv (op0, op1);
591 class dom_jt_state : public jt_state
593 public:
594 dom_jt_state (const_and_copies *copies, avail_exprs_stack *avails)
595 : m_copies (copies), m_avails (avails)
597 bitmap_tree_view (m_blocks_on_stack);
599 void push (edge e) override
601 m_copies->push_marker ();
602 m_avails->push_marker ();
603 jt_state::push (e);
605 void pop () override
607 m_copies->pop_to_marker ();
608 m_avails->pop_to_marker ();
609 jt_state::pop ();
611 void register_equivs_edge (edge e) override
613 record_temporary_equivalences (e, m_copies, m_avails, m_blocks_on_stack);
615 void register_equiv (tree dest, tree src, bool update) override;
616 bitmap get_blocks_on_stack () { return m_blocks_on_stack; }
617 private:
618 const_and_copies *m_copies;
619 avail_exprs_stack *m_avails;
620 /* Set of blocks on the stack, to be used for medium-fast
621 dominance queries in back_propagate_equivalences. */
622 auto_bitmap m_blocks_on_stack;
625 void
626 dom_jt_state::register_equiv (tree dest, tree src, bool)
628 m_copies->record_const_or_copy (dest, src);
631 class dom_jt_simplifier : public hybrid_jt_simplifier
633 public:
634 dom_jt_simplifier (avail_exprs_stack *avails, gimple_ranger *ranger,
635 path_range_query *query)
636 : hybrid_jt_simplifier (ranger, query), m_avails (avails) { }
638 private:
639 tree simplify (gimple *, gimple *, basic_block, jt_state *) override;
640 avail_exprs_stack *m_avails;
643 tree
644 dom_jt_simplifier::simplify (gimple *stmt, gimple *within_stmt,
645 basic_block bb, jt_state *state)
647 /* First see if the conditional is in the hash table. */
648 tree cached_lhs = m_avails->lookup_avail_expr (stmt, false, true);
649 if (cached_lhs)
650 return cached_lhs;
652 /* Otherwise call the ranger if possible. */
653 if (state)
654 return hybrid_jt_simplifier::simplify (stmt, within_stmt, bb, state);
656 return NULL;
659 class dom_opt_dom_walker : public dom_walker
661 public:
662 dom_opt_dom_walker (cdi_direction direction,
663 jump_threader *threader,
664 dom_jt_state *state,
665 gimple_ranger *ranger,
666 const_and_copies *const_and_copies,
667 avail_exprs_stack *avail_exprs_stack)
668 : dom_walker (direction, REACHABLE_BLOCKS)
670 m_ranger = ranger;
671 m_state = state;
672 m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
673 integer_zero_node, NULL, NULL);
674 m_const_and_copies = const_and_copies;
675 m_avail_exprs_stack = avail_exprs_stack;
676 m_threader = threader;
679 edge before_dom_children (basic_block) final override;
680 void after_dom_children (basic_block) final override;
682 private:
684 /* Unwindable equivalences, both const/copy and expression varieties. */
685 class const_and_copies *m_const_and_copies;
686 class avail_exprs_stack *m_avail_exprs_stack;
688 /* Dummy condition to avoid creating lots of throw away statements. */
689 gcond *m_dummy_cond;
691 /* Optimize a single statement within a basic block using the
692 various tables mantained by DOM. Returns the taken edge if
693 the statement is a conditional with a statically determined
694 value. */
695 edge optimize_stmt (basic_block, gimple_stmt_iterator *, bool *);
697 void set_global_ranges_from_unreachable_edges (basic_block);
699 void test_for_singularity (gimple *, avail_exprs_stack *);
700 edge fold_cond (gcond *cond);
702 jump_threader *m_threader;
703 gimple_ranger *m_ranger;
704 dom_jt_state *m_state;
707 /* Jump threading, redundancy elimination and const/copy propagation.
709 This pass may expose new symbols that need to be renamed into SSA. For
710 every new symbol exposed, its corresponding bit will be set in
711 VARS_TO_RENAME. */
713 namespace {
715 const pass_data pass_data_dominator =
717 GIMPLE_PASS, /* type */
718 "dom", /* name */
719 OPTGROUP_NONE, /* optinfo_flags */
720 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
721 ( PROP_cfg | PROP_ssa ), /* properties_required */
722 0, /* properties_provided */
723 0, /* properties_destroyed */
724 0, /* todo_flags_start */
725 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
728 class pass_dominator : public gimple_opt_pass
730 public:
731 pass_dominator (gcc::context *ctxt)
732 : gimple_opt_pass (pass_data_dominator, ctxt),
733 may_peel_loop_headers_p (false)
736 /* opt_pass methods: */
737 opt_pass * clone () final override { return new pass_dominator (m_ctxt); }
738 void set_pass_param (unsigned int n, bool param) final override
740 gcc_assert (n == 0);
741 may_peel_loop_headers_p = param;
743 bool gate (function *) final override { return flag_tree_dom != 0; }
744 unsigned int execute (function *) final override;
746 private:
747 /* This flag is used to prevent loops from being peeled repeatedly in jump
748 threading; it will be removed once we preserve loop structures throughout
749 the compilation -- we will be able to mark the affected loops directly in
750 jump threading, and avoid peeling them next time. */
751 bool may_peel_loop_headers_p;
752 }; // class pass_dominator
754 unsigned int
755 pass_dominator::execute (function *fun)
757 memset (&opt_stats, 0, sizeof (opt_stats));
759 /* Create our hash tables. */
760 hash_table<expr_elt_hasher> *avail_exprs
761 = new hash_table<expr_elt_hasher> (1024);
762 class avail_exprs_stack *avail_exprs_stack
763 = new class avail_exprs_stack (avail_exprs);
764 class const_and_copies *const_and_copies = new class const_and_copies ();
765 need_eh_cleanup = BITMAP_ALLOC (NULL);
766 need_noreturn_fixup.create (0);
768 calculate_dominance_info (CDI_DOMINATORS);
769 cfg_altered = false;
771 /* We need to know loop structures in order to avoid destroying them
772 in jump threading. Note that we still can e.g. thread through loop
773 headers to an exit edge, or through loop header to the loop body, assuming
774 that we update the loop info.
776 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
777 to several overly conservative bail-outs in jump threading, case
778 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
779 missing. We should improve jump threading in future then
780 LOOPS_HAVE_PREHEADERS won't be needed here. */
781 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES
782 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
784 /* We need accurate information regarding back edges in the CFG
785 for jump threading; this may include back edges that are not part of
786 a single loop. */
787 mark_dfs_back_edges ();
789 /* We want to create the edge info structures before the dominator walk
790 so that they'll be in place for the jump threader, particularly when
791 threading through a join block.
793 The conditions will be lazily updated with global equivalences as
794 we reach them during the dominator walk. */
795 basic_block bb;
796 FOR_EACH_BB_FN (bb, fun)
797 record_edge_info (bb);
799 /* Recursively walk the dominator tree optimizing statements. */
800 gimple_ranger *ranger = enable_ranger (fun);
801 path_range_query path_query (/*resolve=*/true, ranger);
802 dom_jt_simplifier simplifier (avail_exprs_stack, ranger, &path_query);
803 dom_jt_state state (const_and_copies, avail_exprs_stack);
804 jump_threader threader (&simplifier, &state);
805 dom_opt_dom_walker walker (CDI_DOMINATORS,
806 &threader,
807 &state,
808 ranger,
809 const_and_copies,
810 avail_exprs_stack);
811 walker.walk (fun->cfg->x_entry_block_ptr);
813 ranger->export_global_ranges ();
814 disable_ranger (fun);
816 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
817 edge. When found, remove jump threads which contain any outgoing
818 edge from the affected block. */
819 if (cfg_altered)
821 FOR_EACH_BB_FN (bb, fun)
823 edge_iterator ei;
824 edge e;
826 /* First see if there are any edges without EDGE_EXECUTABLE
827 set. */
828 bool found = false;
829 FOR_EACH_EDGE (e, ei, bb->succs)
831 if ((e->flags & EDGE_EXECUTABLE) == 0)
833 found = true;
834 break;
838 /* If there were any such edges found, then remove jump threads
839 containing any edge leaving BB. */
840 if (found)
841 FOR_EACH_EDGE (e, ei, bb->succs)
842 threader.remove_jump_threads_including (e);
847 gimple_stmt_iterator gsi;
848 basic_block bb;
849 FOR_EACH_BB_FN (bb, fun)
851 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
852 update_stmt_if_modified (gsi_stmt (gsi));
856 /* If we exposed any new variables, go ahead and put them into
857 SSA form now, before we handle jump threading. This simplifies
858 interactions between rewriting of _DECL nodes into SSA form
859 and rewriting SSA_NAME nodes into SSA form after block
860 duplication and CFG manipulation. */
861 update_ssa (TODO_update_ssa);
863 free_all_edge_infos ();
865 /* Thread jumps, creating duplicate blocks as needed. */
866 cfg_altered |= threader.thread_through_all_blocks (may_peel_loop_headers_p);
868 if (cfg_altered)
869 free_dominance_info (CDI_DOMINATORS);
871 /* Removal of statements may make some EH edges dead. Purge
872 such edges from the CFG as needed. */
873 if (!bitmap_empty_p (need_eh_cleanup))
875 unsigned i;
876 bitmap_iterator bi;
878 /* Jump threading may have created forwarder blocks from blocks
879 needing EH cleanup; the new successor of these blocks, which
880 has inherited from the original block, needs the cleanup.
881 Don't clear bits in the bitmap, as that can break the bitmap
882 iterator. */
883 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
885 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
886 if (bb == NULL)
887 continue;
888 while (single_succ_p (bb)
889 && (single_succ_edge (bb)->flags
890 & (EDGE_EH|EDGE_DFS_BACK)) == 0)
891 bb = single_succ (bb);
892 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
893 continue;
894 if ((unsigned) bb->index != i)
895 bitmap_set_bit (need_eh_cleanup, bb->index);
898 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
899 bitmap_clear (need_eh_cleanup);
902 /* Fixup stmts that became noreturn calls. This may require splitting
903 blocks and thus isn't possible during the dominator walk or before
904 jump threading finished. Do this in reverse order so we don't
905 inadvertedly remove a stmt we want to fixup by visiting a dominating
906 now noreturn call first. */
907 while (!need_noreturn_fixup.is_empty ())
909 gimple *stmt = need_noreturn_fixup.pop ();
910 if (dump_file && dump_flags & TDF_DETAILS)
912 fprintf (dump_file, "Fixing up noreturn call ");
913 print_gimple_stmt (dump_file, stmt, 0);
914 fprintf (dump_file, "\n");
916 fixup_noreturn_call (stmt);
919 statistics_counter_event (fun, "Redundant expressions eliminated",
920 opt_stats.num_re);
921 statistics_counter_event (fun, "Constants propagated",
922 opt_stats.num_const_prop);
923 statistics_counter_event (fun, "Copies propagated",
924 opt_stats.num_copy_prop);
926 /* Debugging dumps. */
927 if (dump_file && (dump_flags & TDF_STATS))
928 dump_dominator_optimization_stats (dump_file, avail_exprs);
930 loop_optimizer_finalize ();
932 /* Delete our main hashtable. */
933 delete avail_exprs;
934 avail_exprs = NULL;
936 /* Free asserted bitmaps and stacks. */
937 BITMAP_FREE (need_eh_cleanup);
938 need_noreturn_fixup.release ();
939 delete avail_exprs_stack;
940 delete const_and_copies;
942 return 0;
945 } // anon namespace
947 gimple_opt_pass *
948 make_pass_dominator (gcc::context *ctxt)
950 return new pass_dominator (ctxt);
953 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
955 static tree
956 dom_valueize (tree t)
958 if (TREE_CODE (t) == SSA_NAME)
960 tree tem = SSA_NAME_VALUE (t);
961 if (tem)
962 return tem;
964 return t;
967 /* We have just found an equivalence for LHS on an edge E.
968 Look backwards to other uses of LHS and see if we can derive
969 additional equivalences that are valid on edge E. */
970 static void
971 back_propagate_equivalences (tree lhs, edge e,
972 class const_and_copies *const_and_copies,
973 bitmap domby)
975 use_operand_p use_p;
976 imm_use_iterator iter;
977 basic_block dest = e->dest;
978 bool domok = (dom_info_state (CDI_DOMINATORS) == DOM_OK);
980 /* Iterate over the uses of LHS to see if any dominate E->dest.
981 If so, they may create useful equivalences too.
983 ??? If the code gets re-organized to a worklist to catch more
984 indirect opportunities and it is made to handle PHIs then this
985 should only consider use_stmts in basic-blocks we have already visited. */
986 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
988 gimple *use_stmt = USE_STMT (use_p);
990 /* Often the use is in DEST, which we trivially know we can't use.
991 This is cheaper than the dominator set tests below. */
992 if (dest == gimple_bb (use_stmt))
993 continue;
995 /* Filter out statements that can never produce a useful
996 equivalence. */
997 tree lhs2 = gimple_get_lhs (use_stmt);
998 if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
999 continue;
1001 if (domok)
1003 if (!dominated_by_p (CDI_DOMINATORS, dest, gimple_bb (use_stmt)))
1004 continue;
1006 else
1008 /* We can use the set of BBs on the stack from a domwalk
1009 for a medium fast way to query dominance. Profiling
1010 has shown non-fast query dominance tests here can be fairly
1011 expensive. */
1012 /* This tests if USE_STMT does not dominate DEST. */
1013 if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
1014 continue;
1017 /* At this point USE_STMT dominates DEST and may result in a
1018 useful equivalence. Try to simplify its RHS to a constant
1019 or SSA_NAME. */
1020 tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
1021 no_follow_ssa_edges);
1022 if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
1023 record_equality (lhs2, res, const_and_copies);
1027 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
1028 by traversing edge E (which are cached in E->aux).
1030 Callers are responsible for managing the unwinding markers. */
1031 static void
1032 record_temporary_equivalences (edge e,
1033 class const_and_copies *const_and_copies,
1034 class avail_exprs_stack *avail_exprs_stack,
1035 bitmap blocks_on_stack)
1037 int i;
1038 class edge_info *edge_info = (class edge_info *) e->aux;
1040 /* If we have info associated with this edge, record it into
1041 our equivalence tables. */
1042 if (edge_info)
1044 cond_equivalence *eq;
1045 /* If we have 0 = COND or 1 = COND equivalences, record them
1046 into our expression hash tables. */
1047 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1048 avail_exprs_stack->record_cond (eq);
1050 edge_info::equiv_pair *seq;
1051 for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1053 tree lhs = seq->first;
1054 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
1055 continue;
1057 /* Record the simple NAME = VALUE equivalence. */
1058 tree rhs = seq->second;
1060 /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
1061 cheaper to compute than the other, then set up the equivalence
1062 such that we replace the expensive one with the cheap one.
1064 If they are the same cost to compute, then do not record
1065 anything. */
1066 if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
1068 gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
1069 int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
1071 gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
1072 int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
1074 if (rhs_cost > lhs_cost)
1075 record_equality (rhs, lhs, const_and_copies);
1076 else if (rhs_cost < lhs_cost)
1077 record_equality (lhs, rhs, const_and_copies);
1079 else
1080 record_equality (lhs, rhs, const_and_copies);
1083 /* Any equivalence found for LHS may result in additional
1084 equivalences for other uses of LHS that we have already
1085 processed. */
1086 back_propagate_equivalences (lhs, e, const_and_copies,
1087 blocks_on_stack);
1092 /* PHI nodes can create equivalences too.
1094 Ignoring any alternatives which are the same as the result, if
1095 all the alternatives are equal, then the PHI node creates an
1096 equivalence. */
1098 static void
1099 record_equivalences_from_phis (basic_block bb)
1101 gphi_iterator gsi;
1103 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1105 gphi *phi = gsi.phi ();
1107 /* We might eliminate the PHI, so advance GSI now. */
1108 gsi_next (&gsi);
1110 tree lhs = gimple_phi_result (phi);
1111 tree rhs = NULL;
1112 size_t i;
1114 for (i = 0; i < gimple_phi_num_args (phi); i++)
1116 tree t = gimple_phi_arg_def (phi, i);
1118 /* Ignore alternatives which are the same as our LHS. Since
1119 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1120 can simply compare pointers. */
1121 if (lhs == t)
1122 continue;
1124 /* If the associated edge is not marked as executable, then it
1125 can be ignored. */
1126 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
1127 continue;
1129 t = dom_valueize (t);
1131 /* If T is an SSA_NAME and its associated edge is a backedge,
1132 then quit as we cannot utilize this equivalence. */
1133 if (TREE_CODE (t) == SSA_NAME
1134 && (gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK))
1135 break;
1137 /* If we have not processed an alternative yet, then set
1138 RHS to this alternative. */
1139 if (rhs == NULL)
1140 rhs = t;
1141 /* If we have processed an alternative (stored in RHS), then
1142 see if it is equal to this one. If it isn't, then stop
1143 the search. */
1144 else if (! operand_equal_for_phi_arg_p (rhs, t))
1145 break;
1148 /* If we had no interesting alternatives, then all the RHS alternatives
1149 must have been the same as LHS. */
1150 if (!rhs)
1151 rhs = lhs;
1153 /* If we managed to iterate through each PHI alternative without
1154 breaking out of the loop, then we have a PHI which may create
1155 a useful equivalence. We do not need to record unwind data for
1156 this, since this is a true assignment and not an equivalence
1157 inferred from a comparison. All uses of this ssa name are dominated
1158 by this assignment, so unwinding just costs time and space. */
1159 if (i == gimple_phi_num_args (phi))
1161 if (may_propagate_copy (lhs, rhs))
1162 set_ssa_name_value (lhs, rhs);
1163 else if (virtual_operand_p (lhs))
1165 gimple *use_stmt;
1166 imm_use_iterator iter;
1167 use_operand_p use_p;
1168 /* For virtual operands we have to propagate into all uses as
1169 otherwise we will create overlapping life-ranges. */
1170 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
1171 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1172 SET_USE (use_p, rhs);
1173 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1174 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
1175 gimple_stmt_iterator tmp_gsi = gsi_for_stmt (phi);
1176 remove_phi_node (&tmp_gsi, true);
1182 /* Return true if all uses of NAME are dominated by STMT or feed STMT
1183 via a chain of single immediate uses. */
1185 static bool
1186 all_uses_feed_or_dominated_by_stmt (tree name, gimple *stmt)
1188 use_operand_p use_p, use2_p;
1189 imm_use_iterator iter;
1190 basic_block stmt_bb = gimple_bb (stmt);
1192 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
1194 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
1195 if (use_stmt == stmt
1196 || is_gimple_debug (use_stmt)
1197 || (gimple_bb (use_stmt) != stmt_bb
1198 && dominated_by_p (CDI_DOMINATORS,
1199 gimple_bb (use_stmt), stmt_bb)))
1200 continue;
1201 while (use_stmt != stmt
1202 && is_gimple_assign (use_stmt)
1203 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
1204 && single_imm_use (gimple_assign_lhs (use_stmt),
1205 &use2_p, &use_stmt2))
1206 use_stmt = use_stmt2;
1207 if (use_stmt != stmt)
1208 return false;
1210 return true;
1213 /* Set global ranges that can be determined from the C->M edge:
1215 <bb C>:
1217 if (something)
1218 goto <bb N>;
1219 else
1220 goto <bb M>;
1221 <bb N>:
1222 __builtin_unreachable ();
1223 <bb M>:
1226 void
1227 dom_opt_dom_walker::set_global_ranges_from_unreachable_edges (basic_block bb)
1229 edge pred_e = single_pred_edge_ignoring_loop_edges (bb, false);
1231 if (!pred_e)
1232 return;
1234 gimple *stmt = last_stmt (pred_e->src);
1235 tree name;
1236 int_range_max r;
1237 if (stmt
1238 && gimple_code (stmt) == GIMPLE_COND
1239 && (name = gimple_cond_lhs (stmt))
1240 && TREE_CODE (name) == SSA_NAME
1241 && r.supports_type_p (TREE_TYPE (name))
1242 && assert_unreachable_fallthru_edge_p (pred_e)
1243 && all_uses_feed_or_dominated_by_stmt (name, stmt)
1244 && m_ranger->range_on_edge (r, pred_e, name)
1245 && !r.varying_p ()
1246 && !r.undefined_p ())
1248 set_range_info (name, r);
1249 maybe_set_nonzero_bits (pred_e, name);
1253 /* Record any equivalences created by the incoming edge to BB into
1254 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1255 incoming edge, then no equivalence is created. */
1257 static void
1258 record_equivalences_from_incoming_edge (basic_block bb,
1259 class const_and_copies *const_and_copies,
1260 class avail_exprs_stack *avail_exprs_stack,
1261 bitmap blocks_on_stack)
1263 edge e;
1264 basic_block parent;
1266 /* If our parent block ended with a control statement, then we may be
1267 able to record some equivalences based on which outgoing edge from
1268 the parent was followed. */
1269 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1271 e = single_pred_edge_ignoring_loop_edges (bb, true);
1273 /* If we had a single incoming edge from our parent block, then enter
1274 any data associated with the edge into our tables. */
1275 if (e && e->src == parent)
1276 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack,
1277 blocks_on_stack);
1280 /* Dump statistics for the hash table HTAB. */
1282 static void
1283 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1285 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1286 (long) htab.size (),
1287 (long) htab.elements (),
1288 htab.collisions ());
1291 /* Dump SSA statistics on FILE. */
1293 static void
1294 dump_dominator_optimization_stats (FILE *file,
1295 hash_table<expr_elt_hasher> *avail_exprs)
1297 fprintf (file, "Total number of statements: %6ld\n\n",
1298 opt_stats.num_stmts);
1299 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1300 opt_stats.num_exprs_considered);
1302 fprintf (file, "\nHash table statistics:\n");
1304 fprintf (file, " avail_exprs: ");
1305 htab_statistics (file, *avail_exprs);
1309 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1310 This constrains the cases in which we may treat this as assignment. */
1312 static void
1313 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1315 tree prev_x = NULL, prev_y = NULL;
1317 if (tree_swap_operands_p (x, y))
1318 std::swap (x, y);
1320 /* Most of the time tree_swap_operands_p does what we want. But there
1321 are cases where we know one operand is better for copy propagation than
1322 the other. Given no other code cares about ordering of equality
1323 comparison operators for that purpose, we just handle the special cases
1324 here. */
1325 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1327 /* If one operand is a single use operand, then make it
1328 X. This will preserve its single use properly and if this
1329 conditional is eliminated, the computation of X can be
1330 eliminated as well. */
1331 if (has_single_use (y) && ! has_single_use (x))
1332 std::swap (x, y);
1334 if (TREE_CODE (x) == SSA_NAME)
1335 prev_x = SSA_NAME_VALUE (x);
1336 if (TREE_CODE (y) == SSA_NAME)
1337 prev_y = SSA_NAME_VALUE (y);
1339 /* If one of the previous values is invariant, or invariant in more loops
1340 (by depth), then use that.
1341 Otherwise it doesn't matter which value we choose, just so
1342 long as we canonicalize on one value. */
1343 if (is_gimple_min_invariant (y))
1345 else if (is_gimple_min_invariant (x))
1346 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1347 else if (prev_x && is_gimple_min_invariant (prev_x))
1348 x = y, y = prev_x, prev_x = prev_y;
1349 else if (prev_y)
1350 y = prev_y;
1352 /* After the swapping, we must have one SSA_NAME. */
1353 if (TREE_CODE (x) != SSA_NAME)
1354 return;
1356 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1357 variable compared against zero. If we're honoring signed zeros,
1358 then we cannot record this value unless we know that the value is
1359 nonzero. */
1360 if (HONOR_SIGNED_ZEROS (x)
1361 && (TREE_CODE (y) != REAL_CST
1362 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1363 return;
1365 const_and_copies->record_const_or_copy (x, y, prev_x);
1368 /* Returns true when STMT is a simple iv increment. It detects the
1369 following situation:
1371 i_1 = phi (..., i_k)
1372 [...]
1373 i_j = i_{j-1} for each j : 2 <= j <= k-1
1374 [...]
1375 i_k = i_{k-1} +/- ... */
1377 bool
1378 simple_iv_increment_p (gimple *stmt)
1380 enum tree_code code;
1381 tree lhs, preinc;
1382 gimple *phi;
1383 size_t i;
1385 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1386 return false;
1388 lhs = gimple_assign_lhs (stmt);
1389 if (TREE_CODE (lhs) != SSA_NAME)
1390 return false;
1392 code = gimple_assign_rhs_code (stmt);
1393 if (code != PLUS_EXPR
1394 && code != MINUS_EXPR
1395 && code != POINTER_PLUS_EXPR)
1396 return false;
1398 preinc = gimple_assign_rhs1 (stmt);
1399 if (TREE_CODE (preinc) != SSA_NAME)
1400 return false;
1402 phi = SSA_NAME_DEF_STMT (preinc);
1403 while (gimple_code (phi) != GIMPLE_PHI)
1405 /* Follow trivial copies, but not the DEF used in a back edge,
1406 so that we don't prevent coalescing. */
1407 if (!gimple_assign_ssa_name_copy_p (phi))
1408 return false;
1409 preinc = gimple_assign_rhs1 (phi);
1410 phi = SSA_NAME_DEF_STMT (preinc);
1413 for (i = 0; i < gimple_phi_num_args (phi); i++)
1414 if (gimple_phi_arg_def (phi, i) == lhs)
1415 return true;
1417 return false;
1420 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1421 successors of BB. */
1423 static void
1424 cprop_into_successor_phis (basic_block bb,
1425 class const_and_copies *const_and_copies)
1427 edge e;
1428 edge_iterator ei;
1430 FOR_EACH_EDGE (e, ei, bb->succs)
1432 int indx;
1433 gphi_iterator gsi;
1435 /* If this is an abnormal edge, then we do not want to copy propagate
1436 into the PHI alternative associated with this edge. */
1437 if (e->flags & EDGE_ABNORMAL)
1438 continue;
1440 gsi = gsi_start_phis (e->dest);
1441 if (gsi_end_p (gsi))
1442 continue;
1444 /* We may have an equivalence associated with this edge. While
1445 we cannot propagate it into non-dominated blocks, we can
1446 propagate them into PHIs in non-dominated blocks. */
1448 /* Push the unwind marker so we can reset the const and copies
1449 table back to its original state after processing this edge. */
1450 const_and_copies->push_marker ();
1452 /* Extract and record any simple NAME = VALUE equivalences.
1454 Don't bother with [01] = COND equivalences, they're not useful
1455 here. */
1456 class edge_info *edge_info = (class edge_info *) e->aux;
1458 if (edge_info)
1460 edge_info::equiv_pair *seq;
1461 for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1463 tree lhs = seq->first;
1464 tree rhs = seq->second;
1466 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1467 const_and_copies->record_const_or_copy (lhs, rhs);
1472 indx = e->dest_idx;
1473 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1475 tree new_val;
1476 use_operand_p orig_p;
1477 tree orig_val;
1478 gphi *phi = gsi.phi ();
1480 /* The alternative may be associated with a constant, so verify
1481 it is an SSA_NAME before doing anything with it. */
1482 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1483 orig_val = get_use_from_ptr (orig_p);
1484 if (TREE_CODE (orig_val) != SSA_NAME)
1485 continue;
1487 /* If we have *ORIG_P in our constant/copy table, then replace
1488 ORIG_P with its value in our constant/copy table. */
1489 new_val = SSA_NAME_VALUE (orig_val);
1490 if (new_val
1491 && new_val != orig_val
1492 && may_propagate_copy (orig_val, new_val))
1493 propagate_value (orig_p, new_val);
1496 const_and_copies->pop_to_marker ();
1500 edge
1501 dom_opt_dom_walker::before_dom_children (basic_block bb)
1503 gimple_stmt_iterator gsi;
1505 if (dump_file && (dump_flags & TDF_DETAILS))
1506 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1508 /* Push a marker on the stacks of local information so that we know how
1509 far to unwind when we finalize this block. */
1510 m_avail_exprs_stack->push_marker ();
1511 m_const_and_copies->push_marker ();
1512 bitmap_set_bit (m_state->get_blocks_on_stack (), bb->index);
1514 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1515 m_avail_exprs_stack,
1516 m_state->get_blocks_on_stack ());
1517 set_global_ranges_from_unreachable_edges (bb);
1519 /* PHI nodes can create equivalences too. */
1520 record_equivalences_from_phis (bb);
1522 /* Create equivalences from redundant PHIs. PHIs are only truly
1523 redundant when they exist in the same block, so push another
1524 marker and unwind right afterwards. */
1525 m_avail_exprs_stack->push_marker ();
1526 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1527 eliminate_redundant_computations (&gsi, m_const_and_copies,
1528 m_avail_exprs_stack);
1529 m_avail_exprs_stack->pop_to_marker ();
1531 edge taken_edge = NULL;
1532 /* Initialize visited flag ahead of us, it has undefined state on
1533 pass entry. */
1534 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1535 gimple_set_visited (gsi_stmt (gsi), false);
1536 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1538 /* Do not optimize a stmt twice, substitution might end up with
1539 _3 = _3 which is not valid. */
1540 if (gimple_visited_p (gsi_stmt (gsi)))
1542 gsi_next (&gsi);
1543 continue;
1546 bool removed_p = false;
1547 taken_edge = this->optimize_stmt (bb, &gsi, &removed_p);
1548 if (!removed_p)
1549 gimple_set_visited (gsi_stmt (gsi), true);
1551 /* Go back and visit stmts inserted by folding after substituting
1552 into the stmt at gsi. */
1553 if (gsi_end_p (gsi))
1555 gcc_checking_assert (removed_p);
1556 gsi = gsi_last_bb (bb);
1557 while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)))
1558 gsi_prev (&gsi);
1560 else
1564 gsi_prev (&gsi);
1566 while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)));
1568 if (gsi_end_p (gsi))
1569 gsi = gsi_start_bb (bb);
1570 else
1571 gsi_next (&gsi);
1574 /* Now prepare to process dominated blocks. */
1575 record_edge_info (bb);
1576 cprop_into_successor_phis (bb, m_const_and_copies);
1577 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1578 return NULL;
1580 return taken_edge;
1583 /* We have finished processing the dominator children of BB, perform
1584 any finalization actions in preparation for leaving this node in
1585 the dominator tree. */
1587 void
1588 dom_opt_dom_walker::after_dom_children (basic_block bb)
1590 m_threader->thread_outgoing_edges (bb);
1591 bitmap_clear_bit (m_state->get_blocks_on_stack (), bb->index);
1592 m_avail_exprs_stack->pop_to_marker ();
1593 m_const_and_copies->pop_to_marker ();
1596 /* Search for redundant computations in STMT. If any are found, then
1597 replace them with the variable holding the result of the computation.
1599 If safe, record this expression into AVAIL_EXPRS_STACK and
1600 CONST_AND_COPIES. */
1602 static void
1603 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1604 class const_and_copies *const_and_copies,
1605 class avail_exprs_stack *avail_exprs_stack)
1607 tree expr_type;
1608 tree cached_lhs;
1609 tree def;
1610 bool insert = true;
1611 bool assigns_var_p = false;
1613 gimple *stmt = gsi_stmt (*gsi);
1615 if (gimple_code (stmt) == GIMPLE_PHI)
1616 def = gimple_phi_result (stmt);
1617 else
1618 def = gimple_get_lhs (stmt);
1620 /* Certain expressions on the RHS can be optimized away, but cannot
1621 themselves be entered into the hash tables. */
1622 if (! def
1623 || TREE_CODE (def) != SSA_NAME
1624 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1625 || gimple_vdef (stmt)
1626 /* Do not record equivalences for increments of ivs. This would create
1627 overlapping live ranges for a very questionable gain. */
1628 || simple_iv_increment_p (stmt))
1629 insert = false;
1631 /* Check if the expression has been computed before. */
1632 cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
1634 opt_stats.num_exprs_considered++;
1636 /* Get the type of the expression we are trying to optimize. */
1637 if (is_gimple_assign (stmt))
1639 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1640 assigns_var_p = true;
1642 else if (gimple_code (stmt) == GIMPLE_COND)
1643 expr_type = boolean_type_node;
1644 else if (is_gimple_call (stmt))
1646 gcc_assert (gimple_call_lhs (stmt));
1647 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1648 assigns_var_p = true;
1650 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1651 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1652 else if (gimple_code (stmt) == GIMPLE_PHI)
1653 /* We can't propagate into a phi, so the logic below doesn't apply.
1654 Instead record an equivalence between the cached LHS and the
1655 PHI result of this statement, provided they are in the same block.
1656 This should be sufficient to kill the redundant phi. */
1658 if (def && cached_lhs)
1659 const_and_copies->record_const_or_copy (def, cached_lhs);
1660 return;
1662 else
1663 gcc_unreachable ();
1665 if (!cached_lhs)
1666 return;
1668 /* It is safe to ignore types here since we have already done
1669 type checking in the hashing and equality routines. In fact
1670 type checking here merely gets in the way of constant
1671 propagation. Also, make sure that it is safe to propagate
1672 CACHED_LHS into the expression in STMT. */
1673 if ((TREE_CODE (cached_lhs) != SSA_NAME
1674 && (assigns_var_p
1675 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1676 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1678 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1679 || is_gimple_min_invariant (cached_lhs));
1681 if (dump_file && (dump_flags & TDF_DETAILS))
1683 fprintf (dump_file, " Replaced redundant expr '");
1684 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1685 fprintf (dump_file, "' with '");
1686 print_generic_expr (dump_file, cached_lhs, dump_flags);
1687 fprintf (dump_file, "'\n");
1690 opt_stats.num_re++;
1692 if (assigns_var_p
1693 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1694 cached_lhs = fold_convert (expr_type, cached_lhs);
1696 propagate_tree_value_into_stmt (gsi, cached_lhs);
1698 /* Since it is always necessary to mark the result as modified,
1699 perhaps we should move this into propagate_tree_value_into_stmt
1700 itself. */
1701 gimple_set_modified (gsi_stmt (*gsi), true);
1705 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1706 the available expressions table or the const_and_copies table.
1707 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1709 We handle only very simple copy equivalences here. The heavy
1710 lifing is done by eliminate_redundant_computations. */
1712 static void
1713 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1714 class avail_exprs_stack *avail_exprs_stack)
1716 tree lhs;
1717 enum tree_code lhs_code;
1719 gcc_assert (is_gimple_assign (stmt));
1721 lhs = gimple_assign_lhs (stmt);
1722 lhs_code = TREE_CODE (lhs);
1724 if (lhs_code == SSA_NAME
1725 && gimple_assign_single_p (stmt))
1727 tree rhs = gimple_assign_rhs1 (stmt);
1729 /* If the RHS of the assignment is a constant or another variable that
1730 may be propagated, register it in the CONST_AND_COPIES table. We
1731 do not need to record unwind data for this, since this is a true
1732 assignment and not an equivalence inferred from a comparison. All
1733 uses of this ssa name are dominated by this assignment, so unwinding
1734 just costs time and space. */
1735 if (may_optimize_p
1736 && (TREE_CODE (rhs) == SSA_NAME
1737 || is_gimple_min_invariant (rhs)))
1739 rhs = dom_valueize (rhs);
1741 if (dump_file && (dump_flags & TDF_DETAILS))
1743 fprintf (dump_file, "==== ASGN ");
1744 print_generic_expr (dump_file, lhs);
1745 fprintf (dump_file, " = ");
1746 print_generic_expr (dump_file, rhs);
1747 fprintf (dump_file, "\n");
1750 set_ssa_name_value (lhs, rhs);
1754 /* Make sure we can propagate &x + CST. */
1755 if (lhs_code == SSA_NAME
1756 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1757 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1758 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1760 tree op0 = gimple_assign_rhs1 (stmt);
1761 tree op1 = gimple_assign_rhs2 (stmt);
1762 tree new_rhs
1763 = build1 (ADDR_EXPR, TREE_TYPE (op0),
1764 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)),
1765 unshare_expr (op0), fold_convert (ptr_type_node,
1766 op1)));
1767 if (dump_file && (dump_flags & TDF_DETAILS))
1769 fprintf (dump_file, "==== ASGN ");
1770 print_generic_expr (dump_file, lhs);
1771 fprintf (dump_file, " = ");
1772 print_generic_expr (dump_file, new_rhs);
1773 fprintf (dump_file, "\n");
1776 set_ssa_name_value (lhs, new_rhs);
1779 /* A memory store, even an aliased store, creates a useful
1780 equivalence. By exchanging the LHS and RHS, creating suitable
1781 vops and recording the result in the available expression table,
1782 we may be able to expose more redundant loads. */
1783 if (!gimple_has_volatile_ops (stmt)
1784 && gimple_references_memory_p (stmt)
1785 && gimple_assign_single_p (stmt)
1786 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1787 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1788 && !is_gimple_reg (lhs))
1790 tree rhs = gimple_assign_rhs1 (stmt);
1791 gassign *new_stmt;
1793 /* Build a new statement with the RHS and LHS exchanged. */
1794 if (TREE_CODE (rhs) == SSA_NAME)
1796 /* NOTE tuples. The call to gimple_build_assign below replaced
1797 a call to build_gimple_modify_stmt, which did not set the
1798 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1799 may cause an SSA validation failure, as the LHS may be a
1800 default-initialized name and should have no definition. I'm
1801 a bit dubious of this, as the artificial statement that we
1802 generate here may in fact be ill-formed, but it is simply
1803 used as an internal device in this pass, and never becomes
1804 part of the CFG. */
1805 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1806 new_stmt = gimple_build_assign (rhs, lhs);
1807 SSA_NAME_DEF_STMT (rhs) = defstmt;
1809 else
1810 new_stmt = gimple_build_assign (rhs, lhs);
1812 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1814 /* Finally enter the statement into the available expression
1815 table. */
1816 avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
1820 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1821 CONST_AND_COPIES. */
1823 static void
1824 cprop_operand (gimple *stmt, use_operand_p op_p, range_query *query)
1826 tree val;
1827 tree op = USE_FROM_PTR (op_p);
1829 /* If the operand has a known constant value or it is known to be a
1830 copy of some other variable, use the value or copy stored in
1831 CONST_AND_COPIES. */
1832 val = SSA_NAME_VALUE (op);
1833 if (!val)
1835 Value_Range r (TREE_TYPE (op));
1836 tree single;
1837 if (query->range_of_expr (r, op, stmt) && r.singleton_p (&single))
1838 val = single;
1841 if (val && val != op)
1843 /* Do not replace hard register operands in asm statements. */
1844 if (gimple_code (stmt) == GIMPLE_ASM
1845 && !may_propagate_copy_into_asm (op))
1846 return;
1848 /* Certain operands are not allowed to be copy propagated due
1849 to their interaction with exception handling and some GCC
1850 extensions. */
1851 if (!may_propagate_copy (op, val))
1852 return;
1854 /* Do not propagate copies into BIVs.
1855 See PR23821 and PR62217 for how this can disturb IV and
1856 number of iteration analysis. */
1857 if (TREE_CODE (val) != INTEGER_CST)
1859 gimple *def = SSA_NAME_DEF_STMT (op);
1860 if (gimple_code (def) == GIMPLE_PHI
1861 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1862 return;
1865 /* Dump details. */
1866 if (dump_file && (dump_flags & TDF_DETAILS))
1868 fprintf (dump_file, " Replaced '");
1869 print_generic_expr (dump_file, op, dump_flags);
1870 fprintf (dump_file, "' with %s '",
1871 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1872 print_generic_expr (dump_file, val, dump_flags);
1873 fprintf (dump_file, "'\n");
1876 if (TREE_CODE (val) != SSA_NAME)
1877 opt_stats.num_const_prop++;
1878 else
1879 opt_stats.num_copy_prop++;
1881 propagate_value (op_p, val);
1883 /* And note that we modified this statement. This is now
1884 safe, even if we changed virtual operands since we will
1885 rescan the statement and rewrite its operands again. */
1886 gimple_set_modified (stmt, true);
1890 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1891 known value for that SSA_NAME (or NULL if no value is known).
1893 Propagate values from CONST_AND_COPIES into the uses, vuses and
1894 vdef_ops of STMT. */
1896 static void
1897 cprop_into_stmt (gimple *stmt, range_query *query)
1899 use_operand_p op_p;
1900 ssa_op_iter iter;
1901 tree last_copy_propagated_op = NULL;
1903 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1905 tree old_op = USE_FROM_PTR (op_p);
1907 /* If we have A = B and B = A in the copy propagation tables
1908 (due to an equality comparison), avoid substituting B for A
1909 then A for B in the trivially discovered cases. This allows
1910 optimization of statements were A and B appear as input
1911 operands. */
1912 if (old_op != last_copy_propagated_op)
1914 cprop_operand (stmt, op_p, query);
1916 tree new_op = USE_FROM_PTR (op_p);
1917 if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
1918 last_copy_propagated_op = new_op;
1923 /* If STMT contains a relational test, try to convert it into an
1924 equality test if there is only a single value which can ever
1925 make the test true.
1927 For example, if the expression hash table contains:
1929 TRUE = (i <= 1)
1931 And we have a test within statement of i >= 1, then we can safely
1932 rewrite the test as i == 1 since there only a single value where
1933 the test is true.
1935 This is similar to code in VRP. */
1937 void
1938 dom_opt_dom_walker::test_for_singularity (gimple *stmt,
1939 avail_exprs_stack *avail_exprs_stack)
1941 /* We want to support gimple conditionals as well as assignments
1942 where the RHS contains a conditional. */
1943 if (is_gimple_assign (stmt) || gimple_code (stmt) == GIMPLE_COND)
1945 enum tree_code code = ERROR_MARK;
1946 tree lhs, rhs;
1948 /* Extract the condition of interest from both forms we support. */
1949 if (is_gimple_assign (stmt))
1951 code = gimple_assign_rhs_code (stmt);
1952 lhs = gimple_assign_rhs1 (stmt);
1953 rhs = gimple_assign_rhs2 (stmt);
1955 else if (gimple_code (stmt) == GIMPLE_COND)
1957 code = gimple_cond_code (as_a <gcond *> (stmt));
1958 lhs = gimple_cond_lhs (as_a <gcond *> (stmt));
1959 rhs = gimple_cond_rhs (as_a <gcond *> (stmt));
1962 /* We're looking for a relational test using LE/GE. Also note we can
1963 canonicalize LT/GT tests against constants into LE/GT tests. */
1964 if (code == LE_EXPR || code == GE_EXPR
1965 || ((code == LT_EXPR || code == GT_EXPR)
1966 && TREE_CODE (rhs) == INTEGER_CST))
1968 /* For LT_EXPR and GT_EXPR, canonicalize to LE_EXPR and GE_EXPR. */
1969 if (code == LT_EXPR)
1970 rhs = fold_build2 (MINUS_EXPR, TREE_TYPE (rhs),
1971 rhs, build_int_cst (TREE_TYPE (rhs), 1));
1973 if (code == GT_EXPR)
1974 rhs = fold_build2 (PLUS_EXPR, TREE_TYPE (rhs),
1975 rhs, build_int_cst (TREE_TYPE (rhs), 1));
1977 /* Determine the code we want to check for in the hash table. */
1978 enum tree_code test_code;
1979 if (code == GE_EXPR || code == GT_EXPR)
1980 test_code = LE_EXPR;
1981 else
1982 test_code = GE_EXPR;
1984 /* Update the dummy statement so we can query the hash tables. */
1985 gimple_cond_set_code (m_dummy_cond, test_code);
1986 gimple_cond_set_lhs (m_dummy_cond, lhs);
1987 gimple_cond_set_rhs (m_dummy_cond, rhs);
1988 tree cached_lhs
1989 = avail_exprs_stack->lookup_avail_expr (m_dummy_cond,
1990 false, false);
1992 /* If the lookup returned 1 (true), then the expression we
1993 queried was in the hash table. As a result there is only
1994 one value that makes the original conditional true. Update
1995 STMT accordingly. */
1996 if (cached_lhs && integer_onep (cached_lhs))
1998 if (is_gimple_assign (stmt))
2000 gimple_assign_set_rhs_code (stmt, EQ_EXPR);
2001 gimple_assign_set_rhs2 (stmt, rhs);
2002 gimple_set_modified (stmt, true);
2004 else
2006 gimple_set_modified (stmt, true);
2007 gimple_cond_set_code (as_a <gcond *> (stmt), EQ_EXPR);
2008 gimple_cond_set_rhs (as_a <gcond *> (stmt), rhs);
2009 gimple_set_modified (stmt, true);
2016 /* If STMT is a comparison of two uniform vectors reduce it to a comparison
2017 of scalar objects, otherwise leave STMT unchanged. */
2019 static void
2020 reduce_vector_comparison_to_scalar_comparison (gimple *stmt)
2022 if (gimple_code (stmt) == GIMPLE_COND)
2024 tree lhs = gimple_cond_lhs (stmt);
2025 tree rhs = gimple_cond_rhs (stmt);
2027 /* We may have a vector comparison where both arms are uniform
2028 vectors. If so, we can simplify the vector comparison down
2029 to a scalar comparison. */
2030 if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
2031 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
2033 /* If either operand is an SSA_NAME, then look back to its
2034 defining statement to try and get at a suitable source. */
2035 if (TREE_CODE (rhs) == SSA_NAME)
2037 gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
2038 if (gimple_assign_single_p (def_stmt))
2039 rhs = gimple_assign_rhs1 (def_stmt);
2042 if (TREE_CODE (lhs) == SSA_NAME)
2044 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
2045 if (gimple_assign_single_p (def_stmt))
2046 lhs = gimple_assign_rhs1 (def_stmt);
2049 /* Now see if they are both uniform vectors and if so replace
2050 the vector comparison with a scalar comparison. */
2051 tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE;
2052 tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE;
2053 if (rhs_elem && lhs_elem)
2055 if (dump_file && dump_flags & TDF_DETAILS)
2057 fprintf (dump_file, "Reducing vector comparison: ");
2058 print_gimple_stmt (dump_file, stmt, 0);
2061 gimple_cond_set_rhs (as_a <gcond *>(stmt), rhs_elem);
2062 gimple_cond_set_lhs (as_a <gcond *>(stmt), lhs_elem);
2063 gimple_set_modified (stmt, true);
2065 if (dump_file && dump_flags & TDF_DETAILS)
2067 fprintf (dump_file, "To scalar equivalent: ");
2068 print_gimple_stmt (dump_file, stmt, 0);
2069 fprintf (dump_file, "\n");
2076 /* If possible, rewrite the conditional as TRUE or FALSE, and return
2077 the taken edge. Otherwise, return NULL. */
2079 edge
2080 dom_opt_dom_walker::fold_cond (gcond *cond)
2082 simplify_using_ranges simplify (m_ranger);
2083 if (simplify.fold_cond (cond))
2085 basic_block bb = gimple_bb (cond);
2086 if (gimple_cond_true_p (cond))
2087 return find_taken_edge (bb, boolean_true_node);
2088 if (gimple_cond_false_p (cond))
2089 return find_taken_edge (bb, boolean_false_node);
2091 return NULL;
2094 /* Optimize the statement in block BB pointed to by iterator SI.
2096 We try to perform some simplistic global redundancy elimination and
2097 constant propagation:
2099 1- To detect global redundancy, we keep track of expressions that have
2100 been computed in this block and its dominators. If we find that the
2101 same expression is computed more than once, we eliminate repeated
2102 computations by using the target of the first one.
2104 2- Constant values and copy assignments. This is used to do very
2105 simplistic constant and copy propagation. When a constant or copy
2106 assignment is found, we map the value on the RHS of the assignment to
2107 the variable in the LHS in the CONST_AND_COPIES table.
2109 3- Very simple redundant store elimination is performed.
2111 4- We can simplify a condition to a constant or from a relational
2112 condition to an equality condition. */
2114 edge
2115 dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
2116 bool *removed_p)
2118 gimple *stmt, *old_stmt;
2119 bool may_optimize_p;
2120 bool modified_p = false;
2121 bool was_noreturn;
2122 edge retval = NULL;
2124 old_stmt = stmt = gsi_stmt (*si);
2125 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
2127 if (dump_file && (dump_flags & TDF_DETAILS))
2129 fprintf (dump_file, "Optimizing statement ");
2130 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2133 /* STMT may be a comparison of uniform vectors that we can simplify
2134 down to a comparison of scalars. Do that transformation first
2135 so that all the scalar optimizations from here onward apply. */
2136 reduce_vector_comparison_to_scalar_comparison (stmt);
2138 update_stmt_if_modified (stmt);
2139 opt_stats.num_stmts++;
2141 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2142 cprop_into_stmt (stmt, m_ranger);
2144 /* If the statement has been modified with constant replacements,
2145 fold its RHS before checking for redundant computations. */
2146 if (gimple_modified_p (stmt))
2148 tree rhs = NULL;
2150 /* Try to fold the statement making sure that STMT is kept
2151 up to date. */
2152 if (fold_stmt (si))
2154 stmt = gsi_stmt (*si);
2155 gimple_set_modified (stmt, true);
2157 if (dump_file && (dump_flags & TDF_DETAILS))
2159 fprintf (dump_file, " Folded to: ");
2160 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2164 /* We only need to consider cases that can yield a gimple operand. */
2165 if (gimple_assign_single_p (stmt))
2166 rhs = gimple_assign_rhs1 (stmt);
2167 else if (gimple_code (stmt) == GIMPLE_GOTO)
2168 rhs = gimple_goto_dest (stmt);
2169 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2170 /* This should never be an ADDR_EXPR. */
2171 rhs = gimple_switch_index (swtch_stmt);
2173 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2174 recompute_tree_invariant_for_addr_expr (rhs);
2176 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2177 even if fold_stmt updated the stmt already and thus cleared
2178 gimple_modified_p flag on it. */
2179 modified_p = true;
2182 /* Check for redundant computations. Do this optimization only
2183 for assignments that have no volatile ops and conditionals. */
2184 may_optimize_p = (!gimple_has_side_effects (stmt)
2185 && (is_gimple_assign (stmt)
2186 || (is_gimple_call (stmt)
2187 && gimple_call_lhs (stmt) != NULL_TREE)
2188 || gimple_code (stmt) == GIMPLE_COND
2189 || gimple_code (stmt) == GIMPLE_SWITCH));
2191 if (may_optimize_p)
2193 if (gimple_code (stmt) == GIMPLE_CALL)
2195 /* Resolve __builtin_constant_p. If it hasn't been
2196 folded to integer_one_node by now, it's fairly
2197 certain that the value simply isn't constant. */
2198 tree callee = gimple_call_fndecl (stmt);
2199 if (callee
2200 && fndecl_built_in_p (callee, BUILT_IN_CONSTANT_P))
2202 propagate_tree_value_into_stmt (si, integer_zero_node);
2203 stmt = gsi_stmt (*si);
2207 if (gimple_code (stmt) == GIMPLE_COND)
2209 tree lhs = gimple_cond_lhs (stmt);
2210 tree rhs = gimple_cond_rhs (stmt);
2212 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
2213 then this conditional is computable at compile time. We can just
2214 shove either 0 or 1 into the LHS, mark the statement as modified
2215 and all the right things will just happen below.
2217 Note this would apply to any case where LHS has a range
2218 narrower than its type implies and RHS is outside that
2219 narrower range. Future work. */
2220 if (TREE_CODE (lhs) == SSA_NAME
2221 && ssa_name_has_boolean_range (lhs)
2222 && TREE_CODE (rhs) == INTEGER_CST
2223 && ! (integer_zerop (rhs) || integer_onep (rhs)))
2225 gimple_cond_set_lhs (as_a <gcond *> (stmt),
2226 fold_convert (TREE_TYPE (lhs),
2227 integer_zero_node));
2228 gimple_set_modified (stmt, true);
2230 else if (TREE_CODE (lhs) == SSA_NAME)
2232 /* Exploiting EVRP data is not yet fully integrated into DOM
2233 but we need to do something for this case to avoid regressing
2234 udr4.f90 and new1.C which have unexecutable blocks with
2235 undefined behavior that get diagnosed if they're left in the
2236 IL because we've attached range information to new
2237 SSA_NAMES. */
2238 update_stmt_if_modified (stmt);
2239 edge taken_edge = fold_cond (as_a <gcond *> (stmt));
2240 if (taken_edge)
2242 gimple_set_modified (stmt, true);
2243 update_stmt (stmt);
2244 cfg_altered = true;
2245 return taken_edge;
2250 update_stmt_if_modified (stmt);
2251 eliminate_redundant_computations (si, m_const_and_copies,
2252 m_avail_exprs_stack);
2253 stmt = gsi_stmt (*si);
2255 /* Perform simple redundant store elimination. */
2256 if (gimple_assign_single_p (stmt)
2257 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2259 tree lhs = gimple_assign_lhs (stmt);
2260 tree rhs = gimple_assign_rhs1 (stmt);
2261 tree cached_lhs;
2262 gassign *new_stmt;
2263 rhs = dom_valueize (rhs);
2264 /* Build a new statement with the RHS and LHS exchanged. */
2265 if (TREE_CODE (rhs) == SSA_NAME)
2267 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
2268 new_stmt = gimple_build_assign (rhs, lhs);
2269 SSA_NAME_DEF_STMT (rhs) = defstmt;
2271 else
2272 new_stmt = gimple_build_assign (rhs, lhs);
2273 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2274 expr_hash_elt *elt = NULL;
2275 cached_lhs = m_avail_exprs_stack->lookup_avail_expr (new_stmt, false,
2276 false, &elt);
2277 if (cached_lhs
2278 && operand_equal_p (rhs, cached_lhs, 0)
2279 && refs_same_for_tbaa_p (elt->expr ()->kind == EXPR_SINGLE
2280 ? elt->expr ()->ops.single.rhs
2281 : NULL_TREE, lhs))
2283 basic_block bb = gimple_bb (stmt);
2284 unlink_stmt_vdef (stmt);
2285 if (gsi_remove (si, true))
2287 bitmap_set_bit (need_eh_cleanup, bb->index);
2288 if (dump_file && (dump_flags & TDF_DETAILS))
2289 fprintf (dump_file, " Flagged to clear EH edges.\n");
2291 release_defs (stmt);
2292 *removed_p = true;
2293 return retval;
2297 /* If this statement was not redundant, we may still be able to simplify
2298 it, which may in turn allow other part of DOM or other passes to do
2299 a better job. */
2300 test_for_singularity (stmt, m_avail_exprs_stack);
2303 /* Record any additional equivalences created by this statement. */
2304 if (is_gimple_assign (stmt))
2305 record_equivalences_from_stmt (stmt, may_optimize_p, m_avail_exprs_stack);
2307 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
2308 know where it goes. */
2309 if (gimple_modified_p (stmt) || modified_p)
2311 tree val = NULL;
2313 if (gimple_code (stmt) == GIMPLE_COND)
2314 val = fold_binary_loc (gimple_location (stmt),
2315 gimple_cond_code (stmt), boolean_type_node,
2316 gimple_cond_lhs (stmt),
2317 gimple_cond_rhs (stmt));
2318 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2319 val = gimple_switch_index (swtch_stmt);
2321 if (val && TREE_CODE (val) == INTEGER_CST)
2323 retval = find_taken_edge (bb, val);
2324 if (retval)
2326 /* Fix the condition to be either true or false. */
2327 if (gimple_code (stmt) == GIMPLE_COND)
2329 if (integer_zerop (val))
2330 gimple_cond_make_false (as_a <gcond *> (stmt));
2331 else if (integer_onep (val))
2332 gimple_cond_make_true (as_a <gcond *> (stmt));
2333 else
2334 gcc_unreachable ();
2336 gimple_set_modified (stmt, true);
2339 /* Further simplifications may be possible. */
2340 cfg_altered = true;
2344 update_stmt_if_modified (stmt);
2346 /* If we simplified a statement in such a way as to be shown that it
2347 cannot trap, update the eh information and the cfg to match. */
2348 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2350 bitmap_set_bit (need_eh_cleanup, bb->index);
2351 if (dump_file && (dump_flags & TDF_DETAILS))
2352 fprintf (dump_file, " Flagged to clear EH edges.\n");
2355 if (!was_noreturn
2356 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
2357 need_noreturn_fixup.safe_push (stmt);
2359 return retval;