InductiveRangeCheckElimination.cpp [plain text]
#include "llvm/ADT/Optional.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.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/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include "llvm/Pass.h"
#include <array>
using namespace llvm;
static cl::opt<unsigned> LoopSizeCutoff("irce-loop-size-cutoff", cl::Hidden,
cl::init(64));
static cl::opt<bool> PrintChangedLoops("irce-print-changed-loops", cl::Hidden,
cl::init(false));
#define DEBUG_TYPE "irce"
namespace {
class InductiveRangeCheck {
const SCEV *Offset;
const SCEV *Scale;
Value *Length;
BranchInst *Branch;
InductiveRangeCheck() :
Offset(nullptr), Scale(nullptr), Length(nullptr), Branch(nullptr) { }
public:
const SCEV *getOffset() const { return Offset; }
const SCEV *getScale() const { return Scale; }
Value *getLength() const { return Length; }
void print(raw_ostream &OS) const {
OS << "InductiveRangeCheck:\n";
OS << " Offset: ";
Offset->print(OS);
OS << " Scale: ";
Scale->print(OS);
OS << " Length: ";
Length->print(OS);
OS << " Branch: ";
getBranch()->print(OS);
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void dump() {
print(dbgs());
}
#endif
BranchInst *getBranch() const { return Branch; }
class Range {
Value *Begin;
Value *End;
public:
Range(Value *Begin, Value *End) : Begin(Begin), End(End) {
assert(Begin->getType() == End->getType() && "ill-typed range!");
}
Type *getType() const { return Begin->getType(); }
Value *getBegin() const { return Begin; }
Value *getEnd() const { return End; }
};
typedef SpecificBumpPtrAllocator<InductiveRangeCheck> AllocatorTy;
bool getPassingDirection() { return true; }
Optional<Range> computeSafeIterationSpace(ScalarEvolution &SE,
IRBuilder<> &B) const;
static InductiveRangeCheck *create(AllocatorTy &Alloc, BranchInst *BI,
Loop *L, ScalarEvolution &SE,
BranchProbabilityInfo &BPI);
};
class InductiveRangeCheckElimination : public LoopPass {
InductiveRangeCheck::AllocatorTy Allocator;
public:
static char ID;
InductiveRangeCheckElimination() : LoopPass(ID) {
initializeInductiveRangeCheckEliminationPass(
*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequiredID(LoopSimplifyID);
AU.addRequiredID(LCSSAID);
AU.addRequired<ScalarEvolution>();
AU.addRequired<BranchProbabilityInfo>();
}
bool runOnLoop(Loop *L, LPPassManager &LPM) override;
};
char InductiveRangeCheckElimination::ID = 0;
}
INITIALIZE_PASS(InductiveRangeCheckElimination, "irce",
"Inductive range check elimination", false, false)
static bool IsLowerBoundCheck(Value *Check, Value *&IndexV) {
using namespace llvm::PatternMatch;
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
Value *LHS = nullptr, *RHS = nullptr;
if (!match(Check, m_ICmp(Pred, m_Value(LHS), m_Value(RHS))))
return false;
switch (Pred) {
default:
return false;
case ICmpInst::ICMP_SLE:
std::swap(LHS, RHS);
case ICmpInst::ICMP_SGE:
if (!match(RHS, m_ConstantInt<0>()))
return false;
IndexV = LHS;
return true;
case ICmpInst::ICMP_SLT:
std::swap(LHS, RHS);
case ICmpInst::ICMP_SGT:
if (!match(RHS, m_ConstantInt<-1>()))
return false;
IndexV = LHS;
return true;
}
}
static bool IsUpperBoundCheck(Value *Check, Value *Index, Value *&UpperLimit) {
using namespace llvm::PatternMatch;
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
Value *LHS = nullptr, *RHS = nullptr;
if (!match(Check, m_ICmp(Pred, m_Value(LHS), m_Value(RHS))))
return false;
switch (Pred) {
default:
return false;
case ICmpInst::ICMP_SGT:
std::swap(LHS, RHS);
case ICmpInst::ICMP_SLT:
if (LHS != Index)
return false;
UpperLimit = RHS;
return true;
case ICmpInst::ICMP_UGT:
std::swap(LHS, RHS);
case ICmpInst::ICMP_ULT:
if (LHS != Index)
return false;
UpperLimit = RHS;
return true;
}
}
static bool SplitRangeCheckCondition(Loop *L, ScalarEvolution &SE,
Value *Condition, const SCEV *&Index,
Value *&UpperLimit) {
using namespace llvm::PatternMatch;
Value *A = nullptr;
Value *B = nullptr;
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
if (match(Condition, m_And(m_Value(A), m_Value(B)))) {
Value *IndexV = nullptr;
Value *ExpectedUpperBoundCheck = nullptr;
if (IsLowerBoundCheck(A, IndexV))
ExpectedUpperBoundCheck = B;
else if (IsLowerBoundCheck(B, IndexV))
ExpectedUpperBoundCheck = A;
else
return false;
if (!IsUpperBoundCheck(ExpectedUpperBoundCheck, IndexV, UpperLimit))
return false;
Index = SE.getSCEV(IndexV);
if (isa<SCEVCouldNotCompute>(Index))
return false;
} else if (match(Condition, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
switch (Pred) {
default:
return false;
case ICmpInst::ICMP_SGT:
std::swap(A, B);
case ICmpInst::ICMP_SLT:
UpperLimit = B;
Index = SE.getSCEV(A);
if (isa<SCEVCouldNotCompute>(Index) || !SE.isKnownNonNegative(Index))
return false;
break;
case ICmpInst::ICMP_UGT:
std::swap(A, B);
case ICmpInst::ICMP_ULT:
UpperLimit = B;
Index = SE.getSCEV(A);
if (isa<SCEVCouldNotCompute>(Index))
return false;
break;
}
} else {
return false;
}
const SCEV *UpperLimitSCEV = SE.getSCEV(UpperLimit);
if (isa<SCEVCouldNotCompute>(UpperLimitSCEV) ||
!SE.isKnownNonNegative(UpperLimitSCEV))
return false;
if (SE.getLoopDisposition(UpperLimitSCEV, L) !=
ScalarEvolution::LoopInvariant) {
DEBUG(dbgs() << " in function: " << L->getHeader()->getParent()->getName()
<< " ";
dbgs() << " UpperLimit is not loop invariant: "
<< UpperLimit->getName() << "\n";);
return false;
}
return true;
}
InductiveRangeCheck *
InductiveRangeCheck::create(InductiveRangeCheck::AllocatorTy &A, BranchInst *BI,
Loop *L, ScalarEvolution &SE,
BranchProbabilityInfo &BPI) {
if (BI->isUnconditional() || BI->getParent() == L->getLoopLatch())
return nullptr;
BranchProbability LikelyTaken(15, 16);
if (BPI.getEdgeProbability(BI->getParent(), (unsigned) 0) < LikelyTaken)
return nullptr;
Value *Length = nullptr;
const SCEV *IndexSCEV = nullptr;
if (!SplitRangeCheckCondition(L, SE, BI->getCondition(), IndexSCEV, Length))
return nullptr;
assert(IndexSCEV && Length && "contract with SplitRangeCheckCondition!");
const SCEVAddRecExpr *IndexAddRec = dyn_cast<SCEVAddRecExpr>(IndexSCEV);
bool IsAffineIndex =
IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
if (!IsAffineIndex)
return nullptr;
InductiveRangeCheck *IRC = new (A.Allocate()) InductiveRangeCheck;
IRC->Length = Length;
IRC->Offset = IndexAddRec->getStart();
IRC->Scale = IndexAddRec->getStepRecurrence(SE);
IRC->Branch = BI;
return IRC;
}
static Value *MaybeSimplify(Value *V) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (Value *Simplified = SimplifyInstruction(
I, I->getParent()->getParent()->getParent()->getDataLayout()))
return Simplified;
return V;
}
static Value *ConstructSMinOf(Value *X, Value *Y, IRBuilder<> &B) {
return MaybeSimplify(B.CreateSelect(B.CreateICmpSLT(X, Y), X, Y));
}
static Value *ConstructSMaxOf(Value *X, Value *Y, IRBuilder<> &B) {
return MaybeSimplify(B.CreateSelect(B.CreateICmpSGT(X, Y), X, Y));
}
namespace {
class LoopConstrainer {
struct LoopStructure {
const char *Tag;
BasicBlock *Header;
BasicBlock *Latch;
BranchInst *LatchBr;
BasicBlock *LatchExit;
unsigned LatchBrExitIdx;
PHINode *CIV;
Value *CIVStart;
Value *CIVNext;
LoopStructure() : Tag(""), Header(nullptr), Latch(nullptr),
LatchBr(nullptr), LatchExit(nullptr),
LatchBrExitIdx(-1), CIV(nullptr),
CIVStart(nullptr), CIVNext(nullptr) { }
template <typename M> LoopStructure map(M Map) const {
LoopStructure Result;
Result.Tag = Tag;
Result.Header = cast<BasicBlock>(Map(Header));
Result.Latch = cast<BasicBlock>(Map(Latch));
Result.LatchBr = cast<BranchInst>(Map(LatchBr));
Result.LatchExit = cast<BasicBlock>(Map(LatchExit));
Result.LatchBrExitIdx = LatchBrExitIdx;
Result.CIV = cast<PHINode>(Map(CIV));
Result.CIVNext = Map(CIVNext);
Result.CIVStart = Map(CIVStart);
return Result;
}
};
struct ClonedLoop {
std::vector<BasicBlock *> Blocks;
ValueToValueMapTy Map;
LoopStructure Structure;
};
struct RewrittenRangeInfo {
BasicBlock *PseudoExit;
BasicBlock *ExitSelector;
std::vector<PHINode *> PHIValuesAtPseudoExit;
RewrittenRangeInfo() : PseudoExit(nullptr), ExitSelector(nullptr) { }
};
struct SubRanges {
Optional<Value *> ExitPreLoopAt;
Optional<Value *> ExitMainLoopAt;
};
static void replacePHIBlock(PHINode *PN, BasicBlock *Block,
BasicBlock *ReplaceBy);
bool recognizeLoop(LoopStructure &LoopStructureOut,
const SCEV *&LatchCountOut, BasicBlock *&PreHeaderOut,
const char *&FailureReasonOut) const;
Optional<SubRanges> calculateSubRanges(Value *&HeaderCount) const;
void cloneLoop(ClonedLoop &CLResult, const char *Tag) const;
RewrittenRangeInfo
changeIterationSpaceEnd(const LoopStructure &LS, BasicBlock *Preheader,
Value *ExitLoopAt,
BasicBlock *ContinuationBlock) const;
BasicBlock *createPreheader(const LoopConstrainer::LoopStructure &LS,
BasicBlock *OldPreheader, const char *Tag) const;
void rewriteIncomingValuesForPHIs(
LoopConstrainer::LoopStructure &LS,
BasicBlock *ContinuationBlockAndPreheader,
const LoopConstrainer::RewrittenRangeInfo &RRI) const;
void addToParentLoopIfNeeded(ArrayRef<BasicBlock *> BBs);
Function &F;
LLVMContext &Ctx;
ScalarEvolution &SE;
Loop &OriginalLoop;
LoopInfo &OriginalLoopInfo;
const SCEV *LatchTakenCount;
BasicBlock *OriginalPreheader;
Value *OriginalHeaderCount;
BasicBlock *MainLoopPreheader;
InductiveRangeCheck::Range Range;
LoopStructure MainLoopStructure;
public:
LoopConstrainer(Loop &L, LoopInfo &LI, ScalarEvolution &SE,
InductiveRangeCheck::Range R)
: F(*L.getHeader()->getParent()), Ctx(L.getHeader()->getContext()), SE(SE),
OriginalLoop(L), OriginalLoopInfo(LI), LatchTakenCount(nullptr),
OriginalPreheader(nullptr), OriginalHeaderCount(nullptr),
MainLoopPreheader(nullptr), Range(R) { }
bool run();
};
}
void LoopConstrainer::replacePHIBlock(PHINode *PN, BasicBlock *Block,
BasicBlock *ReplaceBy) {
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
if (PN->getIncomingBlock(i) == Block)
PN->setIncomingBlock(i, ReplaceBy);
}
bool LoopConstrainer::recognizeLoop(LoopStructure &LoopStructureOut,
const SCEV *&LatchCountOut,
BasicBlock *&PreheaderOut,
const char *&FailureReason) const {
using namespace llvm::PatternMatch;
assert(OriginalLoop.isLoopSimplifyForm() &&
"should follow from addRequired<>");
BasicBlock *Latch = OriginalLoop.getLoopLatch();
if (!OriginalLoop.isLoopExiting(Latch)) {
FailureReason = "no loop latch";
return false;
}
PHINode *CIV = OriginalLoop.getCanonicalInductionVariable();
if (!CIV) {
FailureReason = "no CIV";
return false;
}
BasicBlock *Header = OriginalLoop.getHeader();
BasicBlock *Preheader = OriginalLoop.getLoopPreheader();
if (!Preheader) {
FailureReason = "no preheader";
return false;
}
Value *CIVNext = CIV->getIncomingValueForBlock(Latch);
Value *CIVStart = CIV->getIncomingValueForBlock(Preheader);
const SCEV *LatchCount = SE.getExitCount(&OriginalLoop, Latch);
if (isa<SCEVCouldNotCompute>(LatchCount)) {
FailureReason = "could not compute latch count";
return false;
}
BranchInst *LatchBr = dyn_cast<BranchInst>(&*Latch->rbegin());
if (!LatchBr || LatchBr->isUnconditional()) {
FailureReason = "latch terminator not conditional branch";
return false;
}
if (LatchBr->getSuccessor(0) != Header) {
FailureReason = "unknown latch form (header not first successor)";
return false;
}
Value *CIVComparedTo = nullptr;
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
if (!(match(LatchBr->getCondition(),
m_ICmp(Pred, m_Specific(CIVNext), m_Value(CIVComparedTo))) &&
Pred == ICmpInst::ICMP_SLT)) {
FailureReason = "unknown latch form (not slt)";
return false;
}
const SCEV *CIVComparedToSCEV = SE.getSCEV(CIVComparedTo);
if (isa<SCEVCouldNotCompute>(CIVComparedToSCEV) ||
CIVComparedToSCEV->getType() != LatchCount->getType()) {
FailureReason = "could not relate CIV to latch expression";
return false;
}
const SCEV *ShouldBeOne = SE.getMinusSCEV(CIVComparedToSCEV, LatchCount);
const SCEVConstant *SCEVOne = dyn_cast<SCEVConstant>(ShouldBeOne);
if (!SCEVOne || SCEVOne->getValue()->getValue() != 1) {
FailureReason = "unexpected header count in latch";
return false;
}
unsigned LatchBrExitIdx = 1;
BasicBlock *LatchExit = LatchBr->getSuccessor(LatchBrExitIdx);
assert(SE.getLoopDisposition(LatchCount, &OriginalLoop) ==
ScalarEvolution::LoopInvariant &&
"loop variant exit count doesn't make sense!");
assert(!OriginalLoop.contains(LatchExit) && "expected an exit block!");
LoopStructureOut.Tag = "main";
LoopStructureOut.Header = Header;
LoopStructureOut.Latch = Latch;
LoopStructureOut.LatchBr = LatchBr;
LoopStructureOut.LatchExit = LatchExit;
LoopStructureOut.LatchBrExitIdx = LatchBrExitIdx;
LoopStructureOut.CIV = CIV;
LoopStructureOut.CIVNext = CIVNext;
LoopStructureOut.CIVStart = CIVStart;
LatchCountOut = LatchCount;
PreheaderOut = Preheader;
FailureReason = nullptr;
return true;
}
Optional<LoopConstrainer::SubRanges>
LoopConstrainer::calculateSubRanges(Value *&HeaderCountOut) const {
IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());
if (Range.getType() != Ty)
return None;
SCEVExpander Expander(SE, F.getParent()->getDataLayout(), "irce");
Instruction *InsertPt = OriginalPreheader->getTerminator();
Value *LatchCountV =
MaybeSimplify(Expander.expandCodeFor(LatchTakenCount, Ty, InsertPt));
IRBuilder<> B(InsertPt);
LoopConstrainer::SubRanges Result;
ConstantInt *One = ConstantInt::get(Ty, 1);
HeaderCountOut = MaybeSimplify(B.CreateAdd(LatchCountV, One, "header.count"));
const SCEV *RangeBegin = SE.getSCEV(Range.getBegin());
const SCEV *RangeEnd = SE.getSCEV(Range.getEnd());
const SCEV *HeaderCountSCEV = SE.getSCEV(HeaderCountOut);
const SCEV *Zero = SE.getConstant(Ty, 0);
bool ProvablyNoPreloop =
SE.isKnownPredicate(ICmpInst::ICMP_SLE, RangeBegin, Zero);
if (!ProvablyNoPreloop)
Result.ExitPreLoopAt = ConstructSMinOf(HeaderCountOut, Range.getBegin(), B);
bool ProvablyNoPostLoop =
SE.isKnownPredicate(ICmpInst::ICMP_SLE, HeaderCountSCEV, RangeEnd);
if (!ProvablyNoPostLoop)
Result.ExitMainLoopAt = ConstructSMinOf(HeaderCountOut, Range.getEnd(), B);
return Result;
}
void LoopConstrainer::cloneLoop(LoopConstrainer::ClonedLoop &Result,
const char *Tag) const {
for (BasicBlock *BB : OriginalLoop.getBlocks()) {
BasicBlock *Clone = CloneBasicBlock(BB, Result.Map, Twine(".") + Tag, &F);
Result.Blocks.push_back(Clone);
Result.Map[BB] = Clone;
}
auto GetClonedValue = [&Result](Value *V) {
assert(V && "null values not in domain!");
auto It = Result.Map.find(V);
if (It == Result.Map.end())
return V;
return static_cast<Value *>(It->second);
};
Result.Structure = MainLoopStructure.map(GetClonedValue);
Result.Structure.Tag = Tag;
for (unsigned i = 0, e = Result.Blocks.size(); i != e; ++i) {
BasicBlock *ClonedBB = Result.Blocks[i];
BasicBlock *OriginalBB = OriginalLoop.getBlocks()[i];
assert(Result.Map[OriginalBB] == ClonedBB && "invariant!");
for (Instruction &I : *ClonedBB)
RemapInstruction(&I, Result.Map,
RF_NoModuleLevelChanges | RF_IgnoreMissingEntries);
for (auto SBBI = succ_begin(OriginalBB), SBBE = succ_end(OriginalBB);
SBBI != SBBE; ++SBBI) {
if (OriginalLoop.contains(*SBBI))
continue;
for (Instruction &I : **SBBI) {
if (!isa<PHINode>(&I))
break;
PHINode *PN = cast<PHINode>(&I);
Value *OldIncoming = PN->getIncomingValueForBlock(OriginalBB);
PN->addIncoming(GetClonedValue(OldIncoming), ClonedBB);
}
}
}
}
LoopConstrainer::RewrittenRangeInfo LoopConstrainer::changeIterationSpaceEnd(
const LoopStructure &LS, BasicBlock *Preheader, Value *ExitLoopAt,
BasicBlock *ContinuationBlock) const {
RewrittenRangeInfo RRI;
auto BBInsertLocation = std::next(Function::iterator(LS.Latch));
RRI.ExitSelector = BasicBlock::Create(Ctx, Twine(LS.Tag) + ".exit.selector",
&F, BBInsertLocation);
RRI.PseudoExit = BasicBlock::Create(Ctx, Twine(LS.Tag) + ".pseudo.exit", &F,
BBInsertLocation);
BranchInst *PreheaderJump = cast<BranchInst>(&*Preheader->rbegin());
IRBuilder<> B(PreheaderJump);
Value *EnterLoopCond = B.CreateICmpSLT(LS.CIVStart, ExitLoopAt);
B.CreateCondBr(EnterLoopCond, LS.Header, RRI.PseudoExit);
PreheaderJump->eraseFromParent();
assert(LS.LatchBrExitIdx == 1 && "generalize this as needed!");
B.SetInsertPoint(LS.LatchBr);
Value *ContinueCond = B.CreateICmpSLT(LS.CIVNext, ExitLoopAt);
LS.LatchBr->setCondition(ContinueCond);
assert(LS.LatchBr->getSuccessor(LS.LatchBrExitIdx) == LS.LatchExit &&
"invariant!");
LS.LatchBr->setSuccessor(LS.LatchBrExitIdx, RRI.ExitSelector);
B.SetInsertPoint(RRI.ExitSelector);
Value *IterationsLeft = B.CreateICmpSLT(LS.CIVNext, OriginalHeaderCount);
B.CreateCondBr(IterationsLeft, RRI.PseudoExit, LS.LatchExit);
BranchInst *BranchToContinuation =
BranchInst::Create(ContinuationBlock, RRI.PseudoExit);
for (Instruction &I : *LS.Header) {
if (!isa<PHINode>(&I))
break;
PHINode *PN = cast<PHINode>(&I);
PHINode *NewPHI = PHINode::Create(PN->getType(), 2, PN->getName() + ".copy",
BranchToContinuation);
NewPHI->addIncoming(PN->getIncomingValueForBlock(Preheader), Preheader);
NewPHI->addIncoming(PN->getIncomingValueForBlock(LS.Latch),
RRI.ExitSelector);
RRI.PHIValuesAtPseudoExit.push_back(NewPHI);
}
for (Instruction &I : *LS.LatchExit) {
if (PHINode *PN = dyn_cast<PHINode>(&I))
replacePHIBlock(PN, LS.Latch, RRI.ExitSelector);
else
break;
}
return RRI;
}
void LoopConstrainer::rewriteIncomingValuesForPHIs(
LoopConstrainer::LoopStructure &LS, BasicBlock *ContinuationBlock,
const LoopConstrainer::RewrittenRangeInfo &RRI) const {
unsigned PHIIndex = 0;
for (Instruction &I : *LS.Header) {
if (!isa<PHINode>(&I))
break;
PHINode *PN = cast<PHINode>(&I);
for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
if (PN->getIncomingBlock(i) == ContinuationBlock)
PN->setIncomingValue(i, RRI.PHIValuesAtPseudoExit[PHIIndex++]);
}
LS.CIVStart = LS.CIV->getIncomingValueForBlock(ContinuationBlock);
}
BasicBlock *
LoopConstrainer::createPreheader(const LoopConstrainer::LoopStructure &LS,
BasicBlock *OldPreheader,
const char *Tag) const {
BasicBlock *Preheader = BasicBlock::Create(Ctx, Tag, &F, LS.Header);
BranchInst::Create(LS.Header, Preheader);
for (Instruction &I : *LS.Header) {
if (!isa<PHINode>(&I))
break;
PHINode *PN = cast<PHINode>(&I);
for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
replacePHIBlock(PN, OldPreheader, Preheader);
}
return Preheader;
}
void LoopConstrainer::addToParentLoopIfNeeded(ArrayRef<BasicBlock *> BBs) {
Loop *ParentLoop = OriginalLoop.getParentLoop();
if (!ParentLoop)
return;
for (BasicBlock *BB : BBs)
ParentLoop->addBasicBlockToLoop(BB, OriginalLoopInfo);
}
bool LoopConstrainer::run() {
BasicBlock *Preheader = nullptr;
const char *CouldNotProceedBecause = nullptr;
if (!recognizeLoop(MainLoopStructure, LatchTakenCount, Preheader,
CouldNotProceedBecause)) {
DEBUG(dbgs() << "irce: could not recognize loop, " << CouldNotProceedBecause
<< "\n";);
return false;
}
OriginalPreheader = Preheader;
MainLoopPreheader = Preheader;
Optional<SubRanges> MaybeSR = calculateSubRanges(OriginalHeaderCount);
if (!MaybeSR.hasValue()) {
DEBUG(dbgs() << "irce: could not compute subranges\n");
return false;
}
SubRanges SR = MaybeSR.getValue();
ClonedLoop PreLoop, PostLoop;
bool NeedsPreLoop = SR.ExitPreLoopAt.hasValue();
bool NeedsPostLoop = SR.ExitMainLoopAt.hasValue();
if (NeedsPreLoop)
cloneLoop(PreLoop, "preloop");
if (NeedsPostLoop)
cloneLoop(PostLoop, "postloop");
RewrittenRangeInfo PreLoopRRI;
if (NeedsPreLoop) {
Preheader->getTerminator()->replaceUsesOfWith(MainLoopStructure.Header,
PreLoop.Structure.Header);
MainLoopPreheader =
createPreheader(MainLoopStructure, Preheader, "mainloop");
PreLoopRRI =
changeIterationSpaceEnd(PreLoop.Structure, Preheader,
SR.ExitPreLoopAt.getValue(), MainLoopPreheader);
rewriteIncomingValuesForPHIs(MainLoopStructure, MainLoopPreheader,
PreLoopRRI);
}
BasicBlock *PostLoopPreheader = nullptr;
RewrittenRangeInfo PostLoopRRI;
if (NeedsPostLoop) {
PostLoopPreheader =
createPreheader(PostLoop.Structure, Preheader, "postloop");
PostLoopRRI = changeIterationSpaceEnd(MainLoopStructure, MainLoopPreheader,
SR.ExitMainLoopAt.getValue(),
PostLoopPreheader);
rewriteIncomingValuesForPHIs(PostLoop.Structure, PostLoopPreheader,
PostLoopRRI);
}
BasicBlock *NewMainLoopPreheader =
MainLoopPreheader != Preheader ? MainLoopPreheader : nullptr;
BasicBlock *NewBlocks[] = {PostLoopPreheader, PreLoopRRI.PseudoExit,
PreLoopRRI.ExitSelector, PostLoopRRI.PseudoExit,
PostLoopRRI.ExitSelector, NewMainLoopPreheader};
auto NewBlocksEnd =
std::remove(std::begin(NewBlocks), std::end(NewBlocks), nullptr);
addToParentLoopIfNeeded(makeArrayRef(std::begin(NewBlocks), NewBlocksEnd));
addToParentLoopIfNeeded(PreLoop.Blocks);
addToParentLoopIfNeeded(PostLoop.Blocks);
return true;
}
Optional<InductiveRangeCheck::Range>
InductiveRangeCheck::computeSafeIterationSpace(ScalarEvolution &SE,
IRBuilder<> &B) const {
const SCEVConstant *ScaleC = dyn_cast<SCEVConstant>(getScale());
if (!(ScaleC && ScaleC->getValue()->getValue() == 1)) {
DEBUG(dbgs() << "irce: could not compute safe iteration space for:\n";
print(dbgs()));
return None;
}
const DataLayout &DL =
Branch->getParent()->getParent()->getParent()->getDataLayout();
Value *OffsetV = SCEVExpander(SE, DL, "safe.itr.space").expandCodeFor(
getOffset(), getOffset()->getType(), B.GetInsertPoint());
OffsetV = MaybeSimplify(OffsetV);
Value *Begin = MaybeSimplify(B.CreateNeg(OffsetV));
Value *End = MaybeSimplify(B.CreateSub(getLength(), OffsetV));
return InductiveRangeCheck::Range(Begin, End);
}
static Optional<InductiveRangeCheck::Range>
IntersectRange(const Optional<InductiveRangeCheck::Range> &R1,
const InductiveRangeCheck::Range &R2, IRBuilder<> &B) {
if (!R1.hasValue())
return R2;
auto &R1Value = R1.getValue();
if (R1Value.getType() != R2.getType())
return None;
Value *NewMin = ConstructSMaxOf(R1Value.getBegin(), R2.getBegin(), B);
Value *NewMax = ConstructSMinOf(R1Value.getEnd(), R2.getEnd(), B);
return InductiveRangeCheck::Range(NewMin, NewMax);
}
bool InductiveRangeCheckElimination::runOnLoop(Loop *L, LPPassManager &LPM) {
if (L->getBlocks().size() >= LoopSizeCutoff) {
DEBUG(dbgs() << "irce: giving up constraining loop, too large\n";);
return false;
}
BasicBlock *Preheader = L->getLoopPreheader();
if (!Preheader) {
DEBUG(dbgs() << "irce: loop has no preheader, leaving\n");
return false;
}
LLVMContext &Context = Preheader->getContext();
InductiveRangeCheck::AllocatorTy IRCAlloc;
SmallVector<InductiveRangeCheck *, 16> RangeChecks;
ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
for (auto BBI : L->getBlocks())
if (BranchInst *TBI = dyn_cast<BranchInst>(BBI->getTerminator()))
if (InductiveRangeCheck *IRC =
InductiveRangeCheck::create(IRCAlloc, TBI, L, SE, BPI))
RangeChecks.push_back(IRC);
if (RangeChecks.empty())
return false;
DEBUG(dbgs() << "irce: looking at loop "; L->print(dbgs());
dbgs() << "irce: loop has " << RangeChecks.size()
<< " inductive range checks: \n";
for (InductiveRangeCheck *IRC : RangeChecks)
IRC->print(dbgs());
);
Optional<InductiveRangeCheck::Range> SafeIterRange;
Instruction *ExprInsertPt = Preheader->getTerminator();
SmallVector<InductiveRangeCheck *, 4> RangeChecksToEliminate;
IRBuilder<> B(ExprInsertPt);
for (InductiveRangeCheck *IRC : RangeChecks) {
auto Result = IRC->computeSafeIterationSpace(SE, B);
if (Result.hasValue()) {
auto MaybeSafeIterRange =
IntersectRange(SafeIterRange, Result.getValue(), B);
if (MaybeSafeIterRange.hasValue()) {
RangeChecksToEliminate.push_back(IRC);
SafeIterRange = MaybeSafeIterRange.getValue();
}
}
}
if (!SafeIterRange.hasValue())
return false;
LoopConstrainer LC(*L, getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), SE,
SafeIterRange.getValue());
bool Changed = LC.run();
if (Changed) {
auto PrintConstrainedLoopInfo = [L]() {
dbgs() << "irce: in function ";
dbgs() << L->getHeader()->getParent()->getName() << ": ";
dbgs() << "constrained ";
L->print(dbgs());
};
DEBUG(PrintConstrainedLoopInfo());
if (PrintChangedLoops)
PrintConstrainedLoopInfo();
for (InductiveRangeCheck *IRC : RangeChecksToEliminate) {
ConstantInt *FoldedRangeCheck = IRC->getPassingDirection()
? ConstantInt::getTrue(Context)
: ConstantInt::getFalse(Context);
IRC->getBranch()->setCondition(FoldedRangeCheck);
}
}
return Changed;
}
Pass *llvm::createInductiveRangeCheckEliminationPass() {
return new InductiveRangeCheckElimination;
}