1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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: 13 // 14 // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst)) 15 // ... MCI->getDest() ... MCI->getSource() ... 16 // 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 20 // hack working. 21 // 22 //===----------------------------------------------------------------------===// 23 24 #ifndef LLVM_INTRINSICINST_H 25 #define LLVM_INTRINSICINST_H 26 27 #include "llvm/Constants.h" 28 #include "llvm/Function.h" 29 #include "llvm/Instructions.h" 30 #include "llvm/Intrinsics.h" 31 32 namespace llvm { 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 40 public: 41 /// getIntrinsicID - Return the intrinsic ID of this intrinsic. 42 /// getIntrinsicID()43 Intrinsic::ID getIntrinsicID() const { 44 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID(); 45 } 46 47 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst *)48 static inline bool classof(const IntrinsicInst *) { return true; } classof(const CallInst * I)49 static inline bool classof(const CallInst *I) { 50 if (const Function *CF = I->getCalledFunction()) 51 return CF->getIntrinsicID() != 0; 52 return false; 53 } classof(const Value * V)54 static inline bool classof(const Value *V) { 55 return isa<CallInst>(V) && classof(cast<CallInst>(V)); 56 } 57 }; 58 59 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics 60 /// 61 class DbgInfoIntrinsic : public IntrinsicInst { 62 public: 63 64 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const DbgInfoIntrinsic *)65 static inline bool classof(const DbgInfoIntrinsic *) { return true; } classof(const IntrinsicInst * I)66 static inline bool classof(const IntrinsicInst *I) { 67 switch (I->getIntrinsicID()) { 68 case Intrinsic::dbg_declare: 69 case Intrinsic::dbg_value: 70 return true; 71 default: return false; 72 } 73 } classof(const Value * V)74 static inline bool classof(const Value *V) { 75 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 76 } 77 78 static Value *StripCast(Value *C); 79 }; 80 81 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction. 82 /// 83 class DbgDeclareInst : public DbgInfoIntrinsic { 84 public: 85 Value *getAddress() const; getVariable()86 MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); } 87 88 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const DbgDeclareInst *)89 static inline bool classof(const DbgDeclareInst *) { return true; } classof(const IntrinsicInst * I)90 static inline bool classof(const IntrinsicInst *I) { 91 return I->getIntrinsicID() == Intrinsic::dbg_declare; 92 } classof(const Value * V)93 static inline bool classof(const Value *V) { 94 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 95 } 96 }; 97 98 /// DbgValueInst - This represents the llvm.dbg.value instruction. 99 /// 100 class DbgValueInst : public DbgInfoIntrinsic { 101 public: 102 const Value *getValue() const; 103 Value *getValue(); getOffset()104 uint64_t getOffset() const { 105 return cast<ConstantInt>( 106 const_cast<Value*>(getArgOperand(1)))->getZExtValue(); 107 } getVariable()108 MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); } 109 110 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const DbgValueInst *)111 static inline bool classof(const DbgValueInst *) { return true; } classof(const IntrinsicInst * I)112 static inline bool classof(const IntrinsicInst *I) { 113 return I->getIntrinsicID() == Intrinsic::dbg_value; 114 } classof(const Value * V)115 static inline bool classof(const Value *V) { 116 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 117 } 118 }; 119 120 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove. 121 /// 122 class MemIntrinsic : public IntrinsicInst { 123 public: getRawDest()124 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); } 125 getLength()126 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); } getAlignmentCst()127 ConstantInt *getAlignmentCst() const { 128 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3))); 129 } 130 getAlignment()131 unsigned getAlignment() const { 132 return getAlignmentCst()->getZExtValue(); 133 } 134 getVolatileCst()135 ConstantInt *getVolatileCst() const { 136 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4))); 137 } isVolatile()138 bool isVolatile() const { 139 return !getVolatileCst()->isZero(); 140 } 141 getDestAddressSpace()142 unsigned getDestAddressSpace() const { 143 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 144 } 145 146 /// getDest - This is just like getRawDest, but it strips off any cast 147 /// instructions that feed it, giving the original input. The returned 148 /// value is guaranteed to be a pointer. getDest()149 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 150 151 /// set* - Set the specified arguments of the instruction. 152 /// setDest(Value * Ptr)153 void setDest(Value *Ptr) { 154 assert(getRawDest()->getType() == Ptr->getType() && 155 "setDest called with pointer of wrong type!"); 156 setArgOperand(0, Ptr); 157 } 158 setLength(Value * L)159 void setLength(Value *L) { 160 assert(getLength()->getType() == L->getType() && 161 "setLength called with value of wrong type!"); 162 setArgOperand(2, L); 163 } 164 setAlignment(Constant * A)165 void setAlignment(Constant* A) { 166 setArgOperand(3, A); 167 } 168 setVolatile(Constant * V)169 void setVolatile(Constant* V) { 170 setArgOperand(4, V); 171 } 172 getAlignmentType()173 Type *getAlignmentType() const { 174 return getArgOperand(3)->getType(); 175 } 176 177 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const MemIntrinsic *)178 static inline bool classof(const MemIntrinsic *) { return true; } classof(const IntrinsicInst * I)179 static inline bool classof(const IntrinsicInst *I) { 180 switch (I->getIntrinsicID()) { 181 case Intrinsic::memcpy: 182 case Intrinsic::memmove: 183 case Intrinsic::memset: 184 return true; 185 default: return false; 186 } 187 } classof(const Value * V)188 static inline bool classof(const Value *V) { 189 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 190 } 191 }; 192 193 /// MemSetInst - This class wraps the llvm.memset intrinsic. 194 /// 195 class MemSetInst : public MemIntrinsic { 196 public: 197 /// get* - Return the arguments to the instruction. 198 /// getValue()199 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); } 200 setValue(Value * Val)201 void setValue(Value *Val) { 202 assert(getValue()->getType() == Val->getType() && 203 "setValue called with value of wrong type!"); 204 setArgOperand(1, Val); 205 } 206 207 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const MemSetInst *)208 static inline bool classof(const MemSetInst *) { return true; } classof(const IntrinsicInst * I)209 static inline bool classof(const IntrinsicInst *I) { 210 return I->getIntrinsicID() == Intrinsic::memset; 211 } classof(const Value * V)212 static inline bool classof(const Value *V) { 213 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 214 } 215 }; 216 217 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics. 218 /// 219 class MemTransferInst : public MemIntrinsic { 220 public: 221 /// get* - Return the arguments to the instruction. 222 /// getRawSource()223 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); } 224 225 /// getSource - This is just like getRawSource, but it strips off any cast 226 /// instructions that feed it, giving the original input. The returned 227 /// value is guaranteed to be a pointer. getSource()228 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 229 getSourceAddressSpace()230 unsigned getSourceAddressSpace() const { 231 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 232 } 233 setSource(Value * Ptr)234 void setSource(Value *Ptr) { 235 assert(getRawSource()->getType() == Ptr->getType() && 236 "setSource called with pointer of wrong type!"); 237 setArgOperand(1, Ptr); 238 } 239 240 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const MemTransferInst *)241 static inline bool classof(const MemTransferInst *) { return true; } classof(const IntrinsicInst * I)242 static inline bool classof(const IntrinsicInst *I) { 243 return I->getIntrinsicID() == Intrinsic::memcpy || 244 I->getIntrinsicID() == Intrinsic::memmove; 245 } classof(const Value * V)246 static inline bool classof(const Value *V) { 247 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 248 } 249 }; 250 251 252 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic. 253 /// 254 class MemCpyInst : public MemTransferInst { 255 public: 256 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const MemCpyInst *)257 static inline bool classof(const MemCpyInst *) { return true; } classof(const IntrinsicInst * I)258 static inline bool classof(const IntrinsicInst *I) { 259 return I->getIntrinsicID() == Intrinsic::memcpy; 260 } classof(const Value * V)261 static inline bool classof(const Value *V) { 262 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 263 } 264 }; 265 266 /// MemMoveInst - This class wraps the llvm.memmove intrinsic. 267 /// 268 class MemMoveInst : public MemTransferInst { 269 public: 270 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const MemMoveInst *)271 static inline bool classof(const MemMoveInst *) { return true; } classof(const IntrinsicInst * I)272 static inline bool classof(const IntrinsicInst *I) { 273 return I->getIntrinsicID() == Intrinsic::memmove; 274 } classof(const Value * V)275 static inline bool classof(const Value *V) { 276 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 277 } 278 }; 279 280 } 281 282 #endif 283