22 using namespace PatternMatch;
24 #define DEBUG_TYPE "instcombine"
30 "Unexpected FCmp predicate!");
70 "Unexpected FCmp predicate!");
99 if (!OldLHS->hasOneUse() && !OldRHS->
hasOneUse())
104 if (!OldLHS->hasOneUse())
129 case Instruction::Xor:
132 Value *And = Builder.CreateAnd(X, AndRHS);
134 return BinaryOperator::CreateXor(And, Together);
137 case Instruction::Or:
140 if (TogetherCI && !TogetherCI->
isZero()){
145 Value *And = Builder.CreateAnd(X, Together);
147 return BinaryOperator::CreateOr(And, OpRHS);
167 if ((AddRHS & (AndRHSV - 1)).isNullValue()) {
172 if ((AddRHS & AndRHSV).isNullValue()) {
177 Value *NewAnd = Builder.CreateAnd(X, AndRHS);
179 return BinaryOperator::CreateXor(NewAnd, AndRHS);
186 case Instruction::Shl: {
197 return replaceInstUsesWith(TheAnd, Op);
205 case Instruction::LShr: {
217 return replaceInstUsesWith(TheAnd, Op);
225 case Instruction::AShr:
238 ShVal = Builder.CreateLShr(ShVal, OpRHS, Op->
getName());
239 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.
getName());
251 bool isSigned,
bool Inside) {
253 "Lo is not <= Hi in range emission code!");
272 return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
324 bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2());
325 unsigned MaskVal = 0;
326 if (CCst && CCst->isZero()) {
401 Value *L11, *L12, *L21, *L22;
404 L21 = L22 = L1 =
nullptr;
435 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
438 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
455 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
460 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
479 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
484 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
499 }
else if (L12 == A) {
502 }
else if (L21 == A) {
505 }
else if (L22 == A) {
512 return LeftType & RightType;
519 Value *
A =
nullptr, *
B =
nullptr, *C =
nullptr, *
D =
nullptr, *
E =
nullptr;
527 "Expected equality predicates for masked type of icmps.");
557 return Builder.
CreateICmp(NewCC, NewAnd, Zero);
564 return Builder.
CreateICmp(NewCC, NewAnd, NewOr);
594 else if (NewMask == DCst->
getValue())
607 else if (NewMask == DCst->
getValue())
642 return Builder.
CreateICmp(NewCC, NewAnd, NewOr2);
689 default:
return nullptr;
700 return Builder.CreateICmp(NewPred, Input, RangeEnd);
711 const APInt *C1, *C2;
729 APInt Xor = *C1 ^ *C2;
746 if (*C1 == *C2 - 1) {
774 if (!LHSC || !RHSC || !LHSC->
isZero() || !RHSC->isZero())
780 if (A == D || B == D)
789 Value *Masked = Builder.CreateAnd(A, Mask);
791 return Builder.CreateICmp(NewPred, Masked, Mask);
803 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
true, CxtI))
827 if (
Value *V = simplifyRangeCheck(LHS, RHS,
false))
831 if (
Value *V = simplifyRangeCheck(RHS, LHS,
false))
844 if (LHSC == RHSC && PredL == PredR) {
850 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
851 return Builder.CreateICmp(PredL, NewOr, LHSC);
861 ConstantInt *AndC, *SmallC =
nullptr, *BigC =
nullptr;
875 if (SmallC && BigC) {
881 if ((Low & AndC->
getValue()).isNullValue() &&
882 (Low & BigC->getValue()).isNullValue()) {
883 Value *NewAnd = Builder.CreateAnd(V, Low | AndC->
getValue());
886 return Builder.CreateICmp(PredL, NewAnd, NewVal);
911 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
913 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
927 assert(LHSC != RHSC &&
"Compares not folded above?");
938 return Builder.CreateICmpULT(LHS0, LHSC);
940 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
945 return Builder.CreateICmpSLT(LHS0, LHSC);
958 return Builder.CreateICmp(PredL, LHS0, RHSC);
961 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
971 return Builder.CreateICmp(PredL, LHS0, RHSC);
974 return insertRangeTest(LHS0, LHSC->
getValue() + 1, RHSC->getValue(),
true,
990 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1005 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1019 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1020 return Builder.getFalse();
1026 if (isa<ConstantAggregateZero>(LHS->
getOperand(1)) &&
1027 isa<ConstantAggregateZero>(RHS->
getOperand(1)))
1041 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1042 "Trying to match De Morgan's Laws with something other than and/or");
1045 Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1059 bool InstCombiner::shouldOptimizeCast(
CastInst *CI) {
1068 if (
const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1069 if (isEliminableCastPair(PrecedingCI, CI))
1074 if (CI->
getOpcode() == Instruction::SExt &&
1100 if (ZextTruncC == C) {
1103 return new ZExtInst(NewOp, DestTy);
1144 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1145 Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1151 if (LogicOpc == Instruction::Xor)
1158 if (ICmp0 && ICmp1) {
1159 Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I)
1160 : foldOrOfICmps(ICmp0, ICmp1, I);
1170 if (FCmp0 && FCmp1) {
1171 Value *Res = LogicOpc == Instruction::And ? foldAndOfFCmps(FCmp0, FCmp1)
1172 : foldOrOfFCmps(FCmp0, FCmp1);
1218 return BinaryOperator::CreateXor(A, B);
1224 if (Op0->hasOneUse() || Op1->
hasOneUse())
1242 if (Op0->hasOneUse() || Op1->
hasOneUse())
1253 return BinaryOperator::CreateXor(A, B);
1262 bool Changed = SimplifyAssociativeOrCommutative(I);
1265 if (
Value *V = SimplifyVectorOp(I))
1266 return replaceInstUsesWith(I, V);
1269 return replaceInstUsesWith(I, V);
1273 if (SimplifyDemandedInstructionBits(I))
1281 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1282 return replaceInstUsesWith(I, V);
1285 return replaceInstUsesWith(I, V);
1297 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1302 Value *Op0LHS = Op0I->getOperand(0);
1303 Value *Op0RHS = Op0I->getOperand(1);
1304 switch (Op0I->getOpcode()) {
1306 case Instruction::Xor:
1307 case Instruction::Or: {
1309 if (!Op0I->hasOneUse())
break;
1311 APInt NotAndRHS(~AndRHSMask);
1314 Value *NewRHS = Builder.CreateAnd(Op0RHS, AndRHS,
1318 if (!isa<Constant>(Op0RHS) &&
1321 Value *NewLHS = Builder.CreateAnd(Op0LHS, AndRHS,
1332 switch (Op0I->getOpcode()) {
1335 case Instruction::Xor:
1336 case Instruction::Or:
1337 case Instruction::Mul:
1339 case Instruction::Sub:
1346 if (isa<ZExtInst>(Op0LHS))
1347 BinOp = Builder.CreateBinOp(Op0I->getOpcode(),
X, TruncC1);
1349 BinOp = Builder.CreateBinOp(Op0I->getOpcode(), TruncC1,
X);
1351 auto *And = Builder.CreateAnd(BinOp, TruncC2);
1357 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1358 if (
Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1371 Value *NewCast = Builder.CreateTrunc(X, I.
getType(),
"and.shrunk");
1374 return BinaryOperator::CreateAnd(NewCast, C3);
1379 if (isa<Constant>(Op1))
1380 if (
Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
1387 Value *A =
nullptr, *B =
nullptr, *C =
nullptr;
1390 Value *tmpOp0 = Op0;
1391 Value *tmpOp1 = Op1;
1393 if (A == Op1 || B == Op1 ) {
1408 if (A == tmpOp0 && !isa<Constant>(A))
1409 return BinaryOperator::CreateAnd(A, Builder.CreateNot(B));
1417 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
1423 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
1431 return BinaryOperator::CreateAnd(A, B);
1439 return BinaryOperator::CreateAnd(A, B);
1446 if (
Value *Res = foldAndOfICmps(LHS, RHS, I))
1447 return replaceInstUsesWith(I, Res);
1453 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1454 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1455 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1456 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1457 if (
Value *Res = foldAndOfICmps(LHS, Cmp, I))
1458 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1461 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
1462 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1463 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
1464 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
1465 if (
Value *Res = foldAndOfICmps(Cmp, RHS, I))
1466 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
1473 if (
Value *Res = foldAndOfFCmps(LHS, RHS))
1474 return replaceInstUsesWith(I, Res);
1476 if (
Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1482 return Changed ? &I :
nullptr;
1492 Op0 =
Ext->getOperand(0);
1495 Op1 =
Ext->getOperand(0);
1509 if (!OrOfOrs && !OrOfShifts && !OrOfAnds)
1518 for (
auto *Inst : Insts)
1526 for (
unsigned i = 0; i != NumElts; ++i) {
1529 if (!EltC1 || !EltC2)
1613 if (
Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS,
false, CxtI))
1634 LHSC->
getType() == RHSC->getType() &&
1635 LHSC->
getValue() == (RHSC->getValue())) {
1640 Value *LAddOpnd, *RAddOpnd;
1648 if (LAddOpnd == RAddOpnd && DiffC.
isPowerOf2()) {
1659 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1660 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1661 APInt RangeDiff = LRangeLow.
sgt(RRangeLow) ? LRangeLow - RRangeLow
1662 : RRangeLow - LRangeLow;
1664 if (LowRangeDiff.
isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1668 Value *NewAnd = Builder.CreateAnd(LAddOpnd, MaskC);
1669 Value *NewAdd = Builder.CreateAdd(NewAnd, MaxAddC);
1670 return Builder.CreateICmp(LHS->
getPredicate(), NewAdd, LHSC);
1699 Value *A =
nullptr, *B =
nullptr;
1717 return Builder.CreateICmp(
1723 if (
Value *V = simplifyRangeCheck(LHS, RHS,
true))
1727 if (
Value *V = simplifyRangeCheck(RHS, LHS,
true))
1737 if (LHSC == RHSC && PredL == PredR) {
1740 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
1741 return Builder.CreateICmp(PredL, NewOr, LHSC);
1751 return Builder.CreateICmpULE(LHS0, LHSC);
1774 ShouldSwap = LHSC->
getValue().
sgt(RHSC->getValue());
1776 ShouldSwap = LHSC->
getValue().
ugt(RHSC->getValue());
1790 assert(LHSC != RHSC &&
"Compares not folded above?");
1814 assert(!RHSC->isMaxValue(
false) &&
"Missed icmp simplification");
1815 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
1826 assert(!RHSC->isMaxValue(
true) &&
"Missed icmp simplification");
1827 return insertRangeTest(LHS0, LHSC->
getValue(), RHSC->getValue() + 1,
true,
1842 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1855 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1866 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1867 return Builder.getTrue();
1876 if (isa<ConstantAggregateZero>(LHS->
getOperand(1)) &&
1877 isa<ConstantAggregateZero>(RHS->
getOperand(1)))
1899 if (!CI1)
return nullptr;
1901 Value *V1 =
nullptr;
1908 if (V1 == A || V1 == B) {
1910 return BinaryOperator::CreateOr(NewOp, V1);
1932 Value *V1 =
nullptr;
1941 if (V1 == A || V1 == B) {
1943 return BinaryOperator::CreateXor(NewOp, V1);
1953 bool Changed = SimplifyAssociativeOrCommutative(I);
1956 if (
Value *V = SimplifyVectorOp(I))
1957 return replaceInstUsesWith(I, V);
1960 return replaceInstUsesWith(I, V);
1964 if (SimplifyDemandedInstructionBits(I))
1972 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1973 return replaceInstUsesWith(I, V);
1976 return replaceInstUsesWith(I, V);
1978 if (isa<Constant>(Op1))
1979 if (
Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
1992 Value *NOr = Builder.CreateOr(A, Op1);
1994 return BinaryOperator::CreateXor(NOr,
2001 Value *NOr = Builder.CreateOr(A, Op0);
2003 return BinaryOperator::CreateXor(NOr,
2011 Value *C =
nullptr, *D =
nullptr;
2014 Value *V1 =
nullptr, *
V2 =
nullptr;
2018 if ((C1->
getValue() & C2->getValue()).isNullValue()) {
2026 return BinaryOperator::CreateAnd(A,
2027 Builder.getInt(C1->
getValue()|C2->getValue()));
2034 return BinaryOperator::CreateAnd(B,
2035 Builder.getInt(C1->
getValue()|C2->getValue()));
2043 (C4->getValue() & ~C2->getValue()).isNullValue()) {
2045 return BinaryOperator::CreateAnd(
V2,
2046 Builder.getInt(C1->
getValue()|C2->getValue()));
2054 if (Op0->
hasOneUse() || Op1->hasOneUse()) {
2057 return replaceInstUsesWith(I, V);
2059 return replaceInstUsesWith(I, V);
2061 return replaceInstUsesWith(I, V);
2063 return replaceInstUsesWith(I, V);
2065 return replaceInstUsesWith(I, V);
2067 return replaceInstUsesWith(I, V);
2069 return replaceInstUsesWith(I, V);
2071 return replaceInstUsesWith(I, V);
2099 return BinaryOperator::CreateOr(Op0, C);
2104 return BinaryOperator::CreateOr(Op1, C);
2108 return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2114 bool SwappedForXor =
false;
2117 SwappedForXor =
true;
2124 if (Op0 == A || Op0 == B)
2125 return BinaryOperator::CreateOr(A, B);
2129 return BinaryOperator::CreateOr(A, B);
2132 Value *Not = Builder.CreateNot(B, B->
getName() +
".not");
2133 return BinaryOperator::CreateOr(Not, Op0);
2136 Value *Not = Builder.CreateNot(A, A->
getName() +
".not");
2137 return BinaryOperator::CreateOr(Not, Op0);
2145 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2146 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2147 B->getOpcode() == Instruction::Xor)) {
2148 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2150 Value *Not = Builder.CreateNot(NotOp, NotOp->
getName() +
".not");
2151 return BinaryOperator::CreateOr(Not, Op0);
2161 if (
Value *Res = foldOrOfICmps(LHS, RHS, I))
2162 return replaceInstUsesWith(I, Res);
2168 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2169 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2170 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2171 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2172 if (
Value *Res = foldOrOfICmps(LHS, Cmp, I))
2173 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2176 if (
auto *Cmp = dyn_cast<ICmpInst>(X))
2177 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2178 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2179 if (
auto *Cmp = dyn_cast<ICmpInst>(Y))
2180 if (
Value *Res = foldOrOfICmps(Cmp, RHS, I))
2181 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2188 if (
Value *Res = foldOrOfFCmps(LHS, RHS))
2189 return replaceInstUsesWith(I, Res);
2191 if (
Instruction *CastedOr = foldCastedBitwiseLogic(I))
2208 if (Op0->
hasOneUse() && !isa<ConstantInt>(Op1) &&
2210 Value *Inner = Builder.CreateOr(A, Op1);
2212 return BinaryOperator::CreateOr(Inner, C1);
2219 Value *X =
nullptr, *
Y =
nullptr;
2220 if (Op0->
hasOneUse() && Op1->hasOneUse() &&
2223 Value *orTrue = Builder.CreateOr(A, C);
2224 Value *orFalse = Builder.CreateOr(B, D);
2229 return Changed ? &I :
nullptr;
2283 if (!Op0->hasOneUse() && !Op1->
hasOneUse())
2326 if (OrICmp == LHS && AndICmp == RHS && RHS->
hasOneUse()) {
2329 return Builder.CreateAnd(LHS, RHS);
2331 if (OrICmp == RHS && AndICmp == LHS && LHS->
hasOneUse()) {
2334 return Builder.CreateAnd(LHS, RHS);
2346 bool Changed = SimplifyAssociativeOrCommutative(I);
2349 if (
Value *V = SimplifyVectorOp(I))
2350 return replaceInstUsesWith(I, V);
2353 return replaceInstUsesWith(I, V);
2359 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2360 return replaceInstUsesWith(I, V);
2364 if (SimplifyDemandedInstructionBits(I))
2368 return replaceInstUsesWith(I, V);
2378 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
2379 return BinaryOperator::CreateOr(X, NotY);
2384 Value *NotY = Builder.CreateNot(Y, Y->
getName() +
".not");
2385 return BinaryOperator::CreateAnd(X, NotY);
2391 if (NotVal->
getOpcode() == Instruction::And ||
2392 NotVal->
getOpcode() == Instruction::Or) {
2402 if (NotVal->
getOpcode() == Instruction::And)
2403 return BinaryOperator::CreateOr(NotX, NotY);
2404 return BinaryOperator::CreateAnd(NotX, NotY);
2410 return BinaryOperator::CreateAShr(X, Y);
2420 return BinaryOperator::CreateLShr(NotC, Y);
2426 return BinaryOperator::CreateAShr(NotC, Y);
2434 return replaceInstUsesWith(I, Op0);
2437 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
2439 if (
CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2440 if (
CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2441 if (CI->
hasOneUse() && Op0C->hasOneUse()) {
2443 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2445 Op0C->getDestTy()))) {
2446 CI->setPredicate(CI->getInversePredicate());
2455 if (Op0I->getOpcode() == Instruction::Sub && RHSC->isMinusOne())
2456 if (
Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2462 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2465 if (RHSC->isMinusOne()) {
2467 return BinaryOperator::CreateSub(
SubOne(NegOp0CI),
2469 }
else if (RHSC->getValue().isSignMask()) {
2471 Constant *C = Builder.getInt(RHSC->getValue() + Op0CI->getValue());
2475 }
else if (Op0I->getOpcode() == Instruction::Or) {
2490 }
else if (Op0I->getOpcode() == Instruction::LShr) {
2495 if (Op0I->hasOneUse() &&
2502 FoldConst ^= C3->getValue();
2506 cast<Instruction>(Opnd0)->setDebugLoc(I.
getDebugLoc());
2509 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2516 if (isa<Constant>(Op1))
2517 if (
Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
2524 cast<BinaryOperator>(Op1)->swapOperands();
2533 cast<BinaryOperator>(Op1)->swapOperands();
2549 return BinaryOperator::CreateAnd(A, Builder.CreateNot(Op1));
2556 return BinaryOperator::CreateAnd(Builder.CreateNot(A), Op1);
2567 return BinaryOperator::CreateXor(
2568 Builder.CreateAnd(Builder.CreateNot(A),
B), C);
2570 return BinaryOperator::CreateXor(
2571 Builder.CreateAnd(Builder.CreateNot(B),
A), C);
2577 return BinaryOperator::CreateXor(
2578 Builder.CreateAnd(Builder.CreateNot(A),
B), C);
2580 return BinaryOperator::CreateXor(
2581 Builder.CreateAnd(Builder.CreateNot(B),
A), C);
2586 return BinaryOperator::CreateOr(A, B);
2590 return BinaryOperator::CreateOr(A, B);
2600 if (
auto *LHS = dyn_cast<ICmpInst>(I.
getOperand(0)))
2601 if (
auto *RHS = dyn_cast<ICmpInst>(I.
getOperand(1)))
2602 if (
Value *V = foldXorOfICmps(LHS, RHS))
2603 return replaceInstUsesWith(I, V);
2605 if (
Instruction *CastedXor = foldCastedBitwiseLogic(I))
2608 return Changed ? &I :
nullptr;
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
static ConstantInt * getFalse(LLVMContext &Context)
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to have exactly one bit set when defined. ...
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
This class is the base class for the comparison instructions.
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)
Instruction * visitOr(BinaryOperator &I)
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
bool decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred, Value *&X, Value *&Y, Value *&Z)
Decompose an icmp into the form ((X & Y) pred Z) if possible.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
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="")
match_zero m_Zero()
Match an arbitrary zero/null constant.
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.
static bool areInverseVectorBitmasks(Constant *C1, Constant *C2)
If all elements of two constant vectors are 0/-1 and inverses, return true.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
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.
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)
bool isSigned() const
Determine if this instruction is using a signed comparison.
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
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))
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true...
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.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
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.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
bool swapOperands()
Exchange the two operands to this instruction.
bool isNegative() const
Determine sign of this APInt.
1 0 0 1 True if unordered or equal
bool match(Val *V, const Pattern &P)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
bool isNonNegative() const
Returns true if this value is known to be non-negative.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
static Instruction * FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C, InstCombiner::BuilderTy &Builder)
This helper function folds:
This is the base class for all instructions that perform data casts.
const APInt & getValue() const
Return the constant as an APInt value reference.
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...
static Constant * AddOne(Constant *C)
Add one to a Constant.
0 1 0 1 True if ordered and less than or equal
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
not_match< LHS > m_Not(const LHS &L)
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 * 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="")
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
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.
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.
static Instruction * FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C, InstCombiner::BuilderTy &Builder)
This helper function folds:
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static Value * foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS, bool JoinedByAnd, InstCombiner::BuilderTy &Builder)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
void takeName(Value *V)
Transfer the name from V to this value.
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
OneUse_match< T > m_OneUse(const T &SubPattern)
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
static Instruction * foldOrToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
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).
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.
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 isVectorTy() const
True if this is an instance of VectorType.
bool isEquality() const
Return true if this predicate is either EQ or NE.
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)
ConstantFP - Floating Point Values [float, double].
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool sle(const APInt &RHS) const
Signed less or equal comparison.
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.
Value * getOperand(unsigned i) const
0 1 1 1 True if ordered (no nans)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Type * getSrcTy() const
Return the source type, as a convenience.
static unsigned conjugateICmpMask(unsigned Mask)
Convert an analysis of a masked ICmp into its equivalent if all boolean operations had the opposite s...
Predicate getPredicate() const
Return the predicate for this instruction.
static Constant * getNot(Constant *C)
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
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))
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 Instruction * foldBoolSextMaskToSelect(BinaryOperator &I)
Instruction * visitAnd(BinaryOperator &I)
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
0 0 1 0 True if ordered and greater than
BinaryOps getOpcode() const
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.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
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
Type * getType() const
All values are typed, get the type of this value.
Type * getDestTy() const
Return the destination type, as a convenience.
LLVM_NODISCARD T pop_back_val()
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
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.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
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.
Class for arbitrary precision integers.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
unsigned getVectorNumElements() const
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
bool isMinValue() const
Determine if this is the smallest unsigned value.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
llvm_unreachable("Unknown RegBank!")
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.
bool isAllOnesValue() const
Determine if all bits are set.
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 ...
static 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).
bool isMinSignedValue() const
Determine if this is the smallest signed value.
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 ...
unsigned greater or equal
match_one m_One()
Match an integer 1 or a vector with all elements equal to 1.
static Constant * getOr(Constant *C1, Constant *C2)
bool hasOneUse() const
Return true if there is exactly one user of this value.
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
0 1 1 0 True if ordered and operands are unequal
bool recognizeBSwapOrBitReverseIdiom(Instruction *I, bool MatchBSwaps, bool MatchBitReversals, SmallVectorImpl< Instruction * > &InsertedInsts)
Try and match a bswap or bitreverse idiom.
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="")
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())
match_all_ones m_AllOnes()
Match an integer or vector with all bits set to true.
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
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.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
APInt zext(unsigned width) const
Zero extend to a new width.
bool isNullValue() const
Determine if all bits are clear.
Value * simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted)
Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
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).
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
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.
0 0 0 0 Always false (always folded)
static Constant * getXor(Constant *C1, Constant *C2)