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 class 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_declare:
69 case Intrinsic::dbg_value:
71 default: return false;
74 static inline bool classof(const Value *V) {
75 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
78 static Value *StripCast(Value *C);
81 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
83 class DbgDeclareInst : public DbgInfoIntrinsic {
85 Value *getAddress() const;
86 MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
88 // Methods for support type inquiry through isa, cast, and dyn_cast:
89 static inline bool classof(const DbgDeclareInst *) { return true; }
90 static inline bool classof(const IntrinsicInst *I) {
91 return I->getIntrinsicID() == Intrinsic::dbg_declare;
93 static inline bool classof(const Value *V) {
94 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
98 /// DbgValueInst - This represents the llvm.dbg.value instruction.
100 class DbgValueInst : public DbgInfoIntrinsic {
102 const Value *getValue() const;
104 uint64_t getOffset() const {
105 return cast<ConstantInt>(
106 const_cast<Value*>(getOperand(2)))->getZExtValue();
108 const MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
109 MDNode *getVariable() { return cast<MDNode>(getOperand(3)); }
111 // Methods for support type inquiry through isa, cast, and dyn_cast:
112 static inline bool classof(const DbgValueInst *) { return true; }
113 static inline bool classof(const IntrinsicInst *I) {
114 return I->getIntrinsicID() == Intrinsic::dbg_value;
116 static inline bool classof(const Value *V) {
117 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
121 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
123 class MemIntrinsic : public IntrinsicInst {
125 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
127 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
128 ConstantInt *getAlignmentCst() const {
129 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
132 unsigned getAlignment() const {
133 return getAlignmentCst()->getZExtValue();
136 ConstantInt *getVolatileCst() const {
137 return cast<ConstantInt>(const_cast<Value*>(getOperand(5)));
139 bool isVolatile() const {
140 return getVolatileCst()->getZExtValue() != 0;
143 /// getDest - This is just like getRawDest, but it strips off any cast
144 /// instructions that feed it, giving the original input. The returned
145 /// value is guaranteed to be a pointer.
146 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
148 /// set* - Set the specified arguments of the instruction.
150 void setDest(Value *Ptr) {
151 assert(getRawDest()->getType() == Ptr->getType() &&
152 "setDest called with pointer of wrong type!");
156 void setLength(Value *L) {
157 assert(getLength()->getType() == L->getType() &&
158 "setLength called with value of wrong type!");
162 void setAlignment(Constant* A) {
166 void setVolatile(Constant* V) {
170 const Type *getAlignmentType() const {
171 return getOperand(4)->getType();
174 // Methods for support type inquiry through isa, cast, and dyn_cast:
175 static inline bool classof(const MemIntrinsic *) { return true; }
176 static inline bool classof(const IntrinsicInst *I) {
177 switch (I->getIntrinsicID()) {
178 case Intrinsic::memcpy:
179 case Intrinsic::memmove:
180 case Intrinsic::memset:
182 default: return false;
185 static inline bool classof(const Value *V) {
186 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
190 /// MemSetInst - This class wraps the llvm.memset intrinsic.
192 class MemSetInst : public MemIntrinsic {
194 /// get* - Return the arguments to the instruction.
196 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
198 void setValue(Value *Val) {
199 assert(getValue()->getType() == Val->getType() &&
200 "setSource called with pointer of wrong type!");
204 // Methods for support type inquiry through isa, cast, and dyn_cast:
205 static inline bool classof(const MemSetInst *) { return true; }
206 static inline bool classof(const IntrinsicInst *I) {
207 return I->getIntrinsicID() == Intrinsic::memset;
209 static inline bool classof(const Value *V) {
210 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
214 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
216 class MemTransferInst : public MemIntrinsic {
218 /// get* - Return the arguments to the instruction.
220 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
222 /// getSource - This is just like getRawSource, but it strips off any cast
223 /// instructions that feed it, giving the original input. The returned
224 /// value is guaranteed to be a pointer.
225 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
227 void setSource(Value *Ptr) {
228 assert(getRawSource()->getType() == Ptr->getType() &&
229 "setSource called with pointer of wrong type!");
233 // Methods for support type inquiry through isa, cast, and dyn_cast:
234 static inline bool classof(const MemTransferInst *) { return true; }
235 static inline bool classof(const IntrinsicInst *I) {
236 return I->getIntrinsicID() == Intrinsic::memcpy ||
237 I->getIntrinsicID() == Intrinsic::memmove;
239 static inline bool classof(const Value *V) {
240 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
245 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
247 class MemCpyInst : public MemTransferInst {
249 // Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const MemCpyInst *) { return true; }
251 static inline bool classof(const IntrinsicInst *I) {
252 return I->getIntrinsicID() == Intrinsic::memcpy;
254 static inline bool classof(const Value *V) {
255 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
259 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
261 class MemMoveInst : public MemTransferInst {
263 // Methods for support type inquiry through isa, cast, and dyn_cast:
264 static inline bool classof(const MemMoveInst *) { return true; }
265 static inline bool classof(const IntrinsicInst *I) {
266 return I->getIntrinsicID() == Intrinsic::memmove;
268 static inline bool classof(const Value *V) {
269 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
273 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
275 class EHSelectorInst : public IntrinsicInst {
277 // Methods for support type inquiry through isa, cast, and dyn_cast:
278 static inline bool classof(const EHSelectorInst *) { return true; }
279 static inline bool classof(const IntrinsicInst *I) {
280 return I->getIntrinsicID() == Intrinsic::eh_selector;
282 static inline bool classof(const Value *V) {
283 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
287 /// MemoryUseIntrinsic - This is the common base class for the memory use
288 /// marker intrinsics.
290 class MemoryUseIntrinsic : public IntrinsicInst {
293 // Methods for support type inquiry through isa, cast, and dyn_cast:
294 static inline bool classof(const MemoryUseIntrinsic *) { return true; }
295 static inline bool classof(const IntrinsicInst *I) {
296 switch (I->getIntrinsicID()) {
297 case Intrinsic::lifetime_start:
298 case Intrinsic::lifetime_end:
299 case Intrinsic::invariant_start:
300 case Intrinsic::invariant_end:
302 default: return false;
305 static inline bool classof(const Value *V) {
306 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));