48 #define DEBUG_TYPE "instcombine" 98 const APInt *SelTC, *SelFC;
110 bool CreateAnd =
false;
152 bool ExtraBitInTC = TC.
ugt(FC);
177 unsigned ValZeros = ValC.
logBase2();
178 unsigned AndZeros = AndMask.
logBase2();
186 if (ValZeros > AndZeros) {
188 V = Builder.
CreateShl(V, ValZeros - AndZeros);
189 }
else if (ValZeros < AndZeros) {
190 V = Builder.
CreateLShr(V, AndZeros - ValZeros);
219 case Instruction::Mul:
220 case Instruction::And:
221 case Instruction::Or:
222 case Instruction::Xor:
224 case Instruction::Sub:
225 case Instruction::Shl:
226 case Instruction::LShr:
227 case Instruction::AShr:
240 case Instruction::Sub:
241 case Instruction::Or:
242 case Instruction::Xor:
243 case Instruction::Shl:
244 case Instruction::LShr:
245 case Instruction::AShr:
247 case Instruction::And:
249 case Instruction::Mul:
290 if (TI->
getOpcode() != Instruction::BitCast &&
313 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
318 Value *MatchOp, *OtherOpT, *OtherOpF;
324 MatchIsOpZero =
true;
329 MatchIsOpZero =
false;
336 MatchIsOpZero =
true;
341 MatchIsOpZero =
true;
349 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
350 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
351 if (
auto *BO = dyn_cast<BinaryOperator>(TI)) {
354 if (
auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
355 auto *FGEP = cast<GetElementPtrInst>(FI);
356 Type *ElementType = TGEP->getResultElementType();
357 return TGEP->isInBounds() && FGEP->isInBounds()
378 if (
auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) {
379 if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) {
381 unsigned OpToFold = 0;
382 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
384 }
else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
390 Value *OOp = TVI->getOperand(2-OpToFold);
395 if (!isa<Constant>(OOp) || (OOpIsAPInt &&
isSelect01(CI, *OOpC))) {
409 if (
auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) {
410 if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) {
412 unsigned OpToFold = 0;
413 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
415 }
else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
421 Value *OOp = FVI->getOperand(2-OpToFold);
426 if (!isa<Constant>(OOp) || (OOpIsAPInt &&
isSelect01(CI, *OOpC))) {
481 return new ZExtInst(ICmpNeZero, SelType);
543 bool OrOnTrueVal =
false;
548 if (!OrOnFalseVal && !OrOnTrueVal)
551 Value *
Y = OrOnFalseVal ? TrueVal : FalseVal;
555 bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal);
556 bool NeedShift = C1Log != C2Log;
561 Value *Or = OrOnFalseVal ? FalseVal : TrueVal;
562 if ((NeedShift + NeedXor + NeedZExtTrunc) >
575 }
else if (C1Log > C2Log) {
595 const Value *TrueVal,
596 const Value *FalseVal,
619 "Unexpected isUnsigned predicate!");
622 bool IsNegative =
false;
662 Value *Count = FalseVal;
663 Value *ValueOnZero = TrueVal;
726 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
727 (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
742 AdjustedRHS = SextRHS;
744 SextRHS == TrueVal) {
746 AdjustedRHS = SextRHS;
755 AdjustedRHS = ZextRHS;
757 ZextRHS == TrueVal) {
759 AdjustedRHS = ZextRHS;
771 CmpRHS = AdjustedRHS;
820 "Unexpected results from matchSelectPattern");
850 "Unexpected result from matchSelectPattern");
857 bool CmpCanonicalized = !CmpUsesNegatedOp &&
863 if (CmpCanonicalized && RHSCanonicalized)
872 if (!CmpCanonicalized) {
875 if (CmpUsesNegatedOp)
880 if (!RHSCanonicalized) {
896 assert(FVal == LHS &&
"Unexpected results from matchSelectPattern");
900 assert(TVal == LHS &&
"Unexpected results from matchSelectPattern");
916 if (
Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder))
919 if (
Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder))
922 bool Changed = adjustMinMax(SI, *ICI);
925 return replaceInstUsesWith(SI, V);
931 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
952 bool IsBitTest =
false;
962 TrueWhenUnset =
false;
967 TrueWhenUnset =
true;
972 if (TrueWhenUnset && TrueVal == X &&
976 else if (!TrueWhenUnset && FalseVal == X &&
980 else if (TrueWhenUnset && FalseVal == X &&
984 else if (!TrueWhenUnset && TrueVal == X &&
989 return replaceInstUsesWith(SI, V);
998 return replaceInstUsesWith(SI, V);
1000 if (
Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1001 return replaceInstUsesWith(SI, V);
1004 return replaceInstUsesWith(SI, V);
1006 return Changed ? &
SI :
nullptr;
1018 static bool canSelectOperandBeMappingIntoPredBlock(
const Value *V,
1023 if (!I)
return true;
1029 if (
const PHINode *VP = dyn_cast<PHINode>(I))
1030 if (VP->getParent() == CondPHI->
getParent())
1054 if (C == A || C == B) {
1058 return replaceInstUsesWith(Outer, Inner);
1066 return replaceInstUsesWith(Outer, C);
1070 const APInt *CB, *CC;
1078 return replaceInstUsesWith(Outer, Inner);
1095 return replaceInstUsesWith(Outer, Inner);
1106 return replaceInstUsesWith(Outer, NewSI);
1109 auto IsFreeOrProfitableToInvert =
1110 [&](
Value *V,
Value *&NotV,
bool &ElidesXor) {
1126 Value *NotA, *NotB, *NotC;
1127 bool ElidesXor =
false;
1139 IsFreeOrProfitableToInvert(A, NotA, ElidesXor) &&
1140 IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&
1141 IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) {
1153 return replaceInstUsesWith(Outer, NewOuter);
1168 if (!TI || !FI || !TI->
hasOneUse() || !FI->hasOneUse())
1172 if ((TI->
getOpcode() == Instruction::Sub &&
1174 (TI->
getOpcode() == Instruction::FSub &&
1175 FI->getOpcode() == Instruction::FAdd)) {
1178 }
else if ((FI->getOpcode() == Instruction::Sub &&
1180 (FI->getOpcode() == Instruction::FSub &&
1181 TI->
getOpcode() == Instruction::FAdd)) {
1187 Value *OtherAddOp =
nullptr;
1188 if (SubOp->getOperand(0) == AddOp->
getOperand(0)) {
1190 }
else if (SubOp->getOperand(0) == AddOp->
getOperand(1)) {
1199 NegVal = Builder.
CreateFNeg(SubOp->getOperand(1));
1200 if (
Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
1202 Flags &= SubOp->getFastMathFlags();
1203 NegInst->setFastMathFlags(Flags);
1206 NegVal = Builder.
CreateNeg(SubOp->getOperand(1));
1209 Value *NewTrueOp = OtherAddOp;
1210 Value *NewFalseOp = NegVal;
1218 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
1221 Flags &= SubOp->getFastMathFlags();
1243 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
1262 Value *TruncCVal = cast<Value>(TruncC);
1304 for (
unsigned i = 0; i != NumElts; ++i) {
1315 }
else if (isa<UndefValue>(Elt)) {
1346 if (TVal == A || TVal == B || FVal == A || FVal == B)
1363 if (TSrc == C && FSrc == D) {
1367 }
else if (TSrc == D && FSrc == C) {
1409 if (Extract->getIndices()[0] !=
I)
1424 auto *CmpXchg = isExtractFromCmpXchg(SI.
getCondition(), 1);
1441 if (
X == CmpXchg &&
X->getCompareOperand() == SI.
getTrueValue()) {
1468 Value *MinMaxOp =
nullptr;
1469 Value *ThirdOp =
nullptr;
1473 if (D == A || C == A) {
1478 }
else if (D == B || C == B) {
1486 if (D == A || D == B) {
1491 }
else if (C == A || C == B) {
1498 if (!MinMaxOp || !ThirdOp)
1517 if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) {
1529 SQ.getWithInstruction(&SI)))
1530 return replaceInstUsesWith(SI, V);
1543 CmpInst *Cond = cast<CmpInst>(CondVal);
1556 return BinaryOperator::CreateOr(CondVal, FalseVal);
1561 return BinaryOperator::CreateAnd(NotCond, FalseVal);
1565 return BinaryOperator::CreateAnd(CondVal, TrueVal);
1570 return BinaryOperator::CreateOr(NotCond, TrueVal);
1575 if (CondVal == TrueVal)
1576 return BinaryOperator::CreateOr(CondVal, FalseVal);
1577 if (CondVal == FalseVal)
1578 return BinaryOperator::CreateAnd(CondVal, TrueVal);
1583 return BinaryOperator::CreateAnd(TrueVal, FalseVal);
1585 return BinaryOperator::CreateOr(TrueVal, FalseVal);
1598 return new ZExtInst(CondVal, SelType);
1602 return new SExtInst(CondVal, SelType);
1607 return new ZExtInst(NotCond, SelType);
1613 return new SExtInst(NotCond, SelType);
1618 if (
FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
1619 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
1626 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
1628 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
1630 return replaceInstUsesWith(SI, FalseVal);
1638 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
1640 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
1642 return replaceInstUsesWith(SI, TrueVal);
1662 }
else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
1669 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
1671 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
1673 return replaceInstUsesWith(SI, FalseVal);
1681 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
1683 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
1685 return replaceInstUsesWith(SI, TrueVal);
1711 Value *
X = FCI->getOperand(0);
1721 return replaceInstUsesWith(SI, Fabs);
1728 if (FCI->hasNoSignedZeros() &&
1734 return replaceInstUsesWith(SI, Fabs);
1740 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
1750 if (TI && FI && TI->
getOpcode() == FI->getOpcode())
1759 if (
Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
1762 Value *LHS, *RHS, *LHS2, *RHS2;
1772 bool IsCastNeeded = LHS->
getType() != SelType;
1773 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
1774 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
1777 ((CmpLHS != LHS && CmpLHS != RHS) ||
1778 (CmpRHS != LHS && CmpRHS != RHS)))) {
1786 auto FMF = cast<FPMathOperator>(SI.
getCondition())->getFastMathFlags();
1793 return replaceInstUsesWith(SI, NewSI);
1796 return replaceInstUsesWith(SI, NewCast);
1810 if (
Instruction *
I = factorizeMinMaxTree(SPF, LHS, RHS, Builder))
1822 if (
Instruction *R = foldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
1826 if (
Instruction *R = foldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
1838 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
1839 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
1843 if (
SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
1844 if (TrueSI->getCondition()->getType() == CondVal->
getType()) {
1846 if (TrueSI->getCondition() == CondVal) {
1855 if (TrueSI->getFalseValue() == FalseVal && TrueSI->
hasOneUse()) {
1863 if (
SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
1864 if (FalseSI->getCondition()->getType() == CondVal->
getType()) {
1866 if (FalseSI->getCondition() == CondVal) {
1873 if (FalseSI->getTrueValue() == TrueVal && FalseSI->
hasOneUse()) {
1874 Value *Or = Builder.
CreateOr(CondVal, FalseSI->getCondition());
1884 switch (BO->getOpcode()) {
1887 case Instruction::SRem:
1888 case Instruction::URem:
1889 case Instruction::SDiv:
1890 case Instruction::UDiv:
1900 canMergeSelectThroughBinop(TrueBO)) {
1901 if (
auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->
getOperand(0))) {
1902 if (TrueBOSI->getCondition() == CondVal) {
1903 TrueBO->
setOperand(0, TrueBOSI->getTrueValue());
1904 Worklist.Add(TrueBO);
1908 if (
auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->
getOperand(1))) {
1909 if (TrueBOSI->getCondition() == CondVal) {
1910 TrueBO->
setOperand(1, TrueBOSI->getTrueValue());
1911 Worklist.Add(TrueBO);
1920 canMergeSelectThroughBinop(FalseBO)) {
1921 if (
auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->
getOperand(0))) {
1922 if (FalseBOSI->getCondition() == CondVal) {
1923 FalseBO->
setOperand(0, FalseBOSI->getFalseValue());
1924 Worklist.Add(FalseBO);
1928 if (
auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->
getOperand(1))) {
1929 if (FalseBOSI->getCondition() == CondVal) {
1930 FalseBO->
setOperand(1, FalseBOSI->getFalseValue());
1931 Worklist.Add(FalseBO);
1944 if (
VectorType *VecTy = dyn_cast<VectorType>(SelType)) {
1945 unsigned VWidth = VecTy->getNumElements();
1946 APInt UndefElts(VWidth, 0);
1948 if (
Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
1950 return replaceInstUsesWith(SI, V);
1959 auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator());
1960 if (PBI && PBI->isConditional() &&
1961 PBI->getSuccessor(0) != PBI->getSuccessor(1) &&
1962 (PBI->getSuccessor(0) == Parent || PBI->getSuccessor(1) == Parent)) {
1963 bool CondIsTrue = PBI->getSuccessor(0) == Parent;
1965 PBI->getCondition(), SI.
getCondition(), DL, CondIsTrue);
1967 Value *V = *Implication ? TrueVal : FalseVal;
1968 return replaceInstUsesWith(SI, V);
1982 return replaceInstUsesWith(SI, TrueVal);
1984 return replaceInstUsesWith(SI, FalseVal);
1987 if (
Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_neg_zero_fp >, RHS, Instruction::FSub > m_FNeg(const RHS &X)
Match 'fneg X' as 'fsub -0.0, X'.
static bool isSelect01(const APInt &C1I, const APInt &C2I)
void push_back(const T &Elt)
static ConstantInt * getFalse(LLVMContext &Context)
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).
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
static Instruction * foldSelectBinOpIdentity(SelectInst &Sel)
Fold A = icmp eq/ne i8 x, 0 B = op i8 x, z C = select i1 A, i8 B, i8 y To C = select i1 A...
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.
Compute iterated dominance frontiers using a linear time algorithm.
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.
This class represents zero extension of integer types.
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.
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.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Value * CreateNot(Value *V, const Twine &Name="")
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.
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.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
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.
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.
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
static const Value * getNotArgument(const Value *BinOp)
Value * getOperand(unsigned i) const
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...
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false)
Return true if the two given values are negation.
LLVM Basic Block Representation.
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)
CallInst * CreateIntrinsic(Intrinsic::ID ID, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with no operands.
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.
ConstantFP - Floating Point Values [float, double].
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.
void setArgOperand(unsigned i, Value *v)
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
static bool isNot(const Value *V)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Class to represent integer types.
CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF)
Return the canonical inverse comparison predicate for the specified minimum/maximum flavor...
const Value * getCondition() const
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.
const APFloat & getValueAPF() const
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...
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.
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.
unsigned getNumUses() const
This method computes the number of uses of this Value.
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="")
Optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
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
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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)