tree-ssa-loop-unswitch.c [plain text]
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "tm_p.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "timevar.h"
#include "cfgloop.h"
#include "domwalk.h"
#include "params.h"
#include "tree-pass.h"
static struct loop *tree_unswitch_loop (struct loops *, struct loop *, basic_block,
tree);
static bool tree_unswitch_single_loop (struct loops *, struct loop *, int);
static tree tree_may_unswitch_on (basic_block, struct loop *);
void
tree_ssa_unswitch_loops (struct loops *loops)
{
int i, num;
struct loop *loop;
bool changed = false;
num = loops->num;
for (i = 1; i < num; i++)
{
loop = loops->parray[i];
if (!loop)
continue;
if (loop->inner)
continue;
changed |= tree_unswitch_single_loop (loops, loop, 0);
#ifdef ENABLE_CHECKING
verify_dominators (CDI_DOMINATORS);
verify_loop_structure (loops);
#endif
}
#if 0
if (changed)
cleanup_tree_cfg_loop ();
#endif
}
static tree
tree_may_unswitch_on (basic_block bb, struct loop *loop)
{
tree stmt, def, cond;
basic_block def_bb;
use_optype uses;
unsigned i;
stmt = last_stmt (bb);
if (!stmt || TREE_CODE (stmt) != COND_EXPR)
return NULL_TREE;
get_stmt_operands (stmt);
uses = STMT_USE_OPS (stmt);
for (i = 0; i < NUM_USES (uses); i++)
{
def = SSA_NAME_DEF_STMT (USE_OP (uses, i));
def_bb = bb_for_stmt (def);
if (def_bb
&& flow_bb_inside_loop_p (loop, def_bb))
return NULL_TREE;
}
cond = COND_EXPR_COND (stmt);
if (integer_zerop (cond) || integer_nonzerop (cond))
return NULL_TREE;
return cond;
}
static tree
simplify_using_entry_checks (struct loop *loop, tree cond)
{
edge e = loop_preheader_edge (loop);
tree stmt;
while (1)
{
stmt = last_stmt (e->src);
if (stmt
&& TREE_CODE (stmt) == COND_EXPR
&& operand_equal_p (COND_EXPR_COND (stmt), cond, 0))
return (e->flags & EDGE_TRUE_VALUE
? boolean_true_node
: boolean_false_node);
if (EDGE_COUNT (e->src->preds) > 1)
return cond;
e = EDGE_PRED (e->src, 0);
if (e->src == ENTRY_BLOCK_PTR)
return cond;
}
}
static bool
tree_unswitch_single_loop (struct loops *loops, struct loop *loop, int num)
{
basic_block *bbs;
struct loop *nloop;
unsigned i;
tree cond = NULL_TREE, stmt;
bool changed = false;
if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
return false;
}
if (loop->inner)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching, not innermost loop\n");
return false;
}
if (tree_num_loop_insns (loop)
> (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching, loop too big\n");
return false;
}
i = 0;
bbs = get_loop_body (loop);
while (1)
{
for (; i < loop->num_nodes; i++)
if ((cond = tree_may_unswitch_on (bbs[i], loop)))
break;
if (i == loop->num_nodes)
{
free (bbs);
return changed;
}
cond = simplify_using_entry_checks (loop, cond);
stmt = last_stmt (bbs[i]);
if (integer_nonzerop (cond))
{
COND_EXPR_COND (stmt) = boolean_true_node;
changed = true;
}
else if (integer_zerop (cond))
{
COND_EXPR_COND (stmt) = boolean_false_node;
changed = true;
}
else
break;
modify_stmt (stmt);
i++;
}
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Unswitching loop\n");
nloop = tree_unswitch_loop (loops, loop, bbs[i], cond);
if (!nloop)
return changed;
tree_unswitch_single_loop (loops, nloop, num + 1);
tree_unswitch_single_loop (loops, loop, num + 1);
return true;
}
static struct loop *
tree_unswitch_loop (struct loops *loops, struct loop *loop,
basic_block unswitch_on, tree cond)
{
basic_block condition_bb;
gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
gcc_assert (loop->inner == NULL);
return tree_ssa_loop_version (loops, loop, unshare_expr (cond),
&condition_bb);
}