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 class IntrinsicInst : public CallInst {
34 IntrinsicInst(); // DO NOT IMPLEMENT
35 IntrinsicInst(const IntrinsicInst&); // DO NOT IMPLEMENT
36 void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
39 /// StripPointerCasts - This static method strips off any unneeded pointer
40 /// casts from the specified value, returning the original uncasted value.
41 /// Note that the returned value is guaranteed to have pointer type.
42 static Value *StripPointerCasts(Value *Ptr);
46 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
48 struct MemIntrinsic : public IntrinsicInst {
49 Value *getRawDest() const { return const_cast<Value*>(getOperand(0)); }
51 Value *getLength() const { return const_cast<Value*>(getOperand(2)); }
52 ConstantInt *getAlignment() const {
53 return cast<ConstantInt>(const_cast<Value*>(getOperand(3)));
56 /// getDest - This is just like getRawDest, but it strips off any cast
57 /// instructions that feed it, giving the original input. The returned
58 /// value is guaranteed to be a pointer.
59 Value *getDest() const { return StripPointerCasts(getRawDest()); }
61 /// set* - Set the specified arguments of the instruction.
63 void setDest(Value *Ptr) {
64 assert(getRawDest()->getType() == Ptr->getType() &&
65 "setDest called with pointer of wrong type!");
69 void setLength(Value *L) {
70 assert(getLength()->getType() == L->getType() &&
71 "setLength called with value of wrong type!");
74 void setAlignment(ConstantInt *A) {
75 assert(getAlignment()->getType() == A->getType() &&
76 "setAlignment called with value of wrong type!");
80 // Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const MemIntrinsic *) { return true; }
82 static inline bool classof(const CallInst *I) {
83 if (const Function *CF = I->getCalledFunction())
84 switch (CF->getIntrinsicID()) {
85 case Intrinsic::memcpy:
86 case Intrinsic::memmove:
87 case Intrinsic::memset:
93 static inline bool classof(const Value *V) {
94 return isa<CallInst>(V) && classof(cast<CallInst>(V));
99 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
101 struct MemCpyInst : public MemIntrinsic {
102 /// get* - Return the arguments to the instruction.
104 Value *getRawSource() const { return const_cast<Value*>(getOperand(1)); }
106 /// getSource - This is just like getRawSource, but it strips off any cast
107 /// instructions that feed it, giving the original input. The returned
108 /// value is guaranteed to be a pointer.
109 Value *getSource() const { return StripPointerCasts(getRawSource()); }
112 void setSource(Value *Ptr) {
113 assert(getRawSource()->getType() == Ptr->getType() &&
114 "setSource called with pointer of wrong type!");
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const MemCpyInst *) { return true; }
120 static inline bool classof(const MemIntrinsic *I) {
121 return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memcpy;
123 static inline bool classof(const CallInst *I) {
124 if (const Function *CF = I->getCalledFunction())
125 if (CF->getIntrinsicID() == Intrinsic::memcpy)
129 static inline bool classof(const Value *V) {
130 return isa<CallInst>(V) && classof(cast<CallInst>(V));
134 /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
136 struct MemMoveInst : public MemIntrinsic {
137 /// get* - Return the arguments to the instruction.
139 Value *getRawSource() const { return const_cast<Value*>(getOperand(1)); }
141 /// getSource - This is just like getRawSource, but it strips off any cast
142 /// instructions that feed it, giving the original input. The returned
143 /// value is guaranteed to be a pointer.
144 Value *getSource() const { return StripPointerCasts(getRawSource()); }
146 void setSource(Value *Ptr) {
147 assert(getRawSource()->getType() == Ptr->getType() &&
148 "setSource called with pointer of wrong type!");
152 // Methods for support type inquiry through isa, cast, and dyn_cast:
153 static inline bool classof(const MemMoveInst *) { return true; }
154 static inline bool classof(const MemIntrinsic *I) {
155 return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memmove;
157 static inline bool classof(const CallInst *I) {
158 if (const Function *CF = I->getCalledFunction())
159 if (CF->getIntrinsicID() == Intrinsic::memmove)
163 static inline bool classof(const Value *V) {
164 return isa<CallInst>(V) && classof(cast<CallInst>(V));
168 /// MemSetInst - This class wraps the llvm.memcpy intrinsic.
170 struct MemSetInst : public MemIntrinsic {
171 /// get* - Return the arguments to the instruction.
173 Value *getValue() const { return const_cast<Value*>(getOperand(1)); }
175 void setValue(Value *Val) {
176 assert(getValue()->getType() == Val->getType() &&
177 "setSource called with pointer of wrong type!");
181 // Methods for support type inquiry through isa, cast, and dyn_cast:
182 static inline bool classof(const MemSetInst *) { return true; }
183 static inline bool classof(const MemIntrinsic *I) {
184 return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memset;
186 static inline bool classof(const CallInst *I) {
187 if (const Function *CF = I->getCalledFunction())
188 if (CF->getIntrinsicID() == Intrinsic::memset)
192 static inline bool classof(const Value *V) {
193 return isa<CallInst>(V) && classof(cast<CallInst>(V));