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:
72 case Intrinsic::dbg_value:
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 /// DbgValueInst - This represents the llvm.dbg.value instruction.
176 struct DbgValueInst : public DbgInfoIntrinsic {
177 Value *getValue() const;
178 Value *getOffset() const { return getOperand(2); }
179 MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
181 // Methods for support type inquiry through isa, cast, and dyn_cast:
182 static inline bool classof(const DbgValueInst *) { return true; }
183 static inline bool classof(const IntrinsicInst *I) {
184 return I->getIntrinsicID() == Intrinsic::dbg_value;
186 static inline bool classof(const Value *V) {
187 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
191 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
193 struct MemIntrinsic : public IntrinsicInst {
194 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
196 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
197 ConstantInt *getAlignmentCst() const {
198 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
201 unsigned getAlignment() const {
202 return getAlignmentCst()->getZExtValue();
205 /// getDest - This is just like getRawDest, but it strips off any cast
206 /// instructions that feed it, giving the original input. The returned
207 /// value is guaranteed to be a pointer.
208 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
210 /// set* - Set the specified arguments of the instruction.
212 void setDest(Value *Ptr) {
213 assert(getRawDest()->getType() == Ptr->getType() &&
214 "setDest called with pointer of wrong type!");
218 void setLength(Value *L) {
219 assert(getLength()->getType() == L->getType() &&
220 "setLength called with value of wrong type!");
224 void setAlignment(Constant* A) {
228 const Type *getAlignmentType() const {
229 return getOperand(4)->getType();
232 // Methods for support type inquiry through isa, cast, and dyn_cast:
233 static inline bool classof(const MemIntrinsic *) { return true; }
234 static inline bool classof(const IntrinsicInst *I) {
235 switch (I->getIntrinsicID()) {
236 case Intrinsic::memcpy:
237 case Intrinsic::memmove:
238 case Intrinsic::memset:
240 default: return false;
243 static inline bool classof(const Value *V) {
244 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
248 /// MemSetInst - This class wraps the llvm.memset intrinsic.
250 struct MemSetInst : public MemIntrinsic {
251 /// get* - Return the arguments to the instruction.
253 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
255 void setValue(Value *Val) {
256 assert(getValue()->getType() == Val->getType() &&
257 "setSource called with pointer of wrong type!");
261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const MemSetInst *) { return true; }
263 static inline bool classof(const IntrinsicInst *I) {
264 return I->getIntrinsicID() == Intrinsic::memset;
266 static inline bool classof(const Value *V) {
267 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
271 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
273 struct MemTransferInst : public MemIntrinsic {
274 /// get* - Return the arguments to the instruction.
276 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
278 /// getSource - This is just like getRawSource, but it strips off any cast
279 /// instructions that feed it, giving the original input. The returned
280 /// value is guaranteed to be a pointer.
281 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
283 void setSource(Value *Ptr) {
284 assert(getRawSource()->getType() == Ptr->getType() &&
285 "setSource called with pointer of wrong type!");
289 // Methods for support type inquiry through isa, cast, and dyn_cast:
290 static inline bool classof(const MemTransferInst *) { return true; }
291 static inline bool classof(const IntrinsicInst *I) {
292 return I->getIntrinsicID() == Intrinsic::memcpy ||
293 I->getIntrinsicID() == Intrinsic::memmove;
295 static inline bool classof(const Value *V) {
296 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
301 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
303 struct MemCpyInst : public MemTransferInst {
304 // Methods for support type inquiry through isa, cast, and dyn_cast:
305 static inline bool classof(const MemCpyInst *) { return true; }
306 static inline bool classof(const IntrinsicInst *I) {
307 return I->getIntrinsicID() == Intrinsic::memcpy;
309 static inline bool classof(const Value *V) {
310 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
314 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
316 struct MemMoveInst : public MemTransferInst {
317 // Methods for support type inquiry through isa, cast, and dyn_cast:
318 static inline bool classof(const MemMoveInst *) { return true; }
319 static inline bool classof(const IntrinsicInst *I) {
320 return I->getIntrinsicID() == Intrinsic::memmove;
322 static inline bool classof(const Value *V) {
323 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
327 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
329 struct EHSelectorInst : public IntrinsicInst {
330 // Methods for support type inquiry through isa, cast, and dyn_cast:
331 static inline bool classof(const EHSelectorInst *) { return true; }
332 static inline bool classof(const IntrinsicInst *I) {
333 return I->getIntrinsicID() == Intrinsic::eh_selector;
335 static inline bool classof(const Value *V) {
336 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
340 /// MemoryUseIntrinsic - This is the common base class for the memory use
341 /// marker intrinsics.
343 struct MemoryUseIntrinsic : public IntrinsicInst {
345 // Methods for support type inquiry through isa, cast, and dyn_cast:
346 static inline bool classof(const MemoryUseIntrinsic *) { return true; }
347 static inline bool classof(const IntrinsicInst *I) {
348 switch (I->getIntrinsicID()) {
349 case Intrinsic::lifetime_start:
350 case Intrinsic::lifetime_end:
351 case Intrinsic::invariant_start:
352 case Intrinsic::invariant_end:
354 default: return false;
357 static inline bool classof(const Value *V) {
358 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));