24using namespace PatternMatch;
26#define DEBUG_TYPE "instcombine"
35 Offset = CI->getZExtValue();
52 if (
I->getOpcode() == Instruction::Shl) {
54 Scale = UINT64_C(1) <<
RHS->getZExtValue();
56 return I->getOperand(0);
59 if (
I->getOpcode() == Instruction::Mul) {
61 Scale =
RHS->getZExtValue();
63 return I->getOperand(0);
66 if (
I->getOpcode() == Instruction::Add) {
100 if (!AllocElTy->
isSized() || !CastElTy->
isSized())
return nullptr;
110 bool AllocIsScalable = isa<ScalableVectorType>(AllocElTy);
111 bool CastIsScalable = isa<ScalableVectorType>(CastElTy);
112 if (AllocIsScalable != CastIsScalable)
return nullptr;
116 if (CastElTyAlign < AllocElTyAlign)
return nullptr;
121 if (!AI.
hasOneUse() && CastElTyAlign == AllocElTyAlign)
return nullptr;
126 if (CastElTySize == 0 || AllocElTySize == 0)
return nullptr;
133 if (!AI.
hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize)
return nullptr;
137 unsigned ArraySizeScale;
144 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
145 (AllocElTySize*ArrayOffset ) % CastElTySize != 0)
return nullptr;
148 assert(!AllocIsScalable || (ArrayOffset == 1 && ArraySizeScale == 0));
150 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
151 Value *Amt =
nullptr;
170 New->setMetadata(LLVMContext::MD_DIAssignID,
192 if (
Constant *
C = dyn_cast<Constant>(V)) {
201 unsigned Opc =
I->getOpcode();
203 case Instruction::Add:
204 case Instruction::Sub:
205 case Instruction::Mul:
206 case Instruction::And:
207 case Instruction::Or:
208 case Instruction::Xor:
209 case Instruction::AShr:
210 case Instruction::LShr:
211 case Instruction::Shl:
212 case Instruction::UDiv:
213 case Instruction::URem: {
219 case Instruction::Trunc:
220 case Instruction::ZExt:
221 case Instruction::SExt:
225 if (
I->getOperand(0)->getType() == Ty)
226 return I->getOperand(0);
231 Opc == Instruction::SExt);
233 case Instruction::Select: {
239 case Instruction::PHI: {
250 case Instruction::FPToUI:
251 case Instruction::FPToSI:
255 case Instruction::Call:
257 switch (II->getIntrinsicID()) {
260 case Intrinsic::vscale: {
279InstCombinerImpl::isEliminableCastPair(
const CastInst *CI1,
294 DstTy, SrcIntPtrTy, MidIntPtrTy,
299 if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
300 (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
311 if (
auto *SrcC = dyn_cast<Constant>(Src))
316 if (
auto *CSrc = dyn_cast<CastInst>(Src)) {
322 if (CSrc->hasOneUse())
328 if (
auto *Sel = dyn_cast<SelectInst>(Src)) {
334 auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition());
335 if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType() ||
346 if (
auto *PN = dyn_cast<PHINode>(Src)) {
363 auto *SrcTy = dyn_cast<FixedVectorType>(
X->getType());
364 auto *DestTy = dyn_cast<FixedVectorType>(Ty);
365 if (SrcTy && DestTy &&
366 SrcTy->getNumElements() == DestTy->getNumElements() &&
379 if (isa<Constant>(V))
392 assert(!isa<Constant>(V) &&
"Constant should already be handled.");
393 if (!isa<Instruction>(V))
421 auto *
I = cast<Instruction>(V);
422 Type *OrigTy = V->getType();
423 switch (
I->getOpcode()) {
424 case Instruction::Add:
425 case Instruction::Sub:
426 case Instruction::Mul:
427 case Instruction::And:
428 case Instruction::Or:
429 case Instruction::Xor:
434 case Instruction::UDiv:
435 case Instruction::URem: {
448 case Instruction::Shl: {
459 case Instruction::LShr: {
477 case Instruction::AShr: {
487 unsigned ShiftedBits = OrigBitWidth -
BitWidth;
494 case Instruction::Trunc:
497 case Instruction::ZExt:
498 case Instruction::SExt:
502 case Instruction::Select: {
507 case Instruction::PHI: {
517 case Instruction::FPToUI:
518 case Instruction::FPToSI: {
522 Type *InputTy =
I->getOperand(0)->getType()->getScalarType();
526 I->getOpcode() == Instruction::FPToSI);
547 if (!TruncOp->
hasOneUse() || !isa<IntegerType>(DestType))
550 Value *VecInput =
nullptr;
555 !isa<VectorType>(VecInput->
getType()))
559 unsigned VecWidth = VecType->getPrimitiveSizeInBits();
561 unsigned ShiftAmount = ShiftVal ? ShiftVal->
getZExtValue() : 0;
563 if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0))
568 unsigned NumVecElts = VecWidth / DestWidth;
569 if (VecType->getElementType() != DestType) {
574 unsigned Elt = ShiftAmount / DestWidth;
576 Elt = NumVecElts - 1 - Elt;
586 "Don't narrow to an illegal scalar type");
602 Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
609 if (Or0->
getOpcode() == BinaryOperator::LShr) {
615 Or1->
getOpcode() == BinaryOperator::LShr &&
616 "Illegal or(shift,shift) pair");
625 unsigned MaxShiftAmountWidth =
Log2_32(NarrowWidth);
626 APInt HiBitMask =
~APInt::getLowBitsSet(WideWidth, MaxShiftAmountWidth);
633 if (ShVal0 != ShVal1)
652 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, NarrowWidth);
655 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, NarrowWidth);
675 if (ShVal0 != ShVal1)
677 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
690 if (!isa<VectorType>(SrcTy) && !shouldChangeType(SrcTy, DestTy))
700 case Instruction::And:
701 case Instruction::Or:
702 case Instruction::Xor:
703 case Instruction::Add:
704 case Instruction::Sub:
705 case Instruction::Mul: {
732 case Instruction::LShr:
733 case Instruction::AShr: {
738 unsigned MaxShiftAmt = SrcWidth - DestWidth;
742 APInt(SrcWidth, MaxShiftAmt)))) {
743 auto *OldShift = cast<Instruction>(Trunc.
getOperand(0));
744 bool IsExact = OldShift->isExact();
748 OldShift->getOpcode() == Instruction::AShr
759 if (
Instruction *NarrowOr = narrowFunnelShift(Trunc))
770 auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.
getOperand(0));
771 if (Shuf && Shuf->hasOneUse() &&
match(Shuf->getOperand(1),
m_Undef()) &&
773 Shuf->getType() == Shuf->getOperand(0)->getType()) {
791 assert((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) &&
792 "Unexpected instruction for shrinking");
794 auto *InsElt = dyn_cast<InsertElementInst>(Trunc.
getOperand(0));
795 if (!InsElt || !InsElt->hasOneUse())
800 Value *VecOp = InsElt->getOperand(0);
801 Value *ScalarOp = InsElt->getOperand(1);
808 Value *NarrowOp =
Builder.CreateCast(Opcode, ScalarOp, DestScalarTy);
820 Type *DestTy = Trunc.
getType(), *SrcTy = Src->getType();
828 if ((DestTy->
isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
834 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
846 if (
auto *DestITy = dyn_cast<IntegerType>(DestTy)) {
847 if (DestWidth * 2 < SrcWidth) {
848 auto *NewDestTy = DestITy->getExtendedType();
849 if (shouldChangeType(SrcTy, NewDestTy) &&
852 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
853 " to reduce the width of operand of"
866 if (
SelectInst *Sel = dyn_cast<SelectInst>(Src))
875 if (DestWidth == 1) {
912 unsigned AWidth =
A->getType()->getScalarSizeInBits();
913 unsigned MaxShiftAmt = SrcWidth - std::max(DestWidth, AWidth);
914 auto *OldSh = cast<Instruction>(Src);
915 bool IsExact = OldSh->isExact();
920 APInt(SrcWidth, MaxShiftAmt)))) {
921 auto GetNewShAmt = [&](
unsigned Width) {
931 if (
A->getType() == DestTy) {
932 Constant *ShAmt = GetNewShAmt(DestWidth);
934 return IsExact ? BinaryOperator::CreateExactAShr(
A, ShAmt)
935 : BinaryOperator::CreateAShr(
A, ShAmt);
939 if (Src->hasOneUse()) {
940 Constant *ShAmt = GetNewShAmt(AWidth);
957 if (Src->hasOneUse() &&
958 (isa<VectorType>(SrcTy) || shouldChangeType(SrcTy, DestTy))) {
965 APInt Threshold =
APInt(
C->getType()->getScalarSizeInBits(), DestWidth);
988 auto *VecOpTy = cast<VectorType>(VecOp->
getType());
989 auto VecElts = VecOpTy->getElementCount();
992 if (SrcWidth % DestWidth == 0) {
993 uint64_t TruncRatio = SrcWidth / DestWidth;
994 uint64_t BitCastNumElts = VecElts.getKnownMinValue() * TruncRatio;
997 : VecOpIdx * TruncRatio;
998 assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() &&
1011 unsigned AWidth =
A->getType()->getScalarSizeInBits();
1012 if (AWidth == DestWidth && AWidth >
Log2_32(SrcWidth)) {
1016 return BinaryOperator::CreateAdd(NarrowCtlz, WidthDiff);
1026 if (
Log2_32(*MaxVScale) < DestWidth) {
1054 Value *In = Cmp->getOperand(0);
1056 In->getType()->getScalarSizeInBits() - 1);
1058 if (In->getType() != Zext.
getType())
1068 if (Op1CV->
isZero() && Cmp->isEquality() &&
1069 (Cmp->getOperand(0)->getType() == Zext.
getType() ||
1077 if (KnownZeroMask.isPowerOf2() &&
1079 KnownZeroMask.logBase2() + 1)) {
1080 uint32_t ShAmt = KnownZeroMask.logBase2();
1081 Value *In = Cmp->getOperand(0);
1086 In->getName() +
".lobit");
1102 if (
Cmp->isEquality() && Zext.
getType() ==
Cmp->getOperand(0)->getType()) {
1147 auto *
I = cast<Instruction>(V);
1149 switch (
I->getOpcode()) {
1150 case Instruction::ZExt:
1151 case Instruction::SExt:
1152 case Instruction::Trunc:
1154 case Instruction::And:
1155 case Instruction::Or:
1156 case Instruction::Xor:
1157 case Instruction::Add:
1158 case Instruction::Sub:
1159 case Instruction::Mul:
1164 if (BitsToClear == 0 && Tmp == 0)
1169 if (Tmp == 0 &&
I->isBitwiseLogicOp()) {
1172 unsigned VSize = V->getType()->getScalarSizeInBits();
1178 if (
I->getOpcode() == Instruction::And)
1187 case Instruction::Shl: {
1195 BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
1200 case Instruction::LShr: {
1208 if (BitsToClear > V->getType()->getScalarSizeInBits())
1209 BitsToClear = V->getType()->getScalarSizeInBits();
1215 case Instruction::Select:
1224 case Instruction::PHI: {
1239 case Instruction::Call:
1243 if (II->getIntrinsicID() == Intrinsic::vscale)
1264 Type *SrcTy = Src->getType(), *DestTy = Zext.
getType();
1267 unsigned BitsToClear;
1268 if (shouldChangeType(SrcTy, DestTy) &&
1271 "Can't clear more bits than in SrcTy");
1275 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
1276 " to avoid zero extend: "
1282 if (
auto *
SrcOp = dyn_cast<Instruction>(Src))
1283 if (
SrcOp->hasOneUse())
1293 DestBitSize - SrcBitsKept),
1300 return BinaryOperator::CreateAnd(Res,
C);
1306 if (
auto *CSrc = dyn_cast<TruncInst>(Src)) {
1311 Value *
A = CSrc->getOperand(0);
1312 unsigned SrcSize =
A->getType()->getScalarSizeInBits();
1313 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
1319 if (SrcSize < DstSize) {
1326 if (SrcSize == DstSize) {
1331 if (SrcSize > DstSize) {
1334 return BinaryOperator::CreateAnd(Trunc,
1340 if (
auto *Cmp = dyn_cast<ICmpInst>(Src))
1341 return transformZExtICmp(Cmp, Zext);
1347 X->getType() == DestTy)
1354 X->getType() == DestTy) {
1365 X->getType() == DestTy) {
1367 return BinaryOperator::CreateAnd(
X, ZextC);
1376 unsigned TypeWidth = Src->getType()->getScalarSizeInBits();
1377 if (
Log2_32(*MaxVScale) < TypeWidth) {
1391 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
1395 if (!Op1->getType()->isIntOrIntVectorTy())
1403 if (In->getType() != Sext.
getType())
1409 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1413 if (Cmp->hasOneUse() &&
1414 Cmp->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
1418 if (KnownZeroMask.isPowerOf2()) {
1419 Value *In = Cmp->getOperand(0);
1422 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
1432 unsigned ShiftAmt = KnownZeroMask.countr_zero();
1446 unsigned ShiftAmt = KnownZeroMask.countl_zero();
1454 KnownZeroMask.getBitWidth() - 1),
"sext");
1476 "Can't sign extend type to a smaller type");
1482 auto *
I = cast<Instruction>(V);
1483 switch (
I->getOpcode()) {
1484 case Instruction::SExt:
1485 case Instruction::ZExt:
1486 case Instruction::Trunc:
1488 case Instruction::And:
1489 case Instruction::Or:
1490 case Instruction::Xor:
1491 case Instruction::Add:
1492 case Instruction::Sub:
1493 case Instruction::Mul:
1501 case Instruction::Select:
1505 case Instruction::PHI: {
1532 Type *SrcTy = Src->getType(), *DestTy = Sext.
getType();
1544 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type"
1545 " to avoid sign extend: "
1565 unsigned XBitSize =
X->getType()->getScalarSizeInBits();
1570 if (Src->hasOneUse() &&
X->getType() == DestTy) {
1581 if (Src->hasOneUse() &&
1589 if (
auto *Cmp = dyn_cast<ICmpInst>(Src))
1590 return transformSExtICmp(Cmp, Sext);
1607 Constant *BA =
nullptr, *CA =
nullptr;
1620 return BinaryOperator::CreateAShr(
A, NewShAmt);
1628 Type *XTy =
X->getType();
1635 if (cast<BinaryOperator>(Src)->getOperand(0)->
hasOneUse()) {
1647 if (
Log2_32(*MaxVScale) < (SrcBitSize - 1)) {
1687 auto *CV = dyn_cast<Constant>(V);
1688 auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
1692 Type *MinType =
nullptr;
1694 unsigned NumElts = CVVTy->getNumElements();
1698 for (
unsigned i = 0; i != NumElts; ++i) {
1699 if (isa<UndefValue>(CV->getAggregateElement(i)))
1702 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
1722 if (
auto *FPExt = dyn_cast<FPExtInst>(V))
1723 return FPExt->getOperand(0)->getType();
1728 if (
auto *CFP = dyn_cast<ConstantFP>(V))
1735 if (
auto *FPCExt = dyn_cast<ConstantExpr>(V))
1736 if (FPCExt->getOpcode() == Instruction::FPExt)
1737 return FPCExt->getOperand(0)->getType();
1744 return V->getType();
1751 assert((Opcode == CastInst::SIToFP || Opcode == CastInst::UIToFP) &&
1753 Value *Src =
I.getOperand(0);
1754 Type *SrcTy = Src->getType();
1755 Type *FPTy =
I.getType();
1756 bool IsSigned = Opcode == Instruction::SIToFP;
1762 if (SrcSize <= DestNumSigBits)
1771 int SrcNumSigBits =
F->getType()->getFPMantissaWidth();
1778 if (SrcNumSigBits > 0 && DestNumSigBits > 0 &&
1779 SrcNumSigBits <= DestNumSigBits)
1790 if (SigBits <= DestNumSigBits)
1808 auto *BO = dyn_cast<BinaryOperator>(FPT.
getOperand(0));
1809 if (BO && BO->hasOneUse()) {
1812 unsigned OpWidth = BO->getType()->getFPMantissaWidth();
1815 unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1817 switch (BO->getOpcode()) {
1819 case Instruction::FAdd:
1820 case Instruction::FSub:
1839 if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1847 case Instruction::FMul:
1853 if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1859 case Instruction::FDiv:
1866 if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1872 case Instruction::FRem: {
1877 if (SrcWidth == OpWidth)
1880 if (LHSWidth == SrcWidth) {
1897 if (Op && Op->hasOneUse()) {
1900 if (isa<FPMathOperator>(Op))
1913 X->getType() == Ty) {
1920 X->getType() == Ty) {
1928 if (
auto *II = dyn_cast<IntrinsicInst>(FPT.
getOperand(0))) {
1929 switch (II->getIntrinsicID()) {
1931 case Intrinsic::ceil:
1932 case Intrinsic::fabs:
1933 case Intrinsic::floor:
1934 case Intrinsic::nearbyint:
1935 case Intrinsic::rint:
1936 case Intrinsic::round:
1937 case Intrinsic::roundeven:
1938 case Intrinsic::trunc: {
1939 Value *Src = II->getArgOperand(0);
1940 if (!Src->hasOneUse())
1946 if (II->getIntrinsicID() != Intrinsic::fabs) {
1947 FPExtInst *FPExtSrc = dyn_cast<FPExtInst>(Src);
1948 if (!FPExtSrc || FPExtSrc->
getSrcTy() != Ty)
1956 II->getIntrinsicID(), Ty);
1958 II->getOperandBundlesAsDefs(OpBundles);
1971 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1972 auto *FPCast = cast<CastInst>(Src);
1985 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1986 auto *FPCast = cast<CastInst>(Src);
2002 auto *OpI = cast<CastInst>(FI.
getOperand(0));
2003 Value *
X = OpI->getOperand(0);
2004 Type *XType =
X->getType();
2006 bool IsOutputSigned = isa<FPToSIInst>(FI);
2021 if (OutputSize > OpI->getType()->getFPMantissaWidth())
2026 bool IsInputSigned = isa<SIToFPInst>(OpI);
2027 if (IsInputSigned && IsOutputSigned)
2034 assert(XType == DestType &&
"Unexpected types for int to FP to int casts");
2086 if (
GEP->hasAllZeroIndices() &&
2090 (!isa<AddrSpaceCastInst>(CI) ||
2091 GEP->getType() ==
GEP->getPointerOperandType())) {
2112 if (TySize != PtrSize) {
2119 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
SrcOp)) {
2124 if (
GEP->hasOneUse() &&
2125 isa<ConstantPointerNull>(
GEP->getPointerOperand())) {
2170 if (SrcTy->getElementType() != DestTy->getElementType()) {
2175 if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
2176 DestTy->getElementType()->getPrimitiveSizeInBits())
2181 cast<FixedVectorType>(SrcTy)->getNumElements());
2186 unsigned SrcElts = cast<FixedVectorType>(SrcTy)->getNumElements();
2187 unsigned DestElts = cast<FixedVectorType>(DestTy)->getNumElements();
2189 assert(SrcElts != DestElts &&
"Element counts should be different.");
2194 auto ShuffleMaskStorage = llvm::to_vector<16>(llvm::seq<int>(0, SrcElts));
2198 if (SrcElts > DestElts) {
2207 ShuffleMask = ShuffleMaskStorage;
2209 ShuffleMask = ShuffleMask.
take_back(DestElts);
2211 ShuffleMask = ShuffleMask.
take_front(DestElts);
2222 unsigned DeltaElts = DestElts - SrcElts;
2224 ShuffleMaskStorage.insert(ShuffleMaskStorage.begin(), DeltaElts, NullElt);
2226 ShuffleMaskStorage.append(DeltaElts, NullElt);
2227 ShuffleMask = ShuffleMaskStorage;
2255 "Shift should be a multiple of the element type size");
2258 if (isa<UndefValue>(V))
return true;
2262 if (V->getType() == VecEltTy) {
2264 if (
Constant *
C = dyn_cast<Constant>(V))
2265 if (
C->isNullValue())
2270 ElementIndex = Elements.size() - ElementIndex - 1;
2273 if (Elements[ElementIndex])
2276 Elements[ElementIndex] = V;
2280 if (
Constant *
C = dyn_cast<Constant>(V)) {
2293 if (!isa<IntegerType>(
C->getType()))
2295 C->getType()->getPrimitiveSizeInBits()));
2299 for (
unsigned i = 0; i != NumElts; ++i) {
2300 unsigned ShiftI = Shift+i*ElementSize;
2311 if (!V->hasOneUse())
return false;
2314 if (!
I)
return false;
2315 switch (
I->getOpcode()) {
2316 default:
return false;
2317 case Instruction::BitCast:
2318 if (
I->getOperand(0)->getType()->isVectorTy())
2322 case Instruction::ZExt:
2324 I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
2329 case Instruction::Or:
2334 case Instruction::Shl: {
2336 ConstantInt *CI = dyn_cast<ConstantInt>(
I->getOperand(1));
2337 if (!CI)
return false;
2364 auto *DestVecTy = cast<FixedVectorType>(CI.
getType());
2369 DestVecTy->getElementType(),
2377 for (
unsigned i = 0, e = Elements.size(); i != e; ++i) {
2378 if (!Elements[i])
continue;
2410 auto *FixedVType = dyn_cast<FixedVectorType>(VecType);
2411 if (DestType->
isVectorTy() && FixedVType && FixedVType->getNumElements() == 1)
2438 if (
X->getType()->isFPOrFPVectorTy() &&
2439 Y->getType()->isIntOrIntVectorTy()) {
2445 if (
X->getType()->isIntOrIntVectorTy() &&
2446 Y->getType()->isFPOrFPVectorTy()) {
2461 X->getType() == DestTy && !isa<Constant>(
X)) {
2468 X->getType() == DestTy && !isa<Constant>(
X)) {
2500 if (
auto *CondVTy = dyn_cast<VectorType>(CondTy))
2502 CondVTy->getElementCount() !=
2503 cast<VectorType>(DestTy)->getElementCount())
2513 auto *Sel = cast<Instruction>(BitCast.
getOperand(0));
2516 !isa<Constant>(
X)) {
2518 Value *CastedVal =
Builder.CreateBitCast(FVal, DestTy);
2523 !isa<Constant>(
X)) {
2525 Value *CastedVal =
Builder.CreateBitCast(TVal, DestTy);
2535 if (!isa<StoreInst>(U))
2556 Type *SrcTy = Src->getType();
2568 while (!PhiWorklist.
empty()) {
2570 for (
Value *IncValue : OldPN->incoming_values()) {
2571 if (isa<Constant>(IncValue))
2574 if (
auto *
LI = dyn_cast<LoadInst>(IncValue)) {
2580 if (
Addr == &CI || isa<LoadInst>(
Addr))
2588 if (
LI->hasOneUse() &&
LI->isSimple())
2595 if (
auto *PNode = dyn_cast<PHINode>(IncValue)) {
2596 if (OldPhiNodes.
insert(PNode))
2601 auto *BCI = dyn_cast<BitCastInst>(IncValue);
2607 Type *TyA = BCI->getOperand(0)->getType();
2608 Type *TyB = BCI->getType();
2609 if (TyA != DestTy || TyB != SrcTy)
2616 for (
auto *OldPN : OldPhiNodes) {
2618 if (
auto *SI = dyn_cast<StoreInst>(V)) {
2619 if (!
SI->isSimple() ||
SI->getOperand(0) != OldPN)
2621 }
else if (
auto *BCI = dyn_cast<BitCastInst>(V)) {
2623 Type *TyB = BCI->getOperand(0)->getType();
2624 Type *TyA = BCI->getType();
2625 if (TyA != DestTy || TyB != SrcTy)
2627 }
else if (
auto *
PHI = dyn_cast<PHINode>(V)) {
2631 if (!OldPhiNodes.contains(
PHI))
2641 for (
auto *OldPN : OldPhiNodes) {
2644 NewPNodes[OldPN] = NewPN;
2648 for (
auto *OldPN : OldPhiNodes) {
2649 PHINode *NewPN = NewPNodes[OldPN];
2650 for (
unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
2651 Value *
V = OldPN->getOperand(j);
2652 Value *NewV =
nullptr;
2653 if (
auto *
C = dyn_cast<Constant>(V)) {
2655 }
else if (
auto *
LI = dyn_cast<LoadInst>(V)) {
2664 }
else if (
auto *BCI = dyn_cast<BitCastInst>(V)) {
2665 NewV = BCI->getOperand(0);
2666 }
else if (
auto *PrevPN = dyn_cast<PHINode>(V)) {
2667 NewV = NewPNodes[PrevPN];
2670 NewPN->
addIncoming(NewV, OldPN->getIncomingBlock(j));
2684 for (
auto *OldPN : OldPhiNodes) {
2685 PHINode *NewPN = NewPNodes[OldPN];
2687 if (
auto *SI = dyn_cast<StoreInst>(V)) {
2688 assert(
SI->isSimple() &&
SI->getOperand(0) == OldPN);
2692 SI->setOperand(0, NewBC);
2696 else if (
auto *BCI = dyn_cast<BitCastInst>(V)) {
2697 Type *TyB = BCI->getOperand(0)->getType();
2698 Type *TyA = BCI->getType();
2699 assert(TyA == DestTy && TyB == SrcTy);
2705 }
else if (
auto *
PHI = dyn_cast<PHINode>(V)) {
2720 PointerType *SrcPTy = cast<PointerType>(Src->getType());
2724 if (SrcPTy->isOpaque() || DstPTy->isOpaque())
2727 Type *DstElTy = DstPTy->getNonOpaquePointerElementType();
2728 Type *SrcElTy = SrcPTy->getNonOpaquePointerElementType();
2738 unsigned NumZeros = 0;
2739 while (SrcElTy && SrcElTy != DstElTy) {
2745 if (SrcElTy == DstElTy) {
2748 SrcPTy->getNonOpaquePointerElementType(), Src, Idxs);
2752 bool CanBeNull, CanBeFreed;
2753 if (Src->getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed)) {
2760 if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
2761 GEP->setIsInBounds();
2772 Type *SrcTy = Src->getType();
2777 if (DestTy == Src->getType())
2780 if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy)) {
2785 if (
AllocaInst *AI = dyn_cast<AllocaInst>(Src))
2795 if (DestVTy->getNumElements() == 1 && SrcTy->
isX86_MMXTy()) {
2801 if (isa<IntegerType>(SrcTy)) {
2805 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
2806 CastInst *SrcCast = cast<CastInst>(Src);
2808 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
2810 BCIn->getOperand(0), cast<VectorType>(DestTy), *
this))
2823 if (SrcVTy->getNumElements() == 1) {
2836 if (
auto *InsElt = dyn_cast<InsertElementInst>(Src))
2837 return new BitCastInst(InsElt->getOperand(1), DestTy);
2847 Y->getType()->isIntegerTy() && isDesirableIntType(
BitWidth)) {
2850 IndexC = SrcVTy->getNumElements() - 1 - IndexC;
2856 unsigned EltWidth =
Y->getType()->getScalarSizeInBits();
2860 return BinaryOperator::CreateOr(AndX, ZextY);
2865 if (
auto *Shuf = dyn_cast<ShuffleVectorInst>(Src)) {
2868 Value *ShufOp0 = Shuf->getOperand(0);
2869 Value *ShufOp1 = Shuf->getOperand(1);
2870 auto ShufElts = cast<VectorType>(Shuf->getType())->getElementCount();
2871 auto SrcVecElts = cast<VectorType>(ShufOp0->
getType())->getElementCount();
2872 if (Shuf->hasOneUse() && DestTy->
isVectorTy() &&
2873 cast<VectorType>(DestTy)->getElementCount() == ShufElts &&
2874 ShufElts == SrcVecElts) {
2879 if (((Tmp = dyn_cast<BitCastInst>(ShufOp0)) &&
2881 ((Tmp = dyn_cast<BitCastInst>(ShufOp1)) &&
2895 if (DestTy->
isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 &&
2896 Shuf->hasOneUse() && Shuf->isReverse()) {
2897 unsigned IntrinsicNum = 0;
2900 IntrinsicNum = Intrinsic::bswap;
2902 IntrinsicNum = Intrinsic::bitreverse;
2904 if (IntrinsicNum != 0) {
2905 assert(ShufOp0->
getType() == SrcTy &&
"Unexpected shuffle mask");
2916 if (
PHINode *PN = dyn_cast<PHINode>(Src))
2939 PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType());
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
SmallVector< MachineOperand, 4 > Cond
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
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 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...
static bool hasStoreUsersOnly(CastInst &CI)
Check if all users of CI are StoreInsts.
static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear, InstCombinerImpl &IC, Instruction *CxtI)
Determine if the specified value can be computed in the specified wider type and produce the same low...
static Instruction * canonicalizeBitCastExtElt(BitCastInst &BitCast, InstCombinerImpl &IC)
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract e...
static Instruction * shrinkSplatShuffle(TruncInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of a splat shuffle.
static Instruction * foldBitCastSelect(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a select if we can eliminate a bitcast.
static Instruction * foldBitCastBitwiseLogic(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a bitwise logic operation if we can eliminate a bitcast.
static bool fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem)
Return a Constant* for the specified floating-point constant if it fits in the specified FP type with...
static Instruction * optimizeVectorResizeWithIntegerBitCasts(Value *InVal, VectorType *DestTy, InstCombinerImpl &IC)
This input value (which is known to have vector type) is being zero extended or truncated to the spec...
static Instruction * shrinkInsertElt(CastInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of an insert element.
static Instruction * convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder, const DataLayout &DL)
static bool isMultipleOfTypeSize(unsigned Value, Type *Ty)
static Value * optimizeIntegerToVectorInsertions(BitCastInst &CI, InstCombinerImpl &IC)
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the v...
static bool canAlwaysEvaluateInType(Value *V, Type *Ty)
Constants and extensions/truncates from the destination type are always free to be evaluated in that ...
static Type * shrinkFPConstant(ConstantFP *CFP)
static bool canNotEvaluateInType(Value *V, Type *Ty)
Filter out values that we can not evaluate in the destination type for free.
static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC)
Return true if the cast from integer to FP can be proven to be exact for all possible inputs (the con...
static Type * getMinimumFPType(Value *V)
Find the minimum FP type we can safely truncate to.
static unsigned getTypeSizeIndex(unsigned Value, Type *Ty)
static Type * shrinkFPConstantVector(Value *V)
static Value * decomposeSimpleLinearExpr(Value *Val, unsigned &Scale, uint64_t &Offset)
Analyze 'Val', seeing if it is a simple linear expression.
static Instruction * foldVecTruncToExtElt(TruncInst &Trunc, InstCombinerImpl &IC)
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated,...
static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombinerImpl &IC, Instruction *CxtI)
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type,...
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
static SymbolRef::Type getType(const Symbol *Sym)
static bool hasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI, MachineDominatorTree &MDT, LiveIntervals &LIS)
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
PointerType * getType() const
Overload to return most specific pointer type.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
unsigned getAddressSpace() const
Return the address space for the allocation.
const Value * getArraySize() const
Get the number of elements allocated.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, Instruction *FMFSource, const Twine &Name="")
BinaryOps getOpcode() const
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, Instruction *FMFSource, const Twine &Name="")
This class represents a no-op cast from one type to another.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
Type * getSrcTy() const
Return the source type, as a convenience.
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.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
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 ...
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 CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
Type * getDestTy() const
Return the destination type, as a convenience.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ ICMP_SLT
signed less than
@ ICMP_ULT
unsigned less than
@ ICMP_ULE
unsigned less or equal
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
static Constant * getOr(Constant *C1, Constant *C2)
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
const APFloat & getValueAPF() const
This is the shared class of boolean and integer constants.
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.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
This is an important base class in LLVM.
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool isElementWiseEqual(Value *Y) const
Return true if this constant and a constant 'Y' are element-wise equal.
A parsed version of the target data layout string in and methods for querying it.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
This class represents an extension of floating point types.
This class represents a cast from floating point to signed integer.
This class represents a cast from floating point to unsigned integer.
This class represents a truncation of floating point types.
Class to represent fixed width SIMD vectors.
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This instruction compares its operands according to the predicate given to the constructor.
Common base class shared among various IRBuilders.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateVScale(Constant *Scaling, const Twine &Name="")
Create a call to llvm.vscale, multiplied by Scaling.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Value * CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder's default FMF.
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Value * CreateNot(Value *V, const Twine &Name="")
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitZExt(ZExtInst &Zext)
Instruction * visitAddrSpaceCast(AddrSpaceCastInst &CI)
Instruction * visitSExt(SExtInst &Sext)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Instruction * visitFPToSI(FPToSIInst &FI)
Instruction * visitTrunc(TruncInst &CI)
Instruction * visitUIToFP(CastInst &CI)
Instruction * visitPtrToInt(PtrToIntInst &CI)
Instruction * visitSIToFP(CastInst &CI)
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * foldItoFPtoI(CastInst &FI)
fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X) This is safe if the intermediate ty...
Instruction * visitFPTrunc(FPTruncInst &CI)
Instruction * visitBitCast(BitCastInst &CI)
Instruction * visitIntToPtr(IntToPtrInst &CI)
Instruction * visitFPToUI(FPToUIInst &FI)
Instruction * PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI)
If we find a cast of an allocation instruction, try to eliminate the cast by moving the type informat...
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns true for,...
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * visitFPExt(CastInst &CI)
LoadInst * combineLoadToNewType(LoadInst &LI, Type *NewTy, const Twine &Suffix="")
Helper to combine a load to a new type.
Instruction * commonPointerCastTransforms(CastInst &CI)
Implement the transforms for cast of pointer (bitcast/ptrtoint)
const DataLayout & getDataLayout() const
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Same as InsertNewInstBefore, but also sets the debug loc.
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
unsigned ComputeNumSignBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
void push(Instruction *I)
Push the instruction onto the worklist stack.
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
const Function * getFunction() const
Return the function this instruction belongs to.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
This class represents a cast from an integer to a pointer.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A wrapper class for inspecting calls to intrinsic functions.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
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...
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Class to represent pointers.
bool hasSameElementTypeAs(PointerType *Other)
Return true if both pointer types have the same element type.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static PointerType * getWithSamePointeeType(PointerType *PT, unsigned AddressSpace)
This constructs a pointer type with the same pointee type as input PointerType (or opaque pointer if ...
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents a cast from a pointer to an integer.
Value * getPointerOperand()
Gets the pointer operand.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
bool insert(const value_type &X)
Insert a new element into the SetVector.
This instruction constructs a fixed permutation of two input vectors.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
static Type * getHalfTy(LLVMContext &C)
static Type * getDoubleTy(LLVMContext &C)
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool isPointerTy() const
True if this is an instance of PointerType.
Type * getNonOpaquePointerElementType() const
Only use this method in code that is not reachable with opaque pointers, or part of deprecated method...
bool isX86_MMXTy() const
Return true if this is X86 MMX.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Type * getWithNewType(Type *EltTy) const
Given vector type, change the element type, whilst keeping the old number of elements.
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
bool isX86_AMXTy() const
Return true if this is X86 AMX.
static IntegerType * getInt32Ty(LLVMContext &C)
static Type * getFloatTy(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
static Type * getPPC_FP128Ty(LLVMContext &C)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name="", Instruction *InsertBefore=nullptr)
'undef' values are things that do not have specified contents.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
iterator_range< user_iterator > users()
LLVMContext & getContext() const
All values hold a context through their type.
StringRef getName() const
Return a constant reference to the value's name.
void takeName(Value *V)
Transfer the name from V to this value.
Base class of all SIMD vector types.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
This class represents zero extension of integer types.
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(APInt V)
Match a specific integer value or vector with all elements equal to the value.
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
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.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
OneUse_match< T > m_OneUse(const T &SubPattern)
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
CastClass_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
CastClass_match< OpTy, Instruction::FPToSI > m_FPToSI(const OpTy &Op)
match_combine_or< CastClass_match< OpTy, Instruction::ZExt >, CastClass_match< OpTy, Instruction::SExt > > m_ZExtOrSExt(const OpTy &Op)
CastClass_match< OpTy, Instruction::FPToUI > m_FPToUI(const OpTy &Op)
CastClass_match< OpTy, Instruction::FPExt > m_FPExt(const OpTy &Op)
VScaleVal_match m_VScale()
specific_intval< true > m_SpecificIntAllowUndef(APInt V)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
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...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
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...
@ And
Bitwise or logical AND of integers.
constexpr unsigned BitWidth
bool isKnownNonNegative(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Returns true if the give value is known to be non-negative.
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static const fltSemantics & IEEEsingle() LLVM_READNONE
static constexpr roundingMode rmNearestTiesToEven
static const fltSemantics & IEEEdouble() LLVM_READNONE
static const fltSemantics & IEEEhalf() LLVM_READNONE
static unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
This struct is a compact representation of a valid (non-zero power of two) alignment.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.