e3d799984542f47d5b1e974a743d4b868113a580
[oota-llvm.git] / include / llvm / IR / 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_IR_INTRINSICINST_H
25 #define LLVM_IR_INTRINSICINST_H
26
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/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() LLVM_DELETED_FUNCTION;
38     IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
39     void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
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 CallInst *I) {
49       if (const Function *CF = I->getCalledFunction())
50         return CF->isIntrinsic();
51       return false;
52     }
53     static inline bool classof(const Value *V) {
54       return isa<CallInst>(V) && classof(cast<CallInst>(V));
55     }
56   };
57
58   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
59   ///
60   class DbgInfoIntrinsic : public IntrinsicInst {
61   public:
62
63     // Methods for support type inquiry through isa, cast, and dyn_cast:
64     static inline bool classof(const IntrinsicInst *I) {
65       switch (I->getIntrinsicID()) {
66       case Intrinsic::dbg_declare:
67       case Intrinsic::dbg_value:
68         return true;
69       default: return false;
70       }
71     }
72     static inline bool classof(const Value *V) {
73       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
74     }
75
76     static Value *StripCast(Value *C);
77   };
78
79   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
80   ///
81   class DbgDeclareInst : public DbgInfoIntrinsic {
82   public:
83     Value *getAddress() const;
84     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
85     MDNode *getExpression() const { return cast<MDNode>(getArgOperand(2)); }
86
87     // Methods for support type inquiry through isa, cast, and dyn_cast:
88     static inline bool classof(const IntrinsicInst *I) {
89       return I->getIntrinsicID() == Intrinsic::dbg_declare;
90     }
91     static inline bool classof(const Value *V) {
92       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
93     }
94   };
95
96   /// DbgValueInst - This represents the llvm.dbg.value instruction.
97   ///
98   class DbgValueInst : public DbgInfoIntrinsic {
99   public:
100     const Value *getValue() const;
101     Value *getValue();
102     uint64_t getOffset() const {
103       return cast<ConstantInt>(
104                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
105     }
106     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
107     MDNode *getExpression() const { return cast<MDNode>(getArgOperand(3)); }
108
109     // Methods for support type inquiry through isa, cast, and dyn_cast:
110     static inline bool classof(const IntrinsicInst *I) {
111       return I->getIntrinsicID() == Intrinsic::dbg_value;
112     }
113     static inline bool classof(const Value *V) {
114       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
115     }
116   };
117
118   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
119   ///
120   class MemIntrinsic : public IntrinsicInst {
121   public:
122     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
123     const Use &getRawDestUse() const { return getArgOperandUse(0); }
124     Use &getRawDestUse() { return getArgOperandUse(0); }
125
126     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
127     const Use &getLengthUse() const { return getArgOperandUse(2); }
128     Use &getLengthUse() { return getArgOperandUse(2); }
129
130     ConstantInt *getAlignmentCst() const {
131       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
132     }
133
134     unsigned getAlignment() const {
135       return getAlignmentCst()->getZExtValue();
136     }
137
138     ConstantInt *getVolatileCst() const {
139       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
140     }
141     bool isVolatile() const {
142       return !getVolatileCst()->isZero();
143     }
144
145     unsigned getDestAddressSpace() const {
146       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
147     }
148
149     /// getDest - This is just like getRawDest, but it strips off any cast
150     /// instructions that feed it, giving the original input.  The returned
151     /// value is guaranteed to be a pointer.
152     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
153
154     /// set* - Set the specified arguments of the instruction.
155     ///
156     void setDest(Value *Ptr) {
157       assert(getRawDest()->getType() == Ptr->getType() &&
158              "setDest called with pointer of wrong type!");
159       setArgOperand(0, Ptr);
160     }
161
162     void setLength(Value *L) {
163       assert(getLength()->getType() == L->getType() &&
164              "setLength called with value of wrong type!");
165       setArgOperand(2, L);
166     }
167
168     void setAlignment(Constant* A) {
169       setArgOperand(3, A);
170     }
171
172     void setVolatile(Constant* V) {
173       setArgOperand(4, V);
174     }
175
176     Type *getAlignmentType() const {
177       return getArgOperand(3)->getType();
178     }
179
180     // Methods for support type inquiry through isa, cast, and dyn_cast:
181     static inline bool classof(const IntrinsicInst *I) {
182       switch (I->getIntrinsicID()) {
183       case Intrinsic::memcpy:
184       case Intrinsic::memmove:
185       case Intrinsic::memset:
186         return true;
187       default: return false;
188       }
189     }
190     static inline bool classof(const Value *V) {
191       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
192     }
193   };
194
195   /// MemSetInst - This class wraps the llvm.memset intrinsic.
196   ///
197   class MemSetInst : public MemIntrinsic {
198   public:
199     /// get* - Return the arguments to the instruction.
200     ///
201     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
202     const Use &getValueUse() const { return getArgOperandUse(1); }
203     Use &getValueUse() { return getArgOperandUse(1); }
204
205     void setValue(Value *Val) {
206       assert(getValue()->getType() == Val->getType() &&
207              "setValue called with value of wrong type!");
208       setArgOperand(1, Val);
209     }
210
211     // Methods for support type inquiry through isa, cast, and dyn_cast:
212     static inline bool classof(const IntrinsicInst *I) {
213       return I->getIntrinsicID() == Intrinsic::memset;
214     }
215     static inline bool classof(const Value *V) {
216       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
217     }
218   };
219
220   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
221   ///
222   class MemTransferInst : public MemIntrinsic {
223   public:
224     /// get* - Return the arguments to the instruction.
225     ///
226     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
227     const Use &getRawSourceUse() const { return getArgOperandUse(1); }
228     Use &getRawSourceUse() { return getArgOperandUse(1); }
229
230     /// getSource - This is just like getRawSource, but it strips off any cast
231     /// instructions that feed it, giving the original input.  The returned
232     /// value is guaranteed to be a pointer.
233     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
234
235     unsigned getSourceAddressSpace() const {
236       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
237     }
238
239     void setSource(Value *Ptr) {
240       assert(getRawSource()->getType() == Ptr->getType() &&
241              "setSource called with pointer of wrong type!");
242       setArgOperand(1, Ptr);
243     }
244
245     // Methods for support type inquiry through isa, cast, and dyn_cast:
246     static inline bool classof(const IntrinsicInst *I) {
247       return I->getIntrinsicID() == Intrinsic::memcpy ||
248              I->getIntrinsicID() == Intrinsic::memmove;
249     }
250     static inline bool classof(const Value *V) {
251       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
252     }
253   };
254
255
256   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
257   ///
258   class MemCpyInst : public MemTransferInst {
259   public:
260     // Methods for support type inquiry through isa, cast, and dyn_cast:
261     static inline bool classof(const IntrinsicInst *I) {
262       return I->getIntrinsicID() == Intrinsic::memcpy;
263     }
264     static inline bool classof(const Value *V) {
265       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
266     }
267   };
268
269   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
270   ///
271   class MemMoveInst : public MemTransferInst {
272   public:
273     // Methods for support type inquiry through isa, cast, and dyn_cast:
274     static inline bool classof(const IntrinsicInst *I) {
275       return 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   /// VAStartInst - This represents the llvm.va_start intrinsic.
283   ///
284   class VAStartInst : public IntrinsicInst {
285   public:
286     static inline bool classof(const IntrinsicInst *I) {
287       return I->getIntrinsicID() == Intrinsic::vastart;
288     }
289     static inline bool classof(const Value *V) {
290       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
291     }
292
293     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
294   };
295
296   /// VAEndInst - This represents the llvm.va_end intrinsic.
297   ///
298   class VAEndInst : public IntrinsicInst {
299   public:
300     static inline bool classof(const IntrinsicInst *I) {
301       return I->getIntrinsicID() == Intrinsic::vaend;
302     }
303     static inline bool classof(const Value *V) {
304       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
305     }
306
307     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
308   };
309
310   /// VACopyInst - This represents the llvm.va_copy intrinsic.
311   ///
312   class VACopyInst : public IntrinsicInst {
313   public:
314     static inline bool classof(const IntrinsicInst *I) {
315       return I->getIntrinsicID() == Intrinsic::vacopy;
316     }
317     static inline bool classof(const Value *V) {
318       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
319     }
320
321     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
322     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
323   };
324
325 }
326
327 #endif