/* SSA Dominator optimizations for trees
- Copyright (C) 2001-2017 Free Software Foundation, Inc.
+ Copyright (C) 2001-2021 Free Software Foundation, Inc.
Contributed by Diego Novillo <dnovillo@redhat.com>
This file is part of GCC.
#include "cfgloop.h"
#include "gimple-fold.h"
#include "tree-eh.h"
+#include "tree-inline.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-into-ssa.h"
#include "domwalk.h"
#include "tree-ssa-propagate.h"
#include "tree-ssa-threadupdate.h"
-#include "params.h"
#include "tree-ssa-scopedtables.h"
#include "tree-ssa-threadedge.h"
#include "tree-ssa-dom.h"
#include "gimplify.h"
#include "tree-cfgcleanup.h"
#include "dbgcnt.h"
+#include "alloc-pool.h"
+#include "tree-vrp.h"
+#include "vr-values.h"
+#include "gimple-ssa-evrp-analyze.h"
+#include "alias.h"
/* This file implements optimizations on the dominator tree. */
These structures live for a single iteration of the dominator
optimizer in the edge's AUX field. At the end of an iteration we
free each of these structures. */
-
-struct edge_info
+class edge_info
{
- /* If this edge creates a simple equivalence, the LHS and RHS of
- the equivalence will be stored here. */
- tree lhs;
- tree rhs;
+ public:
+ typedef std::pair <tree, tree> equiv_pair;
+ edge_info (edge);
+ ~edge_info ();
+
+ /* Record a simple LHS = RHS equivalence. This may trigger
+ calls to derive_equivalences. */
+ void record_simple_equiv (tree, tree);
+
+ /* If traversing this edge creates simple equivalences, we store
+ them as LHS/RHS pairs within this vector. */
+ vec<equiv_pair> simple_equivalences;
/* Traversing an edge may also indicate one or more particular conditions
are true or false. */
vec<cond_equivalence> cond_equivalences;
+
+ private:
+ /* Derive equivalences by walking the use-def chains. */
+ void derive_equivalences (tree, tree, int);
};
/* Track whether or not we have changed the control flow graph. */
static struct opt_stats_d opt_stats;
/* Local functions. */
-static edge optimize_stmt (basic_block, gimple_stmt_iterator,
- class const_and_copies *,
- class avail_exprs_stack *);
static void record_equality (tree, tree, class const_and_copies *);
static void record_equivalences_from_phis (basic_block);
static void record_equivalences_from_incoming_edge (basic_block,
class avail_exprs_stack *);
static void record_equivalences_from_stmt (gimple *, int,
class avail_exprs_stack *);
-static edge single_incoming_edge_ignoring_loop_edges (basic_block);
static void dump_dominator_optimization_stats (FILE *file,
hash_table<expr_elt_hasher> *);
+/* Constructor for EDGE_INFO. An EDGE_INFO instance is always
+ associated with an edge E. */
-/* Free the edge_info data attached to E, if it exists. */
+edge_info::edge_info (edge e)
+{
+ /* Free the old one associated with E, if it exists and
+ associate our new object with E. */
+ free_dom_edge_info (e);
+ e->aux = this;
+
+ /* And initialize the embedded vectors. */
+ simple_equivalences = vNULL;
+ cond_equivalences = vNULL;
+}
+
+/* Destructor just needs to release the vectors. */
+
+edge_info::~edge_info (void)
+{
+ this->cond_equivalences.release ();
+ this->simple_equivalences.release ();
+}
+
+/* NAME is known to have the value VALUE, which must be a constant.
+
+ Walk through its use-def chain to see if there are other equivalences
+ we might be able to derive.
+
+ RECURSION_LIMIT controls how far back we recurse through the use-def
+ chains. */
void
-free_dom_edge_info (edge e)
+edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
{
- struct edge_info *edge_info = (struct edge_info *)e->aux;
+ if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
+ return;
- if (edge_info)
+ /* This records the equivalence for the toplevel object. Do
+ this before checking the recursion limit. */
+ simple_equivalences.safe_push (equiv_pair (name, value));
+
+ /* Limit how far up the use-def chains we are willing to walk. */
+ if (recursion_limit == 0)
+ return;
+
+ /* We can walk up the use-def chains to potentially find more
+ equivalences. */
+ gimple *def_stmt = SSA_NAME_DEF_STMT (name);
+ if (is_gimple_assign (def_stmt))
{
- edge_info->cond_equivalences.release ();
- free (edge_info);
+ enum tree_code code = gimple_assign_rhs_code (def_stmt);
+ switch (code)
+ {
+ /* If the result of an OR is zero, then its operands are, too. */
+ case BIT_IOR_EXPR:
+ if (integer_zerop (value))
+ {
+ tree rhs1 = gimple_assign_rhs1 (def_stmt);
+ tree rhs2 = gimple_assign_rhs2 (def_stmt);
+
+ value = build_zero_cst (TREE_TYPE (rhs1));
+ derive_equivalences (rhs1, value, recursion_limit - 1);
+ value = build_zero_cst (TREE_TYPE (rhs2));
+ derive_equivalences (rhs2, value, recursion_limit - 1);
+ }
+ break;
+
+ /* If the result of an AND is nonzero, then its operands are, too. */
+ case BIT_AND_EXPR:
+ if (!integer_zerop (value))
+ {
+ tree rhs1 = gimple_assign_rhs1 (def_stmt);
+ tree rhs2 = gimple_assign_rhs2 (def_stmt);
+
+ /* If either operand has a boolean range, then we
+ know its value must be one, otherwise we just know it
+ is nonzero. The former is clearly useful, I haven't
+ seen cases where the latter is helpful yet. */
+ if (TREE_CODE (rhs1) == SSA_NAME)
+ {
+ if (ssa_name_has_boolean_range (rhs1))
+ {
+ value = build_one_cst (TREE_TYPE (rhs1));
+ derive_equivalences (rhs1, value, recursion_limit - 1);
+ }
+ }
+ if (TREE_CODE (rhs2) == SSA_NAME)
+ {
+ if (ssa_name_has_boolean_range (rhs2))
+ {
+ value = build_one_cst (TREE_TYPE (rhs2));
+ derive_equivalences (rhs2, value, recursion_limit - 1);
+ }
+ }
+ }
+ break;
+
+ /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
+ set via a widening type conversion, then we may be able to record
+ additional equivalences. */
+ case NOP_EXPR:
+ case CONVERT_EXPR:
+ {
+ tree rhs = gimple_assign_rhs1 (def_stmt);
+ tree rhs_type = TREE_TYPE (rhs);
+ if (INTEGRAL_TYPE_P (rhs_type)
+ && (TYPE_PRECISION (TREE_TYPE (name))
+ >= TYPE_PRECISION (rhs_type))
+ && int_fits_type_p (value, rhs_type))
+ derive_equivalences (rhs,
+ fold_convert (rhs_type, value),
+ recursion_limit - 1);
+ break;
+ }
+
+ /* We can invert the operation of these codes trivially if
+ one of the RHS operands is a constant to produce a known
+ value for the other RHS operand. */
+ case POINTER_PLUS_EXPR:
+ case PLUS_EXPR:
+ {
+ tree rhs1 = gimple_assign_rhs1 (def_stmt);
+ tree rhs2 = gimple_assign_rhs2 (def_stmt);
+
+ /* If either argument is a constant, then we can compute
+ a constant value for the nonconstant argument. */
+ if (TREE_CODE (rhs1) == INTEGER_CST
+ && TREE_CODE (rhs2) == SSA_NAME)
+ derive_equivalences (rhs2,
+ fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
+ value, rhs1),
+ recursion_limit - 1);
+ else if (TREE_CODE (rhs2) == INTEGER_CST
+ && TREE_CODE (rhs1) == SSA_NAME)
+ derive_equivalences (rhs1,
+ fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
+ value, rhs2),
+ recursion_limit - 1);
+ break;
+ }
+
+ /* If one of the operands is a constant, then we can compute
+ the value of the other operand. If both operands are
+ SSA_NAMEs, then they must be equal if the result is zero. */
+ case MINUS_EXPR:
+ {
+ tree rhs1 = gimple_assign_rhs1 (def_stmt);
+ tree rhs2 = gimple_assign_rhs2 (def_stmt);
+
+ /* If either argument is a constant, then we can compute
+ a constant value for the nonconstant argument. */
+ if (TREE_CODE (rhs1) == INTEGER_CST
+ && TREE_CODE (rhs2) == SSA_NAME)
+ derive_equivalences (rhs2,
+ fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
+ rhs1, value),
+ recursion_limit - 1);
+ else if (TREE_CODE (rhs2) == INTEGER_CST
+ && TREE_CODE (rhs1) == SSA_NAME)
+ derive_equivalences (rhs1,
+ fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
+ value, rhs2),
+ recursion_limit - 1);
+ else if (integer_zerop (value))
+ {
+ tree cond = build2 (EQ_EXPR, boolean_type_node,
+ gimple_assign_rhs1 (def_stmt),
+ gimple_assign_rhs2 (def_stmt));
+ tree inverted = invert_truthvalue (cond);
+ record_conditions (&this->cond_equivalences, cond, inverted);
+ }
+ break;
+ }
+
+ case EQ_EXPR:
+ case NE_EXPR:
+ {
+ if ((code == EQ_EXPR && integer_onep (value))
+ || (code == NE_EXPR && integer_zerop (value)))
+ {
+ tree rhs1 = gimple_assign_rhs1 (def_stmt);
+ tree rhs2 = gimple_assign_rhs2 (def_stmt);
+
+ /* If either argument is a constant, then record the
+ other argument as being the same as that constant.
+
+ If neither operand is a constant, then we have a
+ conditional name == name equivalence. */
+ if (TREE_CODE (rhs1) == INTEGER_CST)
+ derive_equivalences (rhs2, rhs1, recursion_limit - 1);
+ else if (TREE_CODE (rhs2) == INTEGER_CST)
+ derive_equivalences (rhs1, rhs2, recursion_limit - 1);
+ }
+ else
+ {
+ tree cond = build2 (code, boolean_type_node,
+ gimple_assign_rhs1 (def_stmt),
+ gimple_assign_rhs2 (def_stmt));
+ tree inverted = invert_truthvalue (cond);
+ if (integer_zerop (value))
+ std::swap (cond, inverted);
+ record_conditions (&this->cond_equivalences, cond, inverted);
+ }
+ break;
+ }
+
+ /* For BIT_NOT and NEGATE, we can just apply the operation to the
+ VALUE to get the new equivalence. It will always be a constant
+ so we can recurse. */
+ case BIT_NOT_EXPR:
+ case NEGATE_EXPR:
+ {
+ tree rhs = gimple_assign_rhs1 (def_stmt);
+ tree res;
+ /* If this is a NOT and the operand has a boolean range, then we
+ know its value must be zero or one. We are not supposed to
+ have a BIT_NOT_EXPR for boolean types with precision > 1 in
+ the general case, see e.g. the handling of TRUTH_NOT_EXPR in
+ the gimplifier, but it can be generated by match.pd out of
+ a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR. Now the handling
+ of BIT_AND_EXPR above already forces a specific semantics for
+ boolean types with precision > 1 so we must do the same here,
+ otherwise we could change the semantics of TRUTH_NOT_EXPR for
+ boolean types with precision > 1. */
+ if (code == BIT_NOT_EXPR
+ && TREE_CODE (rhs) == SSA_NAME
+ && ssa_name_has_boolean_range (rhs))
+ {
+ if ((TREE_INT_CST_LOW (value) & 1) == 0)
+ res = build_one_cst (TREE_TYPE (rhs));
+ else
+ res = build_zero_cst (TREE_TYPE (rhs));
+ }
+ else
+ res = fold_build1 (code, TREE_TYPE (rhs), value);
+ derive_equivalences (rhs, res, recursion_limit - 1);
+ break;
+ }
+
+ default:
+ {
+ if (TREE_CODE_CLASS (code) == tcc_comparison)
+ {
+ tree cond = build2 (code, boolean_type_node,
+ gimple_assign_rhs1 (def_stmt),
+ gimple_assign_rhs2 (def_stmt));
+ tree inverted = invert_truthvalue (cond);
+ if (integer_zerop (value))
+ std::swap (cond, inverted);
+ record_conditions (&this->cond_equivalences, cond, inverted);
+ break;
+ }
+ break;
+ }
+ }
}
}
-/* Allocate an EDGE_INFO for edge E and attach it to E.
- Return the new EDGE_INFO structure. */
-
-static struct edge_info *
-allocate_edge_info (edge e)
+void
+edge_info::record_simple_equiv (tree lhs, tree rhs)
{
- struct edge_info *edge_info;
+ /* If the RHS is a constant, then we may be able to derive
+ further equivalences. Else just record the name = name
+ equivalence. */
+ if (TREE_CODE (rhs) == INTEGER_CST)
+ derive_equivalences (lhs, rhs, 4);
+ else
+ simple_equivalences.safe_push (equiv_pair (lhs, rhs));
+}
- /* Free the old one, if it exists. */
- free_dom_edge_info (e);
+/* Free the edge_info data attached to E, if it exists. */
- edge_info = XCNEW (struct edge_info);
+void
+free_dom_edge_info (edge e)
+{
+ class edge_info *edge_info = (class edge_info *)e->aux;
- e->aux = edge_info;
- return edge_info;
+ if (edge_info)
+ delete edge_info;
}
/* Free all EDGE_INFO structures associated with edges in the CFG.
record_edge_info (basic_block bb)
{
gimple_stmt_iterator gsi = gsi_last_bb (bb);
- struct edge_info *edge_info;
+ class edge_info *edge_info;
if (! gsi_end_p (gsi))
{
for (i = 0; i < n_labels; i++)
{
tree label = gimple_switch_label (switch_stmt, i);
- basic_block target_bb = label_to_block (CASE_LABEL (label));
+ basic_block target_bb
+ = label_to_block (cfun, CASE_LABEL (label));
if (CASE_HIGH (label)
|| !CASE_LOW (label)
|| info[target_bb->index])
{
tree x = fold_convert_loc (loc, TREE_TYPE (index),
CASE_LOW (label));
- edge_info = allocate_edge_info (e);
- edge_info->lhs = index;
- edge_info->rhs = x;
+ edge_info = new class edge_info (e);
+ edge_info->record_simple_equiv (index, x);
}
}
free (info);
if (code == EQ_EXPR)
{
- edge_info = allocate_edge_info (true_edge);
- edge_info->lhs = op0;
- edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
-
- edge_info = allocate_edge_info (false_edge);
- edge_info->lhs = op0;
- edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
+ edge_info = new class edge_info (true_edge);
+ edge_info->record_simple_equiv (op0,
+ (integer_zerop (op1)
+ ? false_val : true_val));
+ edge_info = new class edge_info (false_edge);
+ edge_info->record_simple_equiv (op0,
+ (integer_zerop (op1)
+ ? true_val : false_val));
}
else
{
- edge_info = allocate_edge_info (true_edge);
- edge_info->lhs = op0;
- edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
-
- edge_info = allocate_edge_info (false_edge);
- edge_info->lhs = op0;
- edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
+ edge_info = new class edge_info (true_edge);
+ edge_info->record_simple_equiv (op0,
+ (integer_zerop (op1)
+ ? true_val : false_val));
+ edge_info = new class edge_info (false_edge);
+ edge_info->record_simple_equiv (op0,
+ (integer_zerop (op1)
+ ? false_val : true_val));
}
}
+ /* This can show up in the IL as a result of copy propagation
+ it will eventually be canonicalized, but we have to cope
+ with this case within the pass. */
else if (is_gimple_min_invariant (op0)
- && (TREE_CODE (op1) == SSA_NAME
- || is_gimple_min_invariant (op1)))
+ && TREE_CODE (op1) == SSA_NAME)
{
tree cond = build2 (code, boolean_type_node, op0, op1);
tree inverted = invert_truthvalue_loc (loc, cond);
bool can_infer_simple_equiv
= !(HONOR_SIGNED_ZEROS (op0)
&& real_zerop (op0));
- struct edge_info *edge_info;
+ class edge_info *edge_info;
- edge_info = allocate_edge_info (true_edge);
+ edge_info = new class edge_info (true_edge);
record_conditions (&edge_info->cond_equivalences, cond, inverted);
if (can_infer_simple_equiv && code == EQ_EXPR)
- {
- edge_info->lhs = op1;
- edge_info->rhs = op0;
- }
+ edge_info->record_simple_equiv (op1, op0);
- edge_info = allocate_edge_info (false_edge);
+ edge_info = new class edge_info (false_edge);
record_conditions (&edge_info->cond_equivalences, inverted, cond);
if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
- {
- edge_info->lhs = op1;
- edge_info->rhs = op0;
- }
+ edge_info->record_simple_equiv (op1, op0);
}
else if (TREE_CODE (op0) == SSA_NAME
bool can_infer_simple_equiv
= !(HONOR_SIGNED_ZEROS (op1)
&& (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
- struct edge_info *edge_info;
+ class edge_info *edge_info;
- edge_info = allocate_edge_info (true_edge);
+ edge_info = new class edge_info (true_edge);
record_conditions (&edge_info->cond_equivalences, cond, inverted);
if (can_infer_simple_equiv && code == EQ_EXPR)
- {
- edge_info->lhs = op0;
- edge_info->rhs = op1;
- }
+ edge_info->record_simple_equiv (op0, op1);
- edge_info = allocate_edge_info (false_edge);
+ edge_info = new class edge_info (false_edge);
record_conditions (&edge_info->cond_equivalences, inverted, cond);
if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
- {
- edge_info->lhs = op0;
- edge_info->rhs = op1;
- }
+ edge_info->record_simple_equiv (op0, op1);
}
}
-
- /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
}
}
+class dom_jump_threader_simplifier : public jump_threader_simplifier
+{
+public:
+ dom_jump_threader_simplifier (vr_values *v,
+ avail_exprs_stack *avails)
+ : jump_threader_simplifier (v), m_avail_exprs_stack (avails) { }
+
+private:
+ tree simplify (gimple *, gimple *, basic_block, jt_state *) override;
+ avail_exprs_stack *m_avail_exprs_stack;
+};
+
+tree
+dom_jump_threader_simplifier::simplify (gimple *stmt,
+ gimple *within_stmt,
+ basic_block bb,
+ jt_state *state)
+{
+ /* First see if the conditional is in the hash table. */
+ tree cached_lhs = m_avail_exprs_stack->lookup_avail_expr (stmt,
+ false, true);
+ if (cached_lhs)
+ return cached_lhs;
+
+ return jump_threader_simplifier::simplify (stmt, within_stmt, bb, state);
+}
class dom_opt_dom_walker : public dom_walker
{
public:
dom_opt_dom_walker (cdi_direction direction,
- class const_and_copies *const_and_copies,
- class avail_exprs_stack *avail_exprs_stack)
- : dom_walker (direction, true),
- m_const_and_copies (const_and_copies),
- m_avail_exprs_stack (avail_exprs_stack),
- m_dummy_cond (NULL) {}
+ jump_threader *threader,
+ jt_state *state,
+ evrp_range_analyzer *analyzer,
+ const_and_copies *const_and_copies,
+ avail_exprs_stack *avail_exprs_stack)
+ : dom_walker (direction, REACHABLE_BLOCKS)
+ {
+ m_evrp_range_analyzer = analyzer;
+ m_state = state;
+ m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
+ integer_zero_node, NULL, NULL);
+ m_const_and_copies = const_and_copies;
+ m_avail_exprs_stack = avail_exprs_stack;
+ m_threader = threader;
+ }
virtual edge before_dom_children (basic_block);
virtual void after_dom_children (basic_block);
private:
- void thread_across_edge (edge);
/* Unwindable equivalences, both const/copy and expression varieties. */
class const_and_copies *m_const_and_copies;
class avail_exprs_stack *m_avail_exprs_stack;
+ /* Dummy condition to avoid creating lots of throw away statements. */
gcond *m_dummy_cond;
+
+ /* Optimize a single statement within a basic block using the
+ various tables mantained by DOM. Returns the taken edge if
+ the statement is a conditional with a statically determined
+ value. */
+ edge optimize_stmt (basic_block, gimple_stmt_iterator *, bool *);
+
+
+ void test_for_singularity (gimple *, avail_exprs_stack *);
+
+ jump_threader *m_threader;
+ evrp_range_analyzer *m_evrp_range_analyzer;
+ jt_state *m_state;
};
/* Jump threading, redundancy elimination and const/copy propagation.
gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
missing. We should improve jump threading in future then
LOOPS_HAVE_PREHEADERS won't be needed here. */
- loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
-
- /* Initialize the value-handle array. */
- threadedge_initialize_values ();
+ loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES
+ | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
/* We need accurate information regarding back edges in the CFG
for jump threading; this may include back edges that are not part of
record_edge_info (bb);
/* Recursively walk the dominator tree optimizing statements. */
+ evrp_range_analyzer analyzer (true);
+ dom_jump_threader_simplifier simplifier (&analyzer, avail_exprs_stack);
+ jt_state state (const_and_copies, avail_exprs_stack, &analyzer);
+ jump_threader threader (&simplifier, &state);
dom_opt_dom_walker walker (CDI_DOMINATORS,
+ &threader,
+ &state,
+ &analyzer,
const_and_copies,
avail_exprs_stack);
walker.walk (fun->cfg->x_entry_block_ptr);
containing any edge leaving BB. */
if (found)
FOR_EACH_EDGE (e, ei, bb->succs)
- remove_jump_threads_including (e);
+ threader.remove_jump_threads_including (e);
}
}
free_all_edge_infos ();
/* Thread jumps, creating duplicate blocks as needed. */
- cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
+ cfg_altered |= threader.thread_through_all_blocks (may_peel_loop_headers_p);
if (cfg_altered)
free_dominance_info (CDI_DOMINATORS);
if (bb == NULL)
continue;
while (single_succ_p (bb)
- && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
+ && (single_succ_edge (bb)->flags
+ & (EDGE_EH|EDGE_DFS_BACK)) == 0)
bb = single_succ (bb);
if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
continue;
if (dump_file && dump_flags & TDF_DETAILS)
{
fprintf (dump_file, "Fixing up noreturn call ");
- print_gimple_stmt (dump_file, stmt, 0, 0);
+ print_gimple_stmt (dump_file, stmt, 0);
fprintf (dump_file, "\n");
}
fixup_noreturn_call (stmt);
delete avail_exprs_stack;
delete const_and_copies;
- /* Free the value-handle array. */
- threadedge_finalize_values ();
-
return 0;
}
return new pass_dominator (ctxt);
}
-
-/* A trivial wrapper so that we can present the generic jump
- threading code with a simple API for simplifying statements. */
-static tree
-simplify_stmt_for_jump_threading (gimple *stmt,
- gimple *within_stmt ATTRIBUTE_UNUSED,
- class avail_exprs_stack *avail_exprs_stack,
- basic_block bb ATTRIBUTE_UNUSED)
-{
- return avail_exprs_stack->lookup_avail_expr (stmt, false, true);
-}
-
/* Valueize hook for gimple_fold_stmt_to_constant_1. */
static tree
BITMAP_FREE (domby);
}
-/* Record NAME has the value zero and if NAME was set from a BIT_IOR_EXPR
- recurse into both operands recording their values as zero too. */
-
-static void
-derive_equivalencs_from_bit_ior (tree name, const_and_copies *const_and_copies)
-{
- if (TREE_CODE (name) == SSA_NAME)
- {
- tree value = fold_convert (TREE_TYPE (name), integer_zero_node);
-
- /* This records the equivalence for the toplevel object. */
- record_equality (name, value, const_and_copies);
-
- /* And we can recurse into each operand to potentially find more
- equivalences. */
- gimple *def_stmt = SSA_NAME_DEF_STMT (name);
- if (is_gimple_assign (def_stmt)
- && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
- {
- derive_equivalencs_from_bit_ior (gimple_assign_rhs1 (def_stmt),
- const_and_copies);
- derive_equivalencs_from_bit_ior (gimple_assign_rhs2 (def_stmt),
- const_and_copies);
- }
- }
-}
-
/* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
by traversing edge E (which are cached in E->aux).
class avail_exprs_stack *avail_exprs_stack)
{
int i;
- struct edge_info *edge_info = (struct edge_info *) e->aux;
+ class edge_info *edge_info = (class edge_info *) e->aux;
/* If we have info associated with this edge, record it into
our equivalence tables. */
/* If we have 0 = COND or 1 = COND equivalences, record them
into our expression hash tables. */
for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
- {
- avail_exprs_stack->record_cond (eq);
-
- /* If the condition is testing that X == 0 is true or X != 0 is false
- and X is set from a BIT_IOR_EXPR, then we can record equivalences
- for the operands of the BIT_IOR_EXPR (and recurse on those). */
- tree op0 = eq->cond.ops.binary.opnd0;
- tree op1 = eq->cond.ops.binary.opnd1;
- if (TREE_CODE (op0) == SSA_NAME && integer_zerop (op1))
- {
- enum tree_code code = eq->cond.ops.binary.op;
- if ((code == EQ_EXPR && eq->value == boolean_true_node)
- || (code == NE_EXPR && eq->value == boolean_false_node))
- derive_equivalencs_from_bit_ior (op0, const_and_copies);
+ avail_exprs_stack->record_cond (eq);
- /* TODO: We could handle BIT_AND_EXPR in a similar fashion
- recording that the operands have a nonzero value. */
+ edge_info::equiv_pair *seq;
+ for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
+ {
+ tree lhs = seq->first;
+ if (!lhs || TREE_CODE (lhs) != SSA_NAME)
+ continue;
- /* TODO: We can handle more cases here, particularly when OP0 is
- known to have a boolean range. */
- }
- }
+ /* Record the simple NAME = VALUE equivalence. */
+ tree rhs = seq->second;
- tree lhs = edge_info->lhs;
- if (!lhs || TREE_CODE (lhs) != SSA_NAME)
- return;
+ /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
+ cheaper to compute than the other, then set up the equivalence
+ such that we replace the expensive one with the cheap one.
- /* Record the simple NAME = VALUE equivalence. */
- tree rhs = edge_info->rhs;
- record_equality (lhs, rhs, const_and_copies);
+ If they are the same cost to compute, then do not record
+ anything. */
+ if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
+ {
+ gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
+ int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
- /* We already recorded that LHS = RHS, with canonicalization,
- value chain following, etc.
+ gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
+ int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
- We also want to record RHS = LHS, but without any canonicalization
- or value chain following. */
- if (TREE_CODE (rhs) == SSA_NAME)
- const_and_copies->record_const_or_copy_raw (rhs, lhs,
- SSA_NAME_VALUE (rhs));
+ if (rhs_cost > lhs_cost)
+ record_equality (rhs, lhs, const_and_copies);
+ else if (rhs_cost < lhs_cost)
+ record_equality (lhs, rhs, const_and_copies);
+ }
+ else
+ record_equality (lhs, rhs, const_and_copies);
- /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
- set via a widening type conversion, then we may be able to record
- additional equivalences. */
- if (TREE_CODE (rhs) == INTEGER_CST)
- {
- gimple *defstmt = SSA_NAME_DEF_STMT (lhs);
- if (defstmt
- && is_gimple_assign (defstmt)
- && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
- {
- tree old_rhs = gimple_assign_rhs1 (defstmt);
-
- /* If the conversion widens the original value and
- the constant is in the range of the type of OLD_RHS,
- then convert the constant and record the equivalence.
-
- Note that int_fits_type_p does not check the precision
- if the upper and lower bounds are OK. */
- if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
- && (TYPE_PRECISION (TREE_TYPE (lhs))
- > TYPE_PRECISION (TREE_TYPE (old_rhs)))
- && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
- {
- tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
- record_equality (old_rhs, newval, const_and_copies);
- }
- }
+ /* Any equivalence found for LHS may result in additional
+ equivalences for other uses of LHS that we have already
+ processed. */
+ back_propagate_equivalences (lhs, e, const_and_copies);
}
-
- /* Any equivalence found for LHS may result in additional
- equivalences for other uses of LHS that we have already
- processed. */
- back_propagate_equivalences (lhs, e, const_and_copies);
}
}
-/* Wrapper for common code to attempt to thread an edge. For example,
- it handles lazily building the dummy condition and the bookkeeping
- when jump threading is successful. */
-
-void
-dom_opt_dom_walker::thread_across_edge (edge e)
-{
- if (! m_dummy_cond)
- m_dummy_cond =
- gimple_build_cond (NE_EXPR,
- integer_zero_node, integer_zero_node,
- NULL, NULL);
-
- /* Push a marker on both stacks so we can unwind the tables back to their
- current state. */
- m_avail_exprs_stack->push_marker ();
- m_const_and_copies->push_marker ();
-
- /* With all the edge equivalences in the tables, go ahead and attempt
- to thread through E->dest. */
- ::thread_across_edge (m_dummy_cond, e,
- m_const_and_copies, m_avail_exprs_stack,
- simplify_stmt_for_jump_threading);
-
- /* And restore the various tables to their state before
- we threaded this edge.
-
- XXX The code in tree-ssa-threadedge.c will restore the state of
- the const_and_copies table. We we just have to restore the expression
- table. */
- m_avail_exprs_stack->pop_to_marker ();
-}
-
/* PHI nodes can create equivalences too.
Ignoring any alternatives which are the same as the result, if
{
gphi_iterator gsi;
- for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+ for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
{
gphi *phi = gsi.phi ();
+ /* We might eliminate the PHI, so advance GSI now. */
+ gsi_next (&gsi);
+
tree lhs = gimple_phi_result (phi);
tree rhs = NULL;
size_t i;
t = dom_valueize (t);
+ /* If T is an SSA_NAME and its associated edge is a backedge,
+ then quit as we cannot utilize this equivalence. */
+ if (TREE_CODE (t) == SSA_NAME
+ && (gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK))
+ break;
+
/* If we have not processed an alternative yet, then set
RHS to this alternative. */
if (rhs == NULL)
this, since this is a true assignment and not an equivalence
inferred from a comparison. All uses of this ssa name are dominated
by this assignment, so unwinding just costs time and space. */
- if (i == gimple_phi_num_args (phi)
- && may_propagate_copy (lhs, rhs))
- set_ssa_name_value (lhs, rhs);
- }
-}
-
-/* Ignoring loop backedges, if BB has precisely one incoming edge then
- return that edge. Otherwise return NULL. */
-static edge
-single_incoming_edge_ignoring_loop_edges (basic_block bb)
-{
- edge retval = NULL;
- edge e;
- edge_iterator ei;
-
- FOR_EACH_EDGE (e, ei, bb->preds)
- {
- /* A loop back edge can be identified by the destination of
- the edge dominating the source of the edge. */
- if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
- continue;
-
- /* We can safely ignore edges that are not executable. */
- if ((e->flags & EDGE_EXECUTABLE) == 0)
- continue;
-
- /* If we have already seen a non-loop edge, then we must have
- multiple incoming non-loop edges and thus we return NULL. */
- if (retval)
- return NULL;
-
- /* This is the first non-loop incoming edge we have found. Record
- it. */
- retval = e;
+ if (i == gimple_phi_num_args (phi))
+ {
+ if (may_propagate_copy (lhs, rhs))
+ set_ssa_name_value (lhs, rhs);
+ else if (virtual_operand_p (lhs))
+ {
+ gimple *use_stmt;
+ imm_use_iterator iter;
+ use_operand_p use_p;
+ /* For virtual operands we have to propagate into all uses as
+ otherwise we will create overlapping life-ranges. */
+ FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
+ FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
+ SET_USE (use_p, rhs);
+ if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
+ SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
+ gimple_stmt_iterator tmp_gsi = gsi_for_stmt (phi);
+ remove_phi_node (&tmp_gsi, true);
+ }
+ }
}
-
- return retval;
}
/* Record any equivalences created by the incoming edge to BB into
the parent was followed. */
parent = get_immediate_dominator (CDI_DOMINATORS, bb);
- e = single_incoming_edge_ignoring_loop_edges (bb);
+ e = single_pred_edge_ignoring_loop_edges (bb, true);
/* If we had a single incoming edge from our parent block, then enter
any data associated with the edge into our tables. */
/* Returns true when STMT is a simple iv increment. It detects the
following situation:
- i_1 = phi (..., i_2)
- i_2 = i_1 +/- ... */
+ i_1 = phi (..., i_k)
+ [...]
+ i_j = i_{j-1} for each j : 2 <= j <= k-1
+ [...]
+ i_k = i_{k-1} +/- ... */
bool
simple_iv_increment_p (gimple *stmt)
return false;
phi = SSA_NAME_DEF_STMT (preinc);
- if (gimple_code (phi) != GIMPLE_PHI)
- return false;
+ while (gimple_code (phi) != GIMPLE_PHI)
+ {
+ /* Follow trivial copies, but not the DEF used in a back edge,
+ so that we don't prevent coalescing. */
+ if (!gimple_assign_ssa_name_copy_p (phi))
+ return false;
+ preinc = gimple_assign_rhs1 (phi);
+ phi = SSA_NAME_DEF_STMT (preinc);
+ }
for (i = 0; i < gimple_phi_num_args (phi); i++)
if (gimple_phi_arg_def (phi, i) == lhs)
continue;
/* We may have an equivalence associated with this edge. While
- we can not propagate it into non-dominated blocks, we can
+ we cannot propagate it into non-dominated blocks, we can
propagate them into PHIs in non-dominated blocks. */
/* Push the unwind marker so we can reset the const and copies
Don't bother with [01] = COND equivalences, they're not useful
here. */
- struct edge_info *edge_info = (struct edge_info *) e->aux;
+ class edge_info *edge_info = (class edge_info *) e->aux;
+
if (edge_info)
{
- tree lhs = edge_info->lhs;
- tree rhs = edge_info->rhs;
+ edge_info::equiv_pair *seq;
+ for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
+ {
+ tree lhs = seq->first;
+ tree rhs = seq->second;
+
+ if (lhs && TREE_CODE (lhs) == SSA_NAME)
+ const_and_copies->record_const_or_copy (lhs, rhs);
+ }
- if (lhs && TREE_CODE (lhs) == SSA_NAME)
- const_and_copies->record_const_or_copy (lhs, rhs);
}
indx = e->dest_idx;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
+ m_evrp_range_analyzer->enter (bb);
+
/* Push a marker on the stacks of local information so that we know how
far to unwind when we finalize this block. */
m_avail_exprs_stack->push_marker ();
m_avail_exprs_stack->pop_to_marker ();
edge taken_edge = NULL;
+ /* Initialize visited flag ahead of us, it has undefined state on
+ pass entry. */
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- taken_edge
- = optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
+ gimple_set_visited (gsi_stmt (gsi), false);
+ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
+ {
+ /* Do not optimize a stmt twice, substitution might end up with
+ _3 = _3 which is not valid. */
+ if (gimple_visited_p (gsi_stmt (gsi)))
+ {
+ gsi_next (&gsi);
+ continue;
+ }
+
+ m_state->record_ranges_from_stmt (gsi_stmt (gsi), false);
+ bool removed_p = false;
+ taken_edge = this->optimize_stmt (bb, &gsi, &removed_p);
+ if (!removed_p)
+ gimple_set_visited (gsi_stmt (gsi), true);
+
+ /* Go back and visit stmts inserted by folding after substituting
+ into the stmt at gsi. */
+ if (gsi_end_p (gsi))
+ {
+ gcc_checking_assert (removed_p);
+ gsi = gsi_last_bb (bb);
+ while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)))
+ gsi_prev (&gsi);
+ }
+ else
+ {
+ do
+ {
+ gsi_prev (&gsi);
+ }
+ while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)));
+ }
+ if (gsi_end_p (gsi))
+ gsi = gsi_start_bb (bb);
+ else
+ gsi_next (&gsi);
+ }
/* Now prepare to process dominated blocks. */
record_edge_info (bb);
void
dom_opt_dom_walker::after_dom_children (basic_block bb)
{
- gimple *last;
-
- /* If we have an outgoing edge to a block with multiple incoming and
- outgoing edges, then we may be able to thread the edge, i.e., we
- may be able to statically determine which of the outgoing edges
- will be traversed when the incoming edge from BB is traversed. */
- if (single_succ_p (bb)
- && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
- && potentially_threadable_block (single_succ (bb)))
- {
- thread_across_edge (single_succ_edge (bb));
- }
- else if ((last = last_stmt (bb))
- && gimple_code (last) == GIMPLE_COND
- && EDGE_COUNT (bb->succs) == 2
- && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
- && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
- {
- edge true_edge, false_edge;
-
- extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
-
- /* Only try to thread the edge if it reaches a target block with
- more than one predecessor and more than one successor. */
- if (potentially_threadable_block (true_edge->dest))
- thread_across_edge (true_edge);
-
- /* Similarly for the ELSE arm. */
- if (potentially_threadable_block (false_edge->dest))
- thread_across_edge (false_edge);
-
- }
-
- /* These remove expressions local to BB from the tables. */
+ m_threader->thread_outgoing_edges (bb);
m_avail_exprs_stack->pop_to_marker ();
m_const_and_copies->pop_to_marker ();
+ m_evrp_range_analyzer->leave (bb);
}
/* Search for redundant computations in STMT. If any are found, then
else
def = gimple_get_lhs (stmt);
- /* Certain expressions on the RHS can be optimized away, but can not
+ /* Certain expressions on the RHS can be optimized away, but cannot
themselves be entered into the hash tables. */
if (! def
|| TREE_CODE (def) != SSA_NAME
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "==== ASGN ");
- print_generic_expr (dump_file, lhs, 0);
+ print_generic_expr (dump_file, lhs);
fprintf (dump_file, " = ");
- print_generic_expr (dump_file, rhs, 0);
+ print_generic_expr (dump_file, rhs);
fprintf (dump_file, "\n");
}
tree op0 = gimple_assign_rhs1 (stmt);
tree op1 = gimple_assign_rhs2 (stmt);
tree new_rhs
- = build_fold_addr_expr (fold_build2 (MEM_REF,
- TREE_TYPE (TREE_TYPE (op0)),
- unshare_expr (op0),
- fold_convert (ptr_type_node,
- op1)));
+ = build1 (ADDR_EXPR, TREE_TYPE (op0),
+ fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)),
+ unshare_expr (op0), fold_convert (ptr_type_node,
+ op1)));
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "==== ASGN ");
- print_generic_expr (dump_file, lhs, 0);
+ print_generic_expr (dump_file, lhs);
fprintf (dump_file, " = ");
- print_generic_expr (dump_file, new_rhs, 0);
+ print_generic_expr (dump_file, new_rhs);
fprintf (dump_file, "\n");
}
CONST_AND_COPIES. */
static void
-cprop_operand (gimple *stmt, use_operand_p op_p)
+cprop_operand (gimple *stmt, use_operand_p op_p, vr_values *vr_values)
{
tree val;
tree op = USE_FROM_PTR (op_p);
copy of some other variable, use the value or copy stored in
CONST_AND_COPIES. */
val = SSA_NAME_VALUE (op);
+ if (!val)
+ val = vr_values->op_with_constant_singleton_value_range (op);
+
if (val && val != op)
{
/* Do not replace hard register operands in asm statements. */
vdef_ops of STMT. */
static void
-cprop_into_stmt (gimple *stmt)
+cprop_into_stmt (gimple *stmt, vr_values *vr_values)
{
use_operand_p op_p;
ssa_op_iter iter;
operands. */
if (old_op != last_copy_propagated_op)
{
- cprop_operand (stmt, op_p);
+ cprop_operand (stmt, op_p, vr_values);
tree new_op = USE_FROM_PTR (op_p);
if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
}
}
-/* Optimize the statement in block BB pointed to by iterator SI
- using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
+/* If STMT contains a relational test, try to convert it into an
+ equality test if there is only a single value which can ever
+ make the test true.
+
+ For example, if the expression hash table contains:
+
+ TRUE = (i <= 1)
+
+ And we have a test within statement of i >= 1, then we can safely
+ rewrite the test as i == 1 since there only a single value where
+ the test is true.
+
+ This is similar to code in VRP. */
+
+void
+dom_opt_dom_walker::test_for_singularity (gimple *stmt,
+ avail_exprs_stack *avail_exprs_stack)
+{
+ /* We want to support gimple conditionals as well as assignments
+ where the RHS contains a conditional. */
+ if (is_gimple_assign (stmt) || gimple_code (stmt) == GIMPLE_COND)
+ {
+ enum tree_code code = ERROR_MARK;
+ tree lhs, rhs;
+
+ /* Extract the condition of interest from both forms we support. */
+ if (is_gimple_assign (stmt))
+ {
+ code = gimple_assign_rhs_code (stmt);
+ lhs = gimple_assign_rhs1 (stmt);
+ rhs = gimple_assign_rhs2 (stmt);
+ }
+ else if (gimple_code (stmt) == GIMPLE_COND)
+ {
+ code = gimple_cond_code (as_a <gcond *> (stmt));
+ lhs = gimple_cond_lhs (as_a <gcond *> (stmt));
+ rhs = gimple_cond_rhs (as_a <gcond *> (stmt));
+ }
+
+ /* We're looking for a relational test using LE/GE. Also note we can
+ canonicalize LT/GT tests against constants into LE/GT tests. */
+ if (code == LE_EXPR || code == GE_EXPR
+ || ((code == LT_EXPR || code == GT_EXPR)
+ && TREE_CODE (rhs) == INTEGER_CST))
+ {
+ /* For LT_EXPR and GT_EXPR, canonicalize to LE_EXPR and GE_EXPR. */
+ if (code == LT_EXPR)
+ rhs = fold_build2 (MINUS_EXPR, TREE_TYPE (rhs),
+ rhs, build_int_cst (TREE_TYPE (rhs), 1));
+
+ if (code == GT_EXPR)
+ rhs = fold_build2 (PLUS_EXPR, TREE_TYPE (rhs),
+ rhs, build_int_cst (TREE_TYPE (rhs), 1));
+
+ /* Determine the code we want to check for in the hash table. */
+ enum tree_code test_code;
+ if (code == GE_EXPR || code == GT_EXPR)
+ test_code = LE_EXPR;
+ else
+ test_code = GE_EXPR;
+
+ /* Update the dummy statement so we can query the hash tables. */
+ gimple_cond_set_code (m_dummy_cond, test_code);
+ gimple_cond_set_lhs (m_dummy_cond, lhs);
+ gimple_cond_set_rhs (m_dummy_cond, rhs);
+ tree cached_lhs
+ = avail_exprs_stack->lookup_avail_expr (m_dummy_cond,
+ false, false);
+
+ /* If the lookup returned 1 (true), then the expression we
+ queried was in the hash table. As a result there is only
+ one value that makes the original conditional true. Update
+ STMT accordingly. */
+ if (cached_lhs && integer_onep (cached_lhs))
+ {
+ if (is_gimple_assign (stmt))
+ {
+ gimple_assign_set_rhs_code (stmt, EQ_EXPR);
+ gimple_assign_set_rhs2 (stmt, rhs);
+ gimple_set_modified (stmt, true);
+ }
+ else
+ {
+ gimple_set_modified (stmt, true);
+ gimple_cond_set_code (as_a <gcond *> (stmt), EQ_EXPR);
+ gimple_cond_set_rhs (as_a <gcond *> (stmt), rhs);
+ gimple_set_modified (stmt, true);
+ }
+ }
+ }
+ }
+}
+
+/* If STMT is a comparison of two uniform vectors reduce it to a comparison
+ of scalar objects, otherwise leave STMT unchanged. */
+
+static void
+reduce_vector_comparison_to_scalar_comparison (gimple *stmt)
+{
+ if (gimple_code (stmt) == GIMPLE_COND)
+ {
+ tree lhs = gimple_cond_lhs (stmt);
+ tree rhs = gimple_cond_rhs (stmt);
+
+ /* We may have a vector comparison where both arms are uniform
+ vectors. If so, we can simplify the vector comparison down
+ to a scalar comparison. */
+ if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
+ && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
+ {
+ /* If either operand is an SSA_NAME, then look back to its
+ defining statement to try and get at a suitable source. */
+ if (TREE_CODE (rhs) == SSA_NAME)
+ {
+ gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
+ if (gimple_assign_single_p (def_stmt))
+ rhs = gimple_assign_rhs1 (def_stmt);
+ }
+
+ if (TREE_CODE (lhs) == SSA_NAME)
+ {
+ gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
+ if (gimple_assign_single_p (def_stmt))
+ lhs = gimple_assign_rhs1 (def_stmt);
+ }
+
+ /* Now see if they are both uniform vectors and if so replace
+ the vector comparison with a scalar comparison. */
+ tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE;
+ tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE;
+ if (rhs_elem && lhs_elem)
+ {
+ if (dump_file && dump_flags & TDF_DETAILS)
+ {
+ fprintf (dump_file, "Reducing vector comparison: ");
+ print_gimple_stmt (dump_file, stmt, 0);
+ }
+
+ gimple_cond_set_rhs (as_a <gcond *>(stmt), rhs_elem);
+ gimple_cond_set_lhs (as_a <gcond *>(stmt), lhs_elem);
+ gimple_set_modified (stmt, true);
+
+ if (dump_file && dump_flags & TDF_DETAILS)
+ {
+ fprintf (dump_file, "To scalar equivalent: ");
+ print_gimple_stmt (dump_file, stmt, 0);
+ fprintf (dump_file, "\n");
+ }
+ }
+ }
+ }
+}
+
+/* Optimize the statement in block BB pointed to by iterator SI.
We try to perform some simplistic global redundancy elimination and
constant propagation:
2- Constant values and copy assignments. This is used to do very
simplistic constant and copy propagation. When a constant or copy
assignment is found, we map the value on the RHS of the assignment to
- the variable in the LHS in the CONST_AND_COPIES table. */
+ the variable in the LHS in the CONST_AND_COPIES table.
+
+ 3- Very simple redundant store elimination is performed.
+
+ 4- We can simplify a condition to a constant or from a relational
+ condition to an equality condition. */
-static edge
-optimize_stmt (basic_block bb, gimple_stmt_iterator si,
- class const_and_copies *const_and_copies,
- class avail_exprs_stack *avail_exprs_stack)
+edge
+dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
+ bool *removed_p)
{
gimple *stmt, *old_stmt;
bool may_optimize_p;
bool was_noreturn;
edge retval = NULL;
- old_stmt = stmt = gsi_stmt (si);
+ old_stmt = stmt = gsi_stmt (*si);
was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
if (dump_file && (dump_flags & TDF_DETAILS))
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
}
+ /* STMT may be a comparison of uniform vectors that we can simplify
+ down to a comparison of scalars. Do that transformation first
+ so that all the scalar optimizations from here onward apply. */
+ reduce_vector_comparison_to_scalar_comparison (stmt);
+
update_stmt_if_modified (stmt);
opt_stats.num_stmts++;
/* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
- cprop_into_stmt (stmt);
+ cprop_into_stmt (stmt, m_evrp_range_analyzer);
/* If the statement has been modified with constant replacements,
fold its RHS before checking for redundant computations. */
/* Try to fold the statement making sure that STMT is kept
up to date. */
- if (fold_stmt (&si))
+ if (fold_stmt (si))
{
- stmt = gsi_stmt (si);
+ stmt = gsi_stmt (*si);
gimple_set_modified (stmt, true);
if (dump_file && (dump_flags & TDF_DETAILS))
certain that the value simply isn't constant. */
tree callee = gimple_call_fndecl (stmt);
if (callee
- && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
- && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
+ && fndecl_built_in_p (callee, BUILT_IN_CONSTANT_P))
{
- propagate_tree_value_into_stmt (&si, integer_zero_node);
- stmt = gsi_stmt (si);
+ propagate_tree_value_into_stmt (si, integer_zero_node);
+ stmt = gsi_stmt (*si);
}
}
integer_zero_node));
gimple_set_modified (stmt, true);
}
+ else if (TREE_CODE (lhs) == SSA_NAME)
+ {
+ /* Exploiting EVRP data is not yet fully integrated into DOM
+ but we need to do something for this case to avoid regressing
+ udr4.f90 and new1.C which have unexecutable blocks with
+ undefined behavior that get diagnosed if they're left in the
+ IL because we've attached range information to new
+ SSA_NAMES. */
+ update_stmt_if_modified (stmt);
+ edge taken_edge = NULL;
+ m_evrp_range_analyzer->vrp_visit_cond_stmt
+ (as_a <gcond *> (stmt), &taken_edge);
+ if (taken_edge)
+ {
+ if (taken_edge->flags & EDGE_TRUE_VALUE)
+ gimple_cond_make_true (as_a <gcond *> (stmt));
+ else if (taken_edge->flags & EDGE_FALSE_VALUE)
+ gimple_cond_make_false (as_a <gcond *> (stmt));
+ else
+ gcc_unreachable ();
+ gimple_set_modified (stmt, true);
+ update_stmt (stmt);
+ cfg_altered = true;
+ return taken_edge;
+ }
+ }
}
update_stmt_if_modified (stmt);
- eliminate_redundant_computations (&si, const_and_copies,
- avail_exprs_stack);
- stmt = gsi_stmt (si);
+ eliminate_redundant_computations (si, m_const_and_copies,
+ m_avail_exprs_stack);
+ stmt = gsi_stmt (*si);
/* Perform simple redundant store elimination. */
if (gimple_assign_single_p (stmt)
else
new_stmt = gimple_build_assign (rhs, lhs);
gimple_set_vuse (new_stmt, gimple_vuse (stmt));
- cached_lhs = avail_exprs_stack->lookup_avail_expr (new_stmt, false,
- false);
+ expr_hash_elt *elt = NULL;
+ cached_lhs = m_avail_exprs_stack->lookup_avail_expr (new_stmt, false,
+ false, &elt);
if (cached_lhs
- && rhs == cached_lhs)
+ && operand_equal_p (rhs, cached_lhs, 0)
+ && refs_same_for_tbaa_p (elt->expr ()->kind == EXPR_SINGLE
+ ? elt->expr ()->ops.single.rhs
+ : NULL_TREE, lhs))
{
basic_block bb = gimple_bb (stmt);
unlink_stmt_vdef (stmt);
- if (gsi_remove (&si, true))
+ if (gsi_remove (si, true))
{
bitmap_set_bit (need_eh_cleanup, bb->index);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, " Flagged to clear EH edges.\n");
}
release_defs (stmt);
+ *removed_p = true;
return retval;
}
}
+
+ /* If this statement was not redundant, we may still be able to simplify
+ it, which may in turn allow other part of DOM or other passes to do
+ a better job. */
+ test_for_singularity (stmt, m_avail_exprs_stack);
}
/* Record any additional equivalences created by this statement. */
if (is_gimple_assign (stmt))
- record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
+ record_equivalences_from_stmt (stmt, may_optimize_p, m_avail_exprs_stack);
/* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
know where it goes. */