48 #define DEBUG_TYPE "instcombine" 103 if (isa<FPMathOperator>(BO))
126 const APInt *SelTC, *SelFC;
138 bool CreateAnd =
false;
180 bool ExtraBitInTC = TC.
ugt(FC);
205 unsigned ValZeros = ValC.
logBase2();
206 unsigned AndZeros = AndMask.
logBase2();
214 if (ValZeros > AndZeros) {
216 V = Builder.
CreateShl(V, ValZeros - AndZeros);
217 }
else if (ValZeros < AndZeros) {
218 V = Builder.
CreateLShr(V, AndZeros - ValZeros);
247 case Instruction::Mul:
248 case Instruction::And:
249 case Instruction::Or:
250 case Instruction::Xor:
252 case Instruction::Sub:
253 case Instruction::Shl:
254 case Instruction::LShr:
255 case Instruction::AShr:
268 case Instruction::Sub:
269 case Instruction::Or:
270 case Instruction::Xor:
271 case Instruction::Shl:
272 case Instruction::LShr:
273 case Instruction::AShr:
275 case Instruction::And:
277 case Instruction::Mul:
318 if (TI->
getOpcode() != Instruction::BitCast &&
341 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
346 Value *MatchOp, *OtherOpT, *OtherOpF;
352 MatchIsOpZero =
true;
357 MatchIsOpZero =
false;
364 MatchIsOpZero =
true;
369 MatchIsOpZero =
true;
385 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
386 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
387 if (
auto *BO = dyn_cast<BinaryOperator>(TI)) {
393 if (
auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
394 auto *FGEP = cast<GetElementPtrInst>(FI);
395 Type *ElementType = TGEP->getResultElementType();
396 return TGEP->isInBounds() && FGEP->isInBounds()
417 if (
auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) {
418 if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) {
420 unsigned OpToFold = 0;
421 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
423 }
else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
429 Value *OOp = TVI->getOperand(2-OpToFold);
434 if (!isa<Constant>(OOp) || (OOpIsAPInt &&
isSelect01(CI, *OOpC))) {
448 if (
auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) {
449 if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) {
451 unsigned OpToFold = 0;
452 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
454 }
else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
460 Value *OOp = FVI->getOperand(2-OpToFold);
465 if (!isa<Constant>(OOp) || (OOpIsAPInt &&
isSelect01(CI, *OOpC))) {
520 return new ZExtInst(ICmpNeZero, SelType);
582 bool OrOnTrueVal =
false;
587 if (!OrOnFalseVal && !OrOnTrueVal)
590 Value *
Y = OrOnFalseVal ? TrueVal : FalseVal;
594 bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal);
595 bool NeedShift = C1Log != C2Log;
600 Value *Or = OrOnFalseVal ? FalseVal : TrueVal;
601 if ((NeedShift + NeedXor + NeedZExtTrunc) >
614 }
else if (C1Log > C2Log) {
634 const Value *TrueVal,
635 const Value *FalseVal,
658 "Unexpected isUnsigned predicate!");
661 bool IsNegative =
false;
701 Value *Count = FalseVal;
702 Value *ValueOnZero = TrueVal;
772 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
773 (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
788 AdjustedRHS = SextRHS;
790 SextRHS == TrueVal) {
792 AdjustedRHS = SextRHS;
801 AdjustedRHS = ZextRHS;
803 ZextRHS == TrueVal) {
805 AdjustedRHS = ZextRHS;
817 CmpRHS = AdjustedRHS;
866 "Unexpected results from matchSelectPattern");
896 "Unexpected result from matchSelectPattern");
903 bool CmpCanonicalized = !CmpUsesNegatedOp &&
909 if (CmpCanonicalized && RHSCanonicalized)
918 if (!CmpCanonicalized) {
921 if (CmpUsesNegatedOp)
926 if (!RHSCanonicalized) {
942 assert(FVal == LHS &&
"Unexpected results from matchSelectPattern");
946 assert(TVal == LHS &&
"Unexpected results from matchSelectPattern");
962 if (
Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder))
965 if (
Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder))
968 bool Changed = adjustMinMax(SI, *ICI);
971 return replaceInstUsesWith(SI, V);
977 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
998 bool IsBitTest =
false;
1006 Y = &MinSignedValue;
1008 TrueWhenUnset =
false;
1011 Y = &MinSignedValue;
1013 TrueWhenUnset =
true;
1018 if (TrueWhenUnset && TrueVal == X &&
1022 else if (!TrueWhenUnset && FalseVal == X &&
1026 else if (TrueWhenUnset && FalseVal == X &&
1030 else if (!TrueWhenUnset && TrueVal == X &&
1035 return replaceInstUsesWith(SI, V);
1044 return replaceInstUsesWith(SI, V);
1046 if (
Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1047 return replaceInstUsesWith(SI, V);
1050 return replaceInstUsesWith(SI, V);
1052 return Changed ? &
SI :
nullptr;
1064 static bool canSelectOperandBeMappingIntoPredBlock(
const Value *V,
1069 if (!I)
return true;
1075 if (
const PHINode *VP = dyn_cast<PHINode>(I))
1076 if (VP->getParent() == CondPHI->
getParent())
1100 if (C == A || C == B) {
1105 return replaceInstUsesWith(Outer, Inner);
1114 return replaceInstUsesWith(Outer, C);
1118 const APInt *CB, *CC;
1127 return replaceInstUsesWith(Outer, Inner);
1145 return replaceInstUsesWith(Outer, Inner);
1156 return replaceInstUsesWith(Outer, NewSI);
1159 auto IsFreeOrProfitableToInvert =
1160 [&](
Value *V,
Value *&NotV,
bool &ElidesXor) {
1176 Value *NotA, *NotB, *NotC;
1177 bool ElidesXor =
false;
1189 IsFreeOrProfitableToInvert(A, NotA, ElidesXor) &&
1190 IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&
1191 IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) {
1203 return replaceInstUsesWith(Outer, NewOuter);
1218 if (!TI || !FI || !TI->
hasOneUse() || !FI->hasOneUse())
1222 if ((TI->
getOpcode() == Instruction::Sub &&
1224 (TI->
getOpcode() == Instruction::FSub &&
1225 FI->getOpcode() == Instruction::FAdd)) {
1228 }
else if ((FI->getOpcode() == Instruction::Sub &&
1230 (FI->getOpcode() == Instruction::FSub &&
1231 TI->
getOpcode() == Instruction::FAdd)) {
1237 Value *OtherAddOp =
nullptr;
1238 if (SubOp->getOperand(0) == AddOp->
getOperand(0)) {
1240 }
else if (SubOp->getOperand(0) == AddOp->
getOperand(1)) {
1249 NegVal = Builder.
CreateFNeg(SubOp->getOperand(1));
1250 if (
Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
1252 Flags &= SubOp->getFastMathFlags();
1253 NegInst->setFastMathFlags(Flags);
1256 NegVal = Builder.
CreateNeg(SubOp->getOperand(1));
1259 Value *NewTrueOp = OtherAddOp;
1260 Value *NewFalseOp = NegVal;
1268 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
1271 Flags &= SubOp->getFastMathFlags();
1293 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
1312 Value *TruncCVal = cast<Value>(TruncC);
1354 for (
unsigned i = 0; i != NumElts; ++i) {
1365 }
else if (isa<UndefValue>(Elt)) {
1396 if (TVal == A || TVal == B || FVal == A || FVal == B)
1413 if (TSrc == C && FSrc == D) {
1417 }
else if (TSrc == D && FSrc == C) {
1459 if (Extract->getIndices()[0] !=
I)
1474 auto *CmpXchg = isExtractFromCmpXchg(SI.
getCondition(), 1);
1491 if (
X == CmpXchg &&
X->getCompareOperand() == SI.
getTrueValue()) {
1518 Value *MinMaxOp =
nullptr;
1519 Value *ThirdOp =
nullptr;
1523 if (D == A || C == A) {
1528 }
else if (D == B || C == B) {
1536 if (D == A || D == B) {
1541 }
else if (C == A || C == B) {
1548 if (!MinMaxOp || !ThirdOp)
1572 auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->
getOpcode();
1573 auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->
getOpcode();
1574 if (ShiftOpcode0 == ShiftOpcode1)
1603 bool IsFshl = (ShAmt == SA0 && ShiftOpcode0 == BinaryOperator::Shl) ||
1604 (ShAmt == SA1 && ShiftOpcode1 == BinaryOperator::Shl);
1621 if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) {
1633 SQ.getWithInstruction(&SI)))
1634 return replaceInstUsesWith(SI, V);
1647 CmpInst *Cond = cast<CmpInst>(CondVal);
1660 return BinaryOperator::CreateOr(CondVal, FalseVal);
1665 return BinaryOperator::CreateAnd(NotCond, FalseVal);
1669 return BinaryOperator::CreateAnd(CondVal, TrueVal);
1674 return BinaryOperator::CreateOr(NotCond, TrueVal);
1679 if (CondVal == TrueVal)
1680 return BinaryOperator::CreateOr(CondVal, FalseVal);
1681 if (CondVal == FalseVal)
1682 return BinaryOperator::CreateAnd(CondVal, TrueVal);
1687 return BinaryOperator::CreateAnd(TrueVal, FalseVal);
1689 return BinaryOperator::CreateOr(TrueVal, FalseVal);
1702 return new ZExtInst(CondVal, SelType);
1706 return new SExtInst(CondVal, SelType);
1711 return new ZExtInst(NotCond, SelType);
1717 return new SExtInst(NotCond, SelType);
1722 if (
FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
1723 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
1741 }
else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
1765 Value *
X = FCI->getOperand(0);
1775 return replaceInstUsesWith(SI, Fabs);
1782 if (FCI->hasNoSignedZeros() &&
1788 return replaceInstUsesWith(SI, Fabs);
1794 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
1795 if (
Instruction *Result = foldSelectInstWithICmp(SI, ICI))
1804 if (TI && FI && TI->
getOpcode() == FI->getOpcode())
1813 if (
Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
1823 if (
Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
1824 RHS2, SI, SPF, RHS))
1827 if (
Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
1828 RHS2, SI, SPF, LHS))
1839 bool IsCastNeeded = LHS->
getType() != SelType;
1840 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
1841 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
1844 ((CmpLHS != LHS && CmpLHS != RHS) ||
1845 (CmpRHS != LHS && CmpRHS != RHS)))) {
1853 auto FMF = cast<FPMathOperator>(SI.
getCondition())->getFastMathFlags();
1860 return replaceInstUsesWith(SI, NewSI);
1863 return replaceInstUsesWith(SI, NewCast);
1884 cast<SelectInst>(NewMinMax)->swapProfMetadata();
1898 if (
Instruction *
I = factorizeMinMaxTree(SPF, LHS, RHS, Builder))
1906 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
1907 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
1911 if (
SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
1912 if (TrueSI->getCondition()->getType() == CondVal->
getType()) {
1914 if (TrueSI->getCondition() == CondVal) {
1923 if (TrueSI->getFalseValue() == FalseVal && TrueSI->
hasOneUse()) {
1931 if (
SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
1932 if (FalseSI->getCondition()->getType() == CondVal->
getType()) {
1934 if (FalseSI->getCondition() == CondVal) {
1941 if (FalseSI->getTrueValue() == TrueVal && FalseSI->
hasOneUse()) {
1942 Value *Or = Builder.
CreateOr(CondVal, FalseSI->getCondition());
1952 switch (BO->getOpcode()) {
1955 case Instruction::SRem:
1956 case Instruction::URem:
1957 case Instruction::SDiv:
1958 case Instruction::UDiv:
1968 canMergeSelectThroughBinop(TrueBO)) {
1969 if (
auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->
getOperand(0))) {
1970 if (TrueBOSI->getCondition() == CondVal) {
1971 TrueBO->
setOperand(0, TrueBOSI->getTrueValue());
1972 Worklist.Add(TrueBO);
1976 if (
auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->
getOperand(1))) {
1977 if (TrueBOSI->getCondition() == CondVal) {
1978 TrueBO->
setOperand(1, TrueBOSI->getTrueValue());
1979 Worklist.Add(TrueBO);
1988 canMergeSelectThroughBinop(FalseBO)) {
1989 if (
auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->
getOperand(0))) {
1990 if (FalseBOSI->getCondition() == CondVal) {
1991 FalseBO->
setOperand(0, FalseBOSI->getFalseValue());
1992 Worklist.Add(FalseBO);
1996 if (
auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->
getOperand(1))) {
1997 if (FalseBOSI->getCondition() == CondVal) {
1998 FalseBO->
setOperand(1, FalseBOSI->getFalseValue());
1999 Worklist.Add(FalseBO);
2014 if (
VectorType *VecTy = dyn_cast<VectorType>(SelType)) {
2015 unsigned VWidth = VecTy->getNumElements();
2016 APInt UndefElts(VWidth, 0);
2018 if (
Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
2020 return replaceInstUsesWith(SI, V);
2034 return replaceInstUsesWith(SI, TrueVal);
2036 return replaceInstUsesWith(SI, FalseVal);
2039 if (
Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
bool isFPPredicate() const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
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, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
static bool isSelect01(const APInt &C1I, const APInt &C2I)
static ConstantInt * getFalse(LLVMContext &Context)
static Instruction * foldSelectBinOpIdentity(SelectInst &Sel, const TargetLibraryInfo &TLI)
Replace a select operand based on an equality comparison with the identity constant of a binop...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
This class is the base class for the comparison instructions.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
static bool IsFreeToInvert(Value *V, bool WillInvertAllUses)
Return true if the specified value is free to invert (apply ~ to).
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
DiagnosticInfoOptimizationBase::Argument NV
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return an i1 value testing if Arg is not null.
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
This class represents lattice values for constants.
BinaryOps getOpcode() const
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This class represents zero extension of integer types.
void push_back(const T &Elt)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool slt(const APInt &RHS) const
Signed less than comparison.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
const Value * getTrueValue() const
0 1 0 0 True if ordered and less than
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
static unsigned getSelectFoldableOperands(BinaryOperator *I)
We want to turn code that looks like this: C = or A, B D = select cond, C, A into: C = select cond...
1 1 1 0 True if unordered or not equal
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
bool sgt(const APInt &RHS) const
Signed greather than comparison.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
void setArgOperand(unsigned i, Value *v)
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
This class represents a sign extension of integer types.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
bool isVectorTy() const
True if this is an instance of VectorType.
void reserve(size_type N)
bool sle(const APInt &RHS) const
Signed less or equal comparison.
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
bool Ordered
Only applicable if Flavor is SPF_FMINNUM or SPF_FMAXNUM.
unsigned getBitWidth() const
Return the number of bits in the APInt.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Value * CreateNot(Value *V, const Twine &Name="")
Value * getArgOperand(unsigned i) const
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
static Value * foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp, InstCombiner::BuilderTy &Builder)
This folds: select (icmp eq (and X, C1)), TC, FC iff C1 is a power 2 and the difference between TC an...
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
This class represents the LLVM 'select' instruction.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
0 1 0 1 True if ordered and less than or equal
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
This file implements a class to represent arbitrary precision integral constant values and operations...
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Type * getType() const
All values are typed, get the type of this value.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
This instruction compares its operands according to the predicate given to the constructor.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
static Instruction * foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1) into: zext (icmp ne i32 (a...
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.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Value * getOperand(unsigned i) const
bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
OneUse_match< T > m_OneUse(const T &SubPattern)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
bool isAllOnesValue() const
Determine if all bits are set.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false)
Return true if the two given values are negation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
bool ult(const APInt &RHS) const
Unsigned less than comparison.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
bool isOneValue() const
Determine if this is a value of 1.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
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.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Class to represent integer types.
const Value * getCondition() const
C setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh))
void swapProfMetadata()
If the instruction has "branch_weights" MD_prof metadata and the MDNode has three operands (including...
#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 Value * createMinMax(InstCombiner::BuilderTy &Builder, SelectPatternFlavor SPF, Value *A, Value *B)
0 0 1 0 True if ordered and greater than
static Value * foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal, Value *FalseVal, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) into: (or (shl (and X...
unsigned getNumOperands() const
SelectPatternFlavor Flavor
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
SelectPatternFlavor
Specific patterns of select instructions we can match.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Provides information about what library functions are available for the current target.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
static ConstantInt * getTrue(LLVMContext &Context)
bool isCommutative() const
Return true if the instruction is commutative:
Instruction * visitSelectInst(SelectInst &SI)
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
void setOperand(unsigned i, Value *Val)
unsigned logBase2() const
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned getVectorNumElements() const
bool isIntPredicate() const
Class to represent vector types.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
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.
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
iterator_range< user_iterator > users()
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
const Value * getFalseValue() const
void setCondition(Value *V)
static APInt getSelectFoldableConstant(BinaryOperator *I)
For the same transformation as the previous function, return the identity constant that goes into the...
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Predicate getPredicate() const
Return the predicate for this instruction.
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
static Value * canonicalizeSaturatedSubtract(const ICmpInst *ICI, const Value *TrueVal, const Value *FalseVal, InstCombiner::BuilderTy &Builder)
Transform patterns such as: (a > b) ? a - b : 0 into: ((a > b) ? a : b) - b) This produces a canonica...
void setTrueValue(Value *V)
static IntegerType * getInt32Ty(LLVMContext &C)
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
unsigned greater or equal
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
StringRef getName() const
Return a constant reference to the value's name.
bool isEquality() const
Return true if this predicate is either EQ or NE.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void setFalseValue(Value *V)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
bool isOneValue() const
Returns true if the value is one.
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
0 0 0 1 True if ordered and equal
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
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.
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
bool hasOneUse() const
Return true if there is exactly one user of this value.
Convenience struct for specifying and reasoning about fast-math flags.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static APInt getNullValue(unsigned numBits)
Get the '0' value.
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
0 0 1 1 True if ordered and greater than or equal
static Constant * get(ArrayRef< Constant *> V)
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
bool isNullValue() const
Determine if all bits are clear.
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)