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/CallingConv.h"
30 #include "llvm/Instruction.h"
38 PointerIntPair<Instruction*, 1, bool> I;
40 CallSite() : I(0, false) {}
41 CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI), true) {}
42 CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II), false) {}
43 CallSite(Instruction *C);
45 bool operator==(const CallSite &CS) const { return I == CS.I; }
46 bool operator!=(const CallSite &CS) const { return I != CS.I; }
48 /// CallSite::get - This static method is sort of like a constructor. It will
49 /// create an appropriate call site for a Call or Invoke instruction, but it
50 /// can also create a null initialized CallSite object for something which is
53 static CallSite get(Value *V) {
54 if (Instruction *I = dyn_cast<Instruction>(V)) {
55 if (I->getOpcode() == Instruction::Call)
56 return CallSite(reinterpret_cast<CallInst*>(I));
57 else if (I->getOpcode() == Instruction::Invoke)
58 return CallSite(reinterpret_cast<InvokeInst*>(I));
63 /// getCallingConv/setCallingConv - get or set the calling convention of the
65 CallingConv::ID getCallingConv() const;
66 void setCallingConv(CallingConv::ID CC);
68 /// getAttributes/setAttributes - get or set the parameter attributes of
70 const AttrListPtr &getAttributes() const;
71 void setAttributes(const AttrListPtr &PAL);
73 /// paramHasAttr - whether the call or the callee has the given attribute.
74 bool paramHasAttr(uint16_t i, Attributes attr) const;
76 /// @brief Extract the alignment for a call or parameter (0=unknown).
77 uint16_t getParamAlignment(uint16_t i) const;
79 /// @brief Return true if the call should not be inlined.
80 bool isNoInline() const;
81 void setIsNoInline(bool Value = true);
83 /// @brief Determine if the call does not access memory.
84 bool doesNotAccessMemory() const;
85 void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
87 /// @brief Determine if the call does not access or only reads memory.
88 bool onlyReadsMemory() const;
89 void setOnlyReadsMemory(bool onlyReadsMemory = true);
91 /// @brief Determine if the call cannot return.
92 bool doesNotReturn() const;
93 void setDoesNotReturn(bool doesNotReturn = true);
95 /// @brief Determine if the call cannot unwind.
96 bool doesNotThrow() const;
97 void setDoesNotThrow(bool doesNotThrow = true);
99 /// getType - Return the type of the instruction that generated this call site
101 const Type *getType() const { return getInstruction()->getType(); }
103 /// isCall - true if a CallInst is enclosed.
104 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
105 /// it also could signify a NULL Instruction pointer.
106 bool isCall() const { return I.getInt(); }
108 /// isInvoke - true if a InvokeInst is enclosed.
110 bool isInvoke() const { return getInstruction() && !I.getInt(); }
112 /// getInstruction - Return the instruction this call site corresponds to
114 Instruction *getInstruction() const { return I.getPointer(); }
116 /// getCaller - Return the caller function for this call site
118 Function *getCaller() const { return getInstruction()
119 ->getParent()->getParent(); }
121 /// getCalledValue - Return the pointer to function that is being called...
123 Value *getCalledValue() const {
124 assert(getInstruction() && "Not a call or invoke instruction!");
128 /// getCalledFunction - Return the function being called if this is a direct
129 /// call, otherwise return null (if it's an indirect call).
131 Function *getCalledFunction() const {
132 return dyn_cast<Function>(getCalledValue());
135 /// setCalledFunction - Set the callee to the specified value...
137 void setCalledFunction(Value *V) {
138 assert(getInstruction() && "Not a call or invoke instruction!");
142 Value *getArgument(unsigned ArgNo) const {
143 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
144 return *(arg_begin()+ArgNo);
147 void setArgument(unsigned ArgNo, Value* newVal) {
148 assert(getInstruction() && "Not a call or invoke instruction!");
149 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
150 getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
153 /// Given a value use iterator, returns the argument that corresponds to it.
154 /// Iterator must actually correspond to an argument.
155 unsigned getArgumentNo(Value::use_iterator I) const {
156 assert(getInstruction() && "Not a call or invoke instruction!");
157 assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
158 && "Argument # out of range!");
160 return &I.getUse() - arg_begin();
163 /// Given an operand number, returns the argument that corresponds to it.
164 /// OperandNo must be a valid operand number that actually corresponds to an
166 unsigned getArgumentNo(unsigned OperandNo) const {
167 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
169 return OperandNo - getArgumentOffset();
172 /// hasArgument - Returns true if this CallSite passes the given Value* as an
173 /// argument to the called function.
174 bool hasArgument(const Value *Arg) const;
176 /// arg_iterator - The type of iterator to use when looping over actual
177 /// arguments at this call site...
178 typedef User::op_iterator arg_iterator;
180 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
181 /// list for a call site.
182 arg_iterator arg_begin() const {
183 assert(getInstruction() && "Not a call or invoke instruction!");
184 // Skip non-arguments
185 return getInstruction()->op_begin() + getArgumentOffset();
188 arg_iterator arg_end() const { return getInstruction()->op_end() - getArgumentEndOffset(); }
189 bool arg_empty() const { return arg_end() == arg_begin(); }
190 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
192 bool operator<(const CallSite &CS) const {
193 return getInstruction() < CS.getInstruction();
196 bool isCallee(Value::use_iterator UI) const {
197 return getCallee() == &UI.getUse();
199 bool isCallee(Value::use_const_iterator UI) const {
200 return getCallee() == &UI.getUse();
203 /// Returns the operand number of the first argument
204 unsigned getArgumentOffset() const {
206 return 1; // Skip Function
208 return 0; // Args are at the front
211 unsigned getArgumentEndOffset() const {
213 return 0; // Unchanged
215 return 3; // Skip BB, BB, Function
218 User::op_iterator getCallee() const;
221 } // End llvm namespace