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