llvm.org GIT mirror llvm / 9fa222d
[TI removal] Leverage the fact that TerminatorInst is gone to create a normal base class that provides all common "call" functionality. This merges two complex CRTP mixins for the common "call" logic and common operand bundle logic into a single, normal base class of `CallInst` and `InvokeInst`. Going forward, users can typically `dyn_cast<CallBase>` and use the resulting API. No more need for the `CallSite` wrapper. I'm planning to migrate current usage of the wrapper to directly use the base class and then it can be removed, but those are simpler and much more incremental steps. The big change is to introduce this abstraction into the type system. I've tried to do some basic simplifications of the APIs that I couldn't really help but touch as part of this: - I've tried to organize the attribute API and bundle API into groups to make understanding the API of `CallBase` easier. Without this, I wasn't able to navigate the API sanely for all of the ways I needed to modify it. - I've added what seem like more clear and consistent APIs for getting at the called operand. These ended up being especially useful to consolidate the *numerous* duplicated code paths trying to do this. - I've largely reworked the organization and implementation of the APIs for computing the argument operands as they needed to change to work with the new subclass approach. To minimize any cost associated with this abstraction, I've moved the operand layout in memory to store the called operand last. This makes its position relative to the end of the operand array the same, regardless of the subclass. It should make it much cheaper to reference from the `CallBase` abstraction, and this is likely one of the most frequent things to query. We do still pay one abstraction penalty here: we have to branch to determine whether there are 0 or 2 extra operands when computing the end of the argument operand sequence. However, that seems both rare and should optimize well. I've implemented this in a way specifically designed to allow it to optimize fairly well. If this shows up in profiles, we can add overrides of the relevant methods to the subclasses that bypass this penalty. It seems very unlikely that this will be an issue as the code was *already* dealing with an ever present abstraction of whether or not there are operand bundles, so this isn't the first branch to go into the computation. I've tried to remove as much of the obvious vestigial API surface of the old CRTP implementation as I could, but I suspect there is further cleanup that should now be possible, especially around the operand bundle APIs. I'm leaving all of that for future work in this patch as enough things are changing here as-is. One thing that made this harder for me to reason about and debug was the pervasive use of unsigned values in subtraction and other arithmetic computations. I had to debug more than one unintentional wrap. I've switched a few of these to use `int` which seems substantially simpler, but I've held back from doing this more broadly to avoid creating confusing divergence within a single class's API. I also worked to remove all of the magic numbers used to index into operands, putting them behind named constants or putting them into a single method with a comment and strictly using the method elsewhere. This was necessary to be able to re-layout the operands as discussed above. Thanks to Ben for reviewing this (somewhat large and awkward) patch! Differential Revision: https://reviews.llvm.org/D54788 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@347452 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 10 months ago
5 changed file(s) with 810 addition(s) and 818 deletion(s). Raw diff Collapse all Expand all
655655
656656 private:
657657 IterTy getCallee() const {
658 if (isCall()) // Skip Callee
659 return cast(getInstruction())->op_end() - 1;
660 else // Skip BB, BB, Callee
661 return cast(getInstruction())->op_end() - 3;
658 return cast(getInstruction())->op_end() - 1;
662659 }
663660 };
664661
2424 #include "llvm/ADT/Twine.h"
2525 #include "llvm/ADT/iterator_range.h"
2626 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/CallingConv.h"
2728 #include "llvm/IR/Constants.h"
2829 #include "llvm/IR/DerivedTypes.h"
2930 #include "llvm/IR/Instruction.h"
903904
904905 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
905906
906 //===----------------------------------------------------------------------===//
907 // FuncletPadInst Class
908 //===----------------------------------------------------------------------===//
909 class FuncletPadInst : public Instruction {
910 private:
911 FuncletPadInst(const FuncletPadInst &CPI);
912
913 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
914 ArrayRef Args, unsigned Values,
915 const Twine &NameStr, Instruction *InsertBefore);
916 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
917 ArrayRef Args, unsigned Values,
918 const Twine &NameStr, BasicBlock *InsertAtEnd);
919
920 void init(Value *ParentPad, ArrayRef Args, const Twine &NameStr);
921
922 protected:
923 // Note: Instruction needs to be a friend here to call cloneImpl.
924 friend class Instruction;
925 friend class CatchPadInst;
926 friend class CleanupPadInst;
927
928 FuncletPadInst *cloneImpl() const;
929
930 public:
931 /// Provide fast operand accessors
932 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
933
934 /// getNumArgOperands - Return the number of funcletpad arguments.
935 ///
936 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
937
938 /// Convenience accessors
939
940 /// Return the outer EH-pad this funclet is nested within.
941 ///
942 /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
943 /// is a CatchPadInst.
944 Value *getParentPad() const { return Op<-1>(); }
945 void setParentPad(Value *ParentPad) {
946 assert(ParentPad);
947 Op<-1>() = ParentPad;
948 }
949
950 /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
951 ///
952 Value *getArgOperand(unsigned i) const { return getOperand(i); }
953 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
954
955 /// arg_operands - iteration adapter for range-for loops.
956 op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
957
958 /// arg_operands - iteration adapter for range-for loops.
959 const_op_range arg_operands() const {
960 return const_op_range(op_begin(), op_end() - 1);
961 }
962
963 // Methods for support type inquiry through isa, cast, and dyn_cast:
964 static bool classof(const Instruction *I) { return I->isFuncletPad(); }
965 static bool classof(const Value *V) {
966 return isa(V) && classof(cast(V));
967 }
968 };
969
970 template <>
971 struct OperandTraits
972 : public VariadicOperandTraits {};
973
974 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)
975
976907 /// A lightweight accessor for an operand bundle meant to be passed
977908 /// around by value.
978909 struct OperandBundleUse {
1057988 using OperandBundleDef = OperandBundleDefT;
1058989 using ConstOperandBundleDef = OperandBundleDefT;
1059990
1060 /// A mixin to add operand bundle functionality to llvm instruction
1061 /// classes.
991 //===----------------------------------------------------------------------===//
992 // CallBase Class
993 //===----------------------------------------------------------------------===//
994
995 /// Base class for all callable instructions (InvokeInst and CallInst)
996 /// Holds everything related to calling a function.
1062997 ///
1063 /// OperandBundleUser uses the descriptor area co-allocated with the host User
1064 /// to store some meta information about which operands are "normal" operands,
1065 /// and which ones belong to some operand bundle.
998 /// All call-like instructions are required to use a common operand layout:
999 /// - Zero or more arguments to the call,
1000 /// - Zero or more operand bundles with zero or more operand inputs each
1001 /// bundle,
1002 /// - Zero or more subclass controlled operands
1003 /// - The called function.
10661004 ///
1067 /// The layout of an operand bundle user is
1068 ///
1069 /// +-----------uint32_t End-------------------------------------+
1070 /// | |
1071 /// | +--------uint32_t Begin--------------------+ |
1072 /// | | | |
1073 /// ^ ^ v v
1074 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1075 /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un
1076 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1077 /// v v ^ ^
1078 /// | | | |
1079 /// | +--------uint32_t Begin------------+ |
1080 /// | |
1081 /// +-----------uint32_t End-----------------------------+
1082 ///
1083 ///
1084 /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use list.
1085 /// These descriptions are installed and managed by this class, and they're all
1086 /// instances of OperandBundleUser::BundleOpInfo.
1087 ///
1088 /// DU is an additional descriptor installed by User's 'operator new' to keep
1089 /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not
1090 /// access or modify DU in any way, it's an implementation detail private to
1091 /// User.
1092 ///
1093 /// The regular Use& vector for the User starts at U0. The operand bundle uses
1094 /// are part of the Use& vector, just like normal uses. In the diagram above,
1095 /// the operand bundle uses start at BOI0_U0. Each instance of BundleOpInfo has
1096 /// information about a contiguous set of uses constituting an operand bundle,
1097 /// and the total set of operand bundle uses themselves form a contiguous set of
1098 /// uses (i.e. there are no gaps between uses corresponding to individual
1099 /// operand bundles).
1100 ///
1101 /// This class does not know the location of the set of operand bundle uses
1102 /// within the use list -- that is decided by the User using this class via the
1103 /// BeginIdx argument in populateBundleOperandInfos.
1104 ///
1105 /// Currently operand bundle users with hung-off operands are not supported.
1106 template class OperandBundleUser {
1005 /// This allows this base class to easily access the called function and the
1006 /// start of the arguments without knowing how many other operands a particular
1007 /// subclass requires. Note that accessing the end of the argument list isn't
1008 /// as cheap as most other operations on the base class.
1009 class CallBase : public Instruction {
1010 protected:
1011 /// The last operand is the called operand.
1012 static constexpr int CalledOperandOpEndIdx = -1;
1013
1014 AttributeList Attrs; ///< parameter attributes for callable
1015 FunctionType *FTy;
1016
1017 template
1018 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1019 : Instruction(std::forward(Args)...), Attrs(A), FTy(FT) {}
1020
1021 using Instruction::Instruction;
1022
1023 bool hasDescriptor() const { return Value::HasDescriptor; }
1024
1025 unsigned getNumSubclassExtraOperands() const {
1026 switch (getOpcode()) {
1027 case Instruction::Call:
1028 return 0;
1029 case Instruction::Invoke:
1030 return 2;
1031 }
1032 llvm_unreachable("Invalid opcode!");
1033 }
1034
11071035 public:
1036 using Instruction::getContext;
1037
1038 static bool classof(const Instruction *I) {
1039 return I->getOpcode() == Instruction::Call ||
1040 I->getOpcode() == Instruction::Invoke;
1041 }
1042
1043 FunctionType *getFunctionType() const { return FTy; }
1044
1045 void mutateFunctionType(FunctionType *FTy) {
1046 Value::mutateType(FTy->getReturnType());
1047 this->FTy = FTy;
1048 }
1049
1050 /// Return the iterator pointing to the beginning of the argument list.
1051 User::op_iterator arg_begin() { return op_begin(); }
1052 User::const_op_iterator arg_begin() const {
1053 return const_cast(this)->arg_begin();
1054 }
1055
1056 /// Return the iterator pointing to the end of the argument list.
1057 User::op_iterator arg_end() {
1058 // Walk from the end of the operands over the called operand, the subclass
1059 // operands, and any operands for bundles to find the end of the argument
1060 // operands.
1061 return op_end() - getNumTotalBundleOperands() -
1062 getNumSubclassExtraOperands() - 1;
1063 }
1064 User::const_op_iterator arg_end() const {
1065 return const_cast(this)->arg_end();
1066 }
1067
1068 /// Iteration adapter for range-for loops.
1069 iterator_range arg_operands() {
1070 return make_range(arg_begin(), arg_end());
1071 }
1072 iterator_range arg_operands() const {
1073 return make_range(arg_begin(), arg_end());
1074 }
1075
1076 unsigned getNumArgOperands() const { return arg_end() - arg_begin(); }
1077
1078 Value *getArgOperand(unsigned i) const {
1079 assert(i < getNumArgOperands() && "Out of bounds!");
1080 return getOperand(i);
1081 }
1082
1083 void setArgOperand(unsigned i, Value *v) {
1084 assert(i < getNumArgOperands() && "Out of bounds!");
1085 setOperand(i, v);
1086 }
1087
1088 /// Wrappers for getting the \c Use of a call argument.
1089 const Use &getArgOperandUse(unsigned i) const {
1090 assert(i < getNumArgOperands() && "Out of bounds!");
1091 return User::getOperandUse(i);
1092 }
1093 Use &getArgOperandUse(unsigned i) {
1094 assert(i < getNumArgOperands() && "Out of bounds!");
1095 return User::getOperandUse(i);
1096 }
1097
1098 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1099
1100 Value *getCalledOperand() const { return Op(); }
1101
1102 // DEPRECATED: This routine will be removed in favor of `getCalledOperand` in
1103 // the near future.
1104 Value *getCalledValue() const { return getCalledOperand(); }
1105
1106 const Use &getCalledOperandUse() const { return Op(); }
1107 Use &getCalledOperandUse() { return Op(); }
1108
1109 /// Returns the function called, or null if this is an
1110 /// indirect function invocation.
1111 Function *getCalledFunction() const {
1112 return dyn_cast_or_null(getCalledOperand());
1113 }
1114
1115 void setCalledOperand(Value *V) { Op() = V; }
1116
1117 /// Sets the function called, including updating the function type.
1118 void setCalledFunction(Value *Fn) {
1119 setCalledFunction(
1120 cast(cast(Fn->getType())->getElementType()),
1121 Fn);
1122 }
1123
1124 /// Sets the function called, including updating to the specified function
1125 /// type.
1126 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1127 this->FTy = FTy;
1128 assert(FTy == cast(
1129 cast(Fn->getType())->getElementType()));
1130 setCalledOperand(Fn);
1131 }
1132
1133 CallingConv::ID getCallingConv() const {
1134 return static_cast(getSubclassDataFromInstruction() >> 2);
1135 }
1136
1137 void setCallingConv(CallingConv::ID CC) {
1138 auto ID = static_cast(CC);
1139 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1140 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1141 (ID << 2));
1142 }
1143
1144 /// \name Attribute API
1145 ///
1146 /// These methods access and modify attributes on this call (including
1147 /// looking through to the attributes on the called function when necessary).
1148 ///@{
1149
1150 /// Return the parameter attributes for this call.
1151 ///
1152 AttributeList getAttributes() const { return Attrs; }
1153
1154 /// Set the parameter attributes for this call.
1155 ///
1156 void setAttributes(AttributeList A) { Attrs = A; }
1157
1158 /// Determine whether this call has the given attribute.
1159 bool hasFnAttr(Attribute::AttrKind Kind) const {
1160 assert(Kind != Attribute::NoBuiltin &&
1161 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
1162 return hasFnAttrImpl(Kind);
1163 }
1164
1165 /// Determine whether this call has the given attribute.
1166 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1167
1168 /// adds the attribute to the list of attributes.
1169 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
1170 AttributeList PAL = getAttributes();
1171 PAL = PAL.addAttribute(getContext(), i, Kind);
1172 setAttributes(PAL);
1173 }
1174
1175 /// adds the attribute to the list of attributes.
1176 void addAttribute(unsigned i, Attribute Attr) {
1177 AttributeList PAL = getAttributes();
1178 PAL = PAL.addAttribute(getContext(), i, Attr);
1179 setAttributes(PAL);
1180 }
1181
1182 /// Adds the attribute to the indicated argument
1183 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1184 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1185 AttributeList PAL = getAttributes();
1186 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
1187 setAttributes(PAL);
1188 }
1189
1190 /// Adds the attribute to the indicated argument
1191 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1192 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1193 AttributeList PAL = getAttributes();
1194 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
1195 setAttributes(PAL);
1196 }
1197
1198 /// removes the attribute from the list of attributes.
1199 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
1200 AttributeList PAL = getAttributes();
1201 PAL = PAL.removeAttribute(getContext(), i, Kind);
1202 setAttributes(PAL);
1203 }
1204
1205 /// removes the attribute from the list of attributes.
1206 void removeAttribute(unsigned i, StringRef Kind) {
1207 AttributeList PAL = getAttributes();
1208 PAL = PAL.removeAttribute(getContext(), i, Kind);
1209 setAttributes(PAL);
1210 }
1211
1212 /// Removes the attribute from the given argument
1213 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1214 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1215 AttributeList PAL = getAttributes();
1216 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1217 setAttributes(PAL);
1218 }
1219
1220 /// Removes the attribute from the given argument
1221 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1222 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1223 AttributeList PAL = getAttributes();
1224 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1225 setAttributes(PAL);
1226 }
1227
1228 /// adds the dereferenceable attribute to the list of attributes.
1229 void addDereferenceableAttr(unsigned i, uint64_t Bytes) {
1230 AttributeList PAL = getAttributes();
1231 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
1232 setAttributes(PAL);
1233 }
1234
1235 /// adds the dereferenceable_or_null attribute to the list of
1236 /// attributes.
1237 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
1238 AttributeList PAL = getAttributes();
1239 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
1240 setAttributes(PAL);
1241 }
1242
1243 /// Determine whether the return value has the given attribute.
1244 bool hasRetAttr(Attribute::AttrKind Kind) const;
1245
1246 /// Determine whether the argument or parameter has the given attribute.
1247 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
1248
1249 /// Get the attribute of a given kind at a position.
1250 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1251 return getAttributes().getAttribute(i, Kind);
1252 }
1253
1254 /// Get the attribute of a given kind at a position.
1255 Attribute getAttribute(unsigned i, StringRef Kind) const {
1256 return getAttributes().getAttribute(i, Kind);
1257 }
1258
1259 /// Get the attribute of a given kind from a given arg
1260 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1261 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1262 return getAttributes().getParamAttr(ArgNo, Kind);
1263 }
1264
1265 /// Get the attribute of a given kind from a given arg
1266 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1267 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1268 return getAttributes().getParamAttr(ArgNo, Kind);
1269 }
1270
1271 /// Return true if the data operand at index \p i has the attribute \p
1272 /// A.
1273 ///
1274 /// Data operands include call arguments and values used in operand bundles,
1275 /// but does not include the callee operand. This routine dispatches to the
1276 /// underlying AttributeList or the OperandBundleUser as appropriate.
1277 ///
1278 /// The index \p i is interpreted as
1279 ///
1280 /// \p i == Attribute::ReturnIndex -> the return value
1281 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1282 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1283 /// (\p i - 1) in the operand list.
1284 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1285 // Note that we have to add one because `i` isn't zero-indexed.
1286 assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&
1287 "Data operand index out of bounds!");
1288
1289 // The attribute A can either be directly specified, if the operand in
1290 // question is a call argument; or be indirectly implied by the kind of its
1291 // containing operand bundle, if the operand is a bundle operand.
1292
1293 if (i == AttributeList::ReturnIndex)
1294 return hasRetAttr(Kind);
1295
1296 // FIXME: Avoid these i - 1 calculations and update the API to use
1297 // zero-based indices.
1298 if (i < (getNumArgOperands() + 1))
1299 return paramHasAttr(i - 1, Kind);
1300
1301 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
1302 "Must be either a call argument or an operand bundle!");
1303 return bundleOperandHasAttr(i - 1, Kind);
1304 }
1305
1306 /// Extract the alignment of the return value.
1307 unsigned getRetAlignment() const { return Attrs.getRetAlignment(); }
1308
1309 /// Extract the alignment for a call or parameter (0=unknown).
1310 unsigned getParamAlignment(unsigned ArgNo) const {
1311 return Attrs.getParamAlignment(ArgNo);
1312 }
1313
1314 /// Extract the number of dereferenceable bytes for a call or
1315 /// parameter (0=unknown).
1316 uint64_t getDereferenceableBytes(unsigned i) const {
1317 return Attrs.getDereferenceableBytes(i);
1318 }
1319
1320 /// Extract the number of dereferenceable_or_null bytes for a call or
1321 /// parameter (0=unknown).
1322 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1323 return Attrs.getDereferenceableOrNullBytes(i);
1324 }
1325
1326 /// Determine if the return value is marked with NoAlias attribute.
1327 bool returnDoesNotAlias() const {
1328 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1329 }
1330
1331 /// If one of the arguments has the 'returned' attribute, returns its
1332 /// operand value. Otherwise, return nullptr.
1333 Value *getReturnedArgOperand() const;
1334
1335 /// Return true if the call should not be treated as a call to a
1336 /// builtin.
1337 bool isNoBuiltin() const {
1338 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1339 !hasFnAttrImpl(Attribute::Builtin);
1340 }
1341
1342 /// Determine if the call requires strict floating point semantics.
1343 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1344
1345 /// Return true if the call should not be inlined.
1346 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1347 void setIsNoInline() {
1348 addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1349 }
1350 /// Determine if the call does not access memory.
1351 bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); }
1352 void setDoesNotAccessMemory() {
1353 addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
1354 }
1355
1356 /// Determine if the call does not access or only reads memory.
1357 bool onlyReadsMemory() const {
1358 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1359 }
1360 void setOnlyReadsMemory() {
1361 addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
1362 }
1363
1364 /// Determine if the call does not access or only writes memory.
1365 bool doesNotReadMemory() const {
1366 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1367 }
1368 void setDoesNotReadMemory() {
1369 addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
1370 }
1371
1372 /// Determine if the call can access memmory only using pointers based
1373 /// on its arguments.
1374 bool onlyAccessesArgMemory() const {
1375 return hasFnAttr(Attribute::ArgMemOnly);
1376 }
1377 void setOnlyAccessesArgMemory() {
1378 addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly);
1379 }
1380
1381 /// Determine if the function may only access memory that is
1382 /// inaccessible from the IR.
1383 bool onlyAccessesInaccessibleMemory() const {
1384 return hasFnAttr(Attribute::InaccessibleMemOnly);
1385 }
1386 void setOnlyAccessesInaccessibleMemory() {
1387 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly);
1388 }
1389
1390 /// Determine if the function may only access memory that is
1391 /// either inaccessible from the IR or pointed to by its arguments.
1392 bool onlyAccessesInaccessibleMemOrArgMem() const {
1393 return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
1394 }
1395 void setOnlyAccessesInaccessibleMemOrArgMem() {
1396 addAttribute(AttributeList::FunctionIndex,
1397 Attribute::InaccessibleMemOrArgMemOnly);
1398 }
1399 /// Determine if the call cannot return.
1400 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1401 void setDoesNotReturn() {
1402 addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
1403 }
1404
1405 /// Determine if the call should not perform indirect branch tracking.
1406 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1407
1408 /// Determine if the call cannot unwind.
1409 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1410 void setDoesNotThrow() {
1411 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1412 }
1413
1414 /// Determine if the invoke cannot be duplicated.
1415 bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); }
1416 void setCannotDuplicate() {
1417 addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate);
1418 }
1419
1420 /// Determine if the invoke is convergent
1421 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1422 void setConvergent() {
1423 addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1424 }
1425 void setNotConvergent() {
1426 removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1427 }
1428
1429 /// Determine if the call returns a structure through first
1430 /// pointer argument.
1431 bool hasStructRetAttr() const {
1432 if (getNumArgOperands() == 0)
1433 return false;
1434
1435 // Be friendly and also check the callee.
1436 return paramHasAttr(0, Attribute::StructRet);
1437 }
1438
1439 /// Determine if any call argument is an aggregate passed by value.
1440 bool hasByValArgument() const {
1441 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1442 }
1443
1444 ///@{
1445 // End of attribute API.
1446
1447 /// \name Operand Bundle API
1448 ///
1449 /// This group of methods provides the API to access and manipulate operand
1450 /// bundles on this call.
1451 /// @{
1452
11081453 /// Return the number of operand bundles associated with this User.
11091454 unsigned getNumOperandBundles() const {
11101455 return std::distance(bundle_op_info_begin(), bundle_op_info_end());
12601605 /// Return true if \p Other has the same sequence of operand bundle
12611606 /// tags with the same number of operands on each one of them as this
12621607 /// OperandBundleUser.
1263 bool hasIdenticalOperandBundleSchema(
1264 const OperandBundleUser &Other) const {
1608 bool hasIdenticalOperandBundleSchema(const CallBase &Other) const {
12651609 if (getNumOperandBundles() != Other.getNumOperandBundles())
12661610 return false;
12671611
12801624 return false;
12811625 }
12821626
1283 protected:
12841627 /// Is the function attribute S disallowed by some operand bundle on
12851628 /// this operand bundle user?
12861629 bool isFnAttrDisallowedByOpBundle(StringRef S) const {
13391682 /// OperandBundleUse.
13401683 OperandBundleUse
13411684 operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const {
1342 auto op_begin = static_cast(this)->op_begin();
1343 ArrayRef Inputs(op_begin + BOI.Begin, op_begin + BOI.End);
1685 auto begin = op_begin();
1686 ArrayRef Inputs(begin + BOI.Begin, begin + BOI.End);
13441687 return OperandBundleUse(BOI.Tag, Inputs);
13451688 }
13461689
13491692
13501693 /// Return the start of the list of BundleOpInfo instances associated
13511694 /// with this OperandBundleUser.
1695 ///
1696 /// OperandBundleUser uses the descriptor area co-allocated with the host User
1697 /// to store some meta information about which operands are "normal" operands,
1698 /// and which ones belong to some operand bundle.
1699 ///
1700 /// The layout of an operand bundle user is
1701 ///
1702 /// +-----------uint32_t End-------------------------------------+
1703 /// | |
1704 /// | +--------uint32_t Begin--------------------+ |
1705 /// | | | |
1706 /// ^ ^ v v
1707 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1708 /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un
1709 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1710 /// v v ^ ^
1711 /// | | | |
1712 /// | +--------uint32_t Begin------------+ |
1713 /// | |
1714 /// +-----------uint32_t End-----------------------------+
1715 ///
1716 ///
1717 /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use
1718 /// list. These descriptions are installed and managed by this class, and
1719 /// they're all instances of OperandBundleUser::BundleOpInfo.
1720 ///
1721 /// DU is an additional descriptor installed by User's 'operator new' to keep
1722 /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not
1723 /// access or modify DU in any way, it's an implementation detail private to
1724 /// User.
1725 ///
1726 /// The regular Use& vector for the User starts at U0. The operand bundle
1727 /// uses are part of the Use& vector, just like normal uses. In the diagram
1728 /// above, the operand bundle uses start at BOI0_U0. Each instance of
1729 /// BundleOpInfo has information about a contiguous set of uses constituting
1730 /// an operand bundle, and the total set of operand bundle uses themselves
1731 /// form a contiguous set of uses (i.e. there are no gaps between uses
1732 /// corresponding to individual operand bundles).
1733 ///
1734 /// This class does not know the location of the set of operand bundle uses
1735 /// within the use list -- that is decided by the User using this class via
1736 /// the BeginIdx argument in populateBundleOperandInfos.
1737 ///
1738 /// Currently operand bundle users with hung-off operands are not supported.
13521739 bundle_op_iterator bundle_op_info_begin() {
1353 if (!static_cast(this)->hasDescriptor())
1740 if (!hasDescriptor())
13541741 return nullptr;
13551742
1356 uint8_t *BytesBegin = static_cast(this)->getDescriptor().begin();
1743 uint8_t *BytesBegin = getDescriptor().begin();
13571744 return reinterpret_cast(BytesBegin);
13581745 }
13591746
13601747 /// Return the start of the list of BundleOpInfo instances associated
13611748 /// with this OperandBundleUser.
13621749 const_bundle_op_iterator bundle_op_info_begin() const {
1363 auto *NonConstThis =
1364 const_cast *>(this);
1750 auto *NonConstThis = const_cast *>(this);
13651751 return NonConstThis->bundle_op_info_begin();
13661752 }
13671753
13681754 /// Return the end of the list of BundleOpInfo instances associated
13691755 /// with this OperandBundleUser.
13701756 bundle_op_iterator bundle_op_info_end() {
1371 if (!static_cast(this)->hasDescriptor())
1757 if (!hasDescriptor())
13721758 return nullptr;
13731759
1374 uint8_t *BytesEnd = static_cast(this)->getDescriptor().end();
1760 uint8_t *BytesEnd = getDescriptor().end();
13751761 return reinterpret_cast(BytesEnd);
13761762 }
13771763
13781764 /// Return the end of the list of BundleOpInfo instances associated
13791765 /// with this OperandBundleUser.
13801766 const_bundle_op_iterator bundle_op_info_end() const {
1381 auto *NonConstThis =
1382 const_cast *>(this);
1767 auto *NonConstThis = const_cast *>(this);
13831768 return NonConstThis->bundle_op_info_end();
13841769 }
13851770
13991784 ///
14001785 /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo
14011786 /// instance allocated in this User's descriptor.
1402 OpIteratorTy populateBundleOperandInfos(ArrayRef Bundles,
1403 const unsigned BeginIndex) {
1404 auto It = static_cast(this)->op_begin() + BeginIndex;
1405 for (auto &B : Bundles)
1406 It = std::copy(B.input_begin(), B.input_end(), It);
1407
1408 auto *ContextImpl = static_cast(this)->getContext().pImpl;
1409 auto BI = Bundles.begin();
1410 unsigned CurrentIndex = BeginIndex;
1411
1412 for (auto &BOI : bundle_op_infos()) {
1413 assert(BI != Bundles.end() && "Incorrect allocation?");
1414
1415 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
1416 BOI.Begin = CurrentIndex;
1417 BOI.End = CurrentIndex + BI->input_size();
1418 CurrentIndex = BOI.End;
1419 BI++;
1420 }
1421
1422 assert(BI == Bundles.end() && "Incorrect allocation?");
1423
1424 return It;
1425 }
1787 op_iterator populateBundleOperandInfos(ArrayRef Bundles,
1788 const unsigned BeginIndex);
14261789
14271790 /// Return the BundleOpInfo for the operand at index OpIdx.
14281791 ///
14361799 llvm_unreachable("Did not find operand bundle for operand!");
14371800 }
14381801
1802 protected:
14391803 /// Return the total number of values used in \p Bundles.
14401804 static unsigned CountBundleInputs(ArrayRef Bundles) {
14411805 unsigned Total = 0;
14431807 Total += B.input_size();
14441808 return Total;
14451809 }
1810
1811 /// @}
1812 // End of operand bundle API.
1813
1814 private:
1815 bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
1816 bool hasFnAttrOnCalledFunction(StringRef Kind) const;
1817
1818 template bool hasFnAttrImpl(AttrKind Kind) const {
1819 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Kind))
1820 return true;
1821
1822 // Operand bundles override attributes on the called function, but don't
1823 // override attributes directly present on the call instruction.
1824 if (isFnAttrDisallowedByOpBundle(Kind))
1825 return false;
1826
1827 return hasFnAttrOnCalledFunction(Kind);
1828 }
14461829 };
14471830
1831 template <>
1832 struct OperandTraits : public VariadicOperandTraits {};
1833
1834 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)
1835
1836 //===----------------------------------------------------------------------===//
1837 // FuncletPadInst Class
1838 //===----------------------------------------------------------------------===//
1839 class FuncletPadInst : public Instruction {
1840 private:
1841 FuncletPadInst(const FuncletPadInst &CPI);
1842
1843 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1844 ArrayRef Args, unsigned Values,
1845 const Twine &NameStr, Instruction *InsertBefore);
1846 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1847 ArrayRef Args, unsigned Values,
1848 const Twine &NameStr, BasicBlock *InsertAtEnd);
1849
1850 void init(Value *ParentPad, ArrayRef Args, const Twine &NameStr);
1851
1852 protected:
1853 // Note: Instruction needs to be a friend here to call cloneImpl.
1854 friend class Instruction;
1855 friend class CatchPadInst;
1856 friend class CleanupPadInst;
1857
1858 FuncletPadInst *cloneImpl() const;
1859
1860 public:
1861 /// Provide fast operand accessors
1862 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1863
1864 /// getNumArgOperands - Return the number of funcletpad arguments.
1865 ///
1866 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1867
1868 /// Convenience accessors
1869
1870 /// Return the outer EH-pad this funclet is nested within.
1871 ///
1872 /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
1873 /// is a CatchPadInst.
1874 Value *getParentPad() const { return Op<-1>(); }
1875 void setParentPad(Value *ParentPad) {
1876 assert(ParentPad);
1877 Op<-1>() = ParentPad;
1878 }
1879
1880 /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
1881 ///
1882 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1883 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1884
1885 /// arg_operands - iteration adapter for range-for loops.
1886 op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
1887
1888 /// arg_operands - iteration adapter for range-for loops.
1889 const_op_range arg_operands() const {
1890 return const_op_range(op_begin(), op_end() - 1);
1891 }
1892
1893 // Methods for support type inquiry through isa, cast, and dyn_cast:
1894 static bool classof(const Instruction *I) { return I->isFuncletPad(); }
1895 static bool classof(const Value *V) {
1896 return isa(V) && classof(cast(V));
1897 }
1898 };
1899
1900 template <>
1901 struct OperandTraits
1902 : public VariadicOperandTraits {};
1903
1904 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)
1905
14481906 } // end namespace llvm
14491907
14501908 #endif // LLVM_IR_INSTRTYPES_H
14171417 }
14181418 };
14191419
1420 class CallInst;
1421 class InvokeInst;
1422
1423 template struct CallBaseParent { using type = Instruction; };
1424
1425 //===----------------------------------------------------------------------===//
1426 /// Base class for all callable instructions (InvokeInst and CallInst)
1427 /// Holds everything related to calling a function, abstracting from the base
1428 /// type @p BaseInstTy and the concrete instruction @p InstTy
1429 ///
1430 template
1431 class CallBase : public CallBaseParent::type,
1432 public OperandBundleUser {
1433 protected:
1434 AttributeList Attrs; ///< parameter attributes for callable
1435 FunctionType *FTy;
1436 using BaseInstTy = typename CallBaseParent::type;
1437
1438 template
1439 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1440 : BaseInstTy(std::forward(Args)...), Attrs(A), FTy(FT) {}
1441 bool hasDescriptor() const { return Value::HasDescriptor; }
1442
1443 using BaseInstTy::BaseInstTy;
1444
1445 using OperandBundleUser
1446 User::op_iterator>::isFnAttrDisallowedByOpBundle;
1447 using OperandBundleUser::getNumTotalBundleOperands;
1448 using OperandBundleUser::bundleOperandHasAttr;
1449 using Instruction::getSubclassDataFromInstruction;
1450 using Instruction::setInstructionSubclassData;
1451
1452 public:
1453 using Instruction::getContext;
1454 using OperandBundleUser::hasOperandBundles;
1455 using OperandBundleUser
1456 User::op_iterator>::getBundleOperandsStartIndex;
1457
1458 static bool classof(const Instruction *I) {
1459 llvm_unreachable(
1460 "CallBase is not meant to be used as part of the classof hierarchy");
1461 }
1462
1463 public:
1464 /// Return the parameter attributes for this call.
1465 ///
1466 AttributeList getAttributes() const { return Attrs; }
1467
1468 /// Set the parameter attributes for this call.
1469 ///
1470 void setAttributes(AttributeList A) { Attrs = A; }
1471
1472 FunctionType *getFunctionType() const { return FTy; }
1473
1474 void mutateFunctionType(FunctionType *FTy) {
1475 Value::mutateType(FTy->getReturnType());
1476 this->FTy = FTy;
1477 }
1478
1479 /// Return the number of call arguments.
1480 ///
1481 unsigned getNumArgOperands() const {
1482 return getNumOperands() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1483 }
1484
1485 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1486 ///
1487 Value *getArgOperand(unsigned i) const {
1488 assert(i < getNumArgOperands() && "Out of bounds!");
1489 return getOperand(i);
1490 }
1491 void setArgOperand(unsigned i, Value *v) {
1492 assert(i < getNumArgOperands() && "Out of bounds!");
1493 setOperand(i, v);
1494 }
1495
1496 /// Return the iterator pointing to the beginning of the argument list.
1497 User::op_iterator arg_begin() { return op_begin(); }
1498
1499 /// Return the iterator pointing to the end of the argument list.
1500 User::op_iterator arg_end() {
1501 // [ call args ], [ operand bundles ], callee
1502 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1503 }
1504
1505 /// Iteration adapter for range-for loops.
1506 iterator_range arg_operands() {
1507 return make_range(arg_begin(), arg_end());
1508 }
1509
1510 /// Return the iterator pointing to the beginning of the argument list.
1511 User::const_op_iterator arg_begin() const { return op_begin(); }
1512
1513 /// Return the iterator pointing to the end of the argument list.
1514 User::const_op_iterator arg_end() const {
1515 // [ call args ], [ operand bundles ], callee
1516 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1517 }
1518
1519 /// Iteration adapter for range-for loops.
1520 iterator_range arg_operands() const {
1521 return make_range(arg_begin(), arg_end());
1522 }
1523
1524 /// Wrappers for getting the \c Use of a call argument.
1525 const Use &getArgOperandUse(unsigned i) const {
1526 assert(i < getNumArgOperands() && "Out of bounds!");
1527 return User::getOperandUse(i);
1528 }
1529 Use &getArgOperandUse(unsigned i) {
1530 assert(i < getNumArgOperands() && "Out of bounds!");
1531 return User::getOperandUse(i);
1532 }
1533
1534 /// If one of the arguments has the 'returned' attribute, return its
1535 /// operand value. Otherwise, return nullptr.
1536 Value *getReturnedArgOperand() const {
1537 unsigned Index;
1538
1539 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
1540 return getArgOperand(Index - AttributeList::FirstArgIndex);
1541 if (const Function *F = getCalledFunction())
1542 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
1543 Index)
1544 return getArgOperand(Index - AttributeList::FirstArgIndex);
1545
1546 return nullptr;
1547 }
1548
1549 User::op_iterator op_begin() {
1550 return OperandTraits::op_begin(this);
1551 }
1552
1553 User::const_op_iterator op_begin() const {
1554 return OperandTraits::op_begin(const_cast(this));
1555 }
1556
1557 User::op_iterator op_end() { return OperandTraits::op_end(this); }
1558
1559 User::const_op_iterator op_end() const {
1560 return OperandTraits::op_end(const_cast(this));
1561 }
1562
1563 Value *getOperand(unsigned i_nocapture) const {
1564 assert(i_nocapture < OperandTraits::operands(this) &&
1565 "getOperand() out of range!");
1566 return cast_or_null(OperandTraits::op_begin(
1567 const_cast(this))[i_nocapture]
1568 .get());
1569 }
1570
1571 void setOperand(unsigned i_nocapture, Value *Val_nocapture) {
1572 assert(i_nocapture < OperandTraits::operands(this) &&
1573 "setOperand() out of range!");
1574 OperandTraits::op_begin(this)[i_nocapture] = Val_nocapture;
1575 }
1576
1577 unsigned getNumOperands() const {
1578 return OperandTraits::operands(this);
1579 }
1580 template Use &Op() {
1581 return User::OpFrom(this);
1582 }
1583 template const Use &Op() const {
1584 return User::OpFrom(this);
1585 }
1586
1587 /// Return the function called, or null if this is an
1588 /// indirect function invocation.
1589 ///
1590 Function *getCalledFunction() const {
1591 return dyn_cast_or_null(Op<-InstTy::ArgOffset>());
1592 }
1593
1594 /// Determine whether this call has the given attribute.
1595 bool hasFnAttr(Attribute::AttrKind Kind) const {
1596 assert(Kind != Attribute::NoBuiltin &&
1597 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
1598 return hasFnAttrImpl(Kind);
1599 }
1600
1601 /// Determine whether this call has the given attribute.
1602 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1603
1604 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1605 /// function call.
1606 CallingConv::ID getCallingConv() const {
1607 return static_cast(getSubclassDataFromInstruction() >> 2);
1608 }
1609 void setCallingConv(CallingConv::ID CC) {
1610 auto ID = static_cast(CC);
1611 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1612 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1613 (ID << 2));
1614 }
1615
1616
1617 /// adds the attribute to the list of attributes.
1618 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
1619 AttributeList PAL = getAttributes();
1620 PAL = PAL.addAttribute(getContext(), i, Kind);
1621 setAttributes(PAL);
1622 }
1623
1624 /// adds the attribute to the list of attributes.
1625 void addAttribute(unsigned i, Attribute Attr) {
1626 AttributeList PAL = getAttributes();
1627 PAL = PAL.addAttribute(getContext(), i, Attr);
1628 setAttributes(PAL);
1629 }
1630
1631 /// Adds the attribute to the indicated argument
1632 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1633 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1634 AttributeList PAL = getAttributes();
1635 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
1636 setAttributes(PAL);
1637 }
1638
1639 /// Adds the attribute to the indicated argument
1640 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1641 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1642 AttributeList PAL = getAttributes();
1643 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
1644 setAttributes(PAL);
1645 }
1646
1647 /// removes the attribute from the list of attributes.
1648 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
1649 AttributeList PAL = getAttributes();
1650 PAL = PAL.removeAttribute(getContext(), i, Kind);
1651 setAttributes(PAL);
1652 }
1653
1654 /// removes the attribute from the list of attributes.
1655 void removeAttribute(unsigned i, StringRef Kind) {
1656 AttributeList PAL = getAttributes();
1657 PAL = PAL.removeAttribute(getContext(), i, Kind);
1658 setAttributes(PAL);
1659 }
1660
1661 /// Removes the attribute from the given argument
1662 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1663 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1664 AttributeList PAL = getAttributes();
1665 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1666 setAttributes(PAL);
1667 }
1668
1669 /// Removes the attribute from the given argument
1670 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1671 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1672 AttributeList PAL = getAttributes();
1673 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1674 setAttributes(PAL);
1675 }
1676
1677 /// adds the dereferenceable attribute to the list of attributes.
1678 void addDereferenceableAttr(unsigned i, uint64_t Bytes) {
1679 AttributeList PAL = getAttributes();
1680 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
1681 setAttributes(PAL);
1682 }
1683
1684 /// adds the dereferenceable_or_null attribute to the list of
1685 /// attributes.
1686 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
1687 AttributeList PAL = getAttributes();
1688 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
1689 setAttributes(PAL);
1690 }
1691
1692 /// Determine whether the return value has the given attribute.
1693 bool hasRetAttr(Attribute::AttrKind Kind) const {
1694 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
1695 return true;
1696
1697 // Look at the callee, if available.
1698 if (const Function *F = getCalledFunction())
1699 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
1700 return false;
1701 }
1702
1703 /// Determine whether the argument or parameter has the given attribute.
1704 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1705 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
1706
1707 if (Attrs.hasParamAttribute(ArgNo, Kind))
1708 return true;
1709 if (const Function *F = getCalledFunction())
1710 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
1711 return false;
1712 }
1713
1714 /// Get the attribute of a given kind at a position.
1715 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1716 return getAttributes().getAttribute(i, Kind);
1717 }
1718
1719 /// Get the attribute of a given kind at a position.
1720 Attribute getAttribute(unsigned i, StringRef Kind) const {
1721 return getAttributes().getAttribute(i, Kind);
1722 }
1723
1724 /// Get the attribute of a given kind from a given arg
1725 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1726 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1727 return getAttributes().getParamAttr(ArgNo, Kind);
1728 }
1729
1730 /// Get the attribute of a given kind from a given arg
1731 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1732 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1733 return getAttributes().getParamAttr(ArgNo, Kind);
1734 }
1735 /// Return true if the data operand at index \p i has the attribute \p
1736 /// A.
1737 ///
1738 /// Data operands include call arguments and values used in operand bundles,
1739 /// but does not include the callee operand. This routine dispatches to the
1740 /// underlying AttributeList or the OperandBundleUser as appropriate.
1741 ///
1742 /// The index \p i is interpreted as
1743 ///
1744 /// \p i == Attribute::ReturnIndex -> the return value
1745 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1746 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1747 /// (\p i - 1) in the operand list.
1748 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1749 // There are getNumOperands() - (InstTy::ArgOffset - 1) data operands.
1750 // The last operand is the callee.
1751 assert(i < (getNumOperands() - InstTy::ArgOffset + 1) &&
1752 "Data operand index out of bounds!");
1753
1754 // The attribute A can either be directly specified, if the operand in
1755 // question is a call argument; or be indirectly implied by the kind of its
1756 // containing operand bundle, if the operand is a bundle operand.
1757
1758 if (i == AttributeList::ReturnIndex)
1759 return hasRetAttr(Kind);
1760
1761 // FIXME: Avoid these i - 1 calculations and update the API to use
1762 // zero-based indices.
1763 if (i < (getNumArgOperands() + 1))
1764 return paramHasAttr(i - 1, Kind);
1765
1766 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
1767 "Must be either a call argument or an operand bundle!");
1768 return bundleOperandHasAttr(i - 1, Kind);
1769 }
1770
1771 /// Extract the alignment of the return value.
1772 unsigned getRetAlignment() const { return Attrs.getRetAlignment(); }
1773
1774 /// Extract the alignment for a call or parameter (0=unknown).
1775 unsigned getParamAlignment(unsigned ArgNo) const {
1776 return Attrs.getParamAlignment(ArgNo);
1777 }
1778
1779 /// Extract the number of dereferenceable bytes for a call or
1780 /// parameter (0=unknown).
1781 uint64_t getDereferenceableBytes(unsigned i) const {
1782 return Attrs.getDereferenceableBytes(i);
1783 }
1784
1785 /// Extract the number of dereferenceable_or_null bytes for a call or
1786 /// parameter (0=unknown).
1787 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1788 return Attrs.getDereferenceableOrNullBytes(i);
1789 }
1790
1791 /// Determine if the return value is marked with NoAlias attribute.
1792 bool returnDoesNotAlias() const {
1793 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1794 }
1795
1796 /// Return true if the call should not be treated as a call to a
1797 /// builtin.
1798 bool isNoBuiltin() const {
1799 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1800 !hasFnAttrImpl(Attribute::Builtin);
1801 }
1802
1803 /// Determine if the call requires strict floating point semantics.
1804 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1805
1806 /// Return true if the call should not be inlined.
1807 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1808 void setIsNoInline() {
1809 addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1810 }
1811 /// Determine if the call does not access memory.
1812 bool doesNotAccessMemory() const {
1813 return hasFnAttr(Attribute::ReadNone);
1814 }
1815 void setDoesNotAccessMemory() {
1816 addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
1817 }
1818
1819 /// Determine if the call does not access or only reads memory.
1820 bool onlyReadsMemory() const {
1821 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1822 }
1823 void setOnlyReadsMemory() {
1824 addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
1825 }
1826
1827 /// Determine if the call does not access or only writes memory.
1828 bool doesNotReadMemory() const {
1829 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1830 }
1831 void setDoesNotReadMemory() {
1832 addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
1833 }
1834
1835 /// Determine if the call can access memmory only using pointers based
1836 /// on its arguments.
1837 bool onlyAccessesArgMemory() const {
1838 return hasFnAttr(Attribute::ArgMemOnly);
1839 }
1840 void setOnlyAccessesArgMemory() {
1841 addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly);
1842 }
1843
1844 /// Determine if the function may only access memory that is
1845 /// inaccessible from the IR.
1846 bool onlyAccessesInaccessibleMemory() const {
1847 return hasFnAttr(Attribute::InaccessibleMemOnly);
1848 }
1849 void setOnlyAccessesInaccessibleMemory() {
1850 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly);
1851 }
1852
1853 /// Determine if the function may only access memory that is
1854 /// either inaccessible from the IR or pointed to by its arguments.
1855 bool onlyAccessesInaccessibleMemOrArgMem() const {
1856 return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
1857 }
1858 void setOnlyAccessesInaccessibleMemOrArgMem() {
1859 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOrArgMemOnly);
1860 }
1861 /// Determine if the call cannot return.
1862 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1863 void setDoesNotReturn() {
1864 addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
1865 }
1866
1867 /// Determine if the call should not perform indirect branch tracking.
1868 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1869
1870 /// Determine if the call cannot unwind.
1871 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1872 void setDoesNotThrow() {
1873 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1874 }
1875
1876 /// Determine if the invoke cannot be duplicated.
1877 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1878 void setCannotDuplicate() {
1879 addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate);
1880 }
1881
1882 /// Determine if the invoke is convergent
1883 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1884 void setConvergent() {
1885 addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1886 }
1887 void setNotConvergent() {
1888 removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1889 }
1890
1891 /// Determine if the call returns a structure through first
1892 /// pointer argument.
1893 bool hasStructRetAttr() const {
1894 if (getNumArgOperands() == 0)
1895 return false;
1896
1897 // Be friendly and also check the callee.
1898 return paramHasAttr(0, Attribute::StructRet);
1899 }
1900
1901 /// Determine if any call argument is an aggregate passed by value.
1902 bool hasByValArgument() const {
1903 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1904 }
1905 /// Get a pointer to the function that is invoked by this
1906 /// instruction.
1907 const Value *getCalledValue() const { return Op<-InstTy::ArgOffset>(); }
1908 Value *getCalledValue() { return Op<-InstTy::ArgOffset>(); }
1909
1910 /// Set the function called.
1911 void setCalledFunction(Value* Fn) {
1912 setCalledFunction(
1913 cast(cast(Fn->getType())->getElementType()),
1914 Fn);
1915 }
1916 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1917 this->FTy = FTy;
1918 assert(FTy == cast(
1919 cast(Fn->getType())->getElementType()));
1920 Op<-InstTy::ArgOffset>() = Fn;
1921 }
1922
1923 protected:
1924 template bool hasFnAttrImpl(AttrKind Kind) const {
1925 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Kind))
1926 return true;
1927
1928 // Operand bundles override attributes on the called function, but don't
1929 // override attributes directly present on the call instruction.
1930 if (isFnAttrDisallowedByOpBundle(Kind))
1931 return false;
1932
1933 if (const Function *F = getCalledFunction())
1934 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex,
1935 Kind);
1936 return false;
1937 }
1938 };
1939
19401420 //===----------------------------------------------------------------------===//
19411421 /// This class represents a function call, abstracting a target
19421422 /// machine's calling convention. This class uses low bit of the SubClassData
19431423 /// field to indicate whether or not this is a tail call. The rest of the bits
19441424 /// hold the calling convention of the call.
19451425 ///
1946 class CallInst : public CallBase {
1947 friend class OperandBundleUser;
1948
1426 class CallInst : public CallBase {
19491427 CallInst(const CallInst &CI);
19501428
19511429 /// Construct a CallInst given a range of arguments.
19851463 ArrayRef Bundles, const Twine &NameStr);
19861464 void init(Value *Func, const Twine &NameStr);
19871465
1466 /// Compute the number of operands to allocate.
1467 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
1468 // We need one operand for the called function, plus the input operand
1469 // counts provided.
1470 return 1 + NumArgs + NumBundleInputs;
1471 }
1472
19881473 protected:
19891474 // Note: Instruction needs to be a friend here to call cloneImpl.
19901475 friend class Instruction;
19921477 CallInst *cloneImpl() const;
19931478
19941479 public:
1995 static constexpr int ArgOffset = 1;
1996
19971480 static CallInst *Create(Value *Func, ArrayRef Args,
19981481 ArrayRef Bundles = None,
19991482 const Twine &NameStr = "",
20141497 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef Args,
20151498 const Twine &NameStr,
20161499 Instruction *InsertBefore = nullptr) {
2017 return new (unsigned(Args.size() + 1))
1500 return new (ComputeNumOperands(Args.size()))
20181501 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
20191502 }
20201503
20221505 ArrayRef Bundles = None,
20231506 const Twine &NameStr = "",
20241507 Instruction *InsertBefore = nullptr) {
2025 const unsigned TotalOps =
2026 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1508 const int NumOperands =
1509 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
20271510 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
20281511
2029 return new (TotalOps, DescriptorBytes)
1512 return new (NumOperands, DescriptorBytes)
20301513 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
20311514 }
20321515
20331516 static CallInst *Create(Value *Func, ArrayRef Args,
20341517 ArrayRef Bundles,
20351518 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2036 const unsigned TotalOps =
2037 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1519 const int NumOperands =
1520 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
20381521 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
20391522
2040 return new (TotalOps, DescriptorBytes)
1523 return new (NumOperands, DescriptorBytes)
20411524 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
20421525 }
20431526
20441527 static CallInst *Create(Value *Func, ArrayRef Args,
20451528 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2046 return new (unsigned(Args.size() + 1))
1529 return new (ComputeNumOperands(Args.size()))
20471530 CallInst(Func, Args, None, NameStr, InsertAtEnd);
20481531 }
20491532
20501533 static CallInst *Create(Value *F, const Twine &NameStr = "",
20511534 Instruction *InsertBefore = nullptr) {
2052 return new (1) CallInst(F, NameStr, InsertBefore);
1535 return new (ComputeNumOperands(0)) CallInst(F, NameStr, InsertBefore);
20531536 }
20541537
20551538 static CallInst *Create(Value *F, const Twine &NameStr,
20561539 BasicBlock *InsertAtEnd) {
2057 return new (1) CallInst(F, NameStr, InsertAtEnd);
1540 return new (ComputeNumOperands(0)) CallInst(F, NameStr, InsertAtEnd);
20581541 }
20591542
20601543 /// Create a clone of \p CI with a different set of operand bundles and
21451628 }
21461629
21471630 /// Check if this call is an inline asm statement.
2148 bool isInlineAsm() const { return isa(Op<-1>()); }
1631 bool isInlineAsm() const { return isa(getCalledOperand()); }
21491632
21501633 // Methods for support type inquiry through isa, cast, and dyn_cast:
21511634 static bool classof(const Instruction *I) {
21631646 }
21641647 };
21651648
2166 template <>
2167 struct OperandTraits>
2168 : public VariadicOperandTraits, 1> {};
2169
21701649 CallInst::CallInst(Value *Func, ArrayRef Args,
21711650 ArrayRef Bundles, const Twine &NameStr,
21721651 BasicBlock *InsertAtEnd)
2173 : CallBase(
2174 cast(
2175 cast(Func->getType())->getElementType())
2176 ->getReturnType(),
2177 Instruction::Call,
2178 OperandTraits>::op_end(this) -
2179 (Args.size() + CountBundleInputs(Bundles) + 1),
2180 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
1652 : CallBase(cast(
1653 cast(Func->getType())->getElementType())
1654 ->getReturnType(),
1655 Instruction::Call,
1656 OperandTraits::op_end(this) -
1657 (Args.size() + CountBundleInputs(Bundles) + 1),
1658 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1659 InsertAtEnd) {
21811660 init(Func, Args, Bundles, NameStr);
21821661 }
21831662
21841663 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef Args,
21851664 ArrayRef Bundles, const Twine &NameStr,
21861665 Instruction *InsertBefore)
2187 : CallBase(Ty->getReturnType(), Instruction::Call,
2188 OperandTraits>::op_end(this) -
2189 (Args.size() + CountBundleInputs(Bundles) + 1),
2190 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
2191 InsertBefore) {
1666 : CallBase(Ty->getReturnType(), Instruction::Call,
1667 OperandTraits::op_end(this) -
1668 (Args.size() + CountBundleInputs(Bundles) + 1),
1669 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1670 InsertBefore) {
21921671 init(Ty, Func, Args, Bundles, NameStr);
21931672 }
21941673
40653544 /// Invoke instruction. The SubclassData field is used to hold the
40663545 /// calling convention of the call.
40673546 ///
4068 class InvokeInst : public CallBase {
4069 friend class OperandBundleUser;
3547 class InvokeInst : public CallBase {
3548 /// The number of operands for this call beyond the called function,
3549 /// arguments, and operand bundles.
3550 static constexpr int NumExtraOperands = 2;
3551
3552 /// The index from the end of the operand array to the normal destination.
3553 static constexpr int NormalDestOpEndIdx = -3;
3554
3555 /// The index from the end of the operand array to the unwind destination.
3556 static constexpr int UnwindDestOpEndIdx = -2;
40703557
40713558 InvokeInst(const InvokeInst &BI);
40723559
40753562 /// Construct an InvokeInst from a range of arguments
40763563 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
40773564 ArrayRef Args, ArrayRef Bundles,
4078 unsigned Values, const Twine &NameStr,
3565 int NumOperands, const Twine &NameStr,
40793566 Instruction *InsertBefore)
40803567 : InvokeInst(cast(
40813568 cast(Func->getType())->getElementType()),
4082 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
4083 InsertBefore) {}
3569 Func, IfNormal, IfException, Args, Bundles, NumOperands,
3570 NameStr, InsertBefore) {}
40843571
40853572 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
40863573 BasicBlock *IfException, ArrayRef Args,
4087 ArrayRef Bundles, unsigned Values,
3574 ArrayRef Bundles, int NumOperands,
40883575 const Twine &NameStr, Instruction *InsertBefore);
40893576 /// Construct an InvokeInst given a range of arguments.
40903577 ///
40913578 /// Construct an InvokeInst from a range of arguments
40923579 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
40933580 ArrayRef Args, ArrayRef Bundles,
4094 unsigned Values, const Twine &NameStr,
3581 int NumOperands, const Twine &NameStr,
40953582 BasicBlock *InsertAtEnd);
4096
40973583
40983584 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
40993585 ArrayRef Args, ArrayRef Bundles,
41073593 BasicBlock *IfException, ArrayRef Args,
41083594 ArrayRef Bundles, const Twine &NameStr);
41093595
3596 /// Compute the number of operands to allocate.
3597 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
3598 // We need one operand for the called function, plus our extra operands and
3599 // the input operand counts provided.
3600 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3601 }
3602
41103603 protected:
41113604 // Note: Instruction needs to be a friend here to call cloneImpl.
41123605 friend class Instruction;
41143607 InvokeInst *cloneImpl() const;
41153608
41163609 public:
4117 static constexpr int ArgOffset = 3;
41183610 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
41193611 BasicBlock *IfException, ArrayRef Args,
41203612 const Twine &NameStr,
41403632 BasicBlock *IfException, ArrayRef Args,
41413633 const Twine &NameStr,
41423634 Instruction *InsertBefore = nullptr) {
4143 unsigned Values = unsigned(Args.size()) + 3;
4144 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
4145 Values, NameStr, InsertBefore);
3635 int NumOperands = ComputeNumOperands(Args.size());
3636 return new (NumOperands)
3637 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3638 NameStr, InsertBefore);
41463639 }
41473640
41483641 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
41503643 ArrayRef Bundles = None,
41513644 const Twine &NameStr = "",
41523645 Instruction *InsertBefore = nullptr) {
4153 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3646 int NumOperands =
3647 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
41543648 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
41553649
4156 return new (Values, DescriptorBytes)
4157 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3650 return new (NumOperands, DescriptorBytes)
3651 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
41583652 NameStr, InsertBefore);
41593653 }
41603654
41623656 BasicBlock *IfNormal, BasicBlock *IfException,
41633657 ArrayRef Args, const Twine &NameStr,
41643658 BasicBlock *InsertAtEnd) {
4165 unsigned Values = unsigned(Args.size()) + 3;
4166 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
4167 Values, NameStr, InsertAtEnd);
3659 int NumOperands = ComputeNumOperands(Args.size());
3660 return new (NumOperands) InvokeInst(Func, IfNormal, IfException, Args, None,
3661 NumOperands, NameStr, InsertAtEnd);
41683662 }
41693663
41703664 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
41713665 BasicBlock *IfException, ArrayRef Args,
41723666 ArrayRef Bundles,
41733667 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4174 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3668 int NumOperands =
3669 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
41753670 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
41763671
4177 return new (Values, DescriptorBytes)
4178 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
4179 InsertAtEnd);
3672 return new (NumOperands, DescriptorBytes)
3673 InvokeInst(Func, IfNormal, IfException, Args, Bundles, NumOperands,
3674 NameStr, InsertAtEnd);
41803675 }
41813676
41823677 /// Create a clone of \p II with a different set of operand bundles and
41973692 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
41983693 }
41993694
4200 /// Return the function called, or null if this is an
4201 /// indirect function invocation.
4202 ///
4203 Function *getCalledFunction() const {
4204 return dyn_cast(Op<-3>());
4205 }
4206
4207 /// Get a pointer to the function that is invoked by this
4208 /// instruction
4209 const Value *getCalledValue() const { return Op<-3>(); }
4210 Value *getCalledValue() { return Op<-3>(); }
4211
4212 /// Set the function called.
4213 void setCalledFunction(Value* Fn) {
4214 setCalledFunction(
4215 cast(cast(Fn->getType())->getElementType()),
4216 Fn);
4217 }
4218 void setCalledFunction(FunctionType *FTy, Value *Fn) {
4219 this->FTy = FTy;
4220 assert(FTy == cast(
4221 cast(Fn->getType())->getElementType()));
4222 Op<-3>() = Fn;
4223 }
4224
42253695 // get*Dest - Return the destination basic blocks...
42263696 BasicBlock *getNormalDest() const {
4227 return cast(Op<-2>());
3697 return cast(Op<NormalDestOpEndIdx>());
42283698 }
42293699 BasicBlock *getUnwindDest() const {
4230 return cast(Op<-1>());
3700 return cast(Op<UnwindDestOpEndIdx>());
42313701 }
42323702 void setNormalDest(BasicBlock *B) {
4233 Op<-2>() = reinterpret_cast*>(B);
3703 Op<NormalDestOpEndIdx>() = reinterpret_cast*>(B);
42343704 }
42353705 void setUnwindDest(BasicBlock *B) {
4236 Op<-1>() = reinterpret_cast*>(B);
3706 Op<UnwindDestOpEndIdx>() = reinterpret_cast*>(B);
42373707 }
42383708
42393709 /// Get the landingpad instruction from the landing pad
42453715 return i == 0 ? getNormalDest() : getUnwindDest();
42463716 }
42473717
4248 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4249 assert(idx < 2 && "Successor # out of range for invoke!");
4250 *(&Op<-2>() + idx) = reinterpret_cast(NewSucc);
3718 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3719 assert(i < 2 && "Successor # out of range for invoke!");
3720 if (i == 0)
3721 setNormalDest(NewSucc);
3722 else
3723 setUnwindDest(NewSucc);
42513724 }
42523725
42533726 unsigned getNumSuccessors() const { return 2; }
42693742 }
42703743 };
42713744
4272 template <>
4273 struct OperandTraits>
4274 : public VariadicOperandTraits, 3> {};
4275
42763745 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
42773746 BasicBlock *IfException, ArrayRef Args,
4278 ArrayRef Bundles, unsigned Values,
3747 ArrayRef Bundles, int NumOperands,
42793748 const Twine &NameStr, Instruction *InsertBefore)
4280 : CallBase(Ty->getReturnType(), Instruction::Invoke,
4281 OperandTraits>::op_end(this) -
4282 Values,
4283 Values, InsertBefore) {
3749 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3750 OperandTraits::op_end(this) - NumOperands, NumOperands,
3751 InsertBefore) {
42843752 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
42853753 }
42863754
42873755 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
42883756 BasicBlock *IfException, ArrayRef Args,
4289 ArrayRef Bundles, unsigned Values,
3757 ArrayRef Bundles, int NumOperands,
42903758 const Twine &NameStr, BasicBlock *InsertAtEnd)
4291 : CallBase(
4292 cast(
4293 cast(Func->getType())->getElementType())
4294 ->getReturnType(),
4295 Instruction::Invoke,
4296 OperandTraits>::op_end(this) - Values, Values,
4297 InsertAtEnd) {
3759 : CallBase(cast(
3760 cast(Func->getType())->getElementType())
3761 ->getReturnType(),
3762 Instruction::Invoke,
3763 OperandTraits::op_end(this) - NumOperands, NumOperands,
3764 InsertAtEnd) {
42983765 init(Func, IfNormal, IfException, Args, Bundles, NameStr);
42993766 }
4300
43013767
43023768 //===----------------------------------------------------------------------===//
43033769 // ResumeInst Class
6464 //===----------------------------------------------------------------------===//
6565
6666 User::op_iterator CallSite::getCallee() const {
67 Instruction *II(getInstruction());
68 return isCall()
69 ? cast(II)->op_end() - 1 // Skip Callee
70 : cast(II)->op_end() - 3; // Skip BB, BB, Callee
67 return cast(getInstruction())->op_end() - 1;
7168 }
7269
7370 //===----------------------------------------------------------------------===//
253250 }
254251
255252 //===----------------------------------------------------------------------===//
253 // CallBase Implementation
254 //===----------------------------------------------------------------------===//
255
256 Value *CallBase::getReturnedArgOperand() const {
257 unsigned Index;
258
259 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
260 return getArgOperand(Index - AttributeList::FirstArgIndex);
261 if (const Function *F = getCalledFunction())
262 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
263 Index)
264 return getArgOperand(Index - AttributeList::FirstArgIndex);
265
266 return nullptr;
267 }
268
269 bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
270 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
271 return true;
272
273 // Look at the callee, if available.
274 if (const Function *F = getCalledFunction())
275 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
276 return false;
277 }
278
279 /// Determine whether the argument or parameter has the given attribute.
280 bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
281 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
282
283 if (Attrs.hasParamAttribute(ArgNo, Kind))
284 return true;
285 if (const Function *F = getCalledFunction())
286 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
287 return false;
288 }
289
290 bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const {
291 if (const Function *F = getCalledFunction())
292 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
293 return false;
294 }
295
296 bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const {
297 if (const Function *F = getCalledFunction())
298 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind);
299 return false;
300 }
301
302 CallBase::op_iterator
303 CallBase::populateBundleOperandInfos(ArrayRef Bundles,
304 const unsigned BeginIndex) {
305 auto It = op_begin() + BeginIndex;
306 for (auto &B : Bundles)
307 It = std::copy(B.input_begin(), B.input_end(), It);
308
309 auto *ContextImpl = getContext().pImpl;
310 auto BI = Bundles.begin();
311 unsigned CurrentIndex = BeginIndex;
312
313 for (auto &BOI : bundle_op_infos()) {
314 assert(BI != Bundles.end() && "Incorrect allocation?");
315
316 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
317 BOI.Begin = CurrentIndex;
318 BOI.End = CurrentIndex + BI->input_size();
319 CurrentIndex = BOI.End;
320 BI++;
321 }
322
323 assert(BI == Bundles.end() && "Incorrect allocation?");
324
325 return It;
326 }
327
328 //===----------------------------------------------------------------------===//
256329 // CallInst Implementation
257330 //===----------------------------------------------------------------------===//
258331
261334 this->FTy = FTy;
262335 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
263336 "NumOperands not set up?");
264 Op<-1>() = Func;
337 setCalledOperand(Func);
265338
266339 #ifndef NDEBUG
267340 assert((Args.size() == FTy->getNumParams() ||
287360 FTy =
288361 cast(cast(Func->getType())->getElementType());
289362 assert(getNumOperands() == 1 && "NumOperands not set up?");
290 Op<-1>() = Func;
363 setCalledOperand(Func);
291364
292365 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
293366
295368 }
296369
297370 CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
298 : CallBase(
299 cast(
300 cast(Func->getType())->getElementType())
301 ->getReturnType(),
302 Instruction::Call,
303 OperandTraits>::op_end(this) - 1, 1,
304 InsertBefore) {
371 : CallBase(cast(
372 cast(Func->getType())->getElementType())
373 ->getReturnType(),
374 Instruction::Call, OperandTraits::op_end(this) - 1, 1,
375 InsertBefore) {
305376 init(Func, Name);
306377 }
307378
308379 CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
309 : CallBase(
310 cast(
311 cast(Func->getType())->getElementType())
312 ->getReturnType(),
313 Instruction::Call,
314 OperandTraits>::op_end(this) - 1, 1, InsertAtEnd) {
380 : CallBase(cast(
381 cast(Func->getType())->getElementType())
382 ->getReturnType(),
383 Instruction::Call, OperandTraits::op_end(this) - 1, 1,
384 InsertAtEnd) {
315385 init(Func, Name);
316386 }
317387
318388 CallInst::CallInst(const CallInst &CI)
319 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
320 OperandTraits>::op_end(this) -
321 CI.getNumOperands(),
322 CI.getNumOperands()) {
389 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
390 OperandTraits::op_end(this) - CI.getNumOperands(),
391 CI.getNumOperands()) {
323392 setTailCallKind(CI.getTailCallKind());
324393 setCallingConv(CI.getCallingConv());
325394
559628 const Twine &NameStr) {
560629 this->FTy = FTy;
561630
562 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
631 assert((int)getNumOperands() ==
632 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&
563633 "NumOperands not set up?");
564 Op<-3>() = Fn;
565 Op<-2>() = IfNormal;
566 Op<-1>() = IfException;
634 setNormalDest(IfNormal);
635 setUnwindDest(IfException);
636 setCalledOperand(Fn);
567637
568638 #ifndef NDEBUG
569639 assert(((Args.size() == FTy->getNumParams()) ||
586656 }
587657
588658 InvokeInst::InvokeInst(const InvokeInst &II)
589 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
590 OperandTraits>::op_end(this) -
591 II.getNumOperands(),
592 II.getNumOperands()) {
659 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
660 OperandTraits::op_end(this) - II.getNumOperands(),
661 II.getNumOperands()) {
593662 setCallingConv(II.getCallingConv());
594663 std::copy(II.op_begin(), II.op_end(), op_begin());
595664 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
39213921 }
39223922 }
39233923
3924 // Get a pointer to the call base of the instruction if it is some form of
3925 // call.
3926 const CallBase *CBI = dyn_cast(&I);
3927
39243928 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
39253929 Assert(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
39263930
39333937 if (Function *F = dyn_cast(I.getOperand(i))) {
39343938 // Check to make sure that the "address of" an intrinsic function is never
39353939 // taken.
3936 Assert(
3937 !F->isIntrinsic() ||
3938 i == (isa(I) ? e - 1 : isa(I) ? e - 3 : 0),
3939 "Cannot take the address of an intrinsic!", &I);
3940 Assert(!F->isIntrinsic() ||
3941 (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)),
3942 "Cannot take the address of an intrinsic!", &I);
39403943 Assert(
39413944 !F->isIntrinsic() || isa(I) ||
39423945 F->getIntrinsicID() == Intrinsic::donothing ||
39623965 } else if (isa(I.getOperand(i))) {
39633966 verifyDominatesUse(I, i);
39643967 } else if (isa(I.getOperand(i))) {
3965 Assert((i + 1 == e && isa(I)) ||
3966 (i + 3 == e && isa(I)),
3968 Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
39673969 "Cannot take the address of an inline asm!", &I);
39683970 } else if (ConstantExpr *CE = dyn_cast(I.getOperand(i))) {
39693971 if (CE->getType()->isPtrOrPtrVectorTy() ||