Remove #include of metadata.h from intrinsicinst.h. The only
[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/Function.h"
29 #include "llvm/Instructions.h"
30 #include "llvm/Intrinsics.h"
31
32 namespace llvm {
33   /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
34   /// functions.  This allows the standard isa/dyncast/cast functionality to
35   /// work with calls to intrinsic functions.
36   class IntrinsicInst : public CallInst {
37     IntrinsicInst();                      // DO NOT IMPLEMENT
38     IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
39     void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
40   public:
41     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
42     ///
43     Intrinsic::ID getIntrinsicID() const {
44       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
45     }
46     
47     // Methods for support type inquiry through isa, cast, and dyn_cast:
48     static inline bool classof(const IntrinsicInst *) { return true; }
49     static inline bool classof(const CallInst *I) {
50       if (const Function *CF = I->getCalledFunction())
51         return CF->getIntrinsicID() != 0;
52       return false;
53     }
54     static inline bool classof(const Value *V) {
55       return isa<CallInst>(V) && classof(cast<CallInst>(V));
56     }
57   };
58
59   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
60   ///
61   struct DbgInfoIntrinsic : public IntrinsicInst {
62
63     // Methods for support type inquiry through isa, cast, and dyn_cast:
64     static inline bool classof(const DbgInfoIntrinsic *) { return true; }
65     static inline bool classof(const IntrinsicInst *I) {
66       switch (I->getIntrinsicID()) {
67       case Intrinsic::dbg_stoppoint:
68       case Intrinsic::dbg_func_start:
69       case Intrinsic::dbg_region_start:
70       case Intrinsic::dbg_region_end:
71       case Intrinsic::dbg_declare:
72       case Intrinsic::dbg_value:
73         return true;
74       default: return false;
75       }
76     }
77     static inline bool classof(const Value *V) {
78       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
79     }
80     
81     static Value *StripCast(Value *C);
82   };
83
84   /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
85   ///
86   struct DbgStopPointInst : public DbgInfoIntrinsic {
87     Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
88     Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
89     MDNode *getContext() const {
90       return cast<MDNode>(getOperand(3));
91     }
92
93     unsigned getLine() const {
94       return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
95     }
96     unsigned getColumn() const {
97       return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
98     }
99     
100     Value *getFileName() const;
101     Value *getDirectory() const;
102
103     // Methods for support type inquiry through isa, cast, and dyn_cast:
104     static inline bool classof(const DbgStopPointInst *) { return true; }
105     static inline bool classof(const IntrinsicInst *I) {
106       return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
107     }
108     static inline bool classof(const Value *V) {
109       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
110     }
111   };
112   
113   /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
114   ///
115   struct DbgFuncStartInst : public DbgInfoIntrinsic {
116     MDNode *getSubprogram() const { return cast<MDNode>(getOperand(1)); }
117
118     // Methods for support type inquiry through isa, cast, and dyn_cast:
119     static inline bool classof(const DbgFuncStartInst *) { return true; }
120     static inline bool classof(const IntrinsicInst *I) {
121       return I->getIntrinsicID() == Intrinsic::dbg_func_start;
122     }
123     static inline bool classof(const Value *V) {
124       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
125     }
126   };
127
128   /// DbgRegionStartInst - This represents the llvm.dbg.region.start
129   /// instruction.
130   struct DbgRegionStartInst : public DbgInfoIntrinsic {
131     MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
132
133     // Methods for support type inquiry through isa, cast, and dyn_cast:
134     static inline bool classof(const DbgRegionStartInst *) { return true; }
135     static inline bool classof(const IntrinsicInst *I) {
136       return I->getIntrinsicID() == Intrinsic::dbg_region_start;
137     }
138     static inline bool classof(const Value *V) {
139       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
140     }
141   };
142
143   /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
144   ///
145   struct DbgRegionEndInst : public DbgInfoIntrinsic {
146     MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
147
148     // Methods for support type inquiry through isa, cast, and dyn_cast:
149     static inline bool classof(const DbgRegionEndInst *) { return true; }
150     static inline bool classof(const IntrinsicInst *I) {
151       return I->getIntrinsicID() == Intrinsic::dbg_region_end;
152     }
153     static inline bool classof(const Value *V) {
154       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
155     }
156   };
157
158   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
159   ///
160   struct DbgDeclareInst : public DbgInfoIntrinsic {
161     Value *getAddress()  const { return getOperand(1); }
162     MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
163
164     // Methods for support type inquiry through isa, cast, and dyn_cast:
165     static inline bool classof(const DbgDeclareInst *) { return true; }
166     static inline bool classof(const IntrinsicInst *I) {
167       return I->getIntrinsicID() == Intrinsic::dbg_declare;
168     }
169     static inline bool classof(const Value *V) {
170       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
171     }
172   };
173
174   /// DbgValueInst - This represents the llvm.dbg.value instruction.
175   ///
176   struct DbgValueInst : public DbgInfoIntrinsic {
177     Value *getValue() const;
178     Value *getOffset() const { return getOperand(2); }
179     MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
180
181     // Methods for support type inquiry through isa, cast, and dyn_cast:
182     static inline bool classof(const DbgValueInst *) { return true; }
183     static inline bool classof(const IntrinsicInst *I) {
184       return I->getIntrinsicID() == Intrinsic::dbg_value;
185     }
186     static inline bool classof(const Value *V) {
187       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
188     }
189   };
190
191   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
192   ///
193   struct MemIntrinsic : public IntrinsicInst {
194     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
195
196     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
197     ConstantInt *getAlignmentCst() const {
198       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
199     }
200     
201     unsigned getAlignment() const {
202       return getAlignmentCst()->getZExtValue();
203     }
204
205     /// getDest - This is just like getRawDest, but it strips off any cast
206     /// instructions that feed it, giving the original input.  The returned
207     /// value is guaranteed to be a pointer.
208     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
209
210     /// set* - Set the specified arguments of the instruction.
211     ///
212     void setDest(Value *Ptr) {
213       assert(getRawDest()->getType() == Ptr->getType() &&
214              "setDest called with pointer of wrong type!");
215       setOperand(1, Ptr);
216     }
217
218     void setLength(Value *L) {
219       assert(getLength()->getType() == L->getType() &&
220              "setLength called with value of wrong type!");
221       setOperand(3, L);
222     }
223     
224     void setAlignment(Constant* A) {
225       setOperand(4, A);
226     }
227     
228     const Type *getAlignmentType() const {
229       return getOperand(4)->getType();
230     }
231     
232     // Methods for support type inquiry through isa, cast, and dyn_cast:
233     static inline bool classof(const MemIntrinsic *) { return true; }
234     static inline bool classof(const IntrinsicInst *I) {
235       switch (I->getIntrinsicID()) {
236       case Intrinsic::memcpy:
237       case Intrinsic::memmove:
238       case Intrinsic::memset:
239         return true;
240       default: return false;
241       }
242     }
243     static inline bool classof(const Value *V) {
244       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
245     }
246   };
247
248   /// MemSetInst - This class wraps the llvm.memset intrinsic.
249   ///
250   struct MemSetInst : public MemIntrinsic {
251     /// get* - Return the arguments to the instruction.
252     ///
253     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
254     
255     void setValue(Value *Val) {
256       assert(getValue()->getType() == Val->getType() &&
257              "setSource called with pointer of wrong type!");
258       setOperand(2, Val);
259     }
260     
261     // Methods for support type inquiry through isa, cast, and dyn_cast:
262     static inline bool classof(const MemSetInst *) { return true; }
263     static inline bool classof(const IntrinsicInst *I) {
264       return I->getIntrinsicID() == Intrinsic::memset;
265     }
266     static inline bool classof(const Value *V) {
267       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
268     }
269   };
270   
271   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
272   ///
273   struct MemTransferInst : public MemIntrinsic {
274     /// get* - Return the arguments to the instruction.
275     ///
276     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
277     
278     /// getSource - This is just like getRawSource, but it strips off any cast
279     /// instructions that feed it, giving the original input.  The returned
280     /// value is guaranteed to be a pointer.
281     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
282     
283     void setSource(Value *Ptr) {
284       assert(getRawSource()->getType() == Ptr->getType() &&
285              "setSource called with pointer of wrong type!");
286       setOperand(2, Ptr);
287     }
288     
289     // Methods for support type inquiry through isa, cast, and dyn_cast:
290     static inline bool classof(const MemTransferInst *) { return true; }
291     static inline bool classof(const IntrinsicInst *I) {
292       return I->getIntrinsicID() == Intrinsic::memcpy ||
293              I->getIntrinsicID() == Intrinsic::memmove;
294     }
295     static inline bool classof(const Value *V) {
296       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
297     }
298   };
299   
300   
301   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
302   ///
303   struct MemCpyInst : public MemTransferInst {
304     // Methods for support type inquiry through isa, cast, and dyn_cast:
305     static inline bool classof(const MemCpyInst *) { return true; }
306     static inline bool classof(const IntrinsicInst *I) {
307       return I->getIntrinsicID() == Intrinsic::memcpy;
308     }
309     static inline bool classof(const Value *V) {
310       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
311     }
312   };
313
314   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
315   ///
316   struct MemMoveInst : public MemTransferInst {
317     // Methods for support type inquiry through isa, cast, and dyn_cast:
318     static inline bool classof(const MemMoveInst *) { return true; }
319     static inline bool classof(const IntrinsicInst *I) {
320       return I->getIntrinsicID() == Intrinsic::memmove;
321     }
322     static inline bool classof(const Value *V) {
323       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
324     }
325   };
326
327   /// EHSelectorInst - This represents the llvm.eh.selector instruction.
328   ///
329   struct EHSelectorInst : public IntrinsicInst {
330     // Methods for support type inquiry through isa, cast, and dyn_cast:
331     static inline bool classof(const EHSelectorInst *) { return true; }
332     static inline bool classof(const IntrinsicInst *I) {
333       return I->getIntrinsicID() == Intrinsic::eh_selector;
334     }
335     static inline bool classof(const Value *V) {
336       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
337     }
338   };
339   
340   /// MemoryUseIntrinsic - This is the common base class for the memory use
341   /// marker intrinsics.
342   ///
343   struct MemoryUseIntrinsic : public IntrinsicInst {
344
345     // Methods for support type inquiry through isa, cast, and dyn_cast:
346     static inline bool classof(const MemoryUseIntrinsic *) { return true; }
347     static inline bool classof(const IntrinsicInst *I) {
348       switch (I->getIntrinsicID()) {
349       case Intrinsic::lifetime_start:
350       case Intrinsic::lifetime_end:
351       case Intrinsic::invariant_start:
352       case Intrinsic::invariant_end:
353         return true;
354       default: return false;
355       }
356     }
357     static inline bool classof(const Value *V) {
358       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
359     }
360   };
361
362 }
363
364 #endif