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