2013-11-16 23:59:05 +00:00
|
|
|
//===-- LoopReroll.cpp - Loop rerolling pass ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements a simple loop reroller.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2015-01-29 21:52:03 +00:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-01-29 21:52:03 +00:00
|
|
|
#include "llvm/ADT/SmallBitVector.h"
|
2013-11-16 23:59:05 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/AliasSetTracker.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-11-16 23:59:05 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-01-15 02:16:27 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-11-16 23:59:05 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "loop-reroll"
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
STATISTIC(NumRerolledLoops, "Number of rerolled loops");
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxInc("max-reroll-increment", cl::init(2048), cl::Hidden,
|
|
|
|
cl::desc("The maximum increment for loop rerolling"));
|
|
|
|
|
2015-02-12 15:54:14 +00:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("The maximum number of failures to tolerate"
|
|
|
|
" during fuzzy matching. (default: 400)"));
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
// This loop re-rolling transformation aims to transform loops like this:
|
|
|
|
//
|
|
|
|
// int foo(int a);
|
|
|
|
// void bar(int *x) {
|
|
|
|
// for (int i = 0; i < 500; i += 3) {
|
|
|
|
// foo(i);
|
|
|
|
// foo(i+1);
|
|
|
|
// foo(i+2);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// into a loop like this:
|
|
|
|
//
|
|
|
|
// void bar(int *x) {
|
|
|
|
// for (int i = 0; i < 500; ++i)
|
|
|
|
// foo(i);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It does this by looking for loops that, besides the latch code, are composed
|
|
|
|
// of isomorphic DAGs of instructions, with each DAG rooted at some increment
|
|
|
|
// to the induction variable, and where each DAG is isomorphic to the DAG
|
|
|
|
// rooted at the induction variable (excepting the sub-DAGs which root the
|
|
|
|
// other induction-variable increments). In other words, we're looking for loop
|
|
|
|
// bodies of the form:
|
|
|
|
//
|
|
|
|
// %iv = phi [ (preheader, ...), (body, %iv.next) ]
|
|
|
|
// f(%iv)
|
|
|
|
// %iv.1 = add %iv, 1 <-- a root increment
|
|
|
|
// f(%iv.1)
|
|
|
|
// %iv.2 = add %iv, 2 <-- a root increment
|
|
|
|
// f(%iv.2)
|
|
|
|
// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
|
|
|
|
// f(%iv.scale_m_1)
|
|
|
|
// ...
|
|
|
|
// %iv.next = add %iv, scale
|
|
|
|
// %cmp = icmp(%iv, ...)
|
|
|
|
// br %cmp, header, exit
|
|
|
|
//
|
|
|
|
// where each f(i) is a set of instructions that, collectively, are a function
|
|
|
|
// only of i (and other loop-invariant values).
|
|
|
|
//
|
|
|
|
// As a special case, we can also reroll loops like this:
|
|
|
|
//
|
|
|
|
// int foo(int);
|
|
|
|
// void bar(int *x) {
|
|
|
|
// for (int i = 0; i < 500; ++i) {
|
|
|
|
// x[3*i] = foo(0);
|
|
|
|
// x[3*i+1] = foo(0);
|
|
|
|
// x[3*i+2] = foo(0);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// into this:
|
|
|
|
//
|
|
|
|
// void bar(int *x) {
|
|
|
|
// for (int i = 0; i < 1500; ++i)
|
|
|
|
// x[i] = foo(0);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// in which case, we're looking for inputs like this:
|
|
|
|
//
|
|
|
|
// %iv = phi [ (preheader, ...), (body, %iv.next) ]
|
|
|
|
// %scaled.iv = mul %iv, scale
|
|
|
|
// f(%scaled.iv)
|
|
|
|
// %scaled.iv.1 = add %scaled.iv, 1
|
|
|
|
// f(%scaled.iv.1)
|
|
|
|
// %scaled.iv.2 = add %scaled.iv, 2
|
|
|
|
// f(%scaled.iv.2)
|
|
|
|
// %scaled.iv.scale_m_1 = add %scaled.iv, scale-1
|
|
|
|
// f(%scaled.iv.scale_m_1)
|
|
|
|
// ...
|
|
|
|
// %iv.next = add %iv, 1
|
|
|
|
// %cmp = icmp(%iv, ...)
|
|
|
|
// br %cmp, header, exit
|
|
|
|
|
|
|
|
namespace {
|
2015-01-29 21:52:03 +00:00
|
|
|
enum IterationLimits {
|
|
|
|
/// The maximum number of iterations that we'll try and reroll. This
|
|
|
|
/// has to be less than 25 in order to fit into a SmallBitVector.
|
|
|
|
IL_MaxRerollIterations = 16,
|
|
|
|
/// The bitvector index used by loop induction variables and other
|
2015-02-11 09:19:47 +00:00
|
|
|
/// instructions that belong to all iterations.
|
|
|
|
IL_All,
|
2015-01-29 21:52:03 +00:00
|
|
|
IL_End
|
|
|
|
};
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
class LoopReroll : public LoopPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
|
|
|
LoopReroll() : LoopPass(ID) {
|
|
|
|
initializeLoopRerollPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM) override;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-11-16 23:59:05 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2015-01-17 14:16:18 +00:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2013-11-16 23:59:05 +00:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
2015-01-15 10:41:28 +00:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
protected:
|
2013-11-16 23:59:05 +00:00
|
|
|
AliasAnalysis *AA;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
TargetLibraryInfo *TLI;
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
|
|
|
typedef SmallVector<Instruction *, 16> SmallInstructionVector;
|
|
|
|
typedef SmallSet<Instruction *, 16> SmallInstructionSet;
|
|
|
|
|
|
|
|
// A chain of isomorphic instructions, indentified by a single-use PHI,
|
|
|
|
// representing a reduction. Only the last value may be used outside the
|
|
|
|
// loop.
|
|
|
|
struct SimpleLoopReduction {
|
|
|
|
SimpleLoopReduction(Instruction *P, Loop *L)
|
|
|
|
: Valid(false), Instructions(1, P) {
|
|
|
|
assert(isa<PHINode>(P) && "First reduction instruction must be a PHI");
|
|
|
|
add(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid() const {
|
|
|
|
return Valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *getPHI() const {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
|
|
|
return Instructions.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *getReducedValue() const {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
|
|
|
return Instructions.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *get(size_t i) const {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
|
|
|
return Instructions[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *operator [] (size_t i) const { return get(i); }
|
|
|
|
|
|
|
|
// The size, ignoring the initial PHI.
|
|
|
|
size_t size() const {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
|
|
|
return Instructions.size()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef SmallInstructionVector::iterator iterator;
|
|
|
|
typedef SmallInstructionVector::const_iterator const_iterator;
|
|
|
|
|
|
|
|
iterator begin() {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
2014-03-02 12:27:27 +00:00
|
|
|
return std::next(Instructions.begin());
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
|
|
|
assert(Valid && "Using invalid reduction");
|
2014-03-02 12:27:27 +00:00
|
|
|
return std::next(Instructions.begin());
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() { return Instructions.end(); }
|
|
|
|
const_iterator end() const { return Instructions.end(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool Valid;
|
|
|
|
SmallInstructionVector Instructions;
|
|
|
|
|
|
|
|
void add(Loop *L);
|
|
|
|
};
|
|
|
|
|
|
|
|
// The set of all reductions, and state tracking of possible reductions
|
|
|
|
// during loop instruction processing.
|
|
|
|
struct ReductionTracker {
|
|
|
|
typedef SmallVector<SimpleLoopReduction, 16> SmallReductionVector;
|
|
|
|
|
|
|
|
// Add a new possible reduction.
|
2014-10-28 11:54:52 +00:00
|
|
|
void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); }
|
2013-11-16 23:59:05 +00:00
|
|
|
|
|
|
|
// Setup to track possible reductions corresponding to the provided
|
|
|
|
// rerolling scale. Only reductions with a number of non-PHI instructions
|
|
|
|
// that is divisible by the scale are considered. Three instructions sets
|
|
|
|
// are filled in:
|
|
|
|
// - A set of all possible instructions in eligible reductions.
|
|
|
|
// - A set of all PHIs in eligible reductions
|
2014-10-28 11:54:52 +00:00
|
|
|
// - A set of all reduced values (last instructions) in eligible
|
|
|
|
// reductions.
|
2013-11-16 23:59:05 +00:00
|
|
|
void restrictToScale(uint64_t Scale,
|
|
|
|
SmallInstructionSet &PossibleRedSet,
|
|
|
|
SmallInstructionSet &PossibleRedPHISet,
|
|
|
|
SmallInstructionSet &PossibleRedLastSet) {
|
|
|
|
PossibleRedIdx.clear();
|
|
|
|
PossibleRedIter.clear();
|
|
|
|
Reds.clear();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
|
|
|
|
if (PossibleReds[i].size() % Scale == 0) {
|
|
|
|
PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
|
|
|
|
PossibleRedPHISet.insert(PossibleReds[i].getPHI());
|
2014-10-28 11:53:30 +00:00
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
PossibleRedSet.insert(PossibleReds[i].getPHI());
|
|
|
|
PossibleRedIdx[PossibleReds[i].getPHI()] = i;
|
2014-10-28 11:54:05 +00:00
|
|
|
for (Instruction *J : PossibleReds[i]) {
|
|
|
|
PossibleRedSet.insert(J);
|
|
|
|
PossibleRedIdx[J] = i;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The functions below are used while processing the loop instructions.
|
|
|
|
|
|
|
|
// Are the two instructions both from reductions, and furthermore, from
|
|
|
|
// the same reduction?
|
|
|
|
bool isPairInSame(Instruction *J1, Instruction *J2) {
|
|
|
|
DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1);
|
|
|
|
if (J1I != PossibleRedIdx.end()) {
|
|
|
|
DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2);
|
|
|
|
if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The two provided instructions, the first from the base iteration, and
|
|
|
|
// the second from iteration i, form a matched pair. If these are part of
|
|
|
|
// a reduction, record that fact.
|
|
|
|
void recordPair(Instruction *J1, Instruction *J2, unsigned i) {
|
|
|
|
if (PossibleRedIdx.count(J1)) {
|
|
|
|
assert(PossibleRedIdx.count(J2) &&
|
|
|
|
"Recording reduction vs. non-reduction instruction?");
|
|
|
|
|
|
|
|
PossibleRedIter[J1] = 0;
|
|
|
|
PossibleRedIter[J2] = i;
|
|
|
|
|
|
|
|
int Idx = PossibleRedIdx[J1];
|
|
|
|
assert(Idx == PossibleRedIdx[J2] &&
|
|
|
|
"Recording pair from different reductions?");
|
2013-11-17 01:21:54 +00:00
|
|
|
Reds.insert(Idx);
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The functions below can be called after we've finished processing all
|
|
|
|
// instructions in the loop, and we know which reductions were selected.
|
|
|
|
|
|
|
|
// Is the provided instruction the PHI of a reduction selected for
|
|
|
|
// rerolling?
|
|
|
|
bool isSelectedPHI(Instruction *J) {
|
|
|
|
if (!isa<PHINode>(J))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
|
|
|
|
RI != RIE; ++RI) {
|
|
|
|
int i = *RI;
|
|
|
|
if (cast<Instruction>(J) == PossibleReds[i].getPHI())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validateSelected();
|
|
|
|
void replaceSelected();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// The vector of all possible reductions (for any scale).
|
|
|
|
SmallReductionVector PossibleReds;
|
|
|
|
|
|
|
|
DenseMap<Instruction *, int> PossibleRedIdx;
|
|
|
|
DenseMap<Instruction *, int> PossibleRedIter;
|
|
|
|
DenseSet<int> Reds;
|
|
|
|
};
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// A DAGRootSet models an induction variable being used in a rerollable
|
|
|
|
// loop. For example,
|
|
|
|
//
|
|
|
|
// x[i*3+0] = y1
|
|
|
|
// x[i*3+1] = y2
|
|
|
|
// x[i*3+2] = y3
|
|
|
|
//
|
|
|
|
// Base instruction -> i*3
|
|
|
|
// +---+----+
|
|
|
|
// / | \
|
|
|
|
// ST[y1] +1 +2 <-- Roots
|
|
|
|
// | |
|
|
|
|
// ST[y2] ST[y3]
|
|
|
|
//
|
|
|
|
// There may be multiple DAGRoots, for example:
|
|
|
|
//
|
|
|
|
// x[i*2+0] = ... (1)
|
|
|
|
// x[i*2+1] = ... (1)
|
|
|
|
// x[i*2+4] = ... (2)
|
|
|
|
// x[i*2+5] = ... (2)
|
|
|
|
// x[(i+1234)*2+5678] = ... (3)
|
|
|
|
// x[(i+1234)*2+5679] = ... (3)
|
|
|
|
//
|
|
|
|
// The loop will be rerolled by adding a new loop induction variable,
|
|
|
|
// one for the Base instruction in each DAGRootSet.
|
|
|
|
//
|
|
|
|
struct DAGRootSet {
|
|
|
|
Instruction *BaseInst;
|
|
|
|
SmallInstructionVector Roots;
|
|
|
|
// The instructions between IV and BaseInst (but not including BaseInst).
|
|
|
|
SmallInstructionSet SubsumedInsts;
|
|
|
|
};
|
|
|
|
|
2015-01-29 13:48:05 +00:00
|
|
|
// The set of all DAG roots, and state tracking of all roots
|
|
|
|
// for a particular induction variable.
|
|
|
|
struct DAGRootTracker {
|
|
|
|
DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV,
|
|
|
|
ScalarEvolution *SE, AliasAnalysis *AA,
|
2015-03-10 02:37:25 +00:00
|
|
|
TargetLibraryInfo *TLI)
|
|
|
|
: Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), IV(IV) {}
|
2015-01-29 13:48:05 +00:00
|
|
|
|
|
|
|
/// Stage 1: Find all the DAG roots for the induction variable.
|
|
|
|
bool findRoots();
|
|
|
|
/// Stage 2: Validate if the found roots are valid.
|
|
|
|
bool validate(ReductionTracker &Reductions);
|
|
|
|
/// Stage 3: Assuming validate() returned true, perform the
|
|
|
|
/// replacement.
|
|
|
|
/// @param IterCount The maximum iteration count of L.
|
|
|
|
void replace(const SCEV *IterCount);
|
|
|
|
|
|
|
|
protected:
|
2015-01-29 21:52:03 +00:00
|
|
|
typedef MapVector<Instruction*, SmallBitVector> UsesTy;
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
bool findRootsRecursive(Instruction *IVU,
|
|
|
|
SmallInstructionSet SubsumedInsts);
|
|
|
|
bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts);
|
|
|
|
bool collectPossibleRoots(Instruction *Base,
|
|
|
|
std::map<int64_t,Instruction*> &Roots);
|
2015-01-29 13:48:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
|
2015-01-29 13:48:05 +00:00
|
|
|
void collectInLoopUserSet(const SmallInstructionVector &Roots,
|
|
|
|
const SmallInstructionSet &Exclude,
|
|
|
|
const SmallInstructionSet &Final,
|
|
|
|
DenseSet<Instruction *> &Users);
|
|
|
|
void collectInLoopUserSet(Instruction *Root,
|
|
|
|
const SmallInstructionSet &Exclude,
|
|
|
|
const SmallInstructionSet &Final,
|
|
|
|
DenseSet<Instruction *> &Users);
|
|
|
|
|
2015-02-12 15:54:14 +00:00
|
|
|
UsesTy::iterator nextInstr(int Val, UsesTy &In,
|
|
|
|
const SmallInstructionSet &Exclude,
|
|
|
|
UsesTy::iterator *StartI=nullptr);
|
2015-02-11 09:19:47 +00:00
|
|
|
bool isBaseInst(Instruction *I);
|
|
|
|
bool isRootInst(Instruction *I);
|
2015-02-12 15:54:14 +00:00
|
|
|
bool instrDependsOn(Instruction *I,
|
|
|
|
UsesTy::iterator Start,
|
|
|
|
UsesTy::iterator End);
|
2015-01-29 21:52:03 +00:00
|
|
|
|
2015-01-29 13:48:05 +00:00
|
|
|
LoopReroll *Parent;
|
|
|
|
|
|
|
|
// Members of Parent, replicated here for brevity.
|
|
|
|
Loop *L;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
TargetLibraryInfo *TLI;
|
|
|
|
|
|
|
|
// The loop induction variable.
|
|
|
|
Instruction *IV;
|
|
|
|
// Loop step amount.
|
|
|
|
uint64_t Inc;
|
|
|
|
// Loop reroll count; if Inc == 1, this records the scaling applied
|
|
|
|
// to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
|
|
|
|
// If Inc is not 1, Scale = Inc.
|
|
|
|
uint64_t Scale;
|
|
|
|
// The roots themselves.
|
2015-02-11 09:19:47 +00:00
|
|
|
SmallVector<DAGRootSet,16> RootSets;
|
2015-01-29 13:48:05 +00:00
|
|
|
// All increment instructions for IV.
|
|
|
|
SmallInstructionVector LoopIncs;
|
2015-01-29 21:52:03 +00:00
|
|
|
// Map of all instructions in the loop (in order) to the iterations
|
2015-02-11 09:19:47 +00:00
|
|
|
// they are used in (or specially, IL_All for instructions
|
2015-01-29 21:52:03 +00:00
|
|
|
// used in the loop increment mechanism).
|
|
|
|
UsesTy Uses;
|
2015-01-29 13:48:05 +00:00
|
|
|
};
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs);
|
|
|
|
void collectPossibleReductions(Loop *L,
|
|
|
|
ReductionTracker &Reductions);
|
|
|
|
bool reroll(Instruction *IV, Loop *L, BasicBlock *Header, const SCEV *IterCount,
|
|
|
|
ReductionTracker &Reductions);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char LoopReroll::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
2015-01-17 14:16:18 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2014-01-13 13:07:17 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2013-11-16 23:59:05 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
2015-01-15 10:41:28 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2013-11-16 23:59:05 +00:00
|
|
|
INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createLoopRerollPass() {
|
|
|
|
return new LoopReroll;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the provided instruction is used outside the given loop.
|
|
|
|
// This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
|
|
|
|
// non-loop blocks to be outside the loop.
|
|
|
|
static bool hasUsesOutsideLoop(Instruction *I, Loop *L) {
|
2015-01-29 21:52:03 +00:00
|
|
|
for (User *U : I->users()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
if (!L->contains(cast<Instruction>(U)))
|
2013-11-16 23:59:05 +00:00
|
|
|
return true;
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the list of loop induction variables with respect to which it might
|
|
|
|
// be possible to reroll the loop.
|
|
|
|
void LoopReroll::collectPossibleIVs(Loop *L,
|
|
|
|
SmallInstructionVector &PossibleIVs) {
|
|
|
|
BasicBlock *Header = L->getHeader();
|
|
|
|
for (BasicBlock::iterator I = Header->begin(),
|
|
|
|
IE = Header->getFirstInsertionPt(); I != IE; ++I) {
|
|
|
|
if (!isa<PHINode>(I))
|
|
|
|
continue;
|
|
|
|
if (!I->getType()->isIntegerTy())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (const SCEVAddRecExpr *PHISCEV =
|
|
|
|
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(I))) {
|
|
|
|
if (PHISCEV->getLoop() != L)
|
|
|
|
continue;
|
|
|
|
if (!PHISCEV->isAffine())
|
|
|
|
continue;
|
|
|
|
if (const SCEVConstant *IncSCEV =
|
|
|
|
dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE))) {
|
|
|
|
if (!IncSCEV->getValue()->getValue().isStrictlyPositive())
|
|
|
|
continue;
|
|
|
|
if (IncSCEV->getValue()->uge(MaxInc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "LRR: Possible IV: " << *I << " = " <<
|
|
|
|
*PHISCEV << "\n");
|
|
|
|
PossibleIVs.push_back(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the remainder of the reduction-variable chain to the instruction vector
|
|
|
|
// (the initial PHINode has already been added). If successful, the object is
|
|
|
|
// marked as valid.
|
|
|
|
void LoopReroll::SimpleLoopReduction::add(Loop *L) {
|
|
|
|
assert(!Valid && "Cannot add to an already-valid chain");
|
|
|
|
|
|
|
|
// The reduction variable must be a chain of single-use instructions
|
|
|
|
// (including the PHI), except for the last value (which is used by the PHI
|
|
|
|
// and also outside the loop).
|
|
|
|
Instruction *C = Instructions.front();
|
2015-02-16 17:01:52 +00:00
|
|
|
if (C->user_empty())
|
|
|
|
return;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
|
|
|
do {
|
2014-03-09 03:16:01 +00:00
|
|
|
C = cast<Instruction>(*C->user_begin());
|
2013-11-16 23:59:05 +00:00
|
|
|
if (C->hasOneUse()) {
|
|
|
|
if (!C->isBinaryOp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(isa<PHINode>(Instructions.back()) ||
|
|
|
|
C->isSameOperationAs(Instructions.back())))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Instructions.push_back(C);
|
|
|
|
}
|
|
|
|
} while (C->hasOneUse());
|
|
|
|
|
|
|
|
if (Instructions.size() < 2 ||
|
|
|
|
!C->isSameOperationAs(Instructions.back()) ||
|
2014-03-09 03:16:01 +00:00
|
|
|
C->use_empty())
|
2013-11-16 23:59:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// C is now the (potential) last instruction in the reduction chain.
|
2015-01-29 21:52:03 +00:00
|
|
|
for (User *U : C->users()) {
|
2013-11-16 23:59:05 +00:00
|
|
|
// The only in-loop user can be the initial PHI.
|
2014-03-09 03:16:01 +00:00
|
|
|
if (L->contains(cast<Instruction>(U)))
|
|
|
|
if (cast<Instruction>(U) != Instructions.front())
|
2013-11-16 23:59:05 +00:00
|
|
|
return;
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
|
|
|
Instructions.push_back(C);
|
|
|
|
Valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the vector of possible reduction variables.
|
|
|
|
void LoopReroll::collectPossibleReductions(Loop *L,
|
|
|
|
ReductionTracker &Reductions) {
|
|
|
|
BasicBlock *Header = L->getHeader();
|
|
|
|
for (BasicBlock::iterator I = Header->begin(),
|
|
|
|
IE = Header->getFirstInsertionPt(); I != IE; ++I) {
|
|
|
|
if (!isa<PHINode>(I))
|
|
|
|
continue;
|
|
|
|
if (!I->getType()->isSingleValueType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SimpleLoopReduction SLR(I, L);
|
|
|
|
if (!SLR.valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with " <<
|
|
|
|
SLR.size() << " chained instructions)\n");
|
|
|
|
Reductions.addSLR(SLR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the set of all users of the provided root instruction. This set of
|
|
|
|
// users contains not only the direct users of the root instruction, but also
|
|
|
|
// all users of those users, and so on. There are two exceptions:
|
|
|
|
//
|
|
|
|
// 1. Instructions in the set of excluded instructions are never added to the
|
|
|
|
// use set (even if they are users). This is used, for example, to exclude
|
|
|
|
// including root increments in the use set of the primary IV.
|
|
|
|
//
|
|
|
|
// 2. Instructions in the set of final instructions are added to the use set
|
|
|
|
// if they are users, but their users are not added. This is used, for
|
|
|
|
// example, to prevent a reduction update from forcing all later reduction
|
|
|
|
// updates into the use set.
|
2015-01-29 13:48:05 +00:00
|
|
|
void LoopReroll::DAGRootTracker::collectInLoopUserSet(
|
2013-11-16 23:59:05 +00:00
|
|
|
Instruction *Root, const SmallInstructionSet &Exclude,
|
|
|
|
const SmallInstructionSet &Final,
|
|
|
|
DenseSet<Instruction *> &Users) {
|
|
|
|
SmallInstructionVector Queue(1, Root);
|
|
|
|
while (!Queue.empty()) {
|
|
|
|
Instruction *I = Queue.pop_back_val();
|
|
|
|
if (!Users.insert(I).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Final.count(I))
|
2014-03-09 03:16:01 +00:00
|
|
|
for (Use &U : I->uses()) {
|
|
|
|
Instruction *User = cast<Instruction>(U.getUser());
|
2013-11-16 23:59:05 +00:00
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(User)) {
|
|
|
|
// Ignore "wrap-around" uses to PHIs of this loop's header.
|
2014-03-09 03:16:01 +00:00
|
|
|
if (PN->getIncomingBlock(U) == L->getHeader())
|
2013-11-16 23:59:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-28 11:53:30 +00:00
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
if (L->contains(User) && !Exclude.count(User)) {
|
|
|
|
Queue.push_back(User);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We also want to collect single-user "feeder" values.
|
|
|
|
for (User::op_iterator OI = I->op_begin(),
|
|
|
|
OIE = I->op_end(); OI != OIE; ++OI) {
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(*OI))
|
|
|
|
if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) &&
|
|
|
|
!Final.count(Op))
|
|
|
|
Queue.push_back(Op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all of the users of all of the provided root instructions (combined
|
|
|
|
// into a single set).
|
2015-01-29 13:48:05 +00:00
|
|
|
void LoopReroll::DAGRootTracker::collectInLoopUserSet(
|
2013-11-16 23:59:05 +00:00
|
|
|
const SmallInstructionVector &Roots,
|
|
|
|
const SmallInstructionSet &Exclude,
|
|
|
|
const SmallInstructionSet &Final,
|
|
|
|
DenseSet<Instruction *> &Users) {
|
|
|
|
for (SmallInstructionVector::const_iterator I = Roots.begin(),
|
|
|
|
IE = Roots.end(); I != IE; ++I)
|
2015-01-29 13:48:05 +00:00
|
|
|
collectInLoopUserSet(*I, Exclude, Final, Users);
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSimpleLoadStore(Instruction *I) {
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I))
|
|
|
|
return LI->isSimple();
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
|
|
|
return SI->isSimple();
|
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
|
|
|
|
return !MI->isVolatile();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
/// Return true if IVU is a "simple" arithmetic operation.
|
|
|
|
/// This is used for narrowing the search space for DAGRoots; only arithmetic
|
|
|
|
/// and GEPs can be part of a DAGRoot.
|
|
|
|
static bool isSimpleArithmeticOp(User *IVU) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(IVU)) {
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-29 13:48:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
static bool isLoopIncrement(User *U, Instruction *IV) {
|
|
|
|
BinaryOperator *BO = dyn_cast<BinaryOperator>(U);
|
|
|
|
if (!BO || BO->getOpcode() != Instruction::Add)
|
2015-01-29 13:48:05 +00:00
|
|
|
return false;
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
for (auto *UU : BO->users()) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(UU);
|
|
|
|
if (PN && PN == IV)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-29 13:48:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::
|
|
|
|
collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) {
|
|
|
|
SmallInstructionVector BaseUsers;
|
|
|
|
|
|
|
|
for (auto *I : Base->users()) {
|
|
|
|
ConstantInt *CI = nullptr;
|
|
|
|
|
|
|
|
if (isLoopIncrement(I, IV)) {
|
|
|
|
LoopIncs.push_back(cast<Instruction>(I));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The root nodes must be either GEPs, ORs or ADDs.
|
|
|
|
if (auto *BO = dyn_cast<BinaryOperator>(I)) {
|
|
|
|
if (BO->getOpcode() == Instruction::Add ||
|
|
|
|
BO->getOpcode() == Instruction::Or)
|
|
|
|
CI = dyn_cast<ConstantInt>(BO->getOperand(1));
|
|
|
|
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1);
|
|
|
|
CI = dyn_cast<ConstantInt>(LastOperand);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CI) {
|
|
|
|
if (Instruction *II = dyn_cast<Instruction>(I)) {
|
|
|
|
BaseUsers.push_back(II);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t V = CI->getValue().getSExtValue();
|
|
|
|
if (Roots.find(V) != Roots.end())
|
|
|
|
// No duplicates, please.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Add support for negative values.
|
|
|
|
if (V < 0) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting due to negative value: " << V << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Roots[V] = cast<Instruction>(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Roots.empty())
|
2015-01-29 21:52:03 +00:00
|
|
|
return false;
|
2015-02-11 09:19:47 +00:00
|
|
|
|
|
|
|
// If we found non-loop-inc, non-root users of Base, assume they are
|
|
|
|
// for the zeroth root index. This is because "add %a, 0" gets optimized
|
|
|
|
// away.
|
2015-02-16 17:02:00 +00:00
|
|
|
if (BaseUsers.size()) {
|
|
|
|
if (Roots.find(0) != Roots.end()) {
|
|
|
|
DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-11 09:19:47 +00:00
|
|
|
Roots[0] = Base;
|
2015-02-16 17:02:00 +00:00
|
|
|
}
|
2015-02-11 09:19:47 +00:00
|
|
|
|
|
|
|
// Calculate the number of users of the base, or lowest indexed, iteration.
|
|
|
|
unsigned NumBaseUses = BaseUsers.size();
|
|
|
|
if (NumBaseUses == 0)
|
|
|
|
NumBaseUses = Roots.begin()->second->getNumUses();
|
|
|
|
|
|
|
|
// Check that every node has the same number of users.
|
|
|
|
for (auto &KV : Roots) {
|
|
|
|
if (KV.first == 0)
|
|
|
|
continue;
|
|
|
|
if (KV.second->getNumUses() != NumBaseUses) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
|
|
|
|
<< "#Base=" << NumBaseUses << ", #Root=" <<
|
|
|
|
KV.second->getNumUses() << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
return true;
|
2015-01-29 13:48:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::
|
|
|
|
findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) {
|
|
|
|
// Does the user look like it could be part of a root set?
|
|
|
|
// All its users must be simple arithmetic ops.
|
|
|
|
if (I->getNumUses() > IL_MaxRerollIterations)
|
2013-11-16 23:59:05 +00:00
|
|
|
return false;
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
if ((I->getOpcode() == Instruction::Mul ||
|
|
|
|
I->getOpcode() == Instruction::PHI) &&
|
|
|
|
I != IV &&
|
|
|
|
findRootsBase(I, SubsumedInsts))
|
|
|
|
return true;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
SubsumedInsts.insert(I);
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
for (User *V : I->users()) {
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (std::find(LoopIncs.begin(), LoopIncs.end(), I) != LoopIncs.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!I || !isSimpleArithmeticOp(I) ||
|
|
|
|
!findRootsRecursive(I, SubsumedInsts))
|
2013-11-16 23:59:05 +00:00
|
|
|
return false;
|
2015-02-11 09:19:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopReroll::DAGRootTracker::
|
|
|
|
findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) {
|
|
|
|
|
|
|
|
// The base instruction needs to be a multiply so
|
|
|
|
// that we can erase it.
|
|
|
|
if (IVU->getOpcode() != Instruction::Mul &&
|
|
|
|
IVU->getOpcode() != Instruction::PHI)
|
2013-11-16 23:59:05 +00:00
|
|
|
return false;
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
std::map<int64_t, Instruction*> V;
|
|
|
|
if (!collectPossibleRoots(IVU, V))
|
|
|
|
return false;
|
2015-01-29 13:48:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// If we didn't get a root for index zero, then IVU must be
|
|
|
|
// subsumed.
|
|
|
|
if (V.find(0) == V.end())
|
|
|
|
SubsumedInsts.insert(IVU);
|
|
|
|
|
|
|
|
// Partition the vector into monotonically increasing indexes.
|
|
|
|
DAGRootSet DRS;
|
|
|
|
DRS.BaseInst = nullptr;
|
|
|
|
|
|
|
|
for (auto &KV : V) {
|
|
|
|
if (!DRS.BaseInst) {
|
|
|
|
DRS.BaseInst = KV.second;
|
|
|
|
DRS.SubsumedInsts = SubsumedInsts;
|
|
|
|
} else if (DRS.Roots.empty()) {
|
|
|
|
DRS.Roots.push_back(KV.second);
|
|
|
|
} else if (V.find(KV.first - 1) != V.end()) {
|
|
|
|
DRS.Roots.push_back(KV.second);
|
|
|
|
} else {
|
|
|
|
// Linear sequence terminated.
|
|
|
|
RootSets.push_back(DRS);
|
|
|
|
DRS.BaseInst = KV.second;
|
|
|
|
DRS.SubsumedInsts = SubsumedInsts;
|
|
|
|
DRS.Roots.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RootSets.push_back(DRS);
|
2015-01-29 13:48:05 +00:00
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::findRoots() {
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
const SCEVAddRecExpr *RealIVSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(IV));
|
|
|
|
Inc = cast<SCEVConstant>(RealIVSCEV->getOperand(1))->
|
|
|
|
getValue()->getZExtValue();
|
|
|
|
|
|
|
|
assert(RootSets.empty() && "Unclean state!");
|
|
|
|
if (Inc == 1) {
|
|
|
|
for (auto *IVU : IV->users()) {
|
|
|
|
if (isLoopIncrement(IVU, IV))
|
|
|
|
LoopIncs.push_back(cast<Instruction>(IVU));
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
2015-02-11 09:19:47 +00:00
|
|
|
if (!findRootsRecursive(IV, SmallInstructionSet()))
|
|
|
|
return false;
|
|
|
|
LoopIncs.push_back(IV);
|
|
|
|
} else {
|
|
|
|
if (!findRootsBase(IV, SmallInstructionSet()))
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// Ensure all sets have the same size.
|
|
|
|
if (RootSets.empty()) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto &V : RootSets) {
|
|
|
|
if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
|
|
|
|
DEBUG(dbgs()
|
|
|
|
<< "LRR: Aborting because not all root sets have the same size\n");
|
2015-01-29 13:48:05 +00:00
|
|
|
return false;
|
2015-02-11 09:19:47 +00:00
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// And ensure all loop iterations are consecutive. We rely on std::map
|
|
|
|
// providing ordered traversal.
|
|
|
|
for (auto &V : RootSets) {
|
|
|
|
const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(V.BaseInst));
|
|
|
|
if (!ADR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Consider a DAGRootSet with N-1 roots (so N different values including
|
|
|
|
// BaseInst).
|
|
|
|
// Define d = Roots[0] - BaseInst, which should be the same as
|
|
|
|
// Roots[I] - Roots[I-1] for all I in [1..N).
|
|
|
|
// Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
|
|
|
|
// loop iteration J.
|
|
|
|
//
|
|
|
|
// Now, For the loop iterations to be consecutive:
|
|
|
|
// D = d * N
|
|
|
|
|
|
|
|
unsigned N = V.Roots.size() + 1;
|
|
|
|
const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(V.Roots[0]), ADR);
|
|
|
|
const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N);
|
|
|
|
if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV)) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting because iterations are not consecutive\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scale = RootSets[0].Roots.size() + 1;
|
|
|
|
|
|
|
|
if (Scale > IL_MaxRerollIterations) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
|
|
|
|
<< "#Found=" << Scale << ", #Max=" << IL_MaxRerollIterations
|
|
|
|
<< "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale << "\n");
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
|
|
|
|
// Populate the MapVector with all instructions in the block, in order first,
|
|
|
|
// so we can iterate over the contents later in perfect order.
|
|
|
|
for (auto &I : *L->getHeader()) {
|
|
|
|
Uses[&I].resize(IL_End);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallInstructionSet Exclude;
|
2015-02-11 09:19:47 +00:00
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
|
|
|
|
Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
|
|
|
|
Exclude.insert(DRS.BaseInst);
|
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
Exclude.insert(LoopIncs.begin(), LoopIncs.end());
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
DenseSet<Instruction*> VBase;
|
|
|
|
collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
|
|
|
|
for (auto *I : VBase) {
|
|
|
|
Uses[I].set(0);
|
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
unsigned Idx = 1;
|
|
|
|
for (auto *Root : DRS.Roots) {
|
|
|
|
DenseSet<Instruction*> V;
|
|
|
|
collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
|
2015-01-29 21:52:03 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// While we're here, check the use sets are the same size.
|
|
|
|
if (V.size() != VBase.size()) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *I : V) {
|
|
|
|
Uses[I].set(Idx);
|
|
|
|
}
|
|
|
|
++Idx;
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// Make sure our subsumed instructions are remembered too.
|
|
|
|
for (auto *I : DRS.SubsumedInsts) {
|
|
|
|
Uses[I].set(IL_All);
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the loop increments are also accounted for.
|
2015-02-11 09:19:47 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
Exclude.clear();
|
2015-02-11 09:19:47 +00:00
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
|
|
|
|
Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
|
|
|
|
Exclude.insert(DRS.BaseInst);
|
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
|
|
|
|
DenseSet<Instruction*> V;
|
|
|
|
collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
|
|
|
|
for (auto *I : V) {
|
2015-02-11 09:19:47 +00:00
|
|
|
Uses[I].set(IL_All);
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 15:54:14 +00:00
|
|
|
/// Get the next instruction in "In" that is a member of set Val.
|
|
|
|
/// Start searching from StartI, and do not return anything in Exclude.
|
|
|
|
/// If StartI is not given, start from In.begin().
|
2015-01-29 21:52:03 +00:00
|
|
|
LoopReroll::DAGRootTracker::UsesTy::iterator
|
|
|
|
LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In,
|
2015-02-12 15:54:14 +00:00
|
|
|
const SmallInstructionSet &Exclude,
|
|
|
|
UsesTy::iterator *StartI) {
|
|
|
|
UsesTy::iterator I = StartI ? *StartI : In.begin();
|
|
|
|
while (I != In.end() && (I->second.test(Val) == 0 ||
|
|
|
|
Exclude.count(I->first) != 0))
|
2015-01-29 21:52:03 +00:00
|
|
|
++I;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) {
|
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
if (DRS.BaseInst == I)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) {
|
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
if (std::find(DRS.Roots.begin(), DRS.Roots.end(), I) != DRS.Roots.end())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:54:14 +00:00
|
|
|
/// Return true if instruction I depends on any instruction between
|
|
|
|
/// Start and End.
|
|
|
|
bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I,
|
|
|
|
UsesTy::iterator Start,
|
|
|
|
UsesTy::iterator End) {
|
|
|
|
for (auto *U : I->users()) {
|
|
|
|
for (auto It = Start; It != End; ++It)
|
|
|
|
if (U == It->first)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
|
2013-11-16 23:59:05 +00:00
|
|
|
// We now need to check for equivalence of the use graph of each root with
|
|
|
|
// that of the primary induction variable (excluding the roots). Our goal
|
|
|
|
// here is not to solve the full graph isomorphism problem, but rather to
|
|
|
|
// catch common cases without a lot of work. As a result, we will assume
|
|
|
|
// that the relative order of the instructions in each unrolled iteration
|
|
|
|
// is the same (although we will not make an assumption about how the
|
|
|
|
// different iterations are intermixed). Note that while the order must be
|
|
|
|
// the same, the instructions may not be in the same basic block.
|
|
|
|
|
2015-01-29 13:48:05 +00:00
|
|
|
// An array of just the possible reductions for this scale factor. When we
|
|
|
|
// collect the set of all users of some root instructions, these reduction
|
|
|
|
// instructions are treated as 'final' (their uses are not considered).
|
|
|
|
// This is important because we don't want the root use set to search down
|
|
|
|
// the reduction chain.
|
|
|
|
SmallInstructionSet PossibleRedSet;
|
|
|
|
SmallInstructionSet PossibleRedLastSet;
|
|
|
|
SmallInstructionSet PossibleRedPHISet;
|
|
|
|
Reductions.restrictToScale(Scale, PossibleRedSet,
|
|
|
|
PossibleRedPHISet, PossibleRedLastSet);
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// Populate "Uses" with where each instruction is used.
|
|
|
|
if (!collectUsedInstructions(PossibleRedSet))
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// Make sure we mark the reduction PHIs as used in all iterations.
|
|
|
|
for (auto *I : PossibleRedPHISet) {
|
2015-02-11 09:19:47 +00:00
|
|
|
Uses[I].set(IL_All);
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// Make sure all instructions in the loop are in one and only one
|
|
|
|
// set.
|
|
|
|
for (auto &KV : Uses) {
|
|
|
|
if (KV.second.count() != 1) {
|
|
|
|
DEBUG(dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
|
|
|
|
<< *KV.first << " (#uses=" << KV.second.count() << ")\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
DEBUG(
|
|
|
|
for (auto &KV : Uses) {
|
|
|
|
dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n";
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
);
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
for (unsigned Iter = 1; Iter < Scale; ++Iter) {
|
2013-11-16 23:59:05 +00:00
|
|
|
// In addition to regular aliasing information, we need to look for
|
|
|
|
// instructions from later (future) iterations that have side effects
|
|
|
|
// preventing us from reordering them past other instructions with side
|
|
|
|
// effects.
|
|
|
|
bool FutureSideEffects = false;
|
|
|
|
AliasSetTracker AST(*AA);
|
|
|
|
// The map between instructions in f(%iv.(i+1)) and f(%iv).
|
|
|
|
DenseMap<Value *, Value *> BaseMap;
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// Compare iteration Iter to the base.
|
2015-02-12 15:54:14 +00:00
|
|
|
SmallInstructionSet Visited;
|
|
|
|
auto BaseIt = nextInstr(0, Uses, Visited);
|
|
|
|
auto RootIt = nextInstr(Iter, Uses, Visited);
|
2015-01-29 21:52:03 +00:00
|
|
|
auto LastRootIt = Uses.begin();
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
while (BaseIt != Uses.end() && RootIt != Uses.end()) {
|
|
|
|
Instruction *BaseInst = BaseIt->first;
|
|
|
|
Instruction *RootInst = RootIt->first;
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// Skip over the IV or root instructions; only match their users.
|
|
|
|
bool Continue = false;
|
2015-02-11 09:19:47 +00:00
|
|
|
if (isBaseInst(BaseInst)) {
|
2015-02-12 15:54:14 +00:00
|
|
|
Visited.insert(BaseInst);
|
|
|
|
BaseIt = nextInstr(0, Uses, Visited);
|
2015-01-29 21:52:03 +00:00
|
|
|
Continue = true;
|
|
|
|
}
|
2015-02-11 09:19:47 +00:00
|
|
|
if (isRootInst(RootInst)) {
|
2015-01-29 21:52:03 +00:00
|
|
|
LastRootIt = RootIt;
|
2015-02-12 15:54:14 +00:00
|
|
|
Visited.insert(RootInst);
|
|
|
|
RootIt = nextInstr(Iter, Uses, Visited);
|
2015-01-29 21:52:03 +00:00
|
|
|
Continue = true;
|
|
|
|
}
|
|
|
|
if (Continue) continue;
|
|
|
|
|
2015-02-12 15:54:14 +00:00
|
|
|
if (!BaseInst->isSameOperationAs(RootInst)) {
|
|
|
|
// Last chance saloon. We don't try and solve the full isomorphism
|
|
|
|
// problem, but try and at least catch the case where two instructions
|
|
|
|
// *of different types* are round the wrong way. We won't be able to
|
|
|
|
// efficiently tell, given two ADD instructions, which way around we
|
|
|
|
// should match them, but given an ADD and a SUB, we can at least infer
|
|
|
|
// which one is which.
|
|
|
|
//
|
|
|
|
// This should allow us to deal with a greater subset of the isomorphism
|
|
|
|
// problem. It does however change a linear algorithm into a quadratic
|
|
|
|
// one, so limit the number of probes we do.
|
|
|
|
auto TryIt = RootIt;
|
|
|
|
unsigned N = NumToleratedFailedMatches;
|
|
|
|
while (TryIt != Uses.end() &&
|
|
|
|
!BaseInst->isSameOperationAs(TryIt->first) &&
|
|
|
|
N--) {
|
|
|
|
++TryIt;
|
|
|
|
TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TryIt == Uses.end() || TryIt == RootIt ||
|
|
|
|
instrDependsOn(TryIt->first, RootIt, TryIt)) {
|
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
|
|
|
|
" vs. " << *RootInst << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RootIt = TryIt;
|
|
|
|
RootInst = TryIt->first;
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
// All instructions between the last root and this root
|
2015-02-12 15:54:14 +00:00
|
|
|
// may belong to some other iteration. If they belong to a
|
2015-01-29 21:52:03 +00:00
|
|
|
// future iteration, then they're dangerous to alias with.
|
2015-02-12 15:54:14 +00:00
|
|
|
//
|
|
|
|
// Note that because we allow a limited amount of flexibility in the order
|
|
|
|
// that we visit nodes, LastRootIt might be *before* RootIt, in which
|
|
|
|
// case we've already checked this set of instructions so we shouldn't
|
|
|
|
// do anything.
|
|
|
|
for (; LastRootIt < RootIt; ++LastRootIt) {
|
2015-01-29 21:52:03 +00:00
|
|
|
Instruction *I = LastRootIt->first;
|
|
|
|
if (LastRootIt->second.find_first() < (int)Iter)
|
|
|
|
continue;
|
|
|
|
if (I->mayWriteToMemory())
|
|
|
|
AST.add(I);
|
|
|
|
// Note: This is specifically guarded by a check on isa<PHINode>,
|
|
|
|
// which while a valid (somewhat arbitrary) micro-optimization, is
|
|
|
|
// needed because otherwise isSafeToSpeculativelyExecute returns
|
|
|
|
// false on PHI nodes.
|
|
|
|
if (!isa<PHINode>(I) && !isSimpleLoadStore(I) &&
|
2015-03-10 02:37:25 +00:00
|
|
|
!isSafeToSpeculativelyExecute(I))
|
2015-01-29 21:52:03 +00:00
|
|
|
// Intervening instructions cause side effects.
|
|
|
|
FutureSideEffects = true;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that this instruction, which is in the use set of this
|
|
|
|
// root instruction, does not also belong to the base set or the set of
|
2015-01-29 21:52:03 +00:00
|
|
|
// some other root instruction.
|
|
|
|
if (RootIt->second.count() > 1) {
|
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
|
|
|
|
" vs. " << *RootInst << " (prev. case overlap)\n");
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we don't alias with any instruction in the alias set
|
|
|
|
// tracker. If we do, then we depend on a future iteration, and we
|
|
|
|
// can't reroll.
|
2015-01-29 21:52:03 +00:00
|
|
|
if (RootInst->mayReadFromMemory())
|
|
|
|
for (auto &K : AST) {
|
|
|
|
if (K.aliasesUnknownInst(RootInst, *AA)) {
|
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
|
|
|
|
" vs. " << *RootInst << " (depends on future store)\n");
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've past an instruction from a future iteration that may have
|
|
|
|
// side effects, and this instruction might also, then we can't reorder
|
|
|
|
// them, and this matching fails. As an exception, we allow the alias
|
|
|
|
// set tracker to handle regular (simple) load/store dependencies.
|
2015-03-10 02:37:25 +00:00
|
|
|
if (FutureSideEffects && ((!isSimpleLoadStore(BaseInst) &&
|
|
|
|
!isSafeToSpeculativelyExecute(BaseInst)) ||
|
|
|
|
(!isSimpleLoadStore(RootInst) &&
|
|
|
|
!isSafeToSpeculativelyExecute(RootInst)))) {
|
2015-01-29 21:52:03 +00:00
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
|
|
|
|
" vs. " << *RootInst <<
|
2013-11-16 23:59:05 +00:00
|
|
|
" (side effects prevent reordering)\n");
|
2015-01-29 21:52:03 +00:00
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For instructions that are part of a reduction, if the operation is
|
|
|
|
// associative, then don't bother matching the operands (because we
|
|
|
|
// already know that the instructions are isomorphic, and the order
|
|
|
|
// within the iteration does not matter). For non-associative reductions,
|
|
|
|
// we do need to match the operands, because we need to reject
|
|
|
|
// out-of-order instructions within an iteration!
|
|
|
|
// For example (assume floating-point addition), we need to reject this:
|
|
|
|
// x += a[i]; x += b[i];
|
|
|
|
// x += a[i+1]; x += b[i+1];
|
|
|
|
// x += b[i+2]; x += a[i+2];
|
2015-01-29 21:52:03 +00:00
|
|
|
bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
if (!(InReduction && BaseInst->isAssociative())) {
|
2014-04-19 23:56:35 +00:00
|
|
|
bool Swapped = false, SomeOpMatched = false;
|
2015-01-29 21:52:03 +00:00
|
|
|
for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) {
|
|
|
|
Value *Op2 = RootInst->getOperand(j);
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2014-10-28 11:53:30 +00:00
|
|
|
// If this is part of a reduction (and the operation is not
|
|
|
|
// associatve), then we match all operands, but not those that are
|
|
|
|
// part of the reduction.
|
2013-11-16 23:59:05 +00:00
|
|
|
if (InReduction)
|
|
|
|
if (Instruction *Op2I = dyn_cast<Instruction>(Op2))
|
2015-01-29 21:52:03 +00:00
|
|
|
if (Reductions.isPairInSame(RootInst, Op2I))
|
2013-11-16 23:59:05 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2);
|
2015-02-11 09:19:47 +00:00
|
|
|
if (BMI != BaseMap.end()) {
|
2013-11-16 23:59:05 +00:00
|
|
|
Op2 = BMI->second;
|
2015-02-11 09:19:47 +00:00
|
|
|
} else {
|
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
if (DRS.Roots[Iter-1] == (Instruction*) Op2) {
|
|
|
|
Op2 = DRS.BaseInst;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) {
|
2014-10-28 11:53:30 +00:00
|
|
|
// If we've not already decided to swap the matched operands, and
|
|
|
|
// we've not already matched our first operand (note that we could
|
|
|
|
// have skipped matching the first operand because it is part of a
|
|
|
|
// reduction above), and the instruction is commutative, then try
|
|
|
|
// the swapped match.
|
2015-01-29 21:52:03 +00:00
|
|
|
if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched &&
|
|
|
|
BaseInst->getOperand(!j) == Op2) {
|
2013-11-16 23:59:05 +00:00
|
|
|
Swapped = true;
|
|
|
|
} else {
|
2015-01-29 21:52:03 +00:00
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
|
|
|
|
<< " vs. " << *RootInst << " (operand " << j << ")\n");
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SomeOpMatched = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
if ((!PossibleRedLastSet.count(BaseInst) &&
|
|
|
|
hasUsesOutsideLoop(BaseInst, L)) ||
|
|
|
|
(!PossibleRedLastSet.count(RootInst) &&
|
|
|
|
hasUsesOutsideLoop(RootInst, L))) {
|
|
|
|
DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
|
|
|
|
" vs. " << *RootInst << " (uses outside loop)\n");
|
|
|
|
return false;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
Reductions.recordPair(BaseInst, RootInst, Iter);
|
|
|
|
BaseMap.insert(std::make_pair(RootInst, BaseInst));
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 21:52:03 +00:00
|
|
|
LastRootIt = RootIt;
|
2015-02-12 15:54:14 +00:00
|
|
|
Visited.insert(BaseInst);
|
|
|
|
Visited.insert(RootInst);
|
|
|
|
BaseIt = nextInstr(0, Uses, Visited);
|
|
|
|
RootIt = nextInstr(Iter, Uses, Visited);
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
2015-01-29 21:52:03 +00:00
|
|
|
assert (BaseIt == Uses.end() && RootIt == Uses.end() &&
|
|
|
|
"Mismatched set sizes!");
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "LRR: Matched all iteration increments for " <<
|
2015-02-11 09:19:47 +00:00
|
|
|
*IV << "\n");
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 13:48:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-01-29 13:48:05 +00:00
|
|
|
void LoopReroll::DAGRootTracker::replace(const SCEV *IterCount) {
|
|
|
|
BasicBlock *Header = L->getHeader();
|
2013-11-16 23:59:05 +00:00
|
|
|
// Remove instructions associated with non-base iterations.
|
|
|
|
for (BasicBlock::reverse_iterator J = Header->rbegin();
|
|
|
|
J != Header->rend();) {
|
2015-01-29 21:52:03 +00:00
|
|
|
unsigned I = Uses[&*J].find_first();
|
2015-02-11 09:19:47 +00:00
|
|
|
if (I > 0 && I < IL_All) {
|
2013-11-16 23:59:05 +00:00
|
|
|
Instruction *D = &*J;
|
|
|
|
DEBUG(dbgs() << "LRR: removing: " << *D << "\n");
|
|
|
|
D->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-28 11:53:30 +00:00
|
|
|
++J;
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
2015-03-10 02:37:25 +00:00
|
|
|
const DataLayout &DL = Header->getModule()->getDataLayout();
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
// We need to create a new induction variable for each different BaseInst.
|
|
|
|
for (auto &DRS : RootSets) {
|
|
|
|
// Insert the new induction variable.
|
|
|
|
const SCEVAddRecExpr *RealIVSCEV =
|
|
|
|
cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
|
|
|
|
const SCEV *Start = RealIVSCEV->getStart();
|
|
|
|
const SCEVAddRecExpr *H = cast<SCEVAddRecExpr>
|
|
|
|
(SE->getAddRecExpr(Start,
|
|
|
|
SE->getConstant(RealIVSCEV->getType(), 1),
|
|
|
|
L, SCEV::FlagAnyWrap));
|
|
|
|
{ // Limit the lifetime of SCEVExpander.
|
2015-03-10 02:37:25 +00:00
|
|
|
SCEVExpander Expander(*SE, DL, "reroll");
|
2015-02-11 09:19:47 +00:00
|
|
|
Value *NewIV = Expander.expandCodeFor(H, IV->getType(), Header->begin());
|
|
|
|
|
|
|
|
for (auto &KV : Uses) {
|
|
|
|
if (KV.second.find_first() == 0)
|
|
|
|
KV.first->replaceUsesOfWith(DRS.BaseInst, NewIV);
|
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) {
|
|
|
|
// FIXME: Why do we need this check?
|
|
|
|
if (Uses[BI].find_first() == IL_All) {
|
|
|
|
const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE);
|
|
|
|
|
|
|
|
// Iteration count SCEV minus 1
|
|
|
|
const SCEV *ICMinus1SCEV =
|
|
|
|
SE->getMinusSCEV(ICSCEV, SE->getConstant(ICSCEV->getType(), 1));
|
|
|
|
|
|
|
|
Value *ICMinus1; // Iteration count minus 1
|
|
|
|
if (isa<SCEVConstant>(ICMinus1SCEV)) {
|
|
|
|
ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(), BI);
|
|
|
|
} else {
|
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
Preheader = InsertPreheaderForLoop(L, Parent);
|
|
|
|
|
|
|
|
ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(),
|
|
|
|
Preheader->getTerminator());
|
|
|
|
}
|
2014-10-28 11:53:30 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
Value *Cond =
|
2014-10-28 11:54:52 +00:00
|
|
|
new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinus1, "exitcond");
|
2015-02-11 09:19:47 +00:00
|
|
|
BI->setCondition(Cond);
|
2013-11-16 23:59:05 +00:00
|
|
|
|
2015-02-11 09:19:47 +00:00
|
|
|
if (BI->getSuccessor(1) != Header)
|
|
|
|
BI->swapSuccessors();
|
|
|
|
}
|
2013-11-16 23:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 02:37:25 +00:00
|
|
|
SimplifyInstructionsInBlock(Header, TLI);
|
2013-11-16 23:59:05 +00:00
|
|
|
DeleteDeadPHIs(Header, TLI);
|
2015-01-29 13:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the selected reductions. All iterations must have an isomorphic
|
|
|
|
// part of the reduction chain and, for non-associative reductions, the chain
|
|
|
|
// entries must appear in order.
|
|
|
|
bool LoopReroll::ReductionTracker::validateSelected() {
|
|
|
|
// For a non-associative reduction, the chain entries must appear in order.
|
|
|
|
for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
|
|
|
|
RI != RIE; ++RI) {
|
|
|
|
int i = *RI;
|
|
|
|
int PrevIter = 0, BaseCount = 0, Count = 0;
|
|
|
|
for (Instruction *J : PossibleReds[i]) {
|
|
|
|
// Note that all instructions in the chain must have been found because
|
|
|
|
// all instructions in the function must have been assigned to some
|
|
|
|
// iteration.
|
|
|
|
int Iter = PossibleRedIter[J];
|
|
|
|
if (Iter != PrevIter && Iter != PrevIter + 1 &&
|
|
|
|
!PossibleReds[i].getReducedValue()->isAssociative()) {
|
|
|
|
DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: " <<
|
|
|
|
J << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Iter != PrevIter) {
|
|
|
|
if (Count != BaseCount) {
|
|
|
|
DEBUG(dbgs() << "LRR: Iteration " << PrevIter <<
|
|
|
|
" reduction use count " << Count <<
|
|
|
|
" is not equal to the base use count " <<
|
|
|
|
BaseCount << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
++Count;
|
|
|
|
if (Iter == 0)
|
|
|
|
++BaseCount;
|
|
|
|
|
|
|
|
PrevIter = Iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all selected reductions, remove all parts except those in the first
|
|
|
|
// iteration (and the PHI). Replace outside uses of the reduced value with uses
|
|
|
|
// of the first-iteration reduced value (in other words, reroll the selected
|
|
|
|
// reductions).
|
|
|
|
void LoopReroll::ReductionTracker::replaceSelected() {
|
|
|
|
// Fixup reductions to refer to the last instruction associated with the
|
|
|
|
// first iteration (not the last).
|
|
|
|
for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
|
|
|
|
RI != RIE; ++RI) {
|
|
|
|
int i = *RI;
|
|
|
|
int j = 0;
|
|
|
|
for (int e = PossibleReds[i].size(); j != e; ++j)
|
|
|
|
if (PossibleRedIter[PossibleReds[i][j]] != 0) {
|
|
|
|
--j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace users with the new end-of-chain value.
|
|
|
|
SmallInstructionVector Users;
|
2015-01-29 21:52:03 +00:00
|
|
|
for (User *U : PossibleReds[i].getReducedValue()->users()) {
|
2015-01-29 13:48:05 +00:00
|
|
|
Users.push_back(cast<Instruction>(U));
|
2015-01-29 21:52:03 +00:00
|
|
|
}
|
2015-01-29 13:48:05 +00:00
|
|
|
|
|
|
|
for (SmallInstructionVector::iterator J = Users.begin(),
|
|
|
|
JE = Users.end(); J != JE; ++J)
|
|
|
|
(*J)->replaceUsesOfWith(PossibleReds[i].getReducedValue(),
|
|
|
|
PossibleReds[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reroll the provided loop with respect to the provided induction variable.
|
|
|
|
// Generally, we're looking for a loop like this:
|
|
|
|
//
|
|
|
|
// %iv = phi [ (preheader, ...), (body, %iv.next) ]
|
|
|
|
// f(%iv)
|
|
|
|
// %iv.1 = add %iv, 1 <-- a root increment
|
|
|
|
// f(%iv.1)
|
|
|
|
// %iv.2 = add %iv, 2 <-- a root increment
|
|
|
|
// f(%iv.2)
|
|
|
|
// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
|
|
|
|
// f(%iv.scale_m_1)
|
|
|
|
// ...
|
|
|
|
// %iv.next = add %iv, scale
|
|
|
|
// %cmp = icmp(%iv, ...)
|
|
|
|
// br %cmp, header, exit
|
|
|
|
//
|
|
|
|
// Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of
|
|
|
|
// instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can
|
|
|
|
// be intermixed with eachother. The restriction imposed by this algorithm is
|
|
|
|
// that the relative order of the isomorphic instructions in f(%iv), f(%iv.1),
|
|
|
|
// etc. be the same.
|
|
|
|
//
|
|
|
|
// First, we collect the use set of %iv, excluding the other increment roots.
|
|
|
|
// This gives us f(%iv). Then we iterate over the loop instructions (scale-1)
|
|
|
|
// times, having collected the use set of f(%iv.(i+1)), during which we:
|
|
|
|
// - Ensure that the next unmatched instruction in f(%iv) is isomorphic to
|
|
|
|
// the next unmatched instruction in f(%iv.(i+1)).
|
|
|
|
// - Ensure that both matched instructions don't have any external users
|
|
|
|
// (with the exception of last-in-chain reduction instructions).
|
|
|
|
// - Track the (aliasing) write set, and other side effects, of all
|
|
|
|
// instructions that belong to future iterations that come before the matched
|
|
|
|
// instructions. If the matched instructions read from that write set, then
|
|
|
|
// f(%iv) or f(%iv.(i+1)) has some dependency on instructions in
|
|
|
|
// f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly,
|
|
|
|
// if any of these future instructions had side effects (could not be
|
|
|
|
// speculatively executed), and so do the matched instructions, when we
|
|
|
|
// cannot reorder those side-effect-producing instructions, and rerolling
|
|
|
|
// fails.
|
|
|
|
//
|
|
|
|
// Finally, we make sure that all loop instructions are either loop increment
|
|
|
|
// roots, belong to simple latch code, parts of validated reductions, part of
|
|
|
|
// f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions
|
|
|
|
// have been validated), then we reroll the loop.
|
|
|
|
bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header,
|
|
|
|
const SCEV *IterCount,
|
|
|
|
ReductionTracker &Reductions) {
|
2015-03-10 02:37:25 +00:00
|
|
|
DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI);
|
2015-01-29 13:48:05 +00:00
|
|
|
|
|
|
|
if (!DAGRoots.findRoots())
|
|
|
|
return false;
|
|
|
|
DEBUG(dbgs() << "LRR: Found all root induction increments for: " <<
|
|
|
|
*IV << "\n");
|
|
|
|
|
|
|
|
if (!DAGRoots.validate(Reductions))
|
|
|
|
return false;
|
|
|
|
if (!Reductions.validateSelected())
|
|
|
|
return false;
|
|
|
|
// At this point, we've validated the rerolling, and we're committed to
|
|
|
|
// making changes!
|
|
|
|
|
|
|
|
Reductions.replaceSelected();
|
|
|
|
DAGRoots.replace(IterCount);
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
++NumRerolledLoops;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) {
|
2014-02-06 00:07:05 +00:00
|
|
|
if (skipOptnoneFunction(L))
|
|
|
|
return false;
|
|
|
|
|
2013-11-16 23:59:05 +00:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
2015-01-17 14:16:18 +00:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2013-11-16 23:59:05 +00:00
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2015-01-15 10:41:28 +00:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2014-01-13 13:07:17 +00:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2013-11-16 23:59:05 +00:00
|
|
|
|
|
|
|
BasicBlock *Header = L->getHeader();
|
|
|
|
DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() <<
|
|
|
|
"] Loop %" << Header->getName() << " (" <<
|
|
|
|
L->getNumBlocks() << " block(s))\n");
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// For now, we'll handle only single BB loops.
|
|
|
|
if (L->getNumBlocks() > 1)
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
if (!SE->hasLoopInvariantBackedgeTakenCount(L))
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
const SCEV *LIBETC = SE->getBackedgeTakenCount(L);
|
|
|
|
const SCEV *IterCount =
|
|
|
|
SE->getAddExpr(LIBETC, SE->getConstant(LIBETC->getType(), 1));
|
|
|
|
DEBUG(dbgs() << "LRR: iteration count = " << *IterCount << "\n");
|
|
|
|
|
|
|
|
// First, we need to find the induction variable with respect to which we can
|
|
|
|
// reroll (there may be several possible options).
|
|
|
|
SmallInstructionVector PossibleIVs;
|
|
|
|
collectPossibleIVs(L, PossibleIVs);
|
|
|
|
|
|
|
|
if (PossibleIVs.empty()) {
|
|
|
|
DEBUG(dbgs() << "LRR: No possible IVs found\n");
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReductionTracker Reductions;
|
|
|
|
collectPossibleReductions(L, Reductions);
|
|
|
|
|
|
|
|
// For each possible IV, collect the associated possible set of 'root' nodes
|
|
|
|
// (i+1, i+2, etc.).
|
|
|
|
for (SmallInstructionVector::iterator I = PossibleIVs.begin(),
|
|
|
|
IE = PossibleIVs.end(); I != IE; ++I)
|
|
|
|
if (reroll(*I, L, Header, IterCount, Reductions)) {
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|