1 //===-- llvm/Support/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.
13 // NOTE: This class is supposed to have "value semantics". So it should be
14 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
15 // is efficiently copyable, assignable and constructable, with cost equivalent
16 // to copying a pointer (notice that it has only a single data member).
17 // The internal representation carries a flag which indicates which of the two
18 // variants is enclosed. This allows for cheaper checks when various accessors
19 // of CallSite are employed.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_CALLSITE_H
24 #define LLVM_SUPPORT_CALLSITE_H
26 #include "llvm/Attributes.h"
27 #include "llvm/ADT/PointerIntPair.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/Instruction.h"
37 PointerIntPair<Instruction*, 1, bool> I;
39 CallSite() : I(0, false) {}
40 CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI), true) {}
41 CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II), false) {}
42 CallSite(Instruction *C);
43 CallSite(const CallSite &CS) : I(CS.I) {}
44 CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
46 bool operator==(const CallSite &CS) const { return getInstruction()
47 == CS.getInstruction(); }
48 bool operator!=(const CallSite &CS) const { return getInstruction()
49 != CS.getInstruction(); }
51 /// CallSite::get - This static method is sort of like a constructor. It will
52 /// create an appropriate call site for a Call or Invoke instruction, but it
53 /// can also create a null initialized CallSite object for something which is
56 static CallSite get(Value *V) {
57 if (Instruction *I = dyn_cast<Instruction>(V)) {
58 if (I->getOpcode() == Instruction::Call)
59 return CallSite(reinterpret_cast<CallInst*>(I));
60 else if (I->getOpcode() == Instruction::Invoke)
61 return CallSite(reinterpret_cast<InvokeInst*>(I));
66 /// getCallingConv/setCallingConv - get or set the calling convention of the
68 unsigned getCallingConv() const;
69 void setCallingConv(unsigned CC);
71 /// getAttributes/setAttributes - get or set the parameter attributes of
73 const AttrListPtr &getAttributes() const;
74 void setAttributes(const AttrListPtr &PAL);
76 /// paramHasAttr - whether the call or the callee has the given attribute.
77 bool paramHasAttr(uint16_t i, Attributes attr) const;
79 /// @brief Extract the alignment for a call or parameter (0=unknown).
80 uint16_t getParamAlignment(uint16_t i) const;
82 /// @brief Determine if the call does not access memory.
83 bool doesNotAccessMemory() const;
84 void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
86 /// @brief Determine if the call does not access or only reads memory.
87 bool onlyReadsMemory() const;
88 void setOnlyReadsMemory(bool onlyReadsMemory = true);
90 /// @brief Determine if the call cannot return.
91 bool doesNotReturn() const;
92 void setDoesNotReturn(bool doesNotReturn = true);
94 /// @brief Determine if the call cannot unwind.
95 bool doesNotThrow() const;
96 void setDoesNotThrow(bool doesNotThrow = true);
98 /// getType - Return the type of the instruction that generated this call site
100 const Type *getType() const { return getInstruction()->getType(); }
102 /// isCall - true if a CallInst is enclosed.
103 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
104 /// it also could signify a NULL Instruction pointer.
105 bool isCall() const { return I.getInt(); }
107 /// isInvoke - true if a InvokeInst is enclosed.
109 bool isInvoke() const { return getInstruction() && !I.getInt(); }
111 /// getInstruction - Return the instruction this call site corresponds to
113 Instruction *getInstruction() const { return I.getPointer(); }
115 /// getCaller - Return the caller function for this call site
117 Function *getCaller() const { return getInstruction()
118 ->getParent()->getParent(); }
120 /// getCalledValue - Return the pointer to function that is being called...
122 Value *getCalledValue() const {
123 assert(getInstruction() && "Not a call or invoke instruction!");
124 return getInstruction()->getOperand(0);
127 /// getCalledFunction - Return the function being called if this is a direct
128 /// call, otherwise return null (if it's an indirect call).
130 Function *getCalledFunction() const {
131 return dyn_cast<Function>(getCalledValue());
134 /// setCalledFunction - Set the callee to the specified value...
136 void setCalledFunction(Value *V) {
137 assert(getInstruction() && "Not a call or invoke instruction!");
138 getInstruction()->setOperand(0, V);
141 Value *getArgument(unsigned ArgNo) const {
142 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
143 return *(arg_begin()+ArgNo);
146 void setArgument(unsigned ArgNo, Value* newVal) {
147 assert(getInstruction() && "Not a call or invoke instruction!");
148 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
149 getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
152 /// Given an operand number, returns the argument that corresponds to it.
153 /// OperandNo must be a valid operand number that actually corresponds to an
155 unsigned getArgumentNo(unsigned OperandNo) const {
156 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
158 return OperandNo - getArgumentOffset();
161 /// hasArgument - Returns true if this CallSite passes the given Value* as an
162 /// argument to the called function.
163 bool hasArgument(const Value *Arg) const;
165 /// arg_iterator - The type of iterator to use when looping over actual
166 /// arguments at this call site...
167 typedef User::op_iterator arg_iterator;
169 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
170 /// list for a call site.
171 arg_iterator arg_begin() const {
172 assert(getInstruction() && "Not a call or invoke instruction!");
173 // Skip non-arguments
174 return getInstruction()->op_begin() + getArgumentOffset();
177 arg_iterator arg_end() const { return getInstruction()->op_end(); }
178 bool arg_empty() const { return arg_end() == arg_begin(); }
179 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
181 bool operator<(const CallSite &CS) const {
182 return getInstruction() < CS.getInstruction();
186 /// Returns the operand number of the first argument
187 unsigned getArgumentOffset() const {
189 return 1; // Skip Function
191 return 3; // Skip Function, BB, BB
195 } // End llvm namespace