23 #define DEBUG_TYPE "instcombine" 29 "Unexpected FCmp predicate!");
69 "Unexpected FCmp predicate!");
98 if (!OldLHS->hasOneUse() && !OldRHS->
hasOneUse())
103 if (!OldLHS->hasOneUse())
140 if ((AddRHS & (AndRHSV - 1)).isNullValue()) {
145 if ((AddRHS & AndRHSV).isNullValue()) {
150 Value *NewAnd = Builder.CreateAnd(X, AndRHS);
152 return BinaryOperator::CreateXor(NewAnd, AndRHS);
166 bool isSigned,
bool Inside) {
168 "Lo is not < Hi in range emission code!");
185 return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
237 bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2());
238 unsigned MaskVal = 0;
239 if (CCst && CCst->isZero()) {
329 Value *L11, *L12, *L21, *L22;
332 L21 = L22 = L1 =
nullptr;
357 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
360 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
377 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
382 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
401 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
406 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
421 }
else if (L12 == A) {
424 }
else if (L21 == A) {
427 }
else if (L22 == A) {
514 return Builder.
CreateICmp(NewCC, NewAnd, NewMaskedValue);
521 return (C1->
getValue() & C2->getValue()) == C2->getValue();
530 if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst))
542 if (IsSubSetOrEqual(BCst, DCst))
553 if (IsSuperSetOrEqual(BCst, DCst))
558 assert(IsSubSetOrEqual(BCst, DCst) &&
"Precondition due to above code");
575 unsigned LHSMask,
unsigned RHSMask,
578 "Expected equality predicates for masked type of icmps.");
590 LHS, RHS, IsAnd, A, B, C, D, E,
591 PredL, PredR, Builder)) {
594 }
else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) {
596 RHS, LHS, IsAnd, A, D, E, B, C,
597 PredR, PredL, Builder)) {
608 Value *A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr, *
E =
nullptr;
615 "Expected equality predicates for masked type of icmps.");
616 unsigned LHSMask = MaskPair->first;
617 unsigned RHSMask = MaskPair->second;
618 unsigned Mask = LHSMask & RHSMask;
623 LHS, RHS, IsAnd, A,
B,
C,
D,
E, PredL, PredR, LHSMask, RHSMask,
657 return Builder.
CreateICmp(NewCC, NewAnd, Zero);
664 return Builder.
CreateICmp(NewCC, NewAnd, NewOr);
694 else if (NewMask == DCst->
getValue())
707 else if (NewMask == DCst->
getValue())
742 return Builder.
CreateICmp(NewCC, NewAnd, NewOr2);
789 default:
return nullptr;
800 return Builder.CreateICmp(NewPred, Input, RangeEnd);
811 const APInt *C1, *C2;
829 APInt Xor = *C1 ^ *C2;
846 if (*C1 == *C2 - 1) {
874 if (!LHSC || !RHSC || !LHSC->
isZero() || !RHSC->isZero())
880 if (A == D || B == D)
889 Value *Masked = Builder.CreateAnd(A, Mask);
891 return Builder.CreateICmp(NewPred, Masked, Mask);
934 auto tryToMatchSignedTruncationCheck = [](
ICmpInst *ICmp,
Value *&
X,
935 APInt &SignBitMask) ->
bool {
937 const APInt *I01, *I1;
952 if (tryToMatchSignedTruncationCheck(ICmp1, X1, HighestBit))
954 else if (tryToMatchSignedTruncationCheck(ICmp0, X1, HighestBit))
959 assert(HighestBit.
isPowerOf2() &&
"expected to be power of two (non-zero)");
963 APInt &UnsetBitsMask) ->
bool {
967 Pred,
X, UnsetBitsMask,
975 UnsetBitsMask = *
Mask;
984 if (!tryToDecompose(OtherICmp, X0, UnsetBitsMask))
1002 APInt SignBitsMask = ~(HighestBit - 1U);
1009 if (!UnsetBitsMask.
isSubsetOf(SignBitsMask)) {
1010 APInt OtherHighestBit = (~UnsetBitsMask) + 1U;
1019 CxtI.
getName() +
".simplified");
1055 ICmpInst *UnsignedICmp,
bool IsAnd,
1064 auto IsKnownNonZero = [&](
Value *V) {
1071 if (
match(UnsignedICmp,
1075 if (UnsignedICmp->
getOperand(0) != ZeroCmpOp)
1079 if (!IsKnownNonZero(NonZero))
1081 return IsKnownNonZero(NonZero);
1096 IsAnd && GetKnownNonZeroAndOther(B, A))
1102 !IsAnd && GetKnownNonZeroAndOther(B, A))
1110 if (!
match(UnsignedICmp,
1151 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
true, CxtI))
1175 if (
Value *V = simplifyRangeCheck(LHS, RHS,
false))
1179 if (
Value *V = simplifyRangeCheck(RHS, LHS,
false))
1205 if (LHSC == RHSC && PredL == PredR) {
1211 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
1212 return Builder.CreateICmp(PredL, NewOr, LHSC);
1222 ConstantInt *AndC, *SmallC =
nullptr, *BigC =
nullptr;
1236 if (SmallC && BigC) {
1242 if ((Low & AndC->
getValue()).isNullValue() &&
1243 (Low & BigC->getValue()).isNullValue()) {
1244 Value *NewAnd = Builder.CreateAnd(V, Low | AndC->
getValue());
1247 return Builder.CreateICmp(PredL, NewAnd, NewVal);
1272 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
1274 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
1288 assert(LHSC != RHSC &&
"Compares not folded above?");
1299 if (LHSC->
getValue() == (RHSC->getValue() - 1))
1300 return Builder.CreateICmpULT(LHS0, LHSC);
1302 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1307 if (LHSC->
getValue() == (RHSC->getValue() - 1))
1308 return Builder.CreateICmpSLT(LHS0, LHSC);
1311 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1325 if (RHSC->getValue() == (LHSC->
getValue() + 1))
1326 return Builder.CreateICmp(PredL, LHS0, RHSC);
1328 if (RHSC->isMaxValue(
false))
1329 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1333 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1343 if (RHSC->getValue() == (LHSC->
getValue() + 1))
1344 return Builder.CreateICmp(PredL, LHS0, RHSC);
1346 if (RHSC->isMaxValue(
true))
1347 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
1351 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
true,
1365 if (LHS0 == RHS1 && RHS0 == LHS1) {
1385 if (LHS0 == RHS0 && LHS1 == RHS1) {
1388 unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR;
1403 return Builder.CreateFCmp(PredL, LHS0, RHS0);
1416 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1417 "Expecting and/or op for fcmp transform");
1437 Pred != NanPred ||
X->getType() !=
Y->getType())
1441 Pred != NanPred ||
X->getType() !=
Y->getType())
1447 if (
auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) {
1449 NewFCmpInst->copyIRFlags(Op0);
1450 NewFCmpInst->andIRFlags(BO10);
1461 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1462 "Trying to match De Morgan's Laws with something other than and/or");
1465 Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1479 bool InstCombiner::shouldOptimizeCast(
CastInst *CI) {
1488 if (
const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1489 if (isEliminableCastPair(PrecedingCI, CI))
1514 if (ZextTruncC == C) {
1517 return new ZExtInst(NewOp, DestTy);
1524 if (SextTruncC == C) {
1527 return new SExtInst(NewOp, DestTy);
1568 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1569 Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1575 if (LogicOpc == Instruction::Xor)
1582 if (ICmp0 && ICmp1) {
1583 Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I)
1584 : foldOrOfICmps(ICmp0, ICmp1, I);
1595 if (
Value *R = foldLogicOfFCmps(FCmp0, FCmp1, LogicOpc == Instruction::And))
1613 return BinaryOperator::CreateXor(A, B);
1619 if (Op0->hasOneUse() || Op1->
hasOneUse())
1637 if (Op0->hasOneUse() || Op1->
hasOneUse())
1648 return BinaryOperator::CreateXor(A, B);
1656 if (
auto *ScalarC = dyn_cast<ConstantInt>(C))
1657 return ScalarC->getZExtValue() < BitWidth;
1662 for (
unsigned i = 0; i != NumElts; ++i) {
1666 if (isa<UndefValue>(Elt))
1669 if (!CI || CI->getZExtValue() >= BitWidth)
1701 if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->
getType()))
1708 if (Opc == Instruction::LShr || Opc == Instruction::Shl)
1715 Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X)
1716 : Builder.CreateBinOp(Opc, X, NewC);
1717 return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty);
1725 SQ.getWithInstruction(&I)))
1726 return replaceInstUsesWith(I, V);
1728 if (SimplifyAssociativeOrCommutative(I))
1736 if (SimplifyDemandedInstructionBits(I))
1744 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1745 return replaceInstUsesWith(I, V);
1748 return replaceInstUsesWith(I, V);
1766 Value *And = Builder.CreateAnd(X, Op1);
1768 return BinaryOperator::CreateXor(And, NewC);
1779 APInt Together = *C & *OrC;
1790 APInt NotAndMask(~(*C));
1795 Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->
getName() +
".masked");
1801 Value *NewLHS = Builder.CreateAnd(X, Op1, X->
getName() +
".masked");
1808 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1816 switch (Op0I->getOpcode()) {
1819 case Instruction::Xor:
1820 case Instruction::Or:
1821 case Instruction::Mul:
1823 case Instruction::Sub:
1833 Value *Op0LHS = Op0I->getOperand(0);
1834 if (isa<ZExtInst>(Op0LHS))
1835 BinOp = Builder.CreateBinOp(Op0I->getOpcode(),
X, TruncC1);
1837 BinOp = Builder.CreateBinOp(Op0I->getOpcode(), TruncC1,
X);
1839 auto *And = Builder.CreateAnd(BinOp, TruncC2);
1845 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1846 if (
Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1859 Value *NewCast = Builder.CreateTrunc(X, I.
getType(),
"and.shrunk");
1862 return BinaryOperator::CreateAnd(NewCast, C3);
1870 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
1880 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B));
1883 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B));
1889 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
1895 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
1903 return BinaryOperator::CreateAnd(A, B);
1911 return BinaryOperator::CreateAnd(A, B);
1918 if (
Value *Res = foldAndOfICmps(LHS, RHS, I))
1919 return replaceInstUsesWith(I, Res);
1925 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1926 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1927 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1928 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1929 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1930 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1933 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1934 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1935 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1936 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1937 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1938 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1944 if (
Value *Res = foldLogicOfFCmps(LHS, RHS,
true))
1945 return replaceInstUsesWith(I, Res);
1950 if (
Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1971 Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
1980 assert(Or.
getOpcode() == Instruction::Or &&
"bswap requires an 'or'");
1985 Op0 =
Ext->getOperand(0);
1988 Op1 =
Ext->getOperand(0);
2013 if (!OrOfOrs && !OrOfShifts && !OrOfAnds && !OrOfAndAndSh)
2022 for (
auto *Inst : Insts)
2042 Value *ShVal, *ShAmt0, *ShAmt1;
2049 if (ShiftOpcode0 == ShiftOpcode1)
2058 unsigned Mask = Width - 1;
2073 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width);
2074 bool SubIsOnLHS =
false;
2076 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width);
2082 bool IsFshl = (!SubIsOnLHS && ShiftOpcode0 == BinaryOperator::Shl) ||
2083 (SubIsOnLHS && ShiftOpcode1 == BinaryOperator::Shl);
2084 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2092 for (
unsigned i = 0; i != NumElts; ++i) {
2095 if (!EltC1 || !EltC2)
2159 return Builder.CreateXor(Cond, AConst);
2173 if (
Value *Cond = getSelectCondition(A, B)) {
2177 Value *BitcastC = Builder.CreateBitCast(C, A->
getType());
2178 Value *BitcastD = Builder.CreateBitCast(D, A->
getType());
2179 Value *
Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
2180 return Builder.CreateBitCast(Select, OrigType);
2193 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
false, CxtI))
2214 LHSC->
getType() == RHSC->getType() &&
2215 LHSC->
getValue() == (RHSC->getValue())) {
2220 Value *LAddOpnd, *RAddOpnd;
2228 if (LAddOpnd == RAddOpnd && DiffC.
isPowerOf2()) {
2239 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
2240 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
2241 APInt RangeDiff = LRangeLow.
sgt(RRangeLow) ? LRangeLow - RRangeLow
2242 : RRangeLow - LRangeLow;
2244 if (LowRangeDiff.
isPowerOf2() && LowRangeDiff == HighRangeDiff &&
2248 Value *NewAnd = Builder.CreateAnd(LAddOpnd, MaskC);
2249 Value *NewAdd = Builder.CreateAdd(NewAnd, MaxAddC);
2250 return Builder.CreateICmp(LHS->
getPredicate(), NewAdd, LHSC);
2279 Value *A =
nullptr, *B =
nullptr;
2297 return Builder.CreateICmp(
2303 if (
Value *V = simplifyRangeCheck(LHS, RHS,
true))
2307 if (
Value *V = simplifyRangeCheck(RHS, LHS,
true))
2327 if (LHSC == RHSC && PredL == PredR) {
2330 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
2331 return Builder.CreateICmp(PredL, NewOr, LHSC);
2341 return Builder.CreateICmpULE(LHS0, LHSC);
2364 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
2366 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
2380 assert(LHSC != RHSC &&
"Compares not folded above?");
2395 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue() + 1,
2402 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue() + 1,
2414 if (RHSC->isMaxValue(
false))
2415 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue(),
2419 assert(!RHSC->isMaxValue(
false) &&
"Missed icmp simplification");
2420 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
2430 if (RHSC->isMaxValue(
true))
2431 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue(),
2436 assert(!RHSC->isMaxValue(
true) &&
"Missed icmp simplification");
2437 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
true,
2450 SQ.getWithInstruction(&I)))
2451 return replaceInstUsesWith(I, V);
2453 if (SimplifyAssociativeOrCommutative(I))
2461 if (SimplifyDemandedInstructionBits(I))
2469 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2470 return replaceInstUsesWith(I, V);
2473 return replaceInstUsesWith(I, V);
2475 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2490 Value *Or = Builder.CreateOr(X, Y);
2502 Value *V1 =
nullptr, *
V2 =
nullptr;
2503 if ((C1->
getValue() & C2->getValue()).isNullValue()) {
2511 return BinaryOperator::CreateAnd(A,
2512 Builder.getInt(C1->
getValue()|C2->getValue()));
2519 return BinaryOperator::CreateAnd(B,
2520 Builder.getInt(C1->
getValue()|C2->getValue()));
2528 (C4->getValue() & ~C2->getValue()).isNullValue()) {
2530 return BinaryOperator::CreateAnd(
V2,
2531 Builder.getInt(C1->
getValue()|C2->getValue()));
2535 if (C1->
getValue() == ~C2->getValue()) {
2540 return BinaryOperator::CreateOr(Builder.CreateAnd(X, C1),
B);
2543 return BinaryOperator::CreateOr(Builder.CreateAnd(X, C2), A);
2547 return BinaryOperator::CreateXor(Builder.CreateAnd(X, C1),
B);
2550 return BinaryOperator::CreateXor(Builder.CreateAnd(X, C2), A);
2557 if (Op0->
hasOneUse() || Op1->hasOneUse()) {
2559 if (
Value *V = matchSelectFromAndOr(A, C, B, D))
2560 return replaceInstUsesWith(I, V);
2561 if (
Value *V = matchSelectFromAndOr(A, C, D, B))
2562 return replaceInstUsesWith(I, V);
2563 if (
Value *V = matchSelectFromAndOr(C, A, B, D))
2564 return replaceInstUsesWith(I, V);
2565 if (
Value *V = matchSelectFromAndOr(C, A, D, B))
2566 return replaceInstUsesWith(I, V);
2567 if (
Value *V = matchSelectFromAndOr(B, D, A, C))
2568 return replaceInstUsesWith(I, V);
2569 if (
Value *V = matchSelectFromAndOr(B, D, C, A))
2570 return replaceInstUsesWith(I, V);
2571 if (
Value *V = matchSelectFromAndOr(D, B, A, C))
2572 return replaceInstUsesWith(I, V);
2573 if (
Value *V = matchSelectFromAndOr(D, B, C, A))
2574 return replaceInstUsesWith(I, V);
2581 return BinaryOperator::CreateOr(Op0, C);
2586 return BinaryOperator::CreateOr(Op1, C);
2590 return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2596 bool SwappedForXor =
false;
2599 SwappedForXor =
true;
2606 if (Op0 == A || Op0 == B)
2607 return BinaryOperator::CreateOr(A, B);
2611 return BinaryOperator::CreateOr(A, B);
2614 Value *Not = Builder.CreateNot(B, B->
getName() +
".not");
2615 return BinaryOperator::CreateOr(Not, Op0);
2618 Value *Not = Builder.CreateNot(A, A->
getName() +
".not");
2619 return BinaryOperator::CreateOr(Not, Op0);
2627 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2628 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2629 B->getOpcode() == Instruction::Xor)) {
2630 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2632 Value *Not = Builder.CreateNot(NotOp, NotOp->
getName() +
".not");
2633 return BinaryOperator::CreateOr(Not, Op0);
2643 if (
Value *Res = foldOrOfICmps(LHS, RHS, I))
2644 return replaceInstUsesWith(I, Res);
2650 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2651 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2652 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2653 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2654 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2655 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2658 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2659 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2660 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2661 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2662 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2663 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2669 if (
Value *Res = foldLogicOfFCmps(LHS, RHS,
false))
2670 return replaceInstUsesWith(I, Res);
2675 if (
Instruction *CastedOr = foldCastedBitwiseLogic(I))
2692 if (Op0->
hasOneUse() && !isa<ConstantInt>(Op1) &&
2694 Value *Inner = Builder.CreateOr(A, Op1);
2696 return BinaryOperator::CreateOr(Inner, CI);
2703 Value *X =
nullptr, *Y =
nullptr;
2704 if (Op0->
hasOneUse() && Op1->hasOneUse() &&
2707 Value *orTrue = Builder.CreateOr(A, C);
2708 Value *orFalse = Builder.CreateOr(B, D);
2722 Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
2729 canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I))
2780 if (!Op0->hasOneUse() && !Op1->
hasOneUse())
2800 I.
getOperand(1) == RHS &&
"Should be 'xor' with these operands");
2832 return Builder.CreateICmpSLT(Builder.CreateXor(LHS0, RHS0), Zero);
2841 return Builder.CreateICmpSGT(Builder.CreateXor(LHS0, RHS0), MinusOne);
2857 if (OrICmp == LHS && AndICmp == RHS) {
2862 if (OrICmp == RHS && AndICmp == LHS) {
2869 Y->setPredicate(
Y->getInversePredicate());
2871 if (!
Y->hasOneUse()) {
2878 Builder.SetInsertPoint(
Y->getParent(), ++(
Y->getIterator()));
2879 Value *NotY = Builder.CreateNot(
Y,
Y->getName() +
".not");
2881 Y->replaceUsesWithIf(NotY,
2882 [NotY](
Use &U) {
return U.
getUser() != NotY; });
2885 return Builder.CreateAnd(LHS, RHS);
2922 return BinaryOperator::CreateXor(NewA, X);
2931 return BinaryOperator::CreateOr(LHS, RHS);
2960 return BinaryOperator::CreateXor(NotX, Y, I.
getName() +
".demorgan");
2968 SQ.getWithInstruction(&I)))
2969 return replaceInstUsesWith(I, V);
2971 if (SimplifyAssociativeOrCommutative(I))
2981 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2982 return replaceInstUsesWith(I, V);
2986 if (SimplifyDemandedInstructionBits(I))
2990 return replaceInstUsesWith(I, V);
3001 return BinaryOperator::CreateOr(Op0, Op1);
3011 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
3012 return BinaryOperator::CreateOr(X, NotY);
3017 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
3018 return BinaryOperator::CreateAnd(X, NotY);
3027 if (NotVal->
getOpcode() == Instruction::And ||
3028 NotVal->
getOpcode() == Instruction::Or) {
3038 if (NotVal->
getOpcode() == Instruction::And)
3039 return BinaryOperator::CreateOr(NotX, NotY);
3040 return BinaryOperator::CreateAnd(NotX, NotY);
3046 if (isa<Constant>(X) || NotVal->
hasOneUse())
3051 return BinaryOperator::CreateAShr(X, Y);
3094 return replaceInstUsesWith(I, Op0);
3105 return BinaryOperator::CreateSub(NewC, X);
3117 Worklist.Add(cast<Instruction>(Op0));
3125 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
3127 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
3128 if (Op0I->getOpcode() == Instruction::LShr) {
3133 if (Op0I->hasOneUse() &&
3140 FoldConst ^= C3->getValue();
3144 cast<Instruction>(Opnd0)->setDebugLoc(I.
getDebugLoc());
3147 return BinaryOperator::CreateXor(Opnd0, FoldVal);
3154 if (
Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
3160 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0));
3164 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1));
3169 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X));
3177 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X));
3183 return BinaryOperator::CreateXor(
3184 Builder.CreateAnd(Builder.CreateNot(A),
C), B);
3189 return BinaryOperator::CreateXor(
3190 Builder.CreateAnd(Builder.CreateNot(B),
C), A);
3195 return BinaryOperator::CreateOr(A, B);
3199 return BinaryOperator::CreateOr(A, B);
3207 if (
auto *LHS = dyn_cast<ICmpInst>(I.
getOperand(0)))
3208 if (
auto *RHS = dyn_cast<ICmpInst>(I.
getOperand(1)))
3209 if (
Value *V = foldXorOfICmps(LHS, RHS, I))
3210 return replaceInstUsesWith(I, V);
3212 if (
Instruction *CastedXor = foldCastedBitwiseLogic(I))
3233 auto *
Add = cast<BinaryOperator>(Op0);
3234 Value *Neg = Builder.CreateNeg(A,
"",
Add->hasNoUnsignedWrap(),
3235 Add->hasNoSignedWrap());
3258 Value *NotY = Builder.CreateNot(RHS);
3266 Value *NotX = Builder.CreateNot(LHS);
3275 Value *NotLHS = Builder.CreateNot(LHS);
3276 Value *NotRHS = Builder.CreateNot(RHS);
3290 if (
auto *Sel = dyn_cast<SelectInst>(Op0)) {
3293 if (CmpT && CmpF && CmpT->hasOneUse() && CmpF->hasOneUse()) {
3295 CmpF->setPredicate(CmpF->getInversePredicate());
3296 return replaceInstUsesWith(I, Sel);
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
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, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
Constant * getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getICmpCode.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
This class is the base class for the comparison instructions.
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.
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)
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
static Value * foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1, Instruction &CxtI, InstCombiner::BuilderTy &Builder)
General pattern: X & Y.
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 * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
This class represents lattice values for constants.
SimplifyQuery getWithInstruction(Instruction *I) const
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 * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
static Value * foldUnsignedUnderflowCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q, InstCombiner::BuilderTy &Builder)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
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 CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
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.
bool slt(const APInt &RHS) const
Signed less than comparison.
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.
Optional< std::vector< StOtherPiece > > Other
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
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
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
bool sgt(const APInt &RHS) const
Signed greater than comparison.
static Instruction * reassociateFCmps(BinaryOperator &BO, InstCombiner::BuilderTy &Builder)
This a limited reassociation for a special case (see above) where we are checking if two values are e...
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.
Value * CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name="")
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 isFreeToInvert(Value *V, bool WillInvertAllUses)
Return true if the specified value is free to invert (apply ~ to).
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
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="")
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)
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
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, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined. ...
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.
static Value * getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
This is the complement of getICmpCode, which turns an opcode and two operands into either a constant ...
APInt shl(unsigned shiftAmt) const
Left-shift function.
A Use represents the edge between a Value definition and its users.
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static Constant * AddOne(Constant *C)
Add one to a Constant.
0 1 0 1 True if ordered and less than or equal
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, bool UseInstrInfo=true)
Return true if 'V & Mask' is known to be zero.
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)
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
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...
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Type * getType() const
All values are typed, get the type of this value.
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
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.
static Value * foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd, InstCombiner::BuilderTy &Builder)
Reduce a pair of compares that check if a value has exactly 1 bit set.
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.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
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.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
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).
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits...
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
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...
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.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
static Instruction * matchRotate(Instruction &Or)
Transform UB-safe variants of bitwise rotate to the funnel shift intrinsic.
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.
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.
specific_intval m_SpecificInt(APInt V)
Match a specific integer value or vector with all elements equal to the value.
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)
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
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)
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
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)
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)
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
bool isMinValue(bool isSigned) const
This function will return true iff this constant represents the smallest value that may be represente...
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...
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
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.
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
void setOperand(unsigned i, Value *Val)
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
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 isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static Instruction * sinkNotIntoXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
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 bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ?
static Instruction * foldAndToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
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 greater 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.
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
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.
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.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
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.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
0 0 1 1 True if ordered and greater than or equal
static Instruction * foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast, InstCombiner::BuilderTy &Builder)
Fold {and,or,xor} (cast X), C.
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...
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)
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
static Constant * getXor(Constant *C1, Constant *C2)