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"
31 #include "llvm/System/IncludeFile.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
43 /// StripPointerCasts - This static method strips off any unneeded pointer
44 /// casts from the specified value, returning the original uncasted value.
45 /// Note that the returned value is guaranteed to have pointer type.
46 static Value *StripPointerCasts(Value *Ptr);
48 /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
50 Intrinsic::ID getIntrinsicID() const {
51 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
54 // Methods for support type inquiry through isa, cast, and dyn_cast:
55 static inline bool classof(const IntrinsicInst *) { return true; }
56 static inline bool classof(const CallInst *I) {
57 if (const Function *CF = I->getCalledFunction())
58 return CF->getIntrinsicID() != 0;
61 static inline bool classof(const Value *V) {
62 return isa<CallInst>(V) && classof(cast<CallInst>(V));
66 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
68 struct DbgInfoIntrinsic : public IntrinsicInst {
70 // Methods for support type inquiry through isa, cast, and dyn_cast:
71 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
72 static inline bool classof(const IntrinsicInst *I) {
73 switch (I->getIntrinsicID()) {
74 case Intrinsic::dbg_stoppoint:
75 case Intrinsic::dbg_func_start:
76 case Intrinsic::dbg_region_start:
77 case Intrinsic::dbg_region_end:
78 case Intrinsic::dbg_declare:
80 default: return false;
83 static inline bool classof(const Value *V) {
84 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
87 static Value *StripCast(Value *C);
90 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
92 struct DbgStopPointInst : public DbgInfoIntrinsic {
93 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
94 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
95 Value *getContext() const {
96 return StripCast(getOperand(3));
99 unsigned getLine() const {
100 return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
102 unsigned getColumn() const {
103 return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
106 std::string getFileName() const;
107 std::string getDirectory() const;
109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool classof(const DbgStopPointInst *) { return true; }
111 static inline bool classof(const IntrinsicInst *I) {
112 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
114 static inline bool classof(const Value *V) {
115 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
119 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
121 struct DbgFuncStartInst : public DbgInfoIntrinsic {
122 Value *getSubprogram() const { return StripCast(getOperand(1)); }
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const DbgFuncStartInst *) { return true; }
126 static inline bool classof(const IntrinsicInst *I) {
127 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
129 static inline bool classof(const Value *V) {
130 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
134 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
136 struct DbgRegionStartInst : public DbgInfoIntrinsic {
137 Value *getContext() const { return StripCast(getOperand(1)); }
139 // Methods for support type inquiry through isa, cast, and dyn_cast:
140 static inline bool classof(const DbgRegionStartInst *) { return true; }
141 static inline bool classof(const IntrinsicInst *I) {
142 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
144 static inline bool classof(const Value *V) {
145 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
149 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
151 struct DbgRegionEndInst : public DbgInfoIntrinsic {
152 Value *getContext() const { return StripCast(getOperand(1)); }
154 // Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const DbgRegionEndInst *) { return true; }
156 static inline bool classof(const IntrinsicInst *I) {
157 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
159 static inline bool classof(const Value *V) {
160 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
164 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
166 struct DbgDeclareInst : public DbgInfoIntrinsic {
167 Value *getAddress() const { return getOperand(1); }
168 Value *getVariable() const { return StripCast(getOperand(2)); }
170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const DbgDeclareInst *) { return true; }
172 static inline bool classof(const IntrinsicInst *I) {
173 return I->getIntrinsicID() == Intrinsic::dbg_declare;
175 static inline bool classof(const Value *V) {
176 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
180 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
182 struct MemIntrinsic : public IntrinsicInst {
183 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
185 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
186 ConstantInt *getAlignment() const {
187 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
190 /// getDest - This is just like getRawDest, but it strips off any cast
191 /// instructions that feed it, giving the original input. The returned
192 /// value is guaranteed to be a pointer.
193 Value *getDest() const { return StripPointerCasts(getRawDest()); }
195 /// set* - Set the specified arguments of the instruction.
197 void setDest(Value *Ptr) {
198 assert(getRawDest()->getType() == Ptr->getType() &&
199 "setDest called with pointer of wrong type!");
203 void setLength(Value *L) {
204 assert(getLength()->getType() == L->getType() &&
205 "setLength called with value of wrong type!");
208 void setAlignment(ConstantInt *A) {
209 assert(getAlignment()->getType() == A->getType() &&
210 "setAlignment called with value of wrong type!");
214 // Methods for support type inquiry through isa, cast, and dyn_cast:
215 static inline bool classof(const MemIntrinsic *) { return true; }
216 static inline bool classof(const IntrinsicInst *I) {
217 switch (I->getIntrinsicID()) {
218 case Intrinsic::memcpy_i32:
219 case Intrinsic::memcpy_i64:
220 case Intrinsic::memmove_i32:
221 case Intrinsic::memmove_i64:
222 case Intrinsic::memset_i32:
223 case Intrinsic::memset_i64:
225 default: return false;
228 static inline bool classof(const Value *V) {
229 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
234 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
236 struct MemCpyInst : public MemIntrinsic {
237 /// get* - Return the arguments to the instruction.
239 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
241 /// getSource - This is just like getRawSource, but it strips off any cast
242 /// instructions that feed it, giving the original input. The returned
243 /// value is guaranteed to be a pointer.
244 Value *getSource() const { return StripPointerCasts(getRawSource()); }
247 void setSource(Value *Ptr) {
248 assert(getRawSource()->getType() == Ptr->getType() &&
249 "setSource called with pointer of wrong type!");
253 // Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const MemCpyInst *) { return true; }
255 static inline bool classof(const IntrinsicInst *I) {
256 return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
257 I->getIntrinsicID() == Intrinsic::memcpy_i64;
259 static inline bool classof(const Value *V) {
260 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
264 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
266 struct MemMoveInst : public MemIntrinsic {
267 /// get* - Return the arguments to the instruction.
269 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
271 /// getSource - This is just like getRawSource, but it strips off any cast
272 /// instructions that feed it, giving the original input. The returned
273 /// value is guaranteed to be a pointer.
274 Value *getSource() const { return StripPointerCasts(getRawSource()); }
276 void setSource(Value *Ptr) {
277 assert(getRawSource()->getType() == Ptr->getType() &&
278 "setSource called with pointer of wrong type!");
282 // Methods for support type inquiry through isa, cast, and dyn_cast:
283 static inline bool classof(const MemMoveInst *) { return true; }
284 static inline bool classof(const IntrinsicInst *I) {
285 return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
286 I->getIntrinsicID() == Intrinsic::memmove_i64;
288 static inline bool classof(const Value *V) {
289 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
293 /// MemSetInst - This class wraps the llvm.memset intrinsic.
295 struct MemSetInst : public MemIntrinsic {
296 /// get* - Return the arguments to the instruction.
298 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
300 void setValue(Value *Val) {
301 assert(getValue()->getType() == Val->getType() &&
302 "setSource called with pointer of wrong type!");
306 // Methods for support type inquiry through isa, cast, and dyn_cast:
307 static inline bool classof(const MemSetInst *) { return true; }
308 static inline bool classof(const IntrinsicInst *I) {
309 return I->getIntrinsicID() == Intrinsic::memset_i32 ||
310 I->getIntrinsicID() == Intrinsic::memset_i64;
312 static inline bool classof(const Value *V) {
313 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
319 // Ensure that the IntrinsicInst.cpp file gets added as a dependency of any
320 // file that includes this header
321 FORCE_DEFINING_FILE_TO_BE_LINKED(IntrinsicInst)