58 #define DEBUG_TYPE "indvars"
60 STATISTIC(NumWidened ,
"Number of indvars widened");
61 STATISTIC(NumReplaced ,
"Number of exit values replaced");
62 STATISTIC(NumLFTR ,
"Number of loop exit tests replaced");
63 STATISTIC(NumElimExt ,
"Number of IV sign/zero extends eliminated");
64 STATISTIC(NumElimIV ,
"Number of congruent IVs eliminated");
71 cl::desc(
"Verify the ScalarEvolution result after running indvars"));
77 cl::desc(
"Choose the strategy to replace exit value in IndVarSimplify"),
80 "only replace exit value when the cost is cheap"),
82 "always replace exit value whenever possible")));
86 cl::desc(
"Use post increment control-dependent ranges in IndVarSimplify"),
92 class IndVarSimplify {
101 bool Changed =
false;
103 bool isValidRewrite(
Value *FromVal,
Value *ToVal);
106 void rewriteNonIntegerIVs(
Loop *
L);
112 void rewriteFirstIterationLoopExitValues(
Loop *
L);
114 Value *linearFunctionTestReplace(
Loop *
L,
const SCEV *BackedgeTakenCount,
117 void sinkUnusedInvariants(
Loop *
L);
126 : LI(LI), SE(SE), DT(DT), DL(DL), TLI(TLI), TTI(TTI) {}
136 bool IndVarSimplify::isValidRewrite(
Value *FromVal,
Value *ToVal) {
147 Value *FromPtr = FromVal;
148 Value *ToPtr = ToVal;
149 if (
auto *
GEP = dyn_cast<GEPOperator>(FromVal)) {
150 FromPtr =
GEP->getPointerOperand();
152 if (
auto *
GEP = dyn_cast<GEPOperator>(ToVal)) {
153 ToPtr =
GEP->getPointerOperand();
155 if (FromPtr != FromVal || ToPtr != ToVal) {
157 if (FromPtr == ToPtr)
171 const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr));
172 const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr));
173 if (FromBase == ToBase)
176 DEBUG(
dbgs() <<
"INDVARS: GEP rewrite bail out "
177 << *FromBase <<
" != " << *ToBase <<
"\n");
207 assert(InsertPt &&
"Missing phi operand");
213 assert(DT->
dominates(DefI, InsertPt) &&
"def does not dominate all uses");
218 for (
auto *DTN = (*DT)[InsertPt->
getParent()]; DTN; DTN = DTN->getIDom())
220 return DTN->getBlock()->getTerminator();
231 bool isExact =
false;
250 void IndVarSimplify::handleFloatingPointIV(
Loop *
L,
PHINode *PN) {
252 unsigned BackEdge = IncomingEdge^1;
258 if (!InitValueVal || !
ConvertToSInt(InitValueVal->getValueAPF(), InitValue))
264 if (Incr ==
nullptr || Incr->getOpcode() != Instruction::FAdd)
return;
270 if (IncValueVal ==
nullptr || Incr->
getOperand(0) != PN ||
278 if (IncrUse == Incr->user_end())
return;
280 if (IncrUse != Incr->user_end())
return;
308 if (ExitValueVal ==
nullptr ||
348 if (InitValue >= ExitValue)
355 if (++Range == 0)
return;
358 unsigned Leftover = Range %
uint32_t(IncValue);
369 if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
375 if (InitValue <= ExitValue)
382 if (++Range == 0)
return;
385 unsigned Leftover = Range %
uint32_t(-IncValue);
396 if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
446 void IndVarSimplify::rewriteNonIntegerIVs(
Loop *L) {
458 for (
unsigned i = 0, e = PHIs.
size();
i != e; ++
i)
459 if (
PHINode *PN = dyn_cast_or_null<PHINode>(&*PHIs[
i]))
460 handleFloatingPointIV(L, PN);
479 : PN(P), Ith(I), Val(V), HighCost(H) {}
489 if (ExistingValue->getType() == ResultTy)
490 return ExistingValue;
511 void IndVarSimplify::rewriteLoopExitValues(
Loop *L,
SCEVExpander &Rewriter) {
514 "Indvars did not preserve LCSSA!");
533 while ((PN = dyn_cast<PHINode>(BBI++))) {
537 if (!SE->isSCEVable(PN->
getType()))
548 for (
unsigned i = 0; i != NumPreds; ++
i) {
552 if (!isa<Instruction>(InVal))
568 if (!SE->isLoopInvariant(ExitValue, L) ||
578 unsigned NumHardInternalUses = 0;
579 unsigned NumSoftExternalUses = 0;
580 unsigned NumUses = 0;
582 IB !=
IE && NumUses <= 6; ++IB) {
588 NumHardInternalUses++;
590 if (Opc == Instruction::PHI) {
596 PB != PE && NumUses <= 6; ++PB, ++NumUses) {
597 unsigned PhiOpc = cast<Instruction>(*PB)->getOpcode();
599 NumSoftExternalUses++;
604 NumSoftExternalUses++;
607 if (NumUses <= 6 && NumHardInternalUses && !NumSoftExternalUses)
613 expandSCEVIfNeeded(Rewriter, ExitValue, L, Inst, PN->
getType());
615 DEBUG(
dbgs() <<
"INDVARS: RLEV: AfterLoopVal = " << *ExitVal <<
'\n'
616 <<
" LoopVal = " << *Inst <<
"\n");
618 if (!isValidRewrite(Inst, ExitVal)) {
619 DeadInsts.push_back(ExitVal);
629 bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
632 for (
const RewritePhi &Phi : RewritePhiSet) {
634 Value *ExitVal = Phi.Val;
639 DeadInsts.push_back(ExitVal);
651 DeadInsts.push_back(Inst);
655 LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
675 void IndVarSimplify::rewriteFirstIterationLoopExitValues(
Loop *L) {
682 assert(LoopHeader &&
"Invalid loop");
684 for (
auto *ExitBB : ExitBlocks) {
688 while (
auto *PN = dyn_cast<PHINode>(BBI++)) {
690 IncomingValIdx !=
E; ++IncomingValIdx) {
697 if (IncomingBB != LoopHeader)
703 Value *Cond =
nullptr;
704 if (
auto *BI = dyn_cast<BranchInst>(TermInst)) {
707 Cond = BI->getCondition();
708 }
else if (
auto *SI = dyn_cast<SwitchInst>(TermInst))
709 Cond =
SI->getCondition();
727 assert(LoopPreheader &&
"Invalid loop");
728 int PreheaderIdx = ExitVal->getBasicBlockIndex(LoopPreheader);
729 if (PreheaderIdx != -1) {
730 assert(ExitVal->getParent() == LoopHeader &&
731 "ExitVal must be in loop header");
733 ExitVal->getIncomingValue(PreheaderIdx));
743 bool IndVarSimplify::canLoopBeDeleted(
759 if (ExitBlocks.
size() > 1 || ExitingBlocks.
size() > 1)
764 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
765 Value *Incoming =
P->getIncomingValueForBlock(ExitingBlocks[0]);
771 for (
const RewritePhi &Phi : RewritePhiSet) {
772 unsigned i = Phi.Ith;
773 if (Phi.PN ==
P && (Phi.PN)->getIncomingValue(i) == Incoming) {
780 if (!found && (I = dyn_cast<Instruction>(Incoming)))
787 for (
auto *BB : L->
blocks())
804 Type *WidestNativeType =
nullptr;
805 bool IsSigned =
false;
814 bool IsSigned = Cast->
getOpcode() == Instruction::SExt;
815 if (!IsSigned && Cast->
getOpcode() != Instruction::ZExt)
828 if (NarrowIVWidth >= Width)
844 if (!WI.WidestNativeType) {
846 WI.IsSigned = IsSigned;
851 if (WI.IsSigned != IsSigned)
863 struct NarrowIVDefUse {
871 bool NeverNegative =
false;
875 : NarrowDef(ND), NarrowUse(NU), WideDef(WD),
876 NeverNegative(NeverNegative) {}
902 const SCEV *WideIncExpr;
908 enum ExtendKind { ZeroExtended, SignExtended,
Unknown };
924 DefUserPair Key(Def, UseI);
925 auto It = PostIncRangeInfos.find(Key);
926 return It == PostIncRangeInfos.end()
931 void calculatePostIncRanges(
PHINode *OrigPhi);
934 DefUserPair Key(Def, UseI);
935 auto It = PostIncRangeInfos.find(Key);
936 if (It == PostIncRangeInfos.end())
937 PostIncRangeInfos.insert({Key, R});
943 WidenIV(
const WideIVInfo &WI,
LoopInfo *LInfo,
946 OrigPhi(WI.NarrowIV),
947 WideType(WI.WidestNativeType),
952 HasGuards(HasGuards),
955 WideIncExpr(nullptr),
958 ExtendKindMap[OrigPhi] = WI.IsSigned ? SignExtended : ZeroExtended;
964 Value *createExtendInst(
Value *NarrowOper,
Type *WideType,
bool IsSigned,
968 Instruction *cloneArithmeticIVUser(NarrowIVDefUse DU,
970 Instruction *cloneBitwiseIVUser(NarrowIVDefUse DU);
974 typedef std::pair<const SCEVAddRecExpr *, ExtendKind> WidenedRecTy;
976 WidenedRecTy getWideRecurrence(NarrowIVDefUse DU);
978 WidenedRecTy getExtendedOperandRecurrence(NarrowIVDefUse DU);
980 const SCEV *getSCEVByOpCode(
const SCEV *LHS,
const SCEV *RHS,
981 unsigned OpCode)
const;
985 bool widenLoopCompare(NarrowIVDefUse DU);
1002 Value *WidenIV::createExtendInst(
Value *NarrowOper,
Type *WideType,
1012 return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) :
1013 Builder.CreateZExt(NarrowOper, WideType);
1019 Instruction *WidenIV::cloneIVUser(NarrowIVDefUse DU,
1021 unsigned Opcode = DU.NarrowUse->getOpcode();
1026 case Instruction::Mul:
1027 case Instruction::UDiv:
1028 case Instruction::Sub:
1029 return cloneArithmeticIVUser(DU, WideAR);
1034 case Instruction::Shl:
1035 case Instruction::LShr:
1036 case Instruction::AShr:
1037 return cloneBitwiseIVUser(DU);
1041 Instruction *WidenIV::cloneBitwiseIVUser(NarrowIVDefUse DU) {
1046 DEBUG(
dbgs() <<
"Cloning bitwise IVUser: " << *NarrowUse <<
"\n");
1052 bool IsSigned = getExtendKind(NarrowDef) == SignExtended;
1055 : createExtendInst(NarrowUse->
getOperand(0), WideType,
1056 IsSigned, NarrowUse);
1059 : createExtendInst(NarrowUse->
getOperand(1), WideType,
1060 IsSigned, NarrowUse);
1062 auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1063 auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1066 Builder.Insert(WideBO);
1067 WideBO->copyIRFlags(NarrowBO);
1071 Instruction *WidenIV::cloneArithmeticIVUser(NarrowIVDefUse DU,
1077 DEBUG(
dbgs() <<
"Cloning arithmetic IVUser: " << *NarrowUse <<
"\n");
1079 unsigned IVOpIdx = (NarrowUse->
getOperand(0) == NarrowDef) ? 0 : 1;
1090 auto GuessNonIVOperand = [&](
bool SignExt) {
1091 const SCEV *WideLHS;
1092 const SCEV *WideRHS;
1094 auto GetExtend = [
this, SignExt](
const SCEV *S,
Type *Ty) {
1096 return SE->getSignExtendExpr(S, Ty);
1097 return SE->getZeroExtendExpr(S, Ty);
1101 WideLHS = SE->getSCEV(WideDef);
1103 WideRHS = GetExtend(NarrowRHS, WideType);
1106 WideLHS = GetExtend(NarrowLHS, WideType);
1107 WideRHS = SE->getSCEV(WideDef);
1111 const SCEV *WideUse =
nullptr;
1118 WideUse = SE->getAddExpr(WideLHS, WideRHS);
1121 case Instruction::Mul:
1122 WideUse = SE->getMulExpr(WideLHS, WideRHS);
1125 case Instruction::UDiv:
1126 WideUse = SE->getUDivExpr(WideLHS, WideRHS);
1129 case Instruction::Sub:
1130 WideUse = SE->getMinusSCEV(WideLHS, WideRHS);
1134 return WideUse == WideAR;
1137 bool SignExtend = getExtendKind(NarrowDef) == SignExtended;
1138 if (!GuessNonIVOperand(SignExtend)) {
1139 SignExtend = !SignExtend;
1140 if (!GuessNonIVOperand(SignExtend))
1146 : createExtendInst(NarrowUse->
getOperand(0), WideType,
1147 SignExtend, NarrowUse);
1150 : createExtendInst(NarrowUse->
getOperand(1), WideType,
1151 SignExtend, NarrowUse);
1153 auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1154 auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1158 Builder.Insert(WideBO);
1159 WideBO->copyIRFlags(NarrowBO);
1163 WidenIV::ExtendKind WidenIV::getExtendKind(
Instruction *I) {
1164 auto It = ExtendKindMap.find(I);
1165 assert(It != ExtendKindMap.end() &&
"Instruction not yet extended!");
1169 const SCEV *WidenIV::getSCEVByOpCode(
const SCEV *LHS,
const SCEV *RHS,
1170 unsigned OpCode)
const {
1172 return SE->getAddExpr(LHS, RHS);
1173 if (OpCode == Instruction::Sub)
1174 return SE->getMinusSCEV(LHS, RHS);
1175 if (OpCode == Instruction::Mul)
1176 return SE->getMulExpr(LHS, RHS);
1186 WidenIV::WidenedRecTy WidenIV::getExtendedOperandRecurrence(NarrowIVDefUse DU) {
1189 const unsigned OpCode = DU.NarrowUse->getOpcode();
1192 OpCode != Instruction::Mul)
1197 const unsigned ExtendOperIdx =
1198 DU.NarrowUse->getOperand(0) == DU.NarrowDef ? 1 : 0;
1199 assert(DU.NarrowUse->getOperand(1-ExtendOperIdx) == DU.NarrowDef &&
"bad DU");
1201 const SCEV *ExtendOperExpr =
nullptr;
1203 cast<OverflowingBinaryOperator>(DU.NarrowUse);
1204 ExtendKind ExtKind = getExtendKind(DU.NarrowDef);
1206 ExtendOperExpr = SE->getSignExtendExpr(
1207 SE->getSCEV(DU.NarrowUse->getOperand(ExtendOperIdx)), WideType);
1209 ExtendOperExpr = SE->getZeroExtendExpr(
1210 SE->getSCEV(DU.NarrowUse->getOperand(ExtendOperIdx)), WideType);
1219 const SCEV *lhs = SE->getSCEV(DU.WideDef);
1220 const SCEV *rhs = ExtendOperExpr;
1224 if (ExtendOperIdx == 0)
1229 if (!AddRec || AddRec->
getLoop() !=
L)
1232 return {AddRec, ExtKind};
1240 WidenIV::WidenedRecTy WidenIV::getWideRecurrence(NarrowIVDefUse DU) {
1241 if (!SE->isSCEVable(DU.NarrowUse->getType()))
1244 const SCEV *NarrowExpr = SE->getSCEV(DU.NarrowUse);
1245 if (SE->getTypeSizeInBits(NarrowExpr->
getType()) >=
1246 SE->getTypeSizeInBits(WideType)) {
1252 const SCEV *WideExpr;
1254 if (DU.NeverNegative) {
1255 WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1256 if (isa<SCEVAddRecExpr>(WideExpr))
1257 ExtKind = SignExtended;
1259 WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1260 ExtKind = ZeroExtended;
1262 }
else if (getExtendKind(DU.NarrowDef) == SignExtended) {
1263 WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1264 ExtKind = SignExtended;
1266 WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1267 ExtKind = ZeroExtended;
1270 if (!AddRec || AddRec->
getLoop() !=
L)
1272 return {AddRec, ExtKind};
1278 DEBUG(
dbgs() <<
"INDVARS: Truncate IV " << *DU.WideDef
1279 <<
" for user " << *DU.NarrowUse <<
"\n");
1283 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
1289 bool WidenIV::widenLoopCompare(NarrowIVDefUse DU) {
1308 bool IsSigned = getExtendKind(DU.NarrowDef) == SignExtended;
1309 if (!(DU.NeverNegative || IsSigned == Cmp->
isSigned()))
1313 unsigned CastWidth = SE->getTypeSizeInBits(Op->
getType());
1314 unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1315 assert (CastWidth <= IVWidth &&
"Unexpected width while widening compare.");
1320 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1323 if (CastWidth < IVWidth) {
1324 Value *ExtOp = createExtendInst(Op, WideType, Cmp->
isSigned(), Cmp);
1325 DU.NarrowUse->replaceUsesOfWith(Op, ExtOp);
1333 assert(ExtendKindMap.count(DU.NarrowDef) &&
1334 "Should already know the kind of extension used to widen NarrowDef");
1337 if (
PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
1338 if (LI->getLoopFor(UsePhi->getParent()) != L) {
1342 if (UsePhi->getNumOperands() != 1)
1348 if (isa<CatchSwitchInst>(UsePhi->getParent()->getTerminator()))
1352 PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() +
".wide",
1354 WidePhi->
addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
1356 Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType());
1358 DeadInsts.emplace_back(UsePhi);
1359 DEBUG(
dbgs() <<
"INDVARS: Widen lcssa phi " << *UsePhi
1360 <<
" to " << *WidePhi <<
"\n");
1368 auto canWidenBySExt = [&]() {
1369 return DU.NeverNegative || getExtendKind(DU.NarrowDef) == SignExtended;
1371 auto canWidenByZExt = [&]() {
1372 return DU.NeverNegative || getExtendKind(DU.NarrowDef) == ZeroExtended;
1376 if ((isa<SExtInst>(DU.NarrowUse) && canWidenBySExt()) ||
1377 (isa<ZExtInst>(DU.NarrowUse) && canWidenByZExt())) {
1378 Value *NewDef = DU.WideDef;
1379 if (DU.NarrowUse->getType() != WideType) {
1380 unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType());
1381 unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1382 if (CastWidth < IVWidth) {
1385 NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType());
1391 DEBUG(
dbgs() <<
"INDVARS: New IV " << *WidePhi
1392 <<
" not wide enough to subsume " << *DU.NarrowUse <<
"\n");
1393 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1394 NewDef = DU.NarrowUse;
1397 if (NewDef != DU.NarrowUse) {
1398 DEBUG(
dbgs() <<
"INDVARS: eliminating " << *DU.NarrowUse
1399 <<
" replaced by " << *DU.WideDef <<
"\n");
1401 DU.NarrowUse->replaceAllUsesWith(NewDef);
1402 DeadInsts.emplace_back(DU.NarrowUse);
1415 WidenedRecTy WideAddRec = getExtendedOperandRecurrence(DU);
1416 if (!WideAddRec.first)
1417 WideAddRec = getWideRecurrence(DU);
1419 assert((WideAddRec.first ==
nullptr) == (WideAddRec.second ==
Unknown));
1420 if (!WideAddRec.first) {
1423 if (widenLoopCompare(DU))
1434 assert(DU.NarrowUse != DU.NarrowUse->getParent()->getTerminator() &&
1435 "SCEV is not expected to evaluate a block terminator");
1440 if (WideAddRec.first == WideIncExpr &&
1444 WideUse = cloneIVUser(DU, WideAddRec.first);
1453 if (WideAddRec.first != SE->getSCEV(WideUse)) {
1454 DEBUG(
dbgs() <<
"Wide use expression mismatch: " << *WideUse
1455 <<
": " << *SE->getSCEV(WideUse) <<
" != " << *WideAddRec.first <<
"\n");
1456 DeadInsts.emplace_back(WideUse);
1460 ExtendKindMap[DU.NarrowUse] = WideAddRec.second;
1468 const SCEV *NarrowSCEV = SE->getSCEV(NarrowDef);
1469 bool NonNegativeDef =
1470 SE->isKnownPredicate(ICmpInst::ICMP_SGE, NarrowSCEV,
1471 SE->getConstant(NarrowSCEV->
getType(), 0));
1476 if (!Widened.insert(NarrowUser).second)
1479 bool NonNegativeUse =
false;
1480 if (!NonNegativeDef) {
1482 if (
auto RangeInfo = getPostIncRangeInfo(NarrowDef, NarrowUser))
1483 NonNegativeUse = RangeInfo->getSignedMin().isNonNegative();
1486 NarrowIVUsers.emplace_back(NarrowDef, NarrowUser, WideDef,
1487 NonNegativeDef || NonNegativeUse);
1507 const SCEV *WideIVExpr = getExtendKind(OrigPhi) == SignExtended
1508 ? SE->getSignExtendExpr(AddRec, WideType)
1509 : SE->getZeroExtendExpr(AddRec, WideType);
1511 assert(SE->getEffectiveSCEVType(WideIVExpr->
getType()) == WideType &&
1512 "Expect the new IV expression to preserve its type");
1516 if (!AddRec || AddRec->
getLoop() !=
L)
1525 "Loop header phi recurrence inputs do not dominate the loop");
1538 calculatePostIncRanges(OrigPhi);
1545 WidePhi = cast<PHINode>(Rewriter.
expandCodeFor(AddRec, WideType, InsertPt));
1554 WideIncExpr = SE->getSCEV(WideInc);
1558 cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
1562 DEBUG(
dbgs() <<
"Wide IV: " << *WidePhi <<
"\n");
1566 assert(Widened.empty() && NarrowIVUsers.empty() &&
"expect initial state" );
1568 Widened.insert(OrigPhi);
1569 pushNarrowIVUsers(OrigPhi, WidePhi);
1571 while (!NarrowIVUsers.empty()) {
1572 NarrowIVDefUse DU = NarrowIVUsers.pop_back_val();
1580 pushNarrowIVUsers(DU.NarrowUse, WideUse);
1583 if (DU.NarrowDef->use_empty())
1584 DeadInsts.emplace_back(DU.NarrowDef);
1591 void WidenIV::calculatePostIncRange(
Instruction *NarrowDef,
1593 using namespace llvm::PatternMatch;
1595 Value *NarrowDefLHS;
1596 const APInt *NarrowDefRHS;
1599 !NarrowDefRHS->isNonNegative())
1602 auto UpdateRangeFromCondition = [&] (
Value *Condition,
1613 auto CmpRHSRange = SE->getSignedRange(SE->getSCEV(CmpRHS));
1614 auto CmpConstrainedLHSRange =
1616 auto NarrowDefRange =
1617 CmpConstrainedLHSRange.addWithNoSignedWrap(*NarrowDefRHS);
1619 updatePostIncRangeInfo(NarrowDef, NarrowUser, NarrowDefRange);
1622 auto UpdateRangeFromGuards = [&](
Instruction *Ctx) {
1627 Ctx->getParent()->rend())) {
1629 if (
match(&I, m_Intrinsic<Intrinsic::experimental_guard>(
m_Value(C))))
1630 UpdateRangeFromCondition(C,
true);
1634 UpdateRangeFromGuards(NarrowUser);
1639 if (!DT->isReachableFromEntry(NarrowUserBB))
1642 for (
auto *DTB = (*DT)[NarrowUserBB]->getIDom();
1644 DTB = DTB->getIDom()) {
1645 auto *BB = DTB->getBlock();
1647 UpdateRangeFromGuards(TI);
1650 if (!BI || !BI->isConditional())
1653 auto *TrueSuccessor = BI->getSuccessor(0);
1654 auto *FalseSuccessor = BI->getSuccessor(1);
1656 auto DominatesNarrowUser = [
this, NarrowUser] (
BasicBlockEdge BBE) {
1657 return BBE.isSingleEdge() &&
1658 DT->dominates(BBE, NarrowUser->
getParent());
1662 UpdateRangeFromCondition(BI->getCondition(),
true);
1665 UpdateRangeFromCondition(BI->getCondition(),
false);
1670 void WidenIV::calculatePostIncRanges(
PHINode *OrigPhi) {
1676 while (!Worklist.
empty()) {
1679 for (Use &U : NarrowDef->
uses()) {
1680 auto *NarrowUser = cast<Instruction>(U.getUser());
1683 auto *NarrowUserLoop = (*LI)[NarrowUser->
getParent()];
1684 if (!NarrowUserLoop || !L->
contains(NarrowUserLoop))
1687 if (!Visited.
insert(NarrowUser).second)
1692 calculatePostIncRange(NarrowDef, NarrowUser);
1707 class IndVarSimplifyVisitor :
public IVVisitor {
1718 : SE(SCEV), TTI(TTI), IVPhi(IV) {
1720 WI.NarrowIV = IVPhi;
1734 void IndVarSimplify::simplifyAndExtend(
Loop *L,
1739 auto *GuardDecl = L->
getBlocks()[0]->getModule()->getFunction(
1741 bool HasGuards = GuardDecl && !GuardDecl->use_empty();
1751 while (!LoopPhis.
empty()) {
1762 IndVarSimplifyVisitor Visitor(CurrIV, SE, TTI, DT);
1766 if (Visitor.WI.WidestNativeType) {
1769 }
while(!LoopPhis.
empty());
1772 WidenIV Widener(WideIVs.
back(), LI, SE, DT, DeadInsts, HasGuards);
1773 if (
PHINode *WidePhi = Widener.createWideIV(Rewriter)) {
1801 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) ||
1802 BackedgeTakenCount->
isZero())
1826 case Instruction::Sub:
1828 case Instruction::GetElementPtr:
1842 if (IncI->
getOpcode() == Instruction::GetElementPtr)
1864 assert(BI &&
"expected exit branch");
1879 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
1913 if (isa<Constant>(V))
1914 return !isa<UndefValue>(V);
1931 if (!Visited.
insert(Op).second)
1957 if (U != Cond && U != IncV)
return false;
1960 if (U != Cond && U != Phi)
return false;
1982 cast<BranchInst>(L->
getExitingBlock()->getTerminator())->getCondition();
1986 const SCEV *BestInit =
nullptr;
1988 assert(LatchBlock &&
"needsLFTR should guarantee a loop latch");
2012 if (!Step || !Step->
isOne())
2035 if (BestPhi && !
AlmostDeadIV(BestPhi, LatchBlock, Cond)) {
2049 else if (PhiWidth <= SE->getTypeSizeInBits(BestPhi->
getType()))
2082 "Computed iteration count is not loop invariant!");
2091 cast<PointerType>(GEPBase->
getType())
2092 ->getElementType())->isOne() &&
2093 "unit stride pointer IV must be i8*");
2096 return Builder.
CreateGEP(
nullptr, GEPBase, GEPOffset,
"lftr.limit");
2109 const SCEV *IVLimit =
nullptr;
2129 "Computed iteration count is not loop invariant!");
2144 Value *IndVarSimplify::
2145 linearFunctionTestReplace(
Loop *L,
2146 const SCEV *BackedgeTakenCount,
2152 Value *CmpIndVar = IndVar;
2153 const SCEV *IVCount = BackedgeTakenCount;
2162 IVCount = SE->getAddExpr(BackedgeTakenCount,
2163 SE->getOne(BackedgeTakenCount->
getType()));
2173 "genLoopLimit missed a cast");
2178 if (L->
contains(BI->getSuccessor(0)))
2179 P = ICmpInst::ICMP_NE;
2181 P = ICmpInst::ICMP_EQ;
2183 DEBUG(
dbgs() <<
"INDVARS: Rewriting loop exit condition to:\n"
2184 <<
" LHS:" << *CmpIndVar <<
'\n'
2186 << (P == ICmpInst::ICMP_NE ?
"!=" :
"==") <<
"\n"
2187 <<
" RHS:\t" << *ExitCnt <<
"\n"
2188 <<
" IVCount:\t" << *IVCount <<
"\n");
2194 if (
auto *Cond = dyn_cast<Instruction>(BI->getCondition()))
2195 Builder.SetCurrentDebugLocation(Cond->getDebugLoc());
2199 unsigned CmpIndVarSize = SE->getTypeSizeInBits(CmpIndVar->
getType());
2200 unsigned ExitCntSize = SE->getTypeSizeInBits(ExitCnt->
getType());
2201 if (CmpIndVarSize > ExitCntSize) {
2202 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
2206 if (isa<SCEVConstant>(ARStart) && isa<SCEVConstant>(IVCount)) {
2207 const APInt &Start = cast<SCEVConstant>(ARStart)->getAPInt();
2208 APInt Count = cast<SCEVConstant>(IVCount)->getAPInt();
2211 if (IVCount != BackedgeTakenCount && Count == 0) {
2216 Count = Count.
zext(CmpIndVarSize);
2218 if (cast<SCEVConstant>(ARStep)->getValue()->isNegative())
2219 NewLimit = Start - Count;
2221 NewLimit = Start + Count;
2222 ExitCnt = ConstantInt::get(CmpIndVar->
getType(), NewLimit);
2224 DEBUG(
dbgs() <<
" Widen RHS:\t" << *ExitCnt <<
"\n");
2230 bool Extended =
false;
2231 const SCEV *IV = SE->getSCEV(CmpIndVar);
2232 const SCEV *ZExtTrunc =
2233 SE->getZeroExtendExpr(SE->getTruncateExpr(SE->getSCEV(CmpIndVar),
2237 if (ZExtTrunc == IV) {
2239 ExitCnt = Builder.CreateZExt(ExitCnt, IndVar->
getType(),
2242 const SCEV *SExtTrunc =
2243 SE->getSignExtendExpr(SE->getTruncateExpr(SE->getSCEV(CmpIndVar),
2246 if (SExtTrunc == IV) {
2248 ExitCnt = Builder.CreateSExt(ExitCnt, IndVar->
getType(),
2254 CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->
getType(),
2258 Value *Cond = Builder.CreateICmp(P, CmpIndVar, ExitCnt,
"exitcond");
2259 Value *OrigCond = BI->getCondition();
2265 BI->setCondition(Cond);
2266 DeadInsts.push_back(OrigCond);
2280 void IndVarSimplify::sinkUnusedInvariants(
Loop *L) {
2282 if (!ExitBlock)
return;
2285 if (!Preheader)
return;
2289 while (I != Preheader->
begin()) {
2292 if (isa<PHINode>(I))
2305 if (isa<DbgInfoIntrinsic>(I))
2316 if (isa<AllocaInst>(I))
2321 bool UsedInLoop =
false;
2322 for (Use &U : I->
uses()) {
2325 if (
PHINode *P = dyn_cast<PHINode>(User)) {
2327 PHINode::getIncomingValueNumForOperand(U.getOperandNo());
2328 UseBB = P->getIncomingBlock(i);
2330 if (UseBB == Preheader || L->
contains(UseBB)) {
2344 if (I != Preheader->
begin()) {
2348 }
while (isa<DbgInfoIntrinsic>(I) && I != Preheader->
begin());
2350 if (isa<DbgInfoIntrinsic>(I) && I == Preheader->
begin())
2366 bool IndVarSimplify::run(
Loop *L) {
2369 "LCSSA required to run indvars!");
2384 rewriteNonIntegerIVs(L);
2386 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
2401 simplifyAndExtend(L, Rewriter, LI);
2410 !isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2411 rewriteLoopExitValues(L, Rewriter);
2431 (
void)linearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
2442 while (!DeadInsts.empty())
2444 dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
2451 sinkUnusedInvariants(L);
2456 rewriteFirstIterationLoopExitValues(L);
2463 "Indvars did not preserve LCSSA!");
2468 if (
VerifyIndvars && !isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
2470 const SCEV *NewBECount = SE->getBackedgeTakenCount(L);
2471 if (SE->getTypeSizeInBits(BackedgeTakenCount->
getType()) <
2472 SE->getTypeSizeInBits(NewBECount->
getType()))
2473 NewBECount = SE->getTruncateOrNoop(NewBECount,
2474 BackedgeTakenCount->
getType());
2476 BackedgeTakenCount = SE->getTruncateOrNoop(BackedgeTakenCount,
2478 assert(BackedgeTakenCount == NewBECount &&
"indvars must preserve SCEV");
2491 IndVarSimplify IVS(&AR.
LI, &AR.
SE, &AR.
DT, DL, &AR.
TLI, &AR.
TTI);
2500 struct IndVarSimplifyLegacyPass :
public LoopPass {
2510 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2511 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2512 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2513 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
2514 auto *TLI = TLIP ? &TLIP->getTLI() :
nullptr;
2515 auto *TTIP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
2516 auto *TTI = TTIP ? &TTIP->getTTI(*L->
getHeader()->getParent()) :
nullptr;
2519 IndVarSimplify IVS(LI, SE, DT, DL, TLI, TTI);
2532 "Induction Variable Simplification",
false,
false)
2538 return new IndVarSimplifyLegacyPass();
Pass interface - Implemented by all 'passes'.
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. ...
bool hoistIVInc(Instruction *IncV, Instruction *InsertPos)
Utility for hoisting an IV increment.
Induction Variable Simplification
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
iterator_range< use_iterator > uses()
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...
Value * getExactExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Try to find existing LLVM IR value for S available at the point At.
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
static bool isLoopInvariant(Value *V, const Loop *L, const DominatorTree *DT)
Perform a quick domtree based check for loop invariance assuming that V is used within the loop...
bool isOne() const
Return true if the expression is a constant one.
STATISTIC(NumFunctions,"Total number of functions")
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
This is the interface for a simple mod/ref and alias analysis over globals.
bool isZero() const
Return true if the expression is a constant zero.
unsigned getNumOperands() const
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
The main scalar evolution driver.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
bool isHighCostExpansion(const SCEV *Expr, Loop *L, const Instruction *At=nullptr)
Return true for expressions that may incur non-trivial cost to evaluate at runtime.
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
bool isSigned() const
Determine if this instruction is using a signed comparison.
0 1 0 0 True if ordered and less than
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
1 1 1 0 True if unordered or not equal
void setDebugType(const char *s)
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LoopT * getParentLoop() const
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
static cl::opt< bool > UsePostIncrementRanges("indvars-post-increment-ranges", cl::Hidden, cl::desc("Use post increment control-dependent ranges in IndVarSimplify"), cl::init(true))
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
static bool hasConcreteDef(Value *V)
Return true if the given value is concrete.
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
BlockT * getHeader() const
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...
iterator begin()
Instruction iterator methods.
1 0 0 1 True if unordered or equal
bool match(Val *V, const Pattern &P)
static Value * genLoopLimit(PHINode *IndVar, const SCEV *IVCount, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE)
Help linearFunctionTestReplace by generating a value that holds the RHS of the new loop test...
Interface for visiting interesting IV users that are recognized but not simplified by this utility...
#define INITIALIZE_PASS_DEPENDENCY(depName)
This is the interface for a SCEV-based alias analysis.
INITIALIZE_PASS_BEGIN(IndVarSimplifyLegacyPass,"indvars","Induction Variable Simplification", false, false) INITIALIZE_PASS_END(IndVarSimplifyLegacyPass
This is the base class for all instructions that perform data casts.
A Use represents the edge between a Value definition and its users.
static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal)
Convert APF to an integer, if possible.
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...
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, SmallVectorImpl< WeakVH > &Dead, IVVisitor *V=nullptr)
simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution t...
static StringRef getName(Value *V)
0 1 0 1 True if ordered and less than or equal
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
LLVM_NODISCARD bool empty() const
user_iterator_impl< User > user_iterator
int getMaxValue(MCInstrInfo const &MCII, MCInst const &MCI)
bool mayReadFromMemory() const
Return true if this instruction may read memory.
This node represents a polynomial recurrence on the trip count of the specified loop.
Value handle that is nullable, but tries to track the Value.
This instruction compares its operands according to the predicate given to the constructor.
static Instruction * getInsertPointForUses(Instruction *User, Value *Def, DominatorTree *DT, LoopInfo *LI)
Determine the insertion point for this user.
static cl::opt< bool > VerifyIndvars("verify-indvars", cl::Hidden, cl::desc("Verify the ScalarEvolution result after running indvars"))
BasicBlock * getSuccessor(unsigned i) const
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Examine each PHI in the given block and delete it if it is dead.
const SCEV * getSizeOfExpr(Type *IntTy, Type *AllocTy)
Return an expression for sizeof AllocTy that is type IntTy.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
void takeName(Value *V)
Transfer the name from V to this value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
iterator_range< block_iterator > blocks() const
void initializeIndVarSimplifyLegacyPassPass(PassRegistry &)
void clearInsertPoint()
Clear the current insertion point.
void clear()
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression ...
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
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...
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
A set of analyses that are preserved following a run of a transformation pass.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
static bool canExpandBackedgeTakenCount(Loop *L, ScalarEvolution *SE, SCEVExpander &Rewriter)
Return true if this loop's backedge taken count expression can be safely and cheaply expanded into an...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Type * getType() const
Return the LLVM type of this SCEV expression.
Conditional or Unconditional Branch instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
ConstantFP - Floating Point Values [float, double].
static void visitIVCast(CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const TargetTransformInfo *TTI)
Update information about the induction variable that is extended by this sign or zero extend operatio...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
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.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned getBitWidth() const
Return the number of bits in the APInt.
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Value * getOperand(unsigned i) const
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
self_iterator getIterator()
Class to represent integer types.
Predicate getPredicate() const
Return the predicate for this instruction.
opStatus convertToInteger(integerPart *Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
bool isPointerTy() const
True if this is an instance of PointerType.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
Return all unique successor blocks of this loop.
LLVMContext & getContext() const
All values hold a context through their type.
static ICmpInst * getLoopTest(Loop *L)
Return the compare guarding the loop latch, or NULL for unrecognized tests.
1 1 0 1 True if unordered, less than, or equal
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
TargetTransformInfo & TTI
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
bool isConditional() const
0 0 1 0 True if ordered and greater than
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
constexpr bool isInt< 32 >(int64_t x)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty)
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
1 1 0 0 True if unordered or less than
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.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Provides information about what library functions are available for the current target.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
This class represents a range of values.
LLVM_NODISCARD T pop_back_val()
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 PHINode * getLoopPhiForCounter(Value *IncV, Loop *L, DominatorTree *DT)
Return the loop header phi IFF IncV adds a loop invariant value to the phi.
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...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Value handle that asserts if the Value is deleted.
static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT, LoopInfo *LI)
This IV user cannot be widen.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
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.
Value * CreateGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Class for arbitrary precision integers.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
iterator_range< user_iterator > users()
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.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Virtual Register Rewriter
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Value * getCondition() const
void emplace_back(ArgTypes &&...Args)
This class represents an analyzed expression in the program.
static IntegerType * getInt32Ty(LLVMContext &C)
virtual void visitCast(CastInst *Cast)=0
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
bool hasOneUse() const
Return true if there is exactly one user of this value.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
0 1 1 0 True if ordered and operands are unequal
static bool needsLFTR(Loop *L, DominatorTree *DT)
linearFunctionTestReplace policy.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Loop * getLoop() const
1 0 1 0 True if unordered or greater than
const APFloat & getValueAPF() const
const SCEV * getBackedgeTakenCount(const Loop *L)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
This class represents a cast from signed integer to floating point.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
unsigned getSCEVType() const
static cl::opt< ReplaceExitVal > ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl,"never","never replace exit value"), clEnumValN(OnlyCheapRepl,"cheap","only replace exit value when the cost is cheap"), clEnumValN(AlwaysRepl,"always","always replace exit value whenever possible")))
0 0 0 1 True if ordered and equal
Module * getParent()
Get the module that this global value is contained inside of...
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
LLVM Value Representation.
1 0 1 1 True if unordered, greater than, or equal
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static bool AlmostDeadIV(PHINode *Phi, BasicBlock *LatchBlock, Value *Cond)
Return true if this IV has any uses other than the (soon to be rewritten) loop exit test...
static const Function * getParent(const Value *V)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
void disableCanonicalMode()
Disable the behavior of expanding expressions in canonical form rather than in a more literal form...
APInt zext(unsigned width) const
Zero extend to a new width.
A container for analyses that lazily runs them and caches their results.
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU...
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
void setIncomingValue(unsigned i, Value *V)
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
0 0 1 1 True if ordered and greater than or equal
static bool hasConcreteDefImpl(Value *V, SmallPtrSetImpl< Value * > &Visited, unsigned Depth)
Recursive helper for hasConcreteDef().
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
const BasicBlock * getParent() const
Pass * createIndVarSimplifyPass()
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property...
This class represents a constant integer value.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
static PHINode * FindLoopCounter(Loop *L, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT)
Find an affine IV in canonical form.