A little bit more LLVMContextification.
[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         return true;
73       default: return false;
74       }
75     }
76     static inline bool classof(const Value *V) {
77       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
78     }
79     
80     static Value *StripCast(Value *C);
81   };
82
83   /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
84   ///
85   struct DbgStopPointInst : public DbgInfoIntrinsic {
86     Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
87     Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
88     Value *getContext() const {
89       return StripCast(getOperand(3));
90     }
91
92     unsigned getLine() const {
93       return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
94     }
95     unsigned getColumn() const {
96       return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
97     }
98     
99     Value* getFileName() const;
100     Value* getDirectory() const;
101
102     // Methods for support type inquiry through isa, cast, and dyn_cast:
103     static inline bool classof(const DbgStopPointInst *) { return true; }
104     static inline bool classof(const IntrinsicInst *I) {
105       return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
106     }
107     static inline bool classof(const Value *V) {
108       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
109     }
110   };
111   
112   /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
113   ///
114   struct DbgFuncStartInst : public DbgInfoIntrinsic {
115     Value *getSubprogram() const { return StripCast(getOperand(1)); }
116
117     // Methods for support type inquiry through isa, cast, and dyn_cast:
118     static inline bool classof(const DbgFuncStartInst *) { return true; }
119     static inline bool classof(const IntrinsicInst *I) {
120       return I->getIntrinsicID() == Intrinsic::dbg_func_start;
121     }
122     static inline bool classof(const Value *V) {
123       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
124     }
125   };
126
127   /// DbgRegionStartInst - This represents the llvm.dbg.region.start
128   /// instruction.
129   struct DbgRegionStartInst : public DbgInfoIntrinsic {
130     Value *getContext() const { return StripCast(getOperand(1)); }
131
132     // Methods for support type inquiry through isa, cast, and dyn_cast:
133     static inline bool classof(const DbgRegionStartInst *) { return true; }
134     static inline bool classof(const IntrinsicInst *I) {
135       return I->getIntrinsicID() == Intrinsic::dbg_region_start;
136     }
137     static inline bool classof(const Value *V) {
138       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
139     }
140   };
141
142   /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
143   ///
144   struct DbgRegionEndInst : public DbgInfoIntrinsic {
145     Value *getContext() const { return StripCast(getOperand(1)); }
146
147     // Methods for support type inquiry through isa, cast, and dyn_cast:
148     static inline bool classof(const DbgRegionEndInst *) { return true; }
149     static inline bool classof(const IntrinsicInst *I) {
150       return I->getIntrinsicID() == Intrinsic::dbg_region_end;
151     }
152     static inline bool classof(const Value *V) {
153       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
154     }
155   };
156
157   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
158   ///
159   struct DbgDeclareInst : public DbgInfoIntrinsic {
160     Value *getAddress()  const { return getOperand(1); }
161     Value *getVariable() const { return StripCast(getOperand(2)); }
162
163     // Methods for support type inquiry through isa, cast, and dyn_cast:
164     static inline bool classof(const DbgDeclareInst *) { return true; }
165     static inline bool classof(const IntrinsicInst *I) {
166       return I->getIntrinsicID() == Intrinsic::dbg_declare;
167     }
168     static inline bool classof(const Value *V) {
169       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
170     }
171   };
172
173   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
174   ///
175   struct MemIntrinsic : public IntrinsicInst {
176     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
177
178     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
179     ConstantInt *getAlignmentCst() const {
180       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
181     }
182     
183     unsigned getAlignment() const {
184       return getAlignmentCst()->getZExtValue();
185     }
186
187     /// getDest - This is just like getRawDest, but it strips off any cast
188     /// instructions that feed it, giving the original input.  The returned
189     /// value is guaranteed to be a pointer.
190     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
191
192     /// set* - Set the specified arguments of the instruction.
193     ///
194     void setDest(Value *Ptr) {
195       assert(getRawDest()->getType() == Ptr->getType() &&
196              "setDest called with pointer of wrong type!");
197       setOperand(1, Ptr);
198     }
199
200     void setLength(Value *L) {
201       assert(getLength()->getType() == L->getType() &&
202              "setLength called with value of wrong type!");
203       setOperand(3, L);
204     }
205     
206     void setAlignment(Constant* A) {
207       setOperand(4, A);
208     }
209     
210     const Type *getAlignmentType() const {
211       return getOperand(4)->getType();
212     }
213     
214     // Methods for support type inquiry through isa, cast, and dyn_cast:
215     static inline bool classof(const MemIntrinsic *) { return true; }
216     static inline bool classof(const IntrinsicInst *I) {
217       switch (I->getIntrinsicID()) {
218       case Intrinsic::memcpy:
219       case Intrinsic::memmove:
220       case Intrinsic::memset:
221         return true;
222       default: return false;
223       }
224     }
225     static inline bool classof(const Value *V) {
226       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
227     }
228   };
229
230   /// MemSetInst - This class wraps the llvm.memset intrinsic.
231   ///
232   struct MemSetInst : public MemIntrinsic {
233     /// get* - Return the arguments to the instruction.
234     ///
235     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
236     
237     void setValue(Value *Val) {
238       assert(getValue()->getType() == Val->getType() &&
239              "setSource called with pointer of wrong type!");
240       setOperand(2, Val);
241     }
242     
243     // Methods for support type inquiry through isa, cast, and dyn_cast:
244     static inline bool classof(const MemSetInst *) { return true; }
245     static inline bool classof(const IntrinsicInst *I) {
246       return I->getIntrinsicID() == Intrinsic::memset;
247     }
248     static inline bool classof(const Value *V) {
249       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
250     }
251   };
252   
253   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
254   ///
255   struct MemTransferInst : public MemIntrinsic {
256     /// get* - Return the arguments to the instruction.
257     ///
258     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
259     
260     /// getSource - This is just like getRawSource, but it strips off any cast
261     /// instructions that feed it, giving the original input.  The returned
262     /// value is guaranteed to be a pointer.
263     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
264     
265     void setSource(Value *Ptr) {
266       assert(getRawSource()->getType() == Ptr->getType() &&
267              "setSource called with pointer of wrong type!");
268       setOperand(2, Ptr);
269     }
270     
271     // Methods for support type inquiry through isa, cast, and dyn_cast:
272     static inline bool classof(const MemTransferInst *) { return true; }
273     static inline bool classof(const IntrinsicInst *I) {
274       return I->getIntrinsicID() == Intrinsic::memcpy ||
275              I->getIntrinsicID() == Intrinsic::memmove;
276     }
277     static inline bool classof(const Value *V) {
278       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
279     }
280   };
281   
282   
283   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
284   ///
285   struct MemCpyInst : public MemTransferInst {
286     // Methods for support type inquiry through isa, cast, and dyn_cast:
287     static inline bool classof(const MemCpyInst *) { return true; }
288     static inline bool classof(const IntrinsicInst *I) {
289       return I->getIntrinsicID() == Intrinsic::memcpy;
290     }
291     static inline bool classof(const Value *V) {
292       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
293     }
294   };
295
296   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
297   ///
298   struct MemMoveInst : public MemTransferInst {
299     // Methods for support type inquiry through isa, cast, and dyn_cast:
300     static inline bool classof(const MemMoveInst *) { return true; }
301     static inline bool classof(const IntrinsicInst *I) {
302       return I->getIntrinsicID() == Intrinsic::memmove;
303     }
304     static inline bool classof(const Value *V) {
305       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
306     }
307   };
308
309   /// EHSelectorInst - This represents the llvm.eh.selector instruction.
310   ///
311   struct EHSelectorInst : public IntrinsicInst {
312     // Methods for support type inquiry through isa, cast, and dyn_cast:
313     static inline bool classof(const EHSelectorInst *) { return true; }
314     static inline bool classof(const IntrinsicInst *I) {
315       return I->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
316              I->getIntrinsicID() == Intrinsic::eh_selector_i64;
317     }
318     static inline bool classof(const Value *V) {
319       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
320     }
321   };
322   
323 }
324
325 #endif