ba75e4ca8de27724b80020d5ce592325b7a25651
[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 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     DILocalVariable *getVariable() const {
86       return cast<DILocalVariable>(getRawVariable());
87     }
88     DIExpression *getExpression() const {
89       return cast<DIExpression>(getRawExpression());
90     }
91
92     Metadata *getRawVariable() const {
93       return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
94     }
95     Metadata *getRawExpression() const {
96       return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
97     }
98
99     // Methods for support type inquiry through isa, cast, and dyn_cast:
100     static inline bool classof(const IntrinsicInst *I) {
101       return I->getIntrinsicID() == Intrinsic::dbg_declare;
102     }
103     static inline bool classof(const Value *V) {
104       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
105     }
106   };
107
108   /// DbgValueInst - This represents the llvm.dbg.value instruction.
109   ///
110   class DbgValueInst : public DbgInfoIntrinsic {
111   public:
112     const Value *getValue() const;
113     Value *getValue();
114     uint64_t getOffset() const {
115       return cast<ConstantInt>(
116                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
117     }
118     DILocalVariable *getVariable() const {
119       return cast<DILocalVariable>(getRawVariable());
120     }
121     DIExpression *getExpression() const {
122       return cast<DIExpression>(getRawExpression());
123     }
124
125     Metadata *getRawVariable() const {
126       return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
127     }
128     Metadata *getRawExpression() const {
129       return cast<MetadataAsValue>(getArgOperand(3))->getMetadata();
130     }
131
132     // Methods for support type inquiry through isa, cast, and dyn_cast:
133     static inline bool classof(const IntrinsicInst *I) {
134       return I->getIntrinsicID() == Intrinsic::dbg_value;
135     }
136     static inline bool classof(const Value *V) {
137       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
138     }
139   };
140
141   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
142   ///
143   class MemIntrinsic : public IntrinsicInst {
144   public:
145     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
146     const Use &getRawDestUse() const { return getArgOperandUse(0); }
147     Use &getRawDestUse() { return getArgOperandUse(0); }
148
149     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
150     const Use &getLengthUse() const { return getArgOperandUse(2); }
151     Use &getLengthUse() { return getArgOperandUse(2); }
152
153     unsigned getDestAlignment() const {
154       // Note, param attributes start at 1, so offset dest index from 0 to 1.
155       return getParamAlignment(1);
156     }
157
158     ConstantInt *getVolatileCst() const {
159       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
160     }
161     bool isVolatile() const {
162       return !getVolatileCst()->isZero();
163     }
164
165     unsigned getDestAddressSpace() const {
166       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
167     }
168
169     /// getDest - This is just like getRawDest, but it strips off any cast
170     /// instructions that feed it, giving the original input.  The returned
171     /// value is guaranteed to be a pointer.
172     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
173
174     /// set* - Set the specified arguments of the instruction.
175     ///
176     void setDest(Value *Ptr) {
177       assert(getRawDest()->getType() == Ptr->getType() &&
178              "setDest called with pointer of wrong type!");
179       setArgOperand(0, Ptr);
180     }
181
182     void setLength(Value *L) {
183       assert(getLength()->getType() == L->getType() &&
184              "setLength called with value of wrong type!");
185       setArgOperand(2, L);
186     }
187
188     void setDestAlignment(unsigned Align) {
189       // Note, param attributes start at 1, so offset dest index from 0 to 1.
190       setParamAlignment(1, Align);
191     }
192
193     void setVolatile(Constant* V) {
194       setArgOperand(3, V);
195     }
196
197     // Methods for support type inquiry through isa, cast, and dyn_cast:
198     static inline bool classof(const IntrinsicInst *I) {
199       switch (I->getIntrinsicID()) {
200       case Intrinsic::memcpy:
201       case Intrinsic::memmove:
202       case Intrinsic::memset:
203         return true;
204       default: return false;
205       }
206     }
207     static inline bool classof(const Value *V) {
208       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
209     }
210   };
211
212   /// MemSetInst - This class wraps the llvm.memset intrinsic.
213   ///
214   class MemSetInst : public MemIntrinsic {
215   public:
216     /// get* - Return the arguments to the instruction.
217     ///
218     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
219     const Use &getValueUse() const { return getArgOperandUse(1); }
220     Use &getValueUse() { return getArgOperandUse(1); }
221
222     void setValue(Value *Val) {
223       assert(getValue()->getType() == Val->getType() &&
224              "setValue called with value of wrong type!");
225       setArgOperand(1, Val);
226     }
227
228     // Methods for support type inquiry through isa, cast, and dyn_cast:
229     static inline bool classof(const IntrinsicInst *I) {
230       return I->getIntrinsicID() == Intrinsic::memset;
231     }
232     static inline bool classof(const Value *V) {
233       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
234     }
235   };
236
237   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
238   ///
239   class MemTransferInst : public MemIntrinsic {
240   public:
241     /// get* - Return the arguments to the instruction.
242     ///
243     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
244     const Use &getRawSourceUse() const { return getArgOperandUse(1); }
245     Use &getRawSourceUse() { return getArgOperandUse(1); }
246
247     /// getSource - This is just like getRawSource, but it strips off any cast
248     /// instructions that feed it, giving the original input.  The returned
249     /// value is guaranteed to be a pointer.
250     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
251
252     unsigned getSourceAddressSpace() const {
253       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
254     }
255
256     unsigned getSrcAlignment() const {
257       // Note, param attributes start at 1, so offset src index from 1 to 2.
258       return getParamAlignment(2);
259     }
260
261     void setSource(Value *Ptr) {
262       assert(getRawSource()->getType() == Ptr->getType() &&
263              "setSource called with pointer of wrong type!");
264       setArgOperand(1, Ptr);
265     }
266
267     void setSrcAlignment(unsigned Align) {
268       // Note, param attributes start at 1, so offset src index from 1 to 2.
269       setParamAlignment(2, Align);
270     }
271
272     // Methods for support type inquiry through isa, cast, and dyn_cast:
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   class MemCpyInst : public MemTransferInst {
286   public:
287     // Methods for support type inquiry through isa, cast, and dyn_cast:
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   class MemMoveInst : public MemTransferInst {
299   public:
300     // Methods for support type inquiry through isa, cast, and dyn_cast:
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   /// VAStartInst - This represents the llvm.va_start intrinsic.
310   ///
311   class VAStartInst : public IntrinsicInst {
312   public:
313     static inline bool classof(const IntrinsicInst *I) {
314       return I->getIntrinsicID() == Intrinsic::vastart;
315     }
316     static inline bool classof(const Value *V) {
317       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
318     }
319
320     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
321   };
322
323   /// VAEndInst - This represents the llvm.va_end intrinsic.
324   ///
325   class VAEndInst : public IntrinsicInst {
326   public:
327     static inline bool classof(const IntrinsicInst *I) {
328       return I->getIntrinsicID() == Intrinsic::vaend;
329     }
330     static inline bool classof(const Value *V) {
331       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
332     }
333
334     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
335   };
336
337   /// VACopyInst - This represents the llvm.va_copy intrinsic.
338   ///
339   class VACopyInst : public IntrinsicInst {
340   public:
341     static inline bool classof(const IntrinsicInst *I) {
342       return I->getIntrinsicID() == Intrinsic::vacopy;
343     }
344     static inline bool classof(const Value *V) {
345       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
346     }
347
348     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
349     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
350   };
351
352   /// This represents the llvm.instrprof_increment intrinsic.
353   class InstrProfIncrementInst : public IntrinsicInst {
354   public:
355     static inline bool classof(const IntrinsicInst *I) {
356       return I->getIntrinsicID() == Intrinsic::instrprof_increment;
357     }
358     static inline bool classof(const Value *V) {
359       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
360     }
361
362     GlobalVariable *getName() const {
363       return cast<GlobalVariable>(
364           const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
365     }
366
367     ConstantInt *getHash() const {
368       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
369     }
370
371     ConstantInt *getNumCounters() const {
372       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
373     }
374
375     ConstantInt *getIndex() const {
376       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
377     }
378   };
379
380   /// This represents the llvm.instrprof_value_profile intrinsic.
381   class InstrProfValueProfileInst : public IntrinsicInst {
382   public:
383     static inline bool classof(const IntrinsicInst *I) {
384       return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
385     }
386     static inline bool classof(const Value *V) {
387       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
388     }
389
390     GlobalVariable *getName() const {
391       return cast<GlobalVariable>(
392           const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
393     }
394
395     ConstantInt *getHash() const {
396       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
397     }
398
399     Value *getTargetValue() const {
400       return cast<Value>(const_cast<Value *>(getArgOperand(2)));
401     }
402
403     ConstantInt *getValueKind() const {
404       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
405     }
406
407     // Returns the value site index.
408     ConstantInt *getIndex() const {
409       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
410     }
411   };
412 } // namespace llvm
413
414 #endif