f40e8cc76a54ebccd5fe380803852d1f8d4ab4f8
[oota-llvm.git] / include / llvm / IntrinsicInst.h
1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- 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 classes that make it really easy to deal with intrinsic
11 // functions with the isa/dyncast family of functions.  In particular, this
12 // allows you to do things like:
13 //
14 //     if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
15 //        ... MCI->getDest() ... MCI->getSource() ...
16 //
17 // All intrinsic function calls are instances of the call instruction, so these
18 // are all subclasses of the CallInst class.  Note that none of these classes
19 // has state or virtual methods, which is an important part of this gross/neat
20 // hack working.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #ifndef LLVM_INTRINSICINST_H
25 #define LLVM_INTRINSICINST_H
26
27 #include "llvm/Constants.h"
28 #include "llvm/Metadata.h"
29 #include "llvm/Function.h"
30 #include "llvm/Instructions.h"
31 #include "llvm/Intrinsics.h"
32
33 namespace llvm {
34   /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
35   /// functions.  This allows the standard isa/dyncast/cast functionality to
36   /// work with calls to intrinsic functions.
37   class IntrinsicInst : public CallInst {
38     IntrinsicInst();                      // DO NOT IMPLEMENT
39     IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
40     void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
41   public:
42     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
43     ///
44     Intrinsic::ID getIntrinsicID() const {
45       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
46     }
47     
48     // Methods for support type inquiry through isa, cast, and dyn_cast:
49     static inline bool classof(const IntrinsicInst *) { return true; }
50     static inline bool classof(const CallInst *I) {
51       if (const Function *CF = I->getCalledFunction())
52         return CF->getIntrinsicID() != 0;
53       return false;
54     }
55     static inline bool classof(const Value *V) {
56       return isa<CallInst>(V) && classof(cast<CallInst>(V));
57     }
58   };
59
60   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
61   ///
62   class DbgInfoIntrinsic : public IntrinsicInst {
63   public:
64
65     // Methods for support type inquiry through isa, cast, and dyn_cast:
66     static inline bool classof(const DbgInfoIntrinsic *) { return true; }
67     static inline bool classof(const IntrinsicInst *I) {
68       switch (I->getIntrinsicID()) {
69       case Intrinsic::dbg_declare:
70       case Intrinsic::dbg_value:
71         return true;
72       default: return false;
73       }
74     }
75     static inline bool classof(const Value *V) {
76       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
77     }
78     
79     static Value *StripCast(Value *C);
80   };
81
82   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
83   ///
84   class DbgDeclareInst : public DbgInfoIntrinsic {
85   public:
86     Value *getAddress() const {
87       if (MDNode* MD = dyn_cast<MDNode>(getOperand(1)))
88         return MD->getOperand(0);
89       else
90         return NULL;
91     }
92     MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
93
94     // Methods for support type inquiry through isa, cast, and dyn_cast:
95     static inline bool classof(const DbgDeclareInst *) { return true; }
96     static inline bool classof(const IntrinsicInst *I) {
97       return I->getIntrinsicID() == Intrinsic::dbg_declare;
98     }
99     static inline bool classof(const Value *V) {
100       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
101     }
102   };
103
104   /// DbgValueInst - This represents the llvm.dbg.value instruction.
105   ///
106   class DbgValueInst : public DbgInfoIntrinsic {
107   public:
108     const Value *getValue() const;
109     Value *getValue();
110     uint64_t getOffset() const {
111       return cast<ConstantInt>(
112                              const_cast<Value*>(getOperand(2)))->getZExtValue();
113     }
114     const MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
115     MDNode *getVariable() { return cast<MDNode>(getOperand(3)); }
116
117     // Methods for support type inquiry through isa, cast, and dyn_cast:
118     static inline bool classof(const DbgValueInst *) { return true; }
119     static inline bool classof(const IntrinsicInst *I) {
120       return I->getIntrinsicID() == Intrinsic::dbg_value;
121     }
122     static inline bool classof(const Value *V) {
123       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
124     }
125   };
126
127   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
128   ///
129   class MemIntrinsic : public IntrinsicInst {
130   public:
131     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
132
133     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
134     ConstantInt *getAlignmentCst() const {
135       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
136     }
137     
138     unsigned getAlignment() const {
139       return getAlignmentCst()->getZExtValue();
140     }
141
142     /// getDest - This is just like getRawDest, but it strips off any cast
143     /// instructions that feed it, giving the original input.  The returned
144     /// value is guaranteed to be a pointer.
145     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
146
147     /// set* - Set the specified arguments of the instruction.
148     ///
149     void setDest(Value *Ptr) {
150       assert(getRawDest()->getType() == Ptr->getType() &&
151              "setDest called with pointer of wrong type!");
152       setOperand(1, Ptr);
153     }
154
155     void setLength(Value *L) {
156       assert(getLength()->getType() == L->getType() &&
157              "setLength called with value of wrong type!");
158       setOperand(3, L);
159     }
160     
161     void setAlignment(Constant* A) {
162       setOperand(4, A);
163     }
164     
165     const Type *getAlignmentType() const {
166       return getOperand(4)->getType();
167     }
168     
169     // Methods for support type inquiry through isa, cast, and dyn_cast:
170     static inline bool classof(const MemIntrinsic *) { return true; }
171     static inline bool classof(const IntrinsicInst *I) {
172       switch (I->getIntrinsicID()) {
173       case Intrinsic::memcpy:
174       case Intrinsic::memmove:
175       case Intrinsic::memset:
176         return true;
177       default: return false;
178       }
179     }
180     static inline bool classof(const Value *V) {
181       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
182     }
183   };
184
185   /// MemSetInst - This class wraps the llvm.memset intrinsic.
186   ///
187   class MemSetInst : public MemIntrinsic {
188   public:
189     /// get* - Return the arguments to the instruction.
190     ///
191     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
192     
193     void setValue(Value *Val) {
194       assert(getValue()->getType() == Val->getType() &&
195              "setSource called with pointer of wrong type!");
196       setOperand(2, Val);
197     }
198     
199     // Methods for support type inquiry through isa, cast, and dyn_cast:
200     static inline bool classof(const MemSetInst *) { return true; }
201     static inline bool classof(const IntrinsicInst *I) {
202       return I->getIntrinsicID() == Intrinsic::memset;
203     }
204     static inline bool classof(const Value *V) {
205       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
206     }
207   };
208   
209   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
210   ///
211   class MemTransferInst : public MemIntrinsic {
212   public:
213     /// get* - Return the arguments to the instruction.
214     ///
215     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
216     
217     /// getSource - This is just like getRawSource, but it strips off any cast
218     /// instructions that feed it, giving the original input.  The returned
219     /// value is guaranteed to be a pointer.
220     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
221     
222     void setSource(Value *Ptr) {
223       assert(getRawSource()->getType() == Ptr->getType() &&
224              "setSource called with pointer of wrong type!");
225       setOperand(2, Ptr);
226     }
227     
228     // Methods for support type inquiry through isa, cast, and dyn_cast:
229     static inline bool classof(const MemTransferInst *) { return true; }
230     static inline bool classof(const IntrinsicInst *I) {
231       return I->getIntrinsicID() == Intrinsic::memcpy ||
232              I->getIntrinsicID() == Intrinsic::memmove;
233     }
234     static inline bool classof(const Value *V) {
235       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
236     }
237   };
238   
239   
240   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
241   ///
242   class MemCpyInst : public MemTransferInst {
243   public:
244     // Methods for support type inquiry through isa, cast, and dyn_cast:
245     static inline bool classof(const MemCpyInst *) { return true; }
246     static inline bool classof(const IntrinsicInst *I) {
247       return I->getIntrinsicID() == Intrinsic::memcpy;
248     }
249     static inline bool classof(const Value *V) {
250       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
251     }
252   };
253
254   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
255   ///
256   class MemMoveInst : public MemTransferInst {
257   public:
258     // Methods for support type inquiry through isa, cast, and dyn_cast:
259     static inline bool classof(const MemMoveInst *) { return true; }
260     static inline bool classof(const IntrinsicInst *I) {
261       return I->getIntrinsicID() == Intrinsic::memmove;
262     }
263     static inline bool classof(const Value *V) {
264       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
265     }
266   };
267
268   /// EHSelectorInst - This represents the llvm.eh.selector instruction.
269   ///
270   class EHSelectorInst : public IntrinsicInst {
271   public:
272     // Methods for support type inquiry through isa, cast, and dyn_cast:
273     static inline bool classof(const EHSelectorInst *) { return true; }
274     static inline bool classof(const IntrinsicInst *I) {
275       return I->getIntrinsicID() == Intrinsic::eh_selector;
276     }
277     static inline bool classof(const Value *V) {
278       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
279     }
280   };
281   
282   /// MemoryUseIntrinsic - This is the common base class for the memory use
283   /// marker intrinsics.
284   ///
285   class MemoryUseIntrinsic : public IntrinsicInst {
286   public:
287
288     // Methods for support type inquiry through isa, cast, and dyn_cast:
289     static inline bool classof(const MemoryUseIntrinsic *) { return true; }
290     static inline bool classof(const IntrinsicInst *I) {
291       switch (I->getIntrinsicID()) {
292       case Intrinsic::lifetime_start:
293       case Intrinsic::lifetime_end:
294       case Intrinsic::invariant_start:
295       case Intrinsic::invariant_end:
296         return true;
297       default: return false;
298       }
299     }
300     static inline bool classof(const Value *V) {
301       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
302     }
303   };
304
305 }
306
307 #endif