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