40#define DEBUG_TYPE "instcombine"
54 if (!
V->hasOneUse())
return nullptr;
56 bool MadeChange =
false;
60 Value *
A =
nullptr, *
B =
nullptr, *One =
nullptr;
70 if (
I &&
I->isLogicalShift() &&
83 if (
I->getOpcode() == Instruction::LShr && !
I->isExact()) {
88 if (
I->getOpcode() == Instruction::Shl && !
I->hasNoUnsignedWrap()) {
89 I->setHasNoUnsignedWrap();
98 return MadeChange ?
V :
nullptr;
116 bool HasAnyNoWrap =
I.hasNoSignedWrap() ||
I.hasNoUnsignedWrap();
117 Value *Neg = Builder.CreateNeg(OtherOp,
"", HasAnyNoWrap);
118 return Builder.CreateSelect(
Cond, OtherOp, Neg,
"",
126 bool HasAnyNoWrap =
I.hasNoSignedWrap() ||
I.hasNoUnsignedWrap();
127 Value *Neg = Builder.CreateNeg(OtherOp,
"", HasAnyNoWrap);
128 return Builder.CreateSelect(
Cond, Neg, OtherOp,
"",
138 return Builder.CreateSelectFMF(
139 Cond, OtherOp, Builder.CreateFNegFMF(OtherOp, &
I), &
I,
"",
148 return Builder.CreateSelectFMF(
149 Cond, Builder.CreateFNegFMF(OtherOp, &
I), OtherOp, &
I,
"",
163 const bool HasNSW =
Mul.hasNoSignedWrap();
164 const bool HasNUW =
Mul.hasNoUnsignedWrap();
170 return Builder.CreateShl(
X, Z,
Mul.getName(), HasNUW, PropagateNSW);
183 FrX = Builder.CreateFreeze(
X,
X->getName() +
".fr");
184 Value *Shl = Builder.CreateShl(FrX, Z,
"mulshl", HasNUW, PropagateNSW);
185 return Builder.CreateAdd(Shl, FrX,
Mul.getName(), HasNUW, PropagateNSW);
196 FrX = Builder.CreateFreeze(
X,
X->getName() +
".fr");
197 Value *Shl = Builder.CreateShl(FrX, Z,
"mulshl");
198 return Builder.CreateSub(Shl, FrX,
Mul.getName());
205 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
208 SQ.getWithInstruction(&
I)))
223 Type *Ty =
I.getType();
224 const unsigned BitWidth = Ty->getScalarSizeInBits();
225 const bool HasNSW =
I.hasNoSignedWrap();
226 const bool HasNUW =
I.hasNoUnsignedWrap();
245 assert(Shl &&
"Constant folding of immediate constants failed");
248 if (HasNUW &&
Mul->hasNoUnsignedWrap())
264 if (
match(NewCst,
m_APInt(V)) && *V != V->getBitWidth() - 1)
281 (*MulAP - 1).isPowerOf2() && *ShiftC == MulAP->
logBase2()) {
287 BinOp =
Builder.CreateLShr(NewOp, ConstantInt::get(Ty, *ShiftC),
"",
290 auto *NewAdd = BinaryOperator::CreateAdd(NewOp, BinOp);
291 if (HasNSW && (HasNUW || OpBO->
getOpcode() == Instruction::LShr ||
293 NewAdd->setHasNoSignedWrap(
true);
295 NewAdd->setHasNoUnsignedWrap(HasNUW);
309 HasNSW && Op1C->isNotMinSignedValue()));
318 const APInt *NegPow2C;
322 unsigned SrcWidth =
X->getType()->getScalarSizeInBits();
324 if (ShiftAmt >=
BitWidth - SrcWidth) {
327 return BinaryOperator::CreateShl(Z, ConstantInt::get(Ty, ShiftAmt));
358 auto Opc = OtherInst->getOpcode();
363 if (NewTV && NewFV) {
367 if (HasNUW && OtherInst->hasNoUnsignedWrap())
369 if (HasNSW && OtherInst->hasNoSignedWrap() &&
390 (BOp0->getOpcode() == Instruction::Or || BOp0->hasNoUnsignedWrap());
392 auto *BO = BinaryOperator::CreateAdd(NewMul, NewC);
393 if (HasNUW && Op0NUW) {
396 NewMulBO->setHasNoUnsignedWrap();
397 BO->setHasNoUnsignedWrap();
406 return BinaryOperator::CreateMul(
X,
X);
411 if (
I.hasNoSignedWrap() &&
416 I,
Builder.CreateBinaryIntrinsic(Intrinsic::abs,
429 auto *NewMul = BinaryOperator::CreateMul(
X,
Y);
432 NewMul->setHasNoSignedWrap();
445 return BinaryOperator::CreateMul(NegOp0,
X);
453 auto UDivCheck = [&
C1](
const APInt &
C) {
return C.urem(*C1).isZero(); };
454 auto SDivCheck = [&
C1](
const APInt &
C) {
475 if (!
Div || (
Div->getOpcode() != Instruction::UDiv &&
476 Div->getOpcode() != Instruction::SDiv)) {
480 Value *Neg = dyn_castNegVal(
Y);
481 if (
Div &&
Div->hasOneUse() &&
482 (
Div->getOperand(1) ==
Y ||
Div->getOperand(1) == Neg) &&
483 (
Div->getOpcode() == Instruction::UDiv ||
484 Div->getOpcode() == Instruction::SDiv)) {
485 Value *
X =
Div->getOperand(0), *DivOp1 =
Div->getOperand(1);
488 if (
Div->isExact()) {
494 auto RemOpc =
Div->getOpcode() == Instruction::UDiv ? Instruction::URem
499 XFreeze =
Builder.CreateFreeze(
X,
X->getName() +
".fr");
500 Value *Rem =
Builder.CreateBinOp(RemOpc, XFreeze, DivOp1);
502 return BinaryOperator::CreateSub(XFreeze, Rem);
503 return BinaryOperator::CreateSub(Rem, XFreeze);
512 if (Ty->isIntOrIntVectorTy(1) ||
515 return BinaryOperator::CreateAnd(Op0, Op1);
527 X->getType()->isIntOrIntVectorTy(1) &&
X->getType() ==
Y->getType() &&
528 (Op0->
hasOneUse() || Op1->hasOneUse() ||
X ==
Y)) {
537 X->getType()->isIntOrIntVectorTy(1) &&
X->getType() ==
Y->getType() &&
538 (Op0->
hasOneUse() || Op1->hasOneUse())) {
546 return createSelectInstWithUnknownProfile(
X, Op1,
549 return createSelectInstWithUnknownProfile(
X, Op0,
555 X->getType()->isIntOrIntVectorTy(1))
556 return createSelectInstWithUnknownProfile(
557 X,
Builder.CreateNeg(
Y,
"",
I.hasNoSignedWrap()),
565 return createSelectInstWithUnknownProfile(
X, NegC,
572 *
C ==
C->getBitWidth() - 1) {
575 return createSelectInstWithUnknownProfile(IsNeg, NegC,
585 *
C ==
C->getBitWidth() - 1) {
587 return createSelectInstWithUnknownProfile(IsNeg,
Y,
594 return createSelectInstWithUnknownProfile(Tr,
Y,
635 if (!HasNSW && willNotOverflowSignedMul(Op0, Op1,
I)) {
637 I.setHasNoSignedWrap(
true);
640 if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1,
I,
I.hasNoSignedWrap())) {
642 I.setHasNoUnsignedWrap(
true);
650 assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) &&
651 "Expected fmul or fdiv");
653 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
671 (Op0->
hasOneUse() || Op1->hasOneUse())) {
672 Value *XY = Builder.CreateBinOpFMF(Opcode,
X,
Y, &
I);
673 Value *Fabs = Builder.CreateFAbs(XY, &
I,
I.getName());
686 Intrinsic::powi, {
X->getType(), YZ->
getType()}, {
X, YZ}, &
I);
692 unsigned Opcode =
I.getOpcode();
693 assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) &&
694 "Unexpected opcode");
701 Constant *One = ConstantInt::get(
Y->getType(), 1);
702 if (willNotOverflowSignedAdd(
Y, One,
I)) {
703 Value *NewPow = createPowiExpr(
I, *
this,
X,
Y, One);
709 Value *Op0 =
I.getOperand(0);
710 Value *Op1 =
I.getOperand(1);
711 if (Opcode == Instruction::FMul &&
I.isOnlyUserOfAnyOperand() &&
716 Y->getType() == Z->getType() && willNotOverflowSignedAdd(
Y, Z,
I)) {
717 Value *NewPow = createPowiExpr(
I, *
this,
X,
Y, Z);
721 if (Opcode == Instruction::FDiv &&
I.hasAllowReassoc() &&
I.hasNoNaNs()) {
728 willNotOverflowSignedSub(
Y, ConstantInt::get(
Y->getType(), 1),
I)) {
730 Value *NewPow = createPowiExpr(
I, *
this, Op1,
Y, NegOne);
741 willNotOverflowSignedSub(
Y, ConstantInt::get(
Y->getType(), 1),
I)) {
743 auto *NewPow = createPowiExpr(
I, *
this,
X,
Y, NegOne);
775 return !R1.
empty() && !
R2.empty();
809 if (!
X->hasAllowReassoc() || !
X->hasAllowReciprocal() || !
X->hasNoInfs())
816 if (BBx != BBr1 && BBx != BBr2)
825 return (
I->getParent() != BBr1 || !
I->hasAllowReassoc());
835 return (
I->getParent() == BBr2 &&
I->hasAllowReassoc());
840 Value *Op0 =
I.getOperand(0);
841 Value *Op1 =
I.getOperand(1);
905 auto *NewFMul =
Builder.CreateFMulFMF(
X, Z, FMF);
916 Value *Sqrt =
Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &
I);
926 if (
I.hasNoSignedZeros() &&
930 if (
I.hasNoSignedZeros() &&
937 if (
I.hasNoNaNs() &&
I.hasNoSignedZeros() && Op0 == Op1 && Op0->
hasNUses(2)) {
956 Value *Y1 =
Builder.CreateFAddFMF(
Y, ConstantFP::get(
I.getType(), 1.0), &
I);
964 if (
I.isOnlyUserOfAnyOperand()) {
968 auto *YZ =
Builder.CreateFAddFMF(
Y, Z, &
I);
969 auto *NewPow =
Builder.CreateBinaryIntrinsic(Intrinsic::pow,
X, YZ, &
I);
975 auto *XZ =
Builder.CreateFMulFMF(
X, Z, &
I);
976 auto *NewPow =
Builder.CreateBinaryIntrinsic(Intrinsic::pow, XZ,
Y, &
I);
984 Value *Exp =
Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &
I);
992 Value *Exp2 =
Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &
I);
1018 I.getFastMathFlags(),
1019 SQ.getWithInstruction(&
I)))
1044 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1056 if (
I.hasNoNaNs() &&
I.hasNoSignedZeros()) {
1061 X->getType()->isIntOrIntVectorTy(1)) {
1062 auto *
SI = createSelectInstWithUnknownProfile(
1063 X, Op1, ConstantFP::get(
I.getType(), 0.0));
1064 SI->copyFastMathFlags(
I.getFastMathFlags());
1068 X->getType()->isIntOrIntVectorTy(1)) {
1069 auto *
SI = createSelectInstWithUnknownProfile(
1070 X, Op0, ConstantFP::get(
I.getType(), 0.0));
1071 SI->copyFastMathFlags(
I.getFastMathFlags());
1080 if (
I.hasAllowReassoc())
1108 Value *Start =
nullptr, *Step =
nullptr;
1122 if (!Result->hasNoNaNs())
1123 Result->setHasNoInfs(
false);
1128 if (
I.hasAllowContract() &&
1132 Value *Sin =
Builder.CreateUnaryIntrinsic(Intrinsic::sin,
X, &
I);
1133 if (
auto *
Metadata =
I.getMetadata(LLVMContext::MD_fpmath))
1135 SinI->setMetadata(LLVMContext::MD_fpmath,
Metadata);
1145 I,
Builder.CreateIntrinsic(Intrinsic::ldexp,
1146 {X->getType(), Y->getType()}, {X, Y}, &
I));
1183 Value *SelectCond =
SI->getCondition();
1190 while (BBI != BBFront) {
1198 for (
Use &
Op : BBI->operands()) {
1202 }
else if (
Op == SelectCond) {
1212 if (&*BBI == SelectCond)
1213 SelectCond =
nullptr;
1216 if (!SelectCond && !
SI)
1227 Product = IsSigned ?
C1.smul_ov(C2, Overflow) :
C1.umul_ov(C2, Overflow);
1241 if (IsSigned &&
C1.isMinSignedValue() && C2.
isAllOnes())
1244 APInt Remainder(
C1.getBitWidth(), 0ULL, IsSigned);
1254 assert((
I.getOpcode() == Instruction::SDiv ||
1255 I.getOpcode() == Instruction::UDiv) &&
1256 "Expected integer divide");
1258 bool IsSigned =
I.getOpcode() == Instruction::SDiv;
1259 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1260 Type *Ty =
I.getType();
1271 bool HasNUW =
Mul->hasNoUnsignedWrap() && Shl->hasNoUnsignedWrap();
1272 bool HasNSW =
Mul->hasNoSignedWrap() && Shl->hasNoSignedWrap();
1275 if (!IsSigned && HasNUW)
1276 return Builder.CreateLShr(
Y, Z,
"",
I.isExact());
1279 if (IsSigned && HasNSW && (Op0->
hasOneUse() || Op1->hasOneUse())) {
1280 Value *Shl = Builder.CreateShl(ConstantInt::get(Ty, 1), Z);
1281 return Builder.CreateSDiv(
Y, Shl,
"",
I.isExact());
1296 ((Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap()) ||
1297 (Shl0->hasNoUnsignedWrap() && Shl0->hasNoSignedWrap() &&
1298 Shl1->hasNoSignedWrap())))
1299 return Builder.CreateUDiv(
X,
Y,
"",
I.isExact());
1303 if (IsSigned && Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap() &&
1304 Shl1->hasNoUnsignedWrap())
1305 return Builder.CreateSDiv(
X,
Y,
"",
I.isExact());
1315 if (IsSigned ? (Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap())
1316 : (Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap())) {
1317 Constant *One = ConstantInt::get(
X->getType(), 1);
1320 Value *Dividend = Builder.CreateShl(
1321 One,
Y,
"shl.dividend",
1324 IsSigned ? (Shl0->hasNoUnsignedWrap() || Shl1->hasNoUnsignedWrap())
1325 : Shl0->hasNoSignedWrap());
1326 return Builder.CreateLShr(Dividend, Z,
"",
I.isExact());
1336 assert((
Div.getOpcode() == Instruction::SDiv ||
1337 Div.getOpcode() == Instruction::UDiv) &&
1338 "Expected integer divide");
1341 if (!
Div.getType()->isIntegerTy())
1344 unsigned RemOpcode =
Div.getOpcode() == Instruction::SDiv ? Instruction::SRem
1345 : Instruction::URem;
1350 auto IsKnownZeroRemainder = [&](
Value *
Cond) {
1352 if (!Cmp || !Cmp->isEquality())
1365 return Known.isZero();
1375 if (IsKnownZeroRemainder(BI->getCondition()))
1381 if (!AssumeValueHandle)
1384 if (IsKnownZeroRemainder(Assume->getArgOperand(0)))
1394 assert(
I.isIntDivRem() &&
"Unexpected instruction");
1395 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1442 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1443 bool IsSigned =
I.getOpcode() == Instruction::SDiv;
1444 Type *Ty =
I.getType();
1454 APInt Product(
C1->getBitWidth(), 0ULL, IsSigned);
1457 ConstantInt::get(Ty, Product));
1467 ConstantInt::get(Ty, Quotient));
1468 NewDiv->setIsExact(
I.isExact());
1475 ConstantInt::get(Ty, Quotient));
1477 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
1478 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1486 APInt NewC1 = IsSigned ?
C1->sdiv(GCD) :
C1->udiv(GCD);
1490 Value *NewMul =
Builder.CreateMul(
X, ConstantInt::get(Ty, NewC1),
"",
1491 OldMul->hasNoUnsignedWrap(),
1492 OldMul->hasNoSignedWrap());
1495 Constant *NewDivisor = ConstantInt::get(Ty, NewC2);
1498 NewDiv->setIsExact(
I.isExact());
1505 C1->ult(
C1->getBitWidth() - 1)) ||
1507 C1->ult(
C1->getBitWidth()))) {
1509 C1->getBitWidth(),
static_cast<unsigned>(
C1->getZExtValue()));
1512 if (
isMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
1514 ConstantInt::get(Ty, Quotient));
1515 BO->setIsExact(
I.isExact());
1520 if (
isMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
1522 ConstantInt::get(Ty, Quotient));
1524 Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
1525 Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1532 unsigned ShiftAmt =
static_cast<unsigned>(
C1->getZExtValue());
1535 unsigned NewShiftAmt = ShiftAmt -
K;
1540 X, ConstantInt::get(Ty, NewShiftAmt),
"",
1541 OldShift->hasNoUnsignedWrap(), OldShift->hasNoSignedWrap());
1544 Constant *NewDivisor = ConstantInt::get(Ty, NewC2);
1547 NewDiv->setIsExact(
I.isExact());
1561 return BinaryOperator::CreateNSWAdd(
X, ConstantInt::get(Ty, Quotient));
1566 return BinaryOperator::CreateNUWAdd(
X,
1567 ConstantInt::get(Ty,
C1->udiv(*C2)));
1576 assert(!Ty->isIntOrIntVectorTy(1) &&
"i1 divide not removed?");
1583 F1 =
Builder.CreateFreeze(Op1, Op1->getName() +
".fr");
1585 Value *Cmp =
Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3));
1586 return createSelectInstWithUnknownProfile(Cmp, F1,
1587 ConstantInt::get(Ty, 0));
1609 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1),
Y);
1611 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1),
Y);
1617 if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) {
1626 if (!IsSigned && Op1->hasOneUse() &&
1631 Builder.CreateShl(ConstantInt::get(Ty, 1), Z,
"",
true),
Y);
1647 if (!IsSigned &&
Mul->hasNoUnsignedWrap())
1648 NewDiv = BinaryOperator::CreateUDiv(
X,
Y);
1649 else if (IsSigned &&
Mul->hasNoSignedWrap())
1650 NewDiv = BinaryOperator::CreateSDiv(
X,
Y);
1654 NewDiv->
setIsExact(
I.isExact() && InnerDiv->isExact());
1666 auto IsSafeDivisor = [&](
Value *V) {
1673 IsSafeDivisor(DivY)) {
1675 Builder.CreateExactBinOp(
I.getOpcode(), Op0, DivY,
I.isExact());
1680 IsSafeDivisor(DivY)) {
1682 Builder.CreateExactBinOp(
I.getOpcode(), Op0, DivY,
I.isExact());
1698 if (IsSigned && OB0HasNSW) {
1700 return BinaryOperator::CreateSDiv(
A,
B);
1702 if (!IsSigned && OB0HasNUW) {
1704 return BinaryOperator::CreateUDiv(
A,
B);
1706 return BinaryOperator::CreateUDiv(
A,
B);
1712 if (
auto *Val = CreateDivOrNull(
Y, Z))
1716 if (
auto *Val = CreateDivOrNull(
X, Z))
1727 return reinterpret_cast<Value *
>(-1);
1735 return IfFold([&]() {
1751 return IfFold([&]() {
return Builder.CreateZExt(LogX,
Op->getType()); });
1757 if (AssumeNonZero || TI->hasNoUnsignedWrap())
1759 return IfFold([&]() {
1760 return Builder.CreateTrunc(LogX,
Op->getType(),
"",
1761 TI->hasNoUnsignedWrap());
1770 if (AssumeNonZero || BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap())
1772 return IfFold([&]() {
return Builder.CreateAdd(LogX,
Y); });
1779 if (AssumeNonZero || PEO->isExact())
1781 return IfFold([&]() {
return Builder.CreateSub(LogX,
Y); });
1788 return IfFold([&]() {
return LogX; });
1790 return IfFold([&]() {
return LogY; });
1799 return IfFold([&]() {
1800 return Builder.CreateSelect(
SI->getOperand(0), LogX, LogY,
"",
SI);
1813 return IfFold([&]() {
1814 return Builder.CreateBinaryIntrinsic(
MinMax->getIntrinsicID(), LogX,
1820 if (
Op->getType()->getScalarSizeInBits() != 1 &&
1823 return IfFold([&]() {
return X; });
1835 Type *Ty =
I.getType();
1838 X->getType() ==
Y->getType() && (
N->hasOneUse() ||
D->hasOneUse())) {
1875 SQ.getWithInstruction(&
I)))
1885 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1895 X, ConstantInt::get(
X->getType(), C2ShlC1));
1907 willNotOverflowUnsignedMul(
Y, Op1,
I)) {
1909 auto *NewDiv = BinaryOperator::CreateUDiv(
X, YZ);
1912 NewDiv->setIsExact();
1919 Type *Ty =
I.getType();
1945 auto GetShiftableDenom = [&](
Value *Denom) ->
Value * {
1955 return Builder.CreateBinaryIntrinsic(Intrinsic::cttz, Denom,
1961 if (
auto *Res = GetShiftableDenom(Op1))
1963 I,
Builder.CreateLShr(Op0, Res,
I.getName(),
I.isExact()));
1970 SQ.getWithInstruction(&
I)))
1980 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1981 Type *Ty =
I.getType();
1997 return BinaryOperator::CreateExactAShr(Op0,
C);
2003 return BinaryOperator::CreateExactAShr(Op0, ShAmt);
2009 Value *Ashr =
Builder.CreateAShr(Op0,
C,
I.getName() +
".neg",
true);
2030 Value *NarrowOp =
Builder.CreateSDiv(Op0Src, NarrowDivisor);
2038 Constant *NegC = ConstantInt::get(Ty, -(*Op1C));
2047 if (
Value *NegOp0 = dyn_castNegVal(Op0))
2048 if (
Value *NegOp1 = dyn_castNegVal(Op1))
2050 .getSignedMinValue()
2051 .isMinSignedValue() ||
2054 auto *BO = BinaryOperator::CreateSDiv(NegOp0, NegOp1);
2055 BO->setIsExact(
I.isExact());
2062 Builder.CreateSDiv(
X,
Y,
I.getName(),
I.isExact()));
2070 return createSelectInstWithUnknownProfile(
Cond, ConstantInt::get(Ty, 1),
2085 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1,
I.getName());
2086 BO->setIsExact(
I.isExact());
2095 Value *Shr =
Builder.CreateLShr(Op0, CNegLog2,
I.getName(),
I.isExact());
2104 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1,
I.getName());
2105 BO->setIsExact(
I.isExact());
2114 return createSelectInstWithUnknownProfile(
Cond, ConstantInt::get(Ty, 1),
2136 if (
I.hasNoNaNs() &&
2140 Value *CopySign =
B.CreateIntrinsic(
2141 Intrinsic::copysign, {
C->getType()},
2150 if (!(
C->hasExactInverseFP() || (
I.hasAllowReciprocal() &&
C->isNormalFP())))
2158 Instruction::FDiv, ConstantFP::get(
I.getType(), 1.0),
C,
DL);
2159 if (!RecipC || !RecipC->isNormalFP())
2180 if (!
I.hasAllowReassoc() || !
I.hasAllowReciprocal())
2210 if (!
I.hasAllowReassoc() || !
I.hasAllowReciprocal())
2213 Value *Op0 =
I.getOperand(0);
2214 Value *Op1 =
I.getOperand(1);
2216 Value *Divisor = Op1;
2225 if (!
II || !
II->hasOneUse())
2232 case Intrinsic::pow:
2233 Args.push_back(
II->getArgOperand(0));
2234 Args.push_back(Builder.CreateFNegFMF(
II->getArgOperand(1), &
I));
2236 case Intrinsic::powi: {
2244 Args.push_back(
II->getArgOperand(0));
2245 Args.push_back(Builder.CreateNeg(
II->getArgOperand(1)));
2249 case Intrinsic::exp:
2250 case Intrinsic::exp2:
2251 Args.push_back(Builder.CreateFNegFMF(
II->getArgOperand(0), &
I));
2257 Value *
Pow = Builder.CreateIntrinsic(IID, Tys, Args, &
I);
2258 if (
Pow->getType() !=
I.getType())
2259 Pow = Builder.CreateVectorSplat(
2270 if (!
I.hasAllowReassoc() || !
I.hasAllowReciprocal())
2272 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
2274 if (!
II ||
II->getIntrinsicID() != Intrinsic::sqrt || !
II->hasOneUse() ||
2275 !
II->hasAllowReassoc() || !
II->hasAllowReciprocal())
2284 if (!DivOp->hasAllowReassoc() || !
I.hasAllowReciprocal() ||
2285 !DivOp->hasOneUse())
2287 Value *SwapDiv = Builder.CreateFDivFMF(Z,
Y, DivOp);
2289 Builder.CreateUnaryIntrinsic(
II->getIntrinsicID(), SwapDiv,
II);
2312 B.SetInsertPoint(
X);
2318 B.CreateFDiv(ConstantFP::get(
X->getType(), 1.0), SqrtOp));
2319 auto *R1FPMathMDNode = (*R1.
begin())->getMetadata(LLVMContext::MD_fpmath);
2323 R1FPMathMDNode,
I->getMetadata(LLVMContext::MD_fpmath));
2324 R1FMF &=
I->getFastMathFlags();
2328 FDiv->setMetadata(LLVMContext::MD_fpmath, R1FPMathMDNode);
2329 FDiv->copyFastMathFlags(R1FMF);
2336 auto *R2FPMathMDNode = (*
R2.begin())->getMetadata(LLVMContext::MD_fpmath);
2340 R2FPMathMDNode,
I->getMetadata(LLVMContext::MD_fpmath));
2341 R2FMF &=
I->getFastMathFlags();
2345 FSqrt->setMetadata(LLVMContext::MD_fpmath, R2FPMathMDNode);
2346 FSqrt->copyFastMathFlags(R2FMF);
2355 FMul->copyMetadata(*
X);
2365 I.getFastMathFlags(),
2366 SQ.getWithInstruction(&
I)))
2384 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
2413 if (
I.hasAllowReassoc() &&
I.hasAllowReciprocal()) {
2437 if (
I.hasAllowReassoc() && Op0->
hasOneUse() && Op1->hasOneUse()) {
2447 if ((IsTan || IsCot) &&
hasFloatFn(M, &
TLI,
I.getType(), LibFunc_tan,
2448 LibFunc_tanf, LibFunc_tanl)) {
2451 B.setFastMathFlags(
I.getFastMathFlags());
2455 LibFunc_tanl,
B, Attrs);
2457 Res =
B.CreateFDiv(ConstantFP::get(
I.getType(), 1.0), Res);
2466 if (
I.hasNoNaNs() &&
I.hasAllowReassoc() &&
2475 if (
I.hasNoNaNs() &&
I.hasNoInfs() &&
2479 Intrinsic::copysign, ConstantFP::get(
I.getType(), 1.0),
X, &
I);
2490 if (
I.hasAllowReassoc() &&
2494 Builder.CreateFAddFMF(
Y, ConstantFP::get(
I.getType(), -1.0), &
I);
2513 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1), *
X =
nullptr;
2515 bool ShiftByX =
false;
2519 bool &PreserveNSW) ->
bool {
2520 const APInt *Tmp =
nullptr;
2539 const APInt *Tmp =
nullptr;
2551 bool Op0PreserveNSW =
true, Op1PreserveNSW =
true;
2552 if (MatchShiftOrMulXC(Op0,
X,
Y, Op0PreserveNSW) &&
2553 MatchShiftOrMulXC(Op1,
X, Z, Op1PreserveNSW)) {
2555 }
else if (MatchShiftCX(Op0,
Y,
X) && MatchShiftCX(Op1, Z,
X)) {
2561 bool IsSRem =
I.getOpcode() == Instruction::SRem;
2568 bool BO0NoWrap = IsSRem ? BO0HasNSW : BO0HasNUW;
2570 APInt RemYZ = IsSRem ?
Y.srem(Z) :
Y.urem(Z);
2574 if (RemYZ.
isZero() && BO0NoWrap)
2580 auto CreateMulOrShift =
2582 Value *RemSimplification =
2583 ConstantInt::get(
I.getType(), RemSimplificationC);
2584 return ShiftByX ? BinaryOperator::CreateShl(RemSimplification,
X)
2585 : BinaryOperator::CreateMul(
X, RemSimplification);
2591 bool BO1NoWrap = IsSRem ? BO1HasNSW : BO1HasNUW;
2595 if (RemYZ ==
Y && BO1NoWrap) {
2606 if (
Y.uge(Z) && (IsSRem ? (BO0HasNSW && BO1HasNSW) : BO0HasNUW)) {
2624 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
2632 const APInt *Op1Int;
2634 (
I.getOpcode() == Instruction::URem ||
2658 SQ.getWithInstruction(&
I)))
2671 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
2672 Type *Ty =
I.getType();
2678 return BinaryOperator::CreateAnd(Op0,
Add);
2683 Value *Cmp =
Builder.CreateICmpNE(Op1, ConstantInt::get(Ty, 1));
2695 return createSelectInstWithUnknownProfile(Cmp, F0,
Sub);
2704 Value *FrozenOp0 = Op0;
2706 FrozenOp0 =
Builder.CreateFreeze(Op0, Op0->
getName() +
".frozen");
2709 return createSelectInstWithUnknownProfile(
2718 Value *FrozenOp0 = Op0;
2720 FrozenOp0 =
Builder.CreateFreeze(Op0, Op0->
getName() +
".frozen");
2722 return createSelectInstWithUnknownProfile(
2732 SQ.getWithInstruction(&
I)))
2742 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
2760 return BinaryOperator::CreateURem(Op0, Op1,
I.getName());
2768 bool hasNegative =
false;
2769 bool hasMissing =
false;
2770 for (
unsigned i = 0; i != VWidth; ++i) {
2771 Constant *Elt =
C->getAggregateElement(i);
2778 if (RHS->isNegative())
2782 if (hasNegative && !hasMissing) {
2784 for (
unsigned i = 0; i != VWidth; ++i) {
2785 Elts[i] =
C->getAggregateElement(i);
2787 if (RHS->isNegative())
2803 I.getFastMathFlags(),
2804 SQ.getWithInstruction(&
I)))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides internal interfaces used to implement the InstCombine.
static Instruction * convertFSqrtDivIntoFMul(CallInst *CI, Instruction *X, const SmallPtrSetImpl< Instruction * > &R1, const SmallPtrSetImpl< Instruction * > &R2, InstCombiner::BuilderTy &B, InstCombinerImpl *IC)
static Instruction * simplifyIRemMulShl(BinaryOperator &I, InstCombinerImpl &IC)
static Instruction * narrowUDivURem(BinaryOperator &I, InstCombinerImpl &IC)
If we have zero-extended operands of an unsigned div or rem, we may be able to narrow the operation (...
static bool getFSqrtDivOptPattern(Instruction *Div, SmallPtrSetImpl< Instruction * > &R1, SmallPtrSetImpl< Instruction * > &R2)
static Value * foldMulSelectToNegate(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static bool isFSqrtDivToFMulLegal(Instruction *X, SmallPtrSetImpl< Instruction * > &R1, SmallPtrSetImpl< Instruction * > &R2)
static Instruction * foldFDivPowDivisor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Negate the exponent of pow/exp to fold division-by-pow() into multiply.
static Value * simplifyValueKnownNonZero(Value *V, InstCombinerImpl &IC, Instruction &CtxI)
The specific integer value is used in a context where it is known to be non-zero.
static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, bool IsSigned)
True if the multiply can not be expressed in an int this size.
static Value * foldMulShl1(BinaryOperator &Mul, bool CommuteOperands, InstCombiner::BuilderTy &Builder)
Reduce integer multiplication patterns that contain a (+/-1 << Z) factor.
static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, bool IsSigned)
True if C1 is a multiple of C2. Quotient contains C1/C2.
static Instruction * foldFDivSqrtDivisor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Convert div to mul if we have an sqrt divisor iff sqrt's operand is a fdiv instruction.
static Instruction * foldFDivConstantDividend(BinaryOperator &I)
Remove negation and try to reassociate constant math.
static Value * foldIDivShl(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static bool isKnownExactFromRem(const BinaryOperator &Div, const SimplifyQuery &Q)
Return true if a matching remainder is known to be zero at the division.
This file provides the interface for the instcombine pass implementation.
static bool hasNoSignedWrap(BinaryOperator &I)
static bool hasNoUnsignedWrap(BinaryOperator &I)
uint64_t IntrinsicInst * II
This file contains the declarations for profiling metadata utility functions.
SmallVector< MachineOperand, 4 > Cond
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Class for arbitrary precision integers.
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
bool isMinValue() const
Determine if this is the smallest unsigned value.
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
unsigned countr_zero() const
Count the number of trailing zero bits.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
LLVM_ABI APInt ushl_ov(const APInt &Amt, bool &Overflow) const
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
unsigned logBase2() const
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
static BinaryOperator * CreateFAddFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
static LLVM_ABI BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
static BinaryOperator * CreateExact(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
static BinaryOperator * CreateFSubFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
static BinaryOperator * CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Value *CopyO, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
static LLVM_ABI CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
@ ICMP_ULT
unsigned less than
Conditional Branch instruction.
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getExactLogBase2(Constant *C)
If C is a scalar/fixed width vector of known powers of 2, then this function returns a new scalar/fix...
static LLVM_ABI ConstantFP * getInfinity(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
LLVM_ABI bool isNormalFP() const
Return true if this is a normal (as opposed to denormal, infinity, nan, or zero) floating-point scala...
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value, or, for vectors, does not contain smallest...
A parsed version of the target data layout string in and methods for querying it.
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
Convenience struct for specifying and reasoning about fast-math flags.
static FastMathFlags intersectRewrite(FastMathFlags LHS, FastMathFlags RHS)
Intersect rewrite-based flags.
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
bool allowReassoc() const
Flag queries.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Instruction * visitMul(BinaryOperator &I)
Instruction * foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I)
Tries to simplify binops of select and cast of the select condition.
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Instruction * visitUDiv(BinaryOperator &I)
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Performs a few simplifications for operators which are associative or commutative.
Value * foldUsingDistributiveLaws(BinaryOperator &I)
Tries to simplify binary operations which some other binary operation distributes over.
Instruction * visitURem(BinaryOperator &I)
bool SimplifyDemandedInstructionFPClass(Instruction &Inst)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Value * takeLog2(Value *Op, unsigned Depth, bool AssumeNonZero, bool DoFold)
Take the exact integer log2 of the value.
Instruction * visitSRem(BinaryOperator &I)
Instruction * foldBinOpSelectBinOp(BinaryOperator &Op)
In some cases it is beneficial to fold a select into a binary operator.
Instruction * visitFDiv(BinaryOperator &I)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false, bool SimplifyBothArms=false)
Given an instruction with a select as one operand and a constant as the other operand,...
bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I)
Fold a divide or remainder with a select instruction divisor when one of the select operands is zero.
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * commonIDivRemTransforms(BinaryOperator &I)
Common integer divide/remainder transforms.
Value * tryGetLog2(Value *Op, bool AssumeNonZero)
Instruction * commonIDivTransforms(BinaryOperator &I)
This function implements the transforms common to both integer division instructions (udiv and sdiv).
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Instruction * visitFRem(BinaryOperator &I)
InstCombinerImpl(InstructionWorklist &Worklist, Function &F, AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const DataLayout &DL, ReversePostOrderTraversal< BasicBlock * > &RPOT, const InstCombineCLOptions &CLOpts)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * visitFMul(BinaryOperator &I)
Instruction * foldFMulReassoc(BinaryOperator &I)
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
Value * SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS, Value *RHS)
Instruction * foldPowiReassoc(BinaryOperator &I)
Instruction * visitSDiv(BinaryOperator &I)
Instruction * commonIRemTransforms(BinaryOperator &I)
This function implements the transforms common to both integer remainder instructions (urem and srem)...
const DataLayout & getDataLayout() const
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CtxI=nullptr, unsigned Depth=0)
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const Instruction *CtxI=nullptr, unsigned Depth=0) const
IRBuilder< TargetFolder, IRBuilderInstCombineInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
static Value * stripSignOnlyFPOps(Value *Val)
Ignore all operations which only change the sign of a value, returning the underlying magnitude value...
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CtxI, unsigned Depth=0) const
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
LLVM_ABI bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
iterator_range< user_iterator > users()
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI bool hasAllowReassoc() const LLVM_READONLY
Determine whether the allow-reassociation flag is set.
A wrapper class for inspecting calls to intrinsic functions.
static LLVM_ABI MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
A Module instance is used to store all the information related to an LLVM module.
static Value * Negate(bool LHSIsZero, bool IsNSW, Value *Root, InstCombinerImpl &IC)
Attempt to negate Root.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name="", InsertPosition InsertBefore=nullptr)
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
This class represents zero extension of integer types.
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B, bool IsSigned=false)
Compute GCD of two APInt values.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
auto m_PosZeroFP()
Matches a floating-point positive zero.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
AllOnesConstantMatch m_AllOnes()
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
auto m_Sqrt(const Opnd0 &Op0)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_UndefValue()
Match an arbitrary UndefValue constant.
auto m_Constant()
Match an arbitrary Constant and ignore it.
ContainsMatchingVectorElement_match< SPTy > m_ContainsMatchingVectorElement(const SPTy &SubPattern)
Match a vector constant where at least one of its elements matches the subpattern.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::AllowReassoc > m_AllowReassoc(const T &SubPattern)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
is_zero< true > m_Zero()
Match any null constant or a vector with all elements equal to 0.
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
FNeg_match< OpTy, true > m_FNegOrFSubNegZero(const OpTy &X)
Match fneg X or fsub -0.0, X.
auto m_FAbs(const Opnd0 &Op0)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Value * emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
LLVM_ABI cl::opt< bool > ProfcheckDisableMetadataFixes
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Value * simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FMul, fold the result or return null.
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
@ Known
Known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Value * simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
LLVM_ABI Value * simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
LLVM_ABI bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
LLVM_ABI bool isKnownNegative(const WithCache< const Value * > &V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI Constant * getLosslessUnsignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Value * simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FRem, fold the result or return null.
LLVM_ABI Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI Value * simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FDiv, fold the result or return null.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI Value * simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
DWARFExpression::Operation Op
constexpr unsigned BitWidth
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Value * simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
unsigned Log2(Align A)
Returns the log2 of the alignment.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI Value * simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
SimplifyQuery getWithInstruction(const Instruction *I) const
const DomConditionCache * DC