21 using namespace PatternMatch;
23 #define DEBUG_TYPE "instcombine"
32 Offset = CI->getZExtValue();
46 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(
I->getOperand(1))) {
47 if (
I->getOpcode() == Instruction::Shl) {
49 Scale = UINT64_C(1) << RHS->getZExtValue();
51 return I->getOperand(0);
54 if (
I->getOpcode() == Instruction::Mul) {
56 Scale = RHS->getZExtValue();
58 return I->getOperand(0);
67 Offset += RHS->getZExtValue();
87 AllocaBuilder.SetInsertPoint(&AI);
94 unsigned AllocElTyAlign =
DL.getABITypeAlignment(AllocElTy);
95 unsigned CastElTyAlign =
DL.getABITypeAlignment(CastElTy);
96 if (CastElTyAlign < AllocElTyAlign)
return nullptr;
101 if (!AI.
hasOneUse() && CastElTyAlign == AllocElTyAlign)
return nullptr;
103 uint64_t AllocElTySize =
DL.getTypeAllocSize(AllocElTy);
104 uint64_t CastElTySize =
DL.getTypeAllocSize(CastElTy);
105 if (CastElTySize == 0 || AllocElTySize == 0)
return nullptr;
109 uint64_t AllocElTyStoreSize =
DL.getTypeStoreSize(AllocElTy);
110 uint64_t CastElTyStoreSize =
DL.getTypeStoreSize(CastElTy);
111 if (!AI.
hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize)
return nullptr;
115 unsigned ArraySizeScale;
116 uint64_t ArrayOffset;
122 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
123 (AllocElTySize*ArrayOffset ) % CastElTySize != 0)
return nullptr;
125 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
126 Value *Amt =
nullptr;
132 Amt = AllocaBuilder.CreateMul(Amt, NumElements);
135 if (uint64_t
Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
138 Amt = AllocaBuilder.CreateAdd(Amt, Off);
141 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
152 Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.
getType(),
"tmpcast");
153 replaceInstUsesWith(AI, NewCast);
155 return replaceInstUsesWith(CI, New);
162 if (
Constant *
C = dyn_cast<Constant>(V)) {
176 case Instruction::Sub:
177 case Instruction::Mul:
181 case Instruction::AShr:
182 case Instruction::LShr:
183 case Instruction::Shl:
184 case Instruction::UDiv:
185 case Instruction::URem: {
191 case Instruction::Trunc:
192 case Instruction::ZExt:
193 case Instruction::SExt:
203 Opc == Instruction::SExt);
207 Value *False = EvaluateInDifferentType(I->
getOperand(2), Ty, isSigned);
211 case Instruction::PHI: {
228 return InsertNewInstWith(Res, *I);
246 DstTy, SrcIntPtrTy, MidIntPtrTy,
251 if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
252 (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
263 if (
auto *CSrc = dyn_cast<CastInst>(Src)) {
272 if (
auto *
SI = dyn_cast<SelectInst>(Src))
277 if (isa<PHINode>(Src)) {
303 if (isa<Constant>(V))
307 if (!I)
return false;
313 if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
324 case Instruction::Sub:
325 case Instruction::Mul:
333 case Instruction::UDiv:
334 case Instruction::URem: {
338 if (BitWidth < OrigBitWidth) {
348 case Instruction::Shl:
353 if (CI->getLimitedValue(BitWidth) < BitWidth)
357 case Instruction::LShr:
366 CI->getLimitedValue(BitWidth) < BitWidth) {
371 case Instruction::Trunc:
374 case Instruction::ZExt:
375 case Instruction::SExt:
384 case Instruction::PHI: {
412 if (!TruncOp->
hasOneUse() || !isa<IntegerType>(DestType))
415 Value *VecInput =
nullptr;
420 !isa<VectorType>(VecInput->
getType()))
426 unsigned ShiftAmount = ShiftVal ? ShiftVal->
getZExtValue() : 0;
428 if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0))
433 unsigned NumVecElts = VecWidth / DestWidth;
439 unsigned Elt = ShiftAmount / DestWidth;
441 Elt = NumVecElts - 1 - Elt;
450 if (isa<IntegerType>(SrcTy) && !ShouldChangeType(SrcTy, DestTy))
462 Value *NarrowOp0 = Builder->CreateTrunc(LogicOp->
getOperand(0), DestTy);
467 if (
Instruction *Result = commonCastTransforms(CI))
481 if (SimplifyDemandedInstructionBits(CI))
491 if ((DestTy->
isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
496 DEBUG(
dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
497 " to avoid cast: " << CI <<
'\n');
498 Value *Res = EvaluateInDifferentType(Src, DestTy,
false);
500 return replaceInstUsesWith(CI, Res);
506 Src = Builder->CreateAnd(Src, One);
541 cast<Instruction>(Src)->getOperand(0)->hasOneUse()) {
557 if (Src->
hasOneUse() && isa<IntegerType>(SrcTy) &&
558 ShouldChangeType(SrcTy, DestTy)) {
567 Value *NewTrunc = Builder->CreateTrunc(A, DestTy, A->
getName() +
".tr");
570 Instruction::Shl, NewTrunc,
588 const APInt &Op1CV = Op1C->getValue();
594 if (!DoTransform)
return ICI;
599 In = Builder->CreateLShr(In, Sh, In->
getName() +
".lobit");
600 if (In->getType() != CI.
getType())
601 In = Builder->CreateIntCast(In, CI.
getType(),
false);
605 In = Builder->CreateXor(In, One, In->
getName() +
".not");
608 return replaceInstUsesWith(CI, In);
619 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
623 uint32_t BitWidth = Op1C->getType()->getBitWidth();
624 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
627 APInt KnownZeroMask(~KnownZero);
628 if (KnownZeroMask.isPowerOf2()) {
629 if (!DoTransform)
return ICI;
632 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
638 return replaceInstUsesWith(CI, Res);
641 uint32_t ShAmt = KnownZeroMask.logBase2();
650 if ((Op1CV != 0) == isNE) {
652 In = Builder->CreateXor(In, One);
655 if (CI.
getType() == In->getType())
656 return replaceInstUsesWith(CI, In);
658 Value *IntCast = Builder->CreateIntCast(In, CI.
getType(),
false);
659 return replaceInstUsesWith(CI, IntCast);
669 uint32_t BitWidth = ITy->getBitWidth();
673 APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
674 APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
678 if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
679 APInt KnownBits = KnownZeroLHS | KnownOneLHS;
680 APInt UnknownBit = ~KnownBits;
682 if (!DoTransform)
return ICI;
684 Value *Result = Builder->CreateXor(LHS, RHS);
687 if (KnownOneLHS.uge(UnknownBit))
688 Result = Builder->CreateAnd(Result,
692 Result = Builder->CreateLShr(
698 return replaceInstUsesWith(CI, Result);
728 if (isa<Constant>(V))
732 if (!I)
return false;
745 case Instruction::ZExt:
746 case Instruction::SExt:
747 case Instruction::Trunc:
753 case Instruction::Sub:
754 case Instruction::Mul:
759 if (BitsToClear == 0 && Tmp == 0)
777 case Instruction::Shl:
783 uint64_t ShiftAmt = Amt->getZExtValue();
784 BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
788 case Instruction::LShr:
794 BitsToClear += Amt->getZExtValue();
810 case Instruction::PHI: {
838 if (
Instruction *Result = commonCastTransforms(CI))
843 if (SimplifyDemandedInstructionBits(CI))
853 unsigned BitsToClear;
854 if ((DestTy->
isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
856 assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
857 "Unreasonable BitsToClear");
860 DEBUG(
dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
861 " to avoid zero extend: " << CI <<
'\n');
862 Value *Res = EvaluateInDifferentType(Src, DestTy,
false);
872 DestBitSize-SrcBitsKept),
874 return replaceInstUsesWith(CI, Res);
879 return BinaryOperator::CreateAnd(Res, C);
885 if (
TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {
890 Value *
A = CSrc->getOperand(0);
892 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
898 if (SrcSize < DstSize) {
905 if (SrcSize == DstSize) {
910 if (SrcSize > DstSize) {
913 return BinaryOperator::CreateAnd(Trunc,
919 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
920 return transformZExtICmp(ICI, CI);
929 if (LHS && RHS && LHS->
hasOneUse() && RHS->hasOneUse() &&
930 (transformZExtICmp(LHS, CI,
false) ||
931 transformZExtICmp(RHS, CI,
false))) {
934 Value *RCast = Builder->CreateZExt(RHS, CI.
getType(), RHS->getName());
938 if (
auto *LZExt = dyn_cast<ZExtInst>(LCast))
939 transformZExtICmp(LHS, *LZExt);
940 if (
auto *RZExt = dyn_cast<ZExtInst>(RCast))
941 transformZExtICmp(RHS, *RZExt);
961 return BinaryOperator::CreateXor(Builder->CreateAnd(X, ZC), ZC);
973 if (!Op1->getType()->isIntOrIntVectorTy())
976 if (
Constant *Op1C = dyn_cast<Constant>(Op1)) {
984 Value *In = Builder->CreateAShr(Op0, Sh, Op0->
getName()+
".lobit");
985 if (In->getType() != CI.
getType())
986 In = Builder->CreateIntCast(In, CI.
getType(),
true);
989 In = Builder->CreateNot(In, In->getName()+
".not");
990 return replaceInstUsesWith(CI, In);
994 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
999 ICI->
isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
1000 unsigned BitWidth = Op1C->getType()->getBitWidth();
1001 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
1004 APInt KnownZeroMask(~KnownZero);
1005 if (KnownZeroMask.isPowerOf2()) {
1009 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
1013 return replaceInstUsesWith(CI, V);
1019 unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
1022 In = Builder->CreateLShr(In,
1027 In = Builder->CreateAdd(In,
1033 unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
1036 In = Builder->CreateShl(In,
1041 BitWidth - 1),
"sext");
1045 return replaceInstUsesWith(CI, In);
1063 "Can't sign extend type to a smaller type");
1065 if (isa<Constant>(V))
1069 if (!I)
return false;
1080 case Instruction::SExt:
1081 case Instruction::ZExt:
1082 case Instruction::Trunc:
1088 case Instruction::Sub:
1089 case Instruction::Mul:
1101 case Instruction::PHI: {
1129 if (SimplifyDemandedInstructionBits(CI))
1137 bool KnownZero, KnownOne;
1140 Value *ZExt = Builder->CreateZExt(Src, DestTy);
1141 return replaceInstUsesWith(CI, ZExt);
1148 if ((DestTy->
isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
1151 DEBUG(
dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
1152 " to avoid sign extend: " << CI <<
'\n');
1153 Value *Res = EvaluateInDifferentType(Src, DestTy,
true);
1162 return replaceInstUsesWith(CI, Res);
1166 return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt,
"sext"),
1172 if (
TruncInst *TI = dyn_cast<TruncInst>(Src))
1173 if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
1179 Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt,
"sext");
1180 return BinaryOperator::CreateAShr(Res, ShAmt);
1183 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1184 return transformSExtICmp(ICI, CI);
1207 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1209 A = Builder->CreateShl(A, ShAmtV, CI.
getName());
1210 return BinaryOperator::CreateAShr(A, ShAmtV);
1232 if (I->
getOpcode() == Instruction::FPExt)
1238 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1247 if (CFP->getType()->isDoubleTy())
1273 unsigned RHSWidth = RHSOrig->getType()->getFPMantissaWidth();
1274 unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1278 case Instruction::FAdd:
1279 case Instruction::FSub:
1298 if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1300 LHSOrig = Builder->CreateFPExt(LHSOrig, CI.
getType());
1301 if (RHSOrig->getType() != CI.
getType())
1302 RHSOrig = Builder->CreateFPExt(RHSOrig, CI.
getType());
1309 case Instruction::FMul:
1315 if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1317 LHSOrig = Builder->CreateFPExt(LHSOrig, CI.
getType());
1318 if (RHSOrig->getType() != CI.
getType())
1319 RHSOrig = Builder->CreateFPExt(RHSOrig, CI.
getType());
1321 BinaryOperator::CreateFMul(LHSOrig, RHSOrig);
1326 case Instruction::FDiv:
1333 if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1335 LHSOrig = Builder->CreateFPExt(LHSOrig, CI.
getType());
1336 if (RHSOrig->getType() != CI.
getType())
1337 RHSOrig = Builder->CreateFPExt(RHSOrig, CI.
getType());
1339 BinaryOperator::CreateFDiv(LHSOrig, RHSOrig);
1344 case Instruction::FRem:
1349 if (SrcWidth == OpWidth)
1351 if (LHSWidth < SrcWidth)
1352 LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->
getType());
1353 else if (RHSWidth <= SrcWidth)
1354 RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->
getType());
1356 Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig);
1357 if (
Instruction *RI = dyn_cast<Instruction>(ExactResult))
1358 RI->copyFastMathFlags(OpI);
1396 case Intrinsic::fabs: {
1417 return commonCastTransforms(CI);
1432 Type *SrcTy = SrcI->getType();
1433 bool IsInputSigned = isa<SIToFPInst>(OpI);
1434 bool IsOutputSigned = isa<FPToSIInst>(FI);
1447 int ActualSize =
std::min(InputSize, OutputSize);
1449 if (ActualSize <= OpITy->getFPMantissaWidth()) {
1451 if (IsInputSigned && IsOutputSigned)
1458 return replaceInstUsesWith(FI, SrcI);
1467 return commonCastTransforms(FI);
1472 return commonCastTransforms(FI);
1478 return commonCastTransforms(FI);
1483 return commonCastTransforms(FI);
1487 return commonCastTransforms(CI);
1491 return commonCastTransforms(CI);
1500 DL.getPointerSizeInBits(AS)) {
1522 if (
GEP->hasAllZeroIndices() &&
1526 (!isa<AddrSpaceCastInst>(CI) ||
1527 GEP->getType() ==
GEP->getPointerOperand()->getType())) {
1537 return commonCastTransforms(CI);
1549 return commonPointerCastTransforms(CI);
1603 for (
unsigned i = 0, e = SrcElts;
i != e; ++
i)
1636 Type *VecEltTy,
bool isBigEndian) {
1638 "Shift should be a multiple of the element type size");
1641 if (isa<UndefValue>(V))
return true;
1645 if (V->
getType() == VecEltTy) {
1647 if (
Constant *C = dyn_cast<Constant>(V))
1653 ElementIndex = Elements.
size() - ElementIndex - 1;
1656 if (Elements[ElementIndex])
1659 Elements[ElementIndex] = V;
1663 if (
Constant *C = dyn_cast<Constant>(V)) {
1672 Shift, Elements, VecEltTy, isBigEndian);
1676 if (!isa<IntegerType>(C->
getType()))
1682 for (
unsigned i = 0;
i != NumElts; ++
i) {
1683 unsigned ShiftI = Shift+
i*ElementSize;
1697 if (!I)
return false;
1699 default:
return false;
1700 case Instruction::BitCast:
1703 case Instruction::ZExt:
1715 case Instruction::Shl: {
1718 if (!CI)
return false;
1758 for (
unsigned i = 0, e = Elements.size();
i != e; ++
i) {
1759 if (!Elements[
i])
continue;
1777 if (!ExtElt || !ExtElt->hasOneUse())
1786 unsigned NumElts = ExtElt->getVectorOperandType()->getNumElements();
1811 X->
getType() == DestTy && !isa<Constant>(
X)) {
1818 X->
getType() == DestTy && !isa<Constant>(
X)) {
1830 Value *Cond, *TVal, *FVal;
1852 auto *Sel = cast<Instruction>(BitCast.
getOperand(0));
1855 !isa<Constant>(
X)) {
1862 !isa<Constant>(
X)) {
1874 if (!isa<StoreInst>(U))
1906 while (!PhiWorklist.
empty()) {
1908 for (
Value *IncValue : OldPN->incoming_values()) {
1909 if (isa<Constant>(IncValue))
1912 if (
auto *LI = dyn_cast<LoadInst>(IncValue)) {
1917 Value *Addr = LI->getOperand(0);
1918 if (Addr == &CI || isa<LoadInst>(Addr))
1920 if (LI->hasOneUse() && LI->isSimple())
1927 if (
auto *PNode = dyn_cast<PHINode>(IncValue)) {
1928 if (OldPhiNodes.
insert(PNode))
1939 Type *TyA = BCI->getOperand(0)->getType();
1940 Type *TyB = BCI->getType();
1941 if (TyA != DestTy || TyB != SrcTy)
1948 for (
auto *OldPN : OldPhiNodes) {
1949 Builder->SetInsertPoint(OldPN);
1950 PHINode *NewPN = Builder->CreatePHI(DestTy, OldPN->getNumOperands());
1951 NewPNodes[OldPN] = NewPN;
1955 for (
auto *OldPN : OldPhiNodes) {
1956 PHINode *NewPN = NewPNodes[OldPN];
1957 for (
unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
1958 Value *V = OldPN->getOperand(j);
1959 Value *NewV =
nullptr;
1960 if (
auto *C = dyn_cast<Constant>(V)) {
1962 }
else if (
auto *LI = dyn_cast<LoadInst>(V)) {
1963 Builder->SetInsertPoint(LI->getNextNode());
1964 NewV = Builder->CreateBitCast(LI, DestTy);
1966 }
else if (
auto *BCI = dyn_cast<BitCastInst>(V)) {
1967 NewV = BCI->getOperand(0);
1968 }
else if (
auto *PrevPN = dyn_cast<PHINode>(V)) {
1969 NewV = NewPNodes[PrevPN];
1972 NewPN->
addIncoming(NewV, OldPN->getIncomingBlock(j));
1979 if (SI &&
SI->isSimple() &&
SI->getOperand(0) == PN) {
1980 Builder->SetInsertPoint(SI);
1982 cast<BitCastInst>(Builder->CreateBitCast(NewPNodes[PN], SrcTy));
1983 SI->setOperand(0, NewBC);
1989 return replaceInstUsesWith(CI, NewPNodes[PN]);
2002 return replaceInstUsesWith(CI, Src);
2004 if (
PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
2006 Type *DstElTy = DstPTy->getElementType();
2013 if (
AllocaInst *AI = dyn_cast<AllocaInst>(Src))
2014 if (
Instruction *V = PromoteCastOfAllocation(CI, *AI))
2027 unsigned NumZeros = 0;
2028 while (SrcElTy != DstElTy &&
2029 isa<CompositeType>(SrcElTy) && !SrcElTy->
isPointerTy() &&
2031 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(0U);
2036 if (SrcElTy == DstElTy) {
2042 if (
VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
2043 if (DestVTy->getNumElements() == 1 && !SrcTy->
isVectorTy()) {
2044 Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
2050 if (isa<IntegerType>(SrcTy)) {
2054 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
2055 CastInst *SrcCast = cast<CastInst>(Src);
2057 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
2059 cast<VectorType>(DestTy), *
this))
2067 return replaceInstUsesWith(CI, V);
2071 if (
VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
2072 if (SrcVTy->getNumElements() == 1) {
2077 Builder->CreateExtractElement(Src,
2085 dyn_cast<InsertElementInst>(CI.
getOperand(0)))
2094 if (SVI->hasOneUse() && DestTy->
isVectorTy() &&
2096 SVI->getType()->getNumElements() ==
2097 SVI->getOperand(0)->getType()->getVectorNumElements()) {
2102 if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
2104 ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
2106 Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
2107 Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
2116 if (
PHINode *PN = dyn_cast<PHINode>(Src))
2117 if (
Instruction *I = optimizeBitCastFromPhi(CI, PN))
2130 return commonPointerCastTransforms(CI);
2131 return commonCastTransforms(CI);
2142 Type *DestElemTy = DestTy->getElementType();
2150 Value *NewBitCast = Builder->CreateBitCast(Src, MidTy);
2154 return commonPointerCastTransforms(CI);
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
void computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
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.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
DiagnosticInfoOptimizationBase::Argument NV
Instruction * visitBitCast(BitCastInst &CI)
void setAlignment(unsigned Align)
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This class represents zero extension of integer types.
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
const DataLayout & getDataLayout() const
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)
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static Value * lookThroughFPExtensions(Value *V)
If this is a floating-point extension instruction, look through it until we get the source value...
This class represents a sign extension of integer types.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Type * getElementType() const
static Instruction * canonicalizeBitCastExtElt(BitCastInst &BitCast, InstCombiner &IC, const DataLayout &DL)
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract e...
Instruction * visitUIToFP(CastInst &CI)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
Instruction * visitFPExt(CastInst &CI)
Instruction * FoldItoFPtoI(Instruction &FI)
bool match(Val *V, const Pattern &P)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * visitFPToUI(FPToUIInst &FI)
This class represents a conversion between pointers from one address space to another.
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
This class represents the LLVM 'select' instruction.
static bool hasStoreUsersOnly(CastInst &CI)
Check if all users of CI are StoreInsts.
This is the base class for all instructions that perform data casts.
const APInt & getValue() const
Return the constant as an APInt value reference.
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::AShr > m_Shr(const LHS &L, const RHS &R)
Matches LShr or AShr.
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear, InstCombiner &IC, Instruction *CxtI)
Determine if the specified value can be computed in the specified wider type and produce the same low...
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
The core instruction combiner logic.
static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombiner &IC, Instruction *CxtI)
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type...
Instruction * visitIntToPtr(IntToPtrInst &CI)
static Instruction * optimizeVectorResize(Value *InVal, VectorType *DestTy, InstCombiner &IC)
This input value (which is known to have vector type) is being zero extended or truncated to the spec...
Instruction * visitAddrSpaceCast(AddrSpaceCastInst &CI)
static Type * getPPC_FP128Ty(LLVMContext &C)
static unsigned getTypeSizeIndex(unsigned Value, Type *Ty)
This class represents a cast from a pointer to an integer.
LLVM_NODISCARD bool empty() const
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
static bool isMultipleOfTypeSize(unsigned Value, Type *Ty)
bool insert(const value_type &X)
Insert a new element into the SetVector.
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
This class represents a no-op cast from one type to another.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
An instruction for storing to memory.
This class represents a cast from floating point to signed integer.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
static Value * optimizeIntegerToVectorInsertions(BitCastInst &CI, InstCombiner &IC)
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the v...
void takeName(Value *V)
Transfer the name from V to this value.
static Instruction * foldBitCastSelect(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a select if we can eliminate a bitcast.
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
Type * getElementType() const
This class represents a truncation of integer types.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
Class to represent pointers.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
static const fltSemantics & IEEEsingle()
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
OneUse_match< T > m_OneUse(const T &SubPattern)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
static Instruction * foldBitCastBitwiseLogic(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a bitwise logic operation if we can eliminate a bitcast.
This instruction inserts a single (scalar) element into a VectorType value.
APInt trunc(unsigned width) const
Truncate to new width.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isVectorTy() const
True if this is an instance of VectorType.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
This is an important base class in LLVM.
PointerType * getType() const
Overload to return most specific pointer type.
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
static Instruction * foldVecTruncToExtElt(TruncInst &Trunc, InstCombiner &IC, const DataLayout &DL)
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated...
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if 'V & Mask' is known to be zero.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
ConstantFP - Floating Point Values [float, double].
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static const fltSemantics & IEEEhalf()
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
static Value * decomposeSimpleLinearExpr(Value *Val, unsigned &Scale, uint64_t &Offset)
Analyze 'Val', seeing if it is a simple linear expression.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
uint64_t getNumElements() const
Value * getOperand(unsigned i) const
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Class to represent integer types.
Type * getSrcTy() const
Return the source type, as a convenience.
unsigned countPopulation() const
Count the number of bits set.
Predicate getPredicate() const
Return the predicate for this instruction.
This class represents a cast from an integer to a pointer.
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Instruction * visitFPToSI(FPToSIInst &FI)
unsigned getNumContainedTypes() const
Return the number of types in the derived type.
bool isPointerTy() const
True if this is an instance of PointerType.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
LLVMContext & getContext() const
All values hold a context through their type.
const Value * getTrueValue() const
#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.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
BinaryOps getOpcode() const
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
A SetVector that performs no allocations if smaller than a certain size.
This is the shared class of boolean and integer constants.
SelectPatternFlavor Flavor
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
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.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Instruction * visitSExt(SExtInst &CI)
This class represents a cast from floating point to unsigned integer.
Type * getDestTy() const
Return the destination type, as a convenience.
LLVM_NODISCARD T pop_back_val()
Instruction * visitZExt(ZExtInst &CI)
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
static unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
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 PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
Class for arbitrary precision integers.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
iterator_range< user_iterator > users()
unsigned getVectorNumElements() const
static const fltSemantics & IEEEdouble()
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
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 ...
Instruction * visitTrunc(TruncInst &CI)
void ComputeSignBit(const Value *V, bool &KnownZero, bool &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine whether the sign bit is known to be zero or one.
static bool collectInsertionElements(Value *V, unsigned Shift, SmallVectorImpl< Value * > &Elements, Type *VecEltTy, bool isBigEndian)
V is a value which is inserted into a vector of VecEltTy.
static IntegerType * getInt32Ty(LLVMContext &C)
Instruction * visitSIToFP(CastInst &CI)
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
static Constant * fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem)
Return a Constant* for the specified floating-point constant if it fits in the specified FP type with...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const APFloat & getValueAPF() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class represents a truncation of floating point types.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
static bool canEvaluateSExtd(Value *V, Type *Ty)
Return true if we can take the specified value and return it as type Ty without inserting any new cas...
const Value * getArraySize() const
Get the number of elements allocated.
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.
Instruction * commonPointerCastTransforms(CastInst &CI)
Implement the transforms for cast of pointer (bitcast/ptrtoint)
const Value * getFalseValue() const
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
Instruction * visitPtrToInt(PtrToIntInst &CI)
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
op_range incoming_values()
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property...
A wrapper class for inspecting calls to intrinsic functions.
an instruction to allocate memory on the stack
Instruction * visitFPTrunc(FPTruncInst &CI)