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;
452 bool IndVarIncreasing;
455 :
Tag(
""), Header(nullptr), Latch(nullptr), LatchBr(nullptr),
456 LatchExit(nullptr), LatchBrExitIdx(-1), IndVarNext(nullptr),
457 IndVarStart(nullptr), LoopExitAt(nullptr), IndVarIncreasing(
false) {}
459 template <
typename M> LoopStructure map(M Map)
const {
460 LoopStructure Result;
462 Result.Header = cast<BasicBlock>(Map(Header));
463 Result.Latch = cast<BasicBlock>(Map(Latch));
464 Result.LatchBr = cast<BranchInst>(Map(LatchBr));
465 Result.LatchExit = cast<BasicBlock>(Map(LatchExit));
466 Result.LatchBrExitIdx = LatchBrExitIdx;
467 Result.IndVarNext = Map(IndVarNext);
468 Result.IndVarStart = Map(IndVarStart);
469 Result.LoopExitAt = Map(LoopExitAt);
470 Result.IndVarIncreasing = IndVarIncreasing;
489 class LoopConstrainer {
493 std::vector<BasicBlock *> Blocks;
499 LoopStructure Structure;
504 struct RewrittenRangeInfo {
507 std::vector<PHINode *> PHIValuesAtPseudoExit;
511 : PseudoExit(nullptr), ExitSelector(nullptr), IndVarEnd(nullptr) {}
543 void cloneLoop(ClonedLoop &CLResult,
const char *
Tag)
const;
547 Loop *createClonedLoopStructure(
Loop *Original,
Loop *Parent,
573 changeIterationSpaceEnd(
const LoopStructure &
LS,
BasicBlock *Preheader,
581 const char *
Tag)
const;
587 void rewriteIncomingValuesForPHIs(
588 LoopStructure &
LS,
BasicBlock *ContinuationBlockAndPreheader,
589 const LoopConstrainer::RewrittenRangeInfo &RRI)
const;
607 const SCEV *LatchTakenCount;
615 InductiveRangeCheck::Range Range;
619 LoopStructure MainLoopStructure;
625 :
F(*L.getHeader()->
getParent()), Ctx(L.getHeader()->getContext()),
626 SE(SE), DT(DT), LPM(LPM), LI(LI), OriginalLoop(L),
627 LatchTakenCount(nullptr), OriginalPreheader(nullptr),
628 MainLoopPreheader(nullptr), Range(R), MainLoopStructure(LS) {}
659 Loop &L,
const char *&FailureReason) {
661 FailureReason =
"loop not in LoopSimplify form";
666 assert(Latch &&
"Simplified loops only have one latch!");
669 FailureReason =
"loop has already been cloned";
674 FailureReason =
"no loop latch";
681 FailureReason =
"no preheader";
687 FailureReason =
"latch terminator not conditional branch";
691 unsigned LatchBrExitIdx = LatchBr->
getSuccessor(0) == Header ? 1 : 0;
698 FailureReason =
"short running loop, not profitable";
704 FailureReason =
"latch terminator branch not conditional on integral icmp";
709 if (isa<SCEVCouldNotCompute>(LatchCount)) {
710 FailureReason =
"could not compute latch count";
723 if (!isa<SCEVAddRecExpr>(LeftSCEV)) {
724 if (isa<SCEVAddRecExpr>(RightSCEV)) {
729 FailureReason =
"no add recurrences in the icmp";
738 IntegerType *Ty = cast<IntegerType>(AR->getType());
746 const SCEV *ExtendedStep =
749 bool NoSignedWrap = ExtendAfterOp->getStart() == ExtendedStart &&
750 ExtendAfterOp->getStepRecurrence(SE) == ExtendedStep;
760 auto IsInductionVar = [&](
const SCEVAddRecExpr *AR,
bool &IsIncreasing) {
767 if (!HasNoSignedWrap(AR))
774 IsIncreasing = StepCI->
isOne();
785 const SCEVAddRecExpr *IndVarNext = cast<SCEVAddRecExpr>(LeftSCEV);
786 bool IsIncreasing =
false;
787 if (!IsInductionVar(IndVarNext, IsIncreasing)) {
788 FailureReason =
"LHS in icmp not induction variable";
795 bool FoundExpectedPred =
799 if (!FoundExpectedPred) {
800 FailureReason =
"expected icmp slt semantically, found something else";
804 if (LatchBrExitIdx == 0) {
808 FailureReason =
"limit may overflow when coercing sle to slt";
813 RightValue =
B.CreateAdd(RightValue, One);
817 bool FoundExpectedPred =
821 if (!FoundExpectedPred) {
822 FailureReason =
"expected icmp sgt semantically, found something else";
826 if (LatchBrExitIdx == 0) {
830 FailureReason =
"limit may overflow when coercing sge to sgt";
835 RightValue =
B.CreateSub(RightValue, One);
847 "loop variant exit count doesn't make sense!");
851 Value *IndVarStartV =
854 IndVarStartV->
setName(
"indvar.start");
856 LoopStructure Result;
859 Result.Header = Header;
860 Result.Latch = Latch;
861 Result.LatchBr = LatchBr;
862 Result.LatchExit = LatchExit;
863 Result.LatchBrExitIdx = LatchBrExitIdx;
864 Result.IndVarStart = IndVarStartV;
865 Result.IndVarNext = LeftValue;
866 Result.IndVarIncreasing = IsIncreasing;
867 Result.LoopExitAt = RightValue;
869 FailureReason =
nullptr;
875 LoopConstrainer::calculateSubRanges()
const {
876 IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());
878 if (Range.getType() != Ty)
881 LoopConstrainer::SubRanges Result;
887 const SCEV *Start = SE.
getSCEV(MainLoopStructure.IndVarStart);
890 bool Increasing = MainLoopStructure.IndVarIncreasing;
895 const SCEV *Smallest =
nullptr, *Greatest =
nullptr;
921 auto Clamp = [
this, Smallest, Greatest](
const SCEV *S) {
927 bool ProvablyNoPreloop =
929 if (!ProvablyNoPreloop)
930 Result.LowLimit = Clamp(Range.getBegin());
932 bool ProvablyNoPostLoop =
934 if (!ProvablyNoPostLoop)
935 Result.HighLimit = Clamp(Range.getEnd());
940 void LoopConstrainer::cloneLoop(LoopConstrainer::ClonedLoop &Result,
941 const char *
Tag)
const {
942 for (
BasicBlock *BB : OriginalLoop.getBlocks()) {
944 Result.Blocks.push_back(Clone);
945 Result.Map[BB] = Clone;
948 auto GetClonedValue = [&Result](
Value *V) {
949 assert(V &&
"null values not in domain!");
950 auto It = Result.Map.find(V);
951 if (It == Result.Map.end())
953 return static_cast<Value *
>(It->second);
957 cast<BasicBlock>(GetClonedValue(OriginalLoop.getLoopLatch()));
961 Result.Structure = MainLoopStructure.map(GetClonedValue);
962 Result.Structure.Tag =
Tag;
964 for (
unsigned i = 0, e = Result.Blocks.size();
i != e; ++
i) {
966 BasicBlock *OriginalBB = OriginalLoop.getBlocks()[
i];
968 assert(Result.Map[OriginalBB] == ClonedBB &&
"invariant!");
979 if (OriginalLoop.contains(
SBB))
988 PN->
addIncoming(GetClonedValue(OldIncoming), ClonedBB);
994 LoopConstrainer::RewrittenRangeInfo LoopConstrainer::changeIterationSpaceEnd(
1070 RewrittenRangeInfo RRI;
1072 BasicBlock *BBInsertLocation = LS.Latch->getNextNode();
1074 &
F, BBInsertLocation);
1079 bool Increasing = LS.IndVarIncreasing;
1084 Value *EnterLoopCond = Increasing
1085 ?
B.CreateICmpSLT(LS.IndVarStart, ExitSubloopAt)
1086 :
B.CreateICmpSGT(LS.IndVarStart, ExitSubloopAt);
1088 B.CreateCondBr(EnterLoopCond, LS.Header, RRI.PseudoExit);
1091 LS.LatchBr->setSuccessor(LS.LatchBrExitIdx, RRI.ExitSelector);
1092 B.SetInsertPoint(LS.LatchBr);
1093 Value *TakeBackedgeLoopCond =
1094 Increasing ?
B.CreateICmpSLT(LS.IndVarNext, ExitSubloopAt)
1095 :
B.CreateICmpSGT(LS.IndVarNext, ExitSubloopAt);
1096 Value *CondForBranch = LS.LatchBrExitIdx == 1
1097 ? TakeBackedgeLoopCond
1098 :
B.CreateNot(TakeBackedgeLoopCond);
1100 LS.LatchBr->setCondition(CondForBranch);
1102 B.SetInsertPoint(RRI.ExitSelector);
1107 Value *IterationsLeft = Increasing
1108 ?
B.CreateICmpSLT(LS.IndVarNext, LS.LoopExitAt)
1109 :
B.CreateICmpSGT(LS.IndVarNext, LS.LoopExitAt);
1110 B.CreateCondBr(IterationsLeft, RRI.PseudoExit, LS.LatchExit);
1124 BranchToContinuation);
1129 RRI.PHIValuesAtPseudoExit.push_back(NewPHI);
1132 RRI.IndVarEnd =
PHINode::Create(LS.IndVarNext->getType(), 2,
"indvar.end",
1133 BranchToContinuation);
1134 RRI.IndVarEnd->addIncoming(LS.IndVarStart, Preheader);
1135 RRI.IndVarEnd->addIncoming(LS.IndVarNext, RRI.ExitSelector);
1140 if (
PHINode *PN = dyn_cast<PHINode>(&I))
1141 replacePHIBlock(PN, LS.Latch, RRI.ExitSelector);
1149 void LoopConstrainer::rewriteIncomingValuesForPHIs(
1150 LoopStructure &LS,
BasicBlock *ContinuationBlock,
1151 const LoopConstrainer::RewrittenRangeInfo &RRI)
const {
1153 unsigned PHIIndex = 0;
1164 LS.IndVarStart = RRI.IndVarEnd;
1167 BasicBlock *LoopConstrainer::createPreheader(
const LoopStructure &LS,
1169 const char *Tag)
const {
1180 replacePHIBlock(PN, OldPreheader, Preheader);
1195 Loop *LoopConstrainer::createClonedLoopStructure(
Loop *Original,
Loop *Parent,
1197 Loop &New = LPM.addLoop(Parent);
1200 for (
auto *BB : Original->
blocks())
1201 if (LI.getLoopFor(BB) == Original)
1205 for (
Loop *SubLoop : *Original)
1206 createClonedLoopStructure(SubLoop, &New, VM);
1211 bool LoopConstrainer::run() {
1213 LatchTakenCount = SE.
getExitCount(&OriginalLoop, MainLoopStructure.Latch);
1214 Preheader = OriginalLoop.getLoopPreheader();
1215 assert(!isa<SCEVCouldNotCompute>(LatchTakenCount) && Preheader !=
nullptr &&
1218 OriginalPreheader = Preheader;
1219 MainLoopPreheader = Preheader;
1223 DEBUG(
dbgs() <<
"irce: could not compute subranges\n");
1228 bool Increasing = MainLoopStructure.IndVarIncreasing;
1230 cast<IntegerType>(MainLoopStructure.IndVarNext->getType());
1232 SCEVExpander Expander(SE,
F.getParent()->getDataLayout(),
"irce");
1233 Instruction *InsertPt = OriginalPreheader->getTerminator();
1238 ClonedLoop PreLoop, PostLoop;
1240 Increasing ? SR.LowLimit.hasValue() : SR.HighLimit.hasValue();
1241 bool NeedsPostLoop =
1242 Increasing ? SR.HighLimit.hasValue() : SR.LowLimit.hasValue();
1244 Value *ExitPreLoopAt =
nullptr;
1245 Value *ExitMainLoopAt =
nullptr;
1247 cast<SCEVConstant>(SE.
getConstant(IVTy, -1,
true ));
1250 const SCEV *ExitPreLoopAtSCEV =
nullptr;
1253 ExitPreLoopAtSCEV = *SR.LowLimit;
1256 DEBUG(
dbgs() <<
"irce: could not prove no-overflow when computing "
1257 <<
"preloop exit limit. HighLimit = " << *(*SR.HighLimit)
1261 ExitPreLoopAtSCEV = SE.
getAddExpr(*SR.HighLimit, MinusOneS);
1264 ExitPreLoopAt = Expander.expandCodeFor(ExitPreLoopAtSCEV, IVTy, InsertPt);
1265 ExitPreLoopAt->
setName(
"exit.preloop.at");
1268 if (NeedsPostLoop) {
1269 const SCEV *ExitMainLoopAtSCEV =
nullptr;
1272 ExitMainLoopAtSCEV = *SR.HighLimit;
1275 DEBUG(
dbgs() <<
"irce: could not prove no-overflow when computing "
1276 <<
"mainloop exit limit. LowLimit = " << *(*SR.LowLimit)
1280 ExitMainLoopAtSCEV = SE.
getAddExpr(*SR.LowLimit, MinusOneS);
1283 ExitMainLoopAt = Expander.expandCodeFor(ExitMainLoopAtSCEV, IVTy, InsertPt);
1284 ExitMainLoopAt->
setName(
"exit.mainloop.at");
1290 cloneLoop(PreLoop,
"preloop");
1292 cloneLoop(PostLoop,
"postloop");
1294 RewrittenRangeInfo PreLoopRRI;
1298 PreLoop.Structure.Header);
1301 createPreheader(MainLoopStructure, Preheader,
"mainloop");
1302 PreLoopRRI = changeIterationSpaceEnd(PreLoop.Structure, Preheader,
1303 ExitPreLoopAt, MainLoopPreheader);
1304 rewriteIncomingValuesForPHIs(MainLoopStructure, MainLoopPreheader,
1309 RewrittenRangeInfo PostLoopRRI;
1311 if (NeedsPostLoop) {
1313 createPreheader(PostLoop.Structure, Preheader,
"postloop");
1314 PostLoopRRI = changeIterationSpaceEnd(MainLoopStructure, MainLoopPreheader,
1315 ExitMainLoopAt, PostLoopPreheader);
1316 rewriteIncomingValuesForPHIs(PostLoop.Structure, PostLoopPreheader,
1321 MainLoopPreheader != Preheader ? MainLoopPreheader :
nullptr;
1322 BasicBlock *NewBlocks[] = {PostLoopPreheader, PreLoopRRI.PseudoExit,
1323 PreLoopRRI.ExitSelector, PostLoopRRI.PseudoExit,
1324 PostLoopRRI.ExitSelector, NewMainLoopPreheader};
1335 if (!PreLoop.Blocks.empty()) {
1336 auto *L = createClonedLoopStructure(
1337 &OriginalLoop, OriginalLoop.getParentLoop(), PreLoop.Map);
1345 if (!PostLoop.Blocks.empty()) {
1346 auto *L = createClonedLoopStructure(
1347 &OriginalLoop, OriginalLoop.getParentLoop(), PostLoop.Map);
1356 simplifyLoop(&OriginalLoop, &DT, &LI, &SE,
nullptr,
true);
1365 InductiveRangeCheck::computeSafeIterationSpace(
1415 const SCEV *UpperLimit =
nullptr;
1419 if (
Value *V = getLength()) {
1422 assert(
Kind == InductiveRangeCheck::RANGE_CHECK_LOWER &&
"invariant!");
1428 return InductiveRangeCheck::Range(Begin, End);
1434 const InductiveRangeCheck::Range &
R2) {
1441 if (R1Value.getType() != R2.getType())
1444 const SCEV *NewBegin = SE.
getSMaxExpr(R1Value.getBegin(), R2.getBegin());
1447 return InductiveRangeCheck::Range(NewBegin, NewEnd);
1455 DEBUG(
dbgs() <<
"irce: giving up constraining loop, too large\n";);
1461 DEBUG(
dbgs() <<
"irce: loop has no preheader, leaving\n");
1467 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1469 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
1472 if (
BranchInst *TBI = dyn_cast<BranchInst>(BBI->getTerminator()))
1473 InductiveRangeCheck::extractRangeChecksFromBranch(TBI, L, SE, BPI,
1476 if (RangeChecks.
empty())
1479 auto PrintRecognizedRangeChecks = [&](
raw_ostream &OS) {
1480 OS <<
"irce: looking at loop "; L->
print(OS);
1481 OS <<
"irce: loop has " << RangeChecks.
size()
1482 <<
" inductive range checks: \n";
1483 for (InductiveRangeCheck &IRC : RangeChecks)
1487 DEBUG(PrintRecognizedRangeChecks(
dbgs()));
1490 PrintRecognizedRangeChecks(
errs());
1492 const char *FailureReason =
nullptr;
1494 LoopStructure::parseLoopStructure(SE, BPI, *L, FailureReason);
1495 if (!MaybeLoopStructure.
hasValue()) {
1496 DEBUG(
dbgs() <<
"irce: could not parse loop structure: " << FailureReason
1500 LoopStructure LS = MaybeLoopStructure.
getValue();
1501 bool Increasing = LS.IndVarIncreasing;
1502 const SCEV *MinusOne =
1503 SE.
getConstant(LS.IndVarNext->getType(), Increasing ? -1 : 1,
true);
1513 for (InductiveRangeCheck &IRC : RangeChecks) {
1514 auto Result = IRC.computeSafeIterationSpace(SE, IndVar);
1515 if (Result.hasValue()) {
1516 auto MaybeSafeIterRange =
1518 if (MaybeSafeIterRange.hasValue()) {
1520 SafeIterRange = MaybeSafeIterRange.
getValue();
1528 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1529 LoopConstrainer LC(*L, getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), LPM,
1530 LS, SE, DT, SafeIterRange.
getValue());
1531 bool Changed = LC.run();
1534 auto PrintConstrainedLoopInfo = [
L]() {
1535 dbgs() <<
"irce: in function ";
1537 dbgs() <<
"constrained ";
1541 DEBUG(PrintConstrainedLoopInfo());
1544 PrintConstrainedLoopInfo();
1548 for (InductiveRangeCheck &IRC : RangeChecksToEliminate) {
1549 ConstantInt *FoldedRangeCheck = IRC.getPassingDirection()
1552 IRC.getCheckUse()->set(FoldedRangeCheck);
1560 return new InductiveRangeCheckElimination;
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
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 SCEV * getExitCount(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 * 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...
const_iterator begin(StringRef path)
Get begin iterator over path.
LoopT * getParentLoop() const
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
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...
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
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
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 GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
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
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)
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.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
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)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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...
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty)
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...
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Get a canonical add expression, or something simpler if possible.
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.
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.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
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.
void setIncomingValue(unsigned i, Value *V)
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
const BasicBlock * getParent() const
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Pass * createInductiveRangeCheckEliminationPass()
This class represents a constant integer value.