1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
14 // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
15 // ... MCI->getDest() ... MCI->getSource() ...
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
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_INTRINSICINST_H
25 #define LLVM_INTRINSICINST_H
27 #include "llvm/Constants.h"
28 #include "llvm/Function.h"
29 #include "llvm/Instructions.h"
30 #include "llvm/Intrinsics.h"
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
41 /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
43 Intrinsic::ID getIntrinsicID() const {
44 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
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;
54 static inline bool classof(const Value *V) {
55 return isa<CallInst>(V) && classof(cast<CallInst>(V));
59 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
61 struct DbgInfoIntrinsic : public IntrinsicInst {
63 // Methods for support type inquiry through isa, cast, and dyn_cast:
64 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
65 static inline bool classof(const IntrinsicInst *I) {
66 switch (I->getIntrinsicID()) {
67 case Intrinsic::dbg_stoppoint:
68 case Intrinsic::dbg_func_start:
69 case Intrinsic::dbg_region_start:
70 case Intrinsic::dbg_region_end:
71 case Intrinsic::dbg_declare:
73 default: return false;
76 static inline bool classof(const Value *V) {
77 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
80 static Value *StripCast(Value *C);
83 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
85 struct DbgStopPointInst : public DbgInfoIntrinsic {
86 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
87 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
88 Value *getContext() const {
89 return StripCast(getOperand(3));
92 unsigned getLine() const {
93 return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
95 unsigned getColumn() const {
96 return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
99 Value* getFileName() const;
100 Value* getDirectory() const;
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;
107 static inline bool classof(const Value *V) {
108 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
112 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
114 struct DbgFuncStartInst : public DbgInfoIntrinsic {
115 Value *getSubprogram() const { return StripCast(getOperand(1)); }
117 // Methods for support type inquiry through isa, cast, and dyn_cast:
118 static inline bool classof(const DbgFuncStartInst *) { return true; }
119 static inline bool classof(const IntrinsicInst *I) {
120 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
122 static inline bool classof(const Value *V) {
123 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
127 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
129 struct DbgRegionStartInst : public DbgInfoIntrinsic {
130 Value *getContext() const { return StripCast(getOperand(1)); }
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const DbgRegionStartInst *) { return true; }
134 static inline bool classof(const IntrinsicInst *I) {
135 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
137 static inline bool classof(const Value *V) {
138 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
142 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
144 struct DbgRegionEndInst : public DbgInfoIntrinsic {
145 Value *getContext() const { return StripCast(getOperand(1)); }
147 // Methods for support type inquiry through isa, cast, and dyn_cast:
148 static inline bool classof(const DbgRegionEndInst *) { return true; }
149 static inline bool classof(const IntrinsicInst *I) {
150 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
152 static inline bool classof(const Value *V) {
153 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
157 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
159 struct DbgDeclareInst : public DbgInfoIntrinsic {
160 Value *getAddress() const { return getOperand(1); }
161 Value *getVariable() const { return StripCast(getOperand(2)); }
163 // Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const DbgDeclareInst *) { return true; }
165 static inline bool classof(const IntrinsicInst *I) {
166 return I->getIntrinsicID() == Intrinsic::dbg_declare;
168 static inline bool classof(const Value *V) {
169 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
173 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
175 struct MemIntrinsic : public IntrinsicInst {
176 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
178 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
179 ConstantInt *getAlignment() const {
180 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
183 /// getDest - This is just like getRawDest, but it strips off any cast
184 /// instructions that feed it, giving the original input. The returned
185 /// value is guaranteed to be a pointer.
186 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
188 /// set* - Set the specified arguments of the instruction.
190 void setDest(Value *Ptr) {
191 assert(getRawDest()->getType() == Ptr->getType() &&
192 "setDest called with pointer of wrong type!");
196 void setLength(Value *L) {
197 assert(getLength()->getType() == L->getType() &&
198 "setLength called with value of wrong type!");
201 void setAlignment(ConstantInt *A) {
202 assert(getAlignment()->getType() == A->getType() &&
203 "setAlignment called with value of wrong type!");
207 // Methods for support type inquiry through isa, cast, and dyn_cast:
208 static inline bool classof(const MemIntrinsic *) { return true; }
209 static inline bool classof(const IntrinsicInst *I) {
210 switch (I->getIntrinsicID()) {
211 case Intrinsic::memcpy:
212 case Intrinsic::memmove:
213 case Intrinsic::memset:
215 default: return false;
218 static inline bool classof(const Value *V) {
219 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
224 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
226 struct MemCpyInst : public MemIntrinsic {
227 /// get* - Return the arguments to the instruction.
229 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
231 /// getSource - This is just like getRawSource, but it strips off any cast
232 /// instructions that feed it, giving the original input. The returned
233 /// value is guaranteed to be a pointer.
234 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
237 void setSource(Value *Ptr) {
238 assert(getRawSource()->getType() == Ptr->getType() &&
239 "setSource called with pointer of wrong type!");
243 // Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const MemCpyInst *) { return true; }
245 static inline bool classof(const IntrinsicInst *I) {
246 return I->getIntrinsicID() == Intrinsic::memcpy;
248 static inline bool classof(const Value *V) {
249 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
253 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
255 struct MemMoveInst : public MemIntrinsic {
256 /// get* - Return the arguments to the instruction.
258 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
260 /// getSource - This is just like getRawSource, but it strips off any cast
261 /// instructions that feed it, giving the original input. The returned
262 /// value is guaranteed to be a pointer.
263 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
265 void setSource(Value *Ptr) {
266 assert(getRawSource()->getType() == Ptr->getType() &&
267 "setSource called with pointer of wrong type!");
271 // Methods for support type inquiry through isa, cast, and dyn_cast:
272 static inline bool classof(const MemMoveInst *) { return true; }
273 static inline bool classof(const IntrinsicInst *I) {
274 return I->getIntrinsicID() == Intrinsic::memmove;
276 static inline bool classof(const Value *V) {
277 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
281 /// MemSetInst - This class wraps the llvm.memset intrinsic.
283 struct MemSetInst : public MemIntrinsic {
284 /// get* - Return the arguments to the instruction.
286 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
288 void setValue(Value *Val) {
289 assert(getValue()->getType() == Val->getType() &&
290 "setSource called with pointer of wrong type!");
294 // Methods for support type inquiry through isa, cast, and dyn_cast:
295 static inline bool classof(const MemSetInst *) { return true; }
296 static inline bool classof(const IntrinsicInst *I) {
297 return I->getIntrinsicID() == Intrinsic::memset;
299 static inline bool classof(const Value *V) {
300 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
304 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
306 struct EHSelectorInst : public IntrinsicInst {
307 // Methods for support type inquiry through isa, cast, and dyn_cast:
308 static inline bool classof(const EHSelectorInst *) { return true; }
309 static inline bool classof(const IntrinsicInst *I) {
310 return I->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
311 I->getIntrinsicID() == Intrinsic::eh_selector_i64;
313 static inline bool classof(const Value *V) {
314 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));