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