remove bogus assert, use static_cast for additional checking
[oota-llvm.git] / include / llvm / Support / CallSite.h
1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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. When in non-
12 // mutation context (e.g. an analysis) ImmutableCallSite should be used.
13 // Finally, when some degree of customization is necessary between these two
14 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
15 //
16 // NOTE: These classes are supposed to have "value semantics". So they should be
17 // passed by value, not by reference; they should not be "new"ed or "delete"d.
18 // They are efficiently copyable, assignable and constructable, with cost
19 // equivalent to copying a pointer (notice that they have only a single data
20 // member). The internal representation carries a flag which indicates which of
21 // the two variants is enclosed. This allows for cheaper checks when various
22 // accessors of CallSite are employed.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #ifndef LLVM_SUPPORT_CALLSITE_H
27 #define LLVM_SUPPORT_CALLSITE_H
28
29 #include "llvm/Attributes.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/BasicBlock.h"
32 #include "llvm/CallingConv.h"
33 #include "llvm/Instructions.h"
34
35 namespace llvm {
36
37 class CallInst;
38 class InvokeInst;
39
40 template <typename FunTy = const Function,
41           typename ValTy = const Value,
42           typename UserTy = const User,
43           typename InstrTy = const Instruction,
44           typename CallTy = const CallInst,
45           typename InvokeTy = const InvokeInst,
46           typename IterTy = User::const_op_iterator>
47 class CallSiteBase {
48 protected:
49   PointerIntPair<InstrTy*, 1, bool> I;
50 public:
51   CallSiteBase() : I(0, false) {}
52   CallSiteBase(CallTy *CI) : I(CI, true) { /*assert(CI);*/ }
53   CallSiteBase(InvokeTy *II) : I(II, false) { /*assert(II);*/ }
54   CallSiteBase(ValTy *II) { *this = get(II); }
55   CallSiteBase(InstrTy *II) {
56     assert(II && "Null instruction given?");
57     *this = get(II);
58   }
59
60   /// CallSiteBase::get - This static method is sort of like a constructor.  It
61   /// will create an appropriate call site for a Call or Invoke instruction, but
62   /// it can also create a null initialized CallSiteBase object for something
63   /// which is NOT a call site.
64   ///
65   static CallSiteBase get(ValTy *V) {
66     if (InstrTy *II = dyn_cast<InstrTy>(V)) {
67       if (II->getOpcode() == Instruction::Call)
68         return CallSiteBase(static_cast<CallTy*>(II));
69       else if (II->getOpcode() == Instruction::Invoke)
70         return CallSiteBase(static_cast<InvokeTy*>(II));
71     }
72     return CallSiteBase();
73   }
74
75   /// isCall - true if a CallInst is enclosed.
76   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
77   /// it also could signify a NULL Instruction pointer.
78   bool isCall() const { return I.getInt(); }
79
80   /// isInvoke - true if a InvokeInst is enclosed.
81   ///
82   bool isInvoke() const { return getInstruction() && !I.getInt(); }
83
84   InstrTy *getInstruction() const { return I.getPointer(); }
85   InstrTy *operator->() const { return I.getPointer(); }
86   operator bool() const { return I.getPointer(); }
87
88   /// getCalledValue - Return the pointer to function that is being called...
89   ///
90   ValTy *getCalledValue() const {
91     assert(getInstruction() && "Not a call or invoke instruction!");
92     return *getCallee();
93   }
94
95   /// getCalledFunction - Return the function being called if this is a direct
96   /// call, otherwise return null (if it's an indirect call).
97   ///
98   FunTy *getCalledFunction() const {
99     return dyn_cast<FunTy>(getCalledValue());
100   }
101
102   /// setCalledFunction - Set the callee to the specified value...
103   ///
104   void setCalledFunction(Value *V) {
105     assert(getInstruction() && "Not a call or invoke instruction!");
106     *getCallee() = V;
107   }
108
109   /// isCallee - Determine whether the passed iterator points to the
110   /// callee operand's Use.
111   ///
112   bool isCallee(value_use_iterator<UserTy> UI) const {
113     return getCallee() == &UI.getUse();
114   }
115
116   ValTy *getArgument(unsigned ArgNo) const {
117     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
118     return *(arg_begin()+ArgNo);
119   }
120
121   void setArgument(unsigned ArgNo, Value* newVal) {
122     assert(getInstruction() && "Not a call or invoke instruction!");
123     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
124     getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
125   }
126
127   /// Given a value use iterator, returns the argument that corresponds to it.
128   /// Iterator must actually correspond to an argument.
129   unsigned getArgumentNo(value_use_iterator<UserTy> I) const {
130     assert(getInstruction() && "Not a call or invoke instruction!");
131     assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
132            && "Argument # out of range!");
133     return &I.getUse() - arg_begin();
134   }
135
136   /// arg_iterator - The type of iterator to use when looping over actual
137   /// arguments at this call site...
138   typedef IterTy arg_iterator;
139
140   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
141   /// list for a call site.
142   IterTy arg_begin() const {
143     assert(getInstruction() && "Not a call or invoke instruction!");
144     // Skip non-arguments
145     return (*this)->op_begin() + getArgumentOffset();
146   }
147
148   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
149   bool arg_empty() const { return arg_end() == arg_begin(); }
150   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
151   
152   /// getType - Return the type of the instruction that generated this call site
153   ///
154   const Type *getType() const { return (*this)->getType(); }
155
156   /// getCaller - Return the caller function for this call site
157   ///
158   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
159
160 #define CALLSITE_DELEGATE_GETTER(METHOD) \
161   InstrTy *II = getInstruction();    \
162   return isCall()                        \
163     ? cast<CallInst>(II)->METHOD         \
164     : cast<InvokeInst>(II)->METHOD
165
166 #define CALLSITE_DELEGATE_SETTER(METHOD) \
167   InstrTy *II = getInstruction();    \
168   if (isCall())                          \
169     cast<CallInst>(II)->METHOD;          \
170   else                                   \
171     cast<InvokeInst>(II)->METHOD
172
173   /// getCallingConv/setCallingConv - get or set the calling convention of the
174   /// call.
175   CallingConv::ID getCallingConv() const {
176     CALLSITE_DELEGATE_GETTER(getCallingConv());
177   }
178   void setCallingConv(CallingConv::ID CC) {
179     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
180   }
181
182   /// getAttributes/setAttributes - get or set the parameter attributes of
183   /// the call.
184   const AttrListPtr &getAttributes() const {
185     CALLSITE_DELEGATE_GETTER(getAttributes());
186   }
187   void setAttributes(const AttrListPtr &PAL) {
188     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
189   }
190
191   /// paramHasAttr - whether the call or the callee has the given attribute.
192   bool paramHasAttr(uint16_t i, Attributes attr) const {
193     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
194   }
195
196   /// @brief Extract the alignment for a call or parameter (0=unknown).
197   uint16_t getParamAlignment(uint16_t i) const {
198     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
199   }
200
201   /// @brief Return true if the call should not be inlined.
202   bool isNoInline() const {
203     CALLSITE_DELEGATE_GETTER(isNoInline());
204   }
205   void setIsNoInline(bool Value = true) {
206     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
207   }
208
209   /// @brief Determine if the call does not access memory.
210   bool doesNotAccessMemory() const {
211     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
212   }
213   void setDoesNotAccessMemory(bool doesNotAccessMemory = true) {
214     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
215   }
216
217   /// @brief Determine if the call does not access or only reads memory.
218   bool onlyReadsMemory() const {
219     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
220   }
221   void setOnlyReadsMemory(bool onlyReadsMemory = true) {
222     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
223   }
224
225   /// @brief Determine if the call cannot return.
226   bool doesNotReturn() const {
227     CALLSITE_DELEGATE_GETTER(doesNotReturn());
228   }
229   void setDoesNotReturn(bool doesNotReturn = true) {
230     CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
231   }
232
233   /// @brief Determine if the call cannot unwind.
234   bool doesNotThrow() const {
235     CALLSITE_DELEGATE_GETTER(doesNotThrow());
236   }
237   void setDoesNotThrow(bool doesNotThrow = true) {
238     CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
239   }
240
241 #undef CALLSITE_DELEGATE_GETTER
242 #undef CALLSITE_DELEGATE_SETTER
243
244   /// hasArgument - Returns true if this CallSite passes the given Value* as an
245   /// argument to the called function.
246   bool hasArgument(const Value *Arg) const {
247     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
248          ++AI)
249       if (AI->get() == Arg)
250         return true;
251     return false;
252   }
253
254 private:
255   /// Returns the operand number of the first argument
256   /// FIXME: remove this func!
257   unsigned getArgumentOffset() const {
258     return 0; // Args are at the front
259   }
260
261   unsigned getArgumentEndOffset() const {
262     if (isCall())
263       return 1; // Skip Callee
264     else
265       return 3; // Skip BB, BB, Callee
266   }
267
268   IterTy getCallee() const {
269       // FIXME: this is slow, since we do not have the fast versions
270       // of the op_*() functions here. See CallSite::getCallee.
271       //
272     if (isCall())
273       return getInstruction()->op_end() - 1; // Skip Callee
274     else
275       return getInstruction()->op_end() - 3; // Skip BB, BB, Callee
276   }
277 };
278
279 /// ImmutableCallSite - establish a view to a call site for examination
280 class ImmutableCallSite : public CallSiteBase<> {
281   typedef CallSiteBase<> Base;
282 public:
283   ImmutableCallSite(const Value* V) : Base(V) {}
284   ImmutableCallSite(const CallInst *CI) : Base(CI) {}
285   ImmutableCallSite(const InvokeInst *II) : Base(II) {}
286   ImmutableCallSite(const Instruction *II) : Base(II) {}
287 };
288
289 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
290                                      CallInst, InvokeInst, User::op_iterator> {
291   typedef CallSiteBase<Function, Value, User, Instruction,
292                        CallInst, InvokeInst, User::op_iterator> Base;
293 public:
294   CallSite() {}
295   CallSite(Base B) : Base(B) {}
296   CallSite(CallInst *CI) : Base(CI) {}
297   CallSite(InvokeInst *II) : Base(II) {}
298   CallSite(Instruction *II) : Base(II) {}
299
300   bool operator==(const CallSite &CS) const { return I == CS.I; }
301   bool operator!=(const CallSite &CS) const { return I != CS.I; }
302
303   /// CallSite::get - This static method is sort of like a constructor.  It will
304   /// create an appropriate call site for a Call or Invoke instruction, but it
305   /// can also create a null initialized CallSite object for something which is
306   /// NOT a call site.
307   ///
308   static CallSite get(Value *V) {
309     return Base::get(V);
310   }
311
312   bool operator<(const CallSite &CS) const {
313     return getInstruction() < CS.getInstruction();
314   }
315
316 private:
317   User::op_iterator getCallee() const;
318 };
319
320 } // End llvm namespace
321
322 #endif