21 using namespace PatternMatch;
41 if (isa<ConstantInt>(V))
45 if (
CmpInst *CI = dyn_cast<CmpInst>(V))
46 return CI->hasOneUse();
102 return Builder->
CreateICmp(NewPred, LHS, RHS);
144 Value *
And = Builder->CreateAnd(X, AndRHS);
146 return BinaryOperator::CreateXor(And, Together);
151 if (Together != OpRHS) {
153 Value *
Or = Builder->CreateOr(X, Together);
155 return BinaryOperator::CreateAnd(Or, AndRHS);
159 if (TogetherCI && !TogetherCI->
isZero()){
164 Value *And = Builder->CreateAnd(X, Together);
166 return BinaryOperator::CreateOr(And, OpRHS);
171 case Instruction::Add:
186 if ((AddRHS & (AndRHSV-1)) == 0) {
191 if ((AddRHS & AndRHSV) == 0) {
196 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
198 return BinaryOperator::CreateXor(NewAnd, AndRHS);
205 case Instruction::Shl: {
216 return ReplaceInstUsesWith(TheAnd, Op);
224 case Instruction::LShr: {
236 return ReplaceInstUsesWith(TheAnd, Op);
244 case Instruction::AShr:
257 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->
getName());
258 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.
getName());
272 bool isSigned,
bool Inside) {
275 "Lo is not <= Hi in range emission code!");
279 return Builder->getFalse();
282 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
285 return Builder->CreateICmp(pred, V, Hi);
290 Value *Add = Builder->CreateAdd(V, NegLo, V->
getName()+
".off");
292 return Builder->CreateICmpULT(Add, UpperBound);
296 return Builder->getTrue();
299 Hi =
SubOne(cast<ConstantInt>(Hi));
300 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
303 return Builder->CreateICmp(pred, V, Hi);
309 Value *Add = Builder->CreateAdd(V, NegLo, V->
getName()+
".off");
311 return Builder->CreateICmpUGT(Add, LowerBound);
324 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
345 !isa<ConstantInt>(LHSI->
getOperand(1)))
return 0;
362 uint32_t MB = 0, ME = 0;
382 return Builder->CreateSub(LHSI->
getOperand(0), RHS,
"fold");
383 return Builder->CreateAdd(LHSI->
getOperand(0), RHS,
"fold");
431 bool icmp_abit = (ACst != 0 && !ACst->
isZero() &&
433 bool icmp_bbit = (BCst != 0 && !BCst->isZero() &&
434 BCst->getValue().isPowerOf2());
436 if (CCst != 0 && CCst->isZero()) {
468 }
else if (ACst != 0 && CCst != 0 &&
483 }
else if (BCst != 0 && CCst != 0 &&
565 Value *L11,*L12,*L21,*L22;
599 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
601 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
606 E =
R2; R1 = 0; ok =
true;
615 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
616 A = R11; D = R12; E =
R2; ok =
true;
617 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
618 A = R12; D = R11; E =
R2; ok =
true;
633 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
634 A = R11; D = R12; E = R1; ok =
true;
635 }
else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
636 A = R12; D = R11; E = R1; ok =
true;
646 }
else if (L12 == A) {
648 }
else if (L21 == A) {
650 }
else if (L22 == A) {
656 return left_type & right_type;
663 Value *
A = 0, *B = 0, *C = 0, *D = 0, *E = 0;
667 if (mask == 0)
return 0;
669 "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
699 return Builder->
CreateICmp(NEWCC, newAnd, zero);
706 return Builder->
CreateICmp(NEWCC, newAnd, newOr);
720 if (BCst == 0)
return 0;
722 if (DCst == 0)
return 0;
734 else if (NewMask == DCst->
getValue())
746 else if (NewMask == DCst->
getValue())
761 if (CCst == 0)
return 0;
765 if (ECst == 0)
return 0;
778 return Builder->
CreateICmp(NEWCC, newAnd, newOr2);
809 if (LHSCst == 0 || RHSCst == 0)
return 0;
811 if (LHSCst == RHSCst && LHSCC == RHSCC) {
816 Value *NewOr = Builder->CreateOr(Val, Val2);
817 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
822 Value *NewOr = Builder->CreateOr(Val, Val2);
823 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
847 if (SmallCst && BigCst) {
853 if ((Low & AndCst->
getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
854 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->
getValue());
857 return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
864 if (Val != Val2)
return 0;
892 ShouldSwap = LHSCst->
getValue().
sgt(RHSCst->getValue());
894 ShouldSwap = LHSCst->
getValue().
ugt(RHSCst->getValue());
908 assert(LHSCst != RHSCst &&
"Compares not folded above?");
924 if (LHSCst ==
SubOne(RHSCst))
925 return Builder->CreateICmpULT(Val, LHSCst);
928 if (LHSCst ==
SubOne(RHSCst))
929 return Builder->CreateICmpSLT(Val, LHSCst);
940 if (LHSCst ==
SubOne(RHSCst)){
942 Value *Add = Builder->CreateAdd(Val, AddCST, Val->
getName()+
".off");
985 if (RHSCst ==
AddOne(LHSCst))
986 return Builder->CreateICmp(LHSCC, Val, RHSCst);
989 return InsertRangeTest(Val,
AddOne(LHSCst), RHSCst,
false,
true);
1003 if (RHSCst ==
AddOne(LHSCst))
1004 return Builder->CreateICmp(LHSCC, Val, RHSCst);
1007 return InsertRangeTest(Val,
AddOne(LHSCst), RHSCst,
true,
true);
1031 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1032 return Builder->getFalse();
1038 if (isa<ConstantAggregateZero>(LHS->
getOperand(1)) &&
1039 isa<ConstantAggregateZero>(RHS->
getOperand(1)))
1049 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1055 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1068 unsigned Op0Pred =
getFCmpCode(Op0CC, Op0Ordered);
1069 unsigned Op1Pred =
getFCmpCode(Op1CC, Op1Ordered);
1071 if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
1081 if (!Op0Ordered && (Op0Ordered == Op1Ordered))
1083 if (Op0Ordered && (Op0Ordered == Op1Ordered))
1090 return getFCmpValue(
true, Op1Pred, Op0LHS, Op0RHS, Builder);
1099 bool Changed = SimplifyAssociativeOrCommutative(I);
1103 return ReplaceInstUsesWith(I, V);
1106 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1107 return ReplaceInstUsesWith(I, V);
1111 if (SimplifyDemandedInstructionBits(I))
1114 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1119 Value *Op0LHS = Op0I->getOperand(0);
1120 Value *Op0RHS = Op0I->getOperand(1);
1121 switch (Op0I->getOpcode()) {
1126 if (!Op0I->hasOneUse())
break;
1128 APInt NotAndRHS(~AndRHSMask);
1131 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1135 if (!isa<Constant>(Op0RHS) &&
1138 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1145 case Instruction::Add:
1149 if (
Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS,
false, I))
1150 return BinaryOperator::CreateAnd(V, AndRHS);
1151 if (
Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS,
false, I))
1152 return BinaryOperator::CreateAnd(V, AndRHS);
1155 case Instruction::Sub:
1159 if (
Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS,
true, I))
1160 return BinaryOperator::CreateAnd(V, AndRHS);
1164 if (Op0I->hasOneUse() && !
match(Op0LHS,
m_Zero())) {
1170 Value *NewNeg = Builder->CreateNeg(Op0RHS);
1171 return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1176 case Instruction::Shl:
1177 case Instruction::LShr:
1180 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1188 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1189 if (
Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1202 Value *NewCast = Builder->CreateTrunc(X, I.
getType(),
"and.shrunk");
1205 return BinaryOperator::CreateAnd(NewCast, C3);
1210 if (
SelectInst *SI = dyn_cast<SelectInst>(Op0))
1213 if (isa<PHINode>(Op0))
1222 if (Op0->
hasOneUse() && Op1->hasOneUse()) {
1223 Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
1229 Value *
A = 0, *B = 0, *C = 0, *D = 0;
1233 ((A == C && B == D) || (A == D && B == C)))
1234 return BinaryOperator::CreateXor(A, B);
1239 ((A == C && B == D) || (A == D && B == C)))
1240 return BinaryOperator::CreateXor(A, B);
1244 Value *tmpOp0 = Op0;
1245 Value *tmpOp1 = Op1;
1248 if (A == Op1 || B == Op1 ) {
1264 if (A == tmpOp0 && !isa<Constant>(A))
1265 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
1272 return BinaryOperator::CreateAnd(A, Op1);
1275 return BinaryOperator::CreateAnd(A, Op0);
1278 if (
ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
1279 if (
ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
1280 if (
Value *Res = FoldAndOfICmps(LHS, RHS))
1281 return ReplaceInstUsesWith(I, Res);
1286 if (
Value *Res = FoldAndOfFCmps(LHS, RHS))
1287 return ReplaceInstUsesWith(I, Res);
1291 if (
CastInst *Op0C = dyn_cast<CastInst>(Op0))
1292 if (
CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
1293 Type *SrcTy = Op0C->getOperand(0)->getType();
1294 if (Op0C->getOpcode() == Op1C->getOpcode() &&
1295 SrcTy == Op1C->getOperand(0)->getType() &&
1297 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
1300 if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.
getType()) &&
1301 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.
getType())) {
1302 Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.
getName());
1308 if (
ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1309 if (
ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
1310 if (
Value *Res = FoldAndOfICmps(LHS, RHS))
1315 if (
FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1316 if (
FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
1317 if (
Value *Res = FoldAndOfFCmps(LHS, RHS))
1325 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
1326 SI0->getOperand(1) == SI1->getOperand(1) &&
1327 (SI0->hasOneUse() || SI1->hasOneUse())) {
1329 Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
1332 SI1->getOperand(1));
1338 bool OpsSwapped =
false;
1364 return Changed ? &I : 0;
1405 cast<ConstantInt>(I->
getOperand(1))->getLimitedValue(~0U);
1407 if ((ShAmt & 7) || (ShAmt > 8*ByteValues.
size()))
1410 unsigned ByteShift = ShAmt >> 3;
1411 if (I->
getOpcode() == Instruction::Shl) {
1413 OverallLeftShift += ByteShift;
1414 ByteMask >>= ByteShift;
1417 OverallLeftShift -= ByteShift;
1418 ByteMask <<= ByteShift;
1419 ByteMask &= (~0U >> (32-ByteValues.
size()));
1422 if (OverallLeftShift >= (
int)ByteValues.
size())
return true;
1423 if (OverallLeftShift <= -(
int)ByteValues.
size())
return true;
1434 unsigned NumBytes = ByteValues.
size();
1436 const APInt &AndMask = cast<ConstantInt>(I->
getOperand(1))->getValue();
1438 for (
unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
1441 if ((ByteMask & (1 << i)) == 0)
1445 APInt MaskB = AndMask & Byte;
1447 ByteMask &= ~(1U << i);
1477 unsigned DestByteNo = InputByteNo + OverallLeftShift;
1478 if (ByteValues.
size()-1-DestByteNo != InputByteNo)
1483 if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
1485 ByteValues[DestByteNo] = V;
1504 uint32_t ByteMask = ~0U >> (32-ByteValues.
size());
1509 Value *V = ByteValues[0];
1510 if (V == 0)
return 0;
1513 for (
unsigned i = 1, e = ByteValues.
size(); i != e; ++i)
1514 if (ByteValues[i] != V)
1551 if (BO->getOpcode() == Instruction::Shl) {
1553 return One && One->
isOne();
1558 return K->getValue().isPowerOf2();
1583 if (LAnd->
getOperand(0) == RAnd->getOperand(0) &&
1586 Mask = Builder->CreateOr(LAnd->
getOperand(1), RAnd->getOperand(1));
1587 Masked = Builder->CreateAnd(LAnd->
getOperand(0), Mask);
1588 }
else if (LAnd->
getOperand(1) == RAnd->getOperand(1) &&
1591 Mask = Builder->CreateOr(LAnd->
getOperand(0), RAnd->getOperand(0));
1592 Masked = Builder->CreateAnd(LAnd->
getOperand(1), Mask);
1641 return Builder->CreateICmp(
1647 if (LHSCst == 0 || RHSCst == 0)
return 0;
1649 if (LHSCst == RHSCst && LHSCC == RHSCC) {
1652 Value *NewOr = Builder->CreateOr(Val, Val2);
1653 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1663 return Builder->CreateICmpULE(Val, LHSCst);
1668 if (Val != Val2)
return 0;
1686 ShouldSwap = LHSCst->
getValue().
sgt(RHSCst->getValue());
1688 ShouldSwap = LHSCst->
getValue().
ugt(RHSCst->getValue());
1702 assert(LHSCst != RHSCst &&
"Compares not folded above?");
1717 Value *NegCst = Builder->getInt(~Xor);
1723 if (LHSCst ==
SubOne(RHSCst)) {
1726 Value *Add = Builder->CreateAdd(Val, AddCST, Val->
getName()+
".off");
1728 return Builder->CreateICmpULT(Add, AddCST);
1751 return Builder->getTrue();
1761 if (RHSCst->isMaxValue(
false))
1763 return InsertRangeTest(Val, LHSCst,
AddOne(RHSCst),
false,
false);
1781 if (RHSCst->isMaxValue(
true))
1783 return InsertRangeTest(Val, LHSCst,
AddOne(RHSCst),
true,
false);
1803 return Builder->getTrue();
1818 return Builder->getTrue();
1838 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1839 return Builder->getTrue();
1848 if (isa<ConstantAggregateZero>(LHS->
getOperand(1)) &&
1849 isa<ConstantAggregateZero>(RHS->
getOperand(1)))
1859 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1864 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1876 unsigned Op0Pred =
getFCmpCode(Op0CC, Op0Ordered);
1877 unsigned Op1Pred =
getFCmpCode(Op1CC, Op1Ordered);
1878 if (Op0Ordered == Op1Ordered) {
1881 return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
1908 if (V1 == A || V1 == B) {
1909 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1910 return BinaryOperator::CreateOr(NewOp, V1);
1917 bool Changed = SimplifyAssociativeOrCommutative(I);
1921 return ReplaceInstUsesWith(I, V);
1924 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1925 return ReplaceInstUsesWith(I, V);
1929 if (SimplifyDemandedInstructionBits(I))
1932 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1937 (RHS->getValue() & C1->
getValue()) != 0 &&
1939 Value *Or = Builder->CreateOr(X, RHS);
1941 return BinaryOperator::CreateAnd(Or,
1942 Builder->getInt(RHS->getValue() | C1->
getValue()));
1948 Value *Or = Builder->CreateOr(X, RHS);
1950 return BinaryOperator::CreateXor(Or,
1951 Builder->getInt(C1->
getValue() & ~RHS->getValue()));
1955 if (
SelectInst *SI = dyn_cast<SelectInst>(Op0))
1959 if (isa<PHINode>(Op0))
1981 Value *NOr = Builder->CreateOr(A, Op1);
1983 return BinaryOperator::CreateXor(NOr, C1);
1987 if (Op1->hasOneUse() &&
1990 Value *NOr = Builder->CreateOr(A, Op0);
1992 return BinaryOperator::CreateXor(NOr, C1);
1996 Value *C = 0, *D = 0;
2006 if (C1->
getValue() == ~C2->getValue()) {
2007 if ((C2->getValue() & (C2->getValue()+1)) == 0 &&
2011 return ReplaceInstUsesWith(I, A);
2013 return ReplaceInstUsesWith(I, A);
2020 return ReplaceInstUsesWith(I, B);
2022 return ReplaceInstUsesWith(I, B);
2026 if ((C1->
getValue() & C2->getValue()) == 0) {
2032 return BinaryOperator::CreateAnd(A,
2033 Builder->getInt(C1->
getValue()|C2->getValue()));
2038 return BinaryOperator::CreateAnd(B,
2039 Builder->getInt(C1->
getValue()|C2->getValue()));
2047 (C4->getValue() & ~C2->getValue()) == 0) {
2049 return BinaryOperator::CreateAnd(
V2,
2050 Builder->getInt(C1->
getValue()|C2->getValue()));
2072 return BinaryOperator::CreateXor(A, D);
2076 return BinaryOperator::CreateXor(C, D);
2080 return BinaryOperator::CreateXor(A, B);
2084 return BinaryOperator::CreateXor(C, B);
2090 if (Ret)
return Ret;
2096 if (Ret)
return Ret;
2103 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
2104 SI0->getOperand(1) == SI1->getOperand(1) &&
2105 (SI0->hasOneUse() || SI1->hasOneUse())) {
2106 Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
2109 SI1->getOperand(1));
2116 if (Op0->
hasOneUse() && Op1->hasOneUse()) {
2117 Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
2123 bool SwappedForXor =
false;
2126 SwappedForXor =
true;
2133 if (Op0 == A || Op0 == B)
2134 return BinaryOperator::CreateOr(A, B);
2138 return BinaryOperator::CreateOr(A, B);
2141 Value *
Not = Builder->CreateNot(B, B->getName()+
".not");
2142 return BinaryOperator::CreateOr(Not, Op0);
2146 return BinaryOperator::CreateOr(Not, Op0);
2154 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2157 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2160 return BinaryOperator::CreateOr(Not, Op0);
2168 if (
Value *Res = FoldOrOfICmps(LHS, RHS))
2169 return ReplaceInstUsesWith(I, Res);
2174 if (
Value *Res = FoldOrOfFCmps(LHS, RHS))
2175 return ReplaceInstUsesWith(I, Res);
2178 if (
CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2181 Type *SrcTy = Op0C->getOperand(0)->getType();
2186 if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
2189 ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.
getType()) &&
2191 Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.
getName());
2197 if (
ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
2198 if (
ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
2199 if (
Value *Res = FoldOrOfICmps(LHS, RHS))
2204 if (
FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
2205 if (
FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
2206 if (
Value *Res = FoldOrOfFCmps(LHS, RHS))
2224 if (Op0->
hasOneUse() && !isa<ConstantInt>(Op1) &&
2226 Value *Inner = Builder->CreateOr(A, Op1);
2228 return BinaryOperator::CreateOr(Inner, C1);
2236 if (Op0->
hasOneUse() && Op1->hasOneUse() &&
2239 Value *orTrue = Builder->CreateOr(A, C);
2240 Value *orFalse = Builder->CreateOr(B, D);
2245 return Changed ? &I : 0;
2249 bool Changed = SimplifyAssociativeOrCommutative(I);
2253 return ReplaceInstUsesWith(I, V);
2256 if (
Value *V = SimplifyUsingDistributiveLaws(I))
2257 return ReplaceInstUsesWith(I, V);
2261 if (SimplifyDemandedInstructionBits(I))
2272 Op0I->swapOperands();
2275 Builder->CreateNot(Op0I->getOperand(1),
2276 Op0I->getOperand(1)->getName()+
".not");
2278 return BinaryOperator::CreateOr(Op0NotVal, NotY);
2279 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2287 Builder->CreateNot(Op0I->getOperand(0),
"notlhs");
2289 Builder->CreateNot(Op0I->getOperand(1),
"notrhs");
2291 return BinaryOperator::CreateOr(NotX, NotY);
2292 return BinaryOperator::CreateAnd(NotX, NotY);
2295 }
else if (Op0I->getOpcode() == Instruction::AShr) {
2298 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
2304 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2307 if (
CmpInst *CI = dyn_cast<CmpInst>(Op0))
2309 CI->getInversePredicate(),
2313 if (
CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2314 if (
CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2315 if (CI->
hasOneUse() && Op0C->hasOneUse()) {
2317 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2319 Op0C->getDestTy()))) {
2320 CI->setPredicate(CI->getInversePredicate());
2329 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2330 if (
Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2334 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2337 if (
ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2338 if (Op0I->getOpcode() == Instruction::Add) {
2340 if (RHS->isAllOnesValue()) {
2342 return BinaryOperator::CreateSub(
2346 }
else if (RHS->getValue().isSignBit()) {
2348 Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
2349 return BinaryOperator::CreateAdd(Op0I->getOperand(0),
C);
2366 }
else if (Op0I->getOpcode() == Instruction::LShr) {
2371 if (Op0I->hasOneUse() &&
2378 FoldConst ^= C3->getValue();
2382 cast<Instruction>(Opnd0)->setDebugLoc(I.
getDebugLoc());
2385 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2392 if (
SelectInst *SI = dyn_cast<SelectInst>(Op0))
2395 if (isa<PHINode>(Op0))
2408 }
else if (B == Op0) {
2433 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
2439 !isa<ConstantInt>(Op1)) {
2440 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
2446 if (Op0I && Op1I && Op0I->
isShift() &&
2462 if ((A == C && B == D) || (A == D && B ==
C))
2463 return BinaryOperator::CreateXor(A, B);
2468 if ((A == C && B == D) || (A == D && B == C))
2469 return BinaryOperator::CreateXor(A, B);
2477 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2478 LHS->getOperand(1) == RHS->getOperand(0))
2479 LHS->swapOperands();
2480 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2481 LHS->getOperand(1) == RHS->getOperand(1)) {
2482 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2484 bool isSigned = LHS->isSigned() || RHS->isSigned();
2485 return ReplaceInstUsesWith(I,
2492 if (
CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2493 if (
CastInst *Op1C = dyn_cast<CastInst>(Op1))
2494 if (Op0C->getOpcode() == Op1C->getOpcode()) {
2495 Type *SrcTy = Op0C->getOperand(0)->getType();
2496 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->
isIntegerTy() &&
2498 ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
2500 ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
2502 Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2503 Op1C->getOperand(0), I.
getName());
2509 return Changed ? &I : 0;
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Value * FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS)
FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
IntegerType * getType() const
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Abstract base class of comparison instructions.
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Instruction * visitXor(BinaryOperator &I)
Instruction * visitOr(BinaryOperator &I)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
The main container class for the LLVM Intermediate Representation.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=0)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
This class represents zero extension of integer types.
unsigned getNumOperands() const
static Value * foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, llvm::InstCombiner::BuilderTy *Builder)
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout *TD=0, unsigned Depth=0)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
bool isSigned() const
Determine if this instruction is using a signed comparison.
0 1 0 0 True if ordered and less than
1 1 1 0 True if unordered or not equal
const Function * getParent() const
Return the enclosing method, or null if none.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getNullValue(Type *Ty)
StringRef getName() const
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
APInt Not(const APInt &APIVal)
Bitwise complement function.
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)
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Value * SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Base class of casting instructions.
const APInt & getValue() const
Return the constant's value.
#define llvm_unreachable(msg)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
0 1 0 1 True if ordered and less than or equal
bool isShiftedMask(unsigned numBits, const APInt &APIVal)
Return true if the argument APInt value contains a sequence of ones with the remainder zero...
Instruction * FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C)
static Instruction * MatchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D)
not_match< LHS > m_Not(const LHS &L)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
static unsigned foldLogOpOfMaskedICmpsHelper(Value *&A, Value *&B, Value *&C, Value *&D, Value *&E, ICmpInst *LHS, ICmpInst *RHS, ICmpInst::Predicate &LHSCC, ICmpInst::Predicate &RHSCC)
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS)
Represents a floating point comparison operator.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
bool sgt(const APInt &RHS) const
Signed greather than comparison.
static Constant * AddOne(ConstantInt *C)
AddOne - Add one to a ConstantInt.
bool isLogicalShift() const
unsigned getActiveBits() const
Compute the number of active bits in the value.
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
static const Value * getNotArgument(const Value *BinOp)
static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask, SmallVectorImpl< Value * > &ByteValues)
Value * FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS)
FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Construct any of the CastInst subclasses.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
static Value * getFCmpValue(bool isordered, unsigned code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
bool isIntOrIntVectorTy() const
ConstantRange intersectWith(const ConstantRange &CR) const
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
LLVM Constant Representation.
Value * FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS)
static Constant * getAnd(Constant *C1, Constant *C2)
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered)
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Represent an integer comparison operator.
static bool isFreeToInvert(Value *V)
unsigned getBitWidth() const
Return the number of bits in the APInt.
Value * getOperand(unsigned i) const
0 1 1 1 True if ordered (no nans)
static bool isNot(const Value *V)
Integer representation type.
unsigned countPopulation() const
Count the number of bits set.
static unsigned conjugateICmpMask(unsigned Mask)
Predicate getPredicate() const
Return the predicate for this instruction.
static Constant * getNot(Constant *C)
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
1 1 1 1 Always true (always folded)
void swapOperands()
Swap operands and adjust predicate.
LLVMContext & getContext() const
All values hold a context through their type.
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
Instruction * visitAnd(BinaryOperator &I)
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
0 0 1 0 True if ordered and greater than
BinaryOps getOpcode() const
Class for constant integers.
1 1 0 0 True if unordered or less than
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::Shl > m_LogicalShift(const LHS &L, const RHS &R)
m_LogicalShift - Matches LShr or Shl.
static Constant * SubOne(ConstantInt *C)
SubOne - Subtract one from a ConstantInt.
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Get a ConstantInt for a specific signed value.
static Constant * getTrunc(Constant *C, Type *Ty)
Value * SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static ConstantInt * getTrue(LLVMContext &Context)
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred, Value *&X, Value *&Y, Value *&Z)
void setOperand(unsigned i, Value *Val)
bool isAllOnesValue() const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
bool isAllOnesValue() const
Determine if all bits are set.
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=0)
Create a CmpInst.
Value * getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, CmpInst::Predicate &NewICmpPred)
unsigned greater or equal
Value * SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static ConstantRange makeICmpRegion(unsigned Pred, const ConstantRange &Other)
static Value * dyn_castNotVal(Value *V)
static Constant * getOr(Constant *C1, Constant *C2)
0 1 1 0 True if ordered and operands are unequal
const Type * getScalarType() const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=0)
unsigned getPrimitiveSizeInBits() const
1 0 1 0 True if unordered or greater than
OtherOps getOpcode() const
Get the opcode casted to the right type.
0 0 0 1 True if ordered and equal
LLVM Value Representation.
1 0 1 1 True if unordered, greater than, or equal
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static Value * getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
bool isPowerOf2_32(uint32_t Value)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=0)
0 0 1 1 True if ordered and greater than or equal
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
bool isOne() const
Determine if the value is one.
static unsigned getTypeOfMaskedICmp(Value *A, Value *B, Value *C, ICmpInst::Predicate SCC)
static bool IsOneHotValue(Value *V)
0 0 0 0 Always false (always folded)
static Constant * getXor(Constant *C1, Constant *C2)
Value * FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS)