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/iterator_range.h"
30 #include "llvm/ADT/Optional.h"
31 #include "llvm/ADT/PointerIntPair.h"
32 #include "llvm/IR/Attributes.h"
33 #include "llvm/IR/CallingConv.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/InstrTypes.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/IR/Use.h"
41 #include "llvm/IR/User.h"
42 #include "llvm/IR/Value.h"
49 template <typename FunTy = const Function,
50 typename BBTy = const BasicBlock,
51 typename ValTy = const Value,
52 typename UserTy = const User,
53 typename UseTy = const Use,
54 typename InstrTy = const Instruction,
55 typename CallTy = const CallInst,
56 typename InvokeTy = const InvokeInst,
57 typename IterTy = User::const_op_iterator>
60 PointerIntPair<InstrTy*, 1, bool> I;
62 CallSiteBase() : I(nullptr, false) {}
63 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
64 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
65 explicit CallSiteBase(ValTy *II) { *this = get(II); }
68 /// CallSiteBase::get - This static method is sort of like a constructor. It
69 /// will create an appropriate call site for a Call or Invoke instruction, but
70 /// it can also create a null initialized CallSiteBase object for something
71 /// which is NOT a call site.
73 static CallSiteBase get(ValTy *V) {
74 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
75 if (II->getOpcode() == Instruction::Call)
76 return CallSiteBase(static_cast<CallTy*>(II));
77 else if (II->getOpcode() == Instruction::Invoke)
78 return CallSiteBase(static_cast<InvokeTy*>(II));
80 return CallSiteBase();
84 /// isCall - true if a CallInst is enclosed.
85 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
86 /// it also could signify a NULL Instruction pointer.
87 bool isCall() const { return I.getInt(); }
89 /// isInvoke - true if a InvokeInst is enclosed.
91 bool isInvoke() const { return getInstruction() && !I.getInt(); }
93 InstrTy *getInstruction() const { return I.getPointer(); }
94 InstrTy *operator->() const { return I.getPointer(); }
95 explicit operator bool() const { return I.getPointer(); }
97 /// Get the basic block containing the call site
98 BBTy* getParent() const { return getInstruction()->getParent(); }
100 /// getCalledValue - Return the pointer to function that is being called.
102 ValTy *getCalledValue() const {
103 assert(getInstruction() && "Not a call or invoke instruction!");
107 /// getCalledFunction - Return the function being called if this is a direct
108 /// call, otherwise return null (if it's an indirect call).
110 FunTy *getCalledFunction() const {
111 return dyn_cast<FunTy>(getCalledValue());
114 /// setCalledFunction - Set the callee to the specified value.
116 void setCalledFunction(Value *V) {
117 assert(getInstruction() && "Not a call or invoke instruction!");
121 /// Return the intrinsic ID of the intrinsic called by this CallSite,
122 /// or Intrinsic::not_intrinsic if the called function is not an
123 /// intrinsic, or if this CallSite is an indirect call.
124 Intrinsic::ID getIntrinsicID() const {
125 if (auto *F = getCalledFunction())
126 return F->getIntrinsicID();
127 // Don't use Intrinsic::not_intrinsic, as it will require pulling
128 // Intrinsics.h into every header that uses CallSite.
129 return static_cast<Intrinsic::ID>(0);
132 /// isCallee - Determine whether the passed iterator points to the
133 /// callee operand's Use.
134 bool isCallee(Value::const_user_iterator UI) const {
135 return isCallee(&UI.getUse());
138 /// Determine whether this Use is the callee operand's Use.
139 bool isCallee(const Use *U) const { return getCallee() == U; }
141 /// \brief Determine whether the passed iterator points to an argument
143 bool isArgOperand(Value::const_user_iterator UI) const {
144 return isArgOperand(&UI.getUse());
147 /// \brief Determine whether the passed use points to an argument operand.
148 bool isArgOperand(const Use *U) const {
149 assert(getInstruction() == U->getUser());
150 return arg_begin() <= U && U < arg_end();
153 /// \brief Determine whether the passed iterator points to a bundle operand.
154 bool isBundleOperand(Value::const_user_iterator UI) const {
155 return isBundleOperand(&UI.getUse());
158 /// \brief Determine whether the passed use points to a bundle operand.
159 bool isBundleOperand(const Use *U) const {
160 assert(getInstruction() == U->getUser());
161 if (!hasOperandBundles())
163 unsigned OperandNo = U - (*this)->op_begin();
164 return getBundleOperandsStartIndex() <= OperandNo &&
165 OperandNo < getBundleOperandsEndIndex();
168 /// \brief Determine whether the passed iterator points to a data operand.
169 bool isDataOperand(Value::const_user_iterator UI) const {
170 return isDataOperand(&UI.getUse());
173 /// \brief Determine whether the passed use points to a data operand.
174 bool isDataOperand(const Use *U) const {
175 return data_operands_begin() <= U && U < data_operands_end();
178 ValTy *getArgument(unsigned ArgNo) const {
179 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
180 return *(arg_begin() + ArgNo);
183 void setArgument(unsigned ArgNo, Value* newVal) {
184 assert(getInstruction() && "Not a call or invoke instruction!");
185 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
186 getInstruction()->setOperand(ArgNo, newVal);
189 /// Given a value use iterator, returns the argument that corresponds to it.
190 /// Iterator must actually correspond to an argument.
191 unsigned getArgumentNo(Value::const_user_iterator I) const {
192 return getArgumentNo(&I.getUse());
195 /// Given a use for an argument, get the argument number that corresponds to
197 unsigned getArgumentNo(const Use *U) const {
198 assert(getInstruction() && "Not a call or invoke instruction!");
199 assert(isArgOperand(U) && "Argument # out of range!");
200 return U - arg_begin();
203 /// arg_iterator - The type of iterator to use when looping over actual
204 /// arguments at this call site.
205 typedef IterTy arg_iterator;
207 iterator_range<IterTy> args() const {
208 return make_range(arg_begin(), arg_end());
210 bool arg_empty() const { return arg_end() == arg_begin(); }
211 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
213 /// Given a value use iterator, returns the data operand that corresponds to
215 /// Iterator must actually correspond to a data operand.
216 unsigned getDataOperandNo(Value::const_user_iterator UI) const {
217 return getDataOperandNo(&UI.getUse());
220 /// Given a use for a data operand, get the data operand number that
221 /// corresponds to it.
222 unsigned getDataOperandNo(const Use *U) const {
223 assert(getInstruction() && "Not a call or invoke instruction!");
224 assert(isDataOperand(U) && "Data operand # out of range!");
225 return U - data_operands_begin();
228 /// Type of iterator to use when looping over data operands at this call site
230 typedef IterTy data_operand_iterator;
232 /// data_operands_begin/data_operands_end - Return iterators iterating over
233 /// the call / invoke argument list and bundle operands. For invokes, this is
234 /// the set of instruction operands except the invoke target and the two
235 /// successor blocks; and for calls this is the set of instruction operands
236 /// except the call target.
238 IterTy data_operands_begin() const {
239 assert(getInstruction() && "Not a call or invoke instruction!");
240 return (*this)->op_begin();
242 IterTy data_operands_end() const {
243 assert(getInstruction() && "Not a call or invoke instruction!");
244 return (*this)->op_end() - (isCall() ? 1 : 3);
246 iterator_range<IterTy> data_ops() const {
247 return make_range(data_operands_begin(), data_operands_end());
249 bool data_operands_empty() const {
250 return data_operands_end() == data_operands_begin();
252 unsigned data_operands_size() const {
253 return std::distance(data_operands_begin(), data_operands_end());
256 /// getType - Return the type of the instruction that generated this call site
258 Type *getType() const { return (*this)->getType(); }
260 /// getCaller - Return the caller function for this call site
262 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
264 /// \brief Tests if this call site must be tail call optimized. Only a
265 /// CallInst can be tail call optimized.
266 bool isMustTailCall() const {
267 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
270 /// \brief Tests if this call site is marked as a tail call.
271 bool isTailCall() const {
272 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
275 #define CALLSITE_DELEGATE_GETTER(METHOD) \
276 InstrTy *II = getInstruction(); \
278 ? cast<CallInst>(II)->METHOD \
279 : cast<InvokeInst>(II)->METHOD
281 #define CALLSITE_DELEGATE_SETTER(METHOD) \
282 InstrTy *II = getInstruction(); \
284 cast<CallInst>(II)->METHOD; \
286 cast<InvokeInst>(II)->METHOD
288 unsigned getNumArgOperands() const {
289 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
292 ValTy *getArgOperand(unsigned i) const {
293 CALLSITE_DELEGATE_GETTER(getArgOperand(i));
296 ValTy *getReturnedArgOperand() const {
297 CALLSITE_DELEGATE_GETTER(getReturnedArgOperand());
300 bool isInlineAsm() const {
302 return cast<CallInst>(getInstruction())->isInlineAsm();
306 /// getCallingConv/setCallingConv - get or set the calling convention of the
308 CallingConv::ID getCallingConv() const {
309 CALLSITE_DELEGATE_GETTER(getCallingConv());
311 void setCallingConv(CallingConv::ID CC) {
312 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
315 FunctionType *getFunctionType() const {
316 CALLSITE_DELEGATE_GETTER(getFunctionType());
319 void mutateFunctionType(FunctionType *Ty) const {
320 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
323 /// getAttributes/setAttributes - get or set the parameter attributes of
325 AttributeSet getAttributes() const {
326 CALLSITE_DELEGATE_GETTER(getAttributes());
328 void setAttributes(AttributeSet PAL) {
329 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
332 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
333 CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind));
336 void addAttribute(unsigned i, Attribute Attr) {
337 CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr));
340 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
341 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
344 void removeAttribute(unsigned i, StringRef Kind) {
345 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
348 /// \brief Return true if this function has the given attribute.
349 bool hasFnAttr(Attribute::AttrKind Kind) const {
350 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
353 /// \brief Return true if this function has the given attribute.
354 bool hasFnAttr(StringRef Kind) const {
355 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
358 /// \brief Return true if the call or the callee has the given attribute.
359 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
360 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, Kind));
363 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
364 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
367 Attribute getAttribute(unsigned i, StringRef Kind) const {
368 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
371 /// \brief Return true if the data operand at index \p i directly or
372 /// indirectly has the attribute \p A.
374 /// Normal call or invoke arguments have per operand attributes, as specified
375 /// in the attribute set attached to this instruction, while operand bundle
376 /// operands may have some attributes implied by the type of its containing
378 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
379 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind));
382 /// @brief Extract the alignment for a call or parameter (0=unknown).
383 uint16_t getParamAlignment(uint16_t i) const {
384 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
387 /// @brief Extract the number of dereferenceable bytes for a call or
388 /// parameter (0=unknown).
389 uint64_t getDereferenceableBytes(uint16_t i) const {
390 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
393 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
394 /// parameter (0=unknown).
395 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
396 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
399 /// @brief Determine if the parameter or return value is marked with NoAlias
401 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
402 bool doesNotAlias(unsigned n) const {
403 CALLSITE_DELEGATE_GETTER(doesNotAlias(n));
406 /// \brief Return true if the call should not be treated as a call to a
408 bool isNoBuiltin() const {
409 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
412 /// @brief Return true if the call should not be inlined.
413 bool isNoInline() const {
414 CALLSITE_DELEGATE_GETTER(isNoInline());
416 void setIsNoInline(bool Value = true) {
417 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
420 /// @brief Determine if the call does not access memory.
421 bool doesNotAccessMemory() const {
422 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
424 void setDoesNotAccessMemory() {
425 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
428 /// @brief Determine if the call does not access or only reads memory.
429 bool onlyReadsMemory() const {
430 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
432 void setOnlyReadsMemory() {
433 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
436 /// @brief Determine if the call does not access or only writes memory.
437 bool doesNotReadMemory() const {
438 CALLSITE_DELEGATE_GETTER(doesNotReadMemory());
440 void setDoesNotReadMemory() {
441 CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory());
444 /// @brief Determine if the call can access memmory only using pointers based
445 /// on its arguments.
446 bool onlyAccessesArgMemory() const {
447 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
449 void setOnlyAccessesArgMemory() {
450 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
453 /// @brief Determine if the call cannot return.
454 bool doesNotReturn() const {
455 CALLSITE_DELEGATE_GETTER(doesNotReturn());
457 void setDoesNotReturn() {
458 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
461 /// @brief Determine if the call cannot unwind.
462 bool doesNotThrow() const {
463 CALLSITE_DELEGATE_GETTER(doesNotThrow());
465 void setDoesNotThrow() {
466 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
469 /// @brief Determine if the call can be duplicated.
470 bool cannotDuplicate() const {
471 CALLSITE_DELEGATE_GETTER(cannotDuplicate());
473 void setCannotDuplicate() {
474 CALLSITE_DELEGATE_GETTER(setCannotDuplicate());
477 /// @brief Determine if the call is convergent.
478 bool isConvergent() const {
479 CALLSITE_DELEGATE_GETTER(isConvergent());
481 void setConvergent() {
482 CALLSITE_DELEGATE_SETTER(setConvergent());
484 void setNotConvergent() {
485 CALLSITE_DELEGATE_SETTER(setNotConvergent());
488 unsigned getNumOperandBundles() const {
489 CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
492 bool hasOperandBundles() const {
493 CALLSITE_DELEGATE_GETTER(hasOperandBundles());
496 unsigned getBundleOperandsStartIndex() const {
497 CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex());
500 unsigned getBundleOperandsEndIndex() const {
501 CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex());
504 unsigned getNumTotalBundleOperands() const {
505 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
508 OperandBundleUse getOperandBundleAt(unsigned Index) const {
509 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index));
512 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
513 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name));
516 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
517 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID));
520 unsigned countOperandBundlesOfType(uint32_t ID) const {
521 CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID));
524 bool isBundleOperand(unsigned Idx) const {
525 CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx));
528 IterTy arg_begin() const {
529 CALLSITE_DELEGATE_GETTER(arg_begin());
532 IterTy arg_end() const {
533 CALLSITE_DELEGATE_GETTER(arg_end());
536 #undef CALLSITE_DELEGATE_GETTER
537 #undef CALLSITE_DELEGATE_SETTER
539 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
540 const Instruction *II = getInstruction();
541 // Since this is actually a getter that "looks like" a setter, don't use the
542 // above macros to avoid confusion.
544 cast<CallInst>(II)->getOperandBundlesAsDefs(Defs);
546 cast<InvokeInst>(II)->getOperandBundlesAsDefs(Defs);
549 /// @brief Determine whether this data operand is not captured.
550 bool doesNotCapture(unsigned OpNo) const {
551 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
554 /// @brief Determine whether this argument is passed by value.
555 bool isByValArgument(unsigned ArgNo) const {
556 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
559 /// @brief Determine whether this argument is passed in an alloca.
560 bool isInAllocaArgument(unsigned ArgNo) const {
561 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
564 /// @brief Determine whether this argument is passed by value or in an alloca.
565 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
566 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
567 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
570 /// @brief Determine if there are is an inalloca argument. Only the last
571 /// argument can have the inalloca attribute.
572 bool hasInAllocaArgument() const {
573 return paramHasAttr(arg_size(), Attribute::InAlloca);
576 bool doesNotAccessMemory(unsigned OpNo) const {
577 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
580 bool onlyReadsMemory(unsigned OpNo) const {
581 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
582 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
585 /// @brief Return true if the return value is known to be not null.
586 /// This may be because it has the nonnull attribute, or because at least
587 /// one byte is dereferenceable and the pointer is in addrspace(0).
588 bool isReturnNonNull() const {
589 if (paramHasAttr(0, Attribute::NonNull))
591 else if (getDereferenceableBytes(0) > 0 &&
592 getType()->getPointerAddressSpace() == 0)
598 /// hasArgument - Returns true if this CallSite passes the given Value* as an
599 /// argument to the called function.
600 bool hasArgument(const Value *Arg) const {
601 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
603 if (AI->get() == Arg)
609 IterTy getCallee() const {
610 if (isCall()) // Skip Callee
611 return cast<CallInst>(getInstruction())->op_end() - 1;
612 else // Skip BB, BB, Callee
613 return cast<InvokeInst>(getInstruction())->op_end() - 3;
617 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use,
618 Instruction, CallInst, InvokeInst,
621 CallSite() = default;
622 CallSite(CallSiteBase B) : CallSiteBase(B) {}
623 CallSite(CallInst *CI) : CallSiteBase(CI) {}
624 CallSite(InvokeInst *II) : CallSiteBase(II) {}
625 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
626 explicit CallSite(Value *V) : CallSiteBase(V) {}
628 bool operator==(const CallSite &CS) const { return I == CS.I; }
629 bool operator!=(const CallSite &CS) const { return I != CS.I; }
630 bool operator<(const CallSite &CS) const {
631 return getInstruction() < CS.getInstruction();
635 friend struct DenseMapInfo<CallSite>;
637 User::op_iterator getCallee() const;
640 template <> struct DenseMapInfo<CallSite> {
641 using BaseInfo = DenseMapInfo<decltype(CallSite::I)>;
643 static CallSite getEmptyKey() {
645 CS.I = BaseInfo::getEmptyKey();
649 static CallSite getTombstoneKey() {
651 CS.I = BaseInfo::getTombstoneKey();
655 static unsigned getHashValue(const CallSite &CS) {
656 return BaseInfo::getHashValue(CS.I);
659 static bool isEqual(const CallSite &LHS, const CallSite &RHS) {
664 /// ImmutableCallSite - establish a view to a call site for examination
665 class ImmutableCallSite : public CallSiteBase<> {
667 ImmutableCallSite() = default;
668 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
669 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
670 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
671 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
672 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
675 } // end namespace llvm
677 #endif // LLVM_IR_CALLSITE_H