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