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 Determine if the call does not access memory.
80 bool doesNotAccessMemory() const;
81 void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
83 /// @brief Determine if the call does not access or only reads memory.
84 bool onlyReadsMemory() const;
85 void setOnlyReadsMemory(bool onlyReadsMemory = true);
87 /// @brief Determine if the call cannot return.
88 bool doesNotReturn() const;
89 void setDoesNotReturn(bool doesNotReturn = true);
91 /// @brief Determine if the call cannot unwind.
92 bool doesNotThrow() const;
93 void setDoesNotThrow(bool doesNotThrow = true);
95 /// getType - Return the type of the instruction that generated this call site
97 const Type *getType() const { return getInstruction()->getType(); }
99 /// isCall - true if a CallInst is enclosed.
100 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
101 /// it also could signify a NULL Instruction pointer.
102 bool isCall() const { return I.getInt(); }
104 /// isInvoke - true if a InvokeInst is enclosed.
106 bool isInvoke() const { return getInstruction() && !I.getInt(); }
108 /// getInstruction - Return the instruction this call site corresponds to
110 Instruction *getInstruction() const { return I.getPointer(); }
112 /// getCaller - Return the caller function for this call site
114 Function *getCaller() const { return getInstruction()
115 ->getParent()->getParent(); }
117 /// getCalledValue - Return the pointer to function that is being called...
119 Value *getCalledValue() const {
120 assert(getInstruction() && "Not a call or invoke instruction!");
121 return getInstruction()->getOperand(0);
124 /// getCalledFunction - Return the function being called if this is a direct
125 /// call, otherwise return null (if it's an indirect call).
127 Function *getCalledFunction() const {
128 return dyn_cast<Function>(getCalledValue());
131 /// setCalledFunction - Set the callee to the specified value...
133 void setCalledFunction(Value *V) {
134 assert(getInstruction() && "Not a call or invoke instruction!");
135 getInstruction()->setOperand(0, V);
138 Value *getArgument(unsigned ArgNo) const {
139 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
140 return *(arg_begin()+ArgNo);
143 void setArgument(unsigned ArgNo, Value* newVal) {
144 assert(getInstruction() && "Not a call or invoke instruction!");
145 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
146 getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
149 /// Given an operand number, returns the argument that corresponds to it.
150 /// OperandNo must be a valid operand number that actually corresponds to an
152 unsigned getArgumentNo(unsigned OperandNo) const {
153 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
155 return OperandNo - getArgumentOffset();
158 /// hasArgument - Returns true if this CallSite passes the given Value* as an
159 /// argument to the called function.
160 bool hasArgument(const Value *Arg) const;
162 /// arg_iterator - The type of iterator to use when looping over actual
163 /// arguments at this call site...
164 typedef User::op_iterator arg_iterator;
166 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
167 /// list for a call site.
168 arg_iterator arg_begin() const {
169 assert(getInstruction() && "Not a call or invoke instruction!");
170 // Skip non-arguments
171 return getInstruction()->op_begin() + getArgumentOffset();
174 arg_iterator arg_end() const { return getInstruction()->op_end(); }
175 bool arg_empty() const { return arg_end() == arg_begin(); }
176 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
178 bool operator<(const CallSite &CS) const {
179 return getInstruction() < CS.getInstruction();
182 bool isCallee(Value::use_iterator UI) const {
183 return getInstruction()->op_begin() == &UI.getUse();
187 /// Returns the operand number of the first argument
188 unsigned getArgumentOffset() const {
190 return 1; // Skip Function
192 return 3; // Skip Function, BB, BB
196 } // End llvm namespace