49 "disable-i2p-p2i-opt",
cl::init(
false),
50 cl::desc(
"Disables inttoptr/ptrtoint roundtrip optimization"));
63 assert(!Size.isScalable() &&
"Array elements cannot have a scalable size");
64 Size *=
C->getZExtValue();
77 return "both values to select must have same type";
80 return "select values cannot have token type";
85 return "vector select condition element type must be i1";
88 return "selected values for vector select must be vectors";
90 return "vector select requires selected vectors to have "
91 "the same vector length as select condition";
93 return "select condition must be i1 or <n x i1>";
102 PHINode::PHINode(
const PHINode &PN)
104 ReservedSpace(PN.getNumOperands()) {
125 Op<-1>().
set(
nullptr);
141 void PHINode::growOperands() {
143 unsigned NumOps =
e +
e / 2;
144 if (NumOps < 2) NumOps = 2;
146 ReservedSpace = NumOps;
157 if (ConstantValue !=
this)
162 if (ConstantValue ==
this)
164 return ConstantValue;
173 Value *ConstantValue =
nullptr;
176 if (Incoming !=
this && !isa<UndefValue>(Incoming)) {
177 if (ConstantValue && ConstantValue != Incoming)
179 ConstantValue = Incoming;
189 LandingPadInst::LandingPadInst(
Type *RetTy,
unsigned NumReservedValues,
192 init(NumReservedValues, NameStr);
195 LandingPadInst::LandingPadInst(
Type *RetTy,
unsigned NumReservedValues,
198 init(NumReservedValues, NameStr);
203 LP.getNumOperands()),
204 ReservedSpace(LP.getNumOperands()) {
208 for (
unsigned I = 0,
E = ReservedSpace;
I !=
E; ++
I)
215 const Twine &NameStr,
217 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
221 const Twine &NameStr,
223 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
226 void LandingPadInst::init(
unsigned NumReservedValues,
const Twine &NameStr) {
227 ReservedSpace = NumReservedValues;
236 void LandingPadInst::growOperands(
unsigned Size) {
238 if (ReservedSpace >=
e + Size)
return;
239 ReservedSpace = (
std::max(
e, 1U) + Size / 2) * 2;
246 assert(OpNo < ReservedSpace &&
"Growing didn't work!");
260 case Instruction::Invoke:
262 case Instruction::CallBr:
274 if (ChildOB.getTagName() != OpB.
getTag())
286 return cast<CallBrInst>(
this)->getNumIndirectDests() + 1;
291 if (isa<Function>(V) || isa<Constant>(V))
299 if (
auto *CI = dyn_cast<CallInst>(
this))
300 return CI->isMustTailCall();
306 if (
auto *CI = dyn_cast<CallInst>(
this))
307 return CI->isTailCall();
313 return F->getIntrinsicID();
334 if (
F->getAttributes().hasAttrSomewhere(
Kind, &
Index))
347 return F->getAttributes().hasParamAttr(ArgNo,
Kind);
353 if (
auto *CE = dyn_cast<ConstantExpr>(V))
354 if (CE->getOpcode() == BitCast)
355 V = CE->getOperand(0);
357 if (
auto *
F = dyn_cast<Function>(V))
358 return F->getAttributes().hasFnAttr(
Kind);
363 bool CallBase::hasFnAttrOnCalledFunction(
StringRef Kind)
const {
365 if (
auto *CE = dyn_cast<ConstantExpr>(V))
366 if (CE->getOpcode() == BitCast)
367 V = CE->getOperand(0);
369 if (
auto *
F = dyn_cast<Function>(V))
370 return F->getAttributes().hasFnAttr(
Kind);
375 template <
typename AK>
376 Attribute CallBase::getFnAttrOnCalledFunction(AK
Kind)
const {
382 if (
auto *CE = dyn_cast<ConstantExpr>(V))
383 if (
CE->getOpcode() == BitCast)
384 V =
CE->getOperand(0);
386 if (
auto *
F = dyn_cast<Function>(V))
387 return F->getAttributes().getFnAttr(
Kind);
404 const unsigned BeginIndex) {
406 for (
auto &
B : Bundles)
407 It =
std::copy(
B.input_begin(),
B.input_end(), It);
410 auto BI = Bundles.begin();
411 unsigned CurrentIndex = BeginIndex;
414 assert(BI != Bundles.end() &&
"Incorrect allocation?");
416 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
417 BOI.Begin = CurrentIndex;
418 BOI.End = CurrentIndex + BI->input_size();
419 CurrentIndex = BOI.End;
423 assert(BI == Bundles.end() &&
"Incorrect allocation?");
434 if (BOI.Begin <= OpIdx && OpIdx < BOI.End)
440 assert(OpIdx >=
arg_size() &&
"the Idx is not in the operand bundles");
443 "The Idx isn't in the operand bundle");
447 constexpr
unsigned NumberScaling = 1024;
453 while (Begin != End) {
454 unsigned ScaledOperandPerBundle =
455 NumberScaling * (std::prev(End)->End - Begin->
Begin) / (End - Begin);
456 Current = Begin + (((OpIdx - Begin->
Begin) * NumberScaling) /
457 ScaledOperandPerBundle);
459 Current = std::prev(End);
460 assert(Current < End && Current >= Begin &&
461 "the operand bundle doesn't cover every value in the range");
462 if (OpIdx >= Current->
Begin && OpIdx < Current->End)
464 if (OpIdx >= Current->
End)
470 assert(OpIdx >= Current->
Begin && OpIdx < Current->End &&
471 "the operand bundle doesn't cover every value in the range");
483 Bundles.push_back(
OB);
484 return Create(CB, Bundles, InsertPt);
490 bool CreateNew =
false;
494 if (Bundle.getTagID() ==
ID) {
501 return CreateNew ?
Create(CB, Bundles, InsertPt) : CB;
520 "NumOperands not set up?");
525 "Calling a function with bad signature!");
527 for (
unsigned i = 0;
i !=
Args.size(); ++
i)
530 "Calling a function with a bad signature!");
569 CallInst::CallInst(
const CallInst &CI)
572 CI.getNumOperands()) {
600 auto *ProfileData =
getMetadata(LLVMContext::MD_prof);
601 if (ProfileData ==
nullptr)
604 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
605 if (!ProfDataName || (!ProfDataName->getString().equals(
"branch_weights") &&
606 !ProfDataName->getString().equals(
"VP")))
610 LLVM_DEBUG(
dbgs() <<
"Attempting to update profile weights will result in "
611 "div by 0. Ignoring. Likely the function "
613 <<
" has 0 entry count, and contains call instructions "
614 "with non-zero prof info.");
620 Vals.push_back(ProfileData->getOperand(0));
621 APInt APS(128,
S), APT(128,
T);
622 if (ProfDataName->getString().equals(
"branch_weights") &&
623 ProfileData->getNumOperands() > 0) {
625 APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1))
632 }
else if (ProfDataName->getString().equals(
"VP"))
633 for (
unsigned i = 1;
i < ProfileData->getNumOperands();
i += 2) {
635 Vals.push_back(ProfileData->getOperand(
i));
637 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(
i + 1))
642 Vals.push_back(ProfileData->getOperand(
i + 1));
646 APInt Val(128, Count);
657 assert(
val &&
"IsConstantOne does not work with nullptr val");
659 return CVal && CVal->
isOne();
668 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
669 "createMalloc needs either InsertBefore or InsertAtEnd");
677 else if (ArraySize->
getType() != IntPtrTy) {
688 AllocSize = ArraySize;
689 }
else if (
Constant *CO = dyn_cast<Constant>(ArraySize)) {
698 "mallocsize", InsertBefore);
701 "mallocsize", InsertAtEnd);
705 assert(AllocSize->
getType() == IntPtrTy &&
"malloc arg is wrong size");
708 Module *
M =
BB->getParent()->getParent();
713 MallocFunc =
M->getOrInsertFunction(
"malloc", BPTy, IntPtrTy);
721 if (Result->getType() != AllocPtrType)
727 if (Result->getType() != AllocPtrType) {
736 if (!
F->returnDoesNotAlias())
737 F->setReturnDoesNotAlias();
755 return createMalloc(InsertBefore,
nullptr, IntPtrTy, AllocTy, AllocSize,
764 return createMalloc(InsertBefore,
nullptr, IntPtrTy, AllocTy, AllocSize,
765 ArraySize, OpB, MallocF,
Name);
780 return createMalloc(
nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
788 return createMalloc(
nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
789 ArraySize, OpB, MallocF,
Name);
796 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
797 "createFree needs either InsertBefore or InsertAtEnd");
799 "Can not free something of nonpointer type!");
802 Module *
M =
BB->getParent()->getParent();
807 FunctionCallee FreeFunc =
M->getOrInsertFunction(
"free", VoidTy, IntPtrTy);
811 if (
Source->getType() != IntPtrTy)
815 if (
Source->getType() != IntPtrTy)
819 Result->setTailCall();
821 Result->setCallingConv(
F->getCallingConv());
841 assert(FreeCall &&
"CreateFree did not create a CallInst");
848 assert(FreeCall &&
"CreateFree did not create a CallInst");
859 const Twine &NameStr) {
864 "NumOperands not set up?");
869 "Invoking a function with bad signature");
871 for (
unsigned i = 0,
e =
Args.size();
i !=
e;
i++)
874 "Invoking a function with a bad signature!");
894 II.getNumOperands()) {
917 return cast<LandingPadInst>(
getUnwindDest()->getFirstNonPHI());
928 const Twine &NameStr) {
932 ComputeNumOperands(
Args.size(), IndirectDests.
size(),
934 "NumOperands not set up?");
939 "Calling a function with bad signature");
941 for (
unsigned i = 0,
e =
Args.size();
i !=
e;
i++)
944 "Calling a function with a bad signature!");
950 NumIndirectDests = IndirectDests.
size();
952 for (
unsigned i = 0;
i != NumIndirectDests; ++
i)
963 void CallBrInst::updateArgBlockAddresses(
unsigned i,
BasicBlock *
B) {
968 for (
unsigned ArgNo = 0,
e =
arg_size(); ArgNo !=
e; ++ArgNo)
977 CBI.getNumOperands()) {
983 NumIndirectDests = CBI.NumIndirectDests;
997 NewCBI->NumIndirectDests = CBI->NumIndirectDests;
1008 RI.getNumOperands()) {
1010 Op<0>() = RI.
Op<0>();
1041 Op<0>() = RI.
Op<0>();
1063 CRI.getNumOperands(),
1064 CRI.getNumOperands()) {
1065 setSubclassData<Instruction::OpaqueField>(
1067 Op<0>() = CRI.
Op<0>();
1069 Op<1>() = CRI.
Op<1>();
1072 void CleanupReturnInst::init(
Value *CleanupPad,
BasicBlock *UnwindBB) {
1074 setSubclassData<UnwindDestField>(
true);
1076 Op<0>() = CleanupPad;
1081 CleanupReturnInst::CleanupReturnInst(
Value *CleanupPad,
BasicBlock *UnwindBB,
1086 Values, InsertBefore) {
1087 init(CleanupPad, UnwindBB);
1090 CleanupReturnInst::CleanupReturnInst(
Value *CleanupPad,
BasicBlock *UnwindBB,
1095 Values, InsertAtEnd) {
1096 init(CleanupPad, UnwindBB);
1110 Op<0>() = CRI.
Op<0>();
1111 Op<1>() = CRI.
Op<1>();
1134 CatchSwitchInst::CatchSwitchInst(
Value *ParentPad,
BasicBlock *UnwindDest,
1135 unsigned NumReservedValues,
1136 const Twine &NameStr,
1141 ++NumReservedValues;
1142 init(ParentPad, UnwindDest, NumReservedValues + 1);
1146 CatchSwitchInst::CatchSwitchInst(
Value *ParentPad,
BasicBlock *UnwindDest,
1147 unsigned NumReservedValues,
1152 ++NumReservedValues;
1153 init(ParentPad, UnwindDest, NumReservedValues + 1);
1159 CSI.getNumOperands()) {
1164 for (
unsigned I = 1,
E = ReservedSpace;
I !=
E; ++
I)
1169 unsigned NumReservedValues) {
1170 assert(ParentPad && NumReservedValues);
1172 ReservedSpace = NumReservedValues;
1176 Op<0>() = ParentPad;
1178 setSubclassData<UnwindDestField>(
true);
1185 void CatchSwitchInst::growOperands(
unsigned Size) {
1187 assert(NumOperands >= 1);
1188 if (ReservedSpace >= NumOperands + Size)
1190 ReservedSpace = (NumOperands +
Size / 2) * 2;
1197 assert(OpNo < ReservedSpace &&
"Growing didn't work!");
1205 for (
Use *CurDst =
HI.getCurrent(); CurDst != EndDst; ++CurDst)
1206 *CurDst = *(CurDst + 1);
1217 const Twine &NameStr) {
1227 FPI.getNumOperands(),
1228 FPI.getNumOperands()) {
1239 init(ParentPad,
Args, NameStr);
1248 init(ParentPad,
Args, NameStr);
1267 void BranchInst::AssertOK() {
1270 "May only branch on boolean predicates!");
1277 assert(IfTrue &&
"Branch destination may not be null!");
1298 assert(IfTrue &&
"Branch destination may not be null!");
1318 BI.getNumOperands()) {
1322 Op<-3>() = BI.
Op<-3>();
1323 Op<-2>() = BI.
Op<-2>();
1325 Op<-1>() = BI.
Op<-1>();
1331 "Cannot swap successors of an unconditional branch");
1347 assert(!isa<BasicBlock>(Amt) &&
1348 "Passed basic block into allocation size parameter! Use other ctor");
1350 "Allocation array size is not an integer!");
1356 assert(
BB &&
"Insertion BB cannot be null when alignment not provided!");
1358 "BB must be in a Function when alignment not provided!");
1360 return DL.getPrefTypeAlign(Ty);
1364 assert(
I &&
"Insertion position cannot be null when alignment not provided!");
1392 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1402 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1412 return !CI->isOne();
1432 void LoadInst::AssertOK() {
1434 "Ptr must have pointer type.");
1438 assert(
BB &&
"Insertion BB cannot be null when alignment not provided!");
1440 "BB must be in a Function when alignment not provided!");
1442 return DL.getABITypeAlign(Ty);
1446 assert(
I &&
"Insertion position cannot be null when alignment not provided!");
1471 SyncScope::
System, InsertBef) {}
1476 SyncScope::
System, InsertAE) {}
1482 assert(cast<PointerType>(Ptr->
getType())->isOpaqueOrPointeeTypeMatches(Ty));
1494 assert(cast<PointerType>(Ptr->
getType())->isOpaqueOrPointeeTypeMatches(Ty));
1506 void StoreInst::AssertOK() {
1509 "Ptr must have pointer type!");
1512 "Ptr must be a pointer to Val type!");
1536 SyncScope::
System, InsertBefore) {}
1541 SyncScope::
System, InsertAtEnd) {}
1589 "All operands must be non-null!");
1591 "Ptr must have pointer type!");
1594 "Ptr must be a pointer to Cmp type!");
1597 "Ptr must be a pointer to NewVal type!");
1599 "Cmp type and NewVal type must be same!");
1612 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID);
1625 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID);
1643 "All operands must be non-null!");
1645 "Ptr must have pointer type!");
1648 "Ptr must be a pointer to Val type!");
1650 "AtomicRMW instructions must be atomic!");
1700 return "<invalid operation>";
1733 "NumOperands not initialized?");
1742 GEPI.getNumOperands(),
1743 GEPI.getNumOperands()),
1744 SourceElementType(GEPI.SourceElementType),
1745 ResultElementType(GEPI.ResultElementType) {
1751 if (
auto *Struct = dyn_cast<StructType>(Ty)) {
1752 if (!Struct->indexValid(Idx))
1754 return Struct->getTypeAtIndex(Idx);
1758 if (
auto *Array = dyn_cast<ArrayType>(Ty))
1759 return Array->getElementType();
1760 if (
auto *
Vector = dyn_cast<VectorType>(Ty))
1761 return Vector->getElementType();
1766 if (
auto *Struct = dyn_cast<StructType>(Ty)) {
1767 if (Idx >= Struct->getNumElements())
1769 return Struct->getElementType(Idx);
1771 if (
auto *Array = dyn_cast<ArrayType>(Ty))
1772 return Array->getElementType();
1773 if (
auto *
Vector = dyn_cast<VectorType>(Ty))
1774 return Vector->getElementType();
1778 template <
typename IndexTy>
1780 if (IdxList.
empty())
1782 for (IndexTy V : IdxList.
slice(1)) {
1809 if (!CI->isZero())
return false;
1829 cast<GEPOperator>(
this)->setIsInBounds(
B);
1833 return cast<GEPOperator>(
this)->isInBounds();
1837 APInt &Offset)
const {
1839 return cast<GEPOperator>(
this)->accumulateConstantOffset(
DL, Offset);
1845 APInt &ConstantOffset)
const {
1847 return cast<GEPOperator>(
this)->collectOffset(
DL,
BitWidth, VariableOffsets,
1863 "Invalid extractelement instruction operands!");
1877 "Invalid extractelement instruction operands!");
1901 "Invalid insertelement instruction operands!");
1915 "Invalid insertelement instruction operands!");
1928 if (Elt->
getType() != cast<VectorType>(Vec->
getType())->getElementType())
1931 if (!
Index->getType()->isIntegerTy())
1941 assert(V &&
"Cannot create placeholder of nullptr V");
1975 "Invalid shuffle vector instruction operands!");
1993 "Invalid shuffle vector instruction operands!");
2012 "Invalid shuffle vector instruction operands!");
2027 "Invalid shuffle vector instruction operands!");
2036 int NumOpElts = cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2037 int NumMaskElts = ShuffleMask.size();
2039 for (
int i = 0;
i != NumMaskElts; ++
i) {
2045 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts &&
"Out-of-range mask");
2046 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts;
2047 NewMask[
i] = MaskElt;
2050 Op<0>().swap(Op<1>());
2061 cast<VectorType>(V1->
getType())->getElementCount().getKnownMinValue();
2062 for (
int Elem :
Mask)
2066 if (isa<ScalableVectorType>(V1->
getType()))
2081 auto *MaskTy = dyn_cast<VectorType>(
Mask->getType());
2082 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) ||
2083 isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->
getType()))
2087 if (isa<UndefValue>(
Mask) || isa<ConstantAggregateZero>(
Mask))
2090 if (
const auto *MV = dyn_cast<ConstantVector>(
Mask)) {
2091 unsigned V1Size = cast<FixedVectorType>(V1->
getType())->getNumElements();
2092 for (
Value *
Op : MV->operands()) {
2093 if (
auto *CI = dyn_cast<ConstantInt>(
Op)) {
2094 if (CI->uge(V1Size*2))
2096 }
else if (!isa<UndefValue>(
Op)) {
2103 if (
const auto *CDS = dyn_cast<ConstantDataSequential>(
Mask)) {
2104 unsigned V1Size = cast<FixedVectorType>(V1->
getType())->getNumElements();
2105 for (
unsigned i = 0,
e = cast<FixedVectorType>(MaskTy)->getNumElements();
2107 if (CDS->getElementAsInteger(
i) >= V1Size*2)
2119 if (isa<ConstantAggregateZero>(
Mask)) {
2120 Result.resize(EC.getKnownMinValue(), 0);
2124 Result.reserve(EC.getKnownMinValue());
2126 if (EC.isScalable()) {
2127 assert((isa<ConstantAggregateZero>(
Mask) || isa<UndefValue>(
Mask)) &&
2128 "Scalable vector shuffle mask must be undef or zeroinitializer");
2129 int MaskVal = isa<UndefValue>(
Mask) ? -1 : 0;
2130 for (
unsigned I = 0;
I < EC.getKnownMinValue(); ++
I)
2131 Result.emplace_back(MaskVal);
2135 unsigned NumElts = EC.getKnownMinValue();
2137 if (
auto *CDS = dyn_cast<ConstantDataSequential>(
Mask)) {
2138 for (
unsigned i = 0;
i != NumElts; ++
i)
2139 Result.push_back(CDS->getElementAsInteger(
i));
2142 for (
unsigned i = 0;
i != NumElts; ++
i) {
2144 Result.push_back(isa<UndefValue>(
C) ? -1 :
2145 cast<ConstantInt>(
C)->getZExtValue());
2157 if (isa<ScalableVectorType>(ResultTy)) {
2165 for (
int Elem :
Mask) {
2175 assert(!
Mask.empty() &&
"Shuffle mask must contain elements");
2176 bool UsesLHS =
false;
2177 bool UsesRHS =
false;
2178 for (
int I :
Mask) {
2181 assert(
I >= 0 &&
I < (NumOpElts * 2) &&
2182 "Out-of-bounds shuffle mask element");
2183 UsesLHS |= (
I < NumOpElts);
2184 UsesRHS |= (
I >= NumOpElts);
2185 if (UsesLHS && UsesRHS)
2189 return UsesLHS || UsesRHS;
2201 for (
int i = 0, NumMaskElts =
Mask.size();
i < NumMaskElts; ++
i) {
2221 int NumElts =
Mask.size();
2225 for (
int i = 0;
i < NumElts; ++
i) {
2228 if (
Mask[
i] != (NumElts - 1 -
i) &&
Mask[
i] != (NumElts + NumElts - 1 -
i))
2237 for (
int i = 0, NumElts =
Mask.size();
i < NumElts; ++
i) {
2250 for (
int i = 0, NumElts =
Mask.size();
i < NumElts; ++
i) {
2267 int NumElts =
Mask.size();
2277 if ((
Mask[1] -
Mask[0]) != NumElts)
2282 for (
int i = 2;
i < NumElts; ++
i) {
2283 int MaskEltVal =
Mask[
i];
2284 if (MaskEltVal == -1)
2286 int MaskEltPrevVal =
Mask[
i - 2];
2287 if (MaskEltVal - MaskEltPrevVal != 2)
2294 int NumSrcElts,
int &
Index) {
2300 if (NumSrcElts <= (
int)
Mask.size())
2305 for (
int i = 0,
e =
Mask.size();
i !=
e; ++
i) {
2309 int Offset = (
M % NumSrcElts) -
i;
2310 if (0 <= SubIndex && SubIndex != Offset)
2315 if (0 <= SubIndex && SubIndex + (
int)
Mask.size() <= NumSrcElts) {
2323 int NumSrcElts,
int &NumSubElts,
2325 int NumMaskElts =
Mask.size();
2328 if (NumMaskElts < NumSrcElts)
2339 bool Src0Identity =
true;
2340 bool Src1Identity =
true;
2342 for (
int i = 0;
i != NumMaskElts; ++
i) {
2348 if (
M < NumSrcElts) {
2350 Src0Identity &= (
M ==
i);
2354 Src1Identity &= (
M == (
i + NumSrcElts));
2356 assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() &&
2357 "unknown shuffle elements");
2359 "2-source shuffle not found");
2371 int NumSub1Elts = Src1Hi - Src1Lo;
2374 NumSubElts = NumSub1Elts;
2383 int NumSub0Elts = Src0Hi - Src0Lo;
2386 NumSubElts = NumSub0Elts;
2396 if (isa<UndefValue>(Op<2>()))
2401 if (isa<ScalableVectorType>(
getType()))
2404 int NumOpElts = cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2405 int NumMaskElts = cast<FixedVectorType>(
getType())->getNumElements();
2406 if (NumMaskElts <= NumOpElts)
2415 for (
int i = NumOpElts;
i < NumMaskElts; ++
i)
2423 if (isa<UndefValue>(Op<2>()))
2428 if (isa<ScalableVectorType>(
getType()))
2431 int NumOpElts = cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2432 int NumMaskElts = cast<FixedVectorType>(
getType())->getNumElements();
2433 if (NumMaskElts >= NumOpElts)
2441 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) ||
2442 isa<UndefValue>(Op<2>()))
2447 if (isa<ScalableVectorType>(
getType()))
2450 int NumOpElts = cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2451 int NumMaskElts = cast<FixedVectorType>(
getType())->getNumElements();
2452 if (NumMaskElts != NumOpElts * 2)
2463 int ReplicationFactor,
int VF) {
2464 assert(
Mask.size() == (
unsigned)ReplicationFactor * VF &&
2465 "Unexpected mask size.");
2467 for (
int CurrElt :
seq(0, VF)) {
2469 assert(CurrSubMask.
size() == (
unsigned)ReplicationFactor &&
2470 "Run out of mask?");
2471 Mask =
Mask.drop_front(ReplicationFactor);
2472 if (!
all_of(CurrSubMask, [CurrElt](
int MaskElt) {
2477 assert(
Mask.empty() &&
"Did not consume the whole mask?");
2483 int &ReplicationFactor,
int &VF) {
2487 Mask.take_while([](
int MaskElt) {
return MaskElt == 0; }).
size();
2488 if (ReplicationFactor == 0 ||
Mask.size() % ReplicationFactor != 0)
2490 VF =
Mask.size() / ReplicationFactor;
2502 for (
int MaskElt :
Mask) {
2506 if (MaskElt < Largest)
2508 Largest =
std::max(Largest, MaskElt);
2512 for (
int PossibleReplicationFactor :
2513 reverse(seq_inclusive<unsigned>(1,
Mask.size()))) {
2514 if (
Mask.size() % PossibleReplicationFactor != 0)
2516 int PossibleVF =
Mask.size() / PossibleReplicationFactor;
2520 ReplicationFactor = PossibleReplicationFactor;
2532 if (isa<ScalableVectorType>(
getType()))
2535 VF = cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2536 if (ShuffleMask.size() % VF != 0)
2538 ReplicationFactor = ShuffleMask.size() / VF;
2555 assert(!Idxs.
empty() &&
"InsertValueInst must have at least one index");
2558 Val->
getType() &&
"Inserted value must match indexed type!");
2569 Indices(IVI.Indices) {
2584 assert(!Idxs.
empty() &&
"ExtractValueInst must have at least one index");
2592 Indices(EVI.Indices) {
2604 for (
unsigned Index : Idxs) {
2611 if (
ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
2612 if (
Index >= AT->getNumElements())
2614 Agg = AT->getElementType();
2615 }
else if (
StructType *
ST = dyn_cast<StructType>(Agg)) {
2616 if (
Index >=
ST->getNumElements())
2618 Agg =
ST->getElementType(
Index);
2624 return const_cast<Type*
>(Agg);
2663 void UnaryOperator::AssertOK() {
2670 "Unary operation should return same type as operand!");
2672 "Tried to create a floating-point operation on a "
2673 "non-floating-point type!");
2710 void BinaryOperator::AssertOK() {
2714 "Binary operator operand types must match!");
2720 "Arithmetic operation should return same type as operands!");
2722 "Tried to create an integer operation on a non-integer type!");
2724 case FAdd:
case FSub:
2727 "Arithmetic operation should return same type as operands!");
2729 "Tried to create a floating-point operation on a "
2730 "non-floating-point type!");
2735 "Arithmetic operation should return same type as operands!");
2737 "Incorrect operand type (not integer) for S/UDIV");
2741 "Arithmetic operation should return same type as operands!");
2743 "Incorrect operand type (not floating point) for FDIV");
2748 "Arithmetic operation should return same type as operands!");
2750 "Incorrect operand type (not integer) for S/UREM");
2754 "Arithmetic operation should return same type as operands!");
2756 "Incorrect operand type (not floating point) for FREM");
2762 "Shift operation should return same type as operands!");
2764 "Tried to create a shift operation on a non-integral type!");
2769 "Logical operation should return same type as operands!");
2771 "Tried to create a logical operation on a non-integral type!");
2782 "Cannot create binary operator with two operands of differing type!");
2813 return BinaryOperator::CreateNSWSub(
zero,
Op,
Name, InsertBefore);
2819 return BinaryOperator::CreateNSWSub(
zero,
Op,
Name, InsertAtEnd);
2825 return BinaryOperator::CreateNUWSub(
zero,
Op,
Name, InsertBefore);
2831 return BinaryOperator::CreateNUWSub(
zero,
Op,
Name, InsertAtEnd);
2855 Op<0>().swap(Op<1>());
2865 cast<Instruction>(
this)->getMetadata(LLVMContext::MD_fpmath);
2879 default:
return false;
2880 case Instruction::ZExt:
2881 case Instruction::SExt:
2882 case Instruction::Trunc:
2884 case Instruction::BitCast:
2892 if (
getOpcode() != Instruction::BitCast)
2922 case Instruction::Trunc:
2923 case Instruction::ZExt:
2924 case Instruction::SExt:
2925 case Instruction::FPTrunc:
2926 case Instruction::FPExt:
2927 case Instruction::UIToFP:
2928 case Instruction::SIToFP:
2929 case Instruction::FPToUI:
2930 case Instruction::FPToSI:
2931 case Instruction::AddrSpaceCast:
2934 case Instruction::BitCast:
2936 case Instruction::PtrToInt:
2937 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
2939 case Instruction::IntToPtr:
2940 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
2960 Type *DstIntPtrTy) {
2991 const unsigned numCastOps =
2992 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2993 static const uint8_t CastResults[numCastOps][numCastOps] = {
2999 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0},
3000 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0},
3001 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0},
3002 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0},
3003 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0},
3004 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0},
3005 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0},
3006 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0},
3007 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0},
3008 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0},
3009 { 99,99,99,99,99,99,99,99,99,11,99,15, 0},
3010 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14},
3011 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12},
3018 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
3019 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
3020 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
3023 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
3024 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
3025 if (!AreBothBitcasts)
3028 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
3029 [secondOp-Instruction::CastOpsBegin];
3080 return Instruction::BitCast;
3083 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
3086 if (MidSize >= PtrSize)
3087 return Instruction::BitCast;
3097 return Instruction::BitCast;
3098 if (SrcSize < DstSize)
3100 if (SrcSize > DstSize)
3106 return Instruction::ZExt;
3114 if (SrcSize <= PtrSize && SrcSize == DstSize)
3115 return Instruction::BitCast;
3122 return Instruction::AddrSpaceCast;
3123 return Instruction::BitCast;
3134 "Illegal addrspacecast, bitcast sequence!");
3143 return Instruction::AddrSpaceCast;
3155 "Illegal inttoptr, bitcast sequence!");
3167 "Illegal bitcast, ptrtoint sequence!");
3172 return Instruction::UIToFP;
3230 return Create(Instruction::BitCast,
S, Ty,
Name, InsertBefore);
3231 return Create(Instruction::ZExt,
S, Ty,
Name, InsertBefore);
3238 return Create(Instruction::BitCast,
S, Ty,
Name, InsertAtEnd);
3239 return Create(Instruction::ZExt,
S, Ty,
Name, InsertAtEnd);
3246 return Create(Instruction::BitCast,
S, Ty,
Name, InsertBefore);
3247 return Create(Instruction::SExt,
S, Ty,
Name, InsertBefore);
3254 return Create(Instruction::BitCast,
S, Ty,
Name, InsertAtEnd);
3255 return Create(Instruction::SExt,
S, Ty,
Name, InsertAtEnd);
3262 return Create(Instruction::BitCast,
S, Ty,
Name, InsertBefore);
3263 return Create(Instruction::Trunc,
S, Ty,
Name, InsertBefore);
3270 return Create(Instruction::BitCast,
S, Ty,
Name, InsertAtEnd);
3271 return Create(Instruction::Trunc,
S, Ty,
Name, InsertAtEnd);
3277 assert(
S->getType()->isPtrOrPtrVectorTy() &&
"Invalid cast");
3282 cast<VectorType>(Ty)->getElementCount() ==
3283 cast<VectorType>(
S->getType())->getElementCount()) &&
3287 return Create(Instruction::PtrToInt,
S, Ty,
Name, InsertAtEnd);
3296 assert(
S->getType()->isPtrOrPtrVectorTy() &&
"Invalid cast");
3301 cast<VectorType>(Ty)->getElementCount() ==
3302 cast<VectorType>(
S->getType())->getElementCount()) &&
3306 return Create(Instruction::PtrToInt,
S, Ty,
Name, InsertBefore);
3315 assert(
S->getType()->isPtrOrPtrVectorTy() &&
"Invalid cast");
3319 return Create(Instruction::AddrSpaceCast,
S, Ty,
Name, InsertAtEnd);
3321 return Create(Instruction::BitCast,
S, Ty,
Name, InsertAtEnd);
3328 assert(
S->getType()->isPtrOrPtrVectorTy() &&
"Invalid cast");
3332 return Create(Instruction::AddrSpaceCast,
S, Ty,
Name, InsertBefore);
3334 return Create(Instruction::BitCast,
S, Ty,
Name, InsertBefore);
3341 return Create(Instruction::PtrToInt,
S, Ty,
Name, InsertBefore);
3343 return Create(Instruction::IntToPtr,
S, Ty,
Name, InsertBefore);
3345 return Create(Instruction::BitCast,
S, Ty,
Name, InsertBefore);
3352 "Invalid integer cast");
3353 unsigned SrcBits =
C->getType()->getScalarSizeInBits();
3356 (SrcBits == DstBits ? Instruction::BitCast :
3357 (SrcBits > DstBits ? Instruction::Trunc :
3358 (isSigned ? Instruction::SExt : Instruction::ZExt)));
3367 unsigned SrcBits =
C->getType()->getScalarSizeInBits();
3370 (SrcBits == DstBits ? Instruction::BitCast :
3371 (SrcBits > DstBits ? Instruction::Trunc :
3372 (isSigned ? Instruction::SExt : Instruction::ZExt)));
3381 unsigned SrcBits =
C->getType()->getScalarSizeInBits();
3384 (SrcBits == DstBits ? Instruction::BitCast :
3385 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
3394 unsigned SrcBits =
C->getType()->getScalarSizeInBits();
3397 (SrcBits == DstBits ? Instruction::BitCast :
3398 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
3406 if (SrcTy == DestTy)
3409 if (
VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3410 if (
VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
3411 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3413 SrcTy = SrcVecTy->getElementType();
3414 DestTy = DestVecTy->getElementType();
3419 if (
PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
3420 if (
PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
3421 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
3433 if (SrcBits != DestBits)
3445 if (
auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3446 if (
auto *IntTy = dyn_cast<IntegerType>(DestTy))
3447 return (IntTy->getBitWidth() ==
DL.getPointerTypeSizeInBits(PtrTy) &&
3448 !
DL.isNonIntegralPointerType(PtrTy));
3449 if (
auto *PtrTy = dyn_cast<PointerType>(DestTy))
3450 if (
auto *IntTy = dyn_cast<IntegerType>(SrcTy))
3451 return (IntTy->getBitWidth() ==
DL.getPointerTypeSizeInBits(PtrTy) &&
3452 !
DL.isNonIntegralPointerType(PtrTy));
3465 const Value *Src,
bool SrcIsSigned,
Type *DestTy,
bool DestIsSigned) {
3466 Type *SrcTy = Src->getType();
3469 "Only first class types are castable!");
3471 if (SrcTy == DestTy)
3475 if (
VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3476 if (
VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
3477 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3480 SrcTy = SrcVecTy->getElementType();
3481 DestTy = DestVecTy->getElementType();
3491 if (DestBits < SrcBits)
3493 else if (DestBits > SrcBits) {
3507 assert(DestBits == SrcBits &&
3508 "Casting vector to integer of different width");
3512 "Casting from a value that is not first-class type");
3522 if (DestBits < SrcBits) {
3524 }
else if (DestBits > SrcBits) {
3530 assert(DestBits == SrcBits &&
3531 "Casting vector to floating point of different width");
3536 assert(DestBits == SrcBits &&
3537 "Illegal cast to vector (wrong type or size)");
3542 return AddrSpaceCast;
3550 assert(DestBits == SrcBits &&
"Casting vector of wrong width to X86_MMX");
3574 bool SrcIsVec = isa<VectorType>(SrcTy);
3575 bool DstIsVec = isa<VectorType>(DstTy);
3582 ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount()
3584 ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount()
3589 default:
return false;
3590 case Instruction::Trunc:
3592 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3593 case Instruction::ZExt:
3595 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3596 case Instruction::SExt:
3598 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3599 case Instruction::FPTrunc:
3601 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3602 case Instruction::FPExt:
3604 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3605 case Instruction::UIToFP:
3606 case Instruction::SIToFP:
3609 case Instruction::FPToUI:
3610 case Instruction::FPToSI:
3613 case Instruction::PtrToInt:
3617 case Instruction::IntToPtr:
3621 case Instruction::BitCast: {
3627 if (!SrcPtrTy != !DstPtrTy)
3640 if (SrcIsVec && DstIsVec)
3641 return SrcEC == DstEC;
3649 case Instruction::AddrSpaceCast: {
3661 return SrcEC == DstEC;
3855 if (
Op == Instruction::ICmp) {
3875 if (
Op == Instruction::ICmp) {
3884 if (
ICmpInst *IC = dyn_cast<ICmpInst>(
this))
3887 cast<FCmpInst>(
this)->swapOperands();
3891 if (
const ICmpInst *IC = dyn_cast<ICmpInst>(
this))
3892 return IC->isCommutative();
3893 return cast<FCmpInst>(
this)->isCommutative();
3939 default:
return "unknown";
4149 switch (predicate) {
4150 default:
return false;
4157 switch (predicate) {
4158 default:
return false;
4168 case ICmpInst::Predicate::ICMP_EQ:
4170 case ICmpInst::Predicate::ICMP_NE:
4172 case ICmpInst::Predicate::ICMP_UGT:
4174 case ICmpInst::Predicate::ICMP_UGE:
4176 case ICmpInst::Predicate::ICMP_ULT:
4178 case ICmpInst::Predicate::ICMP_ULE:
4180 case ICmpInst::Predicate::ICMP_SGT:
4182 case ICmpInst::Predicate::ICMP_SGE:
4184 case ICmpInst::Predicate::ICMP_SLT:
4186 case ICmpInst::Predicate::ICMP_SLE:
4236 "Call only with non-equality predicates!");
4247 switch (predicate) {
4248 default:
return false;
4256 switch (predicate) {
4257 default:
return false;
4266 default:
return false;
4276 default:
return false;
4315 ReservedSpace = NumReserved;
4330 nullptr, 0, InsertBefore) {
4341 nullptr, 0, InsertAtEnd) {
4347 init(
SI.getCondition(),
SI.getDefaultDest(),
SI.getNumOperands());
4350 const Use *InOL =
SI.getOperandList();
4351 for (
unsigned i = 2,
E =
SI.getNumOperands();
i !=
E;
i += 2) {
4353 OL[
i+1] = InOL[
i+1];
4363 if (OpNo+2 > ReservedSpace)
4366 assert(OpNo+1 < ReservedSpace &&
"Growing didn't work!");
4376 unsigned idx =
I->getCaseIndex();
4384 if (2 + (idx + 1) * 2 != NumOps) {
4385 OL[2 + idx * 2] = OL[NumOps - 2];
4386 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
4390 OL[NumOps-2].
set(
nullptr);
4391 OL[NumOps-2+1].
set(
nullptr);
4394 return CaseIt(
this, idx);
4400 void SwitchInst::growOperands() {
4402 unsigned NumOps =
e*3;
4404 ReservedSpace = NumOps;