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