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 class DbgInfoIntrinsic : public IntrinsicInst {
65 // Methods for support type inquiry through isa, cast, and dyn_cast:
66 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
67 static inline bool classof(const IntrinsicInst *I) {
68 switch (I->getIntrinsicID()) {
69 case Intrinsic::dbg_declare:
70 case Intrinsic::dbg_value:
72 default: return false;
75 static inline bool classof(const Value *V) {
76 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
79 static Value *StripCast(Value *C);
82 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
84 class DbgDeclareInst : public DbgInfoIntrinsic {
86 Value *getAddress() const {
87 if (MDNode* MD = dyn_cast<MDNode>(getOperand(1)))
88 return MD->getOperand(0);
92 MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const DbgDeclareInst *) { return true; }
96 static inline bool classof(const IntrinsicInst *I) {
97 return I->getIntrinsicID() == Intrinsic::dbg_declare;
99 static inline bool classof(const Value *V) {
100 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
104 /// DbgValueInst - This represents the llvm.dbg.value instruction.
106 class DbgValueInst : public DbgInfoIntrinsic {
108 const Value *getValue() const;
110 uint64_t getOffset() const {
111 return cast<ConstantInt>(
112 const_cast<Value*>(getOperand(2)))->getZExtValue();
114 const MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
115 MDNode *getVariable() { return cast<MDNode>(getOperand(3)); }
117 // Methods for support type inquiry through isa, cast, and dyn_cast:
118 static inline bool classof(const DbgValueInst *) { return true; }
119 static inline bool classof(const IntrinsicInst *I) {
120 return I->getIntrinsicID() == Intrinsic::dbg_value;
122 static inline bool classof(const Value *V) {
123 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
127 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
129 class MemIntrinsic : public IntrinsicInst {
131 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
133 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
134 ConstantInt *getAlignmentCst() const {
135 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
138 unsigned getAlignment() const {
139 return getAlignmentCst()->getZExtValue();
142 /// getDest - This is just like getRawDest, but it strips off any cast
143 /// instructions that feed it, giving the original input. The returned
144 /// value is guaranteed to be a pointer.
145 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
147 /// set* - Set the specified arguments of the instruction.
149 void setDest(Value *Ptr) {
150 assert(getRawDest()->getType() == Ptr->getType() &&
151 "setDest called with pointer of wrong type!");
155 void setLength(Value *L) {
156 assert(getLength()->getType() == L->getType() &&
157 "setLength called with value of wrong type!");
161 void setAlignment(Constant* A) {
165 const Type *getAlignmentType() const {
166 return getOperand(4)->getType();
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const MemIntrinsic *) { return true; }
171 static inline bool classof(const IntrinsicInst *I) {
172 switch (I->getIntrinsicID()) {
173 case Intrinsic::memcpy:
174 case Intrinsic::memmove:
175 case Intrinsic::memset:
177 default: return false;
180 static inline bool classof(const Value *V) {
181 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
185 /// MemSetInst - This class wraps the llvm.memset intrinsic.
187 class MemSetInst : public MemIntrinsic {
189 /// get* - Return the arguments to the instruction.
191 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
193 void setValue(Value *Val) {
194 assert(getValue()->getType() == Val->getType() &&
195 "setSource called with pointer of wrong type!");
199 // Methods for support type inquiry through isa, cast, and dyn_cast:
200 static inline bool classof(const MemSetInst *) { return true; }
201 static inline bool classof(const IntrinsicInst *I) {
202 return I->getIntrinsicID() == Intrinsic::memset;
204 static inline bool classof(const Value *V) {
205 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
209 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
211 class MemTransferInst : public MemIntrinsic {
213 /// get* - Return the arguments to the instruction.
215 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
217 /// getSource - This is just like getRawSource, but it strips off any cast
218 /// instructions that feed it, giving the original input. The returned
219 /// value is guaranteed to be a pointer.
220 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
222 void setSource(Value *Ptr) {
223 assert(getRawSource()->getType() == Ptr->getType() &&
224 "setSource called with pointer of wrong type!");
228 // Methods for support type inquiry through isa, cast, and dyn_cast:
229 static inline bool classof(const MemTransferInst *) { return true; }
230 static inline bool classof(const IntrinsicInst *I) {
231 return I->getIntrinsicID() == Intrinsic::memcpy ||
232 I->getIntrinsicID() == Intrinsic::memmove;
234 static inline bool classof(const Value *V) {
235 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
240 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
242 class MemCpyInst : public MemTransferInst {
244 // Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const MemCpyInst *) { return true; }
246 static inline bool classof(const IntrinsicInst *I) {
247 return I->getIntrinsicID() == Intrinsic::memcpy;
249 static inline bool classof(const Value *V) {
250 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
254 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
256 class MemMoveInst : public MemTransferInst {
258 // Methods for support type inquiry through isa, cast, and dyn_cast:
259 static inline bool classof(const MemMoveInst *) { return true; }
260 static inline bool classof(const IntrinsicInst *I) {
261 return I->getIntrinsicID() == Intrinsic::memmove;
263 static inline bool classof(const Value *V) {
264 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
268 /// EHSelectorInst - This represents the llvm.eh.selector instruction.
270 class EHSelectorInst : public IntrinsicInst {
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const EHSelectorInst *) { return true; }
274 static inline bool classof(const IntrinsicInst *I) {
275 return I->getIntrinsicID() == Intrinsic::eh_selector;
277 static inline bool classof(const Value *V) {
278 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
282 /// MemoryUseIntrinsic - This is the common base class for the memory use
283 /// marker intrinsics.
285 class MemoryUseIntrinsic : public IntrinsicInst {
288 // Methods for support type inquiry through isa, cast, and dyn_cast:
289 static inline bool classof(const MemoryUseIntrinsic *) { return true; }
290 static inline bool classof(const IntrinsicInst *I) {
291 switch (I->getIntrinsicID()) {
292 case Intrinsic::lifetime_start:
293 case Intrinsic::lifetime_end:
294 case Intrinsic::invariant_start:
295 case Intrinsic::invariant_end:
297 default: return false;
300 static inline bool classof(const Value *V) {
301 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));