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 I == CS.I; }
47 bool operator!=(const CallSite &CS) const { return I != CS.I; }
49 /// CallSite::get - This static method is sort of like a constructor. It will
50 /// create an appropriate call site for a Call or Invoke instruction, but it
51 /// can also create a null initialized CallSite object for something which is
54 static CallSite get(Value *V) {
55 if (Instruction *I = dyn_cast<Instruction>(V)) {
56 if (I->getOpcode() == Instruction::Call)
57 return CallSite(reinterpret_cast<CallInst*>(I));
58 else if (I->getOpcode() == Instruction::Invoke)
59 return CallSite(reinterpret_cast<InvokeInst*>(I));
64 /// getCallingConv/setCallingConv - get or set the calling convention of the
66 unsigned getCallingConv() const;
67 void setCallingConv(unsigned CC);
69 /// getAttributes/setAttributes - get or set the parameter attributes of
71 const AttrListPtr &getAttributes() const;
72 void setAttributes(const AttrListPtr &PAL);
74 /// paramHasAttr - whether the call or the callee has the given attribute.
75 bool paramHasAttr(uint16_t i, Attributes attr) const;
77 /// @brief Extract the alignment for a call or parameter (0=unknown).
78 uint16_t getParamAlignment(uint16_t i) const;
80 /// @brief Determine if the call does not access memory.
81 bool doesNotAccessMemory() const;
82 void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
84 /// @brief Determine if the call does not access or only reads memory.
85 bool onlyReadsMemory() const;
86 void setOnlyReadsMemory(bool onlyReadsMemory = true);
88 /// @brief Determine if the call cannot return.
89 bool doesNotReturn() const;
90 void setDoesNotReturn(bool doesNotReturn = true);
92 /// @brief Determine if the call cannot unwind.
93 bool doesNotThrow() const;
94 void setDoesNotThrow(bool doesNotThrow = true);
96 /// getType - Return the type of the instruction that generated this call site
98 const Type *getType() const { return getInstruction()->getType(); }
100 /// isCall - true if a CallInst is enclosed.
101 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
102 /// it also could signify a NULL Instruction pointer.
103 bool isCall() const { return I.getInt(); }
105 /// isInvoke - true if a InvokeInst is enclosed.
107 bool isInvoke() const { return getInstruction() && !I.getInt(); }
109 /// getInstruction - Return the instruction this call site corresponds to
111 Instruction *getInstruction() const { return I.getPointer(); }
113 /// getCaller - Return the caller function for this call site
115 Function *getCaller() const { return getInstruction()
116 ->getParent()->getParent(); }
118 /// getCalledValue - Return the pointer to function that is being called...
120 Value *getCalledValue() const {
121 assert(getInstruction() && "Not a call or invoke instruction!");
122 return getInstruction()->getOperand(0);
125 /// getCalledFunction - Return the function being called if this is a direct
126 /// call, otherwise return null (if it's an indirect call).
128 Function *getCalledFunction() const {
129 return dyn_cast<Function>(getCalledValue());
132 /// setCalledFunction - Set the callee to the specified value...
134 void setCalledFunction(Value *V) {
135 assert(getInstruction() && "Not a call or invoke instruction!");
136 getInstruction()->setOperand(0, V);
139 Value *getArgument(unsigned ArgNo) const {
140 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
141 return *(arg_begin()+ArgNo);
144 void setArgument(unsigned ArgNo, Value* newVal) {
145 assert(getInstruction() && "Not a call or invoke instruction!");
146 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
147 getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
150 /// Given an operand number, returns the argument that corresponds to it.
151 /// OperandNo must be a valid operand number that actually corresponds to an
153 unsigned getArgumentNo(unsigned OperandNo) const {
154 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
156 return OperandNo - getArgumentOffset();
159 /// hasArgument - Returns true if this CallSite passes the given Value* as an
160 /// argument to the called function.
161 bool hasArgument(const Value *Arg) const;
163 /// arg_iterator - The type of iterator to use when looping over actual
164 /// arguments at this call site...
165 typedef User::op_iterator arg_iterator;
167 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
168 /// list for a call site.
169 arg_iterator arg_begin() const {
170 assert(getInstruction() && "Not a call or invoke instruction!");
171 // Skip non-arguments
172 return getInstruction()->op_begin() + getArgumentOffset();
175 arg_iterator arg_end() const { return getInstruction()->op_end(); }
176 bool arg_empty() const { return arg_end() == arg_begin(); }
177 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
179 bool operator<(const CallSite &CS) const {
180 return getInstruction() < CS.getInstruction();
183 bool isCallee(Value::use_iterator UI) const {
184 return getInstruction()->op_begin() == &UI.getUse();
188 /// Returns the operand number of the first argument
189 unsigned getArgumentOffset() const {
191 return 1; // Skip Function
193 return 3; // Skip Function, BB, BB
197 } // End llvm namespace