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