31 using namespace PatternMatch;
33 #define DEBUG_TYPE "instcombine"
36 STATISTIC(NumSel,
"Number of select opts");
57 Constant *In2,
bool IsSigned =
false) {
61 for (
unsigned i = 0, e = VTy->getNumElements();
i != e; ++
i) {
73 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
92 Constant *In2,
bool IsSigned =
false) {
96 for (
unsigned i = 0, e = VTy->getNumElements();
i != e; ++
i) {
108 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
115 for (
auto *U : I.
users())
116 if (isa<BranchInst>(U))
125 bool &TrueIfSigned) {
134 TrueIfSigned =
false;
179 const APInt &KnownOne,
184 "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
185 APInt UnknownBits = ~(KnownZero|KnownOne);
190 Max = KnownOne|UnknownBits;
192 if (UnknownBits.isNegative()) {
202 const APInt &KnownOne,
207 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
208 APInt UnknownBits = ~(KnownZero|KnownOne);
213 Max = KnownOne|UnknownBits;
229 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
233 if (ArrayElementCount > 1024)
return nullptr;
253 if (!Idx)
return nullptr;
256 if ((
unsigned)IdxVal != IdxVal)
return nullptr;
258 if (
StructType *STy = dyn_cast<StructType>(EltTy))
259 EltTy = STy->getElementType(IdxVal);
260 else if (
ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
261 if (IdxVal >= ATy->getNumElements())
return nullptr;
262 EltTy = ATy->getElementType();
270 enum { Overdefined = -3, Undefined = -2 };
279 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
283 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
291 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
296 uint64_t MagicBitvector = 0;
300 for (
unsigned i = 0, e = ArrayElementCount;
i != e; ++
i) {
302 if (!Elt)
return nullptr;
305 if (!LaterIndices.
empty())
313 CompareRHS,
DL, &TLI);
315 if (isa<UndefValue>(C)) {
318 if (TrueRangeEnd == (
int)
i-1)
320 if (FalseRangeEnd == (
int)
i-1)
327 if (!isa<ConstantInt>(C))
return nullptr;
331 bool IsTrueForElt = !cast<ConstantInt>(
C)->
isZero();
336 if (FirstTrueElement == Undefined)
337 FirstTrueElement = TrueRangeEnd =
i;
340 if (SecondTrueElement == Undefined)
341 SecondTrueElement =
i;
343 SecondTrueElement = Overdefined;
346 if (TrueRangeEnd == (
int)
i-1)
349 TrueRangeEnd = Overdefined;
353 if (FirstFalseElement == Undefined)
354 FirstFalseElement = FalseRangeEnd =
i;
357 if (SecondFalseElement == Undefined)
358 SecondFalseElement =
i;
360 SecondFalseElement = Overdefined;
363 if (FalseRangeEnd == (
int)
i-1)
366 FalseRangeEnd = Overdefined;
371 if (
i < 64 && IsTrueForElt)
372 MagicBitvector |= 1ULL <<
i;
377 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
378 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
379 FalseRangeEnd == Overdefined)
394 Idx = Builder->CreateTrunc(Idx, IntPtrTy);
399 if (SecondTrueElement != Overdefined) {
401 if (FirstTrueElement == Undefined)
402 return replaceInstUsesWith(ICI, Builder->getFalse());
407 if (SecondTrueElement == Undefined)
411 Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
413 Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
414 return BinaryOperator::CreateOr(C1, C2);
419 if (SecondFalseElement != Overdefined) {
421 if (FirstFalseElement == Undefined)
422 return replaceInstUsesWith(ICI, Builder->getTrue());
427 if (SecondFalseElement == Undefined)
431 Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
433 Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
434 return BinaryOperator::CreateAnd(C1, C2);
439 if (TrueRangeEnd != Overdefined) {
440 assert(TrueRangeEnd != FirstTrueElement &&
"Should emit single compare");
443 if (FirstTrueElement) {
445 Idx = Builder->CreateAdd(Idx, Offs);
449 TrueRangeEnd-FirstTrueElement+1);
454 if (FalseRangeEnd != Overdefined) {
455 assert(FalseRangeEnd != FirstFalseElement &&
"Should emit single compare");
457 if (FirstFalseElement) {
459 Idx = Builder->CreateAdd(Idx, Offs);
463 FalseRangeEnd-FirstFalseElement);
477 if (ArrayElementCount <= Idx->
getType()->getIntegerBitWidth())
480 Ty = DL.getSmallestLegalIntType(Init->
getContext(), ArrayElementCount);
483 Value *V = Builder->CreateIntCast(Idx, Ty,
false);
514 for (i = 1; i != e; ++
i, ++GTI) {
517 if (CI->isZero())
continue;
524 Offset += Size*CI->getSExtValue();
534 if (i == e)
return nullptr;
542 for (++i, ++GTI; i != e; ++
i, ++GTI) {
544 if (!CI)
return nullptr;
547 if (CI->
isZero())
continue;
575 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
577 Offset &= PtrSizeMask;
578 VariableScale &= PtrSizeMask;
584 int64_t NewOffs = Offset / (int64_t)VariableScale;
585 if (Offset != NewOffs*(int64_t)VariableScale)
589 if (VariableIdx->
getType() != IntPtrTy)
613 while (!WorkList.
empty()) {
616 while (!WorkList.
empty()) {
617 if (Explored.
size() >= 100)
622 if (Explored.
count(V) != 0) {
627 if (!isa<IntToPtrInst>(V) && !isa<PtrToIntInst>(V) &&
628 !isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
633 if (isa<IntToPtrInst>(V) || isa<PtrToIntInst>(V)) {
635 if (!CI->isNoopCast(DL))
638 if (Explored.
count(CI->getOperand(0)) == 0)
642 if (
auto *GEP = dyn_cast<GEPOperator>(V)) {
654 if (WorkList.
back() == V) {
660 if (
auto *PN = dyn_cast<PHINode>(V)) {
662 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
670 for (
auto *PN : PHIs)
671 for (
Value *
Op : PN->incoming_values())
679 for (
Value *Val : Explored) {
685 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
686 Explored.count(PHI) == 0)
689 if (PHI->getParent() == Inst->getParent())
699 bool Before =
true) {
700 if (
auto *PHI = dyn_cast<PHINode>(V)) {
701 Builder.
SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt());
704 if (
auto *
I = dyn_cast<Instruction>(V)) {
706 I = &*std::next(
I->getIterator());
710 if (
auto *
A = dyn_cast<Argument>(V)) {
712 BasicBlock &Entry =
A->getParent()->getEntryBlock();
718 assert(isa<Constant>(V) &&
"Setting insertion point for unknown value!");
740 for (
Value *Val : Explored) {
745 if (
auto *PHI = dyn_cast<PHINode>(Val))
746 NewInsts[PHI] =
PHINode::Create(IndexType, PHI->getNumIncomingValues(),
747 PHI->getName() +
".idx", PHI);
752 for (
Value *Val : Explored) {
754 if (NewInsts.
find(Val) != NewInsts.
end())
757 if (
auto *CI = dyn_cast<CastInst>(Val)) {
758 NewInsts[CI] = NewInsts[CI->getOperand(0)];
761 if (
auto *GEP = dyn_cast<GEPOperator>(Val)) {
768 NewInsts[GEP->
getOperand(0)]->getType()->getScalarSizeInBits()) {
769 Index = Builder.CreateSExtOrTrunc(
770 Index, NewInsts[GEP->
getOperand(0)]->getType(),
775 if (isa<ConstantInt>(
Op) && dyn_cast<ConstantInt>(
Op)->
isZero())
776 NewInsts[
GEP] = Index;
778 NewInsts[
GEP] = Builder.CreateNSWAdd(
782 if (isa<PHINode>(Val))
789 for (
Value *Val : Explored) {
794 if (
auto *PHI = dyn_cast<PHINode>(Val)) {
796 for (
unsigned I = 0,
E = PHI->getNumIncomingValues();
I <
E; ++
I) {
797 Value *NewIncoming = PHI->getIncomingValue(
I);
799 if (NewInsts.
find(NewIncoming) != NewInsts.
end())
800 NewIncoming = NewInsts[NewIncoming];
802 NewPhi->
addIncoming(NewIncoming, PHI->getIncomingBlock(
I));
807 for (
Value *Val : Explored) {
816 Value *NewBase = Base;
818 NewBase = Builder.CreateBitOrPointerCast(Base, Start->
getType(),
821 Value *GEP = Builder.CreateInBoundsGEP(
825 if (!Val->getType()->isPointerTy()) {
826 Value *Cast = Builder.CreatePointerCast(GEP, Val->
getType(),
827 Val->getName() +
".conv");
833 return NewInsts[Start];
839 static std::pair<Value *, Value *>
861 if (
auto *CI = dyn_cast<IntToPtrInst>(V)) {
862 if (!CI->isNoopCast(DL))
864 V = CI->getOperand(0);
867 if (
auto *CI = dyn_cast<PtrToIntInst>(V)) {
868 if (!CI->isNoopCast(DL))
870 V = CI->getOperand(0);
891 Value *PtrBase, *Index;
930 if (!isa<GetElementPtrInst>(RHS))
946 }
else if (
GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
949 if (PtrBase != GEPRHS->getOperand(0)) {
950 bool IndicesTheSame = GEPLHS->
getNumOperands()==GEPRHS->getNumOperands();
952 GEPRHS->getOperand(0)->getType();
955 if (GEPLHS->
getOperand(i) != GEPRHS->getOperand(i)) {
956 IndicesTheSame =
false;
967 if (GEPLHS->
isInBounds() && GEPRHS->isInBounds() &&
969 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
980 if (LHSIndexTy != RHSIndexTy) {
983 ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
985 LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
990 return replaceInstUsesWith(I, Cmp);
1001 return foldGEPICmp(GEPRHS, GEPLHS->
getOperand(0),
1005 if (GEPRHS->hasAllZeroIndices())
1006 return foldGEPICmp(GEPLHS, GEPRHS->
getOperand(0), Cond,
I);
1008 bool GEPsInBounds = GEPLHS->
isInBounds() && GEPRHS->isInBounds();
1011 unsigned NumDifferences = 0;
1012 unsigned DiffOperand = 0;
1013 for (
unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++
i)
1014 if (GEPLHS->
getOperand(i) != GEPRHS->getOperand(i)) {
1016 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
1021 if (NumDifferences++)
break;
1026 if (NumDifferences == 0)
1027 return replaceInstUsesWith(I,
1030 else if (NumDifferences == 1 && GEPsInBounds) {
1032 Value *RHSV = GEPRHS->getOperand(DiffOperand);
1040 if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->
hasOneUse()) &&
1041 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
1056 const Value *Other) {
1076 for (
const Use &U : Alloca->
uses()) {
1082 unsigned NumCmps = 0;
1083 while (!Worklist.
empty()) {
1086 const Value *V = U->getUser();
1089 if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V) || isa<PHINode>(V) ||
1090 isa<SelectInst>(V)) {
1092 }
else if (isa<LoadInst>(V)) {
1095 }
else if (
const auto *SI = dyn_cast<StoreInst>(V)) {
1097 if (
SI->getValueOperand() == U->get())
1100 }
else if (isa<ICmpInst>(V)) {
1104 }
else if (
const auto *Intrin = dyn_cast<IntrinsicInst>(V)) {
1105 switch (Intrin->getIntrinsicID()) {
1109 case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
1110 case Intrinsic::dbg_declare:
case Intrinsic::dbg_value:
1111 case Intrinsic::memcpy:
case Intrinsic::memmove:
case Intrinsic::memset:
1119 for (
const Use &U : V->
uses()) {
1127 return replaceInstUsesWith(
1191 return new ICmpInst(Pred, LHS, RHS);
1198 bool IsAShr = isa<AShrOperator>(I.
getOperand(0));
1223 if (IsAShr && AP1 == AP2.
ashr(Shift)) {
1229 }
else if (AP1 == AP2.
lshr(Shift)) {
1237 return replaceInstUsesWith(I, TorF);
1250 return new ICmpInst(Pred, LHS, RHS);
1259 if (!AP1 && AP2TrailingZeros != 0)
1270 if (Shift > 0 && AP2.
shl(Shift) == AP1)
1276 return replaceInstUsesWith(I, TorF);
1304 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1318 unsigned NeededSignBits = CI1->
getBitWidth() - NewWidth + 1;
1329 if (U == AddWithCst)
1347 Intrinsic::sadd_with_overflow, NewType);
1355 Value *TruncA = Builder->CreateTrunc(A, NewType, A->
getName() +
".trunc");
1356 Value *TruncB = Builder->CreateTrunc(B, NewType, B->
getName() +
".trunc");
1357 CallInst *
Call = Builder->CreateCall(F, {TruncA, TruncB},
"sadd");
1358 Value *
Add = Builder->CreateExtractValue(Call, 0,
"sadd.result");
1359 Value *ZExt = Builder->CreateZExt(Add, OrigAdd->
getType());
1378 Value *A =
nullptr, *
B =
nullptr;
1396 Cmp, A,
B, CI2, cast<ConstantInt>(Cmp.
getOperand(1)), *
this))
1424 TrueBB, FalseBB)) &&
1425 TrueBB != FalseBB) {
1436 return replaceInstUsesWith(Cmp, Builder->getFalse());
1438 return replaceInstUsesWith(Cmp, Builder->getTrue());
1480 APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
1523 isTrueIfPositive ^=
true;
1526 if (isTrueIfPositive)
1566 if (!Shift || !Shift->
isShift())
1574 unsigned ShiftOpcode = Shift->
getOpcode();
1575 bool IsShl = ShiftOpcode == Instruction::Shl;
1578 bool CanFold =
false;
1579 if (ShiftOpcode == Instruction::AShr) {
1583 }
else if (ShiftOpcode == Instruction::Shl) {
1590 }
else if (ShiftOpcode == Instruction::LShr) {
1602 APInt SameAsC1 = IsShl ? NewCst.
shl(*C3) : NewCst.
lshr(*C3);
1604 if (SameAsC1 != *C1) {
1614 APInt NewAndCst = IsShl ? C2->
lshr(*C3) : C2->
shl(*C3);
1617 Worklist.Add(Shift);
1672 Value *NewAnd = Builder->CreateAnd(W, ZextC2, And->
getName());
1677 if (
Instruction *I = foldICmpAndShift(Cmp, And, C1, C2))
1690 unsigned UsesRemoved = 0;
1693 if (Or->hasOneUse())
1699 Value *NewOr =
nullptr;
1700 if (
auto *C = dyn_cast<Constant>(B)) {
1701 if (UsesRemoved >= 1)
1704 if (UsesRemoved >= 3)
1705 NewOr = Builder->CreateOr(Builder->CreateShl(One, B, LShr->
getName(),
1710 Value *NewAnd = Builder->CreateAnd(A, NewOr, And->
getName());
1733 if (
Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1741 if (
auto *LI = dyn_cast<LoadInst>(X))
1742 if (
auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1743 if (
auto *GV = dyn_cast<GlobalVariable>(GEP->
getOperand(0)))
1745 !LI->isVolatile() && isa<ConstantInt>(
Y)) {
1747 if (
Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, Cmp, C2))
1757 if (Cmp.
getOperand(1) == Y && (-(*C)).isPowerOf2()) {
1769 if (ExactLogBase2 != -1 && DL.isLegalInteger(ExactLogBase2 + 1)) {
1773 Value *Trunc = Builder->CreateTrunc(X, NTy);
1862 if (CLog2 == TypeBits - 1) {
1890 }
else if (Cmp.
isEquality() && CIsPowerOf2) {
1901 const APInt *ShiftVal;
1903 return foldICmpShlConstConst(Cmp, Shl->
getOperand(1), *
C, *ShiftVal);
1905 const APInt *ShiftAmt;
1912 if (ShiftAmt->uge(TypeBits))
1922 return new ICmpInst(Pred, X, LShrC);
1927 return new ICmpInst(Pred, X, LShrC);
1934 Value *And = Builder->CreateAnd(X, Mask, Shl->
getName() +
".mask");
1935 return new ICmpInst(Pred, And, LShrC);
1946 bool TrueIfSigned =
false;
1952 Value *And = Builder->CreateAnd(X, Mask, Shl->
getName() +
".mask");
1967 "Encountered `ult 0` that should have been eliminated by "
1970 : C->
lshr(*ShiftAmt);
1980 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
1982 DL.isLegalInteger(TypeBits - Amt)) {
1988 return new ICmpInst(Pred, Builder->CreateTrunc(X, TruncTy), NewC);
2005 const APInt *ShiftVal;
2007 return foldICmpShrConstConst(Cmp, Shr->
getOperand(1), *
C, *ShiftVal);
2009 const APInt *ShiftAmt;
2016 unsigned ShAmtVal = ShiftAmt->getLimitedValue(TypeBits);
2017 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2020 bool IsAShr = Shr->
getOpcode() == Instruction::AShr;
2030 if (IsAShr && (!Shr->
isExact() || ShAmtVal == TypeBits - 1))
2039 Value *Tmp = IsAShr ? Builder->CreateSDiv(X, DivCst,
"", Shr->
isExact())
2040 : Builder->CreateUDiv(X, DivCst,
"", Shr->
isExact());
2051 TheDiv->
getOpcode() == Instruction::UDiv);
2053 Instruction *Res = foldICmpDivConstant(Cmp, TheDiv, C);
2054 assert(Res &&
"This div/cst should have folded!");
2064 (!IsAShr && C->
shl(ShAmtVal).
lshr(ShAmtVal) == *
C)) &&
2065 "Expected icmp+shr simplify did not occur.");
2073 return new ICmpInst(Pred, X, ShiftedCmpRHS);
2078 Value *And = Builder->CreateAnd(X, Mask, Shr->
getName() +
".mask");
2079 return new ICmpInst(Pred, And, ShiftedCmpRHS);
2093 assert(C2 != 0 &&
"udiv 0, X should have been simplified already.");
2099 "icmp ugt X, UINT_MAX should have been simplified already.");
2106 assert(C != 0 &&
"icmp ult X, 0 should have been simplified already.");
2136 bool DivIsSigned = Div->
getOpcode() == Instruction::SDiv;
2144 if (*C2 == 0 || *C2 == 1 || (DivIsSigned && C2->
isAllOnesValue()))
2177 int LoOverflow = 0, HiOverflow = 0;
2178 Constant *LoBound =
nullptr, *HiBound =
nullptr;
2183 HiOverflow = LoOverflow = ProdOV;
2193 HiBound = RangeSize;
2196 HiOverflow = LoOverflow = ProdOV;
2202 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2205 LoOverflow =
addWithOverflow(LoBound, HiBound, DivNeg,
true) ? -1 : 0;
2213 LoBound =
AddOne(RangeSize);
2215 if (HiBound == DivRHS) {
2222 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2224 LoOverflow =
addWithOverflow(LoBound, HiBound, RangeSize,
true) ? -1:0;
2227 LoOverflow = HiOverflow = ProdOV;
2240 if (LoOverflow && HiOverflow)
2241 return replaceInstUsesWith(Cmp, Builder->getFalse());
2248 return replaceInstUsesWith(
2250 HiBound->getUniqueInteger(), DivIsSigned,
true));
2252 if (LoOverflow && HiOverflow)
2253 return replaceInstUsesWith(Cmp, Builder->getTrue());
2260 return replaceInstUsesWith(Cmp,
2262 HiBound->getUniqueInteger(),
2263 DivIsSigned,
false));
2266 if (LoOverflow == +1)
2267 return replaceInstUsesWith(Cmp, Builder->getTrue());
2268 if (LoOverflow == -1)
2269 return replaceInstUsesWith(Cmp, Builder->getFalse());
2270 return new ICmpInst(Pred, X, LoBound);
2273 if (HiOverflow == +1)
2274 return replaceInstUsesWith(Cmp, Builder->getFalse());
2275 if (HiOverflow == -1)
2276 return replaceInstUsesWith(Cmp, Builder->getTrue());
2322 (*C2 & (*C - 1)) == (*C - 1))
2368 (*C2 & (*C - 1)) == 0)
2394 if (
Instruction *I = foldICmpXorConstant(Cmp, BO, C))
2398 if (
Instruction *I = foldICmpAndConstant(Cmp, BO, C))
2402 if (
Instruction *I = foldICmpOrConstant(Cmp, BO, C))
2405 case Instruction::Mul:
2406 if (
Instruction *I = foldICmpMulConstant(Cmp, BO, C))
2409 case Instruction::Shl:
2410 if (
Instruction *I = foldICmpShlConstant(Cmp, BO, C))
2413 case Instruction::LShr:
2414 case Instruction::AShr:
2415 if (
Instruction *I = foldICmpShrConstant(Cmp, BO, C))
2418 case Instruction::UDiv:
2419 if (
Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
2422 case Instruction::SDiv:
2423 if (
Instruction *I = foldICmpDivConstant(Cmp, BO, C))
2426 case Instruction::Sub:
2427 if (
Instruction *I = foldICmpSubConstant(Cmp, BO, C))
2431 if (
Instruction *I = foldICmpAddConstant(Cmp, BO, C))
2438 if (
Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))
2444 LHSI->
getOpcode() == Instruction::Trunc)
2445 if (
Instruction *I = foldICmpTruncConstant(Cmp, LHSI, C))
2448 if (
Instruction *I = foldICmpIntrinsicWithConstant(Cmp, C))
2470 case Instruction::SRem:
2475 Value *NewRem = Builder->CreateURem(BOp0, BOp1, BO->
getName());
2487 return new ICmpInst(Pred, BOp0, SubC);
2489 }
else if (*C == 0) {
2492 if (
Value *NegVal = dyn_castNegVal(BOp1))
2493 return new ICmpInst(Pred, BOp0, NegVal);
2494 if (
Value *NegVal = dyn_castNegVal(BOp0))
2495 return new ICmpInst(Pred, NegVal, BOp1);
2497 Value *Neg = Builder->CreateNeg(BOp1);
2499 return new ICmpInst(Pred, BOp0, Neg);
2506 if (
Constant *BOC = dyn_cast<Constant>(BOp1)) {
2510 }
else if (*C == 0) {
2512 return new ICmpInst(Pred, BOp0, BOp1);
2516 case Instruction::Sub:
2522 return new ICmpInst(Pred, BOp1, SubC);
2523 }
else if (*C == 0) {
2525 return new ICmpInst(Pred, BOp0, BOp1);
2536 Value *And = Builder->CreateAnd(BOp0, NotBOC);
2537 return new ICmpInst(Pred, And, NotBOC);
2557 return new ICmpInst(NewPred, BOp0, Zero);
2561 if (*C == 0 && (~(*BOC) + 1).isPowerOf2()) {
2564 return new ICmpInst(NewPred, BOp0, NegBOC);
2569 case Instruction::Mul:
2580 case Instruction::UDiv:
2584 return new ICmpInst(NewPred, BOp1, BOp0);
2602 case Intrinsic::bswap:
2607 case Intrinsic::ctlz:
2608 case Intrinsic::cttz:
2617 case Intrinsic::ctpop: {
2620 bool IsZero = *C == 0;
2645 switch (LHSI->getOpcode()) {
2646 case Instruction::GetElementPtr:
2649 cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2654 case Instruction::PHI:
2666 Value *Op1 =
nullptr, *Op2 =
nullptr;
2668 if (
Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
2672 if (
Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
2684 bool Transform =
false;
2687 else if (Op1 || Op2) {
2689 if (LHSI->hasOneUse())
2692 else if (CI && !CI->
isZero())
2697 replacedSelectWithOperand(cast<SelectInst>(LHSI), &
I, Op1 ? 2 : 1);
2701 Op1 = Builder->CreateICmp(I.
getPredicate(), LHSI->getOperand(1), RHSC,
2704 Op2 = Builder->CreateICmp(I.
getPredicate(), LHSI->getOperand(2), RHSC,
2710 case Instruction::IntToPtr:
2713 DL.getIntPtrType(RHSC->
getType()) == LHSI->getOperand(0)->getType())
2722 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2726 if (
Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
2746 bool NoOp0WrapProblem =
false, NoOp1WrapProblem =
false;
2747 if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2752 if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2760 Value *A =
nullptr, *B =
nullptr, *C =
nullptr, *
D =
nullptr;
2766 C = BO1->getOperand(0);
2767 D = BO1->getOperand(1);
2772 if (
ConstantInt *RHSC = dyn_cast_or_null<ConstantInt>(B))
2773 if (!RHSC->isMinValue(
true))
2777 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2778 return new ICmpInst(Pred, A == Op1 ? B : A,
2782 if ((C == Op0 ||
D == Op0) && NoOp1WrapProblem)
2787 if (A && C && (A == C || A ==
D || B == C || B ==
D) && NoOp0WrapProblem &&
2797 }
else if (A ==
D) {
2801 }
else if (B == C) {
2857 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
2861 const APInt &AP1 = C1->getValue();
2862 const APInt &AP2 = C2->getValue();
2864 APInt AP1Abs = C1->getValue().
abs();
2865 APInt AP2Abs = C2->getValue().
abs();
2866 if (AP1Abs.
uge(AP2Abs)) {
2868 Value *NewAdd = Builder->CreateNSWAdd(A, C3);
2869 return new ICmpInst(Pred, NewAdd, C);
2872 Value *NewAdd = Builder->CreateNSWAdd(C, C3);
2873 return new ICmpInst(Pred, A, NewAdd);
2884 if (BO0 && BO0->
getOpcode() == Instruction::Sub) {
2888 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
2889 C = BO1->getOperand(0);
2890 D = BO1->getOperand(1);
2894 if (A == Op1 && NoOp0WrapProblem)
2898 if (C == Op0 && NoOp1WrapProblem)
2902 if (B &&
D && B ==
D && NoOp0WrapProblem && NoOp1WrapProblem &&
2908 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2917 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
2918 if (!RHSC->isMinValue(
true))
2928 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
2929 Op0 == BO1->getOperand(1))
2939 case ICmpInst::ICMP_NE:
2953 BO1->hasOneUse() && BO0->
getOperand(1) == BO1->getOperand(1)) {
2958 case Instruction::Sub:
2962 BO1->getOperand(0));
2979 case Instruction::Mul:
2993 Value *And2 = Builder->CreateAnd(BO1->getOperand(0),
Mask);
2998 case Instruction::UDiv:
2999 case Instruction::LShr:
3003 case Instruction::SDiv:
3004 case Instruction::AShr:
3005 if (!BO0->
isExact() || !BO1->isExact())
3008 BO1->getOperand(0));
3009 case Instruction::Shl: {
3017 BO1->getOperand(0));
3030 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
3131 if (A == Op1 || B == Op1) {
3132 Value *OtherVal = A == Op1 ? B :
A;
3143 Value *Xor = Builder->CreateXor(C, NC);
3161 Value *OtherVal = A == Op0 ? B :
A;
3169 Value *X =
nullptr, *Y =
nullptr, *Z =
nullptr;
3175 }
else if (A == D) {
3179 }
else if (B == C) {
3183 }
else if (B == D) {
3190 Op1 = Builder->CreateXor(X, Y);
3191 Op1 = Builder->CreateAnd(Op1, Z);
3209 Builder->CreateTrunc(B, A->
getType()));
3220 if (ShAmt < TypeBits && ShAmt != 0) {
3224 Value *Xor = Builder->CreateXor(A, B, I.
getName() +
".unshifted");
3226 return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
3235 if (ShAmt < TypeBits && ShAmt != 0) {
3236 Value *Xor = Builder->CreateXor(A, B, I.
getName() +
".unshifted");
3238 Value *And = Builder->CreateAnd(Xor, Builder->getInt(AndVal),
3254 unsigned ASize = cast<IntegerType>(A->
getType())->getPrimitiveSizeInBits();
3256 if (ShAmt < ASize) {
3264 Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
3277 Type *SrcTy = LHSCIOp->getType();
3283 if (LHSCI->
getOpcode() == Instruction::PtrToInt &&
3285 Value *RHSOp =
nullptr;
3286 if (
auto *RHSC = dyn_cast<PtrToIntOperator>(ICmp.
getOperand(1))) {
3289 LHSCIOp->getType()->getPointerAddressSpace()) {
3292 if (LHSCIOp->getType() != RHSOp->
getType())
3293 RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->
getType());
3295 }
else if (
auto *RHSC = dyn_cast<Constant>(ICmp.
getOperand(1))) {
3305 if (LHSCI->
getOpcode() != Instruction::ZExt &&
3306 LHSCI->
getOpcode() != Instruction::SExt)
3309 bool isSignedExt = LHSCI->
getOpcode() == Instruction::SExt;
3310 bool isSignedCmp = ICmp.
isSigned();
3312 if (
auto *CI = dyn_cast<CastInst>(ICmp.
getOperand(1))) {
3315 if (RHSCIOp->
getType() != LHSCIOp->getType())
3320 if (CI->getOpcode() != LHSCI->
getOpcode())
3329 if (isSignedCmp && isSignedExt)
3354 if (isSignedExt && isSignedCmp)
3368 if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C))
3377 Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICmp.
getName());
3381 return replaceInstUsesWith(ICmp, Result);
3390 if (OrigI.
isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
3393 auto SetResult = [&](
Value *OpResult,
Constant *OverflowVal,
bool ReuseName) {
3395 Overflow = OverflowVal;
3405 Builder->SetInsertPoint(&OrigI);
3414 return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(),
3418 return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(),
true);
3426 return SetResult(LHS, Builder->getFalse(),
false);
3431 if (WillNotOverflowSignedAdd(LHS, RHS, OrigI))
3432 return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(),
3441 return SetResult(LHS, Builder->getFalse(),
false);
3444 if (WillNotOverflowSignedSub(LHS, RHS, OrigI))
3445 return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(),
3448 if (WillNotOverflowUnsignedSub(LHS, RHS, OrigI))
3449 return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(),
3458 return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(),
3461 return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(),
true);
3466 if (isa<UndefValue>(RHS))
3471 return SetResult(RHS, Builder->getFalse(),
false);
3475 return SetResult(LHS, Builder->getFalse(),
false);
3478 if (WillNotOverflowSignedMul(LHS, RHS, OrigI))
3479 return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(),
3505 if (!isa<IntegerType>(MulVal->
getType()))
3513 assert(MulInstr->getOpcode() == Instruction::Mul);
3515 auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)),
3516 *RHS = cast<ZExtOperator>(MulInstr->getOperand(1));
3517 assert(LHS->getOpcode() == Instruction::ZExt);
3518 assert(RHS->getOpcode() == Instruction::ZExt);
3519 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
3524 WidthB = TyB->getPrimitiveSizeInBits();
3527 if (WidthB > WidthA) {
3542 if (
TruncInst *TI = dyn_cast<TruncInst>(U)) {
3545 if (TruncWidth > MulWidth)
3565 case ICmpInst::ICMP_NE:
3569 if (
ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal))
3570 if (Zext->hasOneUse()) {
3571 Value *ZextArg = Zext->getOperand(0);
3572 if (
TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg))
3583 if (ValToMask != MulVal)
3587 unsigned MaskWidth = CVal.
logBase2();
3588 if (MaskWidth == MulWidth)
3598 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3610 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3621 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3633 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3649 if (WidthA < MulWidth)
3651 if (WidthB < MulWidth)
3654 Intrinsic::umul_with_overflow, MulType);
3664 if (U == &I || U == OtherVal)
3666 if (
TruncInst *TI = dyn_cast<TruncInst>(U)) {
3667 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
3675 APInt ShortMask = CI->getValue().
trunc(MulWidth);
3687 if (isa<Instruction>(OtherVal))
3694 case ICmpInst::ICMP_NE:
3767 const Value * Op1) {
3779 int GlobalSwapBenefits = 0;
3782 if (!BinOp || BinOp->
getOpcode() != Instruction::Sub)
3786 int LocalSwapBenefits = -1;
3787 unsigned Op1Idx = 1;
3790 LocalSwapBenefits = 1;
3794 GlobalSwapBenefits += LocalSwapBenefits;
3796 return GlobalSwapBenefits > 0;
3812 assert(DI && UI &&
"Instruction not defined\n");
3823 auto *Usr = cast<Instruction>(U);
3824 if (Usr != UI && !DT.dominates(DB, Usr->
getParent()))
3835 auto *BI = dyn_cast_or_null<BranchInst>(BB->
getTerminator());
3836 if (!BI || BI->getNumSuccessors() != 2)
3839 if (!IC || (IC->getOperand(0) != SI && IC->
getOperand(1) != SI))
3886 const unsigned SIOpd) {
3887 assert((SIOpd == 1 || SIOpd == 2) &&
"Invalid select operand!");
3925 bool IsSignBit =
false;
3932 APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
3933 APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
3937 Op0KnownZero, Op0KnownOne, 0))
3941 Op1KnownZero, Op1KnownOne, 0))
3947 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
3948 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
3964 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
3966 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
3975 case ICmpInst::ICMP_NE: {
3976 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) {
3985 APInt Op0KnownZeroInverted = ~Op0KnownZero;
3986 if (~Op1KnownZero == 0) {
3988 Value *LHS =
nullptr;
3991 *LHSC != Op0KnownZeroInverted)
3996 APInt ValToCheck = Op0KnownZeroInverted;
4003 return new ICmpInst(NewPred, X, CmpC);
4004 }
else if ((++ValToCheck).isPowerOf2()) {
4010 return new ICmpInst(NewPred, X, CmpC);
4016 if (Op0KnownZeroInverted == 1 &&
4028 if (Op0Max.ult(Op1Min))
4030 if (Op0Min.uge(Op1Max))
4032 if (Op1Min == Op0Max)
4033 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4038 if (Op1Max == Op0Min + 1) {
4046 if (Op0Min.ugt(Op1Max))
4049 if (Op0Max.ule(Op1Min))
4052 if (Op1Max == Op0Min)
4053 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4058 if (*CmpC == Op0Max - 1)
4065 if (Op0Max.slt(Op1Min))
4067 if (Op0Min.sge(Op1Max))
4069 if (Op1Min == Op0Max)
4070 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4071 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4072 if (Op1Max == Op0Min + 1)
4074 Builder->getInt(CI->getValue() - 1));
4078 if (Op0Min.sgt(Op1Max))
4080 if (Op0Max.sle(Op1Min))
4083 if (Op1Max == Op0Min)
4084 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4085 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4086 if (Op1Min == Op0Max - 1)
4088 Builder->getInt(CI->getValue() + 1));
4092 assert(!isa<ConstantInt>(Op1) &&
"ICMP_SGE with ConstantInt not folded!");
4093 if (Op0Min.sge(Op1Max))
4095 if (Op0Max.slt(Op1Min))
4099 assert(!isa<ConstantInt>(Op1) &&
"ICMP_SLE with ConstantInt not folded!");
4100 if (Op0Max.sle(Op1Min))
4102 if (Op0Min.sgt(Op1Max))
4106 assert(!isa<ConstantInt>(Op1) &&
"ICMP_UGE with ConstantInt not folded!");
4107 if (Op0Min.uge(Op1Max))
4109 if (Op0Max.ult(Op1Min))
4113 assert(!isa<ConstantInt>(Op1) &&
"ICMP_ULE with ConstantInt not folded!");
4114 if (Op0Max.ule(Op1Min))
4116 if (Op0Min.ugt(Op1Max))
4124 ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
4125 (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
4162 for (
unsigned i = 0; i != NumElts; ++
i) {
4167 if (isa<UndefValue>(Elt))
4190 bool Changed =
false;
4198 if (Op0Cplxity < Op1Cplxity ||
4207 return replaceInstUsesWith(I, V);
4212 Value *Cond, *SelectTrue, *SelectFalse;
4215 if (
Value *V = dyn_castNegVal(SelectTrue)) {
4216 if (V == SelectFalse)
4219 else if (
Value *V = dyn_castNegVal(SelectFalse)) {
4220 if (V == SelectTrue)
4233 Value *Xor = Builder->CreateXor(Op0, Op1, I.
getName() +
"tmp");
4236 case ICmpInst::ICMP_NE:
4237 return BinaryOperator::CreateXor(Op0, Op1);
4244 return BinaryOperator::CreateAnd(Not, Op1);
4251 return BinaryOperator::CreateAnd(Not, Op0);
4258 return BinaryOperator::CreateOr(Not, Op1);
4265 return BinaryOperator::CreateOr(Not, Op0);
4288 if ((
SI->getOperand(1) == Op0 &&
SI->getOperand(2) == Op1) ||
4289 (
SI->getOperand(2) == Op0 &&
SI->getOperand(1) == Op1))
4313 if (
Instruction *Res = foldICmpInstWithConstant(I))
4316 if (
Instruction *Res = foldICmpInstWithConstantNotInt(I))
4320 if (
GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
4323 if (
GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
4330 assert(Op1->getType()->isPointerTy() &&
"Comparing pointer with non-pointer?");
4332 if (
Instruction *New = foldAllocaCmp(I, Alloca, Op1))
4335 if (
Instruction *New = foldAllocaCmp(I, Alloca, Op0))
4342 if (
BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
4344 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
4347 Op0 = CI->getOperand(0);
4351 if (
BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
4355 if (Op0->
getType() != Op1->getType()) {
4356 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
4360 Op1 = Builder->CreateBitCast(Op1, Op0->
getType());
4367 if (isa<CastInst>(Op0)) {
4374 if (isa<Constant>(Op1) || isa<CastInst>(Op1))
4394 Builder->CreateAnd(A, B),
4402 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
4409 isa<IntegerType>(A->
getType())) {
4414 replaceInstUsesWith(*AddI, Result);
4415 return replaceInstUsesWith(I, Overflow);
4443 if (
auto *EVI = dyn_cast<ExtractValueInst>(Op0))
4444 if (
auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
4445 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
4453 return foldICmpAddOpConst(I, X, Cst, I.
getPredicate());
4459 return Changed ? &I :
nullptr;
4465 if (!isa<ConstantFP>(RHSC))
return nullptr;
4466 const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
4471 if (MantissaWidth == -1)
return nullptr;
4475 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
4479 bool IsExact =
false;
4491 return replaceInstUsesWith(I, Builder->getFalse());
4494 return replaceInstUsesWith(I, Builder->getTrue());
4510 if ((
int)InputSize > MantissaWidth) {
4515 if (MaxExponent < (
int)InputSize - !LHSUnsigned)
4521 if (MantissaWidth <= Exp && Exp <= (
int)InputSize - !LHSUnsigned)
4530 assert(!RHS.
isNaN() &&
"NaN comparison not already folded!");
4560 return replaceInstUsesWith(I, Builder->getTrue());
4562 return replaceInstUsesWith(I, Builder->getFalse());
4580 return replaceInstUsesWith(I, Builder->getTrue());
4581 return replaceInstUsesWith(I, Builder->getFalse());
4592 return replaceInstUsesWith(I, Builder->getTrue());
4593 return replaceInstUsesWith(I, Builder->getFalse());
4605 return replaceInstUsesWith(I, Builder->getTrue());
4606 return replaceInstUsesWith(I, Builder->getFalse());
4616 return replaceInstUsesWith(I, Builder->getTrue());
4617 return replaceInstUsesWith(I, Builder->getFalse());
4629 bool Equal = LHSUnsigned
4638 case ICmpInst::ICMP_NE:
4639 return replaceInstUsesWith(I, Builder->getTrue());
4641 return replaceInstUsesWith(I, Builder->getFalse());
4646 return replaceInstUsesWith(I, Builder->getFalse());
4658 return replaceInstUsesWith(I, Builder->getFalse());
4671 return replaceInstUsesWith(I, Builder->getTrue());
4683 return replaceInstUsesWith(I, Builder->getTrue());
4702 bool Changed =
false;
4716 return replaceInstUsesWith(I, V);
4751 if ((
SI->getOperand(1) == Op0 &&
SI->getOperand(2) == Op1) ||
4752 (
SI->getOperand(2) == Op0 &&
SI->getOperand(1) == Op1))
4756 if (
Constant *RHSC = dyn_cast<Constant>(Op1)) {
4757 if (
Instruction *LHSI = dyn_cast<Instruction>(Op0))
4759 case Instruction::FPExt: {
4761 FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
4799 case Instruction::PHI:
4807 case Instruction::SIToFP:
4808 case Instruction::UIToFP:
4812 case Instruction::FSub: {
4822 dyn_cast<GetElementPtrInst>(LHSI->
getOperand(0))) {
4826 if (
Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
4834 CallInst *CI = cast<CallInst>(LHSI);
4836 if (IID != Intrinsic::fabs)
4873 if (
FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
4874 if (
FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
4875 if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
4877 RHSExt->getOperand(0));
4879 return Changed ? &I :
nullptr;
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
bool isArithmeticShift() const
Return true if this is an arithmetic shift right.
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
const Use & getOperandUse(unsigned i) const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
APInt ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
void computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
void push_back(const T &Elt)
static bool isEquality(Predicate Pred)
A parsed version of the target data layout string in and methods for querying it. ...
Type * getIndexedType() const
static ConstantInt * getFalse(LLVMContext &Context)
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to have exactly one bit set when defined. ...
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This class is the base class for the comparison instructions.
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
iterator_range< use_iterator > uses()
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
DiagnosticInfoOptimizationBase::Argument NV
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty >, MaxMin_match< ICmpInst, RHS, LHS, umin_pred_ty > > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
STATISTIC(NumFunctions,"Total number of functions")
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
match_zero m_Zero()
Match an arbitrary zero/null constant.
void setBit(unsigned bitPosition)
Set a given bit to 1.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
This class represents zero extension of integer types.
unsigned getNumOperands() const
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
This class represents a function call, abstracting a target machine's calling convention.
static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth, bool isSignCheck)
When performing a comparison against a constant, it is possible that not all the bits in the LHS are ...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isSigned() const
Determine if this instruction is using a signed comparison.
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
0 1 0 0 True if ordered and less than
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
static Constant * getNUWShl(Constant *C1, Constant *C2)
1 1 1 0 True if unordered or not equal
void Add(Instruction *I)
Add - Add the specified instruction to the worklist if it isn't already in it.
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an FCmpInst, fold the result or return null.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
const Function * getParent() const
Return the enclosing method, or null if none.
static bool subWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
Compute Result = In1-In2, returning true if the result overflowed for this type.
const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers...
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
static Constant * getTrue(Type *Ty)
For a boolean type, or a vector of boolean type, return true, or a vector with every element true...
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
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...
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, const unsigned SIOpd)
Try to replace select with select operand SIOpd in SI-ICmp sequence.
fneg_match< LHS > m_FNeg(const LHS &L)
Match a floating point negate.
static ConstantInt * extractElement(Constant *V, Constant *Idx)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
static bool addWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
Compute Result = In1+In2, returning true if the result overflowed for this type.
size_type size() const
Determine the number of elements in the SetVector.
unsigned getNumIndices() const
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
static void computeSignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
Given a signed integer type and a set of known zero and one bits, compute the maximum and minimum val...
Type * getPointerElementType() const
uint64_t getArrayNumElements() const
static bool isBranchOnSignBitCheck(ICmpInst &I, bool isSignBit)
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparis...
static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit...
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.
Intrinsic::ID getIntrinsicForCallSite(ImmutableCallSite ICS, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Value * rewriteGEPAsOffset(Value *Start, Value *Base, const DataLayout &DL, SetVector< Value * > &Explored)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
APInt Not(const APInt &APIVal)
Bitwise complement function.
bool isNegative() const
Determine sign of this APInt.
1 0 0 1 True if unordered or equal
bool match(Val *V, const Pattern &P)
StructType * getStructTypeOrNull() const
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
static bool hasSubOverflow(ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned)
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
static const fltSemantics & x87DoubleExtended()
This class represents the LLVM 'select' instruction.
OverflowCheckFlavor
Specific patterns of overflow check idioms that we match.
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.
const APInt & getValue() const
Return the constant as an APInt value reference.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Class to represent struct types.
Type * getArrayElementType() const
A Use represents the edge between a Value definition and its users.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The core instruction combiner logic.
static Constant * AddOne(Constant *C)
Add one to a Constant.
static void computeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
Given an unsigned integer type and a set of known zero and one bits, compute the maximum and minimum ...
0 1 0 1 True if ordered and less than or equal
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
not_match< LHS > m_Not(const LHS &L)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
LLVM_NODISCARD bool empty() const
APInt lshr(const APInt &LHS, unsigned shiftAmt)
Logical right-shift function.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
APInt shl(unsigned shiftAmt) const
Left-shift function.
static Instruction * transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, const DataLayout &DL)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
A constant value that is initialized with an expression using other constant values.
bool insert(const value_type &X)
Insert a new element into the SetVector.
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Class to represent array types.
This instruction compares its operands according to the predicate given to the constructor.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
static Instruction * processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombiner &IC)
The caller has matched a pattern of the form: I = icmp ugt (add (add A, B), CI2), CI1 If this is of t...
bool sgt(const APInt &RHS) const
Signed greather than comparison.
This class represents a no-op cast from one type to another.
static CmpInst * Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power of 2.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
void takeName(Value *V)
Transfer the name from V to this value.
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
static bool isChainSelectCmpBranch(const SelectInst *SI)
Return true when the instruction sequence within a block is select-cmp-br.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
This class represents a truncation of integer types.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
static std::pair< Value *, Value * > getAsConstantIndexedAddress(Value *V, const DataLayout &DL)
Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express the input Value as a constant...
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
static const fltSemantics & IEEEsingle()
uint64_t getElementOffset(unsigned Idx) const
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
OneUse_match< T > m_OneUse(const T &SubPattern)
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
static Constant * getFNeg(Constant *C)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
bool eq(const APInt &RHS) const
Equality comparison.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
static void setInsertionPoint(IRBuilder<> &Builder, Value *V, bool Before=true)
APInt trunc(unsigned width) const
Truncate to new width.
LLVM Basic Block Representation.
bool isExact() const
Determine whether the exact flag is set.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Conditional or Unconditional Branch instruction.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isEquality() const
Return true if this predicate is either EQ or NE.
bool isMaxValue(bool isSigned) const
This function will return true iff this constant represents the largest value that may be represented...
This is an important base class in LLVM.
bool isMaxSignedValue() const
Determine if this is the largest signed value.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
static Constant * getAnd(Constant *C1, Constant *C2)
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
static ManagedStatic< OptionRegistry > OR
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
ConstantFP - Floating Point Values [float, double].
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static const fltSemantics & IEEEhalf()
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set to true.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static const unsigned End
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Instruction * visitICmpInst(ICmpInst &I)
bool isMaxValue() const
Determine if this is the largest unsigned value.
Value * getOperand(unsigned i) const
0 1 1 1 True if ordered (no nans)
bool isCommutative() const
Return true if the instruction is commutative:
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
unsigned getIntegerBitWidth() const
Class to represent integer types.
match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, RHS, LHS, smax_pred_ty > > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
Type * getSrcTy() const
Return the source type, as a convenience.
Predicate getPredicate() const
Return the predicate for this instruction.
static Constant * getNot(Constant *C)
match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, RHS, LHS, umax_pred_ty > > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
opStatus convertToInteger(integerPart *Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
bool isFP128Ty() const
Return true if this is 'fp128'.
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.
static const fltSemantics & IEEEquad()
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
LLVMContext & getContext() const
All values hold a context through their type.
static cl::opt< unsigned > MaxIter("bb-vectorize-max-iter", cl::init(0), cl::Hidden, cl::desc("The maximum number of pairing iterations"))
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
1 1 0 1 True if unordered, less than, or equal
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
InstCombineWorklist & Worklist
A worklist of the instructions that need to be simplified.
neg_match< LHS > m_Neg(const LHS &L)
Match an integer negate.
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
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...
0 0 1 0 True if ordered and greater than
static Constant * getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced=false)
BinaryOps getOpcode() const
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
This is the shared class of boolean and integer constants.
SelectPatternFlavor Flavor
bool slt(const APInt &RHS) const
Signed less than comparison.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
unsigned logBase2() const
1 1 0 0 True if unordered or less than
Type * getType() const
All values are typed, get the type of this value.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
This class represents a range of values.
const APInt & getLower() const
Return the lower value for this range.
bool isTrueWhenEqual() const
This is just a convenience.
LLVM_NODISCARD T pop_back_val()
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
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.
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...
static Instruction * foldICmpWithMinMax(ICmpInst &Cmp)
Fold icmp Pred min|max(X, Y), X.
int32_t exactLogBase2() const
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
static ConstantInt * getTrue(LLVMContext &Context)
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
static ICmpInst * canonicalizeCmpWithConstant(ICmpInst &I)
If we have an icmp le or icmp ge instruction with a constant operand, turn it into the appropriate ic...
Class for arbitrary precision integers.
static Value * evaluateGEPOffsetExpression(User *GEP, InstCombiner &IC, const DataLayout &DL)
Return a value that can be used to compare the offset implied by a GEP to zero.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool isIntegerTy() const
True if this is an instance of IntegerType.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
iterator_range< user_iterator > users()
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
unsigned getVectorNumElements() const
static Constant * getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Instruction * foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl, const APInt *C)
Fold icmp (shl 1, Y), C.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
bool isMinValue() const
Determine if this is the smallest unsigned value.
static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C)
Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the pre...
static const fltSemantics & IEEEdouble()
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
bool isAllOnesValue() const
Determine if all bits are set.
unsigned countLeadingOnes() const
Count the number of leading one bits.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
bool isX86_FP80Ty() const
Return true if this is x86 long double.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty >, MaxMin_match< ICmpInst, RHS, LHS, smin_pred_ty > > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
unsigned greater or equal
void clearBit(unsigned bitPosition)
Set a given bit to 0.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static Constant * getOr(Constant *C1, Constant *C2)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
0 1 1 0 True if ordered and operands are unequal
static const fltSemantics & PPCDoubleDouble()
iterator find(const KeyT &Val)
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
unsigned countTrailingOnes() const
Count the number of trailing one bits.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
static Instruction * processUMulZExtIdiom(ICmpInst &I, Value *MulVal, Value *OtherVal, InstCombiner &IC)
Recognize and process idiom involving test for multiplication overflow.
1 0 1 0 True if unordered or greater than
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
const APInt & getUpper() const
Return the upper value for this range.
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
static bool swapMayExposeCSEOpportunities(const Value *Op0, const Value *Op1)
Check if the order of Op0 and Op1 as operand in an ICmpInst should be swapped.
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
unsigned ComputeNumSignBits(Value *Op, unsigned Depth=0, Instruction *CxtI=nullptr) const
0 0 0 1 True if ordered and equal
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
1 0 1 1 True if unordered, greater than, or equal
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Returns true if the given value is known be positive (i.e.
static Constant * getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced=false)
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
cmpResult compare(const APFloat &RHS) const
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
Instruction * visitFCmpInst(FCmpInst &I)
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
bool dominatesAllUses(const Instruction *DI, const Instruction *UI, const BasicBlock *DB) const
True when DB dominates all uses of DI except UI.
APInt zext(unsigned width) const
Zero extend to a new width.
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
static bool isVolatile(Instruction *Inst)
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
APInt abs() const
Get the absolute value;.
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
This class represents an extension of floating point types.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
static bool hasAddOverflow(ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned)
0 0 1 1 True if ordered and greater than or equal
static bool canRewriteGEPAsOffset(Value *Start, Value *Base, const DataLayout &DL, SetVector< Value * > &Explored)
Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an ICmpInst, fold the result or return null.
std::size_t countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
const fltSemantics & getSemantics() const
const BasicBlock * getParent() const
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
int ilogb(const IEEEFloat &Arg)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
A wrapper class for inspecting calls to intrinsic functions.
an instruction to allocate memory on the stack
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
static Constant * getXor(Constant *C1, Constant *C2)
gep_type_iterator gep_type_begin(const User *GEP)