Restore dump() methods to Loop and MachineLoop.
[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 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_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     /// 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 IntrinsicInst *) { return true; }
49     static inline bool classof(const CallInst *I) {
50       if (const Function *CF = I->getCalledFunction())
51         return CF->getIntrinsicID() != 0;
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 DbgInfoIntrinsic *) { return true; }
66     static inline bool classof(const IntrinsicInst *I) {
67       switch (I->getIntrinsicID()) {
68       case Intrinsic::dbg_declare:
69       case Intrinsic::dbg_value:
70         return true;
71       default: return false;
72       }
73     }
74     static inline bool classof(const Value *V) {
75       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
76     }
77     
78     static Value *StripCast(Value *C);
79   };
80
81   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
82   ///
83   class DbgDeclareInst : public DbgInfoIntrinsic {
84   public:
85     Value *getAddress()  const { return getOperand(1); }
86     MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
87
88     // Methods for support type inquiry through isa, cast, and dyn_cast:
89     static inline bool classof(const DbgDeclareInst *) { return true; }
90     static inline bool classof(const IntrinsicInst *I) {
91       return I->getIntrinsicID() == Intrinsic::dbg_declare;
92     }
93     static inline bool classof(const Value *V) {
94       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
95     }
96   };
97
98   /// DbgValueInst - This represents the llvm.dbg.value instruction.
99   ///
100   class DbgValueInst : public DbgInfoIntrinsic {
101   public:
102     Value *getValue() const;
103     Value *getOffset() const { return getOperand(2); }
104     MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
105
106     // Methods for support type inquiry through isa, cast, and dyn_cast:
107     static inline bool classof(const DbgValueInst *) { return true; }
108     static inline bool classof(const IntrinsicInst *I) {
109       return I->getIntrinsicID() == Intrinsic::dbg_value;
110     }
111     static inline bool classof(const Value *V) {
112       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
113     }
114   };
115
116   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
117   ///
118   class MemIntrinsic : public IntrinsicInst {
119   public:
120     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
121
122     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
123     ConstantInt *getAlignmentCst() const {
124       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
125     }
126     
127     unsigned getAlignment() const {
128       return getAlignmentCst()->getZExtValue();
129     }
130
131     /// getDest - This is just like getRawDest, but it strips off any cast
132     /// instructions that feed it, giving the original input.  The returned
133     /// value is guaranteed to be a pointer.
134     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
135
136     /// set* - Set the specified arguments of the instruction.
137     ///
138     void setDest(Value *Ptr) {
139       assert(getRawDest()->getType() == Ptr->getType() &&
140              "setDest called with pointer of wrong type!");
141       setOperand(1, Ptr);
142     }
143
144     void setLength(Value *L) {
145       assert(getLength()->getType() == L->getType() &&
146              "setLength called with value of wrong type!");
147       setOperand(3, L);
148     }
149     
150     void setAlignment(Constant* A) {
151       setOperand(4, A);
152     }
153     
154     const Type *getAlignmentType() const {
155       return getOperand(4)->getType();
156     }
157     
158     // Methods for support type inquiry through isa, cast, and dyn_cast:
159     static inline bool classof(const MemIntrinsic *) { return true; }
160     static inline bool classof(const IntrinsicInst *I) {
161       switch (I->getIntrinsicID()) {
162       case Intrinsic::memcpy:
163       case Intrinsic::memmove:
164       case Intrinsic::memset:
165         return true;
166       default: return false;
167       }
168     }
169     static inline bool classof(const Value *V) {
170       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
171     }
172   };
173
174   /// MemSetInst - This class wraps the llvm.memset intrinsic.
175   ///
176   class MemSetInst : public MemIntrinsic {
177   public:
178     /// get* - Return the arguments to the instruction.
179     ///
180     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
181     
182     void setValue(Value *Val) {
183       assert(getValue()->getType() == Val->getType() &&
184              "setSource called with pointer of wrong type!");
185       setOperand(2, Val);
186     }
187     
188     // Methods for support type inquiry through isa, cast, and dyn_cast:
189     static inline bool classof(const MemSetInst *) { return true; }
190     static inline bool classof(const IntrinsicInst *I) {
191       return I->getIntrinsicID() == Intrinsic::memset;
192     }
193     static inline bool classof(const Value *V) {
194       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
195     }
196   };
197   
198   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
199   ///
200   class MemTransferInst : public MemIntrinsic {
201   public:
202     /// get* - Return the arguments to the instruction.
203     ///
204     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
205     
206     /// getSource - This is just like getRawSource, but it strips off any cast
207     /// instructions that feed it, giving the original input.  The returned
208     /// value is guaranteed to be a pointer.
209     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
210     
211     void setSource(Value *Ptr) {
212       assert(getRawSource()->getType() == Ptr->getType() &&
213              "setSource called with pointer of wrong type!");
214       setOperand(2, Ptr);
215     }
216     
217     // Methods for support type inquiry through isa, cast, and dyn_cast:
218     static inline bool classof(const MemTransferInst *) { return true; }
219     static inline bool classof(const IntrinsicInst *I) {
220       return I->getIntrinsicID() == Intrinsic::memcpy ||
221              I->getIntrinsicID() == Intrinsic::memmove;
222     }
223     static inline bool classof(const Value *V) {
224       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
225     }
226   };
227   
228   
229   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
230   ///
231   class MemCpyInst : public MemTransferInst {
232   public:
233     // Methods for support type inquiry through isa, cast, and dyn_cast:
234     static inline bool classof(const MemCpyInst *) { return true; }
235     static inline bool classof(const IntrinsicInst *I) {
236       return I->getIntrinsicID() == Intrinsic::memcpy;
237     }
238     static inline bool classof(const Value *V) {
239       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
240     }
241   };
242
243   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
244   ///
245   class MemMoveInst : public MemTransferInst {
246   public:
247     // Methods for support type inquiry through isa, cast, and dyn_cast:
248     static inline bool classof(const MemMoveInst *) { return true; }
249     static inline bool classof(const IntrinsicInst *I) {
250       return I->getIntrinsicID() == Intrinsic::memmove;
251     }
252     static inline bool classof(const Value *V) {
253       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
254     }
255   };
256
257   /// EHSelectorInst - This represents the llvm.eh.selector instruction.
258   ///
259   class EHSelectorInst : public IntrinsicInst {
260   public:
261     // Methods for support type inquiry through isa, cast, and dyn_cast:
262     static inline bool classof(const EHSelectorInst *) { return true; }
263     static inline bool classof(const IntrinsicInst *I) {
264       return I->getIntrinsicID() == Intrinsic::eh_selector;
265     }
266     static inline bool classof(const Value *V) {
267       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
268     }
269   };
270   
271   /// MemoryUseIntrinsic - This is the common base class for the memory use
272   /// marker intrinsics.
273   ///
274   class MemoryUseIntrinsic : public IntrinsicInst {
275   public:
276
277     // Methods for support type inquiry through isa, cast, and dyn_cast:
278     static inline bool classof(const MemoryUseIntrinsic *) { return true; }
279     static inline bool classof(const IntrinsicInst *I) {
280       switch (I->getIntrinsicID()) {
281       case Intrinsic::lifetime_start:
282       case Intrinsic::lifetime_end:
283       case Intrinsic::invariant_start:
284       case Intrinsic::invariant_end:
285         return true;
286       default: return false;
287       }
288     }
289     static inline bool classof(const Value *V) {
290       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
291     }
292   };
293
294 }
295
296 #endif