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_IR_INTRINSICINST_H
25 #define LLVM_IR_INTRINSICINST_H
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/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() LLVM_DELETED_FUNCTION;
38 IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
39 void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
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 CallInst *I) {
49 if (const Function *CF = I->getCalledFunction())
50 return CF->isIntrinsic();
53 static inline bool classof(const Value *V) {
54 return isa<CallInst>(V) && classof(cast<CallInst>(V));
58 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
60 class DbgInfoIntrinsic : public IntrinsicInst {
63 // Methods for support type inquiry through isa, cast, and dyn_cast:
64 static inline bool classof(const IntrinsicInst *I) {
65 switch (I->getIntrinsicID()) {
66 case Intrinsic::dbg_declare:
67 case Intrinsic::dbg_value:
69 default: return false;
72 static inline bool classof(const Value *V) {
73 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
76 static Value *StripCast(Value *C);
79 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
81 class DbgDeclareInst : public DbgInfoIntrinsic {
83 Value *getAddress() const;
84 MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
86 // Methods for support type inquiry through isa, cast, and dyn_cast:
87 static inline bool classof(const IntrinsicInst *I) {
88 return I->getIntrinsicID() == Intrinsic::dbg_declare;
90 static inline bool classof(const Value *V) {
91 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
95 /// DbgValueInst - This represents the llvm.dbg.value instruction.
97 class DbgValueInst : public DbgInfoIntrinsic {
99 const Value *getValue() const;
101 uint64_t getOffset() const {
102 return cast<ConstantInt>(
103 const_cast<Value*>(getArgOperand(1)))->getZExtValue();
105 MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
107 // Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const IntrinsicInst *I) {
109 return I->getIntrinsicID() == Intrinsic::dbg_value;
111 static inline bool classof(const Value *V) {
112 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
116 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
118 class MemIntrinsic : public IntrinsicInst {
120 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
121 const Use &getRawDestUse() const { return getArgOperandUse(0); }
122 Use &getRawDestUse() { return getArgOperandUse(0); }
124 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
125 const Use &getLengthUse() const { return getArgOperandUse(2); }
126 Use &getLengthUse() { return getArgOperandUse(2); }
128 ConstantInt *getAlignmentCst() const {
129 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
132 unsigned getAlignment() const {
133 return getAlignmentCst()->getZExtValue();
136 ConstantInt *getVolatileCst() const {
137 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
139 bool isVolatile() const {
140 return !getVolatileCst()->isZero();
143 unsigned getDestAddressSpace() const {
144 return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
147 /// getDest - This is just like getRawDest, but it strips off any cast
148 /// instructions that feed it, giving the original input. The returned
149 /// value is guaranteed to be a pointer.
150 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
152 /// set* - Set the specified arguments of the instruction.
154 void setDest(Value *Ptr) {
155 assert(getRawDest()->getType() == Ptr->getType() &&
156 "setDest called with pointer of wrong type!");
157 setArgOperand(0, Ptr);
160 void setLength(Value *L) {
161 assert(getLength()->getType() == L->getType() &&
162 "setLength called with value of wrong type!");
166 void setAlignment(Constant* A) {
170 void setVolatile(Constant* V) {
174 Type *getAlignmentType() const {
175 return getArgOperand(3)->getType();
178 // Methods for support type inquiry through isa, cast, and dyn_cast:
179 static inline bool classof(const IntrinsicInst *I) {
180 switch (I->getIntrinsicID()) {
181 case Intrinsic::memcpy:
182 case Intrinsic::memmove:
183 case Intrinsic::memset:
185 default: return false;
188 static inline bool classof(const Value *V) {
189 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
193 /// MemSetInst - This class wraps the llvm.memset intrinsic.
195 class MemSetInst : public MemIntrinsic {
197 /// get* - Return the arguments to the instruction.
199 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
200 const Use &getValueUse() const { return getArgOperandUse(1); }
201 Use &getValueUse() { return getArgOperandUse(1); }
203 void setValue(Value *Val) {
204 assert(getValue()->getType() == Val->getType() &&
205 "setValue called with value of wrong type!");
206 setArgOperand(1, Val);
209 // Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const IntrinsicInst *I) {
211 return I->getIntrinsicID() == Intrinsic::memset;
213 static inline bool classof(const Value *V) {
214 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
218 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
220 class MemTransferInst : public MemIntrinsic {
222 /// get* - Return the arguments to the instruction.
224 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
225 const Use &getRawSourceUse() const { return getArgOperandUse(1); }
226 Use &getRawSourceUse() { return getArgOperandUse(1); }
228 /// getSource - This is just like getRawSource, but it strips off any cast
229 /// instructions that feed it, giving the original input. The returned
230 /// value is guaranteed to be a pointer.
231 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
233 unsigned getSourceAddressSpace() const {
234 return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
237 void setSource(Value *Ptr) {
238 assert(getRawSource()->getType() == Ptr->getType() &&
239 "setSource called with pointer of wrong type!");
240 setArgOperand(1, Ptr);
243 // Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const IntrinsicInst *I) {
245 return I->getIntrinsicID() == Intrinsic::memcpy ||
246 I->getIntrinsicID() == Intrinsic::memmove;
248 static inline bool classof(const Value *V) {
249 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
254 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
256 class MemCpyInst : public MemTransferInst {
258 // Methods for support type inquiry through isa, cast, and dyn_cast:
259 static inline bool classof(const IntrinsicInst *I) {
260 return I->getIntrinsicID() == Intrinsic::memcpy;
262 static inline bool classof(const Value *V) {
263 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
267 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
269 class MemMoveInst : public MemTransferInst {
271 // Methods for support type inquiry through isa, cast, and dyn_cast:
272 static inline bool classof(const IntrinsicInst *I) {
273 return I->getIntrinsicID() == Intrinsic::memmove;
275 static inline bool classof(const Value *V) {
276 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
280 /// VAStartInst - This represents the llvm.va_start intrinsic.
282 class VAStartInst : public IntrinsicInst {
284 static inline bool classof(const IntrinsicInst *I) {
285 return I->getIntrinsicID() == Intrinsic::vastart;
287 static inline bool classof(const Value *V) {
288 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
291 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
294 /// VAEndInst - This represents the llvm.va_end intrinsic.
296 class VAEndInst : public IntrinsicInst {
298 static inline bool classof(const IntrinsicInst *I) {
299 return I->getIntrinsicID() == Intrinsic::vaend;
301 static inline bool classof(const Value *V) {
302 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
305 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
308 /// VACopyInst - This represents the llvm.va_copy intrinsic.
310 class VACopyInst : public IntrinsicInst {
312 static inline bool classof(const IntrinsicInst *I) {
313 return I->getIntrinsicID() == Intrinsic::vacopy;
315 static inline bool classof(const Value *V) {
316 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
319 Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
320 Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }