Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===-- llvm/Instruction.h - Instruction class definition -------*- 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 contains the declaration of the Instruction class, which is the |
| 11 | // base class for all of the LLVM instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_IR_INSTRUCTION_H |
| 16 | #define LLVM_IR_INSTRUCTION_H |
| 17 | |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/ADT/None.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/ilist_node.h" |
| 22 | #include "llvm/IR/DebugLoc.h" |
| 23 | #include "llvm/IR/SymbolTableListTraits.h" |
| 24 | #include "llvm/IR/User.h" |
| 25 | #include "llvm/IR/Value.h" |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include <algorithm> |
| 28 | #include <cassert> |
| 29 | #include <cstdint> |
| 30 | #include <utility> |
| 31 | |
| 32 | namespace llvm { |
| 33 | |
| 34 | class BasicBlock; |
| 35 | class FastMathFlags; |
| 36 | class MDNode; |
| 37 | class Module; |
| 38 | struct AAMDNodes; |
| 39 | |
| 40 | template <> struct ilist_alloc_traits<Instruction> { |
| 41 | static inline void deleteNode(Instruction *V); |
| 42 | }; |
| 43 | |
| 44 | class Instruction : public User, |
| 45 | public ilist_node_with_parent<Instruction, BasicBlock> { |
| 46 | BasicBlock *Parent; |
| 47 | DebugLoc DbgLoc; // 'dbg' Metadata cache. |
| 48 | |
| 49 | enum { |
| 50 | /// This is a bit stored in the SubClassData field which indicates whether |
| 51 | /// this instruction has metadata attached to it or not. |
| 52 | HasMetadataBit = 1 << 15 |
| 53 | }; |
| 54 | |
| 55 | protected: |
| 56 | ~Instruction(); // Use deleteValue() to delete a generic Instruction. |
| 57 | |
| 58 | public: |
| 59 | Instruction(const Instruction &) = delete; |
| 60 | Instruction &operator=(const Instruction &) = delete; |
| 61 | |
| 62 | /// Specialize the methods defined in Value, as we know that an instruction |
| 63 | /// can only be used by other instructions. |
| 64 | Instruction *user_back() { return cast<Instruction>(*user_begin());} |
| 65 | const Instruction *user_back() const { return cast<Instruction>(*user_begin());} |
| 66 | |
| 67 | inline const BasicBlock *getParent() const { return Parent; } |
| 68 | inline BasicBlock *getParent() { return Parent; } |
| 69 | |
| 70 | /// Return the module owning the function this instruction belongs to |
| 71 | /// or nullptr it the function does not have a module. |
| 72 | /// |
| 73 | /// Note: this is undefined behavior if the instruction does not have a |
| 74 | /// parent, or the parent basic block does not have a parent function. |
| 75 | const Module *getModule() const; |
| 76 | Module *getModule() { |
| 77 | return const_cast<Module *>( |
| 78 | static_cast<const Instruction *>(this)->getModule()); |
| 79 | } |
| 80 | |
| 81 | /// Return the function this instruction belongs to. |
| 82 | /// |
| 83 | /// Note: it is undefined behavior to call this on an instruction not |
| 84 | /// currently inserted into a function. |
| 85 | const Function *getFunction() const; |
| 86 | Function *getFunction() { |
| 87 | return const_cast<Function *>( |
| 88 | static_cast<const Instruction *>(this)->getFunction()); |
| 89 | } |
| 90 | |
| 91 | /// This method unlinks 'this' from the containing basic block, but does not |
| 92 | /// delete it. |
| 93 | void removeFromParent(); |
| 94 | |
| 95 | /// This method unlinks 'this' from the containing basic block and deletes it. |
| 96 | /// |
| 97 | /// \returns an iterator pointing to the element after the erased one |
| 98 | SymbolTableList<Instruction>::iterator eraseFromParent(); |
| 99 | |
| 100 | /// Insert an unlinked instruction into a basic block immediately before |
| 101 | /// the specified instruction. |
| 102 | void insertBefore(Instruction *InsertPos); |
| 103 | |
| 104 | /// Insert an unlinked instruction into a basic block immediately after the |
| 105 | /// specified instruction. |
| 106 | void insertAfter(Instruction *InsertPos); |
| 107 | |
| 108 | /// Unlink this instruction from its current basic block and insert it into |
| 109 | /// the basic block that MovePos lives in, right before MovePos. |
| 110 | void moveBefore(Instruction *MovePos); |
| 111 | |
| 112 | /// Unlink this instruction and insert into BB before I. |
| 113 | /// |
| 114 | /// \pre I is a valid iterator into BB. |
| 115 | void moveBefore(BasicBlock &BB, SymbolTableList<Instruction>::iterator I); |
| 116 | |
| 117 | /// Unlink this instruction from its current basic block and insert it into |
| 118 | /// the basic block that MovePos lives in, right after MovePos. |
| 119 | void moveAfter(Instruction *MovePos); |
| 120 | |
| 121 | //===--------------------------------------------------------------------===// |
| 122 | // Subclass classification. |
| 123 | //===--------------------------------------------------------------------===// |
| 124 | |
| 125 | /// Returns a member of one of the enums like Instruction::Add. |
| 126 | unsigned getOpcode() const { return getValueID() - InstructionVal; } |
| 127 | |
| 128 | const char *getOpcodeName() const { return getOpcodeName(getOpcode()); } |
| 129 | bool isTerminator() const { return isTerminator(getOpcode()); } |
| 130 | bool isBinaryOp() const { return isBinaryOp(getOpcode()); } |
| 131 | bool isShift() { return isShift(getOpcode()); } |
| 132 | bool isCast() const { return isCast(getOpcode()); } |
| 133 | bool isFuncletPad() const { return isFuncletPad(getOpcode()); } |
| 134 | |
| 135 | static const char* getOpcodeName(unsigned OpCode); |
| 136 | |
| 137 | static inline bool isTerminator(unsigned OpCode) { |
| 138 | return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; |
| 139 | } |
| 140 | |
| 141 | static inline bool isBinaryOp(unsigned Opcode) { |
| 142 | return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd; |
| 143 | } |
| 144 | |
| 145 | /// Determine if the Opcode is one of the shift instructions. |
| 146 | static inline bool isShift(unsigned Opcode) { |
| 147 | return Opcode >= Shl && Opcode <= AShr; |
| 148 | } |
| 149 | |
| 150 | /// Return true if this is a logical shift left or a logical shift right. |
| 151 | inline bool isLogicalShift() const { |
| 152 | return getOpcode() == Shl || getOpcode() == LShr; |
| 153 | } |
| 154 | |
| 155 | /// Return true if this is an arithmetic shift right. |
| 156 | inline bool isArithmeticShift() const { |
| 157 | return getOpcode() == AShr; |
| 158 | } |
| 159 | |
| 160 | /// Determine if the Opcode is and/or/xor. |
| 161 | static inline bool isBitwiseLogicOp(unsigned Opcode) { |
| 162 | return Opcode == And || Opcode == Or || Opcode == Xor; |
| 163 | } |
| 164 | |
| 165 | /// Return true if this is and/or/xor. |
| 166 | inline bool isBitwiseLogicOp() const { |
| 167 | return isBitwiseLogicOp(getOpcode()); |
| 168 | } |
| 169 | |
| 170 | /// Determine if the OpCode is one of the CastInst instructions. |
| 171 | static inline bool isCast(unsigned OpCode) { |
| 172 | return OpCode >= CastOpsBegin && OpCode < CastOpsEnd; |
| 173 | } |
| 174 | |
| 175 | /// Determine if the OpCode is one of the FuncletPadInst instructions. |
| 176 | static inline bool isFuncletPad(unsigned OpCode) { |
| 177 | return OpCode >= FuncletPadOpsBegin && OpCode < FuncletPadOpsEnd; |
| 178 | } |
| 179 | |
| 180 | //===--------------------------------------------------------------------===// |
| 181 | // Metadata manipulation. |
| 182 | //===--------------------------------------------------------------------===// |
| 183 | |
| 184 | /// Return true if this instruction has any metadata attached to it. |
| 185 | bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); } |
| 186 | |
| 187 | /// Return true if this instruction has metadata attached to it other than a |
| 188 | /// debug location. |
| 189 | bool hasMetadataOtherThanDebugLoc() const { |
| 190 | return hasMetadataHashEntry(); |
| 191 | } |
| 192 | |
| 193 | /// Get the metadata of given kind attached to this Instruction. |
| 194 | /// If the metadata is not found then return null. |
| 195 | MDNode *getMetadata(unsigned KindID) const { |
| 196 | if (!hasMetadata()) return nullptr; |
| 197 | return getMetadataImpl(KindID); |
| 198 | } |
| 199 | |
| 200 | /// Get the metadata of given kind attached to this Instruction. |
| 201 | /// If the metadata is not found then return null. |
| 202 | MDNode *getMetadata(StringRef Kind) const { |
| 203 | if (!hasMetadata()) return nullptr; |
| 204 | return getMetadataImpl(Kind); |
| 205 | } |
| 206 | |
| 207 | /// Get all metadata attached to this Instruction. The first element of each |
| 208 | /// pair returned is the KindID, the second element is the metadata value. |
| 209 | /// This list is returned sorted by the KindID. |
| 210 | void |
| 211 | getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { |
| 212 | if (hasMetadata()) |
| 213 | getAllMetadataImpl(MDs); |
| 214 | } |
| 215 | |
| 216 | /// This does the same thing as getAllMetadata, except that it filters out the |
| 217 | /// debug location. |
| 218 | void getAllMetadataOtherThanDebugLoc( |
| 219 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { |
| 220 | if (hasMetadataOtherThanDebugLoc()) |
| 221 | getAllMetadataOtherThanDebugLocImpl(MDs); |
| 222 | } |
| 223 | |
| 224 | /// Fills the AAMDNodes structure with AA metadata from this instruction. |
| 225 | /// When Merge is true, the existing AA metadata is merged with that from this |
| 226 | /// instruction providing the most-general result. |
| 227 | void getAAMetadata(AAMDNodes &N, bool Merge = false) const; |
| 228 | |
| 229 | /// Set the metadata of the specified kind to the specified node. This updates |
| 230 | /// or replaces metadata if already present, or removes it if Node is null. |
| 231 | void setMetadata(unsigned KindID, MDNode *Node); |
| 232 | void setMetadata(StringRef Kind, MDNode *Node); |
| 233 | |
| 234 | /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty, |
| 235 | /// specifies the list of meta data that needs to be copied. If \p WL is |
| 236 | /// empty, all meta data will be copied. |
| 237 | void copyMetadata(const Instruction &SrcInst, |
| 238 | ArrayRef<unsigned> WL = ArrayRef<unsigned>()); |
| 239 | |
| 240 | /// If the instruction has "branch_weights" MD_prof metadata and the MDNode |
| 241 | /// has three operands (including name string), swap the order of the |
| 242 | /// metadata. |
| 243 | void swapProfMetadata(); |
| 244 | |
| 245 | /// Drop all unknown metadata except for debug locations. |
| 246 | /// @{ |
| 247 | /// Passes are required to drop metadata they don't understand. This is a |
| 248 | /// convenience method for passes to do so. |
| 249 | void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs); |
| 250 | void dropUnknownNonDebugMetadata() { |
| 251 | return dropUnknownNonDebugMetadata(None); |
| 252 | } |
| 253 | void dropUnknownNonDebugMetadata(unsigned ID1) { |
| 254 | return dropUnknownNonDebugMetadata(makeArrayRef(ID1)); |
| 255 | } |
| 256 | void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) { |
| 257 | unsigned IDs[] = {ID1, ID2}; |
| 258 | return dropUnknownNonDebugMetadata(IDs); |
| 259 | } |
| 260 | /// @} |
| 261 | |
| 262 | /// Sets the metadata on this instruction from the AAMDNodes structure. |
| 263 | void setAAMetadata(const AAMDNodes &N); |
| 264 | |
| 265 | /// Retrieve the raw weight values of a conditional branch or select. |
| 266 | /// Returns true on success with profile weights filled in. |
| 267 | /// Returns false if no metadata or invalid metadata was found. |
| 268 | bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const; |
| 269 | |
| 270 | /// Retrieve total raw weight values of a branch. |
| 271 | /// Returns true on success with profile total weights filled in. |
| 272 | /// Returns false if no metadata was found. |
| 273 | bool extractProfTotalWeight(uint64_t &TotalVal) const; |
| 274 | |
| 275 | /// Updates branch_weights metadata by scaling it by \p S / \p T. |
| 276 | void updateProfWeight(uint64_t S, uint64_t T); |
| 277 | |
| 278 | /// Sets the branch_weights metadata to \p W for CallInst. |
| 279 | void setProfWeight(uint64_t W); |
| 280 | |
| 281 | /// Set the debug location information for this instruction. |
| 282 | void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); } |
| 283 | |
| 284 | /// Return the debug location for this node as a DebugLoc. |
| 285 | const DebugLoc &getDebugLoc() const { return DbgLoc; } |
| 286 | |
| 287 | /// Set or clear the nsw flag on this instruction, which must be an operator |
| 288 | /// which supports this flag. See LangRef.html for the meaning of this flag. |
| 289 | void setHasNoUnsignedWrap(bool b = true); |
| 290 | |
| 291 | /// Set or clear the nsw flag on this instruction, which must be an operator |
| 292 | /// which supports this flag. See LangRef.html for the meaning of this flag. |
| 293 | void setHasNoSignedWrap(bool b = true); |
| 294 | |
| 295 | /// Set or clear the exact flag on this instruction, which must be an operator |
| 296 | /// which supports this flag. See LangRef.html for the meaning of this flag. |
| 297 | void setIsExact(bool b = true); |
| 298 | |
| 299 | /// Determine whether the no unsigned wrap flag is set. |
| 300 | bool hasNoUnsignedWrap() const; |
| 301 | |
| 302 | /// Determine whether the no signed wrap flag is set. |
| 303 | bool hasNoSignedWrap() const; |
| 304 | |
| 305 | /// Drops flags that may cause this instruction to evaluate to poison despite |
| 306 | /// having non-poison inputs. |
| 307 | void dropPoisonGeneratingFlags(); |
| 308 | |
| 309 | /// Determine whether the exact flag is set. |
| 310 | bool isExact() const; |
| 311 | |
| 312 | /// Set or clear all fast-math-flags on this instruction, which must be an |
| 313 | /// operator which supports this flag. See LangRef.html for the meaning of |
| 314 | /// this flag. |
| 315 | void setFast(bool B); |
| 316 | |
| 317 | /// Set or clear the reassociation flag on this instruction, which must be |
| 318 | /// an operator which supports this flag. See LangRef.html for the meaning of |
| 319 | /// this flag. |
| 320 | void setHasAllowReassoc(bool B); |
| 321 | |
| 322 | /// Set or clear the no-nans flag on this instruction, which must be an |
| 323 | /// operator which supports this flag. See LangRef.html for the meaning of |
| 324 | /// this flag. |
| 325 | void setHasNoNaNs(bool B); |
| 326 | |
| 327 | /// Set or clear the no-infs flag on this instruction, which must be an |
| 328 | /// operator which supports this flag. See LangRef.html for the meaning of |
| 329 | /// this flag. |
| 330 | void setHasNoInfs(bool B); |
| 331 | |
| 332 | /// Set or clear the no-signed-zeros flag on this instruction, which must be |
| 333 | /// an operator which supports this flag. See LangRef.html for the meaning of |
| 334 | /// this flag. |
| 335 | void setHasNoSignedZeros(bool B); |
| 336 | |
| 337 | /// Set or clear the allow-reciprocal flag on this instruction, which must be |
| 338 | /// an operator which supports this flag. See LangRef.html for the meaning of |
| 339 | /// this flag. |
| 340 | void setHasAllowReciprocal(bool B); |
| 341 | |
| 342 | /// Set or clear the approximate-math-functions flag on this instruction, |
| 343 | /// which must be an operator which supports this flag. See LangRef.html for |
| 344 | /// the meaning of this flag. |
| 345 | void setHasApproxFunc(bool B); |
| 346 | |
| 347 | /// Convenience function for setting multiple fast-math flags on this |
| 348 | /// instruction, which must be an operator which supports these flags. See |
| 349 | /// LangRef.html for the meaning of these flags. |
| 350 | void setFastMathFlags(FastMathFlags FMF); |
| 351 | |
| 352 | /// Convenience function for transferring all fast-math flag values to this |
| 353 | /// instruction, which must be an operator which supports these flags. See |
| 354 | /// LangRef.html for the meaning of these flags. |
| 355 | void copyFastMathFlags(FastMathFlags FMF); |
| 356 | |
| 357 | /// Determine whether all fast-math-flags are set. |
| 358 | bool isFast() const; |
| 359 | |
| 360 | /// Determine whether the allow-reassociation flag is set. |
| 361 | bool hasAllowReassoc() const; |
| 362 | |
| 363 | /// Determine whether the no-NaNs flag is set. |
| 364 | bool hasNoNaNs() const; |
| 365 | |
| 366 | /// Determine whether the no-infs flag is set. |
| 367 | bool hasNoInfs() const; |
| 368 | |
| 369 | /// Determine whether the no-signed-zeros flag is set. |
| 370 | bool hasNoSignedZeros() const; |
| 371 | |
| 372 | /// Determine whether the allow-reciprocal flag is set. |
| 373 | bool hasAllowReciprocal() const; |
| 374 | |
| 375 | /// Determine whether the allow-contract flag is set. |
| 376 | bool hasAllowContract() const; |
| 377 | |
| 378 | /// Determine whether the approximate-math-functions flag is set. |
| 379 | bool hasApproxFunc() const; |
| 380 | |
| 381 | /// Convenience function for getting all the fast-math flags, which must be an |
| 382 | /// operator which supports these flags. See LangRef.html for the meaning of |
| 383 | /// these flags. |
| 384 | FastMathFlags getFastMathFlags() const; |
| 385 | |
| 386 | /// Copy I's fast-math flags |
| 387 | void copyFastMathFlags(const Instruction *I); |
| 388 | |
| 389 | /// Convenience method to copy supported exact, fast-math, and (optionally) |
| 390 | /// wrapping flags from V to this instruction. |
| 391 | void copyIRFlags(const Value *V, bool IncludeWrapFlags = true); |
| 392 | |
| 393 | /// Logical 'and' of any supported wrapping, exact, and fast-math flags of |
| 394 | /// V and this instruction. |
| 395 | void andIRFlags(const Value *V); |
| 396 | |
| 397 | /// Merge 2 debug locations and apply it to the Instruction. If the |
| 398 | /// instruction is a CallIns, we need to traverse the inline chain to find |
| 399 | /// the common scope. This is not efficient for N-way merging as each time |
| 400 | /// you merge 2 iterations, you need to rebuild the hashmap to find the |
| 401 | /// common scope. However, we still choose this API because: |
| 402 | /// 1) Simplicity: it takes 2 locations instead of a list of locations. |
| 403 | /// 2) In worst case, it increases the complexity from O(N*I) to |
| 404 | /// O(2*N*I), where N is # of Instructions to merge, and I is the |
| 405 | /// maximum level of inline stack. So it is still linear. |
| 406 | /// 3) Merging of call instructions should be extremely rare in real |
| 407 | /// applications, thus the N-way merging should be in code path. |
| 408 | /// The DebugLoc attached to this instruction will be overwritten by the |
| 409 | /// merged DebugLoc. |
| 410 | void applyMergedLocation(const DILocation *LocA, const DILocation *LocB); |
| 411 | |
| 412 | private: |
| 413 | /// Return true if we have an entry in the on-the-side metadata hash. |
| 414 | bool hasMetadataHashEntry() const { |
| 415 | return (getSubclassDataFromValue() & HasMetadataBit) != 0; |
| 416 | } |
| 417 | |
| 418 | // These are all implemented in Metadata.cpp. |
| 419 | MDNode *getMetadataImpl(unsigned KindID) const; |
| 420 | MDNode *getMetadataImpl(StringRef Kind) const; |
| 421 | void |
| 422 | getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const; |
| 423 | void getAllMetadataOtherThanDebugLocImpl( |
| 424 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const; |
| 425 | /// Clear all hashtable-based metadata from this instruction. |
| 426 | void clearMetadataHashEntries(); |
| 427 | |
| 428 | public: |
| 429 | //===--------------------------------------------------------------------===// |
| 430 | // Predicates and helper methods. |
| 431 | //===--------------------------------------------------------------------===// |
| 432 | |
| 433 | /// Return true if the instruction is associative: |
| 434 | /// |
| 435 | /// Associative operators satisfy: x op (y op z) === (x op y) op z |
| 436 | /// |
| 437 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. |
| 438 | /// |
| 439 | bool isAssociative() const LLVM_READONLY; |
| 440 | static bool isAssociative(unsigned Opcode) { |
| 441 | return Opcode == And || Opcode == Or || Opcode == Xor || |
| 442 | Opcode == Add || Opcode == Mul; |
| 443 | } |
| 444 | |
| 445 | /// Return true if the instruction is commutative: |
| 446 | /// |
| 447 | /// Commutative operators satisfy: (x op y) === (y op x) |
| 448 | /// |
| 449 | /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when |
| 450 | /// applied to any type. |
| 451 | /// |
| 452 | bool isCommutative() const { return isCommutative(getOpcode()); } |
| 453 | static bool isCommutative(unsigned Opcode) { |
| 454 | switch (Opcode) { |
| 455 | case Add: case FAdd: |
| 456 | case Mul: case FMul: |
| 457 | case And: case Or: case Xor: |
| 458 | return true; |
| 459 | default: |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /// Return true if the instruction is idempotent: |
| 465 | /// |
| 466 | /// Idempotent operators satisfy: x op x === x |
| 467 | /// |
| 468 | /// In LLVM, the And and Or operators are idempotent. |
| 469 | /// |
| 470 | bool isIdempotent() const { return isIdempotent(getOpcode()); } |
| 471 | static bool isIdempotent(unsigned Opcode) { |
| 472 | return Opcode == And || Opcode == Or; |
| 473 | } |
| 474 | |
| 475 | /// Return true if the instruction is nilpotent: |
| 476 | /// |
| 477 | /// Nilpotent operators satisfy: x op x === Id, |
| 478 | /// |
| 479 | /// where Id is the identity for the operator, i.e. a constant such that |
| 480 | /// x op Id === x and Id op x === x for all x. |
| 481 | /// |
| 482 | /// In LLVM, the Xor operator is nilpotent. |
| 483 | /// |
| 484 | bool isNilpotent() const { return isNilpotent(getOpcode()); } |
| 485 | static bool isNilpotent(unsigned Opcode) { |
| 486 | return Opcode == Xor; |
| 487 | } |
| 488 | |
| 489 | /// Return true if this instruction may modify memory. |
| 490 | bool mayWriteToMemory() const; |
| 491 | |
| 492 | /// Return true if this instruction may read memory. |
| 493 | bool mayReadFromMemory() const; |
| 494 | |
| 495 | /// Return true if this instruction may read or write memory. |
| 496 | bool mayReadOrWriteMemory() const { |
| 497 | return mayReadFromMemory() || mayWriteToMemory(); |
| 498 | } |
| 499 | |
| 500 | /// Return true if this instruction has an AtomicOrdering of unordered or |
| 501 | /// higher. |
| 502 | bool isAtomic() const; |
| 503 | |
| 504 | /// Return true if this atomic instruction loads from memory. |
| 505 | bool hasAtomicLoad() const; |
| 506 | |
| 507 | /// Return true if this atomic instruction stores to memory. |
| 508 | bool hasAtomicStore() const; |
| 509 | |
| 510 | /// Return true if this instruction may throw an exception. |
| 511 | bool mayThrow() const; |
| 512 | |
| 513 | /// Return true if this instruction behaves like a memory fence: it can load |
| 514 | /// or store to memory location without being given a memory location. |
| 515 | bool isFenceLike() const { |
| 516 | switch (getOpcode()) { |
| 517 | default: |
| 518 | return false; |
| 519 | // This list should be kept in sync with the list in mayWriteToMemory for |
| 520 | // all opcodes which don't have a memory location. |
| 521 | case Instruction::Fence: |
| 522 | case Instruction::CatchPad: |
| 523 | case Instruction::CatchRet: |
| 524 | case Instruction::Call: |
| 525 | case Instruction::Invoke: |
| 526 | return true; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /// Return true if the instruction may have side effects. |
| 531 | /// |
| 532 | /// Note that this does not consider malloc and alloca to have side |
| 533 | /// effects because the newly allocated memory is completely invisible to |
| 534 | /// instructions which don't use the returned value. For cases where this |
| 535 | /// matters, isSafeToSpeculativelyExecute may be more appropriate. |
| 536 | bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); } |
| 537 | |
| 538 | /// Return true if the instruction can be removed if the result is unused. |
| 539 | /// |
| 540 | /// When constant folding some instructions cannot be removed even if their |
| 541 | /// results are unused. Specifically terminator instructions and calls that |
| 542 | /// may have side effects cannot be removed without semantically changing the |
| 543 | /// generated program. |
| 544 | bool isSafeToRemove() const; |
| 545 | |
| 546 | /// Return true if the instruction is a variety of EH-block. |
| 547 | bool isEHPad() const { |
| 548 | switch (getOpcode()) { |
| 549 | case Instruction::CatchSwitch: |
| 550 | case Instruction::CatchPad: |
| 551 | case Instruction::CleanupPad: |
| 552 | case Instruction::LandingPad: |
| 553 | return true; |
| 554 | default: |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /// Create a copy of 'this' instruction that is identical in all ways except |
| 560 | /// the following: |
| 561 | /// * The instruction has no parent |
| 562 | /// * The instruction has no name |
| 563 | /// |
| 564 | Instruction *clone() const; |
| 565 | |
| 566 | /// Return true if the specified instruction is exactly identical to the |
| 567 | /// current one. This means that all operands match and any extra information |
| 568 | /// (e.g. load is volatile) agree. |
| 569 | bool isIdenticalTo(const Instruction *I) const; |
| 570 | |
| 571 | /// This is like isIdenticalTo, except that it ignores the |
| 572 | /// SubclassOptionalData flags, which may specify conditions under which the |
| 573 | /// instruction's result is undefined. |
| 574 | bool isIdenticalToWhenDefined(const Instruction *I) const; |
| 575 | |
| 576 | /// When checking for operation equivalence (using isSameOperationAs) it is |
| 577 | /// sometimes useful to ignore certain attributes. |
| 578 | enum OperationEquivalenceFlags { |
| 579 | /// Check for equivalence ignoring load/store alignment. |
| 580 | CompareIgnoringAlignment = 1<<0, |
| 581 | /// Check for equivalence treating a type and a vector of that type |
| 582 | /// as equivalent. |
| 583 | CompareUsingScalarTypes = 1<<1 |
| 584 | }; |
| 585 | |
| 586 | /// This function determines if the specified instruction executes the same |
| 587 | /// operation as the current one. This means that the opcodes, type, operand |
| 588 | /// types and any other factors affecting the operation must be the same. This |
| 589 | /// is similar to isIdenticalTo except the operands themselves don't have to |
| 590 | /// be identical. |
| 591 | /// @returns true if the specified instruction is the same operation as |
| 592 | /// the current one. |
| 593 | /// @brief Determine if one instruction is the same operation as another. |
| 594 | bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const; |
| 595 | |
| 596 | /// Return true if there are any uses of this instruction in blocks other than |
| 597 | /// the specified block. Note that PHI nodes are considered to evaluate their |
| 598 | /// operands in the corresponding predecessor block. |
| 599 | bool isUsedOutsideOfBlock(const BasicBlock *BB) const; |
| 600 | |
| 601 | |
| 602 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 603 | static bool classof(const Value *V) { |
| 604 | return V->getValueID() >= Value::InstructionVal; |
| 605 | } |
| 606 | |
| 607 | //---------------------------------------------------------------------- |
| 608 | // Exported enumerations. |
| 609 | // |
| 610 | enum TermOps { // These terminate basic blocks |
| 611 | #define FIRST_TERM_INST(N) TermOpsBegin = N, |
| 612 | #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, |
| 613 | #define LAST_TERM_INST(N) TermOpsEnd = N+1 |
| 614 | #include "llvm/IR/Instruction.def" |
| 615 | }; |
| 616 | |
| 617 | enum BinaryOps { |
| 618 | #define FIRST_BINARY_INST(N) BinaryOpsBegin = N, |
| 619 | #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, |
| 620 | #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 |
| 621 | #include "llvm/IR/Instruction.def" |
| 622 | }; |
| 623 | |
| 624 | enum MemoryOps { |
| 625 | #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, |
| 626 | #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, |
| 627 | #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 |
| 628 | #include "llvm/IR/Instruction.def" |
| 629 | }; |
| 630 | |
| 631 | enum CastOps { |
| 632 | #define FIRST_CAST_INST(N) CastOpsBegin = N, |
| 633 | #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N, |
| 634 | #define LAST_CAST_INST(N) CastOpsEnd = N+1 |
| 635 | #include "llvm/IR/Instruction.def" |
| 636 | }; |
| 637 | |
| 638 | enum FuncletPadOps { |
| 639 | #define FIRST_FUNCLETPAD_INST(N) FuncletPadOpsBegin = N, |
| 640 | #define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N, |
| 641 | #define LAST_FUNCLETPAD_INST(N) FuncletPadOpsEnd = N+1 |
| 642 | #include "llvm/IR/Instruction.def" |
| 643 | }; |
| 644 | |
| 645 | enum OtherOps { |
| 646 | #define FIRST_OTHER_INST(N) OtherOpsBegin = N, |
| 647 | #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, |
| 648 | #define LAST_OTHER_INST(N) OtherOpsEnd = N+1 |
| 649 | #include "llvm/IR/Instruction.def" |
| 650 | }; |
| 651 | |
| 652 | private: |
| 653 | friend class SymbolTableListTraits<Instruction>; |
| 654 | |
| 655 | // Shadow Value::setValueSubclassData with a private forwarding method so that |
| 656 | // subclasses cannot accidentally use it. |
| 657 | void setValueSubclassData(unsigned short D) { |
| 658 | Value::setValueSubclassData(D); |
| 659 | } |
| 660 | |
| 661 | unsigned short getSubclassDataFromValue() const { |
| 662 | return Value::getSubclassDataFromValue(); |
| 663 | } |
| 664 | |
| 665 | void setHasMetadataHashEntry(bool V) { |
| 666 | setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | |
| 667 | (V ? HasMetadataBit : 0)); |
| 668 | } |
| 669 | |
| 670 | void setParent(BasicBlock *P); |
| 671 | |
| 672 | protected: |
| 673 | // Instruction subclasses can stick up to 15 bits of stuff into the |
| 674 | // SubclassData field of instruction with these members. |
| 675 | |
| 676 | // Verify that only the low 15 bits are used. |
| 677 | void setInstructionSubclassData(unsigned short D) { |
| 678 | assert((D & HasMetadataBit) == 0 && "Out of range value put into field"); |
| 679 | setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D); |
| 680 | } |
| 681 | |
| 682 | unsigned getSubclassDataFromInstruction() const { |
| 683 | return getSubclassDataFromValue() & ~HasMetadataBit; |
| 684 | } |
| 685 | |
| 686 | Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, |
| 687 | Instruction *InsertBefore = nullptr); |
| 688 | Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, |
| 689 | BasicBlock *InsertAtEnd); |
| 690 | |
| 691 | private: |
| 692 | /// Create a copy of this instruction. |
| 693 | Instruction *cloneImpl() const; |
| 694 | }; |
| 695 | |
| 696 | inline void ilist_alloc_traits<Instruction>::deleteNode(Instruction *V) { |
| 697 | V->deleteValue(); |
| 698 | } |
| 699 | |
| 700 | } // end namespace llvm |
| 701 | |
| 702 | #endif // LLVM_IR_INSTRUCTION_H |