Revert "System: Add SwapByteOrder and update Support/MathExtras.h to use it."
[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     assert(I.getPointer() && "Not a call?");
59   }
60 protected:
61   /// CallSiteBase::get - This static method is sort of like a constructor.  It
62   /// will create an appropriate call site for a Call or Invoke instruction, but
63   /// it can also create a null initialized CallSiteBase object for something
64   /// which is NOT a call site.
65   ///
66   static CallSiteBase get(ValTy *V) {
67     if (InstrTy *II = dyn_cast<InstrTy>(V)) {
68       if (II->getOpcode() == Instruction::Call)
69         return CallSiteBase(static_cast<CallTy*>(II));
70       else if (II->getOpcode() == Instruction::Invoke)
71         return CallSiteBase(static_cast<InvokeTy*>(II));
72     }
73     return CallSiteBase();
74   }
75 public:
76   /// isCall - true if a CallInst is enclosed.
77   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
78   /// it also could signify a NULL Instruction pointer.
79   bool isCall() const { return I.getInt(); }
80
81   /// isInvoke - true if a InvokeInst is enclosed.
82   ///
83   bool isInvoke() const { return getInstruction() && !I.getInt(); }
84
85   InstrTy *getInstruction() const { return I.getPointer(); }
86   InstrTy *operator->() const { return I.getPointer(); }
87   operator bool() const { return I.getPointer(); }
88
89   /// getCalledValue - Return the pointer to function that is being called...
90   ///
91   ValTy *getCalledValue() const {
92     assert(getInstruction() && "Not a call or invoke instruction!");
93     return *getCallee();
94   }
95
96   /// getCalledFunction - Return the function being called if this is a direct
97   /// call, otherwise return null (if it's an indirect call).
98   ///
99   FunTy *getCalledFunction() const {
100     return dyn_cast<FunTy>(getCalledValue());
101   }
102
103   /// setCalledFunction - Set the callee to the specified value...
104   ///
105   void setCalledFunction(Value *V) {
106     assert(getInstruction() && "Not a call or invoke instruction!");
107     *getCallee() = V;
108   }
109
110   /// isCallee - Determine whether the passed iterator points to the
111   /// callee operand's Use.
112   ///
113   bool isCallee(value_use_iterator<UserTy> UI) const {
114     return getCallee() == &UI.getUse();
115   }
116
117   ValTy *getArgument(unsigned ArgNo) const {
118     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
119     return *(arg_begin() + ArgNo);
120   }
121
122   void setArgument(unsigned ArgNo, Value* newVal) {
123     assert(getInstruction() && "Not a call or invoke instruction!");
124     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
125     getInstruction()->setOperand(ArgNo, newVal);
126   }
127
128   /// Given a value use iterator, returns the argument that corresponds to it.
129   /// Iterator must actually correspond to an argument.
130   unsigned getArgumentNo(value_use_iterator<UserTy> I) const {
131     assert(getInstruction() && "Not a call or invoke instruction!");
132     assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
133            && "Argument # out of range!");
134     return &I.getUse() - arg_begin();
135   }
136
137   /// arg_iterator - The type of iterator to use when looping over actual
138   /// arguments at this call site...
139   typedef IterTy arg_iterator;
140
141   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
142   /// list for a call site.
143   IterTy arg_begin() const {
144     assert(getInstruction() && "Not a call or invoke instruction!");
145     // Skip non-arguments
146     return (*this)->op_begin();
147   }
148
149   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
150   bool arg_empty() const { return arg_end() == arg_begin(); }
151   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
152   
153   /// getType - Return the type of the instruction that generated this call site
154   ///
155   const Type *getType() const { return (*this)->getType(); }
156
157   /// getCaller - Return the caller function for this call site
158   ///
159   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
160
161 #define CALLSITE_DELEGATE_GETTER(METHOD) \
162   InstrTy *II = getInstruction();    \
163   return isCall()                        \
164     ? cast<CallInst>(II)->METHOD         \
165     : cast<InvokeInst>(II)->METHOD
166
167 #define CALLSITE_DELEGATE_SETTER(METHOD) \
168   InstrTy *II = getInstruction();    \
169   if (isCall())                          \
170     cast<CallInst>(II)->METHOD;          \
171   else                                   \
172     cast<InvokeInst>(II)->METHOD
173
174   /// getCallingConv/setCallingConv - get or set the calling convention of the
175   /// call.
176   CallingConv::ID getCallingConv() const {
177     CALLSITE_DELEGATE_GETTER(getCallingConv());
178   }
179   void setCallingConv(CallingConv::ID CC) {
180     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
181   }
182
183   /// getAttributes/setAttributes - get or set the parameter attributes of
184   /// the call.
185   const AttrListPtr &getAttributes() const {
186     CALLSITE_DELEGATE_GETTER(getAttributes());
187   }
188   void setAttributes(const AttrListPtr &PAL) {
189     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
190   }
191
192   /// paramHasAttr - whether the call or the callee has the given attribute.
193   bool paramHasAttr(uint16_t i, Attributes attr) const {
194     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
195   }
196
197   /// @brief Extract the alignment for a call or parameter (0=unknown).
198   uint16_t getParamAlignment(uint16_t i) const {
199     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
200   }
201
202   /// @brief Return true if the call should not be inlined.
203   bool isNoInline() const {
204     CALLSITE_DELEGATE_GETTER(isNoInline());
205   }
206   void setIsNoInline(bool Value = true) {
207     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
208   }
209
210   /// @brief Determine if the call does not access memory.
211   bool doesNotAccessMemory() const {
212     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
213   }
214   void setDoesNotAccessMemory(bool doesNotAccessMemory = true) {
215     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
216   }
217
218   /// @brief Determine if the call does not access or only reads memory.
219   bool onlyReadsMemory() const {
220     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
221   }
222   void setOnlyReadsMemory(bool onlyReadsMemory = true) {
223     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
224   }
225
226   /// @brief Determine if the call cannot return.
227   bool doesNotReturn() const {
228     CALLSITE_DELEGATE_GETTER(doesNotReturn());
229   }
230   void setDoesNotReturn(bool doesNotReturn = true) {
231     CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
232   }
233
234   /// @brief Determine if the call cannot unwind.
235   bool doesNotThrow() const {
236     CALLSITE_DELEGATE_GETTER(doesNotThrow());
237   }
238   void setDoesNotThrow(bool doesNotThrow = true) {
239     CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
240   }
241
242 #undef CALLSITE_DELEGATE_GETTER
243 #undef CALLSITE_DELEGATE_SETTER
244
245   /// hasArgument - Returns true if this CallSite passes the given Value* as an
246   /// argument to the called function.
247   bool hasArgument(const Value *Arg) const {
248     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
249          ++AI)
250       if (AI->get() == Arg)
251         return true;
252     return false;
253   }
254
255 private:
256   unsigned getArgumentEndOffset() const {
257     if (isCall())
258       return 1; // Skip Callee
259     else
260       return 3; // Skip BB, BB, Callee
261   }
262
263   IterTy getCallee() const {
264     if (isCall()) // Skip Callee
265       return cast<CallInst>(getInstruction())->op_end() - 1;
266     else // Skip BB, BB, Callee
267       return cast<InvokeInst>(getInstruction())->op_end() - 3;
268   }
269 };
270
271 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
272                                      CallInst, InvokeInst, User::op_iterator> {
273   typedef CallSiteBase<Function, Value, User, Instruction,
274                        CallInst, InvokeInst, User::op_iterator> Base;
275 public:
276   CallSite() {}
277   CallSite(Base B) : Base(B) {}
278   CallSite(Value* V) : Base(V) {}
279   CallSite(CallInst *CI) : Base(CI) {}
280   CallSite(InvokeInst *II) : Base(II) {}
281   CallSite(Instruction *II) : Base(II) {}
282
283   bool operator==(const CallSite &CS) const { return I == CS.I; }
284   bool operator!=(const CallSite &CS) const { return I != CS.I; }
285   bool operator<(const CallSite &CS) const {
286     return getInstruction() < CS.getInstruction();
287   }
288
289 private:
290   User::op_iterator getCallee() const;
291 };
292
293 /// ImmutableCallSite - establish a view to a call site for examination
294 class ImmutableCallSite : public CallSiteBase<> {
295   typedef CallSiteBase<> Base;
296 public:
297   ImmutableCallSite(const Value* V) : Base(V) {}
298   ImmutableCallSite(const CallInst *CI) : Base(CI) {}
299   ImmutableCallSite(const InvokeInst *II) : Base(II) {}
300   ImmutableCallSite(const Instruction *II) : Base(II) {}
301   ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
302 };
303
304 } // End llvm namespace
305
306 #endif