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 Value *getChain() const { return const_cast<Value*>(getOperand(1)); }
71 // Methods for support type inquiry through isa, cast, and dyn_cast:
72 static inline bool classof(const DbgInfoIntrinsic *) { return true; }
73 static inline bool classof(const IntrinsicInst *I) {
74 switch (I->getIntrinsicID()) {
75 case Intrinsic::dbg_stoppoint:
76 case Intrinsic::dbg_region_start:
77 case Intrinsic::dbg_region_end:
78 case Intrinsic::dbg_func_start:
80 default: return false;
83 static inline bool classof(const Value *V) {
84 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
89 /// DbgStopPointInst - This represent llvm.dbg.stoppoint instructions.
91 struct DbgStopPointInst : public DbgInfoIntrinsic {
93 unsigned getLineNo() const {
94 return unsigned(cast<ConstantInt>(getOperand(2))->getRawValue());
96 unsigned getColNo() const {
97 return unsigned(cast<ConstantInt>(getOperand(3))->getRawValue());
99 Value *getContext() const { return const_cast<Value*>(getOperand(4)); }
102 // Methods for support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const DbgStopPointInst *) { return true; }
104 static inline bool classof(const IntrinsicInst *I) {
105 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
107 static inline bool classof(const Value *V) {
108 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
112 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
114 struct MemIntrinsic : public IntrinsicInst {
115 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
117 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
118 ConstantInt *getAlignment() const {
119 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
122 /// getDest - This is just like getRawDest, but it strips off any cast
123 /// instructions that feed it, giving the original input. The returned
124 /// value is guaranteed to be a pointer.
125 Value *getDest() const { return StripPointerCasts(getRawDest()); }
127 /// set* - Set the specified arguments of the instruction.
129 void setDest(Value *Ptr) {
130 assert(getRawDest()->getType() == Ptr->getType() &&
131 "setDest called with pointer of wrong type!");
135 void setLength(Value *L) {
136 assert(getLength()->getType() == L->getType() &&
137 "setLength called with value of wrong type!");
140 void setAlignment(ConstantInt *A) {
141 assert(getAlignment()->getType() == A->getType() &&
142 "setAlignment called with value of wrong type!");
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const MemIntrinsic *) { return true; }
148 static inline bool classof(const IntrinsicInst *I) {
149 switch (I->getIntrinsicID()) {
150 case Intrinsic::memcpy_i32:
151 case Intrinsic::memcpy_i64:
152 case Intrinsic::memmove_i32:
153 case Intrinsic::memmove_i64:
154 case Intrinsic::memset_i32:
155 case Intrinsic::memset_i64:
157 default: return false;
160 static inline bool classof(const Value *V) {
161 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
166 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
168 struct MemCpyInst : public MemIntrinsic {
169 /// get* - Return the arguments to the instruction.
171 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
173 /// getSource - This is just like getRawSource, but it strips off any cast
174 /// instructions that feed it, giving the original input. The returned
175 /// value is guaranteed to be a pointer.
176 Value *getSource() const { return StripPointerCasts(getRawSource()); }
179 void setSource(Value *Ptr) {
180 assert(getRawSource()->getType() == Ptr->getType() &&
181 "setSource called with pointer of wrong type!");
185 // Methods for support type inquiry through isa, cast, and dyn_cast:
186 static inline bool classof(const MemCpyInst *) { return true; }
187 static inline bool classof(const IntrinsicInst *I) {
188 return I->getIntrinsicID() == Intrinsic::memcpy_i32 ||
189 I->getIntrinsicID() == Intrinsic::memcpy_i64;
191 static inline bool classof(const Value *V) {
192 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
196 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
198 struct MemMoveInst : public MemIntrinsic {
199 /// get* - Return the arguments to the instruction.
201 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
203 /// getSource - This is just like getRawSource, but it strips off any cast
204 /// instructions that feed it, giving the original input. The returned
205 /// value is guaranteed to be a pointer.
206 Value *getSource() const { return StripPointerCasts(getRawSource()); }
208 void setSource(Value *Ptr) {
209 assert(getRawSource()->getType() == Ptr->getType() &&
210 "setSource called with pointer of wrong type!");
214 // Methods for support type inquiry through isa, cast, and dyn_cast:
215 static inline bool classof(const MemMoveInst *) { return true; }
216 static inline bool classof(const IntrinsicInst *I) {
217 return I->getIntrinsicID() == Intrinsic::memmove_i32 ||
218 I->getIntrinsicID() == Intrinsic::memmove_i64;
220 static inline bool classof(const Value *V) {
221 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
225 /// MemSetInst - This class wraps the llvm.memset intrinsic.
227 struct MemSetInst : public MemIntrinsic {
228 /// get* - Return the arguments to the instruction.
230 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
232 void setValue(Value *Val) {
233 assert(getValue()->getType() == Val->getType() &&
234 "setSource called with pointer of wrong type!");
238 // Methods for support type inquiry through isa, cast, and dyn_cast:
239 static inline bool classof(const MemSetInst *) { return true; }
240 static inline bool classof(const IntrinsicInst *I) {
241 return I->getIntrinsicID() == Intrinsic::memset_i32 ||
242 I->getIntrinsicID() == Intrinsic::memset_i64;
244 static inline bool classof(const Value *V) {
245 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));