1 //===-- llvm/InstrinsicInst.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:
79 case Intrinsic::dbg_declare:
81 default: return false;
84 static inline bool classof(const Value *V) {
85 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
90 /// DbgStopPointInst - This represent llvm.dbg.stoppoint instructions.
92 struct DbgStopPointInst : public DbgInfoIntrinsic {
94 unsigned getLineNo() const {
95 return unsigned(cast<ConstantInt>(getOperand(2))->getRawValue());
97 unsigned getColNo() const {
98 return unsigned(cast<ConstantInt>(getOperand(3))->getRawValue());
100 Value *getContext() const { return const_cast<Value*>(getOperand(4)); }
103 // Methods for support type inquiry through isa, cast, and dyn_cast:
104 static inline bool classof(const DbgStopPointInst *) { return true; }
105 static inline bool classof(const IntrinsicInst *I) {
106 return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
108 static inline bool classof(const Value *V) {
109 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
113 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
115 struct MemIntrinsic : public IntrinsicInst {
116 Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
118 Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
119 ConstantInt *getAlignment() const {
120 return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
123 /// getDest - This is just like getRawDest, but it strips off any cast
124 /// instructions that feed it, giving the original input. The returned
125 /// value is guaranteed to be a pointer.
126 Value *getDest() const { return StripPointerCasts(getRawDest()); }
128 /// set* - Set the specified arguments of the instruction.
130 void setDest(Value *Ptr) {
131 assert(getRawDest()->getType() == Ptr->getType() &&
132 "setDest called with pointer of wrong type!");
136 void setLength(Value *L) {
137 assert(getLength()->getType() == L->getType() &&
138 "setLength called with value of wrong type!");
141 void setAlignment(ConstantInt *A) {
142 assert(getAlignment()->getType() == A->getType() &&
143 "setAlignment called with value of wrong type!");
147 // Methods for support type inquiry through isa, cast, and dyn_cast:
148 static inline bool classof(const MemIntrinsic *) { return true; }
149 static inline bool classof(const IntrinsicInst *I) {
150 switch (I->getIntrinsicID()) {
151 case Intrinsic::memcpy:
152 case Intrinsic::memmove:
153 case Intrinsic::memset:
155 default: return false;
158 static inline bool classof(const Value *V) {
159 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
164 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
166 struct MemCpyInst : public MemIntrinsic {
167 /// get* - Return the arguments to the instruction.
169 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
171 /// getSource - This is just like getRawSource, but it strips off any cast
172 /// instructions that feed it, giving the original input. The returned
173 /// value is guaranteed to be a pointer.
174 Value *getSource() const { return StripPointerCasts(getRawSource()); }
177 void setSource(Value *Ptr) {
178 assert(getRawSource()->getType() == Ptr->getType() &&
179 "setSource called with pointer of wrong type!");
183 // Methods for support type inquiry through isa, cast, and dyn_cast:
184 static inline bool classof(const MemCpyInst *) { return true; }
185 static inline bool classof(const IntrinsicInst *I) {
186 return I->getIntrinsicID() == Intrinsic::memcpy;
188 static inline bool classof(const Value *V) {
189 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
193 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
195 struct MemMoveInst : public MemIntrinsic {
196 /// get* - Return the arguments to the instruction.
198 Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
200 /// getSource - This is just like getRawSource, but it strips off any cast
201 /// instructions that feed it, giving the original input. The returned
202 /// value is guaranteed to be a pointer.
203 Value *getSource() const { return StripPointerCasts(getRawSource()); }
205 void setSource(Value *Ptr) {
206 assert(getRawSource()->getType() == Ptr->getType() &&
207 "setSource called with pointer of wrong type!");
211 // Methods for support type inquiry through isa, cast, and dyn_cast:
212 static inline bool classof(const MemMoveInst *) { return true; }
213 static inline bool classof(const IntrinsicInst *I) {
214 return I->getIntrinsicID() == Intrinsic::memmove;
216 static inline bool classof(const Value *V) {
217 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
221 /// MemSetInst - This class wraps the llvm.memcpy intrinsic.
223 struct MemSetInst : public MemIntrinsic {
224 /// get* - Return the arguments to the instruction.
226 Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
228 void setValue(Value *Val) {
229 assert(getValue()->getType() == Val->getType() &&
230 "setSource called with pointer of wrong type!");
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const MemSetInst *) { return true; }
236 static inline bool classof(const IntrinsicInst *I) {
237 return I->getIntrinsicID() == Intrinsic::memset;
239 static inline bool classof(const Value *V) {
240 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));