24 #define DEBUG_TYPE "instcombine" 30 "Unexpected FCmp predicate!");
70 "Unexpected FCmp predicate!");
99 if (!OldLHS->hasOneUse() && !OldRHS->
hasOneUse())
104 if (!OldLHS->hasOneUse())
141 if ((AddRHS & (AndRHSV - 1)).isNullValue()) {
146 if ((AddRHS & AndRHSV).isNullValue()) {
151 Value *NewAnd = Builder.CreateAnd(X, AndRHS);
153 return BinaryOperator::CreateXor(NewAnd, AndRHS);
169 "Lo is not <= Hi in range emission code!");
188 return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
240 bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2());
241 unsigned MaskVal = 0;
242 if (CCst && CCst->isZero()) {
332 Value *L11, *L12, *L21, *L22;
335 L21 = L22 = L1 =
nullptr;
360 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
363 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
380 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
385 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
404 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
409 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
424 }
else if (L12 == A) {
427 }
else if (L21 == A) {
430 }
else if (L22 == A) {
517 return Builder.
CreateICmp(NewCC, NewAnd, NewMaskedValue);
524 return (C1->
getValue() & C2->getValue()) == C2->getValue();
533 if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst))
545 if (IsSubSetOrEqual(BCst, DCst))
556 if (IsSuperSetOrEqual(BCst, DCst))
561 assert(IsSubSetOrEqual(BCst, DCst) &&
"Precondition due to above code");
578 unsigned LHSMask,
unsigned RHSMask,
581 "Expected equality predicates for masked type of icmps.");
593 LHS, RHS, IsAnd, A, B, C, D, E,
594 PredL, PredR, Builder)) {
597 }
else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) {
599 RHS, LHS, IsAnd, A, D, E, B, C,
600 PredR, PredL, Builder)) {
611 Value *A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr, *
E =
nullptr;
618 "Expected equality predicates for masked type of icmps.");
619 unsigned LHSMask = MaskPair->first;
620 unsigned RHSMask = MaskPair->second;
621 unsigned Mask = LHSMask & RHSMask;
626 LHS, RHS, IsAnd, A,
B,
C,
D,
E, PredL, PredR, LHSMask, RHSMask,
660 return Builder.
CreateICmp(NewCC, NewAnd, Zero);
667 return Builder.
CreateICmp(NewCC, NewAnd, NewOr);
697 else if (NewMask == DCst->
getValue())
710 else if (NewMask == DCst->
getValue())
745 return Builder.
CreateICmp(NewCC, NewAnd, NewOr2);
792 default:
return nullptr;
803 return Builder.CreateICmp(NewPred, Input, RangeEnd);
814 const APInt *C1, *C2;
832 APInt Xor = *C1 ^ *C2;
849 if (*C1 == *C2 - 1) {
877 if (!LHSC || !RHSC || !LHSC->
isZero() || !RHSC->isZero())
883 if (A == D || B == D)
892 Value *Masked = Builder.CreateAnd(A, Mask);
894 return Builder.CreateICmp(NewPred, Masked, Mask);
906 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
true, CxtI))
930 if (
Value *V = simplifyRangeCheck(LHS, RHS,
false))
934 if (
Value *V = simplifyRangeCheck(RHS, LHS,
false))
947 if (LHSC == RHSC && PredL == PredR) {
953 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
954 return Builder.CreateICmp(PredL, NewOr, LHSC);
964 ConstantInt *AndC, *SmallC =
nullptr, *BigC =
nullptr;
978 if (SmallC && BigC) {
984 if ((Low & AndC->
getValue()).isNullValue() &&
985 (Low & BigC->getValue()).isNullValue()) {
986 Value *NewAnd = Builder.CreateAnd(V, Low | AndC->
getValue());
989 return Builder.CreateICmp(PredL, NewAnd, NewVal);
1014 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
1016 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
1030 assert(LHSC != RHSC &&
"Compares not folded above?");
1040 if (LHSC ==
SubOne(RHSC))
1041 return Builder.CreateICmpULT(LHS0, LHSC);
1043 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1047 if (LHSC ==
SubOne(RHSC))
1048 return Builder.CreateICmpSLT(LHS0, LHSC);
1060 if (RHSC ==
AddOne(LHSC))
1061 return Builder.CreateICmp(PredL, LHS0, RHSC);
1064 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1073 if (RHSC ==
AddOne(LHSC))
1074 return Builder.CreateICmp(PredL, LHS0, RHSC);
1077 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
true,
1091 if (LHS0 == RHS1 && RHS0 == LHS1) {
1111 if (LHS0 == RHS0 && LHS1 == RHS1) {
1114 unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR;
1129 return Builder.CreateFCmp(PredL, LHS0, RHS0);
1141 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1142 "Trying to match De Morgan's Laws with something other than and/or");
1145 Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1159 bool InstCombiner::shouldOptimizeCast(
CastInst *CI) {
1168 if (
const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1169 if (isEliminableCastPair(PrecedingCI, CI))
1194 if (ZextTruncC == C) {
1197 return new ZExtInst(NewOp, DestTy);
1204 if (SextTruncC == C) {
1207 return new SExtInst(NewOp, DestTy);
1248 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1249 Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1255 if (LogicOpc == Instruction::Xor)
1262 if (ICmp0 && ICmp1) {
1263 Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I)
1264 : foldOrOfICmps(ICmp0, ICmp1, I);
1275 if (
Value *R = foldLogicOfFCmps(FCmp0, FCmp1, LogicOpc == Instruction::And))
1293 return BinaryOperator::CreateXor(A, B);
1299 if (Op0->hasOneUse() || Op1->
hasOneUse())
1317 if (Op0->hasOneUse() || Op1->
hasOneUse())
1328 return BinaryOperator::CreateXor(A, B);
1336 if (
auto *ScalarC = dyn_cast<ConstantInt>(C))
1337 return ScalarC->getZExtValue() < BitWidth;
1342 for (
unsigned i = 0; i != NumElts; ++i) {
1346 if (isa<UndefValue>(Elt))
1349 if (!CI || CI->getZExtValue() >= BitWidth)
1381 if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->
getType()))
1388 if (Opc == Instruction::LShr || Opc == Instruction::Shl)
1395 Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X)
1396 : Builder.CreateBinOp(Opc, X, NewC);
1397 return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty);
1405 SQ.getWithInstruction(&I)))
1406 return replaceInstUsesWith(I, V);
1408 if (SimplifyAssociativeOrCommutative(I))
1416 if (SimplifyDemandedInstructionBits(I))
1424 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1425 return replaceInstUsesWith(I, V);
1428 return replaceInstUsesWith(I, V);
1446 Value *And = Builder.CreateAnd(X, Op1);
1448 return BinaryOperator::CreateXor(And, NewC);
1459 APInt Together = *C & *OrC;
1470 APInt NotAndMask(~(*C));
1475 Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->
getName() +
".masked");
1481 Value *NewLHS = Builder.CreateAnd(X, Op1, X->
getName() +
".masked");
1488 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1495 switch (Op0I->getOpcode()) {
1498 case Instruction::Xor:
1499 case Instruction::Or:
1500 case Instruction::Mul:
1502 case Instruction::Sub:
1509 Value *Op0LHS = Op0I->getOperand(0);
1510 if (isa<ZExtInst>(Op0LHS))
1511 BinOp = Builder.CreateBinOp(Op0I->getOpcode(),
X, TruncC1);
1513 BinOp = Builder.CreateBinOp(Op0I->getOpcode(), TruncC1,
X);
1515 auto *And = Builder.CreateAnd(BinOp, TruncC2);
1521 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1522 if (
Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1535 Value *NewCast = Builder.CreateTrunc(X, I.
getType(),
"and.shrunk");
1538 return BinaryOperator::CreateAnd(NewCast, C3);
1546 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
1556 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B));
1559 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B));
1565 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
1571 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
1579 return BinaryOperator::CreateAnd(A, B);
1587 return BinaryOperator::CreateAnd(A, B);
1594 if (
Value *Res = foldAndOfICmps(LHS, RHS, I))
1595 return replaceInstUsesWith(I, Res);
1601 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1602 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1603 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1604 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1605 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1606 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1609 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1610 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1611 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1612 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1613 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1614 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1620 if (
Value *Res = foldLogicOfFCmps(LHS, RHS,
true))
1621 return replaceInstUsesWith(I, Res);
1623 if (
Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1645 Op0 =
Ext->getOperand(0);
1648 Op1 =
Ext->getOperand(0);
1673 if (!OrOfOrs && !OrOfShifts && !OrOfAnds && !OrOfAndAndSh)
1682 for (
auto *Inst : Insts)
1690 for (
unsigned i = 0; i != NumElts; ++i) {
1693 if (!EltC1 || !EltC2)
1777 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
false, CxtI))
1798 LHSC->
getType() == RHSC->getType() &&
1799 LHSC->
getValue() == (RHSC->getValue())) {
1804 Value *LAddOpnd, *RAddOpnd;
1812 if (LAddOpnd == RAddOpnd && DiffC.
isPowerOf2()) {
1823 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1824 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1825 APInt RangeDiff = LRangeLow.
sgt(RRangeLow) ? LRangeLow - RRangeLow
1826 : RRangeLow - LRangeLow;
1828 if (LowRangeDiff.
isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1832 Value *NewAnd = Builder.CreateAnd(LAddOpnd, MaskC);
1833 Value *NewAdd = Builder.CreateAdd(NewAnd, MaxAddC);
1834 return Builder.CreateICmp(LHS->
getPredicate(), NewAdd, LHSC);
1863 Value *A =
nullptr, *
B =
nullptr;
1881 return Builder.CreateICmp(
1887 if (
Value *V = simplifyRangeCheck(LHS, RHS,
true))
1891 if (
Value *V = simplifyRangeCheck(RHS, LHS,
true))
1901 if (LHSC == RHSC && PredL == PredR) {
1904 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
1905 return Builder.CreateICmp(PredL, NewOr, LHSC);
1915 return Builder.CreateICmpULE(LHS0, LHSC);
1938 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
1940 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
1954 assert(LHSC != RHSC &&
"Compares not folded above?");
1978 assert(!RHSC->isMaxValue(
false) &&
"Missed icmp simplification");
1979 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
1990 assert(!RHSC->isMaxValue(
true) &&
"Missed icmp simplification");
1991 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
true,
2004 SQ.getWithInstruction(&I)))
2005 return replaceInstUsesWith(I, V);
2007 if (SimplifyAssociativeOrCommutative(I))
2015 if (SimplifyDemandedInstructionBits(I))
2023 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2024 return replaceInstUsesWith(I, V);
2027 return replaceInstUsesWith(I, V);
2029 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2043 Value *NOr = Builder.CreateOr(A, Op1);
2045 return BinaryOperator::CreateXor(NOr,
2052 Value *NOr = Builder.CreateOr(A, Op0);
2054 return BinaryOperator::CreateXor(NOr,
2062 Value *
C =
nullptr, *
D =
nullptr;
2068 Value *V1 =
nullptr, *
V2 =
nullptr;
2069 if ((C1->
getValue() & C2->getValue()).isNullValue()) {
2077 return BinaryOperator::CreateAnd(A,
2078 Builder.getInt(C1->
getValue()|C2->getValue()));
2085 return BinaryOperator::CreateAnd(B,
2086 Builder.getInt(C1->
getValue()|C2->getValue()));
2094 (C4->getValue() & ~C2->getValue()).isNullValue()) {
2096 return BinaryOperator::CreateAnd(
V2,
2097 Builder.getInt(C1->
getValue()|C2->getValue()));
2101 if (C1->
getValue() == ~C2->getValue()) {
2106 return BinaryOperator::CreateOr(Builder.CreateAnd(X, C1),
B);
2109 return BinaryOperator::CreateOr(Builder.CreateAnd(X, C2), A);
2113 return BinaryOperator::CreateXor(Builder.CreateAnd(X, C1),
B);
2116 return BinaryOperator::CreateXor(Builder.CreateAnd(X, C2), A);
2123 if (Op0->
hasOneUse() || Op1->hasOneUse()) {
2126 return replaceInstUsesWith(I, V);
2128 return replaceInstUsesWith(I, V);
2130 return replaceInstUsesWith(I, V);
2132 return replaceInstUsesWith(I, V);
2134 return replaceInstUsesWith(I, V);
2136 return replaceInstUsesWith(I, V);
2138 return replaceInstUsesWith(I, V);
2140 return replaceInstUsesWith(I, V);
2147 return BinaryOperator::CreateOr(Op0, C);
2152 return BinaryOperator::CreateOr(Op1, C);
2156 return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2162 bool SwappedForXor =
false;
2165 SwappedForXor =
true;
2172 if (Op0 == A || Op0 == B)
2173 return BinaryOperator::CreateOr(A, B);
2177 return BinaryOperator::CreateOr(A, B);
2180 Value *Not = Builder.CreateNot(B, B->
getName() +
".not");
2181 return BinaryOperator::CreateOr(Not, Op0);
2184 Value *Not = Builder.CreateNot(A, A->
getName() +
".not");
2185 return BinaryOperator::CreateOr(Not, Op0);
2193 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2194 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2195 B->getOpcode() == Instruction::Xor)) {
2196 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2198 Value *Not = Builder.CreateNot(NotOp, NotOp->
getName() +
".not");
2199 return BinaryOperator::CreateOr(Not, Op0);
2209 if (
Value *Res = foldOrOfICmps(LHS, RHS, I))
2210 return replaceInstUsesWith(I, Res);
2216 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2217 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2218 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2219 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2220 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2221 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2224 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2225 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2226 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2227 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2228 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2229 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2235 if (
Value *Res = foldLogicOfFCmps(LHS, RHS,
false))
2236 return replaceInstUsesWith(I, Res);
2238 if (
Instruction *CastedOr = foldCastedBitwiseLogic(I))
2255 if (Op0->
hasOneUse() && !isa<ConstantInt>(Op1) &&
2257 Value *Inner = Builder.CreateOr(A, Op1);
2259 return BinaryOperator::CreateOr(Inner, C1);
2266 Value *
X =
nullptr, *
Y =
nullptr;
2267 if (Op0->
hasOneUse() && Op1->hasOneUse() &&
2270 Value *orTrue = Builder.CreateOr(A, C);
2271 Value *orFalse = Builder.CreateOr(B,
D);
2324 if (!Op0->hasOneUse() && !Op1->
hasOneUse())
2371 return Builder.CreateICmpSLT(Builder.CreateXor(LHS0, RHS0), Zero);
2380 return Builder.CreateICmpSGT(Builder.CreateXor(LHS0, RHS0), MinusOne);
2395 if (OrICmp == LHS && AndICmp == RHS && RHS->
hasOneUse()) {
2398 return Builder.CreateAnd(LHS, RHS);
2400 if (OrICmp == RHS && AndICmp == LHS && LHS->
hasOneUse()) {
2403 return Builder.CreateAnd(LHS, RHS);
2440 return BinaryOperator::CreateXor(NewA, X);
2449 return BinaryOperator::CreateOr(LHS, RHS);
2460 SQ.getWithInstruction(&I)))
2461 return replaceInstUsesWith(I, V);
2463 if (SimplifyAssociativeOrCommutative(I))
2473 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2474 return replaceInstUsesWith(I, V);
2478 if (SimplifyDemandedInstructionBits(I))
2482 return replaceInstUsesWith(I, V);
2487 return BinaryOperator::CreateOr(Op0, Op1);
2497 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
2498 return BinaryOperator::CreateOr(X, NotY);
2503 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
2504 return BinaryOperator::CreateAnd(X, NotY);
2513 if (NotVal->
getOpcode() == Instruction::And ||
2514 NotVal->
getOpcode() == Instruction::Or) {
2524 if (NotVal->
getOpcode() == Instruction::And)
2525 return BinaryOperator::CreateOr(NotX, NotY);
2526 return BinaryOperator::CreateAnd(NotX, NotY);
2536 return BinaryOperator::CreateAShr(X, Y);
2547 return BinaryOperator::CreateLShr(NotC, Y);
2554 return BinaryOperator::CreateAShr(NotC, Y);
2562 return replaceInstUsesWith(I, Op0);
2579 return BinaryOperator::CreateSub(NewC, X);
2585 return BinaryOperator::CreateSub(NewC, X);
2598 Worklist.Add(cast<Instruction>(Op0));
2606 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
2608 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2609 if (Op0I->getOpcode() == Instruction::LShr) {
2614 if (Op0I->hasOneUse() &&
2621 FoldConst ^= C3->getValue();
2625 cast<Instruction>(Opnd0)->setDebugLoc(I.
getDebugLoc());
2628 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2635 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2642 cast<BinaryOperator>(Op1)->swapOperands();
2651 cast<BinaryOperator>(Op1)->swapOperands();
2667 return BinaryOperator::CreateAnd(A, Builder.CreateNot(Op1));
2674 return BinaryOperator::CreateAnd(Builder.CreateNot(A), Op1);
2685 return BinaryOperator::CreateXor(
2686 Builder.CreateAnd(Builder.CreateNot(A),
B), C);
2688 return BinaryOperator::CreateXor(
2689 Builder.CreateAnd(Builder.CreateNot(B), A), C);
2695 return BinaryOperator::CreateXor(
2696 Builder.CreateAnd(Builder.CreateNot(A),
B), C);
2698 return BinaryOperator::CreateXor(
2699 Builder.CreateAnd(Builder.CreateNot(B), A), C);
2704 return BinaryOperator::CreateOr(A, B);
2708 return BinaryOperator::CreateOr(A, B);
2718 if (
auto *LHS = dyn_cast<ICmpInst>(I.
getOperand(0)))
2719 if (
auto *RHS = dyn_cast<ICmpInst>(I.
getOperand(1)))
2720 if (
Value *V = foldXorOfICmps(LHS, RHS))
2721 return replaceInstUsesWith(I, V);
2723 if (
Instruction *CastedXor = foldCastedBitwiseLogic(I))
2744 auto *
Add = cast<BinaryOperator>(Op0);
2745 Value *Neg = Builder.CreateNeg(A,
"",
Add->hasNoUnsignedWrap(),
2746 Add->hasNoSignedWrap());
2773 Value *NotY = Builder.CreateNot(RHS);
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of nonnegative values.
static ConstantInt * getFalse(LLVMContext &Context)
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. ...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Type * getSrcTy() const
Return the source type, as a convenience.
static bool IsFreeToInvert(Value *V, bool WillInvertAllUses)
Return true if the specified value is free to invert (apply ~ to).
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
static Value * getFCmpValue(unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
This is the complement of getFCmpCode, which turns an opcode and two operands into either a FCmp inst...
Instruction * visitXor(BinaryOperator &I)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Instruction * visitOr(BinaryOperator &I)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Compute iterated dominance frontiers using a linear time algorithm.
BinaryOps getOpcode() const
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
static unsigned getFCmpCode(FCmpInst::Predicate CC)
Similar to getICmpCode but for FCmpInst.
This class represents zero extension of integer types.
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
Encode a icmp predicate into a three bit mask.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
static bool areInverseVectorBitmasks(Constant *C1, Constant *C2)
If all elements of two constant vectors are 0/-1 and inverses, return true.
APInt zext(unsigned width) const
Zero extend to a new width.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
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)
1 1 1 0 True if unordered or not equal
bool sgt(const APInt &RHS) const
Signed greather than comparison.
MaskedICmpType
Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns that can be simplified...
static Value * SimplifyBSwap(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Transform BITWISE_OP(BSWAP(A),BSWAP(B)) or BITWISE_OP(BSWAP(A), Constant) to BSWAP(BITWISE_OP(A, B))
This class represents a sign extension of integer types.
bool isVectorTy() const
True if this is an instance of VectorType.
bool sle(const APInt &RHS) const
Signed less or equal comparison.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
Value * SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Value * CreateNot(Value *V, const Twine &Name="")
bool swapOperands()
Exchange the two operands to this instruction.
1 0 0 1 True if unordered or equal
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
This is the base class for all instructions that perform data casts.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isIntegerTy() const
True if this is an instance of IntegerType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static Constant * AddOne(Constant *C)
Add one to a Constant.
0 1 0 1 True if ordered and less than or equal
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Value * foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C, Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, llvm::InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!= Y)...
static Value * peekThroughBitcast(Value *V, bool OneUseOnly=false)
Return the source operand of a potentially bitcasted value while optionally checking if it has one us...
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Type * getType() const
All values are typed, get the type of this value.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
This instruction compares its operands according to the predicate given to the constructor.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static Value * foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS, bool JoinedByAnd, InstCombiner::BuilderTy &Builder)
const APInt & getValue() const
Return the constant as an APInt value reference.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if LHS and RHS have no common bits set.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
void takeName(Value *V)
Transfer the name from V to this value.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Value * getOperand(unsigned i) const
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
OneUse_match< T > m_OneUse(const T &SubPattern)
static Optional< std::pair< unsigned, unsigned > > getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C, Value *&D, Value *&E, ICmpInst *LHS, ICmpInst *RHS, ICmpInst::Predicate &PredL, ICmpInst::Predicate &PredR)
Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
static Instruction * foldOrToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
bool isAllOnesValue() const
Determine if all bits are set.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
static Value * getSelectCondition(Value *A, Value *B, InstCombiner::BuilderTy &Builder)
We have an expression of the form (A & C) | (B & D).
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
static unsigned getMaskedICmpType(Value *A, Value *B, Value *C, ICmpInst::Predicate Pred)
Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C) satisfies.
static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth)
Return true if a constant shift amount is always less than the specified bit-width.
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)
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if 'V & Mask' is known to be zero.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
static Constant * getAnd(Constant *C1, Constant *C2)
bool isOneValue() const
Determine if this is a value of 1.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
0 1 1 1 True if ordered (no nans)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
static unsigned conjugateICmpMask(unsigned Mask)
Convert an analysis of a masked ICmp into its equivalent if all boolean operations had the opposite s...
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF)
Return the canonical inverse comparison predicate for the specified minimum/maximum flavor...
static Constant * getNot(Constant *C)
static Constant * getAllOnesValue(Type *Ty)
1 1 1 1 Always true (always folded)
static Value * getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
This is the complement of getICmpCode, which turns an opcode and two operands into either a constant ...
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
static Instruction * matchDeMorgansLaws(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Match De Morgan's Laws: (~A & ~B) == (~(A | B)) (~A | ~B) == (~(A & B))
1 1 0 1 True if unordered, less than, or equal
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Instruction * visitAnd(BinaryOperator &I)
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static Value * foldLogOpOfMaskedICmpsAsymmetric(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C, Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, unsigned LHSMask, unsigned RHSMask, llvm::InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!= Y), where the left-hand ...
0 0 1 0 True if ordered and greater than
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
This is the shared class of boolean and integer constants.
SelectPatternFlavor Flavor
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
1 1 0 0 True if unordered or less than
SelectPatternFlavor
Specific patterns of select instructions we can match.
LLVM_NODISCARD T pop_back_val()
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
Type * getDestTy() const
Return the destination type, as a convenience.
static ConstantInt * getTrue(LLVMContext &Context)
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
Return true if both predicates match sign or if at least one of them is an equality comparison (which...
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
void setOperand(unsigned i, Value *Val)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned getVectorNumElements() const
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
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.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
static Instruction * foldAndToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Value * SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
static Value * foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, llvm::InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!= Y)...
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Predicate getPredicate() const
Return the predicate for this instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Value * getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, CmpInst::Predicate &NewICmpPred)
This is the complement of getICmpCode, which turns an opcode and two operands into either a constant ...
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
unsigned greater or equal
StringRef getName() const
Return a constant reference to the value's name.
static bool isSigned(Value *V)
Can the given value generate sign bits.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static Constant * getOr(Constant *C1, Constant *C2)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
0 1 1 0 True if ordered and operands are unequal
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
1 0 1 0 True if unordered or greater than
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
bool isMinValue() const
Determine if this is the smallest unsigned value.
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
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.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
static Instruction * visitMaskedMerge(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
If we have a masked merge, in the canonical form of: (assuming that A only has one use...
bool hasOneUse() const
Return true if there is exactly one user of this value.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Value * simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted)
Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
bool recognizeBSwapOrBitReverseIdiom(Instruction *I, bool MatchBSwaps, bool MatchBitReversals, SmallVectorImpl< Instruction *> &InsertedInsts)
Try to match a bswap or bitreverse idiom.
0 0 1 1 True if ordered and greater than or equal
static Value * matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D, InstCombiner::BuilderTy &Builder)
We have an expression of the form (A & C) | (B & D).
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
static Instruction * foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast, InstCombiner::BuilderTy &Builder)
Fold {and,or,xor} (cast X), C.
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
static Instruction * foldXorToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
A ^ B can be specified using other logic ops in a variety of patterns.
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
bool isNullValue() const
Determine if all bits are clear.
0 0 0 0 Always false (always folded)
static Constant * getXor(Constant *C1, Constant *C2)
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)