480535fa9e767ae9bc8e85ac5ea2b887d5851468
[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.
12 //
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.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SUPPORT_CALLSITE_H
24 #define LLVM_SUPPORT_CALLSITE_H
25
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"
31
32 namespace llvm {
33
34 class CallInst;
35 class InvokeInst;
36
37 class CallSite {
38   PointerIntPair<Instruction*, 1, bool> I;
39 public:
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);
44
45   bool operator==(const CallSite &CS) const { return I == CS.I; }
46   bool operator!=(const CallSite &CS) const { return I != CS.I; }
47
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
51   /// NOT a call site.
52   ///
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));
59     }
60     return CallSite();
61   }
62
63   /// getCallingConv/setCallingConv - get or set the calling convention of the
64   /// call.
65   CallingConv::ID getCallingConv() const;
66   void setCallingConv(CallingConv::ID CC);
67
68   /// getAttributes/setAttributes - get or set the parameter attributes of
69   /// the call.
70   const AttrListPtr &getAttributes() const;
71   void setAttributes(const AttrListPtr &PAL);
72
73   /// paramHasAttr - whether the call or the callee has the given attribute.
74   bool paramHasAttr(uint16_t i, Attributes attr) const;
75
76   /// @brief Extract the alignment for a call or parameter (0=unknown).
77   uint16_t getParamAlignment(uint16_t i) const;
78
79   /// @brief Return true if the call should not be inlined.
80   bool isNoInline() const;
81   void setIsNoInline(bool Value = true);
82   
83   /// @brief Determine if the call does not access memory.
84   bool doesNotAccessMemory() const;
85   void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
86
87   /// @brief Determine if the call does not access or only reads memory.
88   bool onlyReadsMemory() const;
89   void setOnlyReadsMemory(bool onlyReadsMemory = true);
90
91   /// @brief Determine if the call cannot return.
92   bool doesNotReturn() const;
93   void setDoesNotReturn(bool doesNotReturn = true);
94
95   /// @brief Determine if the call cannot unwind.
96   bool doesNotThrow() const;
97   void setDoesNotThrow(bool doesNotThrow = true);
98
99   /// getType - Return the type of the instruction that generated this call site
100   ///
101   const Type *getType() const { return getInstruction()->getType(); }
102
103   /// isCall - true if a CallInst is enclosed.
104   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
105   /// it also could signify a NULL Instruction pointer.
106   bool isCall() const { return I.getInt(); }
107
108   /// isInvoke - true if a InvokeInst is enclosed.
109   ///
110   bool isInvoke() const { return getInstruction() && !I.getInt(); }
111
112   /// getInstruction - Return the instruction this call site corresponds to
113   ///
114   Instruction *getInstruction() const { return I.getPointer(); }
115
116   /// getCaller - Return the caller function for this call site
117   ///
118   Function *getCaller() const { return getInstruction()
119                                   ->getParent()->getParent(); }
120
121   /// getCalledValue - Return the pointer to function that is being called...
122   ///
123   Value *getCalledValue() const {
124     assert(getInstruction() && "Not a call or invoke instruction!");
125     return *getCallee();
126   }
127
128   /// getCalledFunction - Return the function being called if this is a direct
129   /// call, otherwise return null (if it's an indirect call).
130   ///
131   Function *getCalledFunction() const {
132     return dyn_cast<Function>(getCalledValue());
133   }
134
135   /// setCalledFunction - Set the callee to the specified value...
136   ///
137   void setCalledFunction(Value *V) {
138     assert(getInstruction() && "Not a call or invoke instruction!");
139     *getCallee() = V;
140   }
141
142   Value *getArgument(unsigned ArgNo) const {
143     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
144     return *(arg_begin()+ArgNo);
145   }
146
147   void setArgument(unsigned ArgNo, Value* newVal) {
148     assert(getInstruction() && "Not a call or invoke instruction!");
149     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
150     getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
151   }
152
153   /// Given a value use iterator, returns the argument that corresponds to it.
154   /// Iterator must actually correspond to an argument.
155   unsigned getArgumentNo(Value::use_iterator I) const {
156     assert(getInstruction() && "Not a call or invoke instruction!");
157     assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
158            && "Argument # out of range!");
159
160     return &I.getUse() - arg_begin();
161   }
162
163   /// Given an operand number, returns the argument that corresponds to it.
164   /// OperandNo must be a valid operand number that actually corresponds to an
165   /// argument.
166   unsigned getArgumentNo(unsigned OperandNo) const {
167     assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
168                                                "a valid argument");
169     return OperandNo - getArgumentOffset();
170   }
171
172   /// hasArgument - Returns true if this CallSite passes the given Value* as an
173   /// argument to the called function.
174   bool hasArgument(const Value *Arg) const;
175
176   /// arg_iterator - The type of iterator to use when looping over actual
177   /// arguments at this call site...
178   typedef User::op_iterator arg_iterator;
179
180   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
181   /// list for a call site.
182   arg_iterator arg_begin() const {
183     assert(getInstruction() && "Not a call or invoke instruction!");
184     // Skip non-arguments
185     return getInstruction()->op_begin() + getArgumentOffset();
186   }
187
188   arg_iterator arg_end() const { return getInstruction()->op_end() - getArgumentEndOffset(); }
189   bool arg_empty() const { return arg_end() == arg_begin(); }
190   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
191
192   bool operator<(const CallSite &CS) const {
193     return getInstruction() < CS.getInstruction();
194   }
195
196   bool isCallee(Value::use_iterator UI) const {
197     return getCallee() == &UI.getUse();
198   }
199   bool isCallee(Value::const_use_iterator UI) const {
200     return getCallee() == &UI.getUse();
201   }
202 private:
203   /// Returns the operand number of the first argument
204   unsigned getArgumentOffset() const {
205     if (isCall())
206       return 1; // Skip Function
207     else
208       return 0; // Args are at the front
209   }
210
211   unsigned getArgumentEndOffset() const {
212     if (isCall())
213       return 0; // Unchanged
214     else
215       return 3; // Skip BB, BB, Function
216   }
217
218   User::op_iterator getCallee() const;
219 };
220
221 } // End llvm namespace
222
223 #endif