1 //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CallSite class, which is a handy wrapper for code that
11 // wants to treat Call and Invoke instructions in a generic way. When in non-
12 // mutation context (e.g. an analysis) ImmutableCallSite should be used.
13 // Finally, when some degree of customization is necessary between these two
14 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
16 // NOTE: These classes are supposed to have "value semantics". So they should be
17 // passed by value, not by reference; they should not be "new"ed or "delete"d.
18 // They are efficiently copyable, assignable and constructable, with cost
19 // equivalent to copying a pointer (notice that they have only a single data
20 // member). The internal representation carries a flag which indicates which of
21 // the two variants is enclosed. This allows for cheaper checks when various
22 // accessors of CallSite are employed.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_IR_CALLSITE_H
27 #define LLVM_IR_CALLSITE_H
29 #include "llvm/ADT/PointerIntPair.h"
30 #include "llvm/ADT/iterator_range.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Instructions.h"
40 template <typename FunTy = const Function,
41 typename BBTy = const BasicBlock,
42 typename ValTy = const Value,
43 typename UserTy = const User,
44 typename UseTy = const Use,
45 typename InstrTy = const Instruction,
46 typename CallTy = const CallInst,
47 typename InvokeTy = const InvokeInst,
48 typename IterTy = User::const_op_iterator>
51 PointerIntPair<InstrTy*, 1, bool> I;
53 CallSiteBase() : I(nullptr, false) {}
54 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
55 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
56 explicit CallSiteBase(ValTy *II) { *this = get(II); }
59 /// CallSiteBase::get - This static method is sort of like a constructor. It
60 /// will create an appropriate call site for a Call or Invoke instruction, but
61 /// it can also create a null initialized CallSiteBase object for something
62 /// which is NOT a call site.
64 static CallSiteBase get(ValTy *V) {
65 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
66 if (II->getOpcode() == Instruction::Call)
67 return CallSiteBase(static_cast<CallTy*>(II));
68 else if (II->getOpcode() == Instruction::Invoke)
69 return CallSiteBase(static_cast<InvokeTy*>(II));
71 return CallSiteBase();
75 /// isCall - true if a CallInst is enclosed.
76 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
77 /// it also could signify a NULL Instruction pointer.
78 bool isCall() const { return I.getInt(); }
80 /// isInvoke - true if a InvokeInst is enclosed.
82 bool isInvoke() const { return getInstruction() && !I.getInt(); }
84 InstrTy *getInstruction() const { return I.getPointer(); }
85 InstrTy *operator->() const { return I.getPointer(); }
86 explicit operator bool() const { return I.getPointer(); }
88 /// Get the basic block containing the call site
89 BBTy* getParent() const { return getInstruction()->getParent(); }
91 /// getCalledValue - Return the pointer to function that is being called.
93 ValTy *getCalledValue() const {
94 assert(getInstruction() && "Not a call or invoke instruction!");
98 /// getCalledFunction - Return the function being called if this is a direct
99 /// call, otherwise return null (if it's an indirect call).
101 FunTy *getCalledFunction() const {
102 return dyn_cast<FunTy>(getCalledValue());
105 /// setCalledFunction - Set the callee to the specified value.
107 void setCalledFunction(Value *V) {
108 assert(getInstruction() && "Not a call or invoke instruction!");
112 /// isCallee - Determine whether the passed iterator points to the
113 /// callee operand's Use.
114 bool isCallee(Value::const_user_iterator UI) const {
115 return isCallee(&UI.getUse());
118 /// Determine whether this Use is the callee operand's Use.
119 bool isCallee(const Use *U) const { return getCallee() == U; }
121 ValTy *getArgument(unsigned ArgNo) const {
122 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
123 return *(arg_begin() + ArgNo);
126 void setArgument(unsigned ArgNo, Value* newVal) {
127 assert(getInstruction() && "Not a call or invoke instruction!");
128 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
129 getInstruction()->setOperand(ArgNo, newVal);
132 /// Given a value use iterator, returns the argument that corresponds to it.
133 /// Iterator must actually correspond to an argument.
134 unsigned getArgumentNo(Value::const_user_iterator I) const {
135 return getArgumentNo(&I.getUse());
138 /// Given a use for an argument, get the argument number that corresponds to
140 unsigned getArgumentNo(const Use *U) const {
141 assert(getInstruction() && "Not a call or invoke instruction!");
142 assert(arg_begin() <= U && U < arg_end()
143 && "Argument # out of range!");
144 return U - arg_begin();
147 /// arg_iterator - The type of iterator to use when looping over actual
148 /// arguments at this call site.
149 typedef IterTy arg_iterator;
151 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
152 /// list for a call site.
153 IterTy arg_begin() const {
154 assert(getInstruction() && "Not a call or invoke instruction!");
155 // Skip non-arguments
156 return (*this)->op_begin();
159 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
160 iterator_range<IterTy> args() const {
161 return iterator_range<IterTy>(arg_begin(), arg_end());
163 bool arg_empty() const { return arg_end() == arg_begin(); }
164 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
166 /// Type of iterator to use when looping over data operands at this call site
168 typedef IterTy data_operand_iterator;
170 /// data_operands_begin/data_operands_end - Return iterators iterating over
171 /// the call / invoke argument list and bundle operands. For invokes, this is
172 /// the set of instruction operands except the invoke target and the two
173 /// successor blocks; and for calls this is the set of instruction operands
174 /// except the call target.
176 IterTy data_operands_begin() const {
177 assert(getInstruction() && "Not a call or invoke instruction!");
178 return (*this)->op_begin();
180 IterTy data_operands_end() const {
181 assert(getInstruction() && "Not a call or invoke instruction!");
182 return (*this)->op_end() - (isCall() ? 1 : 3);
184 iterator_range<IterTy> data_ops() const {
185 return iterator_range<IterTy>(data_operands_begin(), data_operands_end());
187 bool data_operands_empty() const {
188 return data_operands_end() == data_operands_begin();
190 unsigned data_operands_size() const {
191 return std::distance(data_operands_begin(), data_operands_end());
194 /// getType - Return the type of the instruction that generated this call site
196 Type *getType() const { return (*this)->getType(); }
198 /// getCaller - Return the caller function for this call site
200 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
202 /// \brief Tests if this call site must be tail call optimized. Only a
203 /// CallInst can be tail call optimized.
204 bool isMustTailCall() const {
205 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
208 /// \brief Tests if this call site is marked as a tail call.
209 bool isTailCall() const {
210 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
213 #define CALLSITE_DELEGATE_GETTER(METHOD) \
214 InstrTy *II = getInstruction(); \
216 ? cast<CallInst>(II)->METHOD \
217 : cast<InvokeInst>(II)->METHOD
219 #define CALLSITE_DELEGATE_SETTER(METHOD) \
220 InstrTy *II = getInstruction(); \
222 cast<CallInst>(II)->METHOD; \
224 cast<InvokeInst>(II)->METHOD
226 unsigned getNumArgOperands() const {
227 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
230 ValTy *getArgOperand(unsigned i) const {
231 CALLSITE_DELEGATE_GETTER(getArgOperand(i));
234 bool isInlineAsm() const {
236 return cast<CallInst>(getInstruction())->isInlineAsm();
240 /// getCallingConv/setCallingConv - get or set the calling convention of the
242 CallingConv::ID getCallingConv() const {
243 CALLSITE_DELEGATE_GETTER(getCallingConv());
245 void setCallingConv(CallingConv::ID CC) {
246 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
249 FunctionType *getFunctionType() const {
250 CALLSITE_DELEGATE_GETTER(getFunctionType());
253 void mutateFunctionType(FunctionType *Ty) const {
254 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
257 /// getAttributes/setAttributes - get or set the parameter attributes of
259 const AttributeSet &getAttributes() const {
260 CALLSITE_DELEGATE_GETTER(getAttributes());
262 void setAttributes(const AttributeSet &PAL) {
263 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
266 /// \brief Return true if this function has the given attribute.
267 bool hasFnAttr(Attribute::AttrKind A) const {
268 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
271 /// \brief Return true if the call or the callee has the given attribute.
272 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
273 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
276 /// \brief Return true if the data operand at index \p i directly or
277 /// indirectly has the attribute \p A.
279 /// Normal call or invoke arguments have per operand attributes, as specified
280 /// in the attribute set attached to this instruction, while operand bundle
281 /// operands may have some attributes implied by the type of its containing
283 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind A) const {
284 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, A));
287 /// @brief Extract the alignment for a call or parameter (0=unknown).
288 uint16_t getParamAlignment(uint16_t i) const {
289 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
292 /// @brief Extract the number of dereferenceable bytes for a call or
293 /// parameter (0=unknown).
294 uint64_t getDereferenceableBytes(uint16_t i) const {
295 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
298 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
299 /// parameter (0=unknown).
300 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
301 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
304 /// @brief Determine if the parameter or return value is marked with NoAlias
306 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
307 bool doesNotAlias(unsigned n) const {
308 CALLSITE_DELEGATE_GETTER(doesNotAlias(n));
311 /// \brief Return true if the call should not be treated as a call to a
313 bool isNoBuiltin() const {
314 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
317 /// @brief Return true if the call should not be inlined.
318 bool isNoInline() const {
319 CALLSITE_DELEGATE_GETTER(isNoInline());
321 void setIsNoInline(bool Value = true) {
322 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
325 /// @brief Determine if the call does not access memory.
326 bool doesNotAccessMemory() const {
327 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
329 void setDoesNotAccessMemory() {
330 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
333 /// @brief Determine if the call does not access or only reads memory.
334 bool onlyReadsMemory() const {
335 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
337 void setOnlyReadsMemory() {
338 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
341 /// @brief Determine if the call can access memmory only using pointers based
342 /// on its arguments.
343 bool onlyAccessesArgMemory() const {
344 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
346 void setOnlyAccessesArgMemory() {
347 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
350 /// @brief Determine if the call cannot return.
351 bool doesNotReturn() const {
352 CALLSITE_DELEGATE_GETTER(doesNotReturn());
354 void setDoesNotReturn() {
355 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
358 /// @brief Determine if the call cannot unwind.
359 bool doesNotThrow() const {
360 CALLSITE_DELEGATE_GETTER(doesNotThrow());
362 void setDoesNotThrow() {
363 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
366 int getNumOperandBundles() const {
367 CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
370 bool hasOperandBundles() const {
371 CALLSITE_DELEGATE_GETTER(hasOperandBundles());
374 int getNumTotalBundleOperands() const {
375 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
378 OperandBundleUse getOperandBundleAt(unsigned Index) const {
379 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index));
382 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
383 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name));
386 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
387 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID));
390 #undef CALLSITE_DELEGATE_GETTER
391 #undef CALLSITE_DELEGATE_SETTER
393 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
394 const Instruction *II = getInstruction();
395 // Since this is actually a getter that "looks like" a setter, don't use the
396 // above macros to avoid confusion.
398 cast<CallInst>(II)->getOperandBundlesAsDefs(Defs);
400 cast<InvokeInst>(II)->getOperandBundlesAsDefs(Defs);
403 /// @brief Determine whether this data operand is not captured.
404 bool doesNotCapture(unsigned OpNo) const {
405 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
408 /// @brief Determine whether this argument is passed by value.
409 bool isByValArgument(unsigned ArgNo) const {
410 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
413 /// @brief Determine whether this argument is passed in an alloca.
414 bool isInAllocaArgument(unsigned ArgNo) const {
415 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
418 /// @brief Determine whether this argument is passed by value or in an alloca.
419 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
420 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
421 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
424 /// @brief Determine if there are is an inalloca argument. Only the last
425 /// argument can have the inalloca attribute.
426 bool hasInAllocaArgument() const {
427 return paramHasAttr(arg_size(), Attribute::InAlloca);
430 bool doesNotAccessMemory(unsigned OpNo) const {
431 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
434 bool onlyReadsMemory(unsigned OpNo) const {
435 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
436 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
439 /// @brief Return true if the return value is known to be not null.
440 /// This may be because it has the nonnull attribute, or because at least
441 /// one byte is dereferenceable and the pointer is in addrspace(0).
442 bool isReturnNonNull() const {
443 if (paramHasAttr(0, Attribute::NonNull))
445 else if (getDereferenceableBytes(0) > 0 &&
446 getType()->getPointerAddressSpace() == 0)
452 /// hasArgument - Returns true if this CallSite passes the given Value* as an
453 /// argument to the called function.
454 bool hasArgument(const Value *Arg) const {
455 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
457 if (AI->get() == Arg)
463 unsigned getArgumentEndOffset() const {
465 // Skip [ operand bundles ], Callee
466 auto *CI = cast<CallInst>(getInstruction());
467 return 1 + CI->getNumTotalBundleOperands();
469 // Skip [ operand bundles ], BB, BB, Callee
470 auto *II = cast<InvokeInst>(getInstruction());
471 return 3 + II->getNumTotalBundleOperands();
475 IterTy getCallee() const {
476 if (isCall()) // Skip Callee
477 return cast<CallInst>(getInstruction())->op_end() - 1;
478 else // Skip BB, BB, Callee
479 return cast<InvokeInst>(getInstruction())->op_end() - 3;
483 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use,
484 Instruction, CallInst, InvokeInst,
488 CallSite(CallSiteBase B) : CallSiteBase(B) {}
489 CallSite(CallInst *CI) : CallSiteBase(CI) {}
490 CallSite(InvokeInst *II) : CallSiteBase(II) {}
491 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
492 explicit CallSite(Value *V) : CallSiteBase(V) {}
494 bool operator==(const CallSite &CS) const { return I == CS.I; }
495 bool operator!=(const CallSite &CS) const { return I != CS.I; }
496 bool operator<(const CallSite &CS) const {
497 return getInstruction() < CS.getInstruction();
501 User::op_iterator getCallee() const;
504 /// ImmutableCallSite - establish a view to a call site for examination
505 class ImmutableCallSite : public CallSiteBase<> {
507 ImmutableCallSite() {}
508 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
509 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
510 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
511 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
512 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
515 } // End llvm namespace