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:
73 case Intrinsic::dbg_value:
75 default: return false;
78 static inline bool classof(const Value *V) {
79 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
82 static Value *StripCast(Value *C);
85 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
87 struct DbgStopPointInst : public DbgInfoIntrinsic {
88 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
89 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
90 MDNode *getContext() const {
91 return cast<MDNode>(getOperand(3));
94 unsigned getLine() const {
95 return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
97 unsigned getColumn() const {
98 return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
101 Value* getFileName() const;
102 Value* getDirectory() const;
104 // Methods for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const DbgStopPointInst *) { return true; }
106 static inline bool classof(const IntrinsicInst *I) {
107 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
109 static inline bool classof(const Value *V) {
110 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
114 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
116 struct DbgFuncStartInst : public DbgInfoIntrinsic {
117 MDNode *getSubprogram() const { return cast<MDNode>(getOperand(1)); }
119 // Methods for support type inquiry through isa, cast, and dyn_cast:
120 static inline bool classof(const DbgFuncStartInst *) { return true; }
121 static inline bool classof(const IntrinsicInst *I) {
122 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
124 static inline bool classof(const Value *V) {
125 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
129 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
131 struct DbgRegionStartInst : public DbgInfoIntrinsic {
132 MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
134 // Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const DbgRegionStartInst *) { return true; }
136 static inline bool classof(const IntrinsicInst *I) {
137 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
139 static inline bool classof(const Value *V) {
140 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
144 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
146 struct DbgRegionEndInst : public DbgInfoIntrinsic {
147 MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
149 // Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const DbgRegionEndInst *) { return true; }
151 static inline bool classof(const IntrinsicInst *I) {
152 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
154 static inline bool classof(const Value *V) {
155 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
159 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
161 struct DbgDeclareInst : public DbgInfoIntrinsic {
162 Value *getAddress() const { return getOperand(1); }
163 MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
165 // Methods for support type inquiry through isa, cast, and dyn_cast:
166 static inline bool classof(const DbgDeclareInst *) { return true; }
167 static inline bool classof(const IntrinsicInst *I) {
168 return I->getIntrinsicID() == Intrinsic::dbg_declare;
170 static inline bool classof(const Value *V) {
171 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
175 /// DbgValueInst - This represents the llvm.dbg.value instruction.
177 struct DbgValueInst : public DbgInfoIntrinsic {
178 Value *getValue() const {
179 return cast<MDNode>(getOperand(1))->getElement(0);
181 Value *getOffset() const { return getOperand(2); }
182 MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
184 // Methods for support type inquiry through isa, cast, and dyn_cast:
185 static inline bool classof(const DbgValueInst *) { return true; }
186 static inline bool classof(const IntrinsicInst *I) {
187 return I->getIntrinsicID() == Intrinsic::dbg_value;
189 static inline bool classof(const Value *V) {
190 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
194 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
196 struct MemIntrinsic : public IntrinsicInst {
197 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
199 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
200 ConstantInt *getAlignmentCst() const {
201 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
204 unsigned getAlignment() const {
205 return getAlignmentCst()->getZExtValue();
208 /// getDest - This is just like getRawDest, but it strips off any cast
209 /// instructions that feed it, giving the original input. The returned
210 /// value is guaranteed to be a pointer.
211 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
213 /// set* - Set the specified arguments of the instruction.
215 void setDest(Value *Ptr) {
216 assert(getRawDest()->getType() == Ptr->getType() &&
217 "setDest called with pointer of wrong type!");
221 void setLength(Value *L) {
222 assert(getLength()->getType() == L->getType() &&
223 "setLength called with value of wrong type!");
227 void setAlignment(Constant* A) {
231 const Type *getAlignmentType() const {
232 return getOperand(4)->getType();
235 // Methods for support type inquiry through isa, cast, and dyn_cast:
236 static inline bool classof(const MemIntrinsic *) { return true; }
237 static inline bool classof(const IntrinsicInst *I) {
238 switch (I->getIntrinsicID()) {
239 case Intrinsic::memcpy:
240 case Intrinsic::memmove:
241 case Intrinsic::memset:
243 default: return false;
246 static inline bool classof(const Value *V) {
247 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
251 /// MemSetInst - This class wraps the llvm.memset intrinsic.
253 struct MemSetInst : public MemIntrinsic {
254 /// get* - Return the arguments to the instruction.
256 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
258 void setValue(Value *Val) {
259 assert(getValue()->getType() == Val->getType() &&
260 "setSource called with pointer of wrong type!");
264 // Methods for support type inquiry through isa, cast, and dyn_cast:
265 static inline bool classof(const MemSetInst *) { return true; }
266 static inline bool classof(const IntrinsicInst *I) {
267 return I->getIntrinsicID() == Intrinsic::memset;
269 static inline bool classof(const Value *V) {
270 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
274 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
276 struct MemTransferInst : public MemIntrinsic {
277 /// get* - Return the arguments to the instruction.
279 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
281 /// getSource - This is just like getRawSource, but it strips off any cast
282 /// instructions that feed it, giving the original input. The returned
283 /// value is guaranteed to be a pointer.
284 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
286 void setSource(Value *Ptr) {
287 assert(getRawSource()->getType() == Ptr->getType() &&
288 "setSource called with pointer of wrong type!");
292 // Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const MemTransferInst *) { return true; }
294 static inline bool classof(const IntrinsicInst *I) {
295 return I->getIntrinsicID() == Intrinsic::memcpy ||
296 I->getIntrinsicID() == Intrinsic::memmove;
298 static inline bool classof(const Value *V) {
299 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
304 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
306 struct MemCpyInst : public MemTransferInst {
307 // Methods for support type inquiry through isa, cast, and dyn_cast:
308 static inline bool classof(const MemCpyInst *) { return true; }
309 static inline bool classof(const IntrinsicInst *I) {
310 return I->getIntrinsicID() == Intrinsic::memcpy;
312 static inline bool classof(const Value *V) {
313 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
317 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
319 struct MemMoveInst : public MemTransferInst {
320 // Methods for support type inquiry through isa, cast, and dyn_cast:
321 static inline bool classof(const MemMoveInst *) { return true; }
322 static inline bool classof(const IntrinsicInst *I) {
323 return I->getIntrinsicID() == Intrinsic::memmove;
325 static inline bool classof(const Value *V) {
326 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
330 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
332 struct EHSelectorInst : public IntrinsicInst {
333 // Methods for support type inquiry through isa, cast, and dyn_cast:
334 static inline bool classof(const EHSelectorInst *) { return true; }
335 static inline bool classof(const IntrinsicInst *I) {
336 return I->getIntrinsicID() == Intrinsic::eh_selector;
338 static inline bool classof(const Value *V) {
339 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
343 /// MemoryUseIntrinsic - This is the common base class for the memory use
344 /// marker intrinsics.
346 struct MemoryUseIntrinsic : public IntrinsicInst {
348 // Methods for support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const MemoryUseIntrinsic *) { return true; }
350 static inline bool classof(const IntrinsicInst *I) {
351 switch (I->getIntrinsicID()) {
352 case Intrinsic::lifetime_start:
353 case Intrinsic::lifetime_end:
354 case Intrinsic::invariant_start:
355 case Intrinsic::invariant_end:
357 default: return false;
360 static inline bool classof(const Value *V) {
361 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));