15 #ifndef LLVM_IR_INSTRUCTIONS_H
16 #define LLVM_IR_INSTRUCTIONS_H
66 "Bitfields must be contiguous");
126 return Align(1ULL << getSubclassData<AlignmentField>());
130 setSubclassData<AlignmentField>(
Log2(
Align));
141 return getSubclassData<UsedWithInAllocaField>();
146 setSubclassData<UsedWithInAllocaField>(V);
150 bool isSwiftError()
const {
return getSubclassData<SwiftErrorField>(); }
156 return (
I->getOpcode() == Instruction::Alloca);
159 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
165 template <
typename Bitfield>
167 Instruction::setSubclassData<Bitfield>(
Value);
182 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
183 "Bitfields must be contiguous");
214 bool isVolatile()
const {
return getSubclassData<VolatileField>(); }
221 return Align(1ULL << (getSubclassData<AlignmentField>()));
225 setSubclassData<AlignmentField>(
Log2(
Align));
230 return getSubclassData<OrderingField>();
235 setSubclassData<OrderingField>(Ordering);
279 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
285 template <
typename Bitfield>
287 Instruction::setSubclassData<Bitfield>(
Value);
306 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
307 "Bitfields must be contiguous");
333 void *
operator new(
size_t S) {
return User::operator
new(
S, 2); }
334 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
337 bool isVolatile()
const {
return getSubclassData<VolatileField>(); }
346 return Align(1ULL << (getSubclassData<AlignmentField>()));
350 setSubclassData<AlignmentField>(
Log2(
Align));
355 return getSubclassData<OrderingField>();
361 setSubclassData<OrderingField>(Ordering);
408 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
414 template <
typename Bitfield>
416 Instruction::setSubclassData<Bitfield>(
Value);
457 void *
operator new(
size_t S) {
return User::operator
new(
S, 0); }
458 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
462 return getSubclassData<OrderingField>();
468 setSubclassData<OrderingField>(Ordering);
483 return I->getOpcode() == Instruction::Fence;
486 return isa<Instruction>(V) && classof(cast<Instruction>(V));
492 template <
typename Bitfield>
494 Instruction::setSubclassData<Bitfield>(
Value);
518 template <
unsigned Offset>
519 using AtomicOrderingBitfieldElement =
540 void *
operator new(
size_t S) {
return User::operator
new(
S, 3); }
541 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
554 "Bitfields must be contiguous");
559 return Align(1ULL << getSubclassData<AlignmentField>());
563 setSubclassData<AlignmentField>(
Log2(
Align));
569 bool isVolatile()
const {
return getSubclassData<VolatileField>(); }
576 bool isWeak()
const {
return getSubclassData<WeakField>(); }
578 void setWeak(
bool IsWeak) { setSubclassData<WeakField>(IsWeak); }
597 return getSubclassData<SuccessOrderingField>();
603 "invalid CmpXchg success ordering");
604 setSubclassData<SuccessOrderingField>(Ordering);
609 return getSubclassData<FailureOrderingField>();
615 "invalid CmpXchg failure ordering");
616 setSubclassData<FailureOrderingField>(Ordering);
667 switch (SuccessOrdering) {
683 return I->getOpcode() == Instruction::AtomicCmpXchg;
686 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
692 template <
typename Bitfield>
694 Instruction::setSubclassData<Bitfield>(
Value);
777 LAST_BINOP = UDecWrap,
782 template <
unsigned Offset>
783 using AtomicOrderingBitfieldElement =
787 template <
unsigned Offset>
788 using BinOpBitfieldElement =
800 void *
operator new(
size_t S) {
return User::operator
new(
S, 2); }
801 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
810 "Bitfields must be contiguous");
829 setSubclassData<OperationField>(
Operation);
835 return Align(1ULL << getSubclassData<AlignmentField>());
839 setSubclassData<AlignmentField>(
Log2(
Align));
844 bool isVolatile()
const {
return getSubclassData<VolatileField>(); }
855 return getSubclassData<AtomicOrderingField>();
861 "atomicrmw instructions can only be atomic.");
863 "atomicrmw instructions cannot be unordered.");
864 setSubclassData<AtomicOrderingField>(Ordering);
890 return isFPOperation(getOperation());
895 return I->getOpcode() == Instruction::AtomicRMW;
898 return isa<Instruction>(V) && classof(cast<Instruction>(V));
907 template <
typename Bitfield>
909 Instruction::setSubclassData<Bitfield>(
Value);
933 assert(Ty &&
"Invalid GetElementPtrInst indices for type!");
941 Type *SourceElementType;
942 Type *ResultElementType;
968 const Twine &NameStr =
"",
970 unsigned Values = 1 + unsigned(IdxList.
size());
971 assert(PointeeType &&
"Must specify element type");
972 assert(cast<PointerType>(
Ptr->getType()->getScalarType())
973 ->isOpaqueOrPointeeTypeMatches(PointeeType));
975 NameStr, InsertBefore);
980 const Twine &NameStr,
982 unsigned Values = 1 + unsigned(IdxList.
size());
983 assert(PointeeType &&
"Must specify element type");
984 assert(cast<PointerType>(
Ptr->getType()->getScalarType())
985 ->isOpaqueOrPointeeTypeMatches(PointeeType));
987 NameStr, InsertAtEnd);
994 const Twine &NameStr =
"",
997 Create(PointeeType,
Ptr, IdxList, NameStr, InsertBefore);
998 GEP->setIsInBounds(
true);
1004 const Twine &NameStr,
1007 Create(PointeeType,
Ptr, IdxList, NameStr, InsertAtEnd);
1008 GEP->setIsInBounds(
true);
1022 ->isOpaqueOrPointeeTypeMatches(ResultElementType));
1023 return ResultElementType;
1088 PointerType *OrigPtrTy = cast<PointerType>(
Ptr->getType()->getScalarType());
1095 if (
auto *PtrVTy = dyn_cast<VectorType>(
Ptr->getType())) {
1100 if (
auto *IndexVTy = dyn_cast<VectorType>(
Index->getType())) {
1144 APInt &ConstantOffset)
const;
1147 return (
I->getOpcode() == Instruction::GetElementPtr);
1150 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
1159 GetElementPtrInst::GetElementPtrInst(
Type *PointeeType,
Value *
Ptr,
1161 const Twine &NameStr,
1163 :
Instruction(getGEPReturnType(PointeeType,
Ptr, IdxList), GetElementPtr,
1165 Values, InsertBefore),
1166 SourceElementType(PointeeType),
1167 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1169 ->isOpaqueOrPointeeTypeMatches(ResultElementType));
1173 GetElementPtrInst::GetElementPtrInst(Type *PointeeType,
Value *
Ptr,
1174 ArrayRef<Value *> IdxList,
unsigned Values,
1175 const Twine &NameStr,
1177 : Instruction(getGEPReturnType(PointeeType,
Ptr, IdxList), GetElementPtr,
1178 OperandTraits<GetElementPtrInst>::op_end(
this) - Values,
1179 Values, InsertAtEnd),
1180 SourceElementType(PointeeType),
1181 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1183 ->isOpaqueOrPointeeTypeMatches(ResultElementType));
1199 assert(isIntPredicate() &&
1200 "Invalid ICmp predicate value");
1202 "Both operands to ICmp instruction are not of the same type!");
1205 getOperand(0)->
getType()->isPtrOrPtrVectorTy()) &&
1206 "Invalid operand types for ICmp instruction");
1223 const Twine &NameStr =
""
1238 const Twine &NameStr =
""
1252 const Twine &NameStr =
""
1287 return P == ICMP_EQ ||
P == ICMP_NE;
1303 return !isEquality();
1309 return !isEquality(
P);
1315 return P == ICMP_SGT ||
P == ICMP_UGT;
1321 return P == ICMP_SLT ||
P == ICMP_ULT;
1327 return P == ICMP_SGE ||
P == ICMP_UGE;
1333 return P == ICMP_SLE ||
P == ICMP_ULE;
1347 Op<0>().swap(Op<1>());
1356 return I->getOpcode() == Instruction::ICmp;
1359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1375 "Both operands to FCmp instruction are not of the same type!");
1378 "Invalid operand types for FCmp instruction");
1395 const Twine &NameStr =
""
1408 const Twine &NameStr =
""
1420 const Twine &NameStr =
"",
1423 RHS, NameStr, nullptr, FlagsSource) {
1459 Op<0>().swap(Op<1>());
1472 return I->getOpcode() == Instruction::FCmp;
1475 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
1496 :
CallInst(Ty, Func,
Args, std::nullopt, NameStr, InsertBefore) {}
1515 static int ComputeNumOperands(
int NumArgs,
int NumBundleInputs = 0) {
1518 return 1 + NumArgs + NumBundleInputs;
1530 return new (ComputeNumOperands(0))
CallInst(Ty,
F, NameStr, InsertBefore);
1534 const Twine &NameStr,
1536 return new (ComputeNumOperands(
Args.size()))
1537 CallInst(Ty, Func,
Args, std::nullopt, NameStr, InsertBefore);
1542 const Twine &NameStr =
"",
1544 const int NumOperands =
1548 return new (NumOperands, DescriptorBytes)
1549 CallInst(Ty, Func,
Args, Bundles, NameStr, InsertBefore);
1554 return new (ComputeNumOperands(0))
CallInst(Ty,
F, NameStr, InsertAtEnd);
1559 return new (ComputeNumOperands(
Args.size()))
1560 CallInst(Ty, Func,
Args, std::nullopt, NameStr, InsertAtEnd);
1566 const int NumOperands =
1570 return new (NumOperands, DescriptorBytes)
1571 CallInst(Ty, Func,
Args, Bundles, NameStr, InsertAtEnd);
1576 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1582 const Twine &NameStr =
"",
1584 return Create(Func.getFunctionType(), Func.getCallee(),
Args, Bundles,
1585 NameStr, InsertBefore);
1589 const Twine &NameStr,
1591 return Create(Func.getFunctionType(), Func.getCallee(),
Args, NameStr,
1597 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1603 return Create(Func.getFunctionType(), Func.getCallee(),
Args, NameStr,
1610 return Create(Func.getFunctionType(), Func.getCallee(),
Args, Bundles,
1611 NameStr, InsertAtEnd);
1631 Value *ArraySize =
nullptr,
1636 Value *ArraySize =
nullptr,
1641 Value *AllocSize,
Value *ArraySize =
nullptr,
1646 Value *AllocSize,
Value *ArraySize =
nullptr,
1670 Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(),
1671 "Bitfields must be contiguous");
1674 return getSubclassData<TailCallKindField>();
1687 setSubclassData<TailCallKindField>(TCK);
1703 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
1712 template <
typename Bitfield>
1714 Instruction::setSubclassData<Bitfield>(
Value);
1719 ArrayRef<OperandBundleDef> Bundles,
const Twine &NameStr,
1721 : CallBase(Ty->getReturnType(), Instruction::
Call,
1722 OperandTraits<CallBase>::op_end(
this) -
1723 (
Args.
size() + CountBundleInputs(Bundles) + 1),
1724 unsigned(
Args.
size() + CountBundleInputs(Bundles) + 1),
1726 init(Ty, Func,
Args, Bundles, NameStr);
1730 ArrayRef<OperandBundleDef> Bundles,
const Twine &NameStr,
1731 Instruction *InsertBefore)
1732 : CallBase(Ty->getReturnType(), Instruction::
Call,
1733 OperandTraits<CallBase>::op_end(
this) -
1734 (
Args.
size() + CountBundleInputs(Bundles) + 1),
1735 unsigned(
Args.
size() + CountBundleInputs(Bundles) + 1),
1737 init(Ty, Func,
Args, Bundles, NameStr);
1750 &Op<0>(), 3, InsertBefore) {
1758 &Op<0>(), 3, InsertAtEnd) {
1778 const Twine &NameStr =
"",
1788 const Twine &NameStr,
1790 return new(3)
SelectInst(
C, S1, S2, NameStr, InsertAtEnd);
1824 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
1867 return I->getOpcode() == VAArg;
1870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1895 const Twine &NameStr =
"",
1901 const Twine &NameStr,
1924 return I->getOpcode() == Instruction::ExtractElement;
1927 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
1947 const Twine &NameStr =
"",
1960 const Twine &NameStr =
"",
1966 const Twine &NameStr,
1973 static bool isValidOperands(
const Value *Vec,
const Value *NewElt,
1987 return I->getOpcode() == Instruction::InsertElement;
1990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2037 const Twine &NameStr =
"",
2042 const Twine &NameStr =
"",
2047 void *
operator new(
size_t S) {
return User::operator
new(
S, 2); }
2048 void operator delete(
void *
Ptr) {
return User::operator
delete(
Ptr); }
2056 static bool isValidOperands(
const Value *V1,
const Value *
V2,
2058 static bool isValidOperands(
const Value *V1,
const Value *
V2,
2082 Result.assign(ShuffleMask.begin(), ShuffleMask.end());
2103 unsigned NumSourceElts = cast<VectorType>(Op<0>()->
getType())
2105 .getKnownMinValue();
2106 unsigned NumMaskElts = ShuffleMask.size();
2107 return NumSourceElts != NumMaskElts;
2114 unsigned NumSourceElts = cast<VectorType>(Op<0>()->
getType())
2116 .getKnownMinValue();
2117 unsigned NumMaskElts = ShuffleMask.size();
2118 return NumSourceElts < NumMaskElts;
2127 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2129 getShuffleMask(
Mask, MaskAsInts);
2130 return isSingleSourceMask(MaskAsInts);
2138 return !changesLength() && isSingleSourceMask(ShuffleMask);
2148 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2152 if (isa<ScalableVectorType>(
Mask->getType()))
2156 getShuffleMask(
Mask, MaskAsInts);
2157 return isIdentityMask(MaskAsInts);
2167 if (isa<ScalableVectorType>(
getType()))
2170 return !changesLength() && isIdentityMask(ShuffleMask);
2175 bool isIdentityWithPadding()
const;
2179 bool isIdentityWithExtract()
const;
2184 bool isConcat()
const;
2196 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2198 getShuffleMask(
Mask, MaskAsInts);
2199 return isSelectMask(MaskAsInts);
2211 return !changesLength() && isSelectMask(ShuffleMask);
2220 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2222 getShuffleMask(
Mask, MaskAsInts);
2240 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2242 getShuffleMask(
Mask, MaskAsInts);
2243 return isZeroEltSplatMask(MaskAsInts);
2253 return !changesLength() && isZeroEltSplatMask(ShuffleMask);
2290 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2292 getShuffleMask(
Mask, MaskAsInts);
2293 return isTransposeMask(MaskAsInts);
2302 return !changesLength() && isTransposeMask(ShuffleMask);
2311 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2313 getShuffleMask(
Mask, MaskAsInts);
2314 return isSpliceMask(MaskAsInts,
Index);
2322 return !changesLength() && isSpliceMask(ShuffleMask,
Index);
2332 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2335 if (isa<ScalableVectorType>(
Mask->getType()))
2338 getShuffleMask(
Mask, MaskAsInts);
2339 return isExtractSubvectorMask(MaskAsInts, NumSrcElts,
Index);
2346 if (isa<ScalableVectorType>(
getType()))
2350 cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2351 return isExtractSubvectorMask(ShuffleMask, NumSrcElts,
Index);
2359 int &NumSubElts,
int &
Index);
2361 int &NumSubElts,
int &
Index) {
2362 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2365 if (isa<ScalableVectorType>(
Mask->getType()))
2368 getShuffleMask(
Mask, MaskAsInts);
2369 return isInsertSubvectorMask(MaskAsInts, NumSrcElts, NumSubElts,
Index);
2376 if (isa<ScalableVectorType>(
getType()))
2380 cast<FixedVectorType>(Op<0>()->
getType())->getNumElements();
2381 return isInsertSubvectorMask(ShuffleMask, NumSrcElts, NumSubElts,
Index);
2392 assert(
Mask->getType()->isVectorTy() &&
"Shuffle needs vector constant.");
2395 if (isa<ScalableVectorType>(
Mask->getType()))
2398 getShuffleMask(
Mask, MaskAsInts);
2399 return isReplicationMask(MaskAsInts, ReplicationFactor, VF);
2403 bool isReplicationMask(
int &ReplicationFactor,
int &VF)
const;
2418 bool isOneUseSingleSourceMask(
int VF)
const;
2423 unsigned InVecNumElts) {
2424 for (
int &Idx :
Mask) {
2427 Idx = Idx < (
int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2428 assert(Idx >= 0 && Idx < (
int)InVecNumElts * 2 &&
2429 "shufflevector mask index out of range");
2435 return I->getOpcode() == Instruction::ShuffleVector;
2438 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2466 const Twine &NameStr,
2483 const Twine &NameStr =
"",
2491 const Twine &NameStr,
2511 return getOperand(0);
2514 return getOperand(0);
2525 return (
unsigned)Indices.size();
2534 return I->getOpcode() == Instruction::ExtractValue;
2537 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2541 ExtractValueInst::ExtractValueInst(
Value *Agg,
2542 ArrayRef<unsigned> Idxs,
2543 const Twine &NameStr,
2544 Instruction *InsertBefore)
2546 ExtractValue, Agg, InsertBefore) {
2547 init(Idxs, NameStr);
2550 ExtractValueInst::ExtractValueInst(
Value *Agg,
2551 ArrayRef<unsigned> Idxs,
2552 const Twine &NameStr,
2555 ExtractValue, Agg, InsertAtEnd) {
2556 init(Idxs, NameStr);
2577 const Twine &NameStr,
2586 const Twine &NameStr =
"",
2592 const Twine &NameStr);
2602 void *
operator new(
size_t S) {
return User::operator
new(
S, 2); }
2603 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
2607 const Twine &NameStr =
"",
2614 const Twine &NameStr,
2655 return (
unsigned)Indices.size();
2664 return I->getOpcode() == Instruction::InsertValue;
2667 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
2676 InsertValueInst::InsertValueInst(
Value *Agg,
2679 const Twine &NameStr,
2684 init(Agg, Val, Idxs, NameStr);
2687 InsertValueInst::InsertValueInst(
Value *Agg,
2689 ArrayRef<unsigned> Idxs,
2690 const Twine &NameStr,
2692 : Instruction(Agg->
getType(), InsertValue,
2693 OperandTraits<InsertValueInst>::op_begin(
this),
2695 init(Agg, Val, Idxs, NameStr);
2711 unsigned ReservedSpace;
2715 explicit PHINode(
Type *Ty,
unsigned NumReservedValues,
2716 const Twine &NameStr =
"",
2719 ReservedSpace(NumReservedValues) {
2722 allocHungoffUses(ReservedSpace);
2728 ReservedSpace(NumReservedValues) {
2731 allocHungoffUses(ReservedSpace);
2751 const Twine &NameStr =
"",
2753 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2758 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2777 return block_begin() + getNumOperands();
2781 return make_range(block_begin(), block_end());
2795 return getOperand(
i);
2798 assert(V &&
"PHI node got a null value!");
2800 "All operands to PHI node must be the same type as the PHI node!");
2815 return block_begin()[
i];
2822 assert(
this == U.
getUser() &&
"Iterator doesn't point to PHI's Uses?");
2823 return getIncomingBlock(
unsigned(&U - op_begin()));
2830 return getIncomingBlock(
I.getUse());
2846 assert(New && Old &&
"PHI node got a null basic block!");
2847 for (
unsigned Op = 0, NumOps = getNumOperands();
Op != NumOps; ++
Op)
2848 if (getIncomingBlock(
Op) == Old)
2849 setIncomingBlock(
Op, New);
2855 if (getNumOperands() == ReservedSpace)
2858 setNumHungOffUseOperands(getNumOperands() + 1);
2859 setIncomingValue(getNumOperands() - 1, V);
2860 setIncomingBlock(getNumOperands() - 1,
BB);
2871 Value *removeIncomingValue(
unsigned Idx,
bool DeletePHIIfEmpty =
true);
2874 int Idx = getBasicBlockIndex(
BB);
2875 assert(Idx >= 0 &&
"Invalid basic block argument to remove!");
2876 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2883 for (
unsigned i = 0,
e = getNumOperands();
i !=
e; ++
i)
2884 if (block_begin()[
i] ==
BB)
2890 int Idx = getBasicBlockIndex(
BB);
2891 assert(Idx >= 0 &&
"Invalid basic block argument!");
2892 return getIncomingValue(Idx);
2897 assert(
BB &&
"PHI node got a null basic block!");
2899 for (
unsigned Op = 0, NumOps = getNumOperands();
Op != NumOps; ++
Op)
2900 if (getIncomingBlock(
Op) ==
BB) {
2902 setIncomingValue(
Op, V);
2905 assert(Found &&
"Invalid basic block argument to set!");
2910 Value *hasConstantValue()
const;
2915 bool hasConstantOrUndefValue()
const;
2922 return getBasicBlockIndex(Pred) >= 0;
2931 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2935 void growOperands();
2961 unsigned ReservedSpace;
2975 void *
operator new(
size_t S) {
return User::operator
new(
S); }
2977 void growOperands(
unsigned Size);
2978 void init(
unsigned NumReservedValues,
const Twine &NameStr);
2987 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
2992 const Twine &NameStr =
"",
3003 bool isCleanup()
const {
return getSubclassData<CleanupField>(); }
3014 return cast<Constant>(getOperandList()[Idx]);
3019 return !isa<ArrayType>(getOperandList()[Idx]->
getType());
3024 return isa<ArrayType>(getOperandList()[Idx]->
getType());
3036 return I->getOpcode() == Instruction::LandingPad;
3039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3086 return new(!!retVal)
ReturnInst(
C, retVal, InsertBefore);
3091 return new(!!retVal)
ReturnInst(
C, retVal, InsertAtEnd);
3103 return getNumOperands() != 0 ? getOperand(0) :
nullptr;
3113 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3117 BasicBlock *getSuccessor(
unsigned idx)
const {
3174 std::random_access_iterator_tag, BasicBlock *,
3175 ptrdiff_t, BasicBlock *, BasicBlock *> {
3185 std::random_access_iterator_tag,
3186 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3187 const BasicBlock *> {
3197 return new(1)
BranchInst(IfTrue, InsertBefore);
3206 return new(1)
BranchInst(IfTrue, InsertAtEnd);
3221 assert(isConditional() &&
"Cannot get condition of an uncond branch!");
3226 assert(isConditional() &&
"Cannot set condition of unconditional branch!");
3233 assert(
i < getNumSuccessors() &&
"Successor # out of range for Branch!");
3234 return cast_or_null<BasicBlock>((&Op<-1>() -
i)->
get());
3238 assert(idx < getNumSuccessors() &&
"Successor # out of range for Branch!");
3239 *(&
Op<-1>() - idx) = NewSucc;
3247 void swapSuccessors();
3257 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3263 return (
I->getOpcode() == Instruction::Br);
3266 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3284 unsigned ReservedSpace;
3307 void *
operator new(
size_t S) {
return User::operator
new(
S); }
3310 void growOperands();
3319 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
3322 static const unsigned DefaultPseudoIndex =
static_cast<unsigned>(~0L-1);
3331 template <
typename SwitchInstT,
typename ConstantIntT,
typename BasicBlockT>
3351 "Index out the number of cases.");
3352 return reinterpret_cast<ConstantIntT *
>(
SI->getOperand(2 +
Index * 2));
3358 (
unsigned)
Index == DefaultPseudoIndex) &&
3359 "Index out the number of cases.");
3360 return SI->getSuccessor(getSuccessorIndex());
3370 "Index out the number of cases.");
3371 return (
unsigned)
Index != DefaultPseudoIndex ?
Index + 1 : 0;
3375 assert(
SI ==
RHS.SI &&
"Incompatible operators.");
3380 using ConstCaseHandle =
3393 "Index out the number of cases.");
3394 SI->setOperand(2 +
Index*2,
reinterpret_cast<Value*
>(V));
3399 SI->setSuccessor(getSuccessorIndex(),
S);
3403 template <
typename CaseHandleT>
3404 class CaseIteratorImpl
3406 std::random_access_iterator_tag,
3407 const CaseHandleT> {
3408 using SwitchInstT =
typename CaseHandleT::SwitchInstType;
3415 CaseIteratorImpl() =
default;
3424 unsigned SuccessorIndex) {
3425 assert(SuccessorIndex < SI->getNumSuccessors() &&
3426 "Successor index # out of range!");
3441 (
unsigned)(Case.Index +
N) <= Case.SI->getNumCases() &&
3442 "Case.Index out the number of cases.");
3450 (
unsigned)(Case.Index -
N) <= Case.SI->getNumCases() &&
3451 "Case.Index out the number of cases.");
3456 assert(Case.SI ==
RHS.Case.SI &&
"Incompatible operators.");
3457 return Case.Index -
RHS.Case.Index;
3460 return Case ==
RHS.Case;
3463 assert(Case.SI ==
RHS.Case.SI &&
"Incompatible operators.");
3464 return Case.Index <
RHS.Case.Index;
3479 unsigned NumCases,
BasicBlock *InsertAtEnd) {
3491 return cast<BasicBlock>(getOperand(1));
3495 setOperand(1,
reinterpret_cast<Value*
>(DefaultCase));
3501 return getNumOperands()/2 - 1;
3519 return CaseIt(
this, getNumCases());
3544 return CaseIt(
this, DefaultPseudoIndex);
3557 const_cast<const SwitchInst *
>(
this)->findCaseValue(
C)->getCaseIndex());
3563 if (
I != case_end())
3566 return case_default();
3572 if (
BB == getDefaultDest())
3576 for (
auto Case :
cases()) {
3577 if (Case.getCaseSuccessor() !=
BB)
3583 CI = Case.getCaseValue();
3602 CaseIt removeCase(CaseIt
I);
3606 assert(idx < getNumSuccessors() &&
"Successor idx out of range for switch!");
3607 return cast<BasicBlock>(getOperand(idx*2+1));
3610 assert(idx < getNumSuccessors() &&
"Successor # out of range for switch!");
3611 setOperand(idx * 2 + 1, NewSucc);
3616 return I->getOpcode() == Instruction::Switch;
3619 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3627 std::optional<SmallVector<uint32_t, 8>> Weights;
3628 bool Changed =
false;
3680 unsigned ReservedSpace;
3699 void *
operator new(
size_t S) {
return User::operator
new(
S); }
3701 void init(
Value *Address,
unsigned NumDests);
3702 void growOperands();
3711 void operator delete(
void *
Ptr) { User::operator
delete(
Ptr); }
3719 std::random_access_iterator_tag, BasicBlock *,
3720 ptrdiff_t, BasicBlock *, BasicBlock *> {
3730 std::random_access_iterator_tag,
3731 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3732 const BasicBlock *> {
3772 void removeDestination(
unsigned i);
3776 return cast<BasicBlock>(getOperand(
i+1));
3779 setOperand(
i + 1, NewSucc);
3794 return I->getOpcode() == Instruction::IndirectBr;
3797 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3817 static constexpr
int NumExtraOperands = 2;
3820 static constexpr
int NormalDestOpEndIdx = -3;
3823 static constexpr
int UnwindDestOpEndIdx = -2;
3845 static int ComputeNumOperands(
int NumArgs,
int NumBundleInputs = 0) {
3848 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3860 const Twine &NameStr,
3862 int NumOperands = ComputeNumOperands(
Args.size());
3863 return new (NumOperands)
3865 NumOperands, NameStr, InsertBefore);
3871 const Twine &NameStr =
"",
3874 ComputeNumOperands(
Args.size(), CountBundleInputs(Bundles));
3877 return new (NumOperands, DescriptorBytes)
3878 InvokeInst(Ty, Func, IfNormal, IfException,
Args, Bundles, NumOperands,
3879 NameStr, InsertBefore);
3885 int NumOperands = ComputeNumOperands(
Args.size());
3886 return new (NumOperands)
3888 NumOperands, NameStr, InsertAtEnd);
3896 ComputeNumOperands(
Args.size(), CountBundleInputs(Bundles));
3899 return new (NumOperands, DescriptorBytes)
3900 InvokeInst(Ty, Func, IfNormal, IfException,
Args, Bundles, NumOperands,
3901 NameStr, InsertAtEnd);
3906 const Twine &NameStr,
3908 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3909 IfException,
Args, std::nullopt, NameStr, InsertBefore);
3915 const Twine &NameStr =
"",
3917 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3918 IfException,
Args, Bundles, NameStr, InsertBefore);
3924 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3925 IfException,
Args, NameStr, InsertAtEnd);
3932 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3933 IfException,
Args, Bundles, NameStr, InsertAtEnd);
3947 return cast<BasicBlock>(Op<NormalDestOpEndIdx>());
3950 return cast<BasicBlock>(Op<UnwindDestOpEndIdx>());
3953 Op<NormalDestOpEndIdx>() =
reinterpret_cast<Value *
>(
B);
3956 Op<UnwindDestOpEndIdx>() =
reinterpret_cast<Value *
>(
B);
3964 assert(
i < 2 &&
"Successor # out of range for invoke!");
3965 return i == 0 ? getNormalDest() : getUnwindDest();
3969 assert(
i < 2 &&
"Successor # out of range for invoke!");
3971 setNormalDest(NewSucc);
3973 setUnwindDest(NewSucc);
3980 return (
I->getOpcode() == Instruction::Invoke);
3983 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3989 template <
typename Bitfield>
3991 Instruction::setSubclassData<Bitfield>(
Value);
3997 ArrayRef<OperandBundleDef> Bundles,
int NumOperands,
3998 const Twine &NameStr, Instruction *InsertBefore)
3999 : CallBase(Ty->getReturnType(), Instruction::Invoke,
4000 OperandTraits<CallBase>::op_end(
this) - NumOperands, NumOperands,
4002 init(Ty, Func, IfNormal, IfException,
Args, Bundles, NameStr);
4007 ArrayRef<OperandBundleDef> Bundles,
int NumOperands,
4008 const Twine &NameStr,
BasicBlock *InsertAtEnd)
4009 : CallBase(Ty->getReturnType(), Instruction::Invoke,
4010 OperandTraits<CallBase>::op_end(
this) - NumOperands, NumOperands,
4012 init(Ty, Func, IfNormal, IfException,
Args, Bundles, NameStr);
4025 unsigned NumIndirectDests;
4049 static int ComputeNumOperands(
int NumArgs,
int NumIndirectDests,
4050 int NumBundleInputs = 0) {
4053 return 2 + NumIndirectDests + NumArgs + NumBundleInputs;
4068 int NumOperands = ComputeNumOperands(
Args.size(), IndirectDests.
size());
4069 return new (NumOperands)
4070 CallBrInst(Ty, Func, DefaultDest, IndirectDests,
Args, std::nullopt,
4071 NumOperands, NameStr, InsertBefore);
4079 int NumOperands = ComputeNumOperands(
Args.size(), IndirectDests.
size(),
4083 return new (NumOperands, DescriptorBytes)
4085 NumOperands, NameStr, InsertBefore);
4093 int NumOperands = ComputeNumOperands(
Args.size(), IndirectDests.
size());
4094 return new (NumOperands)
4095 CallBrInst(Ty, Func, DefaultDest, IndirectDests,
Args, std::nullopt,
4096 NumOperands, NameStr, InsertAtEnd);
4105 int NumOperands = ComputeNumOperands(
Args.size(), IndirectDests.
size(),
4109 return new (NumOperands, DescriptorBytes)
4111 NumOperands, NameStr, InsertAtEnd);
4118 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4119 IndirectDests,
Args, NameStr, InsertBefore);
4126 const Twine &NameStr =
"",
4128 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4129 IndirectDests,
Args, Bundles, NameStr, InsertBefore);
4136 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4137 IndirectDests,
Args, NameStr, InsertAtEnd);
4146 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4147 IndirectDests,
Args, Bundles, NameStr, InsertAtEnd);
4187 return IndirectDests;
4198 "Successor # out of range for callbr!");
4204 "Successor # out of range for callbr!");
4212 return (
I->getOpcode() == Instruction::CallBr);
4215 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
4221 template <
typename Bitfield>
4223 Instruction::setSubclassData<Bitfield>(
Value);
4228 ArrayRef<BasicBlock *> IndirectDests,
4229 ArrayRef<Value *>
Args,
4230 ArrayRef<OperandBundleDef> Bundles,
int NumOperands,
4231 const Twine &NameStr, Instruction *InsertBefore)
4232 : CallBase(Ty->getReturnType(), Instruction::CallBr,
4233 OperandTraits<CallBase>::op_end(
this) - NumOperands, NumOperands,
4235 init(Ty, Func, DefaultDest, IndirectDests,
Args, Bundles, NameStr);
4239 ArrayRef<BasicBlock *> IndirectDests,
4240 ArrayRef<Value *>
Args,
4241 ArrayRef<OperandBundleDef> Bundles,
int NumOperands,
4242 const Twine &NameStr,
BasicBlock *InsertAtEnd)
4243 : CallBase(Ty->getReturnType(), Instruction::CallBr,
4244 OperandTraits<CallBase>::op_end(
this) - NumOperands, NumOperands,
4246 init(Ty, Func, DefaultDest, IndirectDests,
Args, Bundles, NameStr);
4287 return I->getOpcode() == Instruction::Resume;
4290 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));
4294 BasicBlock *getSuccessor(
unsigned idx)
const {
4298 void setSuccessor(
unsigned idx,
BasicBlock *NewSucc) {
4318 unsigned ReservedSpace;
4330 unsigned NumHandlers,
const Twine &NameStr,
4338 unsigned NumHandlers,
const Twine &NameStr,
4342 void *
operator new(
size_t S) {
return User::operator
new(
S); }
4345 void growOperands(
unsigned Size);
4354 void operator delete(
void *
Ptr) {
return User::operator
delete(
Ptr); }
4357 unsigned NumHandlers,
4358 const Twine &NameStr =
"",
4360 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4365 unsigned NumHandlers,
const Twine &NameStr,
4367 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4382 if (hasUnwindDest())
4383 return cast<BasicBlock>(getOperand(1));
4389 setOperand(1, UnwindDest);
4395 if (hasUnwindDest())
4396 return getNumOperands() - 2;
4397 return getNumOperands() - 1;
4401 static BasicBlock *handler_helper(
Value *V) {
return cast<BasicBlock>(V); }
4403 return cast<BasicBlock>(V);
4418 if (hasUnwindDest())
4427 if (hasUnwindDest())
4446 return make_range(handler_begin(), handler_end());
4451 return make_range(handler_begin(), handler_end());
4460 void removeHandler(handler_iterator
HI);
4464 assert(Idx < getNumSuccessors() &&
4465 "Successor # out of range for catchswitch!");
4466 return cast<BasicBlock>(getOperand(Idx + 1));
4469 assert(Idx < getNumSuccessors() &&
4470 "Successor # out of range for catchswitch!");
4471 setOperand(Idx + 1, NewSucc);
4476 return I->getOpcode() == Instruction::CatchSwitch;
4479 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4494 unsigned Values,
const Twine &NameStr,
4497 NameStr, InsertBefore) {}
4499 unsigned Values,
const Twine &NameStr,
4502 NameStr, InsertAtEnd) {}
4507 const Twine &NameStr =
"",
4509 unsigned Values = 1 +
Args.size();
4516 unsigned Values = 1 +
Args.size();
4523 return I->getOpcode() == Instruction::CleanupPad;
4526 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4536 unsigned Values,
const Twine &NameStr,
4539 NameStr, InsertBefore) {}
4541 unsigned Values,
const Twine &NameStr,
4544 NameStr, InsertAtEnd) {}
4548 const Twine &NameStr =
"",
4550 unsigned Values = 1 +
Args.size();
4557 unsigned Values = 1 +
Args.size();
4564 return cast<CatchSwitchInst>(Op<-1>());
4568 Op<-1>() = CatchSwitch;
4573 return I->getOpcode() == Instruction::CatchPad;
4576 return isa<Instruction>(V) &&
classof(cast<Instruction>(V));