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