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/Metadata.h"
29 #include "llvm/Function.h"
30 #include "llvm/Instructions.h"
31 #include "llvm/Intrinsics.h"
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
42 /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
44 Intrinsic::ID getIntrinsicID() const {
45 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
48 // Methods for support type inquiry through isa, cast, and dyn_cast:
49 static inline bool classof(const IntrinsicInst *) { return true; }
50 static inline bool classof(const CallInst *I) {
51 if (const Function *CF = I->getCalledFunction())
52 return CF->getIntrinsicID() != 0;
55 static inline bool classof(const Value *V) {
56 return isa<CallInst>(V) && classof(cast<CallInst>(V));
60 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
62 struct DbgInfoIntrinsic : public IntrinsicInst {
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_stoppoint:
69 case Intrinsic::dbg_func_start:
70 case Intrinsic::dbg_region_start:
71 case Intrinsic::dbg_region_end:
72 case Intrinsic::dbg_declare:
74 default: return false;
77 static inline bool classof(const Value *V) {
78 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
81 static Value *StripCast(Value *C);
84 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
86 struct DbgStopPointInst : public DbgInfoIntrinsic {
87 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
88 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
89 MDNode *getContext() const {
90 return cast<MDNode>(getOperand(3));
93 unsigned getLine() const {
94 return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
96 unsigned getColumn() const {
97 return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
100 Value* getFileName() const;
101 Value* getDirectory() const;
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;
108 static inline bool classof(const Value *V) {
109 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
113 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
115 struct DbgFuncStartInst : public DbgInfoIntrinsic {
116 MDNode *getSubprogram() const { return cast<MDNode>(getOperand(1)); }
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const DbgFuncStartInst *) { return true; }
120 static inline bool classof(const IntrinsicInst *I) {
121 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
123 static inline bool classof(const Value *V) {
124 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
128 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
130 struct DbgRegionStartInst : public DbgInfoIntrinsic {
131 MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const DbgRegionStartInst *) { return true; }
135 static inline bool classof(const IntrinsicInst *I) {
136 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
138 static inline bool classof(const Value *V) {
139 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
143 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
145 struct DbgRegionEndInst : public DbgInfoIntrinsic {
146 MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
148 // Methods for support type inquiry through isa, cast, and dyn_cast:
149 static inline bool classof(const DbgRegionEndInst *) { return true; }
150 static inline bool classof(const IntrinsicInst *I) {
151 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
153 static inline bool classof(const Value *V) {
154 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
158 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
160 struct DbgDeclareInst : public DbgInfoIntrinsic {
161 Value *getAddress() const { return getOperand(1); }
162 MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
164 // Methods for support type inquiry through isa, cast, and dyn_cast:
165 static inline bool classof(const DbgDeclareInst *) { return true; }
166 static inline bool classof(const IntrinsicInst *I) {
167 return I->getIntrinsicID() == Intrinsic::dbg_declare;
169 static inline bool classof(const Value *V) {
170 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
174 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
176 struct MemIntrinsic : public IntrinsicInst {
177 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
179 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
180 ConstantInt *getAlignmentCst() const {
181 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
184 unsigned getAlignment() const {
185 return getAlignmentCst()->getZExtValue();
188 /// getDest - This is just like getRawDest, but it strips off any cast
189 /// instructions that feed it, giving the original input. The returned
190 /// value is guaranteed to be a pointer.
191 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
193 /// set* - Set the specified arguments of the instruction.
195 void setDest(Value *Ptr) {
196 assert(getRawDest()->getType() == Ptr->getType() &&
197 "setDest called with pointer of wrong type!");
201 void setLength(Value *L) {
202 assert(getLength()->getType() == L->getType() &&
203 "setLength called with value of wrong type!");
207 void setAlignment(Constant* A) {
211 const Type *getAlignmentType() const {
212 return getOperand(4)->getType();
215 // Methods for support type inquiry through isa, cast, and dyn_cast:
216 static inline bool classof(const MemIntrinsic *) { return true; }
217 static inline bool classof(const IntrinsicInst *I) {
218 switch (I->getIntrinsicID()) {
219 case Intrinsic::memcpy:
220 case Intrinsic::memmove:
221 case Intrinsic::memset:
223 default: return false;
226 static inline bool classof(const Value *V) {
227 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
231 /// MemSetInst - This class wraps the llvm.memset intrinsic.
233 struct MemSetInst : public MemIntrinsic {
234 /// get* - Return the arguments to the instruction.
236 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
238 void setValue(Value *Val) {
239 assert(getValue()->getType() == Val->getType() &&
240 "setSource called with pointer of wrong type!");
244 // Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const MemSetInst *) { return true; }
246 static inline bool classof(const IntrinsicInst *I) {
247 return I->getIntrinsicID() == Intrinsic::memset;
249 static inline bool classof(const Value *V) {
250 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
254 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
256 struct MemTransferInst : public MemIntrinsic {
257 /// get* - Return the arguments to the instruction.
259 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
261 /// getSource - This is just like getRawSource, but it strips off any cast
262 /// instructions that feed it, giving the original input. The returned
263 /// value is guaranteed to be a pointer.
264 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
266 void setSource(Value *Ptr) {
267 assert(getRawSource()->getType() == Ptr->getType() &&
268 "setSource called with pointer of wrong type!");
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const MemTransferInst *) { return true; }
274 static inline bool classof(const IntrinsicInst *I) {
275 return I->getIntrinsicID() == Intrinsic::memcpy ||
276 I->getIntrinsicID() == Intrinsic::memmove;
278 static inline bool classof(const Value *V) {
279 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
284 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
286 struct MemCpyInst : public MemTransferInst {
287 // Methods for support type inquiry through isa, cast, and dyn_cast:
288 static inline bool classof(const MemCpyInst *) { return true; }
289 static inline bool classof(const IntrinsicInst *I) {
290 return I->getIntrinsicID() == Intrinsic::memcpy;
292 static inline bool classof(const Value *V) {
293 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
297 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
299 struct MemMoveInst : public MemTransferInst {
300 // Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool classof(const MemMoveInst *) { return true; }
302 static inline bool classof(const IntrinsicInst *I) {
303 return I->getIntrinsicID() == Intrinsic::memmove;
305 static inline bool classof(const Value *V) {
306 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
310 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
312 struct EHSelectorInst : public IntrinsicInst {
313 // Methods for support type inquiry through isa, cast, and dyn_cast:
314 static inline bool classof(const EHSelectorInst *) { return true; }
315 static inline bool classof(const IntrinsicInst *I) {
316 return I->getIntrinsicID() == Intrinsic::eh_selector;
318 static inline bool classof(const Value *V) {
319 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));