20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
35 bool MadeChange =
false;
39 Value *
A =
nullptr, *
B =
nullptr, *One =
nullptr;
75 return MadeChange ? V :
nullptr;
84 Product = C1.
smul_ov(C2, Overflow);
86 Product = C1.
umul_ov(C2, Overflow);
95 "Inconsistent width of constants!");
111 return Remainder.isMinValue();
135 bool InstCombiner::WillNotOverflowSignedMul(
Value *LHS,
Value *RHS,
152 if (SignBits > BitWidth + 1)
160 if (SignBits == BitWidth + 1) {
165 bool LHSNonNegative, LHSNegative;
166 bool RHSNonNegative, RHSNegative;
169 if (LHSNonNegative || RHSNonNegative)
176 bool Changed = SimplifyAssociativeOrCommutative(I);
179 if (
Value *V = SimplifyVectorOp(I))
180 return replaceInstUsesWith(I, V);
183 return replaceInstUsesWith(I, V);
185 if (
Value *V = SimplifyUsingDistributiveLaws(I))
186 return replaceInstUsesWith(I, V);
209 BO->setHasNoUnsignedWrap();
212 BO->setHasNoSignedWrap();
243 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
248 const APInt & Val = CI->getValue();
251 Value *
X =
nullptr, *
Y =
nullptr;
254 Value *Sub =
nullptr;
256 Sub = Builder->CreateSub(X,
Y,
"suba");
258 Sub = Builder->CreateSub(Builder->CreateNeg(C1),
Y,
"subc");
269 if (isa<Constant>(Op1)) {
270 if (
Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
278 Value *Mul = Builder->CreateMul(C1, Op1);
287 if (
Value *Op0v = dyn_castNegVal(Op0)) {
288 if (
Value *Op1v = dyn_castNegVal(Op1)) {
309 Value *Neg = dyn_castNegVal(Op1C);
318 if (SDiv->isExact()) {
320 return replaceInstUsesWith(I, Op0BO);
325 if (BO->
getOpcode() == Instruction::UDiv)
326 Rem = Builder->CreateURem(Op0BO, Op1BO);
328 Rem = Builder->CreateSRem(Op0BO, Op1BO);
332 return BinaryOperator::CreateSub(Op0BO, Rem);
333 return BinaryOperator::CreateSub(Rem, Op0BO);
339 return BinaryOperator::CreateAnd(Op0, Op1);
348 BO = BinaryOperator::CreateShl(Op1, Y);
349 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
351 BO = BinaryOperator::CreateShl(Op0, Y);
352 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
370 Value *BoolCast =
nullptr, *OtherOp =
nullptr;
382 return BinaryOperator::CreateAnd(V, OtherOp);
388 if (
SExtInst *Op0Conv = dyn_cast<SExtInst>(Op0)) {
390 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
391 if (Op0Conv->hasOneUse()) {
395 WillNotOverflowSignedMul(Op0Conv->getOperand(0), CI,
I)) {
398 Builder->CreateNSWMul(Op0Conv->getOperand(0), CI,
"mulconv");
405 if (
SExtInst *Op1Conv = dyn_cast<SExtInst>(Op1)) {
409 if (Op0Conv->getOperand(0)->getType() ==
410 Op1Conv->getOperand(0)->getType() &&
411 (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) &&
412 WillNotOverflowSignedMul(Op0Conv->getOperand(0),
413 Op1Conv->getOperand(0),
I)) {
415 Value *NewMul = Builder->CreateNSWMul(
416 Op0Conv->getOperand(0), Op1Conv->getOperand(0),
"mulconv");
424 if (
auto *Op0Conv = dyn_cast<ZExtInst>(Op0)) {
426 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
427 if (Op0Conv->hasOneUse()) {
435 Builder->CreateNUWMul(Op0Conv->getOperand(0), CI,
"mulconv");
442 if (
auto *Op1Conv = dyn_cast<ZExtInst>(Op1)) {
446 if (Op0Conv->getOperand(0)->getType() ==
447 Op1Conv->getOperand(0)->getType() &&
448 (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) &&
450 Op1Conv->getOperand(0),
453 Value *NewMul = Builder->CreateNUWMul(
454 Op0Conv->getOperand(0), Op1Conv->getOperand(0),
"mulconv");
472 return Changed ? &I :
nullptr;
514 return isa<ConstantFP>(
C) &&
537 if (!I || (I->
getOpcode() != Instruction::FMul &&
570 if (FMulOrDiv->
getOpcode() == Instruction::FMul) {
573 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
581 R = BinaryOperator::CreateFDiv(F, Opnd1);
587 R = BinaryOperator::CreateFMul(Opnd0, F);
592 R = BinaryOperator::CreateFDiv(Opnd0, F);
598 R->setHasUnsafeAlgebra(
true);
599 InsertNewInstWith(R, *InsertBefore);
606 bool Changed = SimplifyAssociativeOrCommutative(I);
609 if (
Value *V = SimplifyVectorOp(I))
610 return replaceInstUsesWith(I, V);
612 if (isa<Constant>(Op0))
617 return replaceInstUsesWith(I, V);
622 if (isa<Constant>(Op1)) {
623 if (
Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
629 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
630 RI->copyFastMathFlags(&I);
641 if (
Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
642 return replaceInstUsesWith(I, V);
647 (FAddSub->
getOpcode() == Instruction::FAdd ||
648 FAddSub->
getOpcode() == Instruction::FSub)) {
663 foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
666 if (Swap && FAddSub->
getOpcode() == Instruction::FSub)
670 ? BinaryOperator::CreateFAdd(M0, M1)
671 : BinaryOperator::CreateFSub(M0, M1);
683 if (AllowReassociate && II->getIntrinsicID() == Intrinsic::sqrt)
684 return replaceInstUsesWith(I, II->
getOperand(0));
687 if (II->getIntrinsicID() == Intrinsic::fabs) {
688 Instruction *FMulVal = BinaryOperator::CreateFMul(II->getOperand(0),
699 if (AllowReassociate) {
700 Value *OpX =
nullptr;
701 Value *OpY =
nullptr;
717 Value *FMulVal = Builder->CreateFMul(OpX, Log2);
718 Value *FSub = Builder->CreateFSub(FMulVal, OpX);
720 return replaceInstUsesWith(I, FSub);
727 for (
int i = 0;
i < 2;
i++) {
733 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
734 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
738 Value *FMul = Builder->CreateFMul(N0, N1);
740 return replaceInstUsesWith(I, FMul);
745 Value *
T = Builder->CreateFMul(N0, Opnd1);
746 Value *Neg = Builder->CreateFNeg(T);
748 return replaceInstUsesWith(I, Neg);
760 if (AllowReassociate) {
761 Value *Opnd0_0, *Opnd0_1;
765 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
767 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
773 Value *
T = Builder->CreateFMul(Opnd1, Opnd1);
774 Value *R = Builder->CreateFMul(T, Y);
776 return replaceInstUsesWith(I, R);
781 if (!isa<Constant>(Op1))
787 return Changed ? &I :
nullptr;
795 int NonNullOperand = -1;
797 if (
ST->isNullValue())
801 if (
ST->isNullValue())
804 if (NonNullOperand == -1)
826 while (BBI != BBFront) {
830 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
839 }
else if (*I == SelectCond) {
840 *I = Builder->getInt1(NonNullOperand == 1);
848 if (&*BBI == SelectCond)
849 SelectCond =
nullptr;
852 if (!SelectCond && !SI)
875 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
878 if (
Instruction *LHS = dyn_cast<Instruction>(Op0)) {
883 bool IsSigned = I.
getOpcode() == Instruction::SDiv;
899 if (
IsMultiple(*C2, *C1, Quotient, IsSigned)) {
907 if (
IsMultiple(*C1, *C2, Quotient, IsSigned)) {
912 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
914 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
927 if (
IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
935 if (
IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
940 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
942 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
948 if (
Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I))
953 if (
ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
955 bool isSigned = I.
getOpcode() == Instruction::SDiv;
960 Value *Inc = Builder->CreateAdd(Op1, One);
961 Value *Cmp = Builder->CreateICmpULT(
973 if (SimplifyDemandedInstructionBits(I))
977 Value *
X =
nullptr, *Z =
nullptr;
979 bool isSigned = I.
getOpcode() == Instruction::SDiv;
991 if (
ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
992 if (Z->getSrcTy() == Ty)
993 return Z->getOperand(0);
995 if (
C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->
getBitWidth())
1008 struct UDivFoldAction {
1009 FoldUDivOperandCb FoldAction;
1013 Value *OperandToFold;
1018 size_t SelectLHSIdx;
1022 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand)
1023 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
1024 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand,
size_t SLHS)
1025 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
1032 const APInt &
C = cast<Constant>(Op1)->getUniqueInteger();
1064 if (Op1 != ShiftLeft)
1078 unsigned Depth = 0) {
1083 return Actions.
size();
1088 if (
C->getValue().isNegative()) {
1090 return Actions.
size();
1097 return Actions.
size();
1108 Actions.push_back(UDivFoldAction(
nullptr, Op1, LHSIdx - 1));
1109 return Actions.size();
1118 if (
Value *V = SimplifyVectorOp(I))
1119 return replaceInstUsesWith(I, V);
1122 return replaceInstUsesWith(I, V);
1125 if (
Instruction *Common = commonIDivTransforms(I))
1131 const APInt *C1, *C2;
1148 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1151 Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1,
"div", I.
isExact()),
1157 for (
unsigned i = 0, e = UDivActions.size();
i != e; ++
i) {
1158 FoldUDivOperandCb Action = UDivActions[
i].FoldAction;
1159 Value *ActionOp1 = UDivActions[
i].OperandToFold;
1162 Inst = Action(Op0, ActionOp1, I, *
this);
1167 size_t SelectRHSIdx =
i - 1;
1168 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
1169 size_t SelectLHSIdx = UDivActions[
i].SelectLHSIdx;
1170 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
1172 SelectLHS, SelectRHS);
1180 UDivActions[
i].FoldResult = Inst;
1191 if (
Value *V = SimplifyVectorOp(I))
1192 return replaceInstUsesWith(I, V);
1195 return replaceInstUsesWith(I, V);
1198 if (
Instruction *Common = commonIDivTransforms(I))
1210 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.
getName());
1226 Value *NarrowOp = Builder->CreateSDiv(Op0Src, NarrowDivisor);
1231 if (
Constant *RHS = dyn_cast<Constant>(Op1)) {
1233 if (RHS->isMinSignedValue())
1252 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1262 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1280 bool AllowReciprocal) {
1281 if (!isa<ConstantFP>(Divisor))
1284 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
1291 Cvt = !Reciprocal.isDenormal();
1299 return BinaryOperator::CreateFMul(Dividend, R);
1305 if (
Value *V = SimplifyVectorOp(I))
1306 return replaceInstUsesWith(I, V);
1309 DL, &TLI, &DT, &AC))
1310 return replaceInstUsesWith(I, V);
1312 if (isa<Constant>(Op0))
1320 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
1325 if (AllowReassociate) {
1336 Res = BinaryOperator::CreateFMul(X, C);
1344 Res = BinaryOperator::CreateFDiv(X, C);
1356 T->copyFastMathFlags(&I);
1363 if (AllowReassociate && isa<Constant>(Op0)) {
1364 Constant *C1 = cast<Constant>(Op0), *C2;
1367 bool CreateDiv =
true;
1382 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1383 : BinaryOperator::CreateFMul(X, Fold);
1390 if (AllowReassociate) {
1392 Value *NewInst =
nullptr;
1398 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
1399 NewInst = Builder->CreateFMul(Y, Op1);
1400 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1402 Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1403 RI->setFastMathFlags(Flags);
1405 SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1410 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
1411 NewInst = Builder->CreateFMul(Op0, Y);
1412 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1414 Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1415 RI->setFastMathFlags(Flags);
1417 SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1456 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1459 if (isa<Constant>(Op1)) {
1460 if (
Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1464 }
else if (isa<PHINode>(Op0I)) {
1465 using namespace llvm::PatternMatch;
1466 const APInt *Op1Int;
1467 if (
match(Op1,
m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
1469 !Op1Int->isMinSignedValue())) {
1479 if (SimplifyDemandedInstructionBits(I))
1490 if (
Value *V = SimplifyVectorOp(I))
1491 return replaceInstUsesWith(I, V);
1494 return replaceInstUsesWith(I, V);
1496 if (
Instruction *common = commonIRemTransforms(I))
1500 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1502 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
1508 Value *
Add = Builder->CreateAdd(Op1, N1);
1509 return BinaryOperator::CreateAnd(Op0, Add);
1514 Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
1516 return replaceInstUsesWith(I, Ext);
1520 const APInt *DivisorC;
1522 Value *Cmp = Builder->CreateICmpULT(Op0, Op1);
1523 Value *Sub = Builder->CreateSub(Op0, Op1);
1533 if (
Value *V = SimplifyVectorOp(I))
1534 return replaceInstUsesWith(I, V);
1537 return replaceInstUsesWith(I, V);
1540 if (
Instruction *Common = commonIRemTransforms(I))
1560 return BinaryOperator::CreateURem(Op0, Op1, I.
getName());
1565 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1569 bool hasNegative =
false;
1570 bool hasMissing =
false;
1571 for (
unsigned i = 0;
i != VWidth; ++
i) {
1578 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
1579 if (RHS->isNegative())
1583 if (hasNegative && !hasMissing) {
1585 for (
unsigned i = 0;
i != VWidth; ++
i) {
1587 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[
i])) {
1588 if (RHS->isNegative())
1608 if (
Value *V = SimplifyVectorOp(I))
1609 return replaceInstUsesWith(I, V);
1612 DL, &TLI, &DT, &AC))
1613 return replaceInstUsesWith(I, V);
1616 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
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.
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
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.
Value * SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an SDiv, fold the result or return null.
const DataLayout & getDataLayout() const
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)
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
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.
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)
This class represents the LLVM 'select' instruction.
struct fuzzer::@269 Flags
static Constant * getNegativeZero(Type *Ty)
Exact_match< T > m_Exact(const T &SubPattern)
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)...
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.
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 * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an FMul, fold the result or return null.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
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.
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
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)
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
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)
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...
bool isVectorTy() const
True if this is an instance of VectorType.
This is an important base class in LLVM.
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].
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set to true.
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.
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed...
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")
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...
Value * SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an SRem, fold the result or return null.
DominatorTree & getDominatorTree() const
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static bool isFMulOrFDivWithConstant(Value *V)
Helper function of InstCombiner::visitFMul(BinaryOperator().
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)
Log2 - This function returns 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)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
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.
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
bool isAllOnesValue() const
Determine if all bits are set.
void ComputeSignBit(const Value *V, bool &KnownZero, bool &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine whether the sign bit is known to be zero or one.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Instruction * visitURem(BinaryOperator &I)
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
AssumptionCache & getAssumptionCache() const
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.
Value * SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a URem, fold the result or return null.
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 * SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a Mul, fold the result or return null.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a UDiv, 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())
Value * SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an FRem, fold the result or return null.
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)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
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
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 GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2)
Detect pattern log2(Y * 0.5) with corresponding fast math flags.
const fltSemantics & getSemantics() const
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.
Value * SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an FDiv, fold the result or return null.