20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
35 bool MadeChange =
false;
39 Value *
A =
nullptr, *B =
nullptr, *
One =
nullptr;
49 if (
I->isLogicalShift() &&
60 if (
I->getOpcode() == Instruction::LShr && !
I->isExact()) {
65 if (
I->getOpcode() == Instruction::Shl && !
I->hasNoUnsignedWrap()) {
66 I->setHasNoUnsignedWrap();
75 return MadeChange ? V :
nullptr;
85 Product = C1.
smul_ov(C2, Overflow);
87 Product = C1.
umul_ov(C2, Overflow);
96 "Inconsistent width of constants!");
104 return Remainder.isMinValue();
128 bool InstCombiner::WillNotOverflowSignedMul(
Value *LHS,
Value *RHS,
145 if (SignBits > BitWidth + 1)
153 if (SignBits == BitWidth + 1) {
158 bool LHSNonNegative, LHSNegative;
159 bool RHSNonNegative, RHSNegative;
162 if (LHSNonNegative || RHSNonNegative)
169 bool Changed = SimplifyAssociativeOrCommutative(I);
172 if (
Value *V = SimplifyVectorOp(I))
173 return ReplaceInstUsesWith(I, V);
176 return ReplaceInstUsesWith(I, V);
178 if (
Value *V = SimplifyUsingDistributiveLaws(I))
179 return ReplaceInstUsesWith(I, V);
202 BO->setHasNoUnsignedWrap();
205 BO->setHasNoSignedWrap();
236 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
241 const APInt & Val = CI->getValue();
244 Value *
X =
nullptr, *
Y =
nullptr;
247 Value *Sub =
nullptr;
249 Sub = Builder->CreateSub(X,
Y,
"suba");
251 Sub = Builder->CreateSub(Builder->CreateNeg(C1),
Y,
"subc");
262 if (isa<Constant>(Op1)) {
268 if (isa<PHINode>(Op0))
277 Value *Mul = Builder->CreateMul(C1, Op1);
286 if (
Value *Op0v = dyn_castNegVal(Op0)) {
287 if (
Value *Op1v = dyn_castNegVal(Op1)) {
308 Value *Neg = dyn_castNegVal(Op1C);
317 if (SDiv->isExact()) {
319 return ReplaceInstUsesWith(I, Op0BO);
324 if (BO->
getOpcode() == Instruction::UDiv)
325 Rem = Builder->CreateURem(Op0BO, Op1BO);
327 Rem = Builder->CreateSRem(Op0BO, Op1BO);
331 return BinaryOperator::CreateSub(Op0BO, Rem);
332 return BinaryOperator::CreateSub(Rem, Op0BO);
338 return BinaryOperator::CreateAnd(Op0, Op1);
347 BO = BinaryOperator::CreateShl(Op1, Y);
348 ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
350 BO = BinaryOperator::CreateShl(Op0, Y);
351 ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
369 Value *BoolCast =
nullptr, *OtherOp =
nullptr;
371 BoolCast = Op0, OtherOp = Op1;
373 BoolCast = Op1, OtherOp = Op0;
378 return BinaryOperator::CreateAnd(V, OtherOp);
394 return Changed ? &I :
nullptr;
436 return isa<ConstantFP>(C) &&
459 if (!I || (I->
getOpcode() != Instruction::FMul &&
492 if (FMulOrDiv->
getOpcode() == Instruction::FMul) {
495 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
503 R = BinaryOperator::CreateFDiv(F, Opnd1);
509 R = BinaryOperator::CreateFMul(Opnd0, F);
514 R = BinaryOperator::CreateFDiv(Opnd0, F);
520 R->setHasUnsafeAlgebra(
true);
521 InsertNewInstWith(R, *InsertBefore);
528 bool Changed = SimplifyAssociativeOrCommutative(I);
531 if (
Value *V = SimplifyVectorOp(I))
532 return ReplaceInstUsesWith(I, V);
534 if (isa<Constant>(Op0))
539 return ReplaceInstUsesWith(I, V);
544 if (isa<Constant>(Op1)) {
550 if (isa<PHINode>(Op0))
557 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
558 RI->copyFastMathFlags(&I);
569 if (
Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
570 return ReplaceInstUsesWith(I, V);
575 (FAddSub->
getOpcode() == Instruction::FAdd ||
576 FAddSub->
getOpcode() == Instruction::FSub)) {
591 foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
594 if (Swap && FAddSub->
getOpcode() == Instruction::FSub)
598 ? BinaryOperator::CreateFAdd(M0, M1)
599 : BinaryOperator::CreateFSub(M0, M1);
609 if (AllowReassociate && (Op0 == Op1))
611 if (II->getIntrinsicID() == Intrinsic::sqrt)
612 return ReplaceInstUsesWith(I, II->
getOperand(0));
616 if (AllowReassociate) {
617 Value *OpX =
nullptr;
618 Value *OpY =
nullptr;
634 Value *FMulVal = Builder->CreateFMul(OpX, Log2);
635 Value *FSub = Builder->CreateFSub(FMulVal, OpX);
637 return ReplaceInstUsesWith(I, FSub);
644 for (
int i = 0; i < 2; i++) {
650 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
651 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
655 Value *FMul = Builder->CreateFMul(N0, N1);
657 return ReplaceInstUsesWith(I, FMul);
662 Value *
T = Builder->CreateFMul(N0, Opnd1);
663 Value *Neg = Builder->CreateFNeg(T);
665 return ReplaceInstUsesWith(I, Neg);
677 if (AllowReassociate) {
678 Value *Opnd0_0, *Opnd0_1;
682 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
684 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
690 Value *
T = Builder->CreateFMul(Opnd1, Opnd1);
692 Value *R = Builder->CreateFMul(T, Y);
694 return ReplaceInstUsesWith(I, R);
699 if (!isa<Constant>(Op1))
705 return Changed ? &I :
nullptr;
714 int NonNullOperand = -1;
716 if (
ST->isNullValue())
720 if (
ST->isNullValue())
723 if (NonNullOperand == -1)
745 while (BBI != BBFront) {
749 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
758 }
else if (*I == SelectCond) {
759 *I = Builder->getInt1(NonNullOperand == 1);
767 if (&*BBI == SelectCond)
768 SelectCond =
nullptr;
771 if (!SelectCond && !SI)
794 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
797 if (
Instruction *LHS = dyn_cast<Instruction>(Op0)) {
802 bool IsSigned = I.
getOpcode() == Instruction::SDiv;
818 if (
IsMultiple(*C2, *C1, Quotient, IsSigned)) {
826 if (
IsMultiple(*C1, *C2, Quotient, IsSigned)) {
831 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
833 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
846 if (
IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
854 if (
IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
859 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
861 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
870 if (isa<PHINode>(Op0))
879 bool isSigned = I.
getOpcode() == Instruction::SDiv;
884 Value *Inc = Builder->CreateAdd(Op1,
One);
885 Value *Cmp = Builder->CreateICmpULT(
897 if (SimplifyDemandedInstructionBits(I))
901 Value *
X =
nullptr, *Z =
nullptr;
903 bool isSigned = I.
getOpcode() == Instruction::SDiv;
915 if (
ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
916 if (Z->getSrcTy() == Ty)
917 return Z->getOperand(0);
918 }
else if (
ConstantInt *C = dyn_cast<ConstantInt>(V)) {
919 if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->
getBitWidth())
932 struct UDivFoldAction {
933 FoldUDivOperandCb FoldAction;
937 Value *OperandToFold;
946 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand)
947 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
948 UDivFoldAction(FoldUDivOperandCb FA,
Value *InputOperand,
size_t SLHS)
949 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
956 const APInt &C = cast<Constant>(Op1)->getUniqueInteger();
977 if (isa<ZExtInst>(ShiftLeft))
978 ShiftLeft = cast<Instruction>(ShiftLeft->
getOperand(0));
981 cast<Constant>(ShiftLeft->
getOperand(0))->getUniqueInteger();
985 if (
ZExtInst *Z = dyn_cast<ZExtInst>(Op1))
999 unsigned Depth = 0) {
1004 return Actions.
size();
1009 if (C->getValue().isNegative()) {
1011 return Actions.
size();
1018 return Actions.
size();
1029 Actions.push_back(UDivFoldAction(
nullptr, Op1, LHSIdx - 1));
1030 return Actions.size();
1039 if (
Value *V = SimplifyVectorOp(I))
1040 return ReplaceInstUsesWith(I, V);
1043 return ReplaceInstUsesWith(I, V);
1046 if (
Instruction *Common = commonIDivTransforms(I))
1052 const APInt *C1, *C2;
1069 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1072 Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1,
"div", I.
isExact()),
1078 for (
unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
1079 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
1080 Value *ActionOp1 = UDivActions[i].OperandToFold;
1083 Inst = Action(Op0, ActionOp1, I, *
this);
1088 size_t SelectRHSIdx = i - 1;
1089 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
1090 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
1091 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
1093 SelectLHS, SelectRHS);
1101 UDivActions[i].FoldResult = Inst;
1112 if (
Value *V = SimplifyVectorOp(I))
1113 return ReplaceInstUsesWith(I, V);
1116 return ReplaceInstUsesWith(I, V);
1119 if (
Instruction *Common = commonIDivTransforms(I))
1126 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1128 if (I.
isExact() && RHS->getValue().isNonNegative() &&
1129 RHS->getValue().isPowerOf2()) {
1131 RHS->getValue().exactLogBase2());
1132 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.
getName());
1136 if (
Constant *RHS = dyn_cast<Constant>(Op1)) {
1138 if (RHS->isMinSignedValue())
1157 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1167 auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.
getName());
1185 bool AllowReciprocal) {
1186 if (!isa<ConstantFP>(Divisor))
1189 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
1196 Cvt = !Reciprocal.isDenormal();
1204 return BinaryOperator::CreateFMul(Dividend, R);
1210 if (
Value *V = SimplifyVectorOp(I))
1211 return ReplaceInstUsesWith(I, V);
1215 return ReplaceInstUsesWith(I, V);
1217 if (isa<Constant>(Op0))
1225 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
1230 if (AllowReassociate) {
1241 Res = BinaryOperator::CreateFMul(X, C);
1249 Res = BinaryOperator::CreateFDiv(X, C);
1261 T->copyFastMathFlags(&I);
1268 if (AllowReassociate && isa<Constant>(Op0)) {
1269 Constant *C1 = cast<Constant>(Op0), *C2;
1272 bool CreateDiv =
true;
1287 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1288 : BinaryOperator::CreateFMul(X, Fold);
1295 if (AllowReassociate) {
1297 Value *NewInst =
nullptr;
1303 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
1304 NewInst = Builder->CreateFMul(Y, Op1);
1305 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1307 Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1308 RI->setFastMathFlags(Flags);
1310 SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1315 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
1316 NewInst = Builder->CreateFMul(Op0, Y);
1317 if (
Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1319 Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1320 RI->setFastMathFlags(Flags);
1322 SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1351 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1354 if (isa<Constant>(Op1)) {
1355 if (
Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1359 }
else if (isa<PHINode>(Op0I)) {
1365 if (SimplifyDemandedInstructionBits(I))
1376 if (
Value *V = SimplifyVectorOp(I))
1377 return ReplaceInstUsesWith(I, V);
1380 return ReplaceInstUsesWith(I, V);
1382 if (
Instruction *common = commonIRemTransforms(I))
1386 if (
ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1388 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
1394 Value *Add = Builder->CreateAdd(Op1, N1);
1395 return BinaryOperator::CreateAnd(Op0, Add);
1400 Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
1402 return ReplaceInstUsesWith(I, Ext);
1411 if (
Value *V = SimplifyVectorOp(I))
1412 return ReplaceInstUsesWith(I, V);
1415 return ReplaceInstUsesWith(I, V);
1418 if (
Instruction *Common = commonIRemTransforms(I))
1438 return BinaryOperator::CreateURem(Op0, Op1, I.
getName());
1443 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1447 bool hasNegative =
false;
1448 bool hasMissing =
false;
1449 for (
unsigned i = 0; i != VWidth; ++i) {
1456 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
1457 if (RHS->isNegative())
1461 if (hasNegative && !hasMissing) {
1463 for (
unsigned i = 0; i != VWidth; ++i) {
1465 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
1466 if (RHS->isNegative())
1486 if (
Value *V = SimplifyVectorOp(I))
1487 return ReplaceInstUsesWith(I, V);
1491 return ReplaceInstUsesWith(I, V);
1494 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).
ConstantDataVector - A vector constant whose element type is a simple 1/2/4/8-byte integer or float/d...
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.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
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...
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
bool SimplifyDivRemOfSelect(BinaryOperator &I)
SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select instruction.
static Value * simplifyValueKnownNonZero(Value *V, InstCombiner &IC, Instruction &CxtI)
simplifyValueKnownNonZero - The specific integer value is used in a context where it is known to be n...
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Intrinsic::ID getIntrinsicID() const
getIntrinsicID - 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)
Constant * getElementAsConstant(unsigned i) const
getElementAsConstant - 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)
SimplifySDivInst - Given operands for an SDiv, see if we can fold the result.
const DataLayout & getDataLayout() const
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
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...
bool isFiniteNonZero() const
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Instruction * visitFMul(BinaryOperator &I)
Instruction * visitFDiv(BinaryOperator &I)
static Constant * getNullValue(Type *Ty)
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)
SelectInst - This class represents the LLVM 'select' instruction.
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.
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
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.
bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
static bool isFiniteNonZeroFp(Constant *C)
AssumptionCache * getAssumptionCache() const
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)
Instruction * visitSRem(BinaryOperator &I)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
isKnownToBeAPowerOfTwo - Return true if the given value is known to have exactly one bit set when def...
DominatorTree * getDominatorTree() const
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
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, see if we can fold the result.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power of 2.
void takeName(Value *V)
Transfer the name from V to this value.
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.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
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...
The instances of the Type class are immutable: once they are created, they are never changed...
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
This is an important base class in LLVM.
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
getDebugLoc - 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().
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
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
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)
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
ComputeSignBit - Determine whether the sign bit is known to be zero or one.
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Value * SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
SimplifySRemInst - Given operands for an SRem, see if we can fold the result.
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 hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
static bool isFMulOrFDivWithConstant(Value *V)
Helper function of InstCombiner::visitFMul(BinaryOperator().
BinaryOps getOpcode() const
This is the shared class of boolean and integer constants.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
unsigned getVectorNumElements() const
unsigned getScalarSizeInBits() const LLVM_READONLY
getScalarSizeInBits - If this is a vector type, return the getPrimitiveSizeInBits value for the eleme...
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...
unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
ComputeNumSignBits - Return the number of times the sign bit of the register is replicated into the o...
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.
static bool MultiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, bool IsSigned)
MultiplyOverflows - True if the multiply can not be expressed in an int this size.
static Constant * get(Type *Ty, double V)
get() - This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in the specified type.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
bool isExact() const
Determine whether the exact flag is set.
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
isIntegerTy - 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)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Instruction * visitURem(BinaryOperator &I)
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.
bool hasOneUse() const
Return true if there is exactly one user of this value.
void setArgOperand(unsigned i, Value *v)
unsigned getNumElements() const
getNumElements - 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)
SimplifyURemInst - Given operands for a URem, see if we can fold the result.
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)
SimplifyMulInst - Given operands for a Mul, see if we can fold the result.
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
SimplifyUDivInst - Given operands for a UDiv, see if we can fold the result.
const APFloat & getValueAPF() const
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)
SimplifyFRemInst - Given operands for an FRem, see if we can fold the result.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - 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.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
getOpcode() 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.
Convenience struct for specifying and reasoning about fast-math flags.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
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)
bool getExactInverse(APFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
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
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
IntrinsicInst - A useful 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)
SimplifyFDivInst - Given operands for an FDiv, see if we can fold the result.