30 using namespace PatternMatch;
32 #define DEBUG_TYPE "instcombine"
35 STATISTIC(NumSel,
"Number of select opts");
61 Constant *In2,
bool IsSigned =
false) {
65 for (
unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
77 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
96 Constant *In2,
bool IsSigned =
false) {
100 for (
unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
112 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
121 bool &TrueIfSigned) {
130 TrueIfSigned =
false;
173 return (~CI->
getValue() + 1).isPowerOf2();
181 const APInt& KnownOne,
186 "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
187 APInt UnknownBits = ~(KnownZero|KnownOne);
192 Max = KnownOne|UnknownBits;
194 if (UnknownBits.isNegative()) {
205 const APInt &KnownOne,
210 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
211 APInt UnknownBits = ~(KnownZero|KnownOne);
216 Max = KnownOne|UnknownBits;
233 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
237 if (ArrayElementCount > 1024)
return nullptr;
257 if (!Idx)
return nullptr;
260 if ((
unsigned)IdxVal != IdxVal)
return nullptr;
262 if (
StructType *STy = dyn_cast<StructType>(EltTy))
263 EltTy = STy->getElementType(IdxVal);
264 else if (
ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
265 if (IdxVal >= ATy->getNumElements())
return nullptr;
266 EltTy = ATy->getElementType();
274 enum { Overdefined = -3, Undefined = -2 };
283 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
287 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
295 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
300 uint64_t MagicBitvector = 0;
304 for (
unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
306 if (!Elt)
return nullptr;
309 if (!LaterIndices.
empty())
317 CompareRHS,
DL, TLI);
319 if (isa<UndefValue>(C)) {
322 if (TrueRangeEnd == (
int)i-1)
324 if (FalseRangeEnd == (
int)i-1)
331 if (!isa<ConstantInt>(C))
return nullptr;
335 bool IsTrueForElt = !cast<ConstantInt>(C)->
isZero();
340 if (FirstTrueElement == Undefined)
341 FirstTrueElement = TrueRangeEnd = i;
344 if (SecondTrueElement == Undefined)
345 SecondTrueElement = i;
347 SecondTrueElement = Overdefined;
350 if (TrueRangeEnd == (
int)i-1)
353 TrueRangeEnd = Overdefined;
357 if (FirstFalseElement == Undefined)
358 FirstFalseElement = FalseRangeEnd = i;
361 if (SecondFalseElement == Undefined)
362 SecondFalseElement = i;
364 SecondFalseElement = Overdefined;
367 if (FalseRangeEnd == (
int)i-1)
370 FalseRangeEnd = Overdefined;
376 if (i < 64 && IsTrueForElt)
377 MagicBitvector |= 1ULL << i;
382 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
383 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
384 FalseRangeEnd == Overdefined)
399 Idx = Builder->CreateTrunc(Idx, IntPtrTy);
404 if (SecondTrueElement != Overdefined) {
406 if (FirstTrueElement == Undefined)
407 return ReplaceInstUsesWith(ICI, Builder->getFalse());
412 if (SecondTrueElement == Undefined)
416 Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
418 Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
419 return BinaryOperator::CreateOr(C1, C2);
424 if (SecondFalseElement != Overdefined) {
426 if (FirstFalseElement == Undefined)
427 return ReplaceInstUsesWith(ICI, Builder->getTrue());
432 if (SecondFalseElement == Undefined)
436 Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
438 Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
439 return BinaryOperator::CreateAnd(C1, C2);
444 if (TrueRangeEnd != Overdefined) {
445 assert(TrueRangeEnd != FirstTrueElement &&
"Should emit single compare");
448 if (FirstTrueElement) {
450 Idx = Builder->CreateAdd(Idx, Offs);
454 TrueRangeEnd-FirstTrueElement+1);
459 if (FalseRangeEnd != Overdefined) {
460 assert(FalseRangeEnd != FirstFalseElement &&
"Should emit single compare");
462 if (FirstFalseElement) {
464 Idx = Builder->CreateAdd(Idx, Offs);
468 FalseRangeEnd-FirstFalseElement);
483 if (ArrayElementCount <= Idx->
getType()->getIntegerBitWidth())
486 Ty =
DL.getSmallestLegalIntType(Init->
getContext(), ArrayElementCount);
489 Value *V = Builder->CreateIntCast(Idx, Ty,
false);
521 for (i = 1; i != e; ++i, ++GTI) {
524 if (CI->isZero())
continue;
527 if (
StructType *STy = dyn_cast<StructType>(*GTI)) {
531 Offset += Size*CI->getSExtValue();
541 if (i == e)
return nullptr;
549 for (++i, ++GTI; i != e; ++i, ++GTI) {
551 if (!CI)
return nullptr;
554 if (CI->
isZero())
continue;
557 if (
StructType *STy = dyn_cast<StructType>(*GTI)) {
584 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
586 Offset &= PtrSizeMask;
587 VariableScale &= PtrSizeMask;
593 int64_t NewOffs = Offset / (int64_t)VariableScale;
594 if (Offset != NewOffs*(int64_t)VariableScale)
598 if (VariableIdx->
getType() != IntPtrTy)
620 if (!isa<GetElementPtrInst>(RHS))
636 }
else if (
GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
639 if (PtrBase != GEPRHS->getOperand(0)) {
640 bool IndicesTheSame = GEPLHS->
getNumOperands()==GEPRHS->getNumOperands();
642 GEPRHS->getOperand(0)->getType();
645 if (GEPLHS->
getOperand(i) != GEPRHS->getOperand(i)) {
646 IndicesTheSame =
false;
657 if (GEPLHS->
isInBounds() && GEPRHS->isInBounds() &&
659 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
670 if (LHSIndexTy != RHSIndexTy) {
673 ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
675 LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
680 return ReplaceInstUsesWith(I, Cmp);
690 return FoldGEPICmp(GEPRHS, GEPLHS->
getOperand(0),
694 if (GEPRHS->hasAllZeroIndices())
695 return FoldGEPICmp(GEPLHS, GEPRHS->
getOperand(0), Cond,
I);
697 bool GEPsInBounds = GEPLHS->
isInBounds() && GEPRHS->isInBounds();
700 unsigned NumDifferences = 0;
701 unsigned DiffOperand = 0;
702 for (
unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
703 if (GEPLHS->
getOperand(i) != GEPRHS->getOperand(i)) {
705 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
710 if (NumDifferences++)
break;
715 if (NumDifferences == 0)
716 return ReplaceInstUsesWith(I,
719 else if (NumDifferences == 1 && GEPsInBounds) {
721 Value *RHSV = GEPRHS->getOperand(DiffOperand);
729 if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->
hasOneUse()) &&
730 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
803 bool DivIsSigned = DivI->
getOpcode() == Instruction::SDiv;
810 if (DivRHS->
isOne()) {
842 int LoOverflow = 0, HiOverflow = 0;
843 Constant *LoBound =
nullptr, *HiBound =
nullptr;
848 HiOverflow = LoOverflow = ProdOV;
860 }
else if (CmpRHSV.isStrictlyPositive()) {
862 HiOverflow = LoOverflow = ProdOV;
868 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
879 LoBound =
AddOne(RangeSize);
881 if (HiBound == DivRHS) {
885 }
else if (CmpRHSV.isStrictlyPositive()) {
888 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
890 LoOverflow =
AddWithOverflow(LoBound, HiBound, RangeSize,
true) ? -1:0;
893 LoOverflow = HiOverflow = ProdOV;
906 if (LoOverflow && HiOverflow)
907 return ReplaceInstUsesWith(ICI, Builder->getFalse());
914 return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
917 if (LoOverflow && HiOverflow)
918 return ReplaceInstUsesWith(ICI, Builder->getTrue());
921 ICmpInst::ICMP_ULT, X, LoBound);
924 ICmpInst::ICMP_UGE, X, HiBound);
925 return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
926 DivIsSigned,
false));
927 case ICmpInst::ICMP_ULT:
929 if (LoOverflow == +1)
930 return ReplaceInstUsesWith(ICI, Builder->getTrue());
931 if (LoOverflow == -1)
932 return ReplaceInstUsesWith(ICI, Builder->getFalse());
933 return new ICmpInst(Pred, X, LoBound);
936 if (HiOverflow == +1)
937 return ReplaceInstUsesWith(ICI, Builder->getFalse());
938 if (HiOverflow == -1)
939 return ReplaceInstUsesWith(ICI, Builder->getTrue());
941 return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
949 const APInt &CmpRHSV = cast<ConstantInt>(ICI.
getOperand(1))->getValue();
956 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
968 if (Shr->
getOpcode() == Instruction::AShr &&
969 (!Shr->
isExact() || ShAmtVal == TypeBits - 1))
991 assert(TheDiv->
getOpcode() == Instruction::SDiv ||
992 TheDiv->
getOpcode() == Instruction::UDiv);
994 Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst));
995 assert(Res &&
"This div/cst should have folded!");
1002 APInt Comp = CmpRHSV << ShAmtVal;
1003 ConstantInt *ShiftedCmpRHS = Builder->getInt(Comp);
1004 if (Shr->
getOpcode() == Instruction::LShr)
1005 Comp = Comp.
lshr(ShAmtVal);
1007 Comp = Comp.
ashr(ShAmtVal);
1009 if (Comp != CmpRHSV) {
1011 Constant *Cst = Builder->getInt1(IsICMP_NE);
1012 return ReplaceInstUsesWith(ICI, Cst);
1024 Constant *Mask = Builder->getInt(Val);
1027 Mask, Shr->
getName()+
".mask");
1039 assert(I.
isEquality() &&
"Cannot fold icmp gt/lt");
1041 auto getConstant = [&
I,
this](
bool IsTrue) {
1050 return new ICmpInst(Pred, LHS, RHS);
1059 bool IsAShr = isa<AShrOperator>(Op);
1082 Shift = (~AP2).logBase2() - (~AP1).
logBase2();
1087 if (IsAShr ? AP1 == AP2.
ashr(Shift) : AP1 == AP2.
lshr(Shift))
1091 return getConstant(
false);
1099 assert(I.
isEquality() &&
"Cannot fold icmp gt/lt");
1101 auto getConstant = [&
I,
this](
bool IsTrue) {
1110 return new ICmpInst(Pred, LHS, RHS);
1122 if (!AP1 && AP2TrailingZeros != 0)
1132 if (Shift > 0 && AP2.
shl(Shift) == AP1)
1136 return getConstant(
false);
1147 case Instruction::Trunc:
1153 APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
1157 if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
1162 Builder->getInt(NewRHS));
1177 if (!XorCst->isNegative()) {
1187 isTrueIfPositive ^=
true;
1189 if (isTrueIfPositive)
1199 if (!ICI.
isEquality() && XorCst->getValue().isSignBit()) {
1200 const APInt &SignBit = XorCst->getValue();
1205 Builder->getInt(RHSV ^ SignBit));
1209 if (!ICI.
isEquality() && XorCst->isMaxValue(
true)) {
1210 const APInt &NotSignBit = XorCst->getValue();
1216 Builder->getInt(RHSV ^ NotSignBit));
1223 XorCst->getValue() == ~RHSV && (RHSV + 1).isPowerOf2())
1229 XorCst->getValue() == -RHSV && RHSV.isPowerOf2())
1249 Builder->CreateAnd(Cast->getOperand(0),
1260 IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy());
1265 Builder->CreateAnd(Cast->getOperand(0),
1278 if (Shift && !Shift->
isShift())
1288 bool CanFold =
false;
1289 unsigned ShiftOpcode = Shift->
getOpcode();
1290 if (ShiftOpcode == Instruction::AShr) {
1294 }
else if (ShiftOpcode == Instruction::Shl) {
1302 }
else if (ShiftOpcode == Instruction::LShr) {
1316 if (!ShiftedAndCst->
isNegative() && !ShiftedRHSCst->isNegative())
1323 if (ShiftOpcode == Instruction::Shl)
1335 return ReplaceInstUsesWith(ICI, Builder->getFalse());
1337 return ReplaceInstUsesWith(ICI, Builder->getTrue());
1341 if (ShiftOpcode == Instruction::Shl)
1347 Worklist.Add(Shift);
1356 if (Shift && Shift->
hasOneUse() && RHSV == 0 &&
1361 if (Shift->
getOpcode() == Instruction::LShr) {
1362 NS = Builder->CreateShl(AndCst, Shift->
getOperand(1));
1365 NS = Builder->CreateLShr(AndCst, Shift->
getOperand(1));
1382 if (!ICI.
isSigned() && RHSV == 0) {
1388 unsigned UsesRemoved = 0;
1391 if (Or->hasOneUse())
1395 Value *NewOr =
nullptr;
1397 if (
auto *C = dyn_cast<Constant>(Y)) {
1398 if (UsesRemoved >= 1)
1402 if (UsesRemoved >= 3)
1403 NewOr = Builder->CreateOr(Builder->CreateShl(One, Y,
1409 Value *NewAnd = Builder->CreateAnd(X, NewOr, LHSI->
getName());
1432 dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1434 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1435 !LI->isVolatile() && isa<ConstantInt>(LHSI->
getOperand(1))) {
1437 if (
Instruction *Res = FoldCmpLoadFromIndexedGlobal(
GEP, GV,ICI, C))
1465 Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
1467 Op = BinaryOperator::CreateOr(ICIP, ICIQ);
1473 case Instruction::Mul: {
1481 cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1490 case Instruction::Shl: {
1500 if (!RHSVIsPowerOf2) {
1510 unsigned RHSLog2 = RHSV.
logBase2();
1514 if (RHSLog2 == TypeBits-1) {
1559 if (ShAmt->
uge(TypeBits))
1570 Constant *Cst = Builder->getInt1(IsICMP_NE);
1571 return ReplaceInstUsesWith(ICI, Cst);
1576 if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap())
1582 if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0)
1590 TypeBits - ShAmtVal));
1603 cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1609 bool TrueIfSigned =
false;
1637 Builder->CreateTrunc(LHSI->
getOperand(0), NTy),
1644 case Instruction::LShr:
1645 case Instruction::AShr: {
1649 if (
Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
1661 case Instruction::SDiv:
1662 case Instruction::UDiv:
1670 if (
Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
1675 case Instruction::Sub: {
1684 RHSV.
isPowerOf2() && (LHSV & (RHSV - 1)) == (RHSV - 1))
1686 Builder->CreateOr(LHSI->
getOperand(1), RHSV - 1),
1693 (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == RHSV)
1695 Builder->CreateOr(LHSI->
getOperand(1), RHSV), LHSC);
1699 case Instruction::Add:
1731 RHSV.
isPowerOf2() && (LHSV & (RHSV - 1)) == 0)
1733 Builder->CreateAnd(LHSI->
getOperand(0), -RHSV),
1740 (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == 0)
1742 Builder->CreateAnd(LHSI->
getOperand(0), ~RHSV),
1755 switch (BO->getOpcode()) {
1756 case Instruction::SRem:
1758 if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
1759 const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
1762 Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
1769 case Instruction::Add:
1771 if (
ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1772 if (BO->hasOneUse())
1775 }
else if (RHSV == 0) {
1778 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1780 if (
Value *NegVal = dyn_castNegVal(BOp1))
1782 if (
Value *NegVal = dyn_castNegVal(BOp0))
1784 if (BO->hasOneUse()) {
1785 Value *Neg = Builder->CreateNeg(BOp1);
1794 if (
Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
1797 }
else if (RHSV == 0) {
1803 case Instruction::Sub:
1805 if (
ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) {
1806 if (BO->hasOneUse())
1809 }
else if (RHSV == 0) {
1818 if (
ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1821 return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
1826 if (
ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1829 if ((RHSV & ~BOC->getValue()) != 0)
1830 return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
1839 if (!BO->hasOneUse())
1843 if (BOC->getValue().isSignBit()) {
1844 Value *
X = BO->getOperand(0);
1848 return new ICmpInst(pred, X, Zero);
1853 Value *
X = BO->getOperand(0);
1857 return new ICmpInst(pred, X, NegX);
1861 case Instruction::Mul:
1862 if (RHSV == 0 && BO->hasNoSignedWrap()) {
1863 if (
ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1875 }
else if (
IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
1877 switch (II->getIntrinsicID()) {
1878 case Intrinsic::bswap:
1883 case Intrinsic::ctlz:
1884 case Intrinsic::cttz:
1893 case Intrinsic::ctpop:
1916 Type *SrcTy = LHSCIOp->getType();
1922 if (LHSCI->
getOpcode() == Instruction::PtrToInt &&
1924 Value *RHSOp =
nullptr;
1926 Value *RHSCIOp = RHSC->getOperand(0);
1928 LHSCIOp->getType()->getPointerAddressSpace()) {
1929 RHSOp = RHSC->getOperand(0);
1931 if (LHSCIOp->getType() != RHSOp->
getType())
1932 RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->
getType());
1943 if (LHSCI->
getOpcode() != Instruction::ZExt &&
1944 LHSCI->
getOpcode() != Instruction::SExt)
1947 bool isSignedExt = LHSCI->
getOpcode() == Instruction::SExt;
1952 RHSCIOp = CI->getOperand(0);
1953 if (RHSCIOp->
getType() != LHSCIOp->getType())
1958 if (CI->getOpcode() != LHSCI->
getOpcode())
1967 if (isSignedCmp && isSignedExt)
1993 if (isSignedExt && isSignedCmp)
2005 if (isSignedCmp || !isSignedExt)
2014 Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.
getName());
2018 return ReplaceInstUsesWith(ICI, Result);
2042 if (!AddWithCst->
hasOneUse())
return nullptr;
2047 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
return nullptr;
2060 unsigned NeededSignBits = CI1->
getBitWidth() - NewWidth + 1;
2071 if (U == AddWithCst)
continue;
2115 if (OrigI.
isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
2118 auto SetResult = [&](
Value *OpResult,
Constant *OverflowVal,
bool ReuseName) {
2120 Overflow = OverflowVal;
2133 return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(),
2137 return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(),
true);
2143 return SetResult(LHS, Builder->getFalse(),
false);
2148 if (WillNotOverflowSignedAdd(LHS, RHS, OrigI))
2149 return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(),
2158 return SetResult(LHS, Builder->getFalse(),
false);
2161 if (WillNotOverflowSignedSub(LHS, RHS, OrigI))
2162 return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(),
2165 if (WillNotOverflowUnsignedSub(LHS, RHS, OrigI))
2166 return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(),
2175 return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(),
2178 return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(),
true);
2182 if (isa<UndefValue>(RHS))
2187 return SetResult(RHS, Builder->getFalse(),
false);
2191 return SetResult(LHS, Builder->getFalse(),
false);
2194 if (WillNotOverflowSignedMul(LHS, RHS, OrigI))
2195 return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(),
2221 if (!isa<IntegerType>(MulVal->
getType()))
2226 Instruction *MulInstr = cast<Instruction>(MulVal);
2227 assert(MulInstr->getOpcode() == Instruction::Mul);
2229 auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)),
2230 *RHS = cast<ZExtOperator>(MulInstr->getOperand(1));
2231 assert(LHS->getOpcode() == Instruction::ZExt);
2232 assert(RHS->getOpcode() == Instruction::ZExt);
2233 Value *
A = LHS->getOperand(0), *B = RHS->getOperand(0);
2236 Type *TyA = A->getType(), *TyB = B->getType();
2238 WidthB = TyB->getPrimitiveSizeInBits();
2241 if (WidthB > WidthA) {
2256 if (
TruncInst *TI = dyn_cast<TruncInst>(U)) {
2259 if (TruncWidth > MulWidth)
2265 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
2266 const APInt &CVal = CI->getValue();
2283 if (
ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal))
2284 if (Zext->hasOneUse()) {
2285 Value *ZextArg = Zext->getOperand(0);
2286 if (
TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg))
2287 if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth)
2297 if (ValToMask != MulVal)
2301 unsigned MaskWidth = CVal.
logBase2();
2302 if (MaskWidth == MulWidth)
2312 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
2324 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
2335 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
2347 if (
ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
2363 Value *MulA =
A, *MulB = B;
2364 if (WidthA < MulWidth)
2366 if (WidthB < MulWidth)
2379 if (U == &I || U == OtherVal)
2381 if (
TruncInst *TI = dyn_cast<TruncInst>(U)) {
2382 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
2389 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
2390 APInt ShortMask = CI->getValue().
trunc(MulWidth);
2402 if (isa<Instruction>(OtherVal))
2441 unsigned BitWidth,
bool isSignCheck) {
2483 const Value * Op1) {
2495 int GlobalSwapBenefits = 0;
2498 if (!BinOp || BinOp->
getOpcode() != Instruction::Sub)
2502 int LocalSwapBenefits = -1;
2503 unsigned Op1Idx = 1;
2506 LocalSwapBenefits = 1;
2510 GlobalSwapBenefits += LocalSwapBenefits;
2512 return GlobalSwapBenefits > 0;
2528 assert(DI && UI &&
"Instruction not defined\n");
2542 auto *Usr = cast<Instruction>(U);
2543 if (Usr != UI && !DT->dominates(DB, Usr->
getParent()))
2556 auto *BI = dyn_cast_or_null<BranchInst>(BB->
getTerminator());
2557 if (!BI || BI->getNumSuccessors() != 2)
2560 if (!IC || (IC->getOperand(0) != SI && IC->
getOperand(1) != SI))
2609 const unsigned SIOpd) {
2610 assert((SIOpd == 1 || SIOpd == 2) &&
"Invalid select operand!");
2632 bool Changed =
false;
2640 if (Op0Cplxity < Op1Cplxity ||
2641 (Op0Cplxity == Op1Cplxity &&
2650 return ReplaceInstUsesWith(I, V);
2656 Value *Cond, *SelectTrue, *SelectFalse;
2659 if (
Value *V = dyn_castNegVal(SelectTrue)) {
2660 if (V == SelectFalse)
2663 else if (
Value *V = dyn_castNegVal(SelectFalse)) {
2664 if (V == SelectTrue)
2681 return BinaryOperator::CreateXor(Op0, Op1);
2688 return BinaryOperator::CreateAnd(Not, Op1);
2695 return BinaryOperator::CreateAnd(Not, Op0);
2702 return BinaryOperator::CreateOr(Not, Op1);
2709 return BinaryOperator::CreateOr(Not, Op0);
2714 unsigned BitWidth = 0;
2720 bool isSignBit =
false;
2723 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2724 Value *
A =
nullptr, *B =
nullptr;
2782 assert(!CI->isMaxValue(
false));
2784 Builder->getInt(CI->getValue()+1));
2786 assert(!CI->isMaxValue(
true));
2788 Builder->getInt(CI->getValue()+1));
2790 assert(!CI->isMinValue(
false));
2792 Builder->getInt(CI->getValue()-1));
2794 assert(!CI->isMinValue(
true));
2796 Builder->getInt(CI->getValue()-1));
2804 if (
Instruction *Inst = FoldICmpCstShrCst(I, Op0, A, CI, CI2))
2809 if (
Instruction *Inst = FoldICmpCstShlCst(I, Op0, A, CI, CI2))
2822 if (BitWidth != 0) {
2823 APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
2824 APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
2828 Op0KnownZero, Op0KnownOne, 0))
2838 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
2839 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
2855 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
2858 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
2867 if (Op0Max.
ult(Op1Min) || Op0Min.ugt(Op1Max))
2873 APInt Op0KnownZeroInverted = ~Op0KnownZero;
2874 if (~Op1KnownZero == 0) {
2876 Value *LHS =
nullptr;
2879 LHSC->
getValue() != Op0KnownZeroInverted)
2887 APInt ValToCheck = Op0KnownZeroInverted;
2892 }
else if ((++ValToCheck).isPowerOf2()) {
2902 if (Op0KnownZeroInverted == 1 &&
2912 if (Op0Max.
ult(Op1Min) || Op0Min.ugt(Op1Max))
2918 APInt Op0KnownZeroInverted = ~Op0KnownZero;
2919 if (~Op1KnownZero == 0) {
2921 Value *LHS =
nullptr;
2924 LHSC->
getValue() != Op0KnownZeroInverted)
2932 APInt ValToCheck = Op0KnownZeroInverted;
2937 }
else if ((++ValToCheck).isPowerOf2()) {
2947 if (Op0KnownZeroInverted == 1 &&
2957 if (Op0Max.
ult(Op1Min))
2959 if (Op0Min.uge(Op1Max))
2961 if (Op1Min == Op0Max)
2963 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2964 if (Op1Max == Op0Min+1)
2966 Builder->getInt(CI->getValue()-1));
2969 if (CI->isMinValue(
true))
2975 if (Op0Min.ugt(Op1Max))
2977 if (Op0Max.
ule(Op1Min))
2980 if (Op1Max == Op0Min)
2982 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2983 if (Op1Min == Op0Max-1)
2985 Builder->getInt(CI->getValue()+1));
2988 if (CI->isMaxValue(
true))
2994 if (Op0Max.
slt(Op1Min))
2996 if (Op0Min.sge(Op1Max))
2998 if (Op1Min == Op0Max)
3000 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
3001 if (Op1Max == Op0Min+1)
3003 Builder->getInt(CI->getValue()-1));
3007 if (Op0Min.sgt(Op1Max))
3009 if (Op0Max.
sle(Op1Min))
3012 if (Op1Max == Op0Min)
3014 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
3015 if (Op1Min == Op0Max-1)
3017 Builder->getInt(CI->getValue()+1));
3021 assert(!isa<ConstantInt>(Op1) &&
"ICMP_SGE with ConstantInt not folded!");
3022 if (Op0Min.sge(Op1Max))
3024 if (Op0Max.
slt(Op1Min))
3028 assert(!isa<ConstantInt>(Op1) &&
"ICMP_SLE with ConstantInt not folded!");
3029 if (Op0Max.
sle(Op1Min))
3031 if (Op0Min.sgt(Op1Max))
3035 assert(!isa<ConstantInt>(Op1) &&
"ICMP_UGE with ConstantInt not folded!");
3036 if (Op0Min.uge(Op1Max))
3038 if (Op0Max.
ult(Op1Min))
3042 assert(!isa<ConstantInt>(Op1) &&
"ICMP_ULE with ConstantInt not folded!");
3043 if (Op0Max.
ule(Op1Min))
3045 if (Op0Min.ugt(Op1Max))
3053 ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
3054 (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
3067 if ((
SI->getOperand(1) == Op0 &&
SI->getOperand(2) == Op1) ||
3068 (
SI->getOperand(2) == Op0 &&
SI->getOperand(1) == Op1))
3073 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
3077 if (
Instruction *LHSI = dyn_cast<Instruction>(Op0))
3078 if (
Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
3083 if (
Constant *RHSC = dyn_cast<Constant>(Op1)) {
3084 if (
Instruction *LHSI = dyn_cast<Instruction>(Op0))
3085 switch (LHSI->getOpcode()) {
3086 case Instruction::GetElementPtr:
3088 if (RHSC->isNullValue() &&
3089 cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
3105 Value *Op1 =
nullptr, *Op2 =
nullptr;
3107 if (
Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
3111 if (
Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
3123 bool Transform =
false;
3126 else if (Op1 || Op2) {
3128 if (LHSI->hasOneUse())
3131 else if (CI && !CI->
isZero())
3135 Transform = replacedSelectWithOperand(cast<SelectInst>(LHSI), &
I,
3140 Op1 = Builder->CreateICmp(I.
getPredicate(), LHSI->getOperand(1),
3143 Op2 = Builder->CreateICmp(I.
getPredicate(), LHSI->getOperand(2),
3149 case Instruction::IntToPtr:
3151 if (RHSC->isNullValue() &&
3152 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
3160 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
3162 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
3183 if (
BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
3185 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
3188 Op0 = CI->getOperand(0);
3192 if (
BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
3193 Op1 = CI2->getOperand(0);
3196 if (Op0->
getType() != Op1->getType()) {
3197 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
3201 Op1 = Builder->CreateBitCast(Op1, Op0->
getType());
3208 if (isa<CastInst>(Op0)) {
3215 if (isa<Constant>(Op1) || isa<CastInst>(Op1))
3216 if (
Instruction *R = visitICmpInstWithCastAndCast(I))
3225 bool NoOp0WrapProblem =
false, NoOp1WrapProblem =
false;
3226 if (BO0 && isa<OverflowingBinaryOperator>(BO0))
3230 if (BO1 && isa<OverflowingBinaryOperator>(BO1))
3237 Value *
A =
nullptr, *B =
nullptr, *C =
nullptr, *D =
nullptr;
3238 if (BO0 && BO0->
getOpcode() == Instruction::Add)
3240 if (BO1 && BO1->getOpcode() == Instruction::Add)
3241 C = BO1->getOperand(0), D = BO1->getOperand(1);
3245 if (
ConstantInt *RHSC = dyn_cast_or_null<ConstantInt>(B))
3246 if (!RHSC->isMinValue(
true))
3250 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
3251 return new ICmpInst(Pred, A == Op1 ? B : A,
3255 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
3260 if (A && C && (A == C || A == D || B == C || B == D) &&
3261 NoOp0WrapProblem && NoOp1WrapProblem &&
3270 }
else if (A == D) {
3274 }
else if (B == C) {
3314 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
3318 const APInt &AP1 = C1->getValue();
3319 const APInt &AP2 = C2->getValue();
3321 APInt AP1Abs = C1->getValue().
abs();
3322 APInt AP2Abs = C2->getValue().
abs();
3323 if (AP1Abs.
uge(AP2Abs)) {
3325 Value *NewAdd = Builder->CreateNSWAdd(A, C3);
3326 return new ICmpInst(Pred, NewAdd, C);
3329 Value *NewAdd = Builder->CreateNSWAdd(C, C3);
3330 return new ICmpInst(Pred, A, NewAdd);
3338 A =
nullptr; B =
nullptr; C =
nullptr; D =
nullptr;
3339 if (BO0 && BO0->
getOpcode() == Instruction::Sub)
3341 if (BO1 && BO1->getOpcode() == Instruction::Sub)
3342 C = BO1->getOperand(0), D = BO1->getOperand(1);
3345 if (A == Op1 && NoOp0WrapProblem)
3349 if (C == Op0 && NoOp1WrapProblem)
3353 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
3359 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
3368 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
3369 if (!RHSC->isMinValue(
true))
3376 if (BO0 && BO0->
getOpcode() == Instruction::SRem &&
3380 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
3381 Op0 == BO1->getOperand(1))
3403 if (BO0 && BO1 && BO0->
getOpcode() == BO1->getOpcode() &&
3408 case Instruction::Add:
3409 case Instruction::Sub:
3413 BO1->getOperand(0));
3416 if (CI->getValue().isSignBit()) {
3421 BO1->getOperand(0));
3424 if (CI->isMaxValue(
true)) {
3430 BO1->getOperand(0));
3434 case Instruction::Mul:
3441 if (!CI->isZero() && !CI->isOne()) {
3442 const APInt &AP = CI->getValue();
3448 Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
3453 case Instruction::UDiv:
3454 case Instruction::LShr:
3458 case Instruction::SDiv:
3459 case Instruction::AShr:
3460 if (!BO0->
isExact() || !BO1->isExact())
3463 BO1->getOperand(0));
3464 case Instruction::Shl: {
3472 BO1->getOperand(0));
3486 Builder->CreateAnd(A, B),
3494 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
3501 isa<IntegerType>(A->
getType())) {
3506 ReplaceInstUsesWith(*AddI, Result);
3507 return ReplaceInstUsesWith(I, Overflow);
3526 if (A == Op1 || B == Op1) {
3527 Value *OtherVal = A == Op1 ? B :
A;
3538 Value *
Xor = Builder->CreateXor(C, NC);
3551 (A == Op0 || B == Op0)) {
3553 Value *OtherVal = A == Op0 ? B :
A;
3561 Value *
X =
nullptr, *
Y =
nullptr, *Z =
nullptr;
3564 X = B;
Y = D; Z =
A;
3565 }
else if (A == D) {
3566 X = B;
Y = C; Z =
A;
3567 }
else if (B == C) {
3568 X =
A;
Y = D; Z = B;
3569 }
else if (B == D) {
3570 X =
A;
Y = C; Z = B;
3574 Op1 = Builder->CreateXor(X,
Y);
3575 Op1 = Builder->CreateAnd(Op1, Z);
3588 (Op1->hasOneUse() &&
3595 Builder->CreateTrunc(B, A->
getType()));
3606 if (ShAmt < TypeBits && ShAmt != 0) {
3612 return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
3621 if (ShAmt < TypeBits && ShAmt != 0) {
3624 Value *
And = Builder->CreateAnd(Xor, Builder->getInt(AndVal),
3641 unsigned ASize =cast<IntegerType>(A->
getType())->getPrimitiveSizeInBits();
3643 if (ShAmt < ASize) {
3651 Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
3667 if (
auto *EVI = dyn_cast<ExtractValueInst>(Op0))
3668 if (
auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
3669 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
3683 return Changed ? &I :
nullptr;
3690 if (!isa<ConstantFP>(RHSC))
return nullptr;
3691 const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
3696 if (MantissaWidth == -1)
return nullptr;
3706 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
3712 bool IsExact =
false;
3724 return ReplaceInstUsesWith(I, Builder->getFalse());
3727 return ReplaceInstUsesWith(I, Builder->getTrue());
3740 if ((
int)InputSize > MantissaWidth && !IsCmpZero)
3746 assert(!RHS.
isNaN() &&
"NaN comparison not already folded!");
3776 return ReplaceInstUsesWith(I, Builder->getTrue());
3778 return ReplaceInstUsesWith(I, Builder->getFalse());
3796 return ReplaceInstUsesWith(I, Builder->getTrue());
3797 return ReplaceInstUsesWith(I, Builder->getFalse());
3808 return ReplaceInstUsesWith(I, Builder->getTrue());
3809 return ReplaceInstUsesWith(I, Builder->getFalse());
3821 return ReplaceInstUsesWith(I, Builder->getTrue());
3822 return ReplaceInstUsesWith(I, Builder->getFalse());
3832 return ReplaceInstUsesWith(I, Builder->getTrue());
3833 return ReplaceInstUsesWith(I, Builder->getFalse());
3845 bool Equal = LHSUnsigned
3855 return ReplaceInstUsesWith(I, Builder->getTrue());
3857 return ReplaceInstUsesWith(I, Builder->getFalse());
3862 return ReplaceInstUsesWith(I, Builder->getFalse());
3874 return ReplaceInstUsesWith(I, Builder->getFalse());
3887 return ReplaceInstUsesWith(I, Builder->getTrue());
3899 return ReplaceInstUsesWith(I, Builder->getTrue());
3918 bool Changed =
false;
3932 return ReplaceInstUsesWith(I, V);
3967 if ((
SI->getOperand(1) == Op0 &&
SI->getOperand(2) == Op1) ||
3968 (
SI->getOperand(2) == Op0 &&
SI->getOperand(1) == Op1))
3972 if (
Constant *RHSC = dyn_cast<Constant>(Op1)) {
3973 if (
Instruction *LHSI = dyn_cast<Instruction>(Op0))
3974 switch (LHSI->getOpcode()) {
3975 case Instruction::FPExt: {
3977 FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
4023 case Instruction::SIToFP:
4024 case Instruction::UIToFP:
4028 case Instruction::FSub: {
4038 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
4040 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
4047 if (!RHSC->isNullValue())
4050 CallInst *CI = cast<CallInst>(LHSI);
4057 if (F->getIntrinsicID() == Intrinsic::fabs ||
4058 (TLI->getLibFunc(F->getName(),
Func) && TLI->has(Func) &&
4059 (Func == LibFunc::fabs || Func == LibFunc::fabsf ||
4060 Func == LibFunc::fabsl))) {
4066 return ReplaceInstUsesWith(I, Builder->getFalse());
4095 if (
FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
4096 if (
FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
4097 if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
4099 RHSExt->getOperand(0));
4101 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
isArithmeticShift - Return true if this is an arithmetic shift right.
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the code necessary to compute th...
const Use & getOperandUse(unsigned i) const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
static bool isHighOnes(const ConstantInt *CI)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
void push_back(const T &Elt)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
static bool isEquality(Predicate Pred)
Determine if this is an equality predicate.
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...
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.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT byteSwap() const
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, Instruction *CxtI=nullptr)
SimplifyICmpInst - Given operands for an ICmpInst, see if we can fold the result. ...
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
FoldGEPICmp - Fold comparisons between a GEP instruction and something else.
static bool HasSubOverflow(ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned)
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
STATISTIC(NumFunctions,"Total number of functions")
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
static const fltSemantics IEEEdouble
A Module instance is used to store all the information related to an LLVM module. ...
static void ComputeSignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a set of known zero and one ...
match_zero m_Zero()
Match an arbitrary zero/null constant.
void setBit(unsigned bitPosition)
Set a given bit to 1.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
This class represents zero extension of integer types.
unsigned getNumOperands() const
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
static bool SubWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
SubWithOverflow - Compute Result = In1-In2, returning true if the result overflowed for this type...
CallInst - This class represents a function call, abstracting a target machine's calling convention...
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.
void computeKnownBits(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...
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
bool isDoubleTy() const
isDoubleTy - 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
getFPMantissaWidth - 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)
SimplifyFCmpInst - Given operands for an FCmpInst, see if we can fold the result. ...
static bool isEquality(Predicate P)
isEquality - 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.
Instruction * FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
LoadInst - an instruction for reading from memory.
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 * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, const unsigned SIOpd)
True when a select result is replaced by one of its operands in select-icmp sequence.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
fneg_match< LHS > m_FNeg(const LHS &L)
Match a floating point negate.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
static Constant * getNullValue(Type *Ty)
StringRef getName() const
Return a constant reference to the value's name.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
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.
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
1 0 0 1 True if unordered or equal
bool match(Val *V, const Pattern &P)
static const fltSemantics x87DoubleExtended
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
static Value * EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC, const DataLayout &DL)
EvaluateGEPOffsetExpression - Return a value that can be used to compare the offset implied by a GEP ...
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)
SelectInst - 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.
StructType - Class to represent struct types.
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, bool *) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Type * getArrayElementType() const
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
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...
unsigned logBase2(const APInt &APIVal)
Returns the floor log base 2 of the specified APInt value.
The core instruction combiner logic.
static Constant * AddOne(Constant *C)
Add one to a Constant.
0 1 0 1 True if ordered and less than or equal
static const fltSemantics IEEEquad
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
not_match< LHS > m_Not(const LHS &L)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
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. ...
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool isHalfTy() const
isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
ArrayType - Class to represent array types.
bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
isKnownToBeAPowerOfTwo - Return true if the given value is known to have exactly one bit set when def...
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...
bool sgt(const APInt &RHS) const
Signed greather than comparison.
This class represents a no-op cast from one type to another.
static APInt DemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth, bool isSignCheck)
unsigned getActiveBits() const
Compute the number of active bits in the value.
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 bool AddWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
AddWithOverflow - Compute Result = In1+In2, returning true if the result overflowed for this type...
APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const
Left-shift function.
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.
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
void takeName(Value *V)
Transfer the name from V to this value.
static bool isChainSelectCmpBranch(const SelectInst *SI)
true when the instruction sequence within a block is select-cmp-br.
Instruction * visitICmpInstWithCastAndCast(ICmpInst &ICI)
visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
bool isPPC_FP128Ty() const
isPPC_FP128Ty - 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
isInBounds - Determine whether the GEP has the inbounds flag.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
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)
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
static Instruction * ProcessUMulZExtIdiom(ICmpInst &I, Value *MulVal, Value *OtherVal, InstCombiner &IC)
Recognize and process idiom involving test for multiplication overflow.
OneUse_match< T > m_OneUse(const T &SubPattern)
bool isIntOrIntVectorTy() const
isIntOrIntVectorTy - Return true if this is an integer type or a vector of integer types...
static Instruction * ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombiner &IC)
ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form: I = icmp ugt (add (add A...
static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS)
Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the pre...
static Constant * getFNeg(Constant *C)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
bool eq(const APInt &RHS) const
Equality comparison.
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
cmpResult compare(const APFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
LLVM Basic Block Representation.
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.
bool isEquality() const
isEquality - Return true if this predicate is either EQ or NE.
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
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.
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
bool isFloatTy() const
isFloatTy - 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.
Instruction * FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".
ConstantFP - Floating Point Values [float, double].
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
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.
bool sle(const APInt &RHS) const
Signed less or equal comparison.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
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.
opStatus convert(const fltSemantics &, roundingMode, bool *)
APFloat::convert - convert a value of one floating point type to another.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Instruction * visitICmpInst(ICmpInst &I)
Value * getOperand(unsigned i) const
0 1 1 1 True if ordered (no nans)
bool isCommutative() const
isCommutative - 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 to represent integer types.
Type * getSrcTy() const
Return the source type, as a convenience.
Predicate getPredicate() const
Return the predicate for this instruction.
static Constant * getNot(Constant *C)
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Instruction * FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A, ConstantInt *CI1, ConstantInt *CI2)
FoldICmpCstShrCst - Handle "(icmp eq/ne (ashr/lshr const2, A), const1)" -> (icmp eq/ne A...
bool isFP128Ty() const
isFP128Ty - Return true if this is 'fp128'.
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
LLVMContext & getContext() const
All values hold a context through their type.
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="")
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
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
static const fltSemantics IEEEhalf
bool isRelational() const
isRelational - 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 const fltSemantics PPCDoubleDouble
unsigned getIntegerBitWidth() const
This is the shared class of boolean and integer constants.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
bool slt(const APInt &RHS) const
Signed less than comparison.
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative. ...
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
getScalarSizeInBits - If this is a vector type, return the getPrimitiveSizeInBits value for the eleme...
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.
SequentialType * getType() const
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.
Function * getCalledFunction() const
getCalledFunction - Return the function called, or null if this is an indirect function invocation...
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static Constant * get(Type *Ty, double V)
get() - This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in the specified type.
bool isNullValue() const
isNullValue - Return true if this is the value that would be returned by getNullValue.
bool isExact() const
Determine whether the exact flag is set.
static ConstantInt * getTrue(LLVMContext &Context)
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
void setOperand(unsigned i, Value *Val)
bool isAllOnesValue() const
isAllOnesValue - 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.
VectorType - Class to represent vector types.
Class for arbitrary precision integers.
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
static bool HasAddOverflow(ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned)
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
iterator_range< user_iterator > users()
static ConstantInt * getOne(Constant *C)
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.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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.
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
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.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, bool &TrueIfSigned)
isSignBitCheck - Given an exploded icmp instruction, return true if the comparison only checks the si...
static const fltSemantics IEEEsingle
bool isX86_FP80Ty() const
isX86_FP80Ty - Return true if this is x86 long double.
static IntegerType * getInt32Ty(LLVMContext &C)
opStatus roundToIntegral(roundingMode)
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)
bool hasOneUse() const
Return true if there is exactly one user of this value.
0 1 1 0 True if ordered and operands are unequal
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
static ConstantRange makeConstantRange(Predicate pred, const APInt &C)
Initialize a set of values that all satisfy the predicate with C.
unsigned countTrailingOnes() const
Count the number of trailing one bits.
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Instruction * visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS, ConstantInt *RHS)
visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
uint64_t getArrayNumElements() const
1 0 1 0 True if unordered or greater than
Instruction * FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS and CmpRHS are both known...
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Instruction * FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
FoldCmpLoadFromIndexedGlobal - Called we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global variable with a constant initializer.
Instruction * FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A, ConstantInt *CI1, ConstantInt *CI2)
FoldICmpCstShlCst - Handle "(icmp eq/ne (shl const2, A), const1)" -> (icmp eq/ne A, TrailingZeros(const1) - TrailingZeros(const2)).
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 ...
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.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - 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
Module * getParent()
Get the module that this global value is contained inside of...
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
LLVM Value Representation.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
This file provides internal interfaces used to implement the InstCombine.
1 0 1 1 True if unordered, greater than, or equal
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
bool isZero() const
Returns true if and only if the float is plus or minus zero.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
static Constant * getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced=false)
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
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
Check that one use is in the same block as the definition and all other uses are in blocks dominated ...
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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)
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 ...
0 0 1 1 True if ordered and greater than or equal
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
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.
Instruction * FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI, ICmpInst::Predicate Pred)
FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
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.
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
static Constant * getXor(Constant *C1, Constant *C2)
gep_type_iterator gep_type_begin(const User *GEP)