23 using namespace PatternMatch;
25 #define DEBUG_TYPE "instcombine"
43 FAddendCoef() : IsFp(
false), BufHasFpVal(
false),
IntVal(0) {}
47 assert(!insaneIntVal(C) &&
"Insane coefficient");
60 void operator=(
const FAddendCoef &
A);
62 void operator*=(
const FAddendCoef &S);
64 bool isOne()
const {
return isInt() &&
IntVal == 1; }
65 bool isTwo()
const {
return isInt() &&
IntVal == 2; }
66 bool isMinusOne()
const {
return isInt() &&
IntVal == -1; }
67 bool isMinusTwo()
const {
return isInt() &&
IntVal == -2; }
70 bool insaneIntVal(
int V) {
return V > 4 || V < -4; }
72 {
return reinterpret_cast<APFloat*
>(&FpValBuf.buffer[0]); }
73 const APFloat *getFpValPtr()
const
74 {
return reinterpret_cast<const APFloat*
>(&FpValBuf.buffer[0]); }
76 const APFloat &getFpVal()
const {
77 assert(IsFp && BufHasFpVal &&
"Incorret state");
78 return *getFpValPtr();
82 assert(IsFp && BufHasFpVal &&
"Incorret state");
83 return *getFpValPtr();
86 bool isInt()
const {
return !IsFp; }
118 FAddend() : Val(
nullptr) {}
120 Value *getSymVal()
const {
return Val; }
121 const FAddendCoef &getCoef()
const {
return Coeff; }
123 bool isConstant()
const {
return Val ==
nullptr; }
124 bool isZero()
const {
return Coeff.isZero(); }
126 void set(
short Coefficient,
Value *V) {
127 Coeff.set(Coefficient);
131 Coeff.set(Coefficient);
139 void negate() { Coeff.negate(); }
143 static unsigned drillValueDownOneStep(
Value* V, FAddend &A0, FAddend &A1);
147 unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1)
const;
150 assert((Val == T.Val) &&
"Symbolic-values disagree");
155 void Scale(
const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; }
173 Value *simplifyFAdd(AddendVect& V,
unsigned InstrQuota);
178 Value *createAddendVal(
const FAddend &
A,
bool& NeedNeg);
181 unsigned calcInstrNumber(
const AddendVect& Vect);
187 Value *createNaryFAdd(
const AddendVect& Opnds,
unsigned InstrQuota);
188 void createInstPostProc(
Instruction *NewInst,
bool NoNumber =
false);
195 unsigned CreateInstrNum;
196 void initCreateInstNum() { CreateInstrNum = 0; }
197 void incCreateInstNum() { CreateInstrNum++; }
199 void initCreateInstNum() {}
200 void incCreateInstNum() {}
212 FAddendCoef::~FAddendCoef() {
214 getFpValPtr()->~APFloat();
217 void FAddendCoef::set(
const APFloat&
C) {
227 IsFp = BufHasFpVal =
true;
230 void FAddendCoef::convertToFpType(
const fltSemantics &Sem) {
241 IsFp = BufHasFpVal =
true;
254 void FAddendCoef::operator=(
const FAddendCoef &That) {
258 set(That.getFpVal());
263 if (
isInt() == That.isInt()) {
267 getFpVal().add(That.getFpVal(), RndMode);
274 getFpVal().add(T, RndMode);
282 void FAddendCoef::operator*=(
const FAddendCoef &That) {
286 if (That.isMinusOne()) {
291 if (
isInt() && That.isInt()) {
292 int Res =
IntVal * (int)That.IntVal;
293 assert(!insaneIntVal(Res) &&
"Insane int value");
299 isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics();
302 convertToFpType(Semantic);
306 F0.
multiply(createAPFloatFromInt(Semantic, That.IntVal),
312 void FAddendCoef::negate() {
316 getFpVal().changeSign();
319 Value *FAddendCoef::getValue(
Type *Ty)
const {
322 ConstantFP::get(Ty->getContext(), getFpVal());
336 unsigned FAddend::drillValueDownOneStep
337 (
Value *Val, FAddend &Addend0, FAddend &Addend1) {
339 if (!Val || !(I = dyn_cast<Instruction>(Val)))
344 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) {
348 if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->
isZero())
351 if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->
isZero())
356 Addend0.set(1, Opnd0);
358 Addend0.set(C0,
nullptr);
362 FAddend &Addend = Opnd0 ? Addend1 : Addend0;
364 Addend.set(1, Opnd1);
366 Addend.set(C1,
nullptr);
367 if (Opcode == Instruction::FSub)
372 return Opnd0 && Opnd1 ? 2 : 1;
379 if (I->
getOpcode() == Instruction::FMul) {
382 if (
ConstantFP *C = dyn_cast<ConstantFP>(V0)) {
387 if (
ConstantFP *C = dyn_cast<ConstantFP>(V1)) {
400 unsigned FAddend::drillAddendDownOneStep
401 (FAddend &Addend0, FAddend &Addend1)
const {
405 unsigned BreakNum = FAddend::drillValueDownOneStep(Val, Addend0, Addend1);
406 if (!BreakNum || Coeff.isOne())
409 Addend0.Scale(Coeff);
412 Addend1.Scale(Coeff);
427 I->
getOpcode() == Instruction::FSub) &&
"Expect add/sub");
432 if (!I0 || !I1 || I0->
getOpcode() != I1->getOpcode())
436 if (I0->
getOpcode() == Instruction::FMul)
438 else if (I0->
getOpcode() != Instruction::FDiv)
443 Value *Opnd1_0 = I1->getOperand(0);
444 Value *Opnd1_1 = I1->getOperand(1);
451 Value *Factor =
nullptr;
452 Value *AddSub0 =
nullptr, *AddSub1 =
nullptr;
455 if (Opnd0_0 == Opnd1_0 || Opnd0_0 == Opnd1_1)
457 else if (Opnd0_1 == Opnd1_0 || Opnd0_1 == Opnd1_1)
461 AddSub0 = (Factor == Opnd0_0) ? Opnd0_1 : Opnd0_0;
462 AddSub1 = (Factor == Opnd1_0) ? Opnd1_1 : Opnd1_0;
464 }
else if (Opnd0_1 == Opnd1_1) {
480 createFAdd(AddSub0, AddSub1) :
481 createFSub(AddSub0, AddSub1);
482 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(NewAddSub)) {
483 const APFloat &
F = CFP->getValueAPF();
486 }
else if (
Instruction *II = dyn_cast<Instruction>(NewAddSub))
487 II->setFastMathFlags(Flags);
490 Value *RI = createFMul(Factor, NewAddSub);
492 II->setFastMathFlags(Flags);
496 Value *RI = createFDiv(NewAddSub, Factor);
498 II->setFastMathFlags(Flags);
510 I->
getOpcode() == Instruction::FSub) &&
"Expect add/sub");
515 FAddend Opnd0, Opnd1, Opnd0_0, Opnd0_1, Opnd1_0, Opnd1_1;
517 unsigned OpndNum = FAddend::drillValueDownOneStep(I, Opnd0, Opnd1);
520 unsigned Opnd0_ExpNum = 0;
521 unsigned Opnd1_ExpNum = 0;
523 if (!Opnd0.isConstant())
524 Opnd0_ExpNum = Opnd0.drillAddendDownOneStep(Opnd0_0, Opnd0_1);
527 if (OpndNum == 2 && !Opnd1.isConstant())
528 Opnd1_ExpNum = Opnd1.drillAddendDownOneStep(Opnd1_0, Opnd1_1);
531 if (Opnd0_ExpNum && Opnd1_ExpNum) {
533 AllOpnds.push_back(&Opnd0_0);
534 AllOpnds.push_back(&Opnd1_0);
535 if (Opnd0_ExpNum == 2)
536 AllOpnds.push_back(&Opnd0_1);
537 if (Opnd1_ExpNum == 2)
538 AllOpnds.push_back(&Opnd1_1);
541 unsigned InstQuota = 0;
545 InstQuota = ((!isa<Constant>(V0) && V0->
hasOneUse()) &&
546 (!isa<Constant>(V1) && V1->
hasOneUse())) ? 2 : 1;
548 if (
Value *R = simplifyFAdd(AllOpnds, InstQuota))
557 const FAddendCoef &
CE = Opnd0.getCoef();
558 return CE.isOne() ? Opnd0.getSymVal() :
nullptr;
564 AllOpnds.push_back(&Opnd0);
565 AllOpnds.push_back(&Opnd1_0);
566 if (Opnd1_ExpNum == 2)
567 AllOpnds.push_back(&Opnd1_1);
569 if (
Value *R = simplifyFAdd(AllOpnds, 1))
576 AllOpnds.push_back(&Opnd1);
577 AllOpnds.push_back(&Opnd0_0);
578 if (Opnd0_ExpNum == 2)
579 AllOpnds.push_back(&Opnd0_1);
581 if (
Value *R = simplifyFAdd(AllOpnds, 1))
586 return performFactorization(I);
589 Value *FAddCombine::simplifyFAdd(AddendVect& Addends,
unsigned InstrQuota) {
590 unsigned AddendNum = Addends.size();
591 assert(AddendNum <= 4 &&
"Too many addends");
594 unsigned NextTmpIdx = 0;
595 FAddend TmpResult[3];
603 const FAddend *ConstAdd =
nullptr;
612 for (
unsigned SymIdx = 0; SymIdx < AddendNum; SymIdx++) {
614 const FAddend *ThisAddend = Addends[SymIdx];
620 Value *Val = ThisAddend->getSymVal();
621 unsigned StartIdx = SimpVect.size();
622 SimpVect.push_back(ThisAddend);
630 for (
unsigned SameSymIdx = SymIdx + 1;
631 SameSymIdx < AddendNum; SameSymIdx++) {
632 const FAddend *T = Addends[SameSymIdx];
633 if (T && T->getSymVal() == Val) {
636 Addends[SameSymIdx] =
nullptr;
637 SimpVect.push_back(T);
642 if (StartIdx + 1 != SimpVect.size()) {
643 FAddend &R = TmpResult[NextTmpIdx ++];
644 R = *SimpVect[StartIdx];
645 for (
unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++)
649 SimpVect.resize(StartIdx);
652 SimpVect.push_back(&R);
663 "out-of-bound access");
666 SimpVect.push_back(ConstAdd);
669 if (!SimpVect.empty())
670 Result = createNaryFAdd(SimpVect, InstrQuota);
679 Value *FAddCombine::createNaryFAdd
680 (
const AddendVect &Opnds,
unsigned InstrQuota) {
681 assert(!Opnds.empty() &&
"Expect at least one addend");
685 unsigned InstrNeeded = calcInstrNumber(Opnds);
686 if (InstrNeeded > InstrQuota)
699 Value *LastVal =
nullptr;
700 bool LastValNeedNeg =
false;
703 for (
const FAddend *Opnd : Opnds) {
705 Value *V = createAddendVal(*Opnd, NeedNeg);
708 LastValNeedNeg = NeedNeg;
712 if (LastValNeedNeg == NeedNeg) {
713 LastVal = createFAdd(LastVal, V);
718 LastVal = createFSub(V, LastVal);
720 LastVal = createFSub(LastVal, V);
722 LastValNeedNeg =
false;
725 if (LastValNeedNeg) {
726 LastVal = createFNeg(LastVal);
730 assert(CreateInstrNum == InstrNeeded &&
731 "Inconsistent in instruction numbers");
738 Value *V = Builder.CreateFSub(Opnd0, Opnd1);
740 createInstPostProc(I);
746 Value *NewV = createFSub(Zero, V);
748 createInstPostProc(I,
true);
753 Value *V = Builder.CreateFAdd(Opnd0, Opnd1);
755 createInstPostProc(I);
760 Value *V = Builder.CreateFMul(Opnd0, Opnd1);
762 createInstPostProc(I);
767 Value *V = Builder.CreateFDiv(Opnd0, Opnd1);
769 createInstPostProc(I);
773 void FAddCombine::createInstPostProc(
Instruction *NewInstr,
bool NoNumber) {
786 unsigned FAddCombine::calcInstrNumber(
const AddendVect &Opnds) {
787 unsigned OpndNum = Opnds.size();
788 unsigned InstrNeeded = OpndNum - 1;
791 unsigned NegOpndNum = 0;
794 for (
const FAddend *Opnd : Opnds) {
795 if (Opnd->isConstant())
800 if (isa<UndefValue>(Opnd->getSymVal()))
803 const FAddendCoef &CE = Opnd->getCoef();
804 if (CE.isMinusOne() || CE.isMinusTwo())
810 if (!CE.isMinusOne() && !CE.isOne())
813 if (NegOpndNum == OpndNum)
826 Value *FAddCombine::createAddendVal(
const FAddend &Opnd,
bool &NeedNeg) {
827 const FAddendCoef &Coeff = Opnd.getCoef();
829 if (Opnd.isConstant()) {
831 return Coeff.getValue(Instr->getType());
834 Value *OpndVal = Opnd.getSymVal();
836 if (Coeff.isMinusOne() || Coeff.isOne()) {
837 NeedNeg = Coeff.isMinusOne();
841 if (Coeff.isTwo() || Coeff.isMinusTwo()) {
842 NeedNeg = Coeff.isMinusTwo();
843 return createFAdd(OpndVal, OpndVal);
847 return createFMul(OpndVal, Coeff.getValue(Instr->getType()));
855 bool InstCombiner::willNotOverflowSignedSub(
const Value *LHS,
880 bool InstCombiner::willNotOverflowUnsignedSub(
const Value *LHS,
906 Value *
X =
nullptr, *
Y =
nullptr, *
Z =
nullptr;
907 const APInt *C1 =
nullptr, *C2 =
nullptr;
923 return Builder.
CreateSub(RHS, NewAnd,
"sub");
928 return Builder.
CreateSub(RHS, NewOr,
"sub");
948 return Builder.
CreateSub(RHS, NewOr,
"sub");
964 return BinaryOperator::CreateOr(Op0, Op1);
968 return BinaryOperator::CreateXor(Op0, Op1);
1015 bool Changed = SimplifyAssociativeOrCommutative(I);
1018 if (
Value *V = SimplifyVectorOp(I))
1019 return replaceInstUsesWith(I, V);
1023 SQ.getWithInstruction(&I)))
1024 return replaceInstUsesWith(I, V);
1027 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1028 return replaceInstUsesWith(I, V);
1035 if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1037 if (
ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
1038 if (ZI->getSrcTy()->isIntegerTy(1))
1044 const APInt &RHSVal = CI->getValue();
1045 unsigned ExtendAmt = 0;
1048 if (XorRHS->
getValue() == -RHSVal) {
1050 ExtendAmt = TySizeBits - RHSVal.
logBase2() - 1;
1063 Value *NewShl = Builder.CreateShl(XorLHS, ShAmt,
"sext");
1064 return BinaryOperator::CreateAShr(NewShl, ShAmt);
1071 if ((XorRHS->
getValue() | LHSKnown.
Zero).isAllOnesValue())
1083 if (isa<Constant>(RHS))
1088 return BinaryOperator::CreateXor(LHS, RHS);
1101 if (
Value *LHSV = dyn_castNegVal(LHS)) {
1102 if (!isa<Constant>(RHS))
1103 if (
Value *RHSV = dyn_castNegVal(RHS)) {
1104 Value *NewAdd = Builder.CreateAdd(LHSV, RHSV,
"sum");
1108 return BinaryOperator::CreateSub(RHS, LHSV);
1112 if (!isa<Constant>(RHS))
1113 if (
Value *V = dyn_castNegVal(RHS))
1114 return BinaryOperator::CreateSub(LHS, V);
1117 return replaceInstUsesWith(I, V);
1121 return BinaryOperator::CreateOr(LHS, RHS);
1123 if (
Constant *CRHS = dyn_cast<Constant>(RHS)) {
1126 return BinaryOperator::CreateSub(
SubOne(CRHS), X);
1132 if (
ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
1138 CRHS->getValue() == (CRHS->getValue() & C2->
getValue())) {
1141 const APInt &AddRHSV = CRHS->getValue();
1144 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
1149 if (AddRHSHighBits == AddRHSHighBitsAnd) {
1151 Value *NewAdd = Builder.CreateAdd(X, CRHS, LHS->
getName());
1152 return BinaryOperator::CreateAnd(NewAdd, C2);
1184 if (
SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
1186 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
1187 if (LHSConv->hasOneUse()) {
1191 willNotOverflowSignedAdd(LHSConv->getOperand(0), CI,
I)) {
1194 Builder.CreateNSWAdd(LHSConv->getOperand(0), CI,
"addconv");
1201 if (
SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
1205 if (LHSConv->getOperand(0)->getType() ==
1206 RHSConv->getOperand(0)->getType() &&
1207 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1208 willNotOverflowSignedAdd(LHSConv->getOperand(0),
1209 RHSConv->getOperand(0),
I)) {
1211 Value *NewAdd = Builder.CreateNSWAdd(LHSConv->getOperand(0),
1212 RHSConv->getOperand(0),
"addconv");
1220 if (
auto *LHSConv = dyn_cast<ZExtInst>(LHS)) {
1222 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
1223 if (LHSConv->hasOneUse()) {
1227 willNotOverflowUnsignedAdd(LHSConv->getOperand(0), CI,
I)) {
1230 Builder.CreateNUWAdd(LHSConv->getOperand(0), CI,
"addconv");
1237 if (
auto *RHSConv = dyn_cast<ZExtInst>(RHS)) {
1241 if (LHSConv->getOperand(0)->getType() ==
1242 RHSConv->getOperand(0)->getType() &&
1243 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1244 willNotOverflowUnsignedAdd(LHSConv->getOperand(0),
1245 RHSConv->getOperand(0),
I)) {
1247 Value *NewAdd = Builder.CreateNUWAdd(
1248 LHSConv->getOperand(0), RHSConv->getOperand(0),
"addconv");
1256 Value *
A =
nullptr, *
B =
nullptr;
1259 return BinaryOperator::CreateOr(A,
B);
1263 return BinaryOperator::CreateOr(A,
B);
1268 Value *
A =
nullptr, *
B =
nullptr;
1298 return Changed ? &I :
nullptr;
1302 bool Changed = SimplifyAssociativeOrCommutative(I);
1305 if (
Value *V = SimplifyVectorOp(I))
1306 return replaceInstUsesWith(I, V);
1309 SQ.getWithInstruction(&I)))
1310 return replaceInstUsesWith(I, V);
1312 if (isa<Constant>(RHS))
1313 if (
Instruction *FoldedFAdd = foldOpWithConstantIntoOperand(I))
1318 if (
Value *LHSV = dyn_castFNegVal(LHS)) {
1319 Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV);
1325 if (!isa<Constant>(RHS))
1326 if (
Value *V = dyn_castFNegVal(RHS)) {
1327 Instruction *RI = BinaryOperator::CreateFSub(LHS, V);
1334 if (
SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
1335 Value *LHSIntVal = LHSConv->getOperand(0);
1336 Type *FPType = LHSConv->getType();
1341 auto IsValidPromotion = [](
Type *FTy,
Type *ITy) {
1347 unsigned MaxRepresentableBits =
1357 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
1358 if (IsValidPromotion(FPType, LHSIntVal->
getType())) {
1361 if (LHSConv->hasOneUse() &&
1363 willNotOverflowSignedAdd(LHSIntVal, CI, I)) {
1365 Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, CI,
"addconv");
1371 if (
SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
1372 Value *RHSIntVal = RHSConv->getOperand(0);
1375 if (IsValidPromotion(FPType, LHSIntVal->
getType())) {
1380 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1381 willNotOverflowSignedAdd(LHSIntVal, RHSIntVal, I)) {
1383 Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, RHSIntVal,
"addconv");
1392 Value *A1, *B1, *C1, *A2, *B2, *C2;
1416 if (
Value *V = FAddCombine(Builder).simplify(&I))
1417 return replaceInstUsesWith(I, V);
1420 return Changed ? &I :
nullptr;
1431 bool Swapped =
false;
1436 if (
GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1438 if (LHSGEP->getOperand(0) == RHS) {
1441 }
else if (
GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1443 if (LHSGEP->getOperand(0)->stripPointerCasts() ==
1444 RHSGEP->getOperand(0)->stripPointerCasts()) {
1452 if (
GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1454 if (RHSGEP->getOperand(0) == LHS) {
1457 }
else if (
GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1459 if (RHSGEP->getOperand(0)->stripPointerCasts() ==
1460 LHSGEP->getOperand(0)->stripPointerCasts()) {
1471 (GEP2 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse()))
1481 Result = Builder.CreateSub(Result, Offset);
1486 Result = Builder.CreateNeg(Result,
"diff.neg");
1488 return Builder.CreateIntCast(Result, Ty,
true);
1494 if (
Value *V = SimplifyVectorOp(I))
1495 return replaceInstUsesWith(I, V);
1499 SQ.getWithInstruction(&I)))
1500 return replaceInstUsesWith(I, V);
1503 if (
Value *V = SimplifyUsingDistributiveLaws(I))
1504 return replaceInstUsesWith(I, V);
1507 if (
Value *V = dyn_castNegVal(Op1)) {
1510 if (
const auto *BO = dyn_cast<BinaryOperator>(Op1)) {
1511 assert(BO->getOpcode() == Instruction::Sub &&
1512 "Expected a subtraction operator!");
1516 if (cast<Constant>(Op1)->isNotMinSignedValue() && I.
hasNoSignedWrap())
1524 return BinaryOperator::CreateXor(Op0, Op1);
1530 if (
Constant *C = dyn_cast<Constant>(Op0)) {
1542 if (
PHINode *PN = dyn_cast<PHINode>(Op1))
1572 *ShAmt == BitWidth - 1) {
1573 Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
1574 return BinaryOperator::CreateAShr(X, ShAmtOp);
1577 *ShAmt == BitWidth - 1) {
1578 Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
1579 return BinaryOperator::CreateLShr(X, ShAmtOp);
1587 if ((*Op0C | RHSKnown.
Zero).isAllOnesValue())
1588 return BinaryOperator::CreateXor(Op1, Op0);
1608 return BinaryOperator::CreateAnd(A, B);
1615 return BinaryOperator::CreateAnd(
1616 Y, Builder.CreateNot(Op1, Op1->getName() +
".not"));
1619 if (Op1->hasOneUse()) {
1620 Value *
X =
nullptr, *
Y =
nullptr, *
Z =
nullptr;
1626 Builder.CreateSub(
Z,
Y, Op1->getName()));
1631 return BinaryOperator::CreateAnd(Op0,
1632 Builder.CreateNot(
Y,
Y->getName() +
".not"));
1641 if (
Value *XNeg = dyn_castNegVal(X))
1642 return BinaryOperator::CreateShl(XNeg,
Y);
1648 Y->getType()->getScalarSizeInBits() == 1) {
1673 Value *LHSOp, *RHSOp;
1676 if (
Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.
getType()))
1677 return replaceInstUsesWith(I, Res);
1682 if (
Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.
getType()))
1683 return replaceInstUsesWith(I, Res);
1685 bool Changed =
false;
1695 return Changed ? &I :
nullptr;
1701 if (
Value *V = SimplifyVectorOp(I))
1702 return replaceInstUsesWith(I, V);
1705 SQ.getWithInstruction(&I)))
1706 return replaceInstUsesWith(I, V);
1716 if (isa<Constant>(Op0))
1723 if (
Value *V = dyn_castFNegVal(Op1)) {
1724 Instruction *NewI = BinaryOperator::CreateFAdd(Op0, V);
1728 if (
FPTruncInst *FPTI = dyn_cast<FPTruncInst>(Op1)) {
1729 if (
Value *V = dyn_castFNegVal(FPTI->getOperand(0))) {
1730 Value *NewTrunc = Builder.CreateFPTrunc(V, I.
getType());
1731 Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewTrunc);
1735 }
else if (
FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) {
1736 if (
Value *V = dyn_castFNegVal(FPEI->getOperand(0))) {
1738 Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewExt);
1745 if (
Value *V = FAddCombine(Builder).simplify(&I))
1746 return replaceInstUsesWith(I, V);
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
std::string & operator+=(std::string &buffer, StringRef string)
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
static bool isConstant(const MachineInstr &MI)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
DiagnosticInfoOptimizationBase::Argument NV
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool isOneValue() const
Determine if this is a value of 1.
match_zero m_Zero()
Match an arbitrary zero/null constant.
This class represents zero extension of integer types.
Value * CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name="")
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
bool isOneValue() const
Returns true if the value is one.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Instruction * visitFSub(BinaryOperator &I)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
This class represents a sign extension of integer types.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
StringRef getName() const
Return a constant reference to the value's name.
Value * CreateNot(Value *V, const Twine &Name="")
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isNegative() const
Determine sign of this APInt.
bool isNegativeZeroValue() const
Return true if the value is what would be returned by getZeroValueForNegation.
bool match(Val *V, const Pattern &P)
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)
This class represents the LLVM 'select' instruction.
bool noSignedZeros() const
const APInt & getValue() const
Return the constant as an APInt value reference.
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
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.
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isMask(unsigned numBits) const
Windows NT (Windows on ARM)
zlib-gnu style compression
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)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Value * SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FAdd, fold the result or return null.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if LHS and RHS have no common bits set.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
bool isZero() const
Return true if the value is positive or negative zero.
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.
bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value.
Value * OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty)
Optimize pointer differences into the same array into a size.
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)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
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.
APInt trunc(unsigned width) const
Truncate to new width.
bool isNegative() const
Returns true if this value is known to be negative.
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)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
bool isVectorTy() const
True if this is an instance of VectorType.
bool sge(const APInt &RHS) const
Signed greather or equal comparison.
This is an important base class in LLVM.
const Value * getCondition() const
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.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
ConstantFP - Floating Point Values [float, double].
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
APInt sext(unsigned width) const
Sign extend to a new width.
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
unsigned getBitWidth() const
Return the number of bits in the APInt.
opStatus multiply(const APFloat &RHS, roundingMode RM)
Value * getOperand(unsigned i) const
unsigned getIntegerBitWidth() const
Instruction * visitFAdd(BinaryOperator &I)
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
const Value * getTrueValue() const
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
neg_match< LHS > m_Neg(const LHS &L)
Match an integer negate.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static Constant * getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
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 ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
match_any_zero m_AnyZero()
Match an arbitrary zero/null constant.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
unsigned logBase2() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
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.
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
const fltSemantics & getFltSemantics() const
static unsigned int semanticsPrecision(const fltSemantics &)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
Value * SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FSub, fold the result or return null.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
static Instruction * foldAddWithConstant(BinaryOperator &Add, InstCombiner::BuilderTy &Builder)
This union template exposes a suitably aligned and sized character array member which can hold elemen...
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
static std::vector< std::string > Flags
static Value * checkForNegativeOperand(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
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 ...
opStatus add(const APFloat &RHS, roundingMode RM)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Instruction * visitAdd(BinaryOperator &I)
match_one m_One()
Match an integer 1 or a vector with all elements equal to 1.
bool hasOneUse() const
Return true if there is exactly one user of this value.
static Constant * getZeroValueForNegation(Type *Ty)
Floating point negation must be implemented with f(x) = -0.0 - x.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
const APFloat & getValueAPF() const
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
This class represents a cast from signed integer to floating point.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
match_all_ones m_AllOnes()
Match an integer or vector with all bits set to true.
This class represents a truncation of floating point types.
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
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.
static Constant * getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Value * SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
const Value * getFalseValue() const
Convenience struct for specifying and reasoning about fast-math flags.
bool isNullValue() const
Determine if all bits are clear.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
This class represents an extension of floating point types.
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...
const fltSemantics & getSemantics() const
Instruction * visitSub(BinaryOperator &I)
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
static Constant * getXor(Constant *C1, Constant *C2)