25 #define DEBUG_TYPE "instcombine" 34 Offset = CI->getZExtValue();
48 if (
ConstantInt *RHS = dyn_cast<ConstantInt>(
I->getOperand(1))) {
49 if (
I->getOpcode() == Instruction::Shl) {
51 Scale = UINT64_C(1) << RHS->getZExtValue();
53 return I->getOperand(0);
56 if (
I->getOpcode() == Instruction::Mul) {
58 Scale = RHS->getZExtValue();
60 return I->getOperand(0);
69 Offset += RHS->getZExtValue();
89 AllocaBuilder.SetInsertPoint(&AI);
96 unsigned AllocElTyAlign = DL.getABITypeAlignment(AllocElTy);
97 unsigned CastElTyAlign = DL.getABITypeAlignment(CastElTy);
98 if (CastElTyAlign < AllocElTyAlign)
return nullptr;
103 if (!AI.
hasOneUse() && CastElTyAlign == AllocElTyAlign)
return nullptr;
105 uint64_t AllocElTySize = DL.getTypeAllocSize(AllocElTy);
106 uint64_t CastElTySize = DL.getTypeAllocSize(CastElTy);
107 if (CastElTySize == 0 || AllocElTySize == 0)
return nullptr;
111 uint64_t AllocElTyStoreSize = DL.getTypeStoreSize(AllocElTy);
112 uint64_t CastElTyStoreSize = DL.getTypeStoreSize(CastElTy);
113 if (!AI.
hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize)
return nullptr;
117 unsigned ArraySizeScale;
118 uint64_t ArrayOffset;
124 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
125 (AllocElTySize*ArrayOffset ) % CastElTySize != 0)
return nullptr;
127 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
128 Value *Amt =
nullptr;
134 Amt = AllocaBuilder.CreateMul(Amt, NumElements);
137 if (uint64_t
Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
140 Amt = AllocaBuilder.CreateAdd(Amt, Off);
143 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
154 Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.
getType(),
"tmpcast");
155 replaceInstUsesWith(AI, NewCast);
157 return replaceInstUsesWith(CI, New);
164 if (
Constant *
C = dyn_cast<Constant>(V)) {
178 case Instruction::Sub:
179 case Instruction::Mul:
180 case Instruction::And:
181 case Instruction::Or:
182 case Instruction::Xor:
183 case Instruction::AShr:
184 case Instruction::LShr:
185 case Instruction::Shl:
186 case Instruction::UDiv:
187 case Instruction::URem: {
193 case Instruction::Trunc:
194 case Instruction::ZExt:
195 case Instruction::SExt:
205 Opc == Instruction::SExt);
213 case Instruction::PHI: {
230 return InsertNewInstWith(Res, *I);
248 DstTy, SrcIntPtrTy, MidIntPtrTy,
253 if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
254 (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
265 if (
auto *CSrc = dyn_cast<CastInst>(Src)) {
272 if (CSrc->hasOneUse())
278 if (
auto *Sel = dyn_cast<SelectInst>(Src)) {
284 if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType())
292 if (
auto *PN = dyn_cast<PHINode>(Src)) {
307 if (isa<Constant>(V))
320 assert(!isa<Constant>(V) &&
"Constant should already be handled.");
321 if (!isa<Instruction>(V))
349 auto *
I = cast<Instruction>(V);
351 switch (
I->getOpcode()) {
353 case Instruction::Sub:
354 case Instruction::Mul:
355 case Instruction::And:
356 case Instruction::Or:
357 case Instruction::Xor:
362 case Instruction::UDiv:
363 case Instruction::URem: {
365 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
367 assert(BitWidth < OrigBitWidth &&
"Unexpected bitwidths!");
369 if (IC.MaskedValueIsZero(
I->getOperand(0),
Mask, 0, CxtI) &&
370 IC.MaskedValueIsZero(
I->getOperand(1),
Mask, 0, CxtI)) {
376 case Instruction::Shl: {
387 case Instruction::LShr: {
393 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
396 IC.MaskedValueIsZero(
I->getOperand(0),
403 case Instruction::AShr: {
411 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
414 OrigBitWidth - BitWidth <
415 IC.ComputeNumSignBits(
I->getOperand(0), 0, CxtI))
420 case Instruction::Trunc:
423 case Instruction::ZExt:
424 case Instruction::SExt:
433 case Instruction::PHI: {
460 if (!TruncOp->
hasOneUse() || !isa<IntegerType>(DestType))
463 Value *VecInput =
nullptr;
468 !isa<VectorType>(VecInput->
getType()))
474 unsigned ShiftAmount = ShiftVal ? ShiftVal->
getZExtValue() : 0;
476 if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0))
481 unsigned NumVecElts = VecWidth / DestWidth;
487 unsigned Elt = ShiftAmount / DestWidth;
489 Elt = NumVecElts - 1 - Elt;
499 "Don't narrow to an illegal scalar type");
507 Value *ShVal, *ShAmt0, *ShAmt1;
512 auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode();
513 auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode();
514 if (ShiftOpcode0 == ShiftOpcode1)
545 Value *NarrowShAmt = Builder.CreateTrunc(ShAmt, DestTy);
546 Value *NegShAmt = Builder.CreateNeg(NarrowShAmt);
550 Value *MaskedShAmt = Builder.CreateAnd(NarrowShAmt, MaskC);
551 Value *MaskedNegShAmt = Builder.CreateAnd(NegShAmt, MaskC);
554 Value *
X = Builder.CreateTrunc(ShVal, DestTy);
555 Value *NarrowShAmt0 = SubIsOnLHS ? MaskedNegShAmt : MaskedShAmt;
556 Value *NarrowShAmt1 = SubIsOnLHS ? MaskedShAmt : MaskedNegShAmt;
557 Value *NarrowSh0 = Builder.CreateBinOp(ShiftOpcode0, X, NarrowShAmt0);
558 Value *NarrowSh1 = Builder.CreateBinOp(ShiftOpcode1, X, NarrowShAmt1);
559 return BinaryOperator::CreateOr(NarrowSh0, NarrowSh1);
568 if (!isa<VectorType>(SrcTy) && !shouldChangeType(SrcTy, DestTy))
578 case Instruction::And:
579 case Instruction::Or:
580 case Instruction::Xor:
582 case Instruction::Sub:
583 case Instruction::Mul: {
588 Value *TruncX = Builder.CreateTrunc(BinOp1, DestTy);
594 Value *TruncX = Builder.CreateTrunc(BinOp0, DestTy);
600 Value *NarrowOp1 = Builder.CreateTrunc(BinOp1, DestTy);
605 Value *NarrowOp0 = Builder.CreateTrunc(BinOp0, DestTy);
626 if (Shuf && Shuf->hasOneUse() && isa<UndefValue>(Shuf->getOperand(1)) &&
627 Shuf->getMask()->getSplatValue() &&
628 Shuf->getType() == Shuf->getOperand(0)->getType()) {
646 assert((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) &&
647 "Unexpected instruction for shrinking");
650 if (!InsElt || !InsElt->hasOneUse())
655 Value *VecOp = InsElt->getOperand(0);
656 Value *ScalarOp = InsElt->getOperand(1);
659 if (isa<UndefValue>(VecOp)) {
681 if ((DestTy->
isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
687 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type" 690 Value *Res = EvaluateInDifferentType(Src, DestTy,
false);
692 return replaceInstUsesWith(CI, Res);
706 if (SimplifyDemandedInstructionBits(CI))
712 Src = Builder.CreateAnd(Src, One);
751 Value *SExt = cast<Instruction>(Src)->getOperand(0);
755 const unsigned MaxAmt = SExtSize -
std::max(CISize, ASize);
763 if (ShiftAmt <= MaxAmt) {
766 std::min(ShiftAmt, ASize - 1)));
768 Value *Shift = Builder.CreateAShr(A, std::min(ShiftAmt, ASize - 1));
784 if (Src->
hasOneUse() && isa<IntegerType>(SrcTy) &&
785 shouldChangeType(SrcTy, DestTy)) {
794 Value *NewTrunc = Builder.CreateTrunc(A, DestTy, A->
getName() +
".tr");
797 Instruction::Shl, NewTrunc,
821 if (!DoTransform)
return ICI;
826 In = Builder.CreateLShr(In, Sh, In->
getName() +
".lobit");
827 if (In->getType() != CI.
getType())
828 In = Builder.CreateIntCast(In, CI.
getType(),
false );
832 In = Builder.CreateXor(In, One, In->
getName() +
".not");
835 return replaceInstUsesWith(CI, In);
853 if (KnownZeroMask.isPowerOf2()) {
854 if (!DoTransform)
return ICI;
857 if (!Op1CV->
isNullValue() && (*Op1CV != KnownZeroMask)) {
861 return replaceInstUsesWith(CI, Res);
864 uint32_t ShAmt = KnownZeroMask.logBase2();
875 In = Builder.CreateXor(In, One);
879 return replaceInstUsesWith(CI, In);
881 Value *IntCast = Builder.CreateIntCast(In, CI.
getType(),
false);
882 return replaceInstUsesWith(CI, IntCast);
898 if (KnownLHS.
Zero == KnownRHS.
Zero && KnownLHS.
One == KnownRHS.
One) {
900 APInt UnknownBit = ~KnownBits;
902 if (!DoTransform)
return ICI;
907 if (KnownLHS.
One.
uge(UnknownBit))
908 Result = Builder.CreateAnd(Result,
912 Result = Builder.CreateLShr(
918 return replaceInstUsesWith(CI, Result);
953 auto *
I = cast<Instruction>(V);
955 switch (
I->getOpcode()) {
956 case Instruction::ZExt:
957 case Instruction::SExt:
958 case Instruction::Trunc:
960 case Instruction::And:
961 case Instruction::Or:
962 case Instruction::Xor:
964 case Instruction::Sub:
965 case Instruction::Mul:
970 if (BitsToClear == 0 && Tmp == 0)
975 if (Tmp == 0 &&
I->isBitwiseLogicOp()) {
979 if (IC.MaskedValueIsZero(
I->getOperand(1),
984 if (
I->getOpcode() == Instruction::And)
993 case Instruction::Shl: {
1001 BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
1006 case Instruction::LShr: {
1030 case Instruction::PHI: {
1068 unsigned BitsToClear;
1069 if ((DestTy->isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
1071 assert(BitsToClear <= SrcTy->getScalarSizeInBits() &&
1072 "Can't clear more bits than in SrcTy");
1076 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type" 1077 " to avoid zero extend: " 1079 Value *Res = EvaluateInDifferentType(Src, DestTy,
false);
1083 if (
auto *SrcOp = dyn_cast<Instruction>(Src))
1084 if (SrcOp->hasOneUse())
1088 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1094 DestBitSize-SrcBitsKept),
1096 return replaceInstUsesWith(CI, Res);
1101 return BinaryOperator::CreateAnd(Res, C);
1107 if (
TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {
1112 Value *A = CSrc->getOperand(0);
1114 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
1120 if (SrcSize < DstSize) {
1123 Value *And = Builder.CreateAnd(A, AndConst, CSrc->
getName() +
".mask");
1127 if (SrcSize == DstSize) {
1132 if (SrcSize > DstSize) {
1135 return BinaryOperator::CreateAnd(Trunc,
1141 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1142 return transformZExtICmp(ICI, CI);
1145 if (SrcI && SrcI->
getOpcode() == Instruction::Or) {
1151 if (LHS && RHS && LHS->
hasOneUse() && RHS->hasOneUse() &&
1152 (transformZExtICmp(LHS, CI,
false) ||
1153 transformZExtICmp(RHS, CI,
false))) {
1156 Value *RCast = Builder.CreateZExt(RHS, CI.
getType(), RHS->getName());
1160 if (
auto *LZExt = dyn_cast<ZExtInst>(LCast))
1161 transformZExtICmp(LHS, *LZExt);
1162 if (
auto *RZExt = dyn_cast<ZExtInst>(RCast))
1163 transformZExtICmp(RHS, *RZExt);
1183 return BinaryOperator::CreateXor(Builder.CreateAnd(X, ZC), ZC);
1195 if (!Op1->getType()->isIntOrIntVectorTy())
1204 Value *
In = Builder.CreateAShr(Op0, Sh, Op0->
getName() +
".lobit");
1205 if (In->getType() != CI.
getType())
1206 In = Builder.CreateIntCast(In, CI.
getType(),
true );
1209 In = Builder.CreateNot(In, In->getName() +
".not");
1210 return replaceInstUsesWith(CI, In);
1213 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1218 ICI->
isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
1226 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
1230 return replaceInstUsesWith(CI, V);
1239 In = Builder.CreateLShr(In,
1244 In = Builder.CreateAdd(In,
1253 In = Builder.CreateShl(In,
1262 return replaceInstUsesWith(CI, In);
1280 "Can't sign extend type to a smaller type");
1286 auto *
I = cast<Instruction>(V);
1287 switch (
I->getOpcode()) {
1288 case Instruction::SExt:
1289 case Instruction::ZExt:
1290 case Instruction::Trunc:
1292 case Instruction::And:
1293 case Instruction::Or:
1294 case Instruction::Xor:
1296 case Instruction::Sub:
1297 case Instruction::Mul:
1309 case Instruction::PHI: {
1342 Value *ZExt = Builder.CreateZExt(Src, DestTy);
1343 return replaceInstUsesWith(CI, ZExt);
1350 if ((DestTy->isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
1354 dbgs() <<
"ICE: EvaluateInDifferentType converting expression type" 1355 " to avoid sign extend: " 1357 Value *Res = EvaluateInDifferentType(Src, DestTy,
true);
1361 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1366 return replaceInstUsesWith(CI, Res);
1370 return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt,
"sext"),
1380 unsigned DestBitSize = DestTy->getScalarSizeInBits();
1382 return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt);
1385 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1386 return transformSExtICmp(ICI, CI);
1409 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1411 A = Builder.CreateShl(A, ShAmtV, CI.
getName());
1412 return BinaryOperator::CreateAShr(A, ShAmtV);
1450 if (!CV || !CV->getType()->isVectorTy())
1453 Type *MinType =
nullptr;
1456 for (
unsigned i = 0; i != NumElts; ++i) {
1457 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
1477 if (
auto *FPExt = dyn_cast<FPExtInst>(V))
1478 return FPExt->getOperand(0)->getType();
1483 if (
auto *CFP = dyn_cast<ConstantFP>(V))
1512 unsigned RHSWidth = RHSMinType->getFPMantissaWidth();
1513 unsigned SrcWidth =
std::max(LHSWidth, RHSWidth);
1517 case Instruction::FAdd:
1518 case Instruction::FSub:
1537 if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1545 case Instruction::FMul:
1551 if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1557 case Instruction::FDiv:
1564 if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1570 case Instruction::FRem: {
1575 if (SrcWidth == OpWidth)
1578 if (LHSWidth == SrcWidth) {
1579 LHS = Builder.CreateFPTrunc(OpI->
getOperand(0), LHSMinType);
1580 RHS = Builder.CreateFPTrunc(OpI->
getOperand(1), LHSMinType);
1582 LHS = Builder.CreateFPTrunc(OpI->
getOperand(0), RHSMinType);
1583 RHS = Builder.CreateFPTrunc(OpI->
getOperand(1), RHSMinType);
1586 Value *ExactResult = Builder.CreateFRemFMF(LHS, RHS, OpI);
1598 if (
auto *II = dyn_cast<IntrinsicInst>(FPT.
getOperand(0))) {
1599 switch (II->getIntrinsicID()) {
1601 case Intrinsic::ceil:
1602 case Intrinsic::fabs:
1603 case Intrinsic::floor:
1604 case Intrinsic::nearbyint:
1605 case Intrinsic::rint:
1607 case Intrinsic::trunc: {
1608 Value *Src = II->getArgOperand(0);
1615 if (II->getIntrinsicID() != Intrinsic::fabs) {
1617 if (!FPExtSrc || FPExtSrc->
getSrcTy() != Ty)
1623 Value *InnerTrunc = Builder.CreateFPTrunc(Src, Ty);
1625 II->getIntrinsicID(), Ty);
1627 II->getOperandBundlesAsDefs(OpBundles);
1643 return commonCastTransforms(CI);
1658 Type *SrcTy = SrcI->getType();
1659 bool IsInputSigned = isa<SIToFPInst>(OpI);
1660 bool IsOutputSigned = isa<FPToSIInst>(FI);
1673 int ActualSize = std::min(InputSize, OutputSize);
1675 if (ActualSize <= OpITy->getFPMantissaWidth()) {
1677 if (IsInputSigned && IsOutputSigned)
1684 return replaceInstUsesWith(FI, SrcI);
1693 return commonCastTransforms(FI);
1698 return commonCastTransforms(FI);
1704 return commonCastTransforms(FI);
1709 return commonCastTransforms(FI);
1713 return commonCastTransforms(CI);
1717 return commonCastTransforms(CI);
1726 DL.getPointerSizeInBits(AS)) {
1748 if (
GEP->hasAllZeroIndices() &&
1752 (!isa<AddrSpaceCastInst>(CI) ||
1753 GEP->getType() ==
GEP->getPointerOperandType())) {
1763 return commonCastTransforms(CI);
1775 return commonPointerCastTransforms(CI);
1829 for (
unsigned i = 0, e = SrcElts; i != e; ++i)
1833 for (
unsigned i = 0, e = DestTy->
getNumElements()-SrcElts; i != e; ++i)
1862 Type *VecEltTy,
bool isBigEndian) {
1864 "Shift should be a multiple of the element type size");
1867 if (isa<UndefValue>(V))
return true;
1871 if (V->
getType() == VecEltTy) {
1873 if (
Constant *
C = dyn_cast<Constant>(V))
1874 if (
C->isNullValue())
1879 ElementIndex = Elements.
size() - ElementIndex - 1;
1882 if (Elements[ElementIndex])
1885 Elements[ElementIndex] = V;
1889 if (
Constant *
C = dyn_cast<Constant>(V)) {
1898 Shift, Elements, VecEltTy, isBigEndian);
1902 if (!isa<IntegerType>(
C->
getType()))
1908 for (
unsigned i = 0; i != NumElts; ++i) {
1909 unsigned ShiftI = Shift+i*ElementSize;
1923 if (!I)
return false;
1925 default:
return false;
1926 case Instruction::BitCast:
1929 case Instruction::ZExt:
1936 case Instruction::Or:
1941 case Instruction::Shl: {
1944 if (!CI)
return false;
1984 for (
unsigned i = 0, e = Elements.size(); i != e; ++i) {
1985 if (!Elements[i])
continue;
2002 if (!ExtElt || !ExtElt->hasOneUse())
2011 unsigned NumElts = ExtElt->getVectorOperandType()->getNumElements();
2036 X->
getType() == DestTy && !isa<Constant>(
X)) {
2043 X->
getType() == DestTy && !isa<Constant>(
X)) {
2067 Value *Cond, *TVal, *FVal;
2089 auto *Sel = cast<Instruction>(BitCast.
getOperand(0));
2092 !isa<Constant>(
X)) {
2099 !isa<Constant>(
X)) {
2111 if (!isa<StoreInst>(U))
2143 while (!PhiWorklist.
empty()) {
2145 for (
Value *IncValue : OldPN->incoming_values()) {
2146 if (isa<Constant>(IncValue))
2149 if (
auto *LI = dyn_cast<LoadInst>(IncValue)) {
2154 Value *Addr = LI->getOperand(0);
2155 if (Addr == &CI || isa<LoadInst>(Addr))
2157 if (LI->hasOneUse() && LI->isSimple())
2164 if (
auto *PNode = dyn_cast<PHINode>(IncValue)) {
2165 if (OldPhiNodes.
insert(PNode))
2176 Type *TyA = BCI->getOperand(0)->getType();
2177 Type *TyB = BCI->getType();
2178 if (TyA != DestTy || TyB != SrcTy)
2185 for (
auto *OldPN : OldPhiNodes) {
2186 Builder.SetInsertPoint(OldPN);
2187 PHINode *NewPN = Builder.CreatePHI(DestTy, OldPN->getNumOperands());
2188 NewPNodes[OldPN] = NewPN;
2192 for (
auto *OldPN : OldPhiNodes) {
2193 PHINode *NewPN = NewPNodes[OldPN];
2194 for (
unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
2195 Value *V = OldPN->getOperand(j);
2196 Value *NewV =
nullptr;
2197 if (
auto *
C = dyn_cast<Constant>(V)) {
2199 }
else if (
auto *LI = dyn_cast<LoadInst>(V)) {
2200 Builder.SetInsertPoint(LI->getNextNode());
2201 NewV = Builder.CreateBitCast(LI, DestTy);
2203 }
else if (
auto *BCI = dyn_cast<BitCastInst>(V)) {
2204 NewV = BCI->getOperand(0);
2205 }
else if (
auto *PrevPN = dyn_cast<PHINode>(V)) {
2206 NewV = NewPNodes[PrevPN];
2209 NewPN->
addIncoming(NewV, OldPN->getIncomingBlock(j));
2216 if (
SI &&
SI->isSimple() &&
SI->getOperand(0) == PN) {
2217 Builder.SetInsertPoint(
SI);
2219 cast<BitCastInst>(Builder.CreateBitCast(NewPNodes[PN], SrcTy));
2220 SI->setOperand(0, NewBC);
2226 return replaceInstUsesWith(CI, NewPNodes[PN]);
2239 return replaceInstUsesWith(CI, Src);
2241 if (
PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
2243 Type *DstElTy = DstPTy->getElementType();
2248 if ((DstElTy == SrcElTy) &&
2256 if (
AllocaInst *AI = dyn_cast<AllocaInst>(Src))
2257 if (
Instruction *V = PromoteCastOfAllocation(CI, *AI))
2270 unsigned NumZeros = 0;
2271 while (SrcElTy != DstElTy &&
2272 isa<CompositeType>(SrcElTy) && !SrcElTy->
isPointerTy() &&
2274 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(0U);
2279 if (SrcElTy == DstElTy) {
2285 if (
VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
2286 if (DestVTy->getNumElements() == 1 && !SrcTy->
isVectorTy()) {
2287 Value *Elem = Builder.CreateBitCast(Src, DestVTy->getElementType());
2293 if (isa<IntegerType>(SrcTy)) {
2297 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
2298 CastInst *SrcCast = cast<CastInst>(Src);
2300 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
2302 cast<VectorType>(DestTy), *
this))
2310 return replaceInstUsesWith(CI, V);
2314 if (
VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
2315 if (SrcVTy->getNumElements() == 1) {
2320 Builder.CreateExtractElement(Src,
2328 dyn_cast<InsertElementInst>(CI.
getOperand(0)))
2337 if (SVI->hasOneUse() && DestTy->
isVectorTy() &&
2339 SVI->getType()->getNumElements() ==
2340 SVI->getOperand(0)->getType()->getVectorNumElements()) {
2345 if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
2347 ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
2349 Value *LHS = Builder.CreateBitCast(SVI->getOperand(0), DestTy);
2350 Value *RHS = Builder.CreateBitCast(SVI->getOperand(1), DestTy);
2359 if (
PHINode *PN = dyn_cast<PHINode>(Src))
2373 return commonPointerCastTransforms(CI);
2374 return commonCastTransforms(CI);
2385 Type *DestElemTy = DestTy->getElementType();
2393 Value *NewBitCast = Builder.CreateBitCast(Src, MidTy);
2397 return commonPointerCastTransforms(CI);
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, BinaryOperator *FMFSource, const Twine &Name="")
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
void push_back(const T &Elt)
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This class is the base class for the comparison instructions.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static Type * getDoubleTy(LLVMContext &C)
Type * getSrcTy() const
Return the source type, as a convenience.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
uint64_t getZExtValue() const
Get zero extended value.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
DiagnosticInfoOptimizationBase::Argument NV
Compute iterated dominance frontiers using a linear time algorithm.
BinaryOps getOpcode() const
Instruction * visitBitCast(BitCastInst &CI)
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
void setAlignment(unsigned Align)
This class represents zero extension of integer types.
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
This class represents a function call, abstracting a target machine's calling convention.
static Type * shrinkFPConstant(ConstantFP *CFP)
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...
static uint64_t round(uint64_t Acc, uint64_t Input)
const Value * getTrueValue() const
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
APInt trunc(unsigned width) const
Truncate to new width.
static CallInst * Create(Value *Func, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This class represents a sign extension of integer types.
bool isVectorTy() const
True if this is an instance of VectorType.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
Instruction * visitUIToFP(CastInst &CI)
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
unsigned getBitWidth() const
Return the number of bits in the APInt.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Instruction * visitFPExt(CastInst &CI)
Instruction * FoldItoFPtoI(Instruction &FI)
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
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.
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
static bool hasStoreUsersOnly(CastInst &CI)
Check if all users of CI are StoreInsts.
static Type * getFloatTy(LLVMContext &C)
This is the base class for all instructions that perform data casts.
PointerType * getType() const
Overload to return most specific pointer type.
'undef' values are things that do not have specified contents.
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.
bool isIntegerTy() const
True if this is an instance of IntegerType.
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...
static Instruction * canonicalizeBitCastExtElt(BitCastInst &BitCast, InstCombiner &IC)
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract e...
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)
uint64_t getNumElements() const
static Type * getPPC_FP128Ty(LLVMContext &C)
static unsigned getTypeSizeIndex(unsigned Value, Type *Ty)
This class represents a cast from a pointer to an integer.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
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)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
static bool isMultipleOfTypeSize(unsigned Value, Type *Ty)
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Type * getType() const
All values are typed, get the type of this value.
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.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
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.
This class represents a no-op cast from one type to another.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
const APInt & getValue() const
Return the constant as an APInt value reference.
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Get a value with upper bits starting at loBit set.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
An instruction for storing to memory.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
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...
static const fltSemantics & IEEEdouble() LLVM_READNONE
bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
static Type * getMinimumFPType(Value *V)
Find the minimum FP type we can safely truncate to.
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.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
This class represents a truncation of integer types.
Value * getOperand(unsigned i) const
Class to represent pointers.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
const DataLayout & getDataLayout() const
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
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.
bool isAllOnesValue() const
Determine if all bits are set.
static bool canNotEvaluateInType(Value *V, Type *Ty)
Filter out values that we can not evaluate in the destination type for free.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
unsigned countPopulation() const
Count the number of bits set.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
bool ult(const APInt &RHS) const
Unsigned less than comparison.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
This is an important base class in LLVM.
unsigned getNumContainedTypes() const
Return the number of types in the derived type.
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
bool isPointerTy() const
True if this is an instance of PointerType.
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.
ConstantFP - Floating Point Values [float, double].
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
static Value * decomposeSimpleLinearExpr(Value *Val, unsigned &Scale, uint64_t &Offset)
Analyze 'Val', seeing if it is a simple linear expression.
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 operators which may exhibit overflow - Add, Sub, Mul, and Shl...
match_combine_or< CastClass_match< OpTy, Instruction::ZExt >, CastClass_match< OpTy, Instruction::SExt > > m_ZExtOrSExt(const OpTy &Op)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
Class to represent integer types.
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...
This class represents a cast from an integer to a pointer.
static Constant * getAllOnesValue(Type *Ty)
Instruction * visitFPToSI(FPToSIInst &FI)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
const Value * getArraySize() const
Get the number of elements allocated.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
const APFloat & getValueAPF() const
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static Type * getHalfTy(LLVMContext &C)
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static const fltSemantics & IEEEsingle() LLVM_READNONE
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.
static const fltSemantics & IEEEhalf() LLVM_READNONE
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...
static Instruction * shrinkInsertElt(CastInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of an insert element.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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="")
static Instruction * shrinkSplatShuffle(TruncInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of a splat shuffle.
Instruction * visitSExt(SExtInst &CI)
This class represents a cast from floating point to unsigned integer.
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...
Type * getDestTy() const
Return the destination type, as a convenience.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, BinaryOperator *FMFSource, const Twine &Name="")
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned getVectorNumElements() const
Class to represent vector types.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
iterator_range< user_iterator > users()
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
const Value * getFalseValue() const
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 ...
Predicate getPredicate() const
Return the predicate for this instruction.
Instruction * visitTrunc(TruncInst &CI)
static Type * shrinkFPConstantVector(Value *V)
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM_NODISCARD bool empty() const
Instruction * visitSIToFP(CastInst &CI)
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
StringRef getName() const
Return a constant reference to the value's name.
static bool isSigned(Value *V)
Can the given value generate sign bits.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
static Instruction * foldVecTruncToExtElt(TruncInst &Trunc, InstCombiner &IC)
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated...
static BinaryOperator * CreateFNegFMF(Value *Op, BinaryOperator *FMFSource, const Twine &Name="")
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
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...
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...
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)
Type * getElementType() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
static bool canAlwaysEvaluateInType(Value *V, Type *Ty)
Constants and extensions/truncates from the destination type are always free to be evaluated in that ...
bool isNonNegative() const
Returns true if this value is known to be non-negative.
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
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)
op_range incoming_values()
This class represents an extension of floating point types.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
Type * getElementType() const
bool isNullValue() const
Determine if all bits are clear.
an instruction to allocate memory on the stack
Instruction * visitFPTrunc(FPTruncInst &CI)
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property...