1 //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the CallSite class, which is a handy wrapper for code that
10 // wants to treat Call, Invoke and CallBr instructions in a generic way. When
11 // in non-mutation context (e.g. an analysis) ImmutableCallSite should be used.
12 // Finally, when some degree of customization is necessary between these two
13 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
15 // NOTE: These classes are supposed to have "value semantics". So they should be
16 // passed by value, not by reference; they should not be "new"ed or "delete"d.
17 // They are efficiently copyable, assignable and constructable, with cost
18 // equivalent to copying a pointer (notice that they have only a single data
19 // member). The internal representation carries a flag which indicates which of
20 // the three variants is enclosed. This allows for cheaper checks when various
21 // accessors of CallSite are employed.
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_IR_CALLSITE_H
26 #define LLVM_IR_CALLSITE_H
28 #include "llvm/ADT/Optional.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/Function.h"
34 #include "llvm/IR/InstrTypes.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/Use.h"
38 #include "llvm/IR/User.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/Support/Casting.h"
51 template <typename FunTy
= const Function
, typename BBTy
= const BasicBlock
,
52 typename ValTy
= const Value
, typename UserTy
= const User
,
53 typename UseTy
= const Use
, typename InstrTy
= const Instruction
,
54 typename CallTy
= const CallInst
,
55 typename InvokeTy
= const InvokeInst
,
56 typename CallBrTy
= const CallBrInst
,
57 typename IterTy
= User::const_op_iterator
>
60 PointerIntPair
<InstrTy
*, 2, int> I
;
62 CallSiteBase() = default;
63 CallSiteBase(CallTy
*CI
) : I(CI
, 1) { assert(CI
); }
64 CallSiteBase(InvokeTy
*II
) : I(II
, 0) { assert(II
); }
65 CallSiteBase(CallBrTy
*CBI
) : I(CBI
, 2) { assert(CBI
); }
66 explicit CallSiteBase(ValTy
*II
) { *this = get(II
); }
69 /// This static method is like a constructor. It will create an appropriate
70 /// call site for a Call, Invoke or CallBr instruction, but it can also create
71 /// a null initialized CallSiteBase object for something which is NOT a call
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 if (II
->getOpcode() == Instruction::Invoke
)
78 return CallSiteBase(static_cast<InvokeTy
*>(II
));
79 if (II
->getOpcode() == Instruction::CallBr
)
80 return CallSiteBase(static_cast<CallBrTy
*>(II
));
82 return CallSiteBase();
86 /// Return true if a CallInst is enclosed.
87 bool isCall() const { return I
.getInt() == 1; }
89 /// Return true if a InvokeInst is enclosed. !I.getInt() may also signify a
90 /// NULL instruction pointer, so check that.
91 bool isInvoke() const { return getInstruction() && I
.getInt() == 0; }
93 /// Return true if a CallBrInst is enclosed.
94 bool isCallBr() const { return I
.getInt() == 2; }
96 InstrTy
*getInstruction() const { return I
.getPointer(); }
97 InstrTy
*operator->() const { return I
.getPointer(); }
98 explicit operator bool() const { return I
.getPointer(); }
100 /// Get the basic block containing the call site.
101 BBTy
* getParent() const { return getInstruction()->getParent(); }
103 /// Return the pointer to function that is being called.
104 ValTy
*getCalledValue() const {
105 assert(getInstruction() && "Not a call, invoke or callbr instruction!");
109 /// Return the function being called if this is a direct call, otherwise
110 /// return null (if it's an indirect call).
111 FunTy
*getCalledFunction() const {
112 return dyn_cast
<FunTy
>(getCalledValue());
115 /// Return true if the callsite is an indirect call.
116 bool isIndirectCall() const {
117 const Value
*V
= getCalledValue();
120 if (isa
<FunTy
>(V
) || isa
<Constant
>(V
))
122 if (const CallBase
*CB
= dyn_cast
<CallBase
>(getInstruction()))
123 if (CB
->isInlineAsm())
128 /// Set the callee to the specified value. Unlike the function of the same
129 /// name on CallBase, does not modify the type!
130 void setCalledFunction(Value
*V
) {
131 assert(getInstruction() && "Not a call, callbr, or invoke instruction!");
132 assert(cast
<PointerType
>(V
->getType())->getElementType() ==
133 cast
<CallBase
>(getInstruction())->getFunctionType() &&
134 "New callee type does not match FunctionType on call");
138 /// Return the intrinsic ID of the intrinsic called by this CallSite,
139 /// or Intrinsic::not_intrinsic if the called function is not an
140 /// intrinsic, or if this CallSite is an indirect call.
141 Intrinsic::ID
getIntrinsicID() const {
142 if (auto *F
= getCalledFunction())
143 return F
->getIntrinsicID();
144 // Don't use Intrinsic::not_intrinsic, as it will require pulling
145 // Intrinsics.h into every header that uses CallSite.
146 return static_cast<Intrinsic::ID
>(0);
149 /// Determine whether the passed iterator points to the callee operand's Use.
150 bool isCallee(Value::const_user_iterator UI
) const {
151 return isCallee(&UI
.getUse());
154 /// Determine whether this Use is the callee operand's Use.
155 bool isCallee(const Use
*U
) const { return getCallee() == U
; }
157 /// Determine whether the passed iterator points to an argument operand.
158 bool isArgOperand(Value::const_user_iterator UI
) const {
159 return isArgOperand(&UI
.getUse());
162 /// Determine whether the passed use points to an argument operand.
163 bool isArgOperand(const Use
*U
) const {
164 assert(getInstruction() == U
->getUser());
165 return arg_begin() <= U
&& U
< arg_end();
168 /// Determine whether the passed iterator points to a bundle operand.
169 bool isBundleOperand(Value::const_user_iterator UI
) const {
170 return isBundleOperand(&UI
.getUse());
173 /// Determine whether the passed use points to a bundle operand.
174 bool isBundleOperand(const Use
*U
) const {
175 assert(getInstruction() == U
->getUser());
176 if (!hasOperandBundles())
178 unsigned OperandNo
= U
- (*this)->op_begin();
179 return getBundleOperandsStartIndex() <= OperandNo
&&
180 OperandNo
< getBundleOperandsEndIndex();
183 /// Determine whether the passed iterator points to a data operand.
184 bool isDataOperand(Value::const_user_iterator UI
) const {
185 return isDataOperand(&UI
.getUse());
188 /// Determine whether the passed use points to a data operand.
189 bool isDataOperand(const Use
*U
) const {
190 return data_operands_begin() <= U
&& U
< data_operands_end();
193 ValTy
*getArgument(unsigned ArgNo
) const {
194 assert(arg_begin() + ArgNo
< arg_end() && "Argument # out of range!");
195 return *(arg_begin() + ArgNo
);
198 void setArgument(unsigned ArgNo
, Value
* newVal
) {
199 assert(getInstruction() && "Not a call, invoke or callbr instruction!");
200 assert(arg_begin() + ArgNo
< arg_end() && "Argument # out of range!");
201 getInstruction()->setOperand(ArgNo
, newVal
);
204 /// Given a value use iterator, returns the argument that corresponds to it.
205 /// Iterator must actually correspond to an argument.
206 unsigned getArgumentNo(Value::const_user_iterator I
) const {
207 return getArgumentNo(&I
.getUse());
210 /// Given a use for an argument, get the argument number that corresponds to
212 unsigned getArgumentNo(const Use
*U
) const {
213 assert(getInstruction() && "Not a call, invoke or callbr instruction!");
214 assert(isArgOperand(U
) && "Argument # out of range!");
215 return U
- arg_begin();
218 /// The type of iterator to use when looping over actual arguments at this
220 using arg_iterator
= IterTy
;
222 iterator_range
<IterTy
> args() const {
223 return make_range(arg_begin(), arg_end());
225 bool arg_empty() const { return arg_end() == arg_begin(); }
226 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
228 /// Given a value use iterator, return the data operand corresponding to it.
229 /// Iterator must actually correspond to a data operand.
230 unsigned getDataOperandNo(Value::const_user_iterator UI
) const {
231 return getDataOperandNo(&UI
.getUse());
234 /// Given a use for a data operand, get the data operand number that
235 /// corresponds to it.
236 unsigned getDataOperandNo(const Use
*U
) const {
237 assert(getInstruction() && "Not a call, invoke or callbr instruction!");
238 assert(isDataOperand(U
) && "Data operand # out of range!");
239 return U
- data_operands_begin();
242 /// Type of iterator to use when looping over data operands at this call site
244 using data_operand_iterator
= IterTy
;
246 /// data_operands_begin/data_operands_end - Return iterators iterating over
247 /// the call / invoke / callbr argument list and bundle operands. For invokes,
248 /// this is the set of instruction operands except the invoke target and the
249 /// two successor blocks; for calls this is the set of instruction operands
250 /// except the call target; for callbrs the number of labels to skip must be
251 /// determined first.
253 IterTy
data_operands_begin() const {
254 assert(getInstruction() && "Not a call or invoke instruction!");
255 return cast
<CallBase
>(getInstruction())->data_operands_begin();
257 IterTy
data_operands_end() const {
258 assert(getInstruction() && "Not a call or invoke instruction!");
259 return cast
<CallBase
>(getInstruction())->data_operands_end();
261 iterator_range
<IterTy
> data_ops() const {
262 return make_range(data_operands_begin(), data_operands_end());
264 bool data_operands_empty() const {
265 return data_operands_end() == data_operands_begin();
267 unsigned data_operands_size() const {
268 return std::distance(data_operands_begin(), data_operands_end());
271 /// Return the type of the instruction that generated this call site.
272 Type
*getType() const { return (*this)->getType(); }
274 /// Return the caller function for this call site.
275 FunTy
*getCaller() const { return (*this)->getParent()->getParent(); }
277 /// Tests if this call site must be tail call optimized. Only a CallInst can
278 /// be tail call optimized.
279 bool isMustTailCall() const {
280 return isCall() && cast
<CallInst
>(getInstruction())->isMustTailCall();
283 /// Tests if this call site is marked as a tail call.
284 bool isTailCall() const {
285 return isCall() && cast
<CallInst
>(getInstruction())->isTailCall();
288 #define CALLSITE_DELEGATE_GETTER(METHOD) \
289 InstrTy *II = getInstruction(); \
290 return isCall() ? cast<CallInst>(II)->METHOD \
291 : isCallBr() ? cast<CallBrInst>(II)->METHOD \
292 : cast<InvokeInst>(II)->METHOD
294 #define CALLSITE_DELEGATE_SETTER(METHOD) \
295 InstrTy *II = getInstruction(); \
297 cast<CallInst>(II)->METHOD; \
298 else if (isCallBr()) \
299 cast<CallBrInst>(II)->METHOD; \
301 cast<InvokeInst>(II)->METHOD
303 unsigned getNumArgOperands() const {
304 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
307 ValTy
*getArgOperand(unsigned i
) const {
308 CALLSITE_DELEGATE_GETTER(getArgOperand(i
));
311 ValTy
*getReturnedArgOperand() const {
312 CALLSITE_DELEGATE_GETTER(getReturnedArgOperand());
315 bool isInlineAsm() const {
316 return cast
<CallBase
>(getInstruction())->isInlineAsm();
319 /// Get the calling convention of the call.
320 CallingConv::ID
getCallingConv() const {
321 CALLSITE_DELEGATE_GETTER(getCallingConv());
323 /// Set the calling convention of the call.
324 void setCallingConv(CallingConv::ID CC
) {
325 CALLSITE_DELEGATE_SETTER(setCallingConv(CC
));
328 FunctionType
*getFunctionType() const {
329 CALLSITE_DELEGATE_GETTER(getFunctionType());
332 void mutateFunctionType(FunctionType
*Ty
) const {
333 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty
));
336 /// Get the parameter attributes of the call.
337 AttributeList
getAttributes() const {
338 CALLSITE_DELEGATE_GETTER(getAttributes());
340 /// Set the parameter attributes of the call.
341 void setAttributes(AttributeList PAL
) {
342 CALLSITE_DELEGATE_SETTER(setAttributes(PAL
));
345 void addAttribute(unsigned i
, Attribute::AttrKind Kind
) {
346 CALLSITE_DELEGATE_SETTER(addAttribute(i
, Kind
));
349 void addAttribute(unsigned i
, Attribute Attr
) {
350 CALLSITE_DELEGATE_SETTER(addAttribute(i
, Attr
));
353 void addParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) {
354 CALLSITE_DELEGATE_SETTER(addParamAttr(ArgNo
, Kind
));
357 void removeAttribute(unsigned i
, Attribute::AttrKind Kind
) {
358 CALLSITE_DELEGATE_SETTER(removeAttribute(i
, Kind
));
361 void removeAttribute(unsigned i
, StringRef Kind
) {
362 CALLSITE_DELEGATE_SETTER(removeAttribute(i
, Kind
));
365 void removeParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) {
366 CALLSITE_DELEGATE_SETTER(removeParamAttr(ArgNo
, Kind
));
369 /// Return true if this function has the given attribute.
370 bool hasFnAttr(Attribute::AttrKind Kind
) const {
371 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind
));
374 /// Return true if this function has the given attribute.
375 bool hasFnAttr(StringRef Kind
) const {
376 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind
));
379 /// Return true if this return value has the given attribute.
380 bool hasRetAttr(Attribute::AttrKind Kind
) const {
381 CALLSITE_DELEGATE_GETTER(hasRetAttr(Kind
));
384 /// Return true if the call or the callee has the given attribute.
385 bool paramHasAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
386 CALLSITE_DELEGATE_GETTER(paramHasAttr(ArgNo
, Kind
));
389 Attribute
getAttribute(unsigned i
, Attribute::AttrKind Kind
) const {
390 CALLSITE_DELEGATE_GETTER(getAttribute(i
, Kind
));
393 Attribute
getAttribute(unsigned i
, StringRef Kind
) const {
394 CALLSITE_DELEGATE_GETTER(getAttribute(i
, Kind
));
397 /// Return true if the data operand at index \p i directly or indirectly has
398 /// the attribute \p A.
400 /// Normal call, invoke or callbr arguments have per operand attributes, as
401 /// specified in the attribute set attached to this instruction, while operand
402 /// bundle operands may have some attributes implied by the type of its
403 /// containing operand bundle.
404 bool dataOperandHasImpliedAttr(unsigned i
, Attribute::AttrKind Kind
) const {
405 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i
, Kind
));
408 /// Extract the alignment of the return value.
409 unsigned getRetAlignment() const {
410 CALLSITE_DELEGATE_GETTER(getRetAlignment());
413 /// Extract the alignment for a call or parameter (0=unknown).
414 unsigned getParamAlignment(unsigned ArgNo
) const {
415 CALLSITE_DELEGATE_GETTER(getParamAlignment(ArgNo
));
418 /// Extract the byval type for a call or parameter (nullptr=unknown).
419 Type
*getParamByValType(unsigned ArgNo
) const {
420 CALLSITE_DELEGATE_GETTER(getParamByValType(ArgNo
));
423 /// Extract the number of dereferenceable bytes for a call or parameter
425 uint64_t getDereferenceableBytes(unsigned i
) const {
426 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i
));
429 /// Extract the number of dereferenceable_or_null bytes for a call or
430 /// parameter (0=unknown).
431 uint64_t getDereferenceableOrNullBytes(unsigned i
) const {
432 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i
));
435 /// Determine if the return value is marked with NoAlias attribute.
436 bool returnDoesNotAlias() const {
437 CALLSITE_DELEGATE_GETTER(returnDoesNotAlias());
440 /// Return true if the call should not be treated as a call to a builtin.
441 bool isNoBuiltin() const {
442 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
445 /// Return true if the call requires strict floating point semantics.
446 bool isStrictFP() const {
447 CALLSITE_DELEGATE_GETTER(isStrictFP());
450 /// Return true if the call should not be inlined.
451 bool isNoInline() const {
452 CALLSITE_DELEGATE_GETTER(isNoInline());
454 void setIsNoInline(bool Value
= true) {
455 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value
));
458 /// Determine if the call does not access memory.
459 bool doesNotAccessMemory() const {
460 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
462 void setDoesNotAccessMemory() {
463 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
466 /// Determine if the call does not access or only reads memory.
467 bool onlyReadsMemory() const {
468 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
470 void setOnlyReadsMemory() {
471 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
474 /// Determine if the call does not access or only writes memory.
475 bool doesNotReadMemory() const {
476 CALLSITE_DELEGATE_GETTER(doesNotReadMemory());
478 void setDoesNotReadMemory() {
479 CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory());
482 /// Determine if the call can access memmory only using pointers based
483 /// on its arguments.
484 bool onlyAccessesArgMemory() const {
485 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
487 void setOnlyAccessesArgMemory() {
488 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
491 /// Determine if the function may only access memory that is
492 /// inaccessible from the IR.
493 bool onlyAccessesInaccessibleMemory() const {
494 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemory());
496 void setOnlyAccessesInaccessibleMemory() {
497 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemory());
500 /// Determine if the function may only access memory that is
501 /// either inaccessible from the IR or pointed to by its arguments.
502 bool onlyAccessesInaccessibleMemOrArgMem() const {
503 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemOrArgMem());
505 void setOnlyAccessesInaccessibleMemOrArgMem() {
506 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemOrArgMem());
509 /// Determine if the call cannot return.
510 bool doesNotReturn() const {
511 CALLSITE_DELEGATE_GETTER(doesNotReturn());
513 void setDoesNotReturn() {
514 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
517 /// Determine if the call cannot unwind.
518 bool doesNotThrow() const {
519 CALLSITE_DELEGATE_GETTER(doesNotThrow());
521 void setDoesNotThrow() {
522 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
525 /// Determine if the call can be duplicated.
526 bool cannotDuplicate() const {
527 CALLSITE_DELEGATE_GETTER(cannotDuplicate());
529 void setCannotDuplicate() {
530 CALLSITE_DELEGATE_SETTER(setCannotDuplicate());
533 /// Determine if the call is convergent.
534 bool isConvergent() const {
535 CALLSITE_DELEGATE_GETTER(isConvergent());
537 void setConvergent() {
538 CALLSITE_DELEGATE_SETTER(setConvergent());
540 void setNotConvergent() {
541 CALLSITE_DELEGATE_SETTER(setNotConvergent());
544 unsigned getNumOperandBundles() const {
545 CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
548 bool hasOperandBundles() const {
549 CALLSITE_DELEGATE_GETTER(hasOperandBundles());
552 unsigned getBundleOperandsStartIndex() const {
553 CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex());
556 unsigned getBundleOperandsEndIndex() const {
557 CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex());
560 unsigned getNumTotalBundleOperands() const {
561 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
564 OperandBundleUse
getOperandBundleAt(unsigned Index
) const {
565 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index
));
568 Optional
<OperandBundleUse
> getOperandBundle(StringRef Name
) const {
569 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name
));
572 Optional
<OperandBundleUse
> getOperandBundle(uint32_t ID
) const {
573 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID
));
576 unsigned countOperandBundlesOfType(uint32_t ID
) const {
577 CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID
));
580 bool isBundleOperand(unsigned Idx
) const {
581 CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx
));
584 IterTy
arg_begin() const {
585 CALLSITE_DELEGATE_GETTER(arg_begin());
588 IterTy
arg_end() const {
589 CALLSITE_DELEGATE_GETTER(arg_end());
592 #undef CALLSITE_DELEGATE_GETTER
593 #undef CALLSITE_DELEGATE_SETTER
595 void getOperandBundlesAsDefs(SmallVectorImpl
<OperandBundleDef
> &Defs
) const {
596 // Since this is actually a getter that "looks like" a setter, don't use the
597 // above macros to avoid confusion.
598 cast
<CallBase
>(getInstruction())->getOperandBundlesAsDefs(Defs
);
601 /// Determine whether this data operand is not captured.
602 bool doesNotCapture(unsigned OpNo
) const {
603 return dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::NoCapture
);
606 /// Determine whether this argument is passed by value.
607 bool isByValArgument(unsigned ArgNo
) const {
608 return paramHasAttr(ArgNo
, Attribute::ByVal
);
611 /// Determine whether this argument is passed in an alloca.
612 bool isInAllocaArgument(unsigned ArgNo
) const {
613 return paramHasAttr(ArgNo
, Attribute::InAlloca
);
616 /// Determine whether this argument is passed by value or in an alloca.
617 bool isByValOrInAllocaArgument(unsigned ArgNo
) const {
618 return paramHasAttr(ArgNo
, Attribute::ByVal
) ||
619 paramHasAttr(ArgNo
, Attribute::InAlloca
);
622 /// Determine if there are is an inalloca argument. Only the last argument can
623 /// have the inalloca attribute.
624 bool hasInAllocaArgument() const {
625 return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca
);
628 bool doesNotAccessMemory(unsigned OpNo
) const {
629 return dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::ReadNone
);
632 bool onlyReadsMemory(unsigned OpNo
) const {
633 return dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::ReadOnly
) ||
634 dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::ReadNone
);
637 bool doesNotReadMemory(unsigned OpNo
) const {
638 return dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::WriteOnly
) ||
639 dataOperandHasImpliedAttr(OpNo
+ 1, Attribute::ReadNone
);
642 /// Return true if the return value is known to be not null.
643 /// This may be because it has the nonnull attribute, or because at least
644 /// one byte is dereferenceable and the pointer is in addrspace(0).
645 bool isReturnNonNull() const {
646 if (hasRetAttr(Attribute::NonNull
))
648 else if (getDereferenceableBytes(AttributeList::ReturnIndex
) > 0 &&
649 !NullPointerIsDefined(getCaller(),
650 getType()->getPointerAddressSpace()))
656 /// Returns true if this CallSite passes the given Value* as an argument to
657 /// the called function.
658 bool hasArgument(const Value
*Arg
) const {
659 for (arg_iterator AI
= this->arg_begin(), E
= this->arg_end(); AI
!= E
;
661 if (AI
->get() == Arg
)
667 IterTy
getCallee() const {
668 return cast
<CallBase
>(getInstruction())->op_end() - 1;
672 class CallSite
: public CallSiteBase
<Function
, BasicBlock
, Value
, User
, Use
,
673 Instruction
, CallInst
, InvokeInst
,
674 CallBrInst
, User::op_iterator
> {
676 CallSite() = default;
677 CallSite(CallSiteBase B
) : CallSiteBase(B
) {}
678 CallSite(CallInst
*CI
) : CallSiteBase(CI
) {}
679 CallSite(InvokeInst
*II
) : CallSiteBase(II
) {}
680 CallSite(CallBrInst
*CBI
) : CallSiteBase(CBI
) {}
681 explicit CallSite(Instruction
*II
) : CallSiteBase(II
) {}
682 explicit CallSite(Value
*V
) : CallSiteBase(V
) {}
684 bool operator==(const CallSite
&CS
) const { return I
== CS
.I
; }
685 bool operator!=(const CallSite
&CS
) const { return I
!= CS
.I
; }
686 bool operator<(const CallSite
&CS
) const {
687 return getInstruction() < CS
.getInstruction();
691 friend struct DenseMapInfo
<CallSite
>;
693 User::op_iterator
getCallee() const;
698 /// An abstract call site is a wrapper that allows to treat direct,
699 /// indirect, and callback calls the same. If an abstract call site
700 /// represents a direct or indirect call site it behaves like a stripped
701 /// down version of a normal call site object. The abstract call site can
702 /// also represent a callback call, thus the fact that the initially
703 /// called function (=broker) may invoke a third one (=callback callee).
704 /// In this case, the abstract call site hides the middle man, hence the
705 /// broker function. The result is a representation of the callback call,
706 /// inside the broker, but in the context of the original call to the broker.
708 /// There are up to three functions involved when we talk about callback call
709 /// sites. The caller (1), which invokes the broker function. The broker
710 /// function (2), that will invoke the callee zero or more times. And finally
711 /// the callee (3), which is the target of the callback call.
713 /// The abstract call site will handle the mapping from parameters to arguments
714 /// depending on the semantic of the broker function. However, it is important
715 /// to note that the mapping is often partial. Thus, some arguments of the
716 /// call/invoke instruction are mapped to parameters of the callee while others
718 class AbstractCallSite
{
721 /// The encoding of a callback with regards to the underlying instruction.
722 struct CallbackInfo
{
724 /// For direct/indirect calls the parameter encoding is empty. If it is not,
725 /// the abstract call site represents a callback. In that case, the first
726 /// element of the encoding vector represents which argument of the call
727 /// site CS is the callback callee. The remaining elements map parameters
728 /// (identified by their position) to the arguments that will be passed
729 /// through (also identified by position but in the call site instruction).
731 /// NOTE that we use LLVM argument numbers (starting at 0) and not
732 /// clang/source argument numbers (starting at 1). The -1 entries represent
733 /// unknown values that are passed to the callee.
734 using ParameterEncodingTy
= SmallVector
<int, 0>;
735 ParameterEncodingTy ParameterEncoding
;
741 /// The underlying call site:
742 /// caller -> callee, if this is a direct or indirect call site
743 /// caller -> broker function, if this is a callback call site
746 /// The encoding of a callback with regards to the underlying instruction.
750 /// Sole constructor for abstract call sites (ACS).
752 /// An abstract call site can only be constructed through a llvm::Use because
753 /// each operand (=use) of an instruction could potentially be a different
754 /// abstract call site. Furthermore, even if the value of the llvm::Use is the
755 /// same, and the user is as well, the abstract call sites might not be.
757 /// If a use is not associated with an abstract call site the constructed ACS
758 /// will evaluate to false if converted to a boolean.
760 /// If the use is the callee use of a call or invoke instruction, the
761 /// constructed abstract call site will behave as a llvm::CallSite would.
763 /// If the use is not a callee use of a call or invoke instruction, the
764 /// callback metadata is used to determine the argument <-> parameter mapping
765 /// as well as the callee of the abstract call site.
766 AbstractCallSite(const Use
*U
);
768 /// Conversion operator to conveniently check for a valid/initialized ACS.
769 explicit operator bool() const { return (bool)CS
; }
771 /// Return the underlying instruction.
772 Instruction
*getInstruction() const { return CS
.getInstruction(); }
774 /// Return the call site abstraction for the underlying instruction.
775 CallSite
getCallSite() const { return CS
; }
777 /// Return true if this ACS represents a direct call.
778 bool isDirectCall() const {
779 return !isCallbackCall() && !CS
.isIndirectCall();
782 /// Return true if this ACS represents an indirect call.
783 bool isIndirectCall() const {
784 return !isCallbackCall() && CS
.isIndirectCall();
787 /// Return true if this ACS represents a callback call.
788 bool isCallbackCall() const {
789 // For a callback call site the callee is ALWAYS stored first in the
790 // transitive values vector. Thus, a non-empty vector indicates a callback.
791 return !CI
.ParameterEncoding
.empty();
794 /// Return true if @p UI is the use that defines the callee of this ACS.
795 bool isCallee(Value::const_user_iterator UI
) const {
796 return isCallee(&UI
.getUse());
799 /// Return true if @p U is the use that defines the callee of this ACS.
800 bool isCallee(const Use
*U
) const {
802 return CS
.isCallee(U
);
804 assert(!CI
.ParameterEncoding
.empty() &&
805 "Callback without parameter encoding!");
807 return (int)CS
.getArgumentNo(U
) == CI
.ParameterEncoding
[0];
810 /// Return the number of parameters of the callee.
811 unsigned getNumArgOperands() const {
813 return CS
.getNumArgOperands();
814 // Subtract 1 for the callee encoding.
815 return CI
.ParameterEncoding
.size() - 1;
818 /// Return the operand index of the underlying instruction associated with @p
820 int getCallArgOperandNo(Argument
&Arg
) const {
821 return getCallArgOperandNo(Arg
.getArgNo());
824 /// Return the operand index of the underlying instruction associated with
825 /// the function parameter number @p ArgNo or -1 if there is none.
826 int getCallArgOperandNo(unsigned ArgNo
) const {
829 // Add 1 for the callee encoding.
830 return CI
.ParameterEncoding
[ArgNo
+ 1];
833 /// Return the operand of the underlying instruction associated with @p Arg.
834 Value
*getCallArgOperand(Argument
&Arg
) const {
835 return getCallArgOperand(Arg
.getArgNo());
838 /// Return the operand of the underlying instruction associated with the
839 /// function parameter number @p ArgNo or nullptr if there is none.
840 Value
*getCallArgOperand(unsigned ArgNo
) const {
842 return CS
.getArgOperand(ArgNo
);
843 // Add 1 for the callee encoding.
844 return CI
.ParameterEncoding
[ArgNo
+ 1] >= 0
845 ? CS
.getArgOperand(CI
.ParameterEncoding
[ArgNo
+ 1])
849 /// Return the operand index of the underlying instruction associated with the
850 /// callee of this ACS. Only valid for callback calls!
851 int getCallArgOperandNoForCallee() const {
852 assert(isCallbackCall());
853 assert(CI
.ParameterEncoding
.size() && CI
.ParameterEncoding
[0] > 0);
854 return CI
.ParameterEncoding
[0];
857 /// Return the use of the callee value in the underlying instruction. Only
858 /// valid for callback calls!
859 const Use
&getCalleeUseForCallback() const {
860 int CalleeArgIdx
= getCallArgOperandNoForCallee();
861 assert(CalleeArgIdx
>= 0 &&
862 unsigned(CalleeArgIdx
) < getInstruction()->getNumOperands());
863 return getInstruction()->getOperandUse(CalleeArgIdx
);
866 /// Return the pointer to function that is being called.
867 Value
*getCalledValue() const {
869 return CS
.getCalledValue();
870 return CS
.getArgOperand(getCallArgOperandNoForCallee());
873 /// Return the function being called if this is a direct call, otherwise
874 /// return null (if it's an indirect call).
875 Function
*getCalledFunction() const {
876 Value
*V
= getCalledValue();
877 return V
? dyn_cast
<Function
>(V
->stripPointerCasts()) : nullptr;
881 template <> struct DenseMapInfo
<CallSite
> {
882 using BaseInfo
= DenseMapInfo
<decltype(CallSite::I
)>;
884 static CallSite
getEmptyKey() {
886 CS
.I
= BaseInfo::getEmptyKey();
890 static CallSite
getTombstoneKey() {
892 CS
.I
= BaseInfo::getTombstoneKey();
896 static unsigned getHashValue(const CallSite
&CS
) {
897 return BaseInfo::getHashValue(CS
.I
);
900 static bool isEqual(const CallSite
&LHS
, const CallSite
&RHS
) {
905 /// Establish a view to a call site for examination.
906 class ImmutableCallSite
: public CallSiteBase
<> {
908 ImmutableCallSite() = default;
909 ImmutableCallSite(const CallInst
*CI
) : CallSiteBase(CI
) {}
910 ImmutableCallSite(const InvokeInst
*II
) : CallSiteBase(II
) {}
911 ImmutableCallSite(const CallBrInst
*CBI
) : CallSiteBase(CBI
) {}
912 explicit ImmutableCallSite(const Instruction
*II
) : CallSiteBase(II
) {}
913 explicit ImmutableCallSite(const Value
*V
) : CallSiteBase(V
) {}
914 ImmutableCallSite(CallSite CS
) : CallSiteBase(CS
.getInstruction()) {}
917 } // end namespace llvm
919 #endif // LLVM_IR_CALLSITE_H