544c7d7761b1489f9034c111b55d310464458d0f
[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/Instruction.h"
30
31 namespace llvm {
32
33 class CallInst;
34 class InvokeInst;
35
36 class CallSite {
37   PointerIntPair<Instruction*, 1, bool> I;
38 public:
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);
43   CallSite(const CallSite &CS) : I(CS.I) {}
44   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
45
46   bool operator==(const CallSite &CS) const { return getInstruction()
47                                                 == CS.getInstruction(); }
48   bool operator!=(const CallSite &CS) const { return getInstruction()
49                                                 != CS.getInstruction(); }
50   
51   /// CallSite::get - This static method is sort of like a constructor.  It will
52   /// create an appropriate call site for a Call or Invoke instruction, but it
53   /// can also create a null initialized CallSite object for something which is
54   /// NOT a call site.
55   ///
56   static CallSite get(Value *V) {
57     if (Instruction *I = dyn_cast<Instruction>(V)) {
58       if (I->getOpcode() == Instruction::Call)
59         return CallSite(reinterpret_cast<CallInst*>(I));
60       else if (I->getOpcode() == Instruction::Invoke)
61         return CallSite(reinterpret_cast<InvokeInst*>(I));
62     }
63     return CallSite();
64   }
65
66   /// getCallingConv/setCallingConv - get or set the calling convention of the
67   /// call.
68   unsigned getCallingConv() const;
69   void setCallingConv(unsigned CC);
70
71   /// getAttributes/setAttributes - get or set the parameter attributes of
72   /// the call.
73   const AttrListPtr &getAttributes() const;
74   void setAttributes(const AttrListPtr &PAL);
75
76   /// paramHasAttr - whether the call or the callee has the given attribute.
77   bool paramHasAttr(uint16_t i, Attributes attr) const;
78
79   /// @brief Extract the alignment for a call or parameter (0=unknown).
80   uint16_t getParamAlignment(uint16_t i) const;
81
82   /// @brief Determine if the call does not access memory.
83   bool doesNotAccessMemory() const;
84   void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
85
86   /// @brief Determine if the call does not access or only reads memory.
87   bool onlyReadsMemory() const;
88   void setOnlyReadsMemory(bool onlyReadsMemory = true);
89
90   /// @brief Determine if the call cannot return.
91   bool doesNotReturn() const;
92   void setDoesNotReturn(bool doesNotReturn = true);
93
94   /// @brief Determine if the call cannot unwind.
95   bool doesNotThrow() const;
96   void setDoesNotThrow(bool doesNotThrow = true);
97
98   /// getType - Return the type of the instruction that generated this call site
99   ///
100   const Type *getType() const { return getInstruction()->getType(); }
101
102   /// isCall - true if a CallInst is enclosed.
103   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
104   /// it also could signify a NULL Instruction pointer.
105   bool isCall() const { return I.getInt(); }
106
107   /// isInvoke - true if a InvokeInst is enclosed.
108   ///
109   bool isInvoke() const { return getInstruction() && !I.getInt(); }
110
111   /// getInstruction - Return the instruction this call site corresponds to
112   ///
113   Instruction *getInstruction() const { return I.getPointer(); }
114
115   /// getCaller - Return the caller function for this call site
116   ///
117   Function *getCaller() const { return getInstruction()
118                                   ->getParent()->getParent(); }
119
120   /// getCalledValue - Return the pointer to function that is being called...
121   ///
122   Value *getCalledValue() const {
123     assert(getInstruction() && "Not a call or invoke instruction!");
124     return getInstruction()->getOperand(0);
125   }
126
127   /// getCalledFunction - Return the function being called if this is a direct
128   /// call, otherwise return null (if it's an indirect call).
129   ///
130   Function *getCalledFunction() const {
131     return dyn_cast<Function>(getCalledValue());
132   }
133
134   /// setCalledFunction - Set the callee to the specified value...
135   ///
136   void setCalledFunction(Value *V) {
137     assert(getInstruction() && "Not a call or invoke instruction!");
138     getInstruction()->setOperand(0, V);
139   }
140
141   Value *getArgument(unsigned ArgNo) const {
142     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
143     return *(arg_begin()+ArgNo);
144   }
145
146   void setArgument(unsigned ArgNo, Value* newVal) {
147     assert(getInstruction() && "Not a call or invoke instruction!");
148     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
149     getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
150   }
151
152   /// Given an operand number, returns the argument that corresponds to it.
153   /// OperandNo must be a valid operand number that actually corresponds to an
154   /// argument.
155   unsigned getArgumentNo(unsigned OperandNo) const {
156     assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
157                                                "a valid argument");
158     return OperandNo - getArgumentOffset();
159   }
160
161   /// hasArgument - Returns true if this CallSite passes the given Value* as an
162   /// argument to the called function.
163   bool hasArgument(const Value *Arg) const;
164
165   /// arg_iterator - The type of iterator to use when looping over actual
166   /// arguments at this call site...
167   typedef User::op_iterator arg_iterator;
168
169   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
170   /// list for a call site.
171   arg_iterator arg_begin() const {
172     assert(getInstruction() && "Not a call or invoke instruction!");
173                 // Skip non-arguments
174     return getInstruction()->op_begin() + getArgumentOffset();
175   }
176
177   arg_iterator arg_end() const { return getInstruction()->op_end(); }
178   bool arg_empty() const { return arg_end() == arg_begin(); }
179   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
180
181   bool operator<(const CallSite &CS) const {
182     return getInstruction() < CS.getInstruction();
183   }
184
185 private:
186   /// Returns the operand number of the first argument
187   unsigned getArgumentOffset() const {
188     if (isCall())
189       return 1; // Skip Function
190     else
191       return 3; // Skip Function, BB, BB
192   }
193 };
194
195 } // End llvm namespace
196
197 #endif