The guaranteed alignment of ptr+offset is only the minimum of
[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 #include "llvm/System/IncludeFile.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();                      // DO NOT IMPLEMENT
39     IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
40     void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
41   public:
42
43     /// StripPointerCasts - This static method strips off any unneeded pointer
44     /// casts from the specified value, returning the original uncasted value.
45     /// Note that the returned value is guaranteed to have pointer type.
46     static Value *StripPointerCasts(Value *Ptr);
47     
48     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
49     ///
50     Intrinsic::ID getIntrinsicID() const {
51       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
52     }
53     
54     // Methods for support type inquiry through isa, cast, and dyn_cast:
55     static inline bool classof(const IntrinsicInst *) { return true; }
56     static inline bool classof(const CallInst *I) {
57       if (const Function *CF = I->getCalledFunction())
58         return CF->getIntrinsicID() != 0;
59       return false;
60     }
61     static inline bool classof(const Value *V) {
62       return isa<CallInst>(V) && classof(cast<CallInst>(V));
63     }
64   };
65
66   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
67   ///
68   struct DbgInfoIntrinsic : public IntrinsicInst {
69
70     // Methods for support type inquiry through isa, cast, and dyn_cast:
71     static inline bool classof(const DbgInfoIntrinsic *) { return true; }
72     static inline bool classof(const IntrinsicInst *I) {
73       switch (I->getIntrinsicID()) {
74       case Intrinsic::dbg_stoppoint:
75       case Intrinsic::dbg_func_start:
76       case Intrinsic::dbg_region_start:
77       case Intrinsic::dbg_region_end:
78       case Intrinsic::dbg_declare:
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     static Value *StripCast(Value *C);
88   };
89
90   /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
91   ///
92   struct DbgStopPointInst : public DbgInfoIntrinsic {
93     Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
94     Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
95     Value *getContext() const {
96       return StripCast(getOperand(3));
97     }
98
99     unsigned getLine() const {
100       return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
101     }
102     unsigned getColumn() const {
103       return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
104     }
105     
106     std::string getFileName() const;
107     std::string getDirectory() const;
108
109     // Methods for support type inquiry through isa, cast, and dyn_cast:
110     static inline bool classof(const DbgStopPointInst *) { return true; }
111     static inline bool classof(const IntrinsicInst *I) {
112       return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
113     }
114     static inline bool classof(const Value *V) {
115       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
116     }
117   };
118   
119   /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
120   ///
121   struct DbgFuncStartInst : public DbgInfoIntrinsic {
122     Value *getSubprogram() const { return StripCast(getOperand(1)); }
123
124     // Methods for support type inquiry through isa, cast, and dyn_cast:
125     static inline bool classof(const DbgFuncStartInst *) { return true; }
126     static inline bool classof(const IntrinsicInst *I) {
127       return I->getIntrinsicID() == Intrinsic::dbg_func_start;
128     }
129     static inline bool classof(const Value *V) {
130       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
131     }
132   };
133
134   /// DbgRegionStartInst - This represents the llvm.dbg.region.start
135   /// instruction.
136   struct DbgRegionStartInst : public DbgInfoIntrinsic {
137     Value *getContext() const { return StripCast(getOperand(1)); }
138
139     // Methods for support type inquiry through isa, cast, and dyn_cast:
140     static inline bool classof(const DbgRegionStartInst *) { return true; }
141     static inline bool classof(const IntrinsicInst *I) {
142       return I->getIntrinsicID() == Intrinsic::dbg_region_start;
143     }
144     static inline bool classof(const Value *V) {
145       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
146     }
147   };
148
149   /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
150   ///
151   struct DbgRegionEndInst : public DbgInfoIntrinsic {
152     Value *getContext() const { return StripCast(getOperand(1)); }
153
154     // Methods for support type inquiry through isa, cast, and dyn_cast:
155     static inline bool classof(const DbgRegionEndInst *) { return true; }
156     static inline bool classof(const IntrinsicInst *I) {
157       return I->getIntrinsicID() == Intrinsic::dbg_region_end;
158     }
159     static inline bool classof(const Value *V) {
160       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
161     }
162   };
163
164   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
165   ///
166   struct DbgDeclareInst : public DbgInfoIntrinsic {
167     Value *getAddress()  const { return getOperand(1); }
168     Value *getVariable() const { return StripCast(getOperand(2)); }
169
170     // Methods for support type inquiry through isa, cast, and dyn_cast:
171     static inline bool classof(const DbgDeclareInst *) { return true; }
172     static inline bool classof(const IntrinsicInst *I) {
173       return I->getIntrinsicID() == Intrinsic::dbg_declare;
174     }
175     static inline bool classof(const Value *V) {
176       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
177     }
178   };
179
180   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
181   ///
182   struct MemIntrinsic : public IntrinsicInst {
183     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
184
185     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
186     ConstantInt *getAlignment() const {
187       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
188     }
189
190     /// getDest - This is just like getRawDest, but it strips off any cast
191     /// instructions that feed it, giving the original input.  The returned
192     /// value is guaranteed to be a pointer.
193     Value *getDest() const { return StripPointerCasts(getRawDest()); }
194
195     /// set* - Set the specified arguments of the instruction.
196     ///
197     void setDest(Value *Ptr) {
198       assert(getRawDest()->getType() == Ptr->getType() &&
199              "setDest called with pointer of wrong type!");
200       setOperand(1, Ptr);
201     }
202
203     void setLength(Value *L) {
204       assert(getLength()->getType() == L->getType() &&
205              "setLength called with value of wrong type!");
206       setOperand(3, L);
207     }
208     void setAlignment(ConstantInt *A) {
209       assert(getAlignment()->getType() == A->getType() &&
210              "setAlignment called with value of wrong type!");
211       setOperand(4, A);
212     }
213
214     // Methods for support type inquiry through isa, cast, and dyn_cast:
215     static inline bool classof(const MemIntrinsic *) { return true; }
216     static inline bool classof(const IntrinsicInst *I) {
217       switch (I->getIntrinsicID()) {
218       case Intrinsic::memcpy_i32:
219       case Intrinsic::memcpy_i64:
220       case Intrinsic::memmove_i32:
221       case Intrinsic::memmove_i64:
222       case Intrinsic::memset_i32:
223       case Intrinsic::memset_i64:
224         return true;
225       default: return false;
226       }
227     }
228     static inline bool classof(const Value *V) {
229       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
230     }
231   };
232
233
234   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
235   ///
236   struct MemCpyInst : public MemIntrinsic {
237     /// get* - Return the arguments to the instruction.
238     ///
239     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
240
241     /// getSource - This is just like getRawSource, but it strips off any cast
242     /// instructions that feed it, giving the original input.  The returned
243     /// value is guaranteed to be a pointer.
244     Value *getSource() const { return StripPointerCasts(getRawSource()); }
245
246
247     void setSource(Value *Ptr) {
248       assert(getRawSource()->getType() == Ptr->getType() &&
249              "setSource called with pointer of wrong type!");
250       setOperand(2, Ptr);
251     }
252
253     // Methods for support type inquiry through isa, cast, and dyn_cast:
254     static inline bool classof(const MemCpyInst *) { return true; }
255     static inline bool classof(const IntrinsicInst *I) {
256       return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
257              I->getIntrinsicID() == Intrinsic::memcpy_i64;
258     }
259     static inline bool classof(const Value *V) {
260       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
261     }
262   };
263
264   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
265   ///
266   struct MemMoveInst : public MemIntrinsic {
267     /// get* - Return the arguments to the instruction.
268     ///
269     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
270
271     /// getSource - This is just like getRawSource, but it strips off any cast
272     /// instructions that feed it, giving the original input.  The returned
273     /// value is guaranteed to be a pointer.
274     Value *getSource() const { return StripPointerCasts(getRawSource()); }
275
276     void setSource(Value *Ptr) {
277       assert(getRawSource()->getType() == Ptr->getType() &&
278              "setSource called with pointer of wrong type!");
279       setOperand(2, Ptr);
280     }
281
282     // Methods for support type inquiry through isa, cast, and dyn_cast:
283     static inline bool classof(const MemMoveInst *) { return true; }
284     static inline bool classof(const IntrinsicInst *I) {
285       return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
286              I->getIntrinsicID() == Intrinsic::memmove_i64;
287     }
288     static inline bool classof(const Value *V) {
289       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
290     }
291   };
292
293   /// MemSetInst - This class wraps the llvm.memset intrinsic.
294   ///
295   struct MemSetInst : public MemIntrinsic {
296     /// get* - Return the arguments to the instruction.
297     ///
298     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
299
300     void setValue(Value *Val) {
301       assert(getValue()->getType() == Val->getType() &&
302              "setSource called with pointer of wrong type!");
303       setOperand(2, Val);
304     }
305
306     // Methods for support type inquiry through isa, cast, and dyn_cast:
307     static inline bool classof(const MemSetInst *) { return true; }
308     static inline bool classof(const IntrinsicInst *I) {
309       return I->getIntrinsicID() == Intrinsic::memset_i32 ||
310              I->getIntrinsicID() == Intrinsic::memset_i64;
311     }
312     static inline bool classof(const Value *V) {
313       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
314     }
315   };
316
317 }
318
319 // Ensure that the IntrinsicInst.cpp file gets added as a dependency of any 
320 // file that includes this header
321 FORCE_DEFINING_FILE_TO_BE_LINKED(IntrinsicInst)
322
323 #endif