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);
44 bool operator==(const CallSite &CS) const { return I == CS.I; }
45 bool operator!=(const CallSite &CS) const { return I != CS.I; }
47 /// CallSite::get - This static method is sort of like a constructor. It will
48 /// create an appropriate call site for a Call or Invoke instruction, but it
49 /// can also create a null initialized CallSite object for something which is
52 static CallSite get(Value *V) {
53 if (Instruction *I = dyn_cast<Instruction>(V)) {
54 if (I->getOpcode() == Instruction::Call)
55 return CallSite(reinterpret_cast<CallInst*>(I));
56 else if (I->getOpcode() == Instruction::Invoke)
57 return CallSite(reinterpret_cast<InvokeInst*>(I));
62 /// getCallingConv/setCallingConv - get or set the calling convention of the
64 unsigned getCallingConv() const;
65 void setCallingConv(unsigned CC);
67 /// getAttributes/setAttributes - get or set the parameter attributes of
69 const AttrListPtr &getAttributes() const;
70 void setAttributes(const AttrListPtr &PAL);
72 /// paramHasAttr - whether the call or the callee has the given attribute.
73 bool paramHasAttr(uint16_t i, Attributes attr) const;
75 /// @brief Extract the alignment for a call or parameter (0=unknown).
76 uint16_t getParamAlignment(uint16_t i) const;
78 /// @brief Determine if the call does not access memory.
79 bool doesNotAccessMemory() const;
80 void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
82 /// @brief Determine if the call does not access or only reads memory.
83 bool onlyReadsMemory() const;
84 void setOnlyReadsMemory(bool onlyReadsMemory = true);
86 /// @brief Determine if the call cannot return.
87 bool doesNotReturn() const;
88 void setDoesNotReturn(bool doesNotReturn = true);
90 /// @brief Determine if the call cannot unwind.
91 bool doesNotThrow() const;
92 void setDoesNotThrow(bool doesNotThrow = true);
94 /// getType - Return the type of the instruction that generated this call site
96 const Type *getType() const { return getInstruction()->getType(); }
98 /// isCall - true if a CallInst is enclosed.
99 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
100 /// it also could signify a NULL Instruction pointer.
101 bool isCall() const { return I.getInt(); }
103 /// isInvoke - true if a InvokeInst is enclosed.
105 bool isInvoke() const { return getInstruction() && !I.getInt(); }
107 /// getInstruction - Return the instruction this call site corresponds to
109 Instruction *getInstruction() const { return I.getPointer(); }
111 /// getCaller - Return the caller function for this call site
113 Function *getCaller() const { return getInstruction()
114 ->getParent()->getParent(); }
116 /// getCalledValue - Return the pointer to function that is being called...
118 Value *getCalledValue() const {
119 assert(getInstruction() && "Not a call or invoke instruction!");
120 return getInstruction()->getOperand(0);
123 /// getCalledFunction - Return the function being called if this is a direct
124 /// call, otherwise return null (if it's an indirect call).
126 Function *getCalledFunction() const {
127 return dyn_cast<Function>(getCalledValue());
130 /// setCalledFunction - Set the callee to the specified value...
132 void setCalledFunction(Value *V) {
133 assert(getInstruction() && "Not a call or invoke instruction!");
134 getInstruction()->setOperand(0, V);
137 Value *getArgument(unsigned ArgNo) const {
138 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
139 return *(arg_begin()+ArgNo);
142 void setArgument(unsigned ArgNo, Value* newVal) {
143 assert(getInstruction() && "Not a call or invoke instruction!");
144 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
145 getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
148 /// Given an operand number, returns the argument that corresponds to it.
149 /// OperandNo must be a valid operand number that actually corresponds to an
151 unsigned getArgumentNo(unsigned OperandNo) const {
152 assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
154 return OperandNo - getArgumentOffset();
157 /// hasArgument - Returns true if this CallSite passes the given Value* as an
158 /// argument to the called function.
159 bool hasArgument(const Value *Arg) const;
161 /// arg_iterator - The type of iterator to use when looping over actual
162 /// arguments at this call site...
163 typedef User::op_iterator arg_iterator;
165 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
166 /// list for a call site.
167 arg_iterator arg_begin() const {
168 assert(getInstruction() && "Not a call or invoke instruction!");
169 // Skip non-arguments
170 return getInstruction()->op_begin() + getArgumentOffset();
173 arg_iterator arg_end() const { return getInstruction()->op_end(); }
174 bool arg_empty() const { return arg_end() == arg_begin(); }
175 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
177 bool operator<(const CallSite &CS) const {
178 return getInstruction() < CS.getInstruction();
181 bool isCallee(Value::use_iterator UI) const {
182 return getInstruction()->op_begin() == &UI.getUse();
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