Temporarily revert this, it's causing an issue with an internal project.
[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 Determine if the call does not access memory.
80   bool doesNotAccessMemory() const;
81   void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
82
83   /// @brief Determine if the call does not access or only reads memory.
84   bool onlyReadsMemory() const;
85   void setOnlyReadsMemory(bool onlyReadsMemory = true);
86
87   /// @brief Determine if the call cannot return.
88   bool doesNotReturn() const;
89   void setDoesNotReturn(bool doesNotReturn = true);
90
91   /// @brief Determine if the call cannot unwind.
92   bool doesNotThrow() const;
93   void setDoesNotThrow(bool doesNotThrow = true);
94
95   /// getType - Return the type of the instruction that generated this call site
96   ///
97   const Type *getType() const { return getInstruction()->getType(); }
98
99   /// isCall - true if a CallInst is enclosed.
100   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
101   /// it also could signify a NULL Instruction pointer.
102   bool isCall() const { return I.getInt(); }
103
104   /// isInvoke - true if a InvokeInst is enclosed.
105   ///
106   bool isInvoke() const { return getInstruction() && !I.getInt(); }
107
108   /// getInstruction - Return the instruction this call site corresponds to
109   ///
110   Instruction *getInstruction() const { return I.getPointer(); }
111
112   /// getCaller - Return the caller function for this call site
113   ///
114   Function *getCaller() const { return getInstruction()
115                                   ->getParent()->getParent(); }
116
117   /// getCalledValue - Return the pointer to function that is being called...
118   ///
119   Value *getCalledValue() const {
120     assert(getInstruction() && "Not a call or invoke instruction!");
121     return *getCallee();
122   }
123
124   /// getCalledFunction - Return the function being called if this is a direct
125   /// call, otherwise return null (if it's an indirect call).
126   ///
127   Function *getCalledFunction() const {
128     return dyn_cast<Function>(getCalledValue());
129   }
130
131   /// setCalledFunction - Set the callee to the specified value...
132   ///
133   void setCalledFunction(Value *V) {
134     assert(getInstruction() && "Not a call or invoke instruction!");
135     *getCallee() = V;
136   }
137
138   Value *getArgument(unsigned ArgNo) const {
139     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
140     return *(arg_begin()+ArgNo);
141   }
142
143   void setArgument(unsigned ArgNo, Value* newVal) {
144     assert(getInstruction() && "Not a call or invoke instruction!");
145     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
146     getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
147   }
148
149   /// Given a value use iterator, returns the argument that corresponds to it.
150   /// Iterator must actually correspond to an argument.
151   unsigned getArgumentNo(Value::use_iterator I) const {
152     assert(getInstruction() && "Not a call or invoke instruction!");
153     assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
154            && "Argument # out of range!");
155
156     return &I.getUse() - arg_begin();
157   }
158
159   /// Given an operand number, returns the argument that corresponds to it.
160   /// OperandNo must be a valid operand number that actually corresponds to an
161   /// argument.
162   unsigned getArgumentNo(unsigned OperandNo) const {
163     assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
164                                                "a valid argument");
165     return OperandNo - getArgumentOffset();
166   }
167
168   /// hasArgument - Returns true if this CallSite passes the given Value* as an
169   /// argument to the called function.
170   bool hasArgument(const Value *Arg) const;
171
172   /// arg_iterator - The type of iterator to use when looping over actual
173   /// arguments at this call site...
174   typedef User::op_iterator arg_iterator;
175
176   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
177   /// list for a call site.
178   arg_iterator arg_begin() const {
179     assert(getInstruction() && "Not a call or invoke instruction!");
180     // Skip non-arguments
181     return getInstruction()->op_begin() + getArgumentOffset();
182   }
183
184   arg_iterator arg_end() const { return getInstruction()->op_end() - getArgumentEndOffset(); }
185   bool arg_empty() const { return arg_end() == arg_begin(); }
186   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
187
188   bool operator<(const CallSite &CS) const {
189     return getInstruction() < CS.getInstruction();
190   }
191
192   bool isCallee(Value::use_iterator UI) const {
193     return getCallee() == &UI.getUse();
194   }
195   bool isCallee(Value::use_const_iterator UI) const {
196     return getCallee() == &UI.getUse();
197   }
198 private:
199   /// Returns the operand number of the first argument
200   unsigned getArgumentOffset() const {
201     if (isCall())
202       return 1; // Skip Function
203     else
204       return 0; // Args are at the front
205   }
206
207   unsigned getArgumentEndOffset() const {
208     if (isCall())
209       return 0; // Unchanged
210     else
211       return 3; // Skip BB, BB, Function
212   }
213
214   User::op_iterator getCallee() const;
215 };
216
217 } // End llvm namespace
218
219 #endif