20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
35 bool MadeChange =
false;
39 Value *
A =
nullptr, *
B =
nullptr, *One =
nullptr;
73 return MadeChange ? V :
nullptr;
82 Product = C1.
smul_ov(C2, Overflow);
84 Product = C1.
umul_ov(C2, Overflow);
93 "Inconsistent width of constants!");
109 return Remainder.isMinValue();
133 bool InstCombiner::willNotOverflowSignedMul(
const Value *LHS,
151 if (SignBits > BitWidth + 1)
159 if (SignBits == BitWidth + 1) {
173 bool Changed = SimplifyAssociativeOrCommutative(I);
176 if (
Value *V = SimplifyVectorOp(I))
177 return replaceInstUsesWith(I, V);
180 return replaceInstUsesWith(I, V);
182 if (
Value *V = SimplifyUsingDistributiveLaws(I))
183 return replaceInstUsesWith(I, V);
206 BO->setHasNoUnsignedWrap();
209 BO->setHasNoSignedWrap();
240 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
245 const APInt & Val = CI->getValue();
248 Value *
X =
nullptr, *
Y =
nullptr;
251 Value *Sub =
nullptr;
253 Sub = Builder.CreateSub(X,
Y,
"suba");
255 Sub = Builder.CreateSub(Builder.CreateNeg(C1),
Y,
"subc");
266 if (isa<Constant>(Op1)) {
267 if (
Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
275 Value *Mul = Builder.CreateMul(C1, Op1);
284 if (
Value *Op0v = dyn_castNegVal(Op0)) {
285 if (
Value *Op1v = dyn_castNegVal(Op1)) {
300 if (!Div || (Div->
getOpcode() != Instruction::UDiv &&
301 Div->
getOpcode() != Instruction::SDiv)) {
305 Value *Neg = dyn_castNegVal(Y);
308 (Div->
getOpcode() == Instruction::UDiv ||
309 Div->
getOpcode() == Instruction::SDiv)) {
315 return replaceInstUsesWith(I, X);
319 auto RemOpc = Div->
getOpcode() == Instruction::UDiv ? Instruction::URem
321 Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1);
323 return BinaryOperator::CreateSub(X, Rem);
324 return BinaryOperator::CreateSub(Rem, X);
330 return BinaryOperator::CreateAnd(Op0, Op1);
339 BO = BinaryOperator::CreateShl(Op1, Y);
340 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
342 BO = BinaryOperator::CreateShl(Op0, Y);
343 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
361 Value *BoolCast =
nullptr, *OtherOp =
nullptr;
373 return BinaryOperator::CreateAnd(V, OtherOp);
379 if (
SExtInst *Op0Conv = dyn_cast<SExtInst>(Op0)) {
381 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
382 if (Op0Conv->hasOneUse()) {
386 willNotOverflowSignedMul(Op0Conv->getOperand(0), CI,
I)) {
389 Builder.CreateNSWMul(Op0Conv->getOperand(0), CI,
"mulconv");
396 if (
SExtInst *Op1Conv = dyn_cast<SExtInst>(Op1)) {
400 if (Op0Conv->getOperand(0)->getType() ==
401 Op1Conv->getOperand(0)->getType() &&
402 (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) &&
403 willNotOverflowSignedMul(Op0Conv->getOperand(0),
404 Op1Conv->getOperand(0),
I)) {
406 Value *NewMul = Builder.CreateNSWMul(
407 Op0Conv->getOperand(0), Op1Conv->getOperand(0),
"mulconv");
415 if (
auto *Op0Conv = dyn_cast<ZExtInst>(Op0)) {
417 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
418 if (Op0Conv->hasOneUse()) {
422 willNotOverflowUnsignedMul(Op0Conv->getOperand(0), CI,
I)) {
425 Builder.CreateNUWMul(Op0Conv->getOperand(0), CI,
"mulconv");
432 if (
auto *Op1Conv = dyn_cast<ZExtInst>(Op1)) {
436 if (Op0Conv->getOperand(0)->getType() ==
437 Op1Conv->getOperand(0)->getType() &&
438 (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) &&
439 willNotOverflowUnsignedMul(Op0Conv->getOperand(0),
440 Op1Conv->getOperand(0),
I)) {
442 Value *NewMul = Builder.CreateNUWMul(
443 Op0Conv->getOperand(0), Op1Conv->getOperand(0),
"mulconv");
459 return Changed ? &I :
nullptr;
501 return isa<ConstantFP>(
C) &&
524 if (!I || (I->
getOpcode() != Instruction::FMul &&
557 if (FMulOrDiv->
getOpcode() == Instruction::FMul) {
560 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
568 R = BinaryOperator::CreateFDiv(F, Opnd1);
574 R = BinaryOperator::CreateFMul(Opnd0, F);
579 R = BinaryOperator::CreateFDiv(Opnd0, F);
585 R->setHasUnsafeAlgebra(
true);
586 InsertNewInstWith(R, *InsertBefore);
593 bool Changed = SimplifyAssociativeOrCommutative(I);
596 if (
Value *V = SimplifyVectorOp(I))
597 return replaceInstUsesWith(I, V);
599 if (isa<Constant>(Op0))
603 SQ.getWithInstruction(&I)))
604 return replaceInstUsesWith(I, V);
609 if (isa<Constant>(Op1)) {
610 if (
Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
616 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
617 RI->copyFastMathFlags(&I);
628 if (
Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
629 return replaceInstUsesWith(I, V);
634 (FAddSub->
getOpcode() == Instruction::FAdd ||
635 FAddSub->
getOpcode() == Instruction::FSub)) {
650 foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
653 if (Swap && FAddSub->
getOpcode() == Instruction::FSub)
657 ? BinaryOperator::CreateFAdd(M0, M1)
658 : BinaryOperator::CreateFSub(M0, M1);
670 if (AllowReassociate && II->getIntrinsicID() == Intrinsic::sqrt)
671 return replaceInstUsesWith(I, II->
getOperand(0));
674 if (II->getIntrinsicID() == Intrinsic::fabs) {
675 Instruction *FMulVal = BinaryOperator::CreateFMul(II->getOperand(0),
686 if (AllowReassociate) {
687 Value *OpX =
nullptr;
688 Value *OpY =
nullptr;
704 Value *FMulVal = Builder.CreateFMul(OpX, Log2);
705 Value *FSub = Builder.CreateFSub(FMulVal, OpX);
707 return replaceInstUsesWith(I, FSub);
714 for (
int i = 0; i < 2; i++) {
720 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
721 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
725 Value *FMul = Builder.CreateFMul(N0, N1);
727 return replaceInstUsesWith(I, FMul);
732 Value *
T = Builder.CreateFMul(N0, Opnd1);
733 Value *Neg = Builder.CreateFNeg(T);
735 return replaceInstUsesWith(I, Neg);
747 if (AllowReassociate) {
748 Value *Opnd0_0, *Opnd0_1;
752 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
754 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
760 Value *
T = Builder.CreateFMul(Opnd1, Opnd1);
761 Value *R = Builder.CreateFMul(T, Y);
763 return replaceInstUsesWith(I, R);
768 if (!isa<Constant>(Op1))
774 return Changed ? &I :
nullptr;
782 int NonNullOperand = -1;
784 if (
ST->isNullValue())
788 if (
ST->isNullValue())
791 if (NonNullOperand == -1)
813 while (BBI != BBFront) {
817 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
826 }
else if (*I == SelectCond) {
827 *I = Builder.getInt1(NonNullOperand == 1);
835 if (&*BBI == SelectCond)
836 SelectCond =
nullptr;
839 if (!SelectCond && !SI)
862 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
865 if (
Instruction *LHS = dyn_cast<Instruction>(Op0)) {
870 bool IsSigned = I.
getOpcode() == Instruction::SDiv;
886 if (
IsMultiple(*C2, *C1, Quotient, IsSigned)) {
894 if (
IsMultiple(*C1, *C2, Quotient, IsSigned)) {
899 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
901 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
914 if (
IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
922 if (
IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
927 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
929 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
935 if (
Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I))
942 if (I.
getOpcode() == Instruction::SDiv) {
946 Value *Inc = Builder.CreateAdd(Op1, Op0);
957 if (SimplifyDemandedInstructionBits(I))
961 Value *
X =
nullptr, *
Z =
nullptr;
963 bool isSigned = I.
getOpcode() == Instruction::SDiv;
975 if (
ZExtInst *
Z = dyn_cast<ZExtInst>(V)) {
976 if (
Z->getSrcTy() == Ty)
977 return Z->getOperand(0);
979 if (
C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->
getBitWidth())
992 struct UDivFoldAction {
993 FoldUDivOperandCb FoldAction;
997 Value *OperandToFold;
1002 size_t SelectLHSIdx;
1006 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand)
1007 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
1008 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand,
size_t SLHS)
1009 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
1016 const APInt &
C = cast<Constant>(Op1)->getUniqueInteger();
1047 if (Op1 != ShiftLeft)
1061 unsigned Depth = 0) {
1066 return Actions.
size();
1071 if (
C->getValue().isNegative()) {
1073 return Actions.
size();
1080 return Actions.
size();
1091 Actions.push_back(UDivFoldAction(
nullptr, Op1, LHSIdx - 1));
1092 return Actions.size();
1101 if (
Value *V = SimplifyVectorOp(I))
1102 return replaceInstUsesWith(I, V);
1105 return replaceInstUsesWith(I, V);
1108 if (
Instruction *Common = commonIDivTransforms(I))
1114 const APInt *C1, *C2;
1131 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1134 Builder.CreateUDiv(ZOp0->getOperand(0), ZOp1,
"div", I.
isExact()),
1140 for (
unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
1141 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
1142 Value *ActionOp1 = UDivActions[i].OperandToFold;
1145 Inst = Action(Op0, ActionOp1, I, *
this);
1150 size_t SelectRHSIdx = i - 1;
1151 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
1152 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
1153 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
1155 SelectLHS, SelectRHS);
1163 UDivActions[i].FoldResult = Inst;
1174 if (
Value *V = SimplifyVectorOp(I))
1175 return replaceInstUsesWith(I, V);
1178 return replaceInstUsesWith(I, V);
1181 if (
Instruction *Common = commonIDivTransforms(I))
1193 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.
getName());
1209 Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor);
1214 if (
Constant *RHS = dyn_cast<Constant>(Op1)) {
1216 if (RHS->isMinSignedValue())
1234 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1244 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1261 bool AllowReciprocal) {
1262 if (!isa<ConstantFP>(Divisor))
1265 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
1272 Cvt = !Reciprocal.isDenormal();
1280 return BinaryOperator::CreateFMul(Dividend, R);
1286 if (
Value *V = SimplifyVectorOp(I))
1287 return replaceInstUsesWith(I, V);
1290 SQ.getWithInstruction(&I)))
1291 return replaceInstUsesWith(I, V);
1293 if (isa<Constant>(Op0))
1301 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
1306 if (AllowReassociate) {
1317 Res = BinaryOperator::CreateFMul(X, C);
1325 Res = BinaryOperator::CreateFDiv(X, C);
1337 T->copyFastMathFlags(&I);
1344 if (AllowReassociate && isa<Constant>(Op0)) {
1345 Constant *C1 = cast<Constant>(Op0), *C2;
1348 bool CreateDiv =
true;
1363 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1364 : BinaryOperator::CreateFMul(X, Fold);
1371 if (AllowReassociate) {
1373 Value *NewInst =
nullptr;
1379 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
1380 NewInst = Builder.CreateFMul(Y, Op1);
1381 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1383 Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1384 RI->setFastMathFlags(Flags);
1386 SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1391 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
1392 NewInst = Builder.CreateFMul(Op0, Y);
1393 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1395 Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1396 RI->setFastMathFlags(Flags);
1398 SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1437 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1440 if (isa<Constant>(Op1)) {
1441 if (
Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1445 }
else if (
auto *PN = dyn_cast<PHINode>(Op0I)) {
1446 using namespace llvm::PatternMatch;
1447 const APInt *Op1Int;
1448 if (
match(Op1,
m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
1450 !Op1Int->isMinSignedValue())) {
1460 if (SimplifyDemandedInstructionBits(I))
1471 if (
Value *V = SimplifyVectorOp(I))
1472 return replaceInstUsesWith(I, V);
1475 return replaceInstUsesWith(I, V);
1477 if (
Instruction *common = commonIRemTransforms(I))
1481 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1483 return new ZExtInst(Builder.CreateURem(ZOp0->getOperand(0), ZOp1),
1489 Value *
Add = Builder.CreateAdd(Op1, N1);
1490 return BinaryOperator::CreateAnd(Op0, Add);
1495 Value *Cmp = Builder.CreateICmpNE(Op1, Op0);
1497 return replaceInstUsesWith(I, Ext);
1501 const APInt *DivisorC;
1503 Value *Cmp = Builder.CreateICmpULT(Op0, Op1);
1504 Value *Sub = Builder.CreateSub(Op0, Op1);
1514 if (
Value *V = SimplifyVectorOp(I))
1515 return replaceInstUsesWith(I, V);
1518 return replaceInstUsesWith(I, V);
1521 if (
Instruction *Common = commonIRemTransforms(I))
1540 return BinaryOperator::CreateURem(Op0, Op1, I.
getName());
1544 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1548 bool hasNegative =
false;
1549 bool hasMissing =
false;
1550 for (
unsigned i = 0; i != VWidth; ++i) {
1557 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
1558 if (RHS->isNegative())
1562 if (hasNegative && !hasMissing) {
1564 for (
unsigned i = 0; i != VWidth; ++i) {
1566 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
1567 if (RHS->isNegative())
1587 if (
Value *V = SimplifyVectorOp(I))
1588 return replaceInstUsesWith(I, V);
1591 SQ.getWithInstruction(&I)))
1592 return replaceInstUsesWith(I, V);
1595 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
static Instruction * foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, InstCombiner &IC)
Instruction * visitSDiv(BinaryOperator &I)
void push_back(const T &Elt)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
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.
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to have exactly one bit set when defined. ...
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...
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
DiagnosticInfoOptimizationBase::Argument NV
bool SimplifyDivRemOfSelect(BinaryOperator &I)
Try to fold a divide or remainder of a select instruction.
static Value * simplifyValueKnownNonZero(Value *V, InstCombiner &IC, Instruction &CxtI)
The specific integer value is used in a context where it is known to be non-zero. ...
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
match_zero m_Zero()
Match an arbitrary zero/null constant.
This class represents zero extension of integer types.
Instruction * visitFRem(BinaryOperator &I)
static Constant * getLogBase2Vector(ConstantDataVector *CV)
A helper routine of InstCombiner::visitMul().
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index's element.
APInt smul_ov(const APInt &RHS, bool &Overflow) const
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Value * SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
This class represents a sign extension of integer types.
fneg_match< LHS > m_FNeg(const LHS &L)
Match a floating point negate.
bool isFiniteNonZero() const
Instruction * visitFMul(BinaryOperator &I)
Instruction * visitFDiv(BinaryOperator &I)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
Instruction * visitMul(BinaryOperator &I)
static Constant * getFMul(Constant *C1, Constant *C2)
Instruction * commonIRemTransforms(BinaryOperator &I)
This function implements the transforms common to both integer remainder instructions (urem and srem)...
bool isNegative() const
Determine sign of this APInt.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
bool isNonNegative() const
Returns true if this value is known to be non-negative.
This class represents the LLVM 'select' instruction.
static Constant * getNegativeZero(Type *Ty)
Exact_match< T > m_Exact(const T &SubPattern)
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static Constant * get(ArrayRef< Constant * > V)
The core instruction combiner logic.
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
static bool isFiniteNonZeroFp(Constant *C)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Instruction * commonIDivTransforms(BinaryOperator &I)
This function implements the transforms common to both integer division instructions (udiv and sdiv)...
zlib-gnu style compression
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Instruction * visitSRem(BinaryOperator &I)
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
Value * SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
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)
APInt umul_ov(const APInt &RHS, bool &Overflow) const
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power of 2.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
void takeName(Value *V)
Transfer the name from V to this value.
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
APInt ushl_ov(const APInt &Amt, bool &Overflow) const
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
static Constant * getFDiv(Constant *C1, Constant *C2)
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value.
Instruction * visitUDiv(BinaryOperator &I)
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...
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
bool isExact() const
Determine whether the exact flag is set.
The instances of the Type class are immutable: once they are created, they are never changed...
Value * SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
bool isVectorTy() const
True if this is an instance of VectorType.
This is an important base class in LLVM.
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if 'V & Mask' is known to be zero.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
ConstantFP - Floating Point Values [float, double].
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Value * foldFMulConst(Instruction *FMulOrDiv, Constant *C, Instruction *InsertBefore)
foldFMulConst() is a helper routine of InstCombiner::visitFMul().
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
Value * SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FDiv, fold the result or return null.
unsigned getBitWidth() const
Return the number of bits in the APInt.
Value * getOperand(unsigned i) const
self_iterator getIterator()
static Instruction * foldUDivNegCst(Value *Op0, Value *Op1, const BinaryOperator &I, InstCombiner &IC)
static bool isNormalFp(Constant *C)
static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, SmallVectorImpl< UDivFoldAction > &Actions, unsigned Depth=0)
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
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...
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Value * SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FRem, fold the result or return null.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static bool isFMulOrFDivWithConstant(Value *V)
Helper function of InstCombiner::visitFMul(BinaryOperator().
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
BinaryOps getOpcode() const
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Iterator for intrusive lists based on ilist_node.
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.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
double Log2(double Value)
Return the log base 2 of the specified value.
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.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
static Instruction * CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor, bool AllowReciprocal)
CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special FP value and: 1) 1/C is e...
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
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.
int32_t exactLogBase2() const
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 Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
void setOperand(unsigned i, Value *Val)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class for arbitrary precision integers.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static Instruction * foldUDivPow2Cst(Value *Op0, Value *Op1, const BinaryOperator &I, InstCombiner &IC)
unsigned getVectorNumElements() const
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool isMinValue() const
Determine if this is the smallest unsigned value.
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FMul, fold the result or return null.
llvm_unreachable("Unknown RegBank!")
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
static std::vector< std::string > Flags
bool isAllOnesValue() const
Determine if all bits are set.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Instruction * visitURem(BinaryOperator &I)
match_one m_One()
Match an integer 1 or a vector with all elements equal to 1.
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, bool IsSigned)
True if C2 is a multiple of C1. Quotient contains C2/C1.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
void setArgOperand(unsigned i, Value *v)
unsigned getNumElements() const
Return the number of elements in the array or vector.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Value * SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
match_all_ones m_AllOnes()
Match an integer or vector with all bits set to true.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
LLVM Value Representation.
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
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.
Convenience struct for specifying and reasoning about fast-math flags.
bool getExactInverse(APFloat *inv) const
bool isNullValue() const
Determine if all bits are clear.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
APInt abs() const
Get the absolute value;.
static Value * dyn_castZExtVal(Value *V, Type *Ty)
dyn_castZExtVal - Checks if V is a zext or constant that can be truncated to Ty without losing bits...
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2)
Detect pattern log2(Y * 0.5) with corresponding fast math flags.
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
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.