84 #define DEBUG_TYPE "irce"
98 class InductiveRangeCheck {
100 enum RangeCheckKind :
unsigned {
102 RANGE_CHECK_LOWER = 1,
105 RANGE_CHECK_UPPER = 2,
109 RANGE_CHECK_BOTH = RANGE_CHECK_LOWER | RANGE_CHECK_UPPER,
115 static StringRef rangeCheckKindToStr(RangeCheckKind);
118 const SCEV *Scale =
nullptr;
119 Value *Length =
nullptr;
120 Use *CheckUse =
nullptr;
121 RangeCheckKind
Kind = RANGE_CHECK_UNKNOWN;
123 static RangeCheckKind parseRangeCheckICmp(
Loop *
L,
ICmpInst *ICI,
134 const SCEV *getScale()
const {
return Scale; }
135 Value *getLength()
const {
return Length; }
138 OS <<
"InductiveRangeCheck:\n";
139 OS <<
" Kind: " << rangeCheckKindToStr(Kind) <<
"\n";
149 OS <<
"\n CheckUse: ";
150 getCheckUse()->getUser()->print(OS);
151 OS <<
" Operand: " << getCheckUse()->getOperandNo() <<
"\n";
159 Use *getCheckUse()
const {
return CheckUse; }
169 Range(
const SCEV *Begin,
const SCEV *
End) : Begin(Begin),
End(End) {
174 const SCEV *getBegin()
const {
return Begin; }
175 const SCEV *getEnd()
const {
return End; }
180 bool getPassingDirection() {
return true; }
199 class InductiveRangeCheckElimination :
public LoopPass {
202 InductiveRangeCheckElimination() :
LoopPass(
ID) {
219 "Inductive range check elimination",
false,
false)
225 StringRef InductiveRangeCheck::rangeCheckKindToStr(
226 InductiveRangeCheck::RangeCheckKind RCK) {
228 case InductiveRangeCheck::RANGE_CHECK_UNKNOWN:
229 return "RANGE_CHECK_UNKNOWN";
231 case InductiveRangeCheck::RANGE_CHECK_UPPER:
232 return "RANGE_CHECK_UPPER";
234 case InductiveRangeCheck::RANGE_CHECK_LOWER:
235 return "RANGE_CHECK_LOWER";
237 case InductiveRangeCheck::RANGE_CHECK_BOTH:
238 return "RANGE_CHECK_BOTH";
251 InductiveRangeCheck::RangeCheckKind
252 InductiveRangeCheck::parseRangeCheckICmp(
Loop *
L,
ICmpInst *ICI,
256 auto IsNonNegativeAndNotLoopVarying = [&SE,
L](
Value *V) {
258 if (isa<SCEVCouldNotCompute>(S))
265 using namespace llvm::PatternMatch;
273 return RANGE_CHECK_UNKNOWN;
279 if (
match(RHS, m_ConstantInt<0>())) {
281 return RANGE_CHECK_LOWER;
283 return RANGE_CHECK_UNKNOWN;
289 if (
match(RHS, m_ConstantInt<-1>())) {
291 return RANGE_CHECK_LOWER;
294 if (IsNonNegativeAndNotLoopVarying(LHS)) {
297 return RANGE_CHECK_UPPER;
299 return RANGE_CHECK_UNKNOWN;
305 if (IsNonNegativeAndNotLoopVarying(LHS)) {
308 return RANGE_CHECK_BOTH;
310 return RANGE_CHECK_UNKNOWN;
316 void InductiveRangeCheck::extractRangeChecksFromCond(
320 using namespace llvm::PatternMatch;
322 Value *Condition = ConditionUse.
get();
323 if (!Visited.
insert(Condition).second)
328 extractRangeChecksFromCond(L, SE, cast<User>(Condition)->getOperandUse(0),
330 extractRangeChecksFromCond(L, SE, cast<User>(Condition)->getOperandUse(1),
333 if (SubChecks.
size() == 2) {
336 const auto &RChkA = SubChecks[0];
337 const auto &RChkB = SubChecks[1];
338 if ((RChkA.Length == RChkB.Length || !RChkA.Length || !RChkB.Length) &&
339 RChkA.Offset == RChkB.Offset && RChkA.Scale == RChkB.Scale) {
346 (InductiveRangeCheck::RangeCheckKind)(RChkA.Kind | RChkB.Kind);
347 SubChecks[0].Length = RChkA.Length ? RChkA.Length : RChkB.Length;
348 SubChecks[0].CheckUse = &ConditionUse;
363 Value *Length =
nullptr, *Index;
364 auto RCKind = parseRangeCheckICmp(L, ICI, SE, Index, Length);
365 if (RCKind == InductiveRangeCheck::RANGE_CHECK_UNKNOWN)
370 IndexAddRec && (IndexAddRec->getLoop() ==
L) && IndexAddRec->
isAffine();
375 InductiveRangeCheck IRC;
377 IRC.Offset = IndexAddRec->getStart();
378 IRC.Scale = IndexAddRec->getStepRecurrence(SE);
379 IRC.CheckUse = &ConditionUse;
384 void InductiveRangeCheck::extractRangeChecksFromBranch(
398 InductiveRangeCheck::extractRangeChecksFromCond(L, SE, BI->
getOperandUse(0),
411 Context, {
MDString::get(Context,
"llvm.loop.unroll.disable")});
416 {
MDString::get(Context,
"llvm.loop.vectorize.enable"), FalseVal});
418 Context, {
MDString::get(Context,
"llvm.loop.licm_versioning.disable")});
421 {
MDString::get(Context,
"llvm.loop.distribute.enable"), FalseVal});
424 DisableLICMVersioning, DisableDistribution});
437 struct LoopStructure {
447 unsigned LatchBrExitIdx;
461 bool IndVarIncreasing;
464 :
Tag(
""), Header(nullptr), Latch(nullptr), LatchBr(nullptr),
465 LatchExit(nullptr), LatchBrExitIdx(-1), IndVarNext(nullptr),
466 IndVarStart(nullptr), LoopExitAt(nullptr), IndVarIncreasing(
false) {}
468 template <
typename M> LoopStructure map(M Map)
const {
469 LoopStructure Result;
471 Result.Header = cast<BasicBlock>(Map(Header));
472 Result.Latch = cast<BasicBlock>(Map(Latch));
473 Result.LatchBr = cast<BranchInst>(Map(LatchBr));
474 Result.LatchExit = cast<BasicBlock>(Map(LatchExit));
475 Result.LatchBrExitIdx = LatchBrExitIdx;
476 Result.IndVarNext = Map(IndVarNext);
477 Result.IndVarStart = Map(IndVarStart);
478 Result.LoopExitAt = Map(LoopExitAt);
479 Result.IndVarIncreasing = IndVarIncreasing;
498 class LoopConstrainer {
502 std::vector<BasicBlock *> Blocks;
508 LoopStructure Structure;
513 struct RewrittenRangeInfo {
516 std::vector<PHINode *> PHIValuesAtPseudoExit;
520 : PseudoExit(nullptr), ExitSelector(nullptr), IndVarEnd(nullptr) {}
552 void cloneLoop(ClonedLoop &CLResult,
const char *
Tag)
const;
556 Loop *createClonedLoopStructure(
Loop *Original,
Loop *Parent,
582 changeIterationSpaceEnd(
const LoopStructure &
LS,
BasicBlock *Preheader,
590 const char *
Tag)
const;
596 void rewriteIncomingValuesForPHIs(
597 LoopStructure &
LS,
BasicBlock *ContinuationBlockAndPreheader,
598 const LoopConstrainer::RewrittenRangeInfo &RRI)
const;
616 const SCEV *LatchTakenCount;
624 InductiveRangeCheck::Range Range;
628 LoopStructure MainLoopStructure;
634 :
F(*L.getHeader()->
getParent()), Ctx(L.getHeader()->getContext()),
635 SE(SE), DT(DT), LPM(LPM), LI(LI), OriginalLoop(L),
636 LatchTakenCount(nullptr), OriginalPreheader(nullptr),
637 MainLoopPreheader(nullptr), Range(R), MainLoopStructure(LS) {}
668 Loop &L,
const char *&FailureReason) {
670 FailureReason =
"loop not in LoopSimplify form";
675 assert(Latch &&
"Simplified loops only have one latch!");
678 FailureReason =
"loop has already been cloned";
683 FailureReason =
"no loop latch";
690 FailureReason =
"no preheader";
696 FailureReason =
"latch terminator not conditional branch";
700 unsigned LatchBrExitIdx = LatchBr->
getSuccessor(0) == Header ? 1 : 0;
707 FailureReason =
"short running loop, not profitable";
713 FailureReason =
"latch terminator branch not conditional on integral icmp";
718 if (isa<SCEVCouldNotCompute>(LatchCount)) {
719 FailureReason =
"could not compute latch count";
732 if (!isa<SCEVAddRecExpr>(LeftSCEV)) {
733 if (isa<SCEVAddRecExpr>(RightSCEV)) {
738 FailureReason =
"no add recurrences in the icmp";
747 IntegerType *Ty = cast<IntegerType>(AR->getType());
755 const SCEV *ExtendedStep =
758 bool NoSignedWrap = ExtendAfterOp->getStart() == ExtendedStart &&
759 ExtendAfterOp->getStepRecurrence(SE) == ExtendedStep;
769 auto IsInductionVar = [&](
const SCEVAddRecExpr *AR,
bool &IsIncreasing) {
776 if (!HasNoSignedWrap(AR))
783 IsIncreasing = StepCI->
isOne();
794 const SCEVAddRecExpr *IndVarNext = cast<SCEVAddRecExpr>(LeftSCEV);
795 bool IsIncreasing =
false;
796 if (!IsInductionVar(IndVarNext, IsIncreasing)) {
797 FailureReason =
"LHS in icmp not induction variable";
808 bool DecreasedRightValueByOne =
false;
824 DecreasedRightValueByOne =
true;
827 bool FoundExpectedPred =
831 if (!FoundExpectedPred) {
832 FailureReason =
"expected icmp slt semantically, found something else";
836 if (LatchBrExitIdx == 0) {
840 FailureReason =
"limit may overflow when coercing sle to slt";
847 FailureReason =
"Induction variable start not bounded by upper limit";
853 if (!DecreasedRightValueByOne) {
855 RightValue =
B.CreateAdd(RightValue, One);
860 FailureReason =
"Induction variable start not bounded by upper limit";
863 assert(!DecreasedRightValueByOne &&
864 "Right value can be decreased only for LatchBrExitIdx == 0!");
867 bool IncreasedRightValueByOne =
false;
883 IncreasedRightValueByOne =
true;
886 bool FoundExpectedPred =
890 if (!FoundExpectedPred) {
891 FailureReason =
"expected icmp sgt semantically, found something else";
895 if (LatchBrExitIdx == 0) {
899 FailureReason =
"limit may overflow when coercing sge to sgt";
906 FailureReason =
"Induction variable start not bounded by lower limit";
912 if (!IncreasedRightValueByOne) {
914 RightValue =
B.CreateSub(RightValue, One);
919 FailureReason =
"Induction variable start not bounded by lower limit";
922 assert(!IncreasedRightValueByOne &&
923 "Right value can be increased only for LatchBrExitIdx == 0!");
931 "loop variant exit count doesn't make sense!");
935 Value *IndVarStartV =
938 IndVarStartV->
setName(
"indvar.start");
940 LoopStructure Result;
943 Result.Header = Header;
944 Result.Latch = Latch;
945 Result.LatchBr = LatchBr;
946 Result.LatchExit = LatchExit;
947 Result.LatchBrExitIdx = LatchBrExitIdx;
948 Result.IndVarStart = IndVarStartV;
949 Result.IndVarNext = LeftValue;
950 Result.IndVarIncreasing = IsIncreasing;
951 Result.LoopExitAt = RightValue;
953 FailureReason =
nullptr;
959 LoopConstrainer::calculateSubRanges()
const {
960 IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());
962 if (Range.getType() != Ty)
965 LoopConstrainer::SubRanges Result;
970 const SCEV *Start = SE.
getSCEV(MainLoopStructure.IndVarStart);
973 bool Increasing = MainLoopStructure.IndVarIncreasing;
979 const SCEV *Smallest =
nullptr, *Greatest =
nullptr, *GreatestSeen =
nullptr;
1006 GreatestSeen = Start;
1009 auto Clamp = [
this, Smallest, Greatest](
const SCEV *S) {
1015 bool ProvablyNoPreloop =
1017 if (!ProvablyNoPreloop)
1018 Result.LowLimit = Clamp(Range.getBegin());
1020 bool ProvablyNoPostLoop =
1022 if (!ProvablyNoPostLoop)
1023 Result.HighLimit = Clamp(Range.getEnd());
1028 void LoopConstrainer::cloneLoop(LoopConstrainer::ClonedLoop &Result,
1029 const char *
Tag)
const {
1030 for (
BasicBlock *BB : OriginalLoop.getBlocks()) {
1032 Result.Blocks.push_back(Clone);
1033 Result.Map[BB] = Clone;
1036 auto GetClonedValue = [&Result](
Value *V) {
1037 assert(V &&
"null values not in domain!");
1038 auto It = Result.Map.find(V);
1039 if (It == Result.Map.end())
1041 return static_cast<Value *
>(It->second);
1045 cast<BasicBlock>(GetClonedValue(OriginalLoop.getLoopLatch()));
1049 Result.Structure = MainLoopStructure.map(GetClonedValue);
1050 Result.Structure.Tag =
Tag;
1052 for (
unsigned i = 0, e = Result.Blocks.size(); i != e; ++i) {
1054 BasicBlock *OriginalBB = OriginalLoop.getBlocks()[i];
1056 assert(Result.Map[OriginalBB] == ClonedBB &&
"invariant!");
1067 if (OriginalLoop.contains(
SBB))
1076 PN->
addIncoming(GetClonedValue(OldIncoming), ClonedBB);
1082 LoopConstrainer::RewrittenRangeInfo LoopConstrainer::changeIterationSpaceEnd(
1158 RewrittenRangeInfo RRI;
1160 BasicBlock *BBInsertLocation = LS.Latch->getNextNode();
1162 &
F, BBInsertLocation);
1167 bool Increasing = LS.IndVarIncreasing;
1172 Value *EnterLoopCond = Increasing
1173 ?
B.CreateICmpSLT(LS.IndVarStart, ExitSubloopAt)
1174 :
B.CreateICmpSGT(LS.IndVarStart, ExitSubloopAt);
1176 B.CreateCondBr(EnterLoopCond, LS.Header, RRI.PseudoExit);
1179 LS.LatchBr->setSuccessor(LS.LatchBrExitIdx, RRI.ExitSelector);
1180 B.SetInsertPoint(LS.LatchBr);
1181 Value *TakeBackedgeLoopCond =
1182 Increasing ?
B.CreateICmpSLT(LS.IndVarNext, ExitSubloopAt)
1183 :
B.CreateICmpSGT(LS.IndVarNext, ExitSubloopAt);
1184 Value *CondForBranch = LS.LatchBrExitIdx == 1
1185 ? TakeBackedgeLoopCond
1186 :
B.CreateNot(TakeBackedgeLoopCond);
1188 LS.LatchBr->setCondition(CondForBranch);
1190 B.SetInsertPoint(RRI.ExitSelector);
1195 Value *IterationsLeft = Increasing
1196 ?
B.CreateICmpSLT(LS.IndVarNext, LS.LoopExitAt)
1197 :
B.CreateICmpSGT(LS.IndVarNext, LS.LoopExitAt);
1198 B.CreateCondBr(IterationsLeft, RRI.PseudoExit, LS.LatchExit);
1212 BranchToContinuation);
1217 RRI.PHIValuesAtPseudoExit.push_back(NewPHI);
1220 RRI.IndVarEnd =
PHINode::Create(LS.IndVarNext->getType(), 2,
"indvar.end",
1221 BranchToContinuation);
1222 RRI.IndVarEnd->addIncoming(LS.IndVarStart, Preheader);
1223 RRI.IndVarEnd->addIncoming(LS.IndVarNext, RRI.ExitSelector);
1228 if (
PHINode *PN = dyn_cast<PHINode>(&I))
1229 replacePHIBlock(PN, LS.Latch, RRI.ExitSelector);
1237 void LoopConstrainer::rewriteIncomingValuesForPHIs(
1238 LoopStructure &LS,
BasicBlock *ContinuationBlock,
1239 const LoopConstrainer::RewrittenRangeInfo &RRI)
const {
1241 unsigned PHIIndex = 0;
1252 LS.IndVarStart = RRI.IndVarEnd;
1255 BasicBlock *LoopConstrainer::createPreheader(
const LoopStructure &LS,
1257 const char *Tag)
const {
1268 replacePHIBlock(PN, OldPreheader, Preheader);
1283 Loop *LoopConstrainer::createClonedLoopStructure(
Loop *Original,
Loop *Parent,
1289 LI.addTopLevelLoop(&New);
1293 for (
auto *BB : Original->
blocks())
1294 if (LI.getLoopFor(BB) == Original)
1298 for (
Loop *SubLoop : *Original)
1299 createClonedLoopStructure(SubLoop, &New, VM);
1304 bool LoopConstrainer::run() {
1306 LatchTakenCount = SE.
getExitCount(&OriginalLoop, MainLoopStructure.Latch);
1307 Preheader = OriginalLoop.getLoopPreheader();
1308 assert(!isa<SCEVCouldNotCompute>(LatchTakenCount) && Preheader !=
nullptr &&
1311 OriginalPreheader = Preheader;
1312 MainLoopPreheader = Preheader;
1316 DEBUG(
dbgs() <<
"irce: could not compute subranges\n");
1321 bool Increasing = MainLoopStructure.IndVarIncreasing;
1323 cast<IntegerType>(MainLoopStructure.IndVarNext->getType());
1325 SCEVExpander Expander(SE,
F.getParent()->getDataLayout(),
"irce");
1326 Instruction *InsertPt = OriginalPreheader->getTerminator();
1331 ClonedLoop PreLoop, PostLoop;
1333 Increasing ? SR.LowLimit.hasValue() : SR.HighLimit.hasValue();
1334 bool NeedsPostLoop =
1335 Increasing ? SR.HighLimit.hasValue() : SR.LowLimit.hasValue();
1337 Value *ExitPreLoopAt =
nullptr;
1338 Value *ExitMainLoopAt =
nullptr;
1340 cast<SCEVConstant>(SE.
getConstant(IVTy, -1,
true ));
1343 const SCEV *ExitPreLoopAtSCEV =
nullptr;
1346 ExitPreLoopAtSCEV = *SR.LowLimit;
1349 DEBUG(
dbgs() <<
"irce: could not prove no-overflow when computing "
1350 <<
"preloop exit limit. HighLimit = " << *(*SR.HighLimit)
1354 ExitPreLoopAtSCEV = SE.
getAddExpr(*SR.HighLimit, MinusOneS);
1357 ExitPreLoopAt = Expander.expandCodeFor(ExitPreLoopAtSCEV, IVTy, InsertPt);
1358 ExitPreLoopAt->
setName(
"exit.preloop.at");
1361 if (NeedsPostLoop) {
1362 const SCEV *ExitMainLoopAtSCEV =
nullptr;
1365 ExitMainLoopAtSCEV = *SR.HighLimit;
1368 DEBUG(
dbgs() <<
"irce: could not prove no-overflow when computing "
1369 <<
"mainloop exit limit. LowLimit = " << *(*SR.LowLimit)
1373 ExitMainLoopAtSCEV = SE.
getAddExpr(*SR.LowLimit, MinusOneS);
1376 ExitMainLoopAt = Expander.expandCodeFor(ExitMainLoopAtSCEV, IVTy, InsertPt);
1377 ExitMainLoopAt->
setName(
"exit.mainloop.at");
1383 cloneLoop(PreLoop,
"preloop");
1385 cloneLoop(PostLoop,
"postloop");
1387 RewrittenRangeInfo PreLoopRRI;
1391 PreLoop.Structure.Header);
1394 createPreheader(MainLoopStructure, Preheader,
"mainloop");
1395 PreLoopRRI = changeIterationSpaceEnd(PreLoop.Structure, Preheader,
1396 ExitPreLoopAt, MainLoopPreheader);
1397 rewriteIncomingValuesForPHIs(MainLoopStructure, MainLoopPreheader,
1402 RewrittenRangeInfo PostLoopRRI;
1404 if (NeedsPostLoop) {
1406 createPreheader(PostLoop.Structure, Preheader,
"postloop");
1407 PostLoopRRI = changeIterationSpaceEnd(MainLoopStructure, MainLoopPreheader,
1408 ExitMainLoopAt, PostLoopPreheader);
1409 rewriteIncomingValuesForPHIs(PostLoop.Structure, PostLoopPreheader,
1414 MainLoopPreheader != Preheader ? MainLoopPreheader :
nullptr;
1415 BasicBlock *NewBlocks[] = {PostLoopPreheader, PreLoopRRI.PseudoExit,
1416 PreLoopRRI.ExitSelector, PostLoopRRI.PseudoExit,
1417 PostLoopRRI.ExitSelector, NewMainLoopPreheader};
1432 Loop *PreL =
nullptr, *PostL =
nullptr;
1433 if (!PreLoop.Blocks.empty()) {
1434 PreL = createClonedLoopStructure(
1435 &OriginalLoop, OriginalLoop.getParentLoop(), PreLoop.Map);
1438 if (!PostLoop.Blocks.empty()) {
1439 PostL = createClonedLoopStructure(
1440 &OriginalLoop, OriginalLoop.getParentLoop(), PostLoop.Map);
1444 auto CanonicalizeLoop = [&] (
Loop *
L,
bool IsOriginalLoop) {
1449 if (!IsOriginalLoop)
1453 CanonicalizeLoop(PreL,
false);
1455 CanonicalizeLoop(PostL,
false);
1456 CanonicalizeLoop(&OriginalLoop,
true);
1465 InductiveRangeCheck::computeSafeIterationSpace(
1515 const SCEV *UpperLimit =
nullptr;
1519 if (
Value *V = getLength()) {
1522 assert(
Kind == InductiveRangeCheck::RANGE_CHECK_LOWER &&
"invariant!");
1528 return InductiveRangeCheck::Range(Begin, End);
1534 const InductiveRangeCheck::Range &
R2) {
1541 if (R1Value.getType() != R2.getType())
1544 const SCEV *NewBegin = SE.
getSMaxExpr(R1Value.getBegin(), R2.getBegin());
1547 return InductiveRangeCheck::Range(NewBegin, NewEnd);
1555 DEBUG(
dbgs() <<
"irce: giving up constraining loop, too large\n";);
1561 DEBUG(
dbgs() <<
"irce: loop has no preheader, leaving\n");
1567 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1569 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
1572 if (
BranchInst *TBI = dyn_cast<BranchInst>(BBI->getTerminator()))
1573 InductiveRangeCheck::extractRangeChecksFromBranch(TBI, L, SE, BPI,
1576 if (RangeChecks.
empty())
1579 auto PrintRecognizedRangeChecks = [&](
raw_ostream &OS) {
1580 OS <<
"irce: looking at loop "; L->
print(OS);
1581 OS <<
"irce: loop has " << RangeChecks.
size()
1582 <<
" inductive range checks: \n";
1583 for (InductiveRangeCheck &IRC : RangeChecks)
1587 DEBUG(PrintRecognizedRangeChecks(
dbgs()));
1590 PrintRecognizedRangeChecks(
errs());
1592 const char *FailureReason =
nullptr;
1594 LoopStructure::parseLoopStructure(SE, BPI, *L, FailureReason);
1595 if (!MaybeLoopStructure.
hasValue()) {
1596 DEBUG(
dbgs() <<
"irce: could not parse loop structure: " << FailureReason
1600 LoopStructure LS = MaybeLoopStructure.
getValue();
1601 bool Increasing = LS.IndVarIncreasing;
1602 const SCEV *MinusOne =
1603 SE.
getConstant(LS.IndVarNext->getType(), Increasing ? -1 : 1,
true);
1613 for (InductiveRangeCheck &IRC : RangeChecks) {
1614 auto Result = IRC.computeSafeIterationSpace(SE, IndVar);
1615 if (Result.hasValue()) {
1616 auto MaybeSafeIterRange =
1618 if (MaybeSafeIterRange.hasValue()) {
1620 SafeIterRange = MaybeSafeIterRange.
getValue();
1628 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1629 LoopConstrainer LC(*L, getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), LPM,
1630 LS, SE, DT, SafeIterRange.
getValue());
1631 bool Changed = LC.run();
1634 auto PrintConstrainedLoopInfo = [
L]() {
1635 dbgs() <<
"irce: in function ";
1637 dbgs() <<
"constrained ";
1641 DEBUG(PrintConstrainedLoopInfo());
1644 PrintConstrainedLoopInfo();
1648 for (InductiveRangeCheck &IRC : RangeChecksToEliminate) {
1649 ConstantInt *FoldedRangeCheck = IRC.getPassingDirection()
1652 IRC.getCheckUse()->set(FoldedRangeCheck);
1660 return new InductiveRangeCheckElimination;
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Pass interface - Implemented by all 'passes'.
const Use & getOperandUse(unsigned i) const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
const_iterator end(StringRef path)
Get end iterator over path.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
const SCEV * getConstant(ConstantInt *V)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Inductive range check false
static MDString * get(LLVMContext &Context, StringRef Str)
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
The main scalar evolution driver.
bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
LoopT * getParentLoop() const
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static cl::opt< bool > PrintRangeChecks("irce-print-range-checks", cl::Hidden, cl::init(false))
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
BlockT * getHeader() const
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
const SCEV * getStart() const
StringRef getName() const
Return a constant reference to the value's name.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
The SCEV is loop-invariant.
bool match(Val *V, const Pattern &P)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isUnconditional() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
const APInt & getValue() const
Return the constant as an APInt value reference.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void setName(const Twine &Name)
Change the name of the value.
static cl::opt< int > MaxExitProbReciprocal("irce-max-exit-prob-reciprocal", cl::Hidden, cl::init(10))
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
LLVM_NODISCARD bool empty() const
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS...
static void DisableAllLoopOptsOnLoop(Loop &L)
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
This node represents a polynomial recurrence on the trip count of the specified loop.
static const char * ClonedLoopTag
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
const T & getValue() const LLVM_LVALUE_FUNCTION
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Inductive range check elimination
BasicBlock * getSuccessor(unsigned i) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static Optional< InductiveRangeCheck::Range > IntersectRange(ScalarEvolution &SE, const Optional< InductiveRangeCheck::Range > &R1, const InductiveRangeCheck::Range &R2)
Legacy analysis pass which computes BranchProbabilityInfo.
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
iterator_range< block_iterator > blocks() const
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS. ...
initializer< Ty > init(const Ty &Val)
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
Type * getType() const
Return the LLVM type of this SCEV expression.
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
Conditional or Unconditional Branch instruction.
void initializeInductiveRangeCheckEliminationPass(PassRegistry &)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
static bool CanBeSMin(ScalarEvolution &SE, const SCEV *S)
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
static const unsigned End
Value * getOperand(unsigned i) const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Class to represent integer types.
Predicate getPredicate() const
Return the predicate for this instruction.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
static wasm::ValType getType(const TargetRegisterClass *RC)
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static cl::opt< bool > SkipProfitabilityChecks("irce-skip-profitability-checks", cl::Hidden, cl::init(false))
This is the shared class of boolean and integer constants.
void setIncomingBlock(unsigned i, BasicBlock *BB)
INITIALIZE_PASS_BEGIN(InductiveRangeCheckElimination,"irce","Inductive range check elimination", false, false) INITIALIZE_PASS_END(InductiveRangeCheckElimination
static bool CanBeSMax(ScalarEvolution &SE, const SCEV *S)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
ConstantInt * getValue() const
static ConstantInt * getTrue(LLVMContext &Context)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false...
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, DebugInfoFinder *DIFinder=nullptr)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM...
This class uses information about analyze scalars to rewrite expressions in canonical form...
iterator insert(iterator I, T &&Elt)
If this flag is set, the remapper ignores missing function-local entries (Argument, Instruction, BasicBlock) that are not in the value map.
llvm_unreachable("Unknown RegBank!")
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Value * getCondition() const
Analysis providing branch probability information.
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static cl::opt< unsigned > LoopSizeCutoff("irce-loop-size-cutoff", cl::Hidden, cl::init(64))
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
static cl::opt< bool > PrintChangedLoops("irce-print-changed-loops", cl::Hidden, cl::init(false))
LLVMContext & getContext() const
Get the context in which this basic block lives.
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
void print(raw_ostream &OS) const
Print out the internal representation of this scalar to the specified stream.
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
StringRef - Represent a constant reference to a string, i.e.
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
const SCEV * getExitCount(const Loop *L, BasicBlock *ExitingBlock)
Get the expression for the number of loop iterations for which this loop is guaranteed not to exit vi...
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void setIncomingValue(unsigned i, Value *V)
const BasicBlock * getParent() const
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Pass * createInductiveRangeCheckEliminationPass()
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
This class represents a constant integer value.