32 using namespace PatternMatch;
56 if (U->getType() == Ty)
57 if (
CastInst *CI = dyn_cast<CastInst>(U))
58 if (CI->getOpcode() ==
Op) {
68 CI->replaceAllUsesWith(Ret);
83 assert(SE.DT.dominates(Ret, &*BIP));
85 rememberInstruction(Ret);
92 if (
auto *II = dyn_cast<InvokeInst>(I))
93 IP = II->getNormalDest()->begin();
95 while (isa<PHINode>(IP))
98 if (isa<FuncletPadInst>(IP) || isa<LandingPadInst>(IP)) {
100 }
else if (isa<CatchSwitchInst>(IP)) {
103 assert(!IP->isEHPad() &&
"unexpected eh pad!");
114 assert((Op == Instruction::BitCast ||
115 Op == Instruction::PtrToInt ||
116 Op == Instruction::IntToPtr) &&
117 "InsertNoopCastOfTo cannot perform non-noop casts!");
118 assert(SE.getTypeSizeInBits(V->
getType()) == SE.getTypeSizeInBits(Ty) &&
119 "InsertNoopCastOfTo cannot change sizes!");
122 if (Op == Instruction::BitCast) {
125 if (
CastInst *CI = dyn_cast<CastInst>(V)) {
126 if (CI->getOperand(0)->getType() == Ty)
127 return CI->getOperand(0);
131 if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
132 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->
getType())) {
133 if (
CastInst *CI = dyn_cast<CastInst>(V))
134 if ((CI->getOpcode() == Instruction::PtrToInt ||
135 CI->getOpcode() == Instruction::IntToPtr) &&
136 SE.getTypeSizeInBits(CI->getType()) ==
137 SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
138 return CI->getOperand(0);
140 if ((
CE->getOpcode() == Instruction::PtrToInt ||
141 CE->getOpcode() == Instruction::IntToPtr) &&
142 SE.getTypeSizeInBits(
CE->getType()) ==
143 SE.getTypeSizeInBits(
CE->getOperand(0)->getType()))
144 return CE->getOperand(0);
153 if (
Argument *
A = dyn_cast<Argument>(V)) {
155 while ((isa<BitCastInst>(IP) &&
156 isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) &&
157 cast<BitCastInst>(IP)->getOperand(0) !=
A) ||
158 isa<DbgInfoIntrinsic>(IP))
160 return ReuseOrCreateCast(
A, Ty, Op, IP);
166 return ReuseOrCreateCast(I, Ty, Op, IP);
174 if (
Constant *CLHS = dyn_cast<Constant>(LHS))
175 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
179 unsigned ScanLimit = 6;
183 if (IP != BlockBegin) {
185 for (; ScanLimit; --IP, --ScanLimit) {
188 if (isa<DbgInfoIntrinsic>(IP))
190 if (IP->getOpcode() == (
unsigned)Opcode && IP->getOperand(0) == LHS &&
191 IP->getOperand(1) == RHS)
193 if (IP == BlockBegin)
break;
198 DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
199 SCEVInsertPointGuard Guard(Builder,
this);
202 while (
const Loop *
L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
203 if (!
L->isLoopInvariant(LHS) || !
L->isLoopInvariant(RHS))
break;
205 if (!Preheader)
break;
212 Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS));
214 rememberInstruction(BO);
263 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
267 if (
const SCEVConstant *
C = dyn_cast<SCEVConstant>(M->getOperand(0)))
268 if (!
C->getAPInt().srem(FC->
getAPInt())) {
278 const SCEV *Step =
A->getStepRecurrence(SE);
284 const SCEV *Start =
A->getStart();
302 unsigned NumAddRecs = 0;
303 for (
unsigned i = Ops.
size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i)
309 const SCEV *Sum = NoAddRecs.empty() ?
317 else if (!Sum->isZero())
320 Ops.
append(AddRecs.begin(), AddRecs.end());
333 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
335 const SCEV *Start =
A->getStart();
336 if (Start->
isZero())
break;
339 A->getStepRecurrence(SE),
345 e +=
Add->getNumOperands();
350 if (!AddRecs.
empty()) {
385 Value *SCEVExpander::expandAddToGEP(
const SCEV *
const *op_begin,
386 const SCEV *
const *op_end,
391 Type *ElTy = OriginalElTy;
394 bool AnyNonZeroIndices =
false;
412 const SCEV *ElSize = SE.getSizeOfExpr(IntPtrTy, ElTy);
415 for (
const SCEV *Op : Ops) {
416 const SCEV *Remainder = SE.getConstant(Ty, 0);
422 AnyNonZeroIndices =
true;
430 if (!ScaledOps.
empty()) {
443 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
447 while (
StructType *STy = dyn_cast<StructType>(ElTy)) {
448 bool FoundFieldNo =
false;
450 if (STy->getNumElements() == 0)
break;
456 if (SE.getTypeSizeInBits(
C->getType()) <= 64) {
458 uint64_t FullOffset =
C->getValue()->getZExtValue();
463 ElTy = STy->getTypeAtIndex(ElIdx);
466 AnyNonZeroIndices =
true;
474 ElTy = STy->getTypeAtIndex(0u);
480 if (
ArrayType *ATy = dyn_cast<ArrayType>(ElTy))
481 ElTy = ATy->getElementType();
489 if (!AnyNonZeroIndices) {
491 V = InsertNoopCastOfTo(V,
494 assert(!isa<Instruction>(V) ||
495 SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint()));
498 Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
501 if (
Constant *CLHS = dyn_cast<Constant>(V))
502 if (
Constant *CRHS = dyn_cast<Constant>(Idx))
507 unsigned ScanLimit = 6;
511 if (IP != BlockBegin) {
513 for (; ScanLimit; --IP, --ScanLimit) {
516 if (isa<DbgInfoIntrinsic>(IP))
518 if (IP->getOpcode() == Instruction::GetElementPtr &&
519 IP->getOperand(0) == V && IP->getOperand(1) == Idx)
521 if (IP == BlockBegin)
break;
526 SCEVInsertPointGuard Guard(Builder,
this);
529 while (
const Loop *
L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
530 if (!
L->isLoopInvariant(V) || !
L->isLoopInvariant(Idx))
break;
532 if (!Preheader)
break;
539 Value *
GEP = Builder.CreateGEP(Builder.getInt8Ty(), V, Idx,
"uglygep");
540 rememberInstruction(GEP);
546 SCEVInsertPointGuard Guard(Builder,
this);
549 while (
const Loop *
L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
550 if (!
L->isLoopInvariant(V))
break;
552 bool AnyIndexNotLoopInvariant =
any_of(
553 GepIndices, [
L](
Value *Op) {
return !
L->isLoopInvariant(Op); });
555 if (AnyIndexNotLoopInvariant)
559 if (!Preheader)
break;
570 Casted = InsertNoopCastOfTo(Casted, PTy);
571 Value *GEP = Builder.CreateGEP(OriginalElTy, Casted, GepIndices,
"scevgep");
572 Ops.push_back(SE.getUnknown(GEP));
573 rememberInstruction(GEP);
576 return expand(SE.getAddExpr(Ops));
595 const Loop *SCEVExpander::getRelevantLoop(
const SCEV *S) {
597 auto Pair = RelevantLoops.insert(std::make_pair(S,
nullptr));
599 return Pair.first->second;
601 if (isa<SCEVConstant>(S))
604 if (
const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
605 if (
const Instruction *I = dyn_cast<Instruction>(U->getValue()))
606 return Pair.first->second = SE.LI.getLoopFor(I->
getParent());
611 const Loop *
L =
nullptr;
614 for (
const SCEV *Op :
N->operands())
616 return RelevantLoops[
N] =
L;
619 const Loop *Result = getRelevantLoop(
C->getOperand());
620 return RelevantLoops[
C] = Result;
624 getRelevantLoop(
D->getLHS()), getRelevantLoop(
D->getRHS()), SE.DT);
625 return RelevantLoops[
D] = Result;
638 bool operator()(std::pair<const Loop *, const SCEV *> LHS,
639 std::pair<const Loop *, const SCEV *> RHS)
const {
641 if (LHS.second->getType()->isPointerTy() !=
642 RHS.second->getType()->isPointerTy())
643 return LHS.second->getType()->isPointerTy();
646 if (LHS.first != RHS.first)
652 if (LHS.second->isNonConstantNegative()) {
653 if (!RHS.second->isNonConstantNegative())
655 }
else if (RHS.second->isNonConstantNegative())
673 for (std::reverse_iterator<SCEVAddExpr::op_iterator>
I(S->
op_end()),
675 OpsAndLoops.
push_back(std::make_pair(getRelevantLoop(*I), *I));
679 std::stable_sort(OpsAndLoops.
begin(), OpsAndLoops.
end(), LoopCompare(SE.DT));
683 Value *Sum =
nullptr;
684 for (
auto I = OpsAndLoops.
begin(),
E = OpsAndLoops.
end(); I !=
E;) {
685 const Loop *CurLoop = I->first;
686 const SCEV *Op = I->second;
691 }
else if (
PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) {
695 for (; I !=
E && I->first == CurLoop; ++
I) {
698 const SCEV *
X = I->second;
699 if (
const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X))
700 if (!isa<Instruction>(U->getValue()))
701 X = SE.getSCEV(U->getValue());
704 Sum = expandAddToGEP(NewOps.
begin(), NewOps.
end(), PTy, Ty, Sum);
710 NewOps.
push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) :
712 for (++I; I !=
E && I->first == CurLoop; ++
I)
714 Sum = expandAddToGEP(NewOps.
begin(), NewOps.
end(), PTy, Ty,
expand(Op));
717 Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty);
718 Sum = InsertNoopCastOfTo(Sum, Ty);
719 Sum = InsertBinop(Instruction::Sub, Sum, W);
723 Value *W = expandCodeFor(Op, Ty);
724 Sum = InsertNoopCastOfTo(Sum, Ty);
726 if (isa<Constant>(Sum))
std::swap(Sum, W);
741 for (std::reverse_iterator<SCEVMulExpr::op_iterator>
I(S->
op_end()),
743 OpsAndLoops.
push_back(std::make_pair(getRelevantLoop(*I), *I));
746 std::stable_sort(OpsAndLoops.
begin(), OpsAndLoops.
end(), LoopCompare(SE.DT));
750 Value *Prod =
nullptr;
751 auto I = OpsAndLoops.
begin();
756 const auto ExpandOpBinPowN = [
this, &
I, &OpsAndLoops, &Ty]() {
761 const uint64_t MaxExponent = UINT64_MAX >> 1;
766 while (
E != OpsAndLoops.end() && *I == *
E && Exponent != MaxExponent) {
770 assert(Exponent > 0 &&
"Trying to calculate a zeroth exponent of operand?");
774 Value *
P = expandCodeFor(I->second, Ty);
775 Value *Result =
nullptr;
778 for (uint64_t BinExp = 2; BinExp <= Exponent; BinExp <<= 1) {
779 P = InsertBinop(Instruction::Mul, P, P);
780 if (Exponent & BinExp)
781 Result = Result ? InsertBinop(Instruction::Mul, Result, P) : P;
785 assert(Result &&
"Nothing was expanded?");
789 while (I != OpsAndLoops.end()) {
792 Prod = ExpandOpBinPowN();
793 }
else if (I->second->isAllOnesValue()) {
795 Prod = InsertNoopCastOfTo(Prod, Ty);
800 Value *W = ExpandOpBinPowN();
801 Prod = InsertNoopCastOfTo(Prod, Ty);
803 if (isa<Constant>(Prod))
std::swap(Prod, W);
808 Prod = InsertBinop(Instruction::Shl, Prod,
811 Prod = InsertBinop(Instruction::Mul, Prod, W);
824 const APInt &RHS =
SC->getAPInt();
826 return InsertBinop(Instruction::LShr, LHS,
831 return InsertBinop(Instruction::UDiv, LHS, RHS);
840 Base =
A->getStart();
843 A->getStepRecurrence(SE),
847 if (
const SCEVAddExpr *
A = dyn_cast<SCEVAddExpr>(Base)) {
848 Base =
A->getOperand(
A->getNumOperands()-1);
850 NewAddOps.
back() = Rest;
861 (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV)))
866 if (L == IVIncInsertLoop) {
868 OE = IncV->
op_end(); OI != OE; ++OI)
869 if (
Instruction *OInst = dyn_cast<Instruction>(OI))
870 if (!SE.DT.dominates(OInst, IVIncInsertPos))
884 return isNormalAddRecExprPHI(PN, IncV, L);
899 if (IncV == InsertPos)
907 case Instruction::Sub: {
909 if (!OInst || SE.DT.dominates(OInst, InsertPos))
910 return dyn_cast<Instruction>(IncV->
getOperand(0));
913 case Instruction::BitCast:
915 case Instruction::GetElementPtr:
917 if (isa<Constant>(*I))
919 if (
Instruction *OInst = dyn_cast<Instruction>(*I)) {
920 if (!SE.DT.dominates(OInst, InsertPos))
933 unsigned AS = cast<PointerType>(IncV->
getType())->getAddressSpace();
950 void SCEVExpander::fixupInsertPoints(
Instruction *I) {
953 if (Builder.GetInsertPoint() == It)
954 Builder.SetInsertPoint(&*NewInsertPt);
955 for (
auto *InsertPtGuard : InsertPointGuards)
956 if (InsertPtGuard->GetInsertPoint() == It)
957 InsertPtGuard->SetInsertPoint(NewInsertPt);
964 if (SE.DT.dominates(IncV, InsertPos))
969 if (isa<PHINode>(InsertPos) ||
973 if (!SE.LI.movementPreservesLCSSAForm(IncV, InsertPos))
979 Instruction *Oper = getIVIncOperand(IncV, InsertPos,
true);
985 if (SE.DT.dominates(IncV, InsertPos))
988 for (
auto I = IVIncs.
rbegin(),
E = IVIncs.
rend(); I !=
E; ++
I) {
989 fixupInsertPoints(*I);
990 (*I)->moveBefore(InsertPos);
1020 PointerType *GEPPtrTy = cast<PointerType>(ExpandTy);
1023 if (!isa<ConstantInt>(StepV))
1026 const SCEV *
const StepArray[1] = { SE.getSCEV(StepV) };
1027 IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN);
1029 IncV = Builder.CreateBitCast(IncV, PN->
getType());
1030 rememberInstruction(IncV);
1033 IncV = useSubtract ?
1034 Builder.CreateSub(PN, StepV,
Twine(IVName) +
".iv.next") :
1035 Builder.CreateAdd(PN, StepV,
Twine(IVName) +
".iv.next");
1036 rememberInstruction(IncV);
1050 fixupInsertPoints(InstToHoist);
1053 InstToHoist = cast<Instruction>(InstToHoist->
getOperand(0));
1054 }
while (InstToHoist != LoopPhi);
1075 if (Phi == Requested) {
1091 if (!isa<IntegerType>(AR->
getType()))
1099 const SCEV *ExtendAfterOp =
1101 return ExtendAfterOp == OpAfterExtend;
1105 if (!isa<IntegerType>(AR->
getType()))
1113 const SCEV *ExtendAfterOp =
1115 return ExtendAfterOp == OpAfterExtend;
1122 SCEVExpander::getAddRecExprPHILiterally(
const SCEVAddRecExpr *Normalized,
1128 assert((!IVIncInsertLoop||IVIncInsertPos) &&
"Uninitialized insert position");
1133 PHINode *AddRecPhiMatch =
nullptr;
1140 bool TryNonMatchingSCEV =
1142 SE.DT.properlyDominates(LatchBlock, IVIncInsertLoop->getHeader());
1146 if (!PN || !SE.isSCEVable(PN->
getType()))
1153 bool IsMatchingSCEV = PhiSCEV == Normalized;
1157 if (!IsMatchingSCEV && !TryNonMatchingSCEV)
1165 if (!isExpandedAddRecExprPHI(PN, TempIncV, L))
1167 if (L == IVIncInsertLoop && !hoistIVInc(TempIncV, IVIncInsertPos))
1170 if (!isNormalAddRecExprPHI(PN, TempIncV, L))
1175 if (IsMatchingSCEV) {
1179 AddRecPhiMatch = PN;
1185 if ((!TruncTy || InvertStep) &&
1189 AddRecPhiMatch = PN;
1191 TruncTy = SE.getEffectiveSCEVType(Normalized->
getType());
1195 if (AddRecPhiMatch) {
1198 if (L == IVIncInsertLoop)
1199 hoistBeforePos(&SE.DT, IncV, IVIncInsertPos, AddRecPhiMatch);
1203 InsertedValues.insert(AddRecPhiMatch);
1205 rememberInstruction(IncV);
1206 return AddRecPhiMatch;
1211 SCEVInsertPointGuard Guard(Builder,
this);
1221 PostIncLoops.
clear();
1225 "Can't expand add recurrences without a loop preheader!");
1226 Value *StartV = expandCodeFor(Normalized->
getStart(), ExpandTy,
1231 assert(!isa<Instruction>(StartV) ||
1232 SE.DT.properlyDominates(cast<Instruction>(StartV)->getParent(),
1243 Step = SE.getNegativeSCEV(Step);
1245 Value *StepV = expandCodeFor(Step, IntTy, &L->
getHeader()->front());
1250 bool IncrementIsNUW = !useSubtract &&
IsIncrementNUW(SE, Normalized);
1251 bool IncrementIsNSW = !useSubtract &&
IsIncrementNSW(SE, Normalized);
1255 Builder.SetInsertPoint(Header, Header->
begin());
1257 PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE),
1258 Twine(IVName) +
".iv");
1259 rememberInstruction(PN);
1276 Builder.SetInsertPoint(InsertPos);
1277 Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1279 if (isa<OverflowingBinaryOperator>(IncV)) {
1281 cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap();
1283 cast<BinaryOperator>(IncV)->setHasNoSignedWrap();
1290 PostIncLoops = SavedPostIncLoops;
1293 InsertedValues.
insert(PN);
1300 Type *IntTy = SE.getEffectiveSCEVType(STy);
1306 if (PostIncLoops.count(L)) {
1314 const SCEV *PostLoopOffset =
nullptr;
1315 if (!SE.properlyDominates(Start, L->
getHeader())) {
1316 PostLoopOffset = Start;
1317 Start = SE.getConstant(Normalized->
getType(), 0);
1318 Normalized = cast<SCEVAddRecExpr>(
1326 const SCEV *PostLoopScale =
nullptr;
1327 if (!SE.dominates(Step, L->
getHeader())) {
1328 PostLoopScale = Step;
1329 Step = SE.getConstant(Normalized->
getType(), 1);
1333 assert(!PostLoopOffset &&
"Start not-null but PostLoopOffset set?");
1334 PostLoopOffset = Start;
1335 Start = SE.getConstant(Normalized->
getType(), 0);
1338 cast<SCEVAddRecExpr>(SE.getAddRecExpr(
1339 Start, Step, Normalized->
getLoop(),
1345 Type *ExpandTy = PostLoopScale ? IntTy : STy;
1348 Type *AddRecPHIExpandTy =
1353 Type *TruncTy =
nullptr;
1354 bool InvertStep =
false;
1355 PHINode *PN = getAddRecExprPHILiterally(Normalized, L, AddRecPHIExpandTy,
1356 IntTy, TruncTy, InvertStep);
1360 if (!PostIncLoops.count(L))
1365 assert(LatchBlock &&
"PostInc mode requires a unique loop latch!");
1371 if (isa<Instruction>(Result) &&
1372 !SE.DT.dominates(cast<Instruction>(Result),
1373 &*Builder.GetInsertPoint())) {
1386 Step = SE.getNegativeSCEV(Step);
1390 SCEVInsertPointGuard Guard(Builder,
this);
1391 StepV = expandCodeFor(Step, IntTy, &L->
getHeader()->front());
1393 Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1402 if (ResTy != SE.getEffectiveSCEVType(ResTy))
1403 Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy));
1405 if (TruncTy != Result->
getType()) {
1406 Result = Builder.CreateTrunc(Result, TruncTy);
1407 rememberInstruction(Result);
1411 Result = Builder.CreateSub(expandCodeFor(Normalized->
getStart(), TruncTy),
1413 rememberInstruction(Result);
1418 if (PostLoopScale) {
1419 assert(S->
isAffine() &&
"Can't linearly scale non-affine recurrences.");
1420 Result = InsertNoopCastOfTo(Result, IntTy);
1421 Result = Builder.CreateMul(Result,
1422 expandCodeFor(PostLoopScale, IntTy));
1423 rememberInstruction(Result);
1427 if (PostLoopOffset) {
1428 if (
PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) {
1429 if (Result->getType()->isIntegerTy()) {
1430 Value *
Base = expandCodeFor(PostLoopOffset, ExpandTy);
1431 const SCEV *
const OffsetArray[1] = {SE.getUnknown(Result)};
1432 Result = expandAddToGEP(OffsetArray, OffsetArray + 1, PTy, IntTy, Base);
1434 const SCEV *
const OffsetArray[1] = {PostLoopOffset};
1436 expandAddToGEP(OffsetArray, OffsetArray + 1, PTy, IntTy, Result);
1439 Result = InsertNoopCastOfTo(Result, IntTy);
1440 Result = Builder.CreateAdd(Result,
1441 expandCodeFor(PostLoopOffset, IntTy));
1442 rememberInstruction(Result);
1450 if (!CanonicalMode)
return expandAddRecExprLiterally(S);
1456 PHINode *CanonicalIV =
nullptr;
1458 if (SE.getTypeSizeInBits(PN->
getType()) >= SE.getTypeSizeInBits(Ty))
1464 SE.getTypeSizeInBits(CanonicalIV->
getType()) >
1465 SE.getTypeSizeInBits(Ty)) {
1468 NewOps[i] = SE.getAnyExtendExpr(S->
op_begin()[i], CanonicalIV->
getType());
1473 V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty),
nullptr,
1481 NewOps[0] = SE.getConstant(Ty, 0);
1482 const SCEV *Rest = SE.getAddRecExpr(NewOps, L,
1488 const SCEV *RestArray[1] = { Rest };
1496 if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
1498 assert(StartV->
getType() == PTy &&
"Pointer type mismatch for GEP!");
1499 return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
1508 const SCEV *AddExprRHS = SE.getUnknown(
expand(Rest));
1509 return expand(SE.getAddExpr(AddExprLHS, AddExprRHS));
1520 rememberInstruction(CanonicalIV);
1526 if (!PredSeen.
insert(HP).second) {
1540 rememberInstruction(Add);
1550 assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->
getType()) &&
1551 "IVs with types different from the canonical IV should "
1552 "already have been handled!");
1561 expand(SE.getTruncateOrNoop(
1562 SE.getMulExpr(SE.getUnknown(CanonicalIV),
1571 const SCEV *IH = SE.getUnknown(CanonicalIV);
1574 const SCEV *NewS = S;
1575 const SCEV *
Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->
getType());
1576 if (isa<SCEVAddRecExpr>(Ext))
1579 const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
1583 const SCEV *
T = SE.getTruncateOrNoop(V, Ty);
1591 Value *I = Builder.CreateTrunc(V, Ty);
1592 rememberInstruction(I);
1600 Value *I = Builder.CreateZExt(V, Ty);
1601 rememberInstruction(I);
1609 Value *I = Builder.CreateSExt(V, Ty);
1610 rememberInstruction(I);
1621 Ty = SE.getEffectiveSCEVType(Ty);
1622 LHS = InsertNoopCastOfTo(LHS, Ty);
1625 Value *ICmp = Builder.CreateICmpSGT(LHS, RHS);
1626 rememberInstruction(ICmp);
1627 Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS,
"smax");
1628 rememberInstruction(Sel);
1634 LHS = InsertNoopCastOfTo(LHS, S->
getType());
1645 Ty = SE.getEffectiveSCEVType(Ty);
1646 LHS = InsertNoopCastOfTo(LHS, Ty);
1649 Value *ICmp = Builder.CreateICmpUGT(LHS, RHS);
1650 rememberInstruction(ICmp);
1651 Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS,
"umax");
1652 rememberInstruction(Sel);
1658 LHS = InsertNoopCastOfTo(LHS, S->
getType());
1665 return expandCodeFor(SH, Ty);
1672 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->
getType()) &&
1673 "non-trivial casts should be done with the SCEVs directly!");
1674 V = InsertNoopCastOfTo(V, Ty);
1679 ScalarEvolution::ValueOffsetPair
1680 SCEVExpander::FindValueInExprValueMap(
const SCEV *S,
1685 if (CanonicalMode || !SE.containsAddRecurrence(S)) {
1691 for (
auto const &VOPair : *Set) {
1692 Value *V = VOPair.first;
1695 if (V && isa<Instruction>(V) && (EntInst = cast<Instruction>(V)) &&
1698 SE.DT.dominates(EntInst, InsertPt) &&
1699 (SE.LI.getLoopFor(EntInst->
getParent()) ==
nullptr ||
1705 return {
nullptr,
nullptr};
1714 Value *SCEVExpander::expand(
const SCEV *S) {
1717 Instruction *InsertPt = &*Builder.GetInsertPoint();
1718 for (
Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1720 if (SE.isLoopInvariant(S, L)) {
1728 InsertPt = &*L->
getHeader()->getFirstInsertionPt();
1734 if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1735 InsertPt = &*L->
getHeader()->getFirstInsertionPt();
1736 while (InsertPt->
getIterator() != Builder.GetInsertPoint() &&
1737 (isInsertedInstruction(InsertPt) ||
1738 isa<DbgInfoIntrinsic>(InsertPt))) {
1745 auto I = InsertedExpressions.find(std::make_pair(S, InsertPt));
1746 if (I != InsertedExpressions.end())
1749 SCEVInsertPointGuard Guard(Builder,
this);
1750 Builder.SetInsertPoint(InsertPt);
1753 ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, InsertPt);
1754 Value *V = VO.first;
1758 else if (VO.second) {
1762 int64_t ESize = SE.getTypeSizeInBits(Ety);
1763 if ((Offset * 8) % ESize == 0) {
1766 V = Builder.CreateGEP(Ety, V, Idx,
"scevgep");
1770 unsigned AS = Vty->getAddressSpace();
1774 V = Builder.CreateBitCast(V, Vty);
1777 V = Builder.CreateSub(V, VO.second);
1786 InsertedExpressions[std::make_pair(S, InsertPt)] = V;
1790 void SCEVExpander::rememberInstruction(
Value *I) {
1791 if (!PostIncLoops.empty())
1792 InsertedPostIncValues.insert(I);
1794 InsertedValues.insert(I);
1808 const SCEV *
H = SE.getAddRecExpr(SE.getConstant(Ty, 0),
1812 SCEVInsertPointGuard Guard(Builder,
this);
1814 cast<PHINode>(expandCodeFor(H,
nullptr, &L->
getHeader()->front()));
1832 if (
auto *PN = dyn_cast<PHINode>(&I))
1847 unsigned NumElim = 0;
1855 if (!SE.isSCEVable(PN->
getType()))
1860 return Const->getValue();
1866 if (V->
getType() != Phi->getType())
1868 Phi->replaceAllUsesWith(V);
1872 <<
"INDVARS: Eliminated constant iv: " << *Phi <<
'\n');
1876 if (!SE.isSCEVable(Phi->getType()))
1879 PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
1882 if (Phi->getType()->isIntegerTy() && TTI &&
1886 const SCEV *TruncExpr =
1887 SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType());
1888 ExprToIVMap[TruncExpr] = Phi;
1904 if (OrigInc && IsomorphicInc) {
1908 if (OrigPhiRef->
getType() == Phi->getType() &&
1909 !(ChainedPhis.count(Phi) ||
1910 isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)) &&
1911 (ChainedPhis.count(Phi) ||
1912 isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) {
1923 const SCEV *TruncExpr =
1924 SE.getTruncateOrNoop(SE.getSCEV(OrigInc), IsomorphicInc->getType());
1925 if (OrigInc != IsomorphicInc &&
1926 TruncExpr == SE.getSCEV(IsomorphicInc) &&
1927 SE.LI.replacementPreservesLCSSAForm(IsomorphicInc, OrigInc) &&
1928 hoistIVInc(OrigInc, IsomorphicInc)) {
1930 dbgs() <<
"INDVARS: Eliminated congruent iv.inc: "
1931 << *IsomorphicInc <<
'\n');
1932 Value *NewInc = OrigInc;
1933 if (OrigInc->
getType() != IsomorphicInc->getType()) {
1935 if (
PHINode *PN = dyn_cast<PHINode>(OrigInc))
1943 OrigInc, IsomorphicInc->
getType(), IVName);
1953 Value *NewIV = OrigPhiRef;
1954 if (OrigPhiRef->
getType() != Phi->getType()) {
1957 NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->
getType(), IVName);
1968 getRelatedExistingExpansion(S, At, L);
1969 if (VO && VO.
getValue().second ==
nullptr)
1977 using namespace llvm::PatternMatch;
1988 if (!
match(BB->getTerminator(),
1993 if (SE.getSCEV(LHS) == S && SE.DT.dominates(LHS, At))
1994 return ScalarEvolution::ValueOffsetPair(LHS,
nullptr);
1996 if (SE.getSCEV(RHS) == S && SE.DT.dominates(RHS, At))
1997 return ScalarEvolution::ValueOffsetPair(RHS,
nullptr);
2002 ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, At);
2013 bool SCEVExpander::isHighCostExpansionHelper(
2019 if (At && getRelatedExistingExpansion(S, At, L))
2028 return isHighCostExpansionHelper(cast<SCEVTruncateExpr>(S)->getOperand(),
2031 return isHighCostExpansionHelper(cast<SCEVZeroExtendExpr>(S)->getOperand(),
2034 return isHighCostExpansionHelper(cast<SCEVSignExtendExpr>(S)->getOperand(),
2038 if (!Processed.
insert(S).second)
2041 if (
auto *UDivExpr = dyn_cast<SCEVUDivExpr>(S)) {
2045 if (
auto *
SC = dyn_cast<SCEVConstant>(UDivExpr->getRHS()))
2046 if (
SC->getAPInt().isPowerOf2()) {
2048 L->
getHeader()->getParent()->getParent()->getDataLayout();
2049 unsigned Width = cast<IntegerType>(UDivExpr->getType())->
getBitWidth();
2066 At = &ExitingBB->
back();
2067 if (!getRelatedExistingExpansion(
2068 SE.getAddExpr(S, SE.getConstant(S->
getType(), 1)), At,
L))
2074 if (isa<SCEVSMaxExpr>(S) || isa<SCEVUMaxExpr>(S))
2080 if (
const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(S)) {
2081 for (
auto *Op : NAry->operands())
2082 if (isHighCostExpansionHelper(Op, L, At, Processed))
2096 return expandUnionPredicate(cast<SCEVUnionPredicate>(Pred), IP);
2098 return expandEqualPredicate(cast<SCEVEqualPredicate>(Pred), IP);
2100 auto *AddRecPred = cast<SCEVWrapPredicate>(Pred);
2101 return expandWrapPredicate(AddRecPred, IP);
2112 Builder.SetInsertPoint(IP);
2113 auto *I = Builder.CreateICmpNE(Expr0, Expr1,
"ident.check");
2120 "non-affine expression");
2123 const SCEV *ExitCount =
2124 SE.getPredicatedBackedgeTakenCount(AR->
getLoop(), Pred);
2126 assert(ExitCount != SE.getCouldNotCompute() &&
"Invalid loop count");
2131 unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->
getType());
2132 unsigned DstBits = SE.getTypeSizeInBits(AR->
getType());
2140 Builder.SetInsertPoint(Loc);
2141 Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc);
2146 Value *StepValue = expandCodeFor(Step, Ty, Loc);
2147 Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc);
2148 Value *StartValue = expandCodeFor(Start, Ty, Loc);
2153 Builder.SetInsertPoint(Loc);
2156 Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue);
2159 Value *TruncTripCount = Builder.CreateZExtOrTrunc(TripCountVal, Ty);
2161 Intrinsic::umul_with_overflow, Ty);
2164 CallInst *Mul = Builder.CreateCall(MulF, {AbsStep, TruncTripCount},
"mul");
2165 Value *MulV = Builder.CreateExtractValue(Mul, 0,
"mul.result");
2166 Value *OfMul = Builder.CreateExtractValue(Mul, 1,
"mul.overflow");
2171 Value *Add = Builder.CreateAdd(StartValue, MulV);
2172 Value *Sub = Builder.CreateSub(StartValue, MulV);
2174 Value *EndCompareGT = Builder.CreateICmp(
2177 Value *EndCompareLT = Builder.CreateICmp(
2182 Builder.CreateSelect(StepCompare, EndCompareGT, EndCompareLT);
2187 if (SE.getTypeSizeInBits(CountTy) > SE.getTypeSizeInBits(Ty)) {
2189 auto *BackedgeCheck =
2190 Builder.CreateICmp(ICmpInst::ICMP_UGT, TripCountVal,
2192 BackedgeCheck = Builder.CreateAnd(
2195 EndCheck = Builder.CreateOr(EndCheck, BackedgeCheck);
2198 EndCheck = Builder.CreateOr(EndCheck, OfMul);
2204 const auto *
A = cast<SCEVAddRecExpr>(Pred->
getExpr());
2205 Value *NSSWCheck =
nullptr, *NUSWCheck =
nullptr;
2209 NUSWCheck = generateOverflowCheck(
A, IP,
false);
2213 NSSWCheck = generateOverflowCheck(
A, IP,
true);
2215 if (NUSWCheck && NSSWCheck)
2216 return Builder.CreateOr(NUSWCheck, NSSWCheck);
2234 auto *NextCheck = expandCodeForPredicate(Pred, IP);
2235 Builder.SetInsertPoint(IP);
2236 Check = Builder.CreateOr(Check, NextCheck);
2263 struct SCEVFindUnsafe {
2269 bool follow(
const SCEV *S) {
2278 const SCEV *Step = AR->getStepRecurrence(SE);
2279 if (!AR->isAffine() && !SE.dominates(Step, AR->getLoop()->getHeader())) {
2286 bool isDone()
const {
return IsUnsafe; }
2292 SCEVFindUnsafe Search(SE);
2294 return !Search.IsUnsafe;
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
static bool Check(DecodeStatus &Out, DecodeStatus In)
IncrementWrapFlags getFlags() const
Returns the set assumed no overflow flags.
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.
static ConstantInt * getFalse(LLVMContext &Context)
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getExactExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Try to find existing LLVM IR value for S available at the point At.
This class represents an incoming formal argument to a Function.
bool isNonIntegralPointerType(PointerType *PT) const
const Instruction & back() const
bool isOne() const
Return true if the expression is a constant one.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
const SCEV * getConstant(ConstantInt *V)
LLVMContext & getContext() const
bool isZero() const
Return true if the expression is a constant zero.
const SCEV * normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE)
Normalize S to be post-increment for all loops present in Loops.
unsigned getNumOperands() const
The main scalar evolution driver.
This class represents a function call, abstracting a target machine's calling convention.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
Optional< ScalarEvolution::ValueOffsetPair > getRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Try to find the ValueOffsetPair for S.
This class represents a truncation of an integer value to a smaller integer value.
Value * expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LoopT * getParentLoop() const
const Instruction & front() const
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Type * getElementType() const
BlockT * getHeader() const
This is the base class for unary cast operator classes.
return AArch64::GPR64RegClass contains(Reg)
Type * getPointerElementType() const
const SCEV * getStart() const
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
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.
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
bool match(Val *V, const Pattern &P)
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Value * expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc)
Generates a code sequence that evaluates this predicate.
const SCEV * getRHS() const
Returns the right hand side of the equality.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static const Loop * PickMostRelevantLoop(const Loop *A, const Loop *B, DominatorTree &DT)
PickMostRelevantLoop - Given two loops pick the one that's most relevant for SCEV expansion...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
This is the base class for all instructions that perform data casts.
Class to represent struct types.
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 getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Windows NT (Windows on ARM)
op_iterator op_begin() const
This node represents multiplication of some number of SCEVs.
Value * generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
Generates code that evaluates if the AR expression will overflow.
LLVM_NODISCARD bool empty() const
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
A constant value that is initialized with an expression using other constant values.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
This node represents a polynomial recurrence on the trip count of the specified loop.
const T & getValue() const LLVM_LVALUE_FUNCTION
Class to represent array types.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
const Function * getFunction() const
Return the function this instruction belongs to.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power of 2.
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...
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
size_t getNumOperands() const
Class to represent pointers.
uint64_t getElementOffset(unsigned Idx) const
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Function Alias Analysis false
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
This class represents a binary unsigned division operation.
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.
static void SplitAddRecs(SmallVectorImpl< const SCEV * > &Ops, Type *Ty, ScalarEvolution &SE)
SplitAddRecs - Flatten a list of add operands, moving addrec start values out to the top level...
bool isVectorTy() const
True if this is an instance of VectorType.
This is an important base class in LLVM.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const SCEV * getOperand(unsigned i) const
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const SCEV * getExpr() const override
Implementation of the SCEVPredicate interface.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
bool isIllegalInteger(uint64_t Width) const
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
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...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static bool FactorOutConstant(const SCEV *&S, const SCEV *&Remainder, const SCEV *Factor, ScalarEvolution &SE, const DataLayout &DL)
FactorOutConstant - Test if S is divisible by Factor, using signed division.
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
Interval::pred_iterator pred_end(Interval *I)
self_iterator getIterator()
unsigned getIntegerBitWidth() const
Class to represent integer types.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
static Expected< BitVector > expand(StringRef S, StringRef Original)
const SCEV * getLHS() const
static void SimplifyAddOperands(SmallVectorImpl< const SCEV * > &Ops, Type *Ty, ScalarEvolution &SE)
SimplifyAddOperands - Sort and simplify a list of add operands.
const APInt & getAPInt() 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 isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
SCEVPredicateKind getKind() const
LLVMContext & getContext() const
All values hold a context through their type.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
const SCEV * getRHS() const
This class represents an assumption made using SCEV expressions which can be checked at run-time...
static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, ScalarEvolution &SE)
Move parts of Base into Rest to leave Base with the minimal expression that provides a pointer operan...
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Iterator for intrusive lists based on ilist_node.
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
This is the shared class of boolean and integer constants.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
unsigned logBase2() const
PHINode * getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty)
This method returns the canonical induction variable of the specified type for the specified loop (in...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
CHAIN = SC CHAIN, Imm128 - System call.
uint64_t getSizeInBytes() const
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
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 ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
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
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.
This node represents an addition of some number of SCEVs.
static BasicBlock::iterator findInsertPointAfter(Instruction *I, BasicBlock *MustDominate)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
True if this is an instance of IntegerType.
This class represents a signed maximum selection.
iterator_range< user_iterator > users()
InstListType::iterator iterator
Instruction iterators...
Value * expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
llvm_unreachable("Unknown RegBank!")
const SCEV * getLHS() const
Returns the left hand side of the equality.
This class represents a zero extension of a small integer value to a larger integer value...
Value * CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
void emplace_back(ArgTypes &&...Args)
This class represents an analyzed expression in the program.
static IntegerType * getInt32Ty(LLVMContext &C)
Represents a single loop in the control flow graph.
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
This class represents a sign extension of a small integer value to a larger integer value...
This class represents an unsigned maximum selection.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Instruction * getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale)
Return the induction variable increment's IV operand.
const Loop * getLoop() const
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
reverse_iterator rbegin()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class represents a composition of other SCEV predicates, and is the class that most clients will...
unsigned getSCEVType() const
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
Value * expandEqualPredicate(const SCEVEqualPredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LLVM Value Representation.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
A vector that has set insertion semantics.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
static Value * SimplifyPHINode(PHINode *PN, const SimplifyQuery &Q)
See if we can fold the given phi. If not, returns null.
op_iterator op_end() const
APInt zext(unsigned width) const
Zero extend to a new width.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
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.
static APInt getNullValue(unsigned numBits)
Get the '0' value.
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...
This node is a base class providing common functionality for n'ary operators.
This class represents an assumption made on an AddRec expression.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
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...
const SCEV * getOperand() const
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
This class represents an assumption that two SCEV expressions are equal, and this can be checked at r...
static IntegerType * getInt8Ty(LLVMContext &C)
const BasicBlock * getParent() const
const SmallVectorImpl< const SCEVPredicate * > & getPredicates() const
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
static bool canBeCheaplyTransformed(ScalarEvolution &SE, const SCEVAddRecExpr *Phi, const SCEVAddRecExpr *Requested, bool &InvertStep)
Check whether we can cheaply express the requested SCEV in terms of the available PHI SCEV by truncat...
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)