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"
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
42 static void destroyThis(IntrinsicInst* v) {
43 CallInst::destroyThis(v);
48 /// StripPointerCasts - This static method strips off any unneeded pointer
49 /// casts from the specified value, returning the original uncasted value.
50 /// Note that the returned value is guaranteed to have pointer type.
51 static Value *StripPointerCasts(Value *Ptr);
53 /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
55 Intrinsic::ID getIntrinsicID() const {
56 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
59 // Methods for support type inquiry through isa, cast, and dyn_cast:
60 static inline bool classof(const IntrinsicInst *) { return true; }
61 static inline bool classof(const CallInst *I) {
62 if (const Function *CF = I->getCalledFunction())
63 return CF->getIntrinsicID() != 0;
66 static inline bool classof(const Value *V) {
67 return isa<CallInst>(V) && classof(cast<CallInst>(V));
71 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
73 struct DbgInfoIntrinsic : public IntrinsicInst {
75 // Methods for support type inquiry through isa, cast, and dyn_cast:
76 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
77 static inline bool classof(const IntrinsicInst *I) {
78 switch (I->getIntrinsicID()) {
79 case Intrinsic::dbg_stoppoint:
80 case Intrinsic::dbg_func_start:
81 case Intrinsic::dbg_region_start:
82 case Intrinsic::dbg_region_end:
83 case Intrinsic::dbg_declare:
85 default: return false;
88 static inline bool classof(const Value *V) {
89 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
92 static Value *StripCast(Value *C);
94 static void destroyThis(DbgInfoIntrinsic* v) {
95 IntrinsicInst::destroyThis(v);
100 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
102 struct DbgStopPointInst : public DbgInfoIntrinsic {
103 Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
104 Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
105 Value *getContext() const {
106 return StripCast(getOperand(3));
109 unsigned getLine() const {
110 return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
112 unsigned getColumn() const {
113 return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
116 std::string getFileName() const;
117 std::string getDirectory() const;
119 // Methods for support type inquiry through isa, cast, and dyn_cast:
120 static inline bool classof(const DbgStopPointInst *) { return true; }
121 static inline bool classof(const IntrinsicInst *I) {
122 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
124 static inline bool classof(const Value *V) {
125 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
128 static void destroyThis(DbgStopPointInst* v) {
129 DbgInfoIntrinsic::destroyThis(v);
134 /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
136 struct DbgFuncStartInst : public DbgInfoIntrinsic {
137 Value *getSubprogram() const { return StripCast(getOperand(1)); }
139 // Methods for support type inquiry through isa, cast, and dyn_cast:
140 static inline bool classof(const DbgFuncStartInst *) { return true; }
141 static inline bool classof(const IntrinsicInst *I) {
142 return I->getIntrinsicID() == Intrinsic::dbg_func_start;
144 static inline bool classof(const Value *V) {
145 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
148 static void destroyThis(DbgFuncStartInst* v) {
149 DbgInfoIntrinsic::destroyThis(v);
154 /// DbgRegionStartInst - This represents the llvm.dbg.region.start
156 struct DbgRegionStartInst : public DbgInfoIntrinsic {
157 Value *getContext() const { return StripCast(getOperand(1)); }
159 // Methods for support type inquiry through isa, cast, and dyn_cast:
160 static inline bool classof(const DbgRegionStartInst *) { return true; }
161 static inline bool classof(const IntrinsicInst *I) {
162 return I->getIntrinsicID() == Intrinsic::dbg_region_start;
164 static inline bool classof(const Value *V) {
165 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
168 static void destroyThis(DbgRegionStartInst* v) {
169 DbgInfoIntrinsic::destroyThis(v);
174 /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
176 struct DbgRegionEndInst : public DbgInfoIntrinsic {
177 Value *getContext() const { return StripCast(getOperand(1)); }
179 // Methods for support type inquiry through isa, cast, and dyn_cast:
180 static inline bool classof(const DbgRegionEndInst *) { return true; }
181 static inline bool classof(const IntrinsicInst *I) {
182 return I->getIntrinsicID() == Intrinsic::dbg_region_end;
184 static inline bool classof(const Value *V) {
185 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
189 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
191 struct DbgDeclareInst : public DbgInfoIntrinsic {
192 Value *getAddress() const { return getOperand(1); }
193 Value *getVariable() const { return StripCast(getOperand(2)); }
195 // Methods for support type inquiry through isa, cast, and dyn_cast:
196 static inline bool classof(const DbgDeclareInst *) { return true; }
197 static inline bool classof(const IntrinsicInst *I) {
198 return I->getIntrinsicID() == Intrinsic::dbg_declare;
200 static inline bool classof(const Value *V) {
201 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
204 static void destroyThis(DbgDeclareInst* v) {
205 DbgInfoIntrinsic::destroyThis(v);
210 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
212 struct MemIntrinsic : public IntrinsicInst {
213 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
215 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
216 ConstantInt *getAlignment() const {
217 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
220 /// getDest - This is just like getRawDest, but it strips off any cast
221 /// instructions that feed it, giving the original input. The returned
222 /// value is guaranteed to be a pointer.
223 Value *getDest() const { return StripPointerCasts(getRawDest()); }
225 /// set* - Set the specified arguments of the instruction.
227 void setDest(Value *Ptr) {
228 assert(getRawDest()->getType() == Ptr->getType() &&
229 "setDest called with pointer of wrong type!");
233 void setLength(Value *L) {
234 assert(getLength()->getType() == L->getType() &&
235 "setLength called with value of wrong type!");
238 void setAlignment(ConstantInt *A) {
239 assert(getAlignment()->getType() == A->getType() &&
240 "setAlignment called with value of wrong type!");
244 // Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const MemIntrinsic *) { return true; }
246 static inline bool classof(const IntrinsicInst *I) {
247 switch (I->getIntrinsicID()) {
248 case Intrinsic::memcpy_i32:
249 case Intrinsic::memcpy_i64:
250 case Intrinsic::memmove_i32:
251 case Intrinsic::memmove_i64:
252 case Intrinsic::memset_i32:
253 case Intrinsic::memset_i64:
255 default: return false;
258 static inline bool classof(const Value *V) {
259 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
262 static void destroyThis(MemIntrinsic* v) {
263 IntrinsicInst::destroyThis(v);
269 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
271 struct MemCpyInst : public MemIntrinsic {
272 /// get* - Return the arguments to the instruction.
274 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
276 /// getSource - This is just like getRawSource, but it strips off any cast
277 /// instructions that feed it, giving the original input. The returned
278 /// value is guaranteed to be a pointer.
279 Value *getSource() const { return StripPointerCasts(getRawSource()); }
282 void setSource(Value *Ptr) {
283 assert(getRawSource()->getType() == Ptr->getType() &&
284 "setSource called with pointer of wrong type!");
288 // Methods for support type inquiry through isa, cast, and dyn_cast:
289 static inline bool classof(const MemCpyInst *) { return true; }
290 static inline bool classof(const IntrinsicInst *I) {
291 return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
292 I->getIntrinsicID() == Intrinsic::memcpy_i64;
294 static inline bool classof(const Value *V) {
295 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
298 static void destroyThis(MemCpyInst* v) {
299 MemIntrinsic::destroyThis(v);
304 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
306 struct MemMoveInst : public MemIntrinsic {
307 /// get* - Return the arguments to the instruction.
309 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
311 /// getSource - This is just like getRawSource, but it strips off any cast
312 /// instructions that feed it, giving the original input. The returned
313 /// value is guaranteed to be a pointer.
314 Value *getSource() const { return StripPointerCasts(getRawSource()); }
316 void setSource(Value *Ptr) {
317 assert(getRawSource()->getType() == Ptr->getType() &&
318 "setSource called with pointer of wrong type!");
322 // Methods for support type inquiry through isa, cast, and dyn_cast:
323 static inline bool classof(const MemMoveInst *) { return true; }
324 static inline bool classof(const IntrinsicInst *I) {
325 return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
326 I->getIntrinsicID() == Intrinsic::memmove_i64;
328 static inline bool classof(const Value *V) {
329 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
332 static void destroyThis(MemMoveInst* v) {
333 MemIntrinsic::destroyThis(v);
338 /// MemSetInst - This class wraps the llvm.memset intrinsic.
340 struct MemSetInst : public MemIntrinsic {
341 /// get* - Return the arguments to the instruction.
343 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
345 void setValue(Value *Val) {
346 assert(getValue()->getType() == Val->getType() &&
347 "setSource called with pointer of wrong type!");
351 // Methods for support type inquiry through isa, cast, and dyn_cast:
352 static inline bool classof(const MemSetInst *) { return true; }
353 static inline bool classof(const IntrinsicInst *I) {
354 return I->getIntrinsicID() == Intrinsic::memset_i32 ||
355 I->getIntrinsicID() == Intrinsic::memset_i64;
357 static inline bool classof(const Value *V) {
358 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
361 static void destroyThis(MemSetInst* v) {
362 MemIntrinsic::destroyThis(v);
369 // Ensure that the IntrinsicInst.cpp file gets added as a dependency of any
370 // file that includes this header
371 FORCE_DEFINING_FILE_TO_BE_LINKED(IntrinsicInst)