Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines classes that make it really easy to deal with intrinsic |
| 10 | // functions with the isa/dyncast family of functions. In particular, this |
| 11 | // allows you to do things like: |
| 12 | // |
| 13 | // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst)) |
| 14 | // ... MCI->getDest() ... MCI->getSource() ... |
| 15 | // |
| 16 | // All intrinsic function calls are instances of the call instruction, so these |
| 17 | // are all subclasses of the CallInst class. Note that none of these classes |
| 18 | // has state or virtual methods, which is an important part of this gross/neat |
| 19 | // hack working. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #ifndef LLVM_IR_INTRINSICINST_H |
| 24 | #define LLVM_IR_INTRINSICINST_H |
| 25 | |
| 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/GlobalVariable.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/Metadata.h" |
| 33 | #include "llvm/IR/Value.h" |
| 34 | #include "llvm/Support/Casting.h" |
| 35 | #include <cassert> |
| 36 | #include <cstdint> |
| 37 | |
| 38 | namespace llvm { |
| 39 | |
| 40 | /// A wrapper class for inspecting calls to intrinsic functions. |
| 41 | /// This allows the standard isa/dyncast/cast functionality to work with calls |
| 42 | /// to intrinsic functions. |
| 43 | class IntrinsicInst : public CallInst { |
| 44 | public: |
| 45 | IntrinsicInst() = delete; |
| 46 | IntrinsicInst(const IntrinsicInst &) = delete; |
| 47 | IntrinsicInst &operator=(const IntrinsicInst &) = delete; |
| 48 | |
| 49 | /// Return the intrinsic ID of this intrinsic. |
| 50 | Intrinsic::ID getIntrinsicID() const { |
| 51 | return getCalledFunction()->getIntrinsicID(); |
| 52 | } |
| 53 | |
| 54 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 55 | static bool classof(const CallInst *I) { |
| 56 | if (const Function *CF = I->getCalledFunction()) |
| 57 | return CF->isIntrinsic(); |
| 58 | return false; |
| 59 | } |
| 60 | static bool classof(const Value *V) { |
| 61 | return isa<CallInst>(V) && classof(cast<CallInst>(V)); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | /// This is the common base class for debug info intrinsics. |
| 66 | class DbgInfoIntrinsic : public IntrinsicInst { |
| 67 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | /// \name Casting methods |
| 69 | /// @{ |
| 70 | static bool classof(const IntrinsicInst *I) { |
| 71 | switch (I->getIntrinsicID()) { |
| 72 | case Intrinsic::dbg_declare: |
| 73 | case Intrinsic::dbg_value: |
| 74 | case Intrinsic::dbg_addr: |
| 75 | case Intrinsic::dbg_label: |
| 76 | return true; |
| 77 | default: return false; |
| 78 | } |
| 79 | } |
| 80 | static bool classof(const Value *V) { |
| 81 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 82 | } |
| 83 | /// @} |
| 84 | }; |
| 85 | |
| 86 | /// This is the common base class for debug info intrinsics for variables. |
| 87 | class DbgVariableIntrinsic : public DbgInfoIntrinsic { |
| 88 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | /// Get the location corresponding to the variable referenced by the debug |
| 90 | /// info intrinsic. Depending on the intrinsic, this could be the |
| 91 | /// variable's value or its address. |
| 92 | Value *getVariableLocation(bool AllowNullOp = true) const; |
| 93 | |
| 94 | /// Does this describe the address of a local variable. True for dbg.addr |
| 95 | /// and dbg.declare, but not dbg.value, which describes its value. |
| 96 | bool isAddressOfVariable() const { |
| 97 | return getIntrinsicID() != Intrinsic::dbg_value; |
| 98 | } |
| 99 | |
| 100 | DILocalVariable *getVariable() const { |
| 101 | return cast<DILocalVariable>(getRawVariable()); |
| 102 | } |
| 103 | |
| 104 | DIExpression *getExpression() const { |
| 105 | return cast<DIExpression>(getRawExpression()); |
| 106 | } |
| 107 | |
| 108 | Metadata *getRawVariable() const { |
| 109 | return cast<MetadataAsValue>(getArgOperand(1))->getMetadata(); |
| 110 | } |
| 111 | |
| 112 | Metadata *getRawExpression() const { |
| 113 | return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); |
| 114 | } |
| 115 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 116 | /// Get the size (in bits) of the variable, or fragment of the variable that |
| 117 | /// is described. |
| 118 | Optional<uint64_t> getFragmentSizeInBits() const; |
| 119 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | /// \name Casting methods |
| 121 | /// @{ |
| 122 | static bool classof(const IntrinsicInst *I) { |
| 123 | switch (I->getIntrinsicID()) { |
| 124 | case Intrinsic::dbg_declare: |
| 125 | case Intrinsic::dbg_value: |
| 126 | case Intrinsic::dbg_addr: |
| 127 | return true; |
| 128 | default: return false; |
| 129 | } |
| 130 | } |
| 131 | static bool classof(const Value *V) { |
| 132 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 133 | } |
| 134 | /// @} |
| 135 | }; |
| 136 | |
| 137 | /// This represents the llvm.dbg.declare instruction. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 138 | class DbgDeclareInst : public DbgVariableIntrinsic { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | public: |
| 140 | Value *getAddress() const { return getVariableLocation(); } |
| 141 | |
| 142 | /// \name Casting methods |
| 143 | /// @{ |
| 144 | static bool classof(const IntrinsicInst *I) { |
| 145 | return I->getIntrinsicID() == Intrinsic::dbg_declare; |
| 146 | } |
| 147 | static bool classof(const Value *V) { |
| 148 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 149 | } |
| 150 | /// @} |
| 151 | }; |
| 152 | |
| 153 | /// This represents the llvm.dbg.addr instruction. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 154 | class DbgAddrIntrinsic : public DbgVariableIntrinsic { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | public: |
| 156 | Value *getAddress() const { return getVariableLocation(); } |
| 157 | |
| 158 | /// \name Casting methods |
| 159 | /// @{ |
| 160 | static bool classof(const IntrinsicInst *I) { |
| 161 | return I->getIntrinsicID() == Intrinsic::dbg_addr; |
| 162 | } |
| 163 | static bool classof(const Value *V) { |
| 164 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | /// This represents the llvm.dbg.value instruction. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 169 | class DbgValueInst : public DbgVariableIntrinsic { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 170 | public: |
| 171 | Value *getValue() const { |
| 172 | return getVariableLocation(/* AllowNullOp = */ false); |
| 173 | } |
| 174 | |
| 175 | /// \name Casting methods |
| 176 | /// @{ |
| 177 | static bool classof(const IntrinsicInst *I) { |
| 178 | return I->getIntrinsicID() == Intrinsic::dbg_value; |
| 179 | } |
| 180 | static bool classof(const Value *V) { |
| 181 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 182 | } |
| 183 | /// @} |
| 184 | }; |
| 185 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 186 | /// This represents the llvm.dbg.label instruction. |
| 187 | class DbgLabelInst : public DbgInfoIntrinsic { |
| 188 | public: |
| 189 | DILabel *getLabel() const { |
| 190 | return cast<DILabel>(getRawLabel()); |
| 191 | } |
| 192 | |
| 193 | Metadata *getRawLabel() const { |
| 194 | return cast<MetadataAsValue>(getArgOperand(0))->getMetadata(); |
| 195 | } |
| 196 | |
| 197 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 198 | /// @{ |
| 199 | static bool classof(const IntrinsicInst *I) { |
| 200 | return I->getIntrinsicID() == Intrinsic::dbg_label; |
| 201 | } |
| 202 | static bool classof(const Value *V) { |
| 203 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 204 | } |
| 205 | /// @} |
| 206 | }; |
| 207 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 208 | /// This is the common base class for constrained floating point intrinsics. |
| 209 | class ConstrainedFPIntrinsic : public IntrinsicInst { |
| 210 | public: |
| 211 | enum RoundingMode { |
| 212 | rmInvalid, |
| 213 | rmDynamic, |
| 214 | rmToNearest, |
| 215 | rmDownward, |
| 216 | rmUpward, |
| 217 | rmTowardZero |
| 218 | }; |
| 219 | |
| 220 | enum ExceptionBehavior { |
| 221 | ebInvalid, |
| 222 | ebIgnore, |
| 223 | ebMayTrap, |
| 224 | ebStrict |
| 225 | }; |
| 226 | |
| 227 | bool isUnaryOp() const; |
| 228 | bool isTernaryOp() const; |
| 229 | RoundingMode getRoundingMode() const; |
| 230 | ExceptionBehavior getExceptionBehavior() const; |
| 231 | |
| 232 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 233 | static bool classof(const IntrinsicInst *I) { |
| 234 | switch (I->getIntrinsicID()) { |
| 235 | case Intrinsic::experimental_constrained_fadd: |
| 236 | case Intrinsic::experimental_constrained_fsub: |
| 237 | case Intrinsic::experimental_constrained_fmul: |
| 238 | case Intrinsic::experimental_constrained_fdiv: |
| 239 | case Intrinsic::experimental_constrained_frem: |
| 240 | case Intrinsic::experimental_constrained_fma: |
| 241 | case Intrinsic::experimental_constrained_sqrt: |
| 242 | case Intrinsic::experimental_constrained_pow: |
| 243 | case Intrinsic::experimental_constrained_powi: |
| 244 | case Intrinsic::experimental_constrained_sin: |
| 245 | case Intrinsic::experimental_constrained_cos: |
| 246 | case Intrinsic::experimental_constrained_exp: |
| 247 | case Intrinsic::experimental_constrained_exp2: |
| 248 | case Intrinsic::experimental_constrained_log: |
| 249 | case Intrinsic::experimental_constrained_log10: |
| 250 | case Intrinsic::experimental_constrained_log2: |
| 251 | case Intrinsic::experimental_constrained_rint: |
| 252 | case Intrinsic::experimental_constrained_nearbyint: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 253 | case Intrinsic::experimental_constrained_maxnum: |
| 254 | case Intrinsic::experimental_constrained_minnum: |
| 255 | case Intrinsic::experimental_constrained_ceil: |
| 256 | case Intrinsic::experimental_constrained_floor: |
| 257 | case Intrinsic::experimental_constrained_round: |
| 258 | case Intrinsic::experimental_constrained_trunc: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 259 | return true; |
| 260 | default: return false; |
| 261 | } |
| 262 | } |
| 263 | static bool classof(const Value *V) { |
| 264 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | /// Common base class for all memory intrinsics. Simply provides |
| 269 | /// common methods. |
| 270 | /// Written as CRTP to avoid a common base class amongst the |
| 271 | /// three atomicity hierarchies. |
| 272 | template <typename Derived> class MemIntrinsicBase : public IntrinsicInst { |
| 273 | private: |
| 274 | enum { ARG_DEST = 0, ARG_LENGTH = 2 }; |
| 275 | |
| 276 | public: |
| 277 | Value *getRawDest() const { |
| 278 | return const_cast<Value *>(getArgOperand(ARG_DEST)); |
| 279 | } |
| 280 | const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); } |
| 281 | Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); } |
| 282 | |
| 283 | Value *getLength() const { |
| 284 | return const_cast<Value *>(getArgOperand(ARG_LENGTH)); |
| 285 | } |
| 286 | const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); } |
| 287 | Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); } |
| 288 | |
| 289 | /// This is just like getRawDest, but it strips off any cast |
| 290 | /// instructions (including addrspacecast) that feed it, giving the |
| 291 | /// original input. The returned value is guaranteed to be a pointer. |
| 292 | Value *getDest() const { return getRawDest()->stripPointerCasts(); } |
| 293 | |
| 294 | unsigned getDestAddressSpace() const { |
| 295 | return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); |
| 296 | } |
| 297 | |
| 298 | unsigned getDestAlignment() const { return getParamAlignment(ARG_DEST); } |
| 299 | |
| 300 | /// Set the specified arguments of the instruction. |
| 301 | void setDest(Value *Ptr) { |
| 302 | assert(getRawDest()->getType() == Ptr->getType() && |
| 303 | "setDest called with pointer of wrong type!"); |
| 304 | setArgOperand(ARG_DEST, Ptr); |
| 305 | } |
| 306 | |
| 307 | void setDestAlignment(unsigned Align) { |
| 308 | removeParamAttr(ARG_DEST, Attribute::Alignment); |
| 309 | if (Align > 0) |
| 310 | addParamAttr(ARG_DEST, |
| 311 | Attribute::getWithAlignment(getContext(), Align)); |
| 312 | } |
| 313 | |
| 314 | void setLength(Value *L) { |
| 315 | assert(getLength()->getType() == L->getType() && |
| 316 | "setLength called with value of wrong type!"); |
| 317 | setArgOperand(ARG_LENGTH, L); |
| 318 | } |
| 319 | }; |
| 320 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 321 | /// Common base class for all memory transfer intrinsics. Simply provides |
| 322 | /// common methods. |
| 323 | template <class BaseCL> class MemTransferBase : public BaseCL { |
| 324 | private: |
| 325 | enum { ARG_SOURCE = 1 }; |
| 326 | |
| 327 | public: |
| 328 | /// Return the arguments to the instruction. |
| 329 | Value *getRawSource() const { |
| 330 | return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE)); |
| 331 | } |
| 332 | const Use &getRawSourceUse() const { |
| 333 | return BaseCL::getArgOperandUse(ARG_SOURCE); |
| 334 | } |
| 335 | Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); } |
| 336 | |
| 337 | /// This is just like getRawSource, but it strips off any cast |
| 338 | /// instructions that feed it, giving the original input. The returned |
| 339 | /// value is guaranteed to be a pointer. |
| 340 | Value *getSource() const { return getRawSource()->stripPointerCasts(); } |
| 341 | |
| 342 | unsigned getSourceAddressSpace() const { |
| 343 | return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); |
| 344 | } |
| 345 | |
| 346 | unsigned getSourceAlignment() const { |
| 347 | return BaseCL::getParamAlignment(ARG_SOURCE); |
| 348 | } |
| 349 | |
| 350 | void setSource(Value *Ptr) { |
| 351 | assert(getRawSource()->getType() == Ptr->getType() && |
| 352 | "setSource called with pointer of wrong type!"); |
| 353 | BaseCL::setArgOperand(ARG_SOURCE, Ptr); |
| 354 | } |
| 355 | |
| 356 | void setSourceAlignment(unsigned Align) { |
| 357 | BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment); |
| 358 | if (Align > 0) |
| 359 | BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment( |
| 360 | BaseCL::getContext(), Align)); |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | /// Common base class for all memset intrinsics. Simply provides |
| 365 | /// common methods. |
| 366 | template <class BaseCL> class MemSetBase : public BaseCL { |
| 367 | private: |
| 368 | enum { ARG_VALUE = 1 }; |
| 369 | |
| 370 | public: |
| 371 | Value *getValue() const { |
| 372 | return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE)); |
| 373 | } |
| 374 | const Use &getValueUse() const { |
| 375 | return BaseCL::getArgOperandUse(ARG_VALUE); |
| 376 | } |
| 377 | Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); } |
| 378 | |
| 379 | void setValue(Value *Val) { |
| 380 | assert(getValue()->getType() == Val->getType() && |
| 381 | "setValue called with value of wrong type!"); |
| 382 | BaseCL::setArgOperand(ARG_VALUE, Val); |
| 383 | } |
| 384 | }; |
| 385 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 386 | // The common base class for the atomic memset/memmove/memcpy intrinsics |
| 387 | // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove |
| 388 | class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> { |
| 389 | private: |
| 390 | enum { ARG_ELEMENTSIZE = 3 }; |
| 391 | |
| 392 | public: |
| 393 | Value *getRawElementSizeInBytes() const { |
| 394 | return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE)); |
| 395 | } |
| 396 | |
| 397 | ConstantInt *getElementSizeInBytesCst() const { |
| 398 | return cast<ConstantInt>(getRawElementSizeInBytes()); |
| 399 | } |
| 400 | |
| 401 | uint32_t getElementSizeInBytes() const { |
| 402 | return getElementSizeInBytesCst()->getZExtValue(); |
| 403 | } |
| 404 | |
| 405 | void setElementSizeInBytes(Constant *V) { |
| 406 | assert(V->getType() == Type::getInt8Ty(getContext()) && |
| 407 | "setElementSizeInBytes called with value of wrong type!"); |
| 408 | setArgOperand(ARG_ELEMENTSIZE, V); |
| 409 | } |
| 410 | |
| 411 | static bool classof(const IntrinsicInst *I) { |
| 412 | switch (I->getIntrinsicID()) { |
| 413 | case Intrinsic::memcpy_element_unordered_atomic: |
| 414 | case Intrinsic::memmove_element_unordered_atomic: |
| 415 | case Intrinsic::memset_element_unordered_atomic: |
| 416 | return true; |
| 417 | default: |
| 418 | return false; |
| 419 | } |
| 420 | } |
| 421 | static bool classof(const Value *V) { |
| 422 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 423 | } |
| 424 | }; |
| 425 | |
| 426 | /// This class represents atomic memset intrinsic |
| 427 | // i.e. llvm.element.unordered.atomic.memset |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 428 | class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 429 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 430 | static bool classof(const IntrinsicInst *I) { |
| 431 | return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic; |
| 432 | } |
| 433 | static bool classof(const Value *V) { |
| 434 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | // This class wraps the atomic memcpy/memmove intrinsics |
| 439 | // i.e. llvm.element.unordered.atomic.memcpy/memmove |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 440 | class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 441 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 442 | static bool classof(const IntrinsicInst *I) { |
| 443 | switch (I->getIntrinsicID()) { |
| 444 | case Intrinsic::memcpy_element_unordered_atomic: |
| 445 | case Intrinsic::memmove_element_unordered_atomic: |
| 446 | return true; |
| 447 | default: |
| 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | static bool classof(const Value *V) { |
| 452 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 453 | } |
| 454 | }; |
| 455 | |
| 456 | /// This class represents the atomic memcpy intrinsic |
| 457 | /// i.e. llvm.element.unordered.atomic.memcpy |
| 458 | class AtomicMemCpyInst : public AtomicMemTransferInst { |
| 459 | public: |
| 460 | static bool classof(const IntrinsicInst *I) { |
| 461 | return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic; |
| 462 | } |
| 463 | static bool classof(const Value *V) { |
| 464 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 465 | } |
| 466 | }; |
| 467 | |
| 468 | /// This class represents the atomic memmove intrinsic |
| 469 | /// i.e. llvm.element.unordered.atomic.memmove |
| 470 | class AtomicMemMoveInst : public AtomicMemTransferInst { |
| 471 | public: |
| 472 | static bool classof(const IntrinsicInst *I) { |
| 473 | return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic; |
| 474 | } |
| 475 | static bool classof(const Value *V) { |
| 476 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | /// This is the common base class for memset/memcpy/memmove. |
| 481 | class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> { |
| 482 | private: |
| 483 | enum { ARG_VOLATILE = 3 }; |
| 484 | |
| 485 | public: |
| 486 | ConstantInt *getVolatileCst() const { |
| 487 | return cast<ConstantInt>( |
| 488 | const_cast<Value *>(getArgOperand(ARG_VOLATILE))); |
| 489 | } |
| 490 | |
| 491 | bool isVolatile() const { |
| 492 | return !getVolatileCst()->isZero(); |
| 493 | } |
| 494 | |
| 495 | void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); } |
| 496 | |
| 497 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 498 | static bool classof(const IntrinsicInst *I) { |
| 499 | switch (I->getIntrinsicID()) { |
| 500 | case Intrinsic::memcpy: |
| 501 | case Intrinsic::memmove: |
| 502 | case Intrinsic::memset: |
| 503 | return true; |
| 504 | default: return false; |
| 505 | } |
| 506 | } |
| 507 | static bool classof(const Value *V) { |
| 508 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 509 | } |
| 510 | }; |
| 511 | |
| 512 | /// This class wraps the llvm.memset intrinsic. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 513 | class MemSetInst : public MemSetBase<MemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 514 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 515 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 516 | static bool classof(const IntrinsicInst *I) { |
| 517 | return I->getIntrinsicID() == Intrinsic::memset; |
| 518 | } |
| 519 | static bool classof(const Value *V) { |
| 520 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 521 | } |
| 522 | }; |
| 523 | |
| 524 | /// This class wraps the llvm.memcpy/memmove intrinsics. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 525 | class MemTransferInst : public MemTransferBase<MemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 526 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 527 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 528 | static bool classof(const IntrinsicInst *I) { |
| 529 | return I->getIntrinsicID() == Intrinsic::memcpy || |
| 530 | I->getIntrinsicID() == Intrinsic::memmove; |
| 531 | } |
| 532 | static bool classof(const Value *V) { |
| 533 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 534 | } |
| 535 | }; |
| 536 | |
| 537 | /// This class wraps the llvm.memcpy intrinsic. |
| 538 | class MemCpyInst : public MemTransferInst { |
| 539 | public: |
| 540 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 541 | static bool classof(const IntrinsicInst *I) { |
| 542 | return I->getIntrinsicID() == Intrinsic::memcpy; |
| 543 | } |
| 544 | static bool classof(const Value *V) { |
| 545 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 546 | } |
| 547 | }; |
| 548 | |
| 549 | /// This class wraps the llvm.memmove intrinsic. |
| 550 | class MemMoveInst : public MemTransferInst { |
| 551 | public: |
| 552 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 553 | static bool classof(const IntrinsicInst *I) { |
| 554 | return I->getIntrinsicID() == Intrinsic::memmove; |
| 555 | } |
| 556 | static bool classof(const Value *V) { |
| 557 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 558 | } |
| 559 | }; |
| 560 | |
| 561 | // The common base class for any memset/memmove/memcpy intrinsics; |
| 562 | // whether they be atomic or non-atomic. |
| 563 | // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove |
| 564 | // and llvm.memset/memcpy/memmove |
| 565 | class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> { |
| 566 | public: |
| 567 | bool isVolatile() const { |
| 568 | // Only the non-atomic intrinsics can be volatile |
| 569 | if (auto *MI = dyn_cast<MemIntrinsic>(this)) |
| 570 | return MI->isVolatile(); |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | static bool classof(const IntrinsicInst *I) { |
| 575 | switch (I->getIntrinsicID()) { |
| 576 | case Intrinsic::memcpy: |
| 577 | case Intrinsic::memmove: |
| 578 | case Intrinsic::memset: |
| 579 | case Intrinsic::memcpy_element_unordered_atomic: |
| 580 | case Intrinsic::memmove_element_unordered_atomic: |
| 581 | case Intrinsic::memset_element_unordered_atomic: |
| 582 | return true; |
| 583 | default: |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | static bool classof(const Value *V) { |
| 588 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 589 | } |
| 590 | }; |
| 591 | |
| 592 | /// This class represents any memset intrinsic |
| 593 | // i.e. llvm.element.unordered.atomic.memset |
| 594 | // and llvm.memset |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 595 | class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 596 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 597 | static bool classof(const IntrinsicInst *I) { |
| 598 | switch (I->getIntrinsicID()) { |
| 599 | case Intrinsic::memset: |
| 600 | case Intrinsic::memset_element_unordered_atomic: |
| 601 | return true; |
| 602 | default: |
| 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | static bool classof(const Value *V) { |
| 607 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 608 | } |
| 609 | }; |
| 610 | |
| 611 | // This class wraps any memcpy/memmove intrinsics |
| 612 | // i.e. llvm.element.unordered.atomic.memcpy/memmove |
| 613 | // and llvm.memcpy/memmove |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 614 | class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 615 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 616 | static bool classof(const IntrinsicInst *I) { |
| 617 | switch (I->getIntrinsicID()) { |
| 618 | case Intrinsic::memcpy: |
| 619 | case Intrinsic::memmove: |
| 620 | case Intrinsic::memcpy_element_unordered_atomic: |
| 621 | case Intrinsic::memmove_element_unordered_atomic: |
| 622 | return true; |
| 623 | default: |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | static bool classof(const Value *V) { |
| 628 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | /// This class represents any memcpy intrinsic |
| 633 | /// i.e. llvm.element.unordered.atomic.memcpy |
| 634 | /// and llvm.memcpy |
| 635 | class AnyMemCpyInst : public AnyMemTransferInst { |
| 636 | public: |
| 637 | static bool classof(const IntrinsicInst *I) { |
| 638 | switch (I->getIntrinsicID()) { |
| 639 | case Intrinsic::memcpy: |
| 640 | case Intrinsic::memcpy_element_unordered_atomic: |
| 641 | return true; |
| 642 | default: |
| 643 | return false; |
| 644 | } |
| 645 | } |
| 646 | static bool classof(const Value *V) { |
| 647 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 648 | } |
| 649 | }; |
| 650 | |
| 651 | /// This class represents any memmove intrinsic |
| 652 | /// i.e. llvm.element.unordered.atomic.memmove |
| 653 | /// and llvm.memmove |
| 654 | class AnyMemMoveInst : public AnyMemTransferInst { |
| 655 | public: |
| 656 | static bool classof(const IntrinsicInst *I) { |
| 657 | switch (I->getIntrinsicID()) { |
| 658 | case Intrinsic::memmove: |
| 659 | case Intrinsic::memmove_element_unordered_atomic: |
| 660 | return true; |
| 661 | default: |
| 662 | return false; |
| 663 | } |
| 664 | } |
| 665 | static bool classof(const Value *V) { |
| 666 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 667 | } |
| 668 | }; |
| 669 | |
| 670 | /// This represents the llvm.va_start intrinsic. |
| 671 | class VAStartInst : public IntrinsicInst { |
| 672 | public: |
| 673 | static bool classof(const IntrinsicInst *I) { |
| 674 | return I->getIntrinsicID() == Intrinsic::vastart; |
| 675 | } |
| 676 | static bool classof(const Value *V) { |
| 677 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 678 | } |
| 679 | |
| 680 | Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } |
| 681 | }; |
| 682 | |
| 683 | /// This represents the llvm.va_end intrinsic. |
| 684 | class VAEndInst : public IntrinsicInst { |
| 685 | public: |
| 686 | static bool classof(const IntrinsicInst *I) { |
| 687 | return I->getIntrinsicID() == Intrinsic::vaend; |
| 688 | } |
| 689 | static bool classof(const Value *V) { |
| 690 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 691 | } |
| 692 | |
| 693 | Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } |
| 694 | }; |
| 695 | |
| 696 | /// This represents the llvm.va_copy intrinsic. |
| 697 | class VACopyInst : public IntrinsicInst { |
| 698 | public: |
| 699 | static bool classof(const IntrinsicInst *I) { |
| 700 | return I->getIntrinsicID() == Intrinsic::vacopy; |
| 701 | } |
| 702 | static bool classof(const Value *V) { |
| 703 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 704 | } |
| 705 | |
| 706 | Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); } |
| 707 | Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); } |
| 708 | }; |
| 709 | |
| 710 | /// This represents the llvm.instrprof_increment intrinsic. |
| 711 | class InstrProfIncrementInst : public IntrinsicInst { |
| 712 | public: |
| 713 | static bool classof(const IntrinsicInst *I) { |
| 714 | return I->getIntrinsicID() == Intrinsic::instrprof_increment; |
| 715 | } |
| 716 | static bool classof(const Value *V) { |
| 717 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 718 | } |
| 719 | |
| 720 | GlobalVariable *getName() const { |
| 721 | return cast<GlobalVariable>( |
| 722 | const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); |
| 723 | } |
| 724 | |
| 725 | ConstantInt *getHash() const { |
| 726 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); |
| 727 | } |
| 728 | |
| 729 | ConstantInt *getNumCounters() const { |
| 730 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2))); |
| 731 | } |
| 732 | |
| 733 | ConstantInt *getIndex() const { |
| 734 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); |
| 735 | } |
| 736 | |
| 737 | Value *getStep() const; |
| 738 | }; |
| 739 | |
| 740 | class InstrProfIncrementInstStep : public InstrProfIncrementInst { |
| 741 | public: |
| 742 | static bool classof(const IntrinsicInst *I) { |
| 743 | return I->getIntrinsicID() == Intrinsic::instrprof_increment_step; |
| 744 | } |
| 745 | static bool classof(const Value *V) { |
| 746 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 747 | } |
| 748 | }; |
| 749 | |
| 750 | /// This represents the llvm.instrprof_value_profile intrinsic. |
| 751 | class InstrProfValueProfileInst : public IntrinsicInst { |
| 752 | public: |
| 753 | static bool classof(const IntrinsicInst *I) { |
| 754 | return I->getIntrinsicID() == Intrinsic::instrprof_value_profile; |
| 755 | } |
| 756 | static bool classof(const Value *V) { |
| 757 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 758 | } |
| 759 | |
| 760 | GlobalVariable *getName() const { |
| 761 | return cast<GlobalVariable>( |
| 762 | const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); |
| 763 | } |
| 764 | |
| 765 | ConstantInt *getHash() const { |
| 766 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); |
| 767 | } |
| 768 | |
| 769 | Value *getTargetValue() const { |
| 770 | return cast<Value>(const_cast<Value *>(getArgOperand(2))); |
| 771 | } |
| 772 | |
| 773 | ConstantInt *getValueKind() const { |
| 774 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); |
| 775 | } |
| 776 | |
| 777 | // Returns the value site index. |
| 778 | ConstantInt *getIndex() const { |
| 779 | return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4))); |
| 780 | } |
| 781 | }; |
| 782 | |
| 783 | } // end namespace llvm |
| 784 | |
| 785 | #endif // LLVM_IR_INTRINSICINST_H |