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"
31 #include "llvm/IR/Metadata.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() LLVM_DELETED_FUNCTION;
39 IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
40 void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
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 CallInst *I) {
50 if (const Function *CF = I->getCalledFunction())
51 return CF->isIntrinsic();
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 IntrinsicInst *I) {
66 switch (I->getIntrinsicID()) {
67 case Intrinsic::dbg_declare:
68 case Intrinsic::dbg_value:
70 default: return false;
73 static inline bool classof(const Value *V) {
74 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
77 static Value *StripCast(Value *C);
80 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
82 class DbgDeclareInst : public DbgInfoIntrinsic {
84 Value *getAddress() const;
85 MDNode *getVariable() const {
87 cast<MetadataAsValue>(getArgOperand(1))->getMetadata());
89 MDNode *getExpression() const {
91 cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const IntrinsicInst *I) {
96 return I->getIntrinsicID() == Intrinsic::dbg_declare;
98 static inline bool classof(const Value *V) {
99 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
103 /// DbgValueInst - This represents the llvm.dbg.value instruction.
105 class DbgValueInst : public DbgInfoIntrinsic {
107 const Value *getValue() const;
109 uint64_t getOffset() const {
110 return cast<ConstantInt>(
111 const_cast<Value*>(getArgOperand(1)))->getZExtValue();
113 MDNode *getVariable() const {
115 cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
117 MDNode *getExpression() const {
119 cast<MetadataAsValue>(getArgOperand(3))->getMetadata());
122 // Methods for support type inquiry through isa, cast, and dyn_cast:
123 static inline bool classof(const IntrinsicInst *I) {
124 return I->getIntrinsicID() == Intrinsic::dbg_value;
126 static inline bool classof(const Value *V) {
127 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
131 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
133 class MemIntrinsic : public IntrinsicInst {
135 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
136 const Use &getRawDestUse() const { return getArgOperandUse(0); }
137 Use &getRawDestUse() { return getArgOperandUse(0); }
139 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
140 const Use &getLengthUse() const { return getArgOperandUse(2); }
141 Use &getLengthUse() { return getArgOperandUse(2); }
143 ConstantInt *getAlignmentCst() const {
144 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
147 unsigned getAlignment() const {
148 return getAlignmentCst()->getZExtValue();
151 ConstantInt *getVolatileCst() const {
152 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
154 bool isVolatile() const {
155 return !getVolatileCst()->isZero();
158 unsigned getDestAddressSpace() const {
159 return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
162 /// getDest - This is just like getRawDest, but it strips off any cast
163 /// instructions that feed it, giving the original input. The returned
164 /// value is guaranteed to be a pointer.
165 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
167 /// set* - Set the specified arguments of the instruction.
169 void setDest(Value *Ptr) {
170 assert(getRawDest()->getType() == Ptr->getType() &&
171 "setDest called with pointer of wrong type!");
172 setArgOperand(0, Ptr);
175 void setLength(Value *L) {
176 assert(getLength()->getType() == L->getType() &&
177 "setLength called with value of wrong type!");
181 void setAlignment(Constant* A) {
185 void setVolatile(Constant* V) {
189 Type *getAlignmentType() const {
190 return getArgOperand(3)->getType();
193 // Methods for support type inquiry through isa, cast, and dyn_cast:
194 static inline bool classof(const IntrinsicInst *I) {
195 switch (I->getIntrinsicID()) {
196 case Intrinsic::memcpy:
197 case Intrinsic::memmove:
198 case Intrinsic::memset:
200 default: return false;
203 static inline bool classof(const Value *V) {
204 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
208 /// MemSetInst - This class wraps the llvm.memset intrinsic.
210 class MemSetInst : public MemIntrinsic {
212 /// get* - Return the arguments to the instruction.
214 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
215 const Use &getValueUse() const { return getArgOperandUse(1); }
216 Use &getValueUse() { return getArgOperandUse(1); }
218 void setValue(Value *Val) {
219 assert(getValue()->getType() == Val->getType() &&
220 "setValue called with value of wrong type!");
221 setArgOperand(1, Val);
224 // Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const IntrinsicInst *I) {
226 return I->getIntrinsicID() == Intrinsic::memset;
228 static inline bool classof(const Value *V) {
229 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
233 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
235 class MemTransferInst : public MemIntrinsic {
237 /// get* - Return the arguments to the instruction.
239 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
240 const Use &getRawSourceUse() const { return getArgOperandUse(1); }
241 Use &getRawSourceUse() { return getArgOperandUse(1); }
243 /// getSource - This is just like getRawSource, but it strips off any cast
244 /// instructions that feed it, giving the original input. The returned
245 /// value is guaranteed to be a pointer.
246 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
248 unsigned getSourceAddressSpace() const {
249 return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
252 void setSource(Value *Ptr) {
253 assert(getRawSource()->getType() == Ptr->getType() &&
254 "setSource called with pointer of wrong type!");
255 setArgOperand(1, Ptr);
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 ||
261 I->getIntrinsicID() == Intrinsic::memmove;
263 static inline bool classof(const Value *V) {
264 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
269 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
271 class MemCpyInst : public MemTransferInst {
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const IntrinsicInst *I) {
275 return I->getIntrinsicID() == Intrinsic::memcpy;
277 static inline bool classof(const Value *V) {
278 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
282 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
284 class MemMoveInst : public MemTransferInst {
286 // Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const IntrinsicInst *I) {
288 return I->getIntrinsicID() == Intrinsic::memmove;
290 static inline bool classof(const Value *V) {
291 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
295 /// VAStartInst - This represents the llvm.va_start intrinsic.
297 class VAStartInst : public IntrinsicInst {
299 static inline bool classof(const IntrinsicInst *I) {
300 return I->getIntrinsicID() == Intrinsic::vastart;
302 static inline bool classof(const Value *V) {
303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
306 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
309 /// VAEndInst - This represents the llvm.va_end intrinsic.
311 class VAEndInst : public IntrinsicInst {
313 static inline bool classof(const IntrinsicInst *I) {
314 return I->getIntrinsicID() == Intrinsic::vaend;
316 static inline bool classof(const Value *V) {
317 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
320 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
323 /// VACopyInst - This represents the llvm.va_copy intrinsic.
325 class VACopyInst : public IntrinsicInst {
327 static inline bool classof(const IntrinsicInst *I) {
328 return I->getIntrinsicID() == Intrinsic::vacopy;
330 static inline bool classof(const Value *V) {
331 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
334 Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
335 Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
338 /// This represents the llvm.instrprof_increment intrinsic.
339 class InstrProfIncrementInst : public IntrinsicInst {
341 static inline bool classof(const IntrinsicInst *I) {
342 return I->getIntrinsicID() == Intrinsic::instrprof_increment;
344 static inline bool classof(const Value *V) {
345 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
348 GlobalVariable *getName() const {
349 return cast<GlobalVariable>(
350 const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
353 ConstantInt *getHash() const {
354 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
357 ConstantInt *getNumCounters() const {
358 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
361 ConstantInt *getIndex() const {
362 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));