Typo.
[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     Value *getChain() const { return const_cast<Value*>(getOperand(1)); }
70
71     // Methods for support type inquiry through isa, cast, and dyn_cast:
72     static inline bool classof(const DbgInfoIntrinsic *) { return true; }
73     static inline bool classof(const IntrinsicInst *I) {
74       switch (I->getIntrinsicID()) {
75       case Intrinsic::dbg_stoppoint:
76       case Intrinsic::dbg_region_start:
77       case Intrinsic::dbg_region_end:
78       case Intrinsic::dbg_func_start:
79         return true;
80       default: return false;
81       }
82     }
83     static inline bool classof(const Value *V) {
84       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
85     }
86   };
87
88
89   /// DbgStopPointInst - This represent llvm.dbg.stoppoint instructions.
90   ///
91   struct DbgStopPointInst : public DbgInfoIntrinsic {
92
93     unsigned getLineNo() const {
94       return unsigned(cast<ConstantInt>(getOperand(2))->getRawValue());
95     }
96     unsigned getColNo() const {
97       return unsigned(cast<ConstantInt>(getOperand(3))->getRawValue());
98     }
99     Value *getContext() const { return const_cast<Value*>(getOperand(4)); }
100
101
102     // Methods for support type inquiry through isa, cast, and dyn_cast:
103     static inline bool classof(const DbgStopPointInst *) { return true; }
104     static inline bool classof(const IntrinsicInst *I) {
105       return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
106     }
107     static inline bool classof(const Value *V) {
108       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
109     }
110   };
111
112   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
113   ///
114   struct MemIntrinsic : public IntrinsicInst {
115     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
116
117     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
118     ConstantInt *getAlignment() const {
119       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
120     }
121
122     /// getDest - This is just like getRawDest, but it strips off any cast
123     /// instructions that feed it, giving the original input.  The returned
124     /// value is guaranteed to be a pointer.
125     Value *getDest() const { return StripPointerCasts(getRawDest()); }
126
127     /// set* - Set the specified arguments of the instruction.
128     ///
129     void setDest(Value *Ptr) {
130       assert(getRawDest()->getType() == Ptr->getType() &&
131              "setDest called with pointer of wrong type!");
132       setOperand(1, Ptr);
133     }
134
135     void setLength(Value *L) {
136       assert(getLength()->getType() == L->getType() &&
137              "setLength called with value of wrong type!");
138       setOperand(3, L);
139     }
140     void setAlignment(ConstantInt *A) {
141       assert(getAlignment()->getType() == A->getType() &&
142              "setAlignment called with value of wrong type!");
143       setOperand(4, A);
144     }
145
146     // Methods for support type inquiry through isa, cast, and dyn_cast:
147     static inline bool classof(const MemIntrinsic *) { return true; }
148     static inline bool classof(const IntrinsicInst *I) {
149       switch (I->getIntrinsicID()) {
150       case Intrinsic::memcpy_i32:
151       case Intrinsic::memcpy_i64:
152       case Intrinsic::memmove_i32:
153       case Intrinsic::memmove_i64:
154       case Intrinsic::memset_i32:
155       case Intrinsic::memset_i64:
156         return true;
157       default: return false;
158       }
159     }
160     static inline bool classof(const Value *V) {
161       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
162     }
163   };
164
165
166   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
167   ///
168   struct MemCpyInst : public MemIntrinsic {
169     /// get* - Return the arguments to the instruction.
170     ///
171     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
172
173     /// getSource - This is just like getRawSource, but it strips off any cast
174     /// instructions that feed it, giving the original input.  The returned
175     /// value is guaranteed to be a pointer.
176     Value *getSource() const { return StripPointerCasts(getRawSource()); }
177
178
179     void setSource(Value *Ptr) {
180       assert(getRawSource()->getType() == Ptr->getType() &&
181              "setSource called with pointer of wrong type!");
182       setOperand(2, Ptr);
183     }
184
185     // Methods for support type inquiry through isa, cast, and dyn_cast:
186     static inline bool classof(const MemCpyInst *) { return true; }
187     static inline bool classof(const IntrinsicInst *I) {
188       return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
189              I->getIntrinsicID() == Intrinsic::memcpy_i64;
190     }
191     static inline bool classof(const Value *V) {
192       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
193     }
194   };
195
196   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
197   ///
198   struct MemMoveInst : public MemIntrinsic {
199     /// get* - Return the arguments to the instruction.
200     ///
201     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
202
203     /// getSource - This is just like getRawSource, but it strips off any cast
204     /// instructions that feed it, giving the original input.  The returned
205     /// value is guaranteed to be a pointer.
206     Value *getSource() const { return StripPointerCasts(getRawSource()); }
207
208     void setSource(Value *Ptr) {
209       assert(getRawSource()->getType() == Ptr->getType() &&
210              "setSource called with pointer of wrong type!");
211       setOperand(2, Ptr);
212     }
213
214     // Methods for support type inquiry through isa, cast, and dyn_cast:
215     static inline bool classof(const MemMoveInst *) { return true; }
216     static inline bool classof(const IntrinsicInst *I) {
217       return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
218              I->getIntrinsicID() == Intrinsic::memmove_i64;
219     }
220     static inline bool classof(const Value *V) {
221       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
222     }
223   };
224
225   /// MemSetInst - This class wraps the llvm.memset intrinsic.
226   ///
227   struct MemSetInst : public MemIntrinsic {
228     /// get* - Return the arguments to the instruction.
229     ///
230     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
231
232     void setValue(Value *Val) {
233       assert(getValue()->getType() == Val->getType() &&
234              "setSource called with pointer of wrong type!");
235       setOperand(2, Val);
236     }
237
238     // Methods for support type inquiry through isa, cast, and dyn_cast:
239     static inline bool classof(const MemSetInst *) { return true; }
240     static inline bool classof(const IntrinsicInst *I) {
241       return I->getIntrinsicID() == Intrinsic::memset_i32 ||
242              I->getIntrinsicID() == Intrinsic::memset_i64;
243     }
244     static inline bool classof(const Value *V) {
245       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
246     }
247   };
248 }
249
250 #endif