1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
42 /// StripPointerCasts - This static method strips off any unneeded pointer
43 /// casts from the specified value, returning the original uncasted value.
44 /// Note that the returned value is guaranteed to have pointer type.
45 static Value *StripPointerCasts(Value *Ptr);
47 /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
49 Intrinsic::ID getIntrinsicID() const {
50 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
53 // Methods for support type inquiry through isa, cast, and dyn_cast:
54 static inline bool classof(const IntrinsicInst *) { return true; }
55 static inline bool classof(const CallInst *I) {
56 if (const Function *CF = I->getCalledFunction())
57 return CF->getIntrinsicID() != 0;
60 static inline bool classof(const Value *V) {
61 return isa<CallInst>(V) && classof(cast<CallInst>(V));
65 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
67 struct DbgInfoIntrinsic : public IntrinsicInst {
69 // Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
71 static inline bool classof(const IntrinsicInst *I) {
72 switch (I->getIntrinsicID()) {
73 case Intrinsic::dbg_stoppoint:
74 case Intrinsic::dbg_func_start:
75 case Intrinsic::dbg_region_start:
76 case Intrinsic::dbg_region_end:
77 case Intrinsic::dbg_declare:
79 default: return false;
82 static inline bool classof(const Value *V) {
83 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
86 static Value *StripCast(Value *C);
89 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
91 struct DbgStopPointInst : public DbgInfoIntrinsic {
92 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
93 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
94 Value *getContext() const {
95 return StripCast(getOperand(3));
98 unsigned getLine() const {
99 return unsigned(cast<ConstantInt>(getOperand(1))->getRawValue());
101 unsigned getColumn() const {
102 return unsigned(cast<ConstantInt>(getOperand(2))->getRawValue());
105 std::string getFileName() const;
106 std::string getDirectory() const;
108 // Methods for support type inquiry through isa, cast, and dyn_cast:
109 static inline bool classof(const DbgStopPointInst *) { return true; }
110 static inline bool classof(const IntrinsicInst *I) {
111 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
113 static inline bool classof(const Value *V) {
114 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
118 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
120 struct DbgFuncStartInst : public DbgInfoIntrinsic {
121 Value *getSubprogram() const { return StripCast(getOperand(1)); }
123 // Methods for support type inquiry through isa, cast, and dyn_cast:
124 static inline bool classof(const DbgFuncStartInst *) { return true; }
125 static inline bool classof(const IntrinsicInst *I) {
126 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
128 static inline bool classof(const Value *V) {
129 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
133 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
135 struct DbgRegionStartInst : public DbgInfoIntrinsic {
136 Value *getContext() const { return StripCast(getOperand(1)); }
138 // Methods for support type inquiry through isa, cast, and dyn_cast:
139 static inline bool classof(const DbgRegionStartInst *) { return true; }
140 static inline bool classof(const IntrinsicInst *I) {
141 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
143 static inline bool classof(const Value *V) {
144 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
148 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
150 struct DbgRegionEndInst : public DbgInfoIntrinsic {
151 Value *getContext() const { return StripCast(getOperand(1)); }
153 // Methods for support type inquiry through isa, cast, and dyn_cast:
154 static inline bool classof(const DbgRegionEndInst *) { return true; }
155 static inline bool classof(const IntrinsicInst *I) {
156 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
158 static inline bool classof(const Value *V) {
159 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
163 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
165 struct DbgDeclareInst : public DbgInfoIntrinsic {
166 Value *getAddress() const { return getOperand(1); }
167 Value *getVariable() const { return StripCast(getOperand(2)); }
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const DbgDeclareInst *) { return true; }
171 static inline bool classof(const IntrinsicInst *I) {
172 return I->getIntrinsicID() == Intrinsic::dbg_declare;
174 static inline bool classof(const Value *V) {
175 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
179 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
181 struct MemIntrinsic : public IntrinsicInst {
182 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
184 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
185 ConstantInt *getAlignment() const {
186 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
189 /// getDest - This is just like getRawDest, but it strips off any cast
190 /// instructions that feed it, giving the original input. The returned
191 /// value is guaranteed to be a pointer.
192 Value *getDest() const { return StripPointerCasts(getRawDest()); }
194 /// set* - Set the specified arguments of the instruction.
196 void setDest(Value *Ptr) {
197 assert(getRawDest()->getType() == Ptr->getType() &&
198 "setDest called with pointer of wrong type!");
202 void setLength(Value *L) {
203 assert(getLength()->getType() == L->getType() &&
204 "setLength called with value of wrong type!");
207 void setAlignment(ConstantInt *A) {
208 assert(getAlignment()->getType() == A->getType() &&
209 "setAlignment called with value of wrong type!");
213 // Methods for support type inquiry through isa, cast, and dyn_cast:
214 static inline bool classof(const MemIntrinsic *) { return true; }
215 static inline bool classof(const IntrinsicInst *I) {
216 switch (I->getIntrinsicID()) {
217 case Intrinsic::memcpy_i32:
218 case Intrinsic::memcpy_i64:
219 case Intrinsic::memmove_i32:
220 case Intrinsic::memmove_i64:
221 case Intrinsic::memset_i32:
222 case Intrinsic::memset_i64:
224 default: return false;
227 static inline bool classof(const Value *V) {
228 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
233 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
235 struct MemCpyInst : public MemIntrinsic {
236 /// get* - Return the arguments to the instruction.
238 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
240 /// getSource - This is just like getRawSource, but it strips off any cast
241 /// instructions that feed it, giving the original input. The returned
242 /// value is guaranteed to be a pointer.
243 Value *getSource() const { return StripPointerCasts(getRawSource()); }
246 void setSource(Value *Ptr) {
247 assert(getRawSource()->getType() == Ptr->getType() &&
248 "setSource called with pointer of wrong type!");
252 // Methods for support type inquiry through isa, cast, and dyn_cast:
253 static inline bool classof(const MemCpyInst *) { return true; }
254 static inline bool classof(const IntrinsicInst *I) {
255 return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
256 I->getIntrinsicID() == Intrinsic::memcpy_i64;
258 static inline bool classof(const Value *V) {
259 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
263 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
265 struct MemMoveInst : public MemIntrinsic {
266 /// get* - Return the arguments to the instruction.
268 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
270 /// getSource - This is just like getRawSource, but it strips off any cast
271 /// instructions that feed it, giving the original input. The returned
272 /// value is guaranteed to be a pointer.
273 Value *getSource() const { return StripPointerCasts(getRawSource()); }
275 void setSource(Value *Ptr) {
276 assert(getRawSource()->getType() == Ptr->getType() &&
277 "setSource called with pointer of wrong type!");
281 // Methods for support type inquiry through isa, cast, and dyn_cast:
282 static inline bool classof(const MemMoveInst *) { return true; }
283 static inline bool classof(const IntrinsicInst *I) {
284 return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
285 I->getIntrinsicID() == Intrinsic::memmove_i64;
287 static inline bool classof(const Value *V) {
288 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
292 /// MemSetInst - This class wraps the llvm.memset intrinsic.
294 struct MemSetInst : public MemIntrinsic {
295 /// get* - Return the arguments to the instruction.
297 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
299 void setValue(Value *Val) {
300 assert(getValue()->getType() == Val->getType() &&
301 "setSource called with pointer of wrong type!");
305 // Methods for support type inquiry through isa, cast, and dyn_cast:
306 static inline bool classof(const MemSetInst *) { return true; }
307 static inline bool classof(const IntrinsicInst *I) {
308 return I->getIntrinsicID() == Intrinsic::memset_i32 ||
309 I->getIntrinsicID() == Intrinsic::memset_i64;
311 static inline bool classof(const Value *V) {
312 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));