1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file contains the declaration of the Instruction class, which is the 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// base class for all of the LLVM instructions. 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_INSTRUCTION_H 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_INSTRUCTION_H 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/User.h" 19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/ilist_node.h" 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/DebugLoc.h" 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm { 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass LLVMContext; 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MDNode; 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename ValueSubClass, typename ItemParentClass> 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman class SymbolTableListTraits; 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Instruction : public User, public ilist_node<Instruction> { 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void operator=(const Instruction &); // Do not implement 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction(const Instruction &); // Do not implement 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BasicBlock *Parent; 35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DebugLoc DbgLoc; // 'dbg' Metadata cache. 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum { 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// HasMetadataBit - This is a bit stored in the SubClassData field which 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// indicates whether this instruction has metadata attached to it or not. 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman HasMetadataBit = 1 << 15 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Out of line virtual method, so the vtable, etc has a home. 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~Instruction(); 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// use_back - Specialize the methods defined in Value, as we know that an 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// instruction can only be used by other instructions. 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *use_back() { return cast<Instruction>(*use_begin());} 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const Instruction *use_back() const { return cast<Instruction>(*use_begin());} 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline const BasicBlock *getParent() const { return Parent; } 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline BasicBlock *getParent() { return Parent; } 53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// removeFromParent - This method unlinks 'this' from the containing basic 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// block, but does not delete it. 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void removeFromParent(); 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// eraseFromParent - This method unlinks 'this' from the containing basic 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// block and deletes it. 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void eraseFromParent(); 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// insertBefore - Insert an unlinked instructions into a basic block 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// immediately before the specified instruction. 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void insertBefore(Instruction *InsertPos); 67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// insertAfter - Insert an unlinked instructions into a basic block 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// immediately after the specified instruction. 70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void insertAfter(Instruction *InsertPos); 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// moveBefore - Unlink this instruction from its current basic block and 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// insert it into the basic block that MovePos lives in, right before 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// MovePos. 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void moveBefore(Instruction *MovePos); 76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Subclass classification. 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getOpcode() returns a member of one of the enums like Instruction::Add. 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getOpcode() const { return getValueID() - InstructionVal; } 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const char *getOpcodeName() const { return getOpcodeName(getOpcode()); } 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isTerminator() const { return isTerminator(getOpcode()); } 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isBinaryOp() const { return isBinaryOp(getOpcode()); } 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isShift() { return isShift(getOpcode()); } 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isCast() const { return isCast(getOpcode()); } 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static const char* getOpcodeName(unsigned OpCode); 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool isTerminator(unsigned OpCode) { 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool isBinaryOp(unsigned Opcode) { 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd; 98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @brief Determine if the Opcode is one of the shift instructions. 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool isShift(unsigned Opcode) { 102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Opcode >= Shl && Opcode <= AShr; 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isLogicalShift - Return true if this is a logical shift left or a logical 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// shift right. 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline bool isLogicalShift() const { 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getOpcode() == Shl || getOpcode() == LShr; 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isArithmeticShift - Return true if this is an arithmetic shift right. 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline bool isArithmeticShift() const { 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getOpcode() == AShr; 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @brief Determine if the OpCode is one of the CastInst instructions. 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool isCast(unsigned OpCode) { 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OpCode >= CastOpsBegin && OpCode < CastOpsEnd; 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Metadata manipulation. 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// hasMetadata() - Return true if this instruction has any metadata attached 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// to it. 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool hasMetadata() const { 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return !DbgLoc.isUnknown() || hasMetadataHashEntry(); 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// hasMetadataOtherThanDebugLoc - Return true if this instruction has 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// metadata attached to it other than a debug location. 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool hasMetadataOtherThanDebugLoc() const { 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return hasMetadataHashEntry(); 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getMetadata - Get the metadata of given kind attached to this Instruction. 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// If the metadata is not found then return null. 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode *getMetadata(unsigned KindID) const { 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!hasMetadata()) return 0; 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getMetadataImpl(KindID); 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getMetadata - Get the metadata of given kind attached to this Instruction. 145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// If the metadata is not found then return null. 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode *getMetadata(const char *Kind) const { 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!hasMetadata()) return 0; 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getMetadataImpl(Kind); 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getAllMetadata - Get all metadata attached to this Instruction. The first 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// element of each pair returned is the KindID, the second element is the 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// metadata value. This list is returned sorted by the KindID. 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const{ 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (hasMetadata()) 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman getAllMetadataImpl(MDs); 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getAllMetadataOtherThanDebugLoc - This does the same thing as 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getAllMetadata, except that it filters out the debug location. 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void getAllMetadataOtherThanDebugLoc(SmallVectorImpl<std::pair<unsigned, 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode*> > &MDs) const { 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (hasMetadataOtherThanDebugLoc()) 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman getAllMetadataOtherThanDebugLocImpl(MDs); 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// setMetadata - Set the metadata of the specified kind to the specified 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// node. This updates/replaces metadata if already present, or removes it if 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Node is null. 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setMetadata(unsigned KindID, MDNode *Node); 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setMetadata(const char *Kind, MDNode *Node); 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// setDebugLoc - Set the debug location information for this instruction. 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setDebugLoc(const DebugLoc &Loc) { DbgLoc = Loc; } 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getDebugLoc - Return the debug location for this node as a DebugLoc. 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const DebugLoc &getDebugLoc() const { return DbgLoc; } 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate: 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// metadata hash. 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool hasMetadataHashEntry() const { 183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return (getSubclassDataFromValue() & HasMetadataBit) != 0; 184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // These are all implemented in Metadata.cpp. 187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode *getMetadataImpl(unsigned KindID) const; 188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode *getMetadataImpl(const char *Kind) const; 189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> > &)const; 190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, 191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MDNode*> > &) const; 192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void clearMetadataHashEntries(); 193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Predicates and helper methods. 196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //===--------------------------------------------------------------------===// 197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isAssociative - Return true if the instruction is associative: 200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Associative operators satisfy: x op (y op z) === (x op y) op z 202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. 204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool isAssociative() const { return isAssociative(getOpcode()); } 20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman static bool isAssociative(unsigned op); 207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isCommutative - Return true if the instruction is commutative: 209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Commutative operators satisfy: (x op y) === (y op x) 211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// applied to any type. 214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isCommutative() const { return isCommutative(getOpcode()); } 216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool isCommutative(unsigned op); 217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// mayWriteToMemory - Return true if this instruction may modify memory. 219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool mayWriteToMemory() const; 221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// mayReadFromMemory - Return true if this instruction may read memory. 223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool mayReadFromMemory() const; 225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// mayReadOrWriteMemory - Return true if this instruction may read or 22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// write memory. 22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// 22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool mayReadOrWriteMemory() const { 23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return mayReadFromMemory() || mayWriteToMemory(); 23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// mayThrow - Return true if this instruction may throw an exception. 23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// 23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool mayThrow() const; 23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// mayHaveSideEffects - Return true if the instruction may have side effects. 238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Note that this does not consider malloc and alloca to have side 240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// effects because the newly allocated memory is completely invisible to 241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// instructions which don't used the returned value. For cases where this 242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// matters, isSafeToSpeculativelyExecute may be more appropriate. 243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool mayHaveSideEffects() const { 24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return mayWriteToMemory() || mayThrow(); 245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isSafeToSpeculativelyExecute - Return true if the instruction does not 248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// have any effects besides calculating the result and does not have 249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// undefined behavior. 250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// This method never returns true for an instruction that returns true for 252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// mayHaveSideEffects; however, this method also does some other checks in 253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// addition. It checks for undefined behavior, like dividing by zero or 254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// loading from an invalid pointer (but not for undefined results, like a 255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// shift with a shift amount larger than the width of the result). It checks 256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// for malloc and alloca because speculatively executing them might cause a 257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// memory leak. It also returns false for instructions related to control 258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// flow, specifically terminators and PHI nodes. 259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// This method only looks at the instruction itself and its operands, so if 261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// this method returns true, it is safe to move the instruction as long as 262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// the correct dominance relationships for the operands and users hold. 263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// However, this method can return true for instructions that read memory; 264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// for such instructions, moving them may change the resulting value. 265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isSafeToSpeculativelyExecute() const; 266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// clone() - Create a copy of 'this' instruction that is identical in all 268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ways except the following: 269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// * The instruction has no parent 270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// * The instruction has no name 271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *clone() const; 273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isIdenticalTo - Return true if the specified instruction is exactly 275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// identical to the current one. This means that all operands match and any 276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// extra information (e.g. load is volatile) agree. 277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isIdenticalTo(const Instruction *I) const; 278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it 280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ignores the SubclassOptionalData flags, which specify conditions 281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// under which the instruction's result is undefined. 282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isIdenticalToWhenDefined(const Instruction *I) const; 283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// This function determines if the specified instruction executes the same 285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// operation as the current one. This means that the opcodes, type, operand 286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// types and any other factors affecting the operation must be the same. This 287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// is similar to isIdenticalTo except the operands themselves don't have to 288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// be identical. 289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @returns true if the specified instruction is the same operation as 290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// the current one. 291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @brief Determine if one instruction is the same operation as another. 292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isSameOperationAs(const Instruction *I) const; 293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isUsedOutsideOfBlock - Return true if there are any uses of this 295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// instruction in blocks other than the specified block. Note that PHI nodes 296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// are considered to evaluate their operands in the corresponding predecessor 297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// block. 298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isUsedOutsideOfBlock(const BasicBlock *BB) const; 299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Methods for support type inquiry through isa, cast, and dyn_cast: 302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool classof(const Instruction *) { return true; } 303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool classof(const Value *V) { 304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V->getValueID() >= Value::InstructionVal; 305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //---------------------------------------------------------------------- 308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Exported enumerations. 309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum TermOps { // These terminate basic blocks 311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define FIRST_TERM_INST(N) TermOpsBegin = N, 312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, 313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LAST_TERM_INST(N) TermOpsEnd = N+1 314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.def" 315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum BinaryOps { 318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define FIRST_BINARY_INST(N) BinaryOpsBegin = N, 319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, 320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.def" 322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum MemoryOps { 325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, 326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, 327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.def" 329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum CastOps { 332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define FIRST_CAST_INST(N) CastOpsBegin = N, 333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N, 334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LAST_CAST_INST(N) CastOpsEnd = N+1 335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.def" 336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum OtherOps { 339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define FIRST_OTHER_INST(N) OtherOpsBegin = N, 340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, 341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LAST_OTHER_INST(N) OtherOpsEnd = N+1 342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.def" 343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate: 345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Shadow Value::setValueSubclassData with a private forwarding method so that 346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // subclasses cannot accidentally use it. 347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setValueSubclassData(unsigned short D) { 348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value::setValueSubclassData(D); 349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned short getSubclassDataFromValue() const { 351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Value::getSubclassDataFromValue(); 352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setHasMetadataHashEntry(bool V) { 355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | 356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (V ? HasMetadataBit : 0)); 357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman friend class SymbolTableListTraits<Instruction, BasicBlock>; 360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setParent(BasicBlock *P); 361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected: 362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Instruction subclasses can stick up to 15 bits of stuff into the 363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SubclassData field of instruction with these members. 364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Verify that only the low 15 bits are used. 366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setInstructionSubclassData(unsigned short D) { 367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert((D & HasMetadataBit) == 0 && "Out of range value put into field"); 368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D); 369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getSubclassDataFromInstruction() const { 372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getSubclassDataFromValue() & ~HasMetadataBit; 373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *InsertBefore = 0); 37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BasicBlock *InsertAtEnd); 379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual Instruction *clone_impl() const = 0; 380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Instruction* is only 4-byte aligned. 384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<> 385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass PointerLikeTypeTraits<Instruction*> { 386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef Instruction* PT; 387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline void *getAsVoidPointer(PT P) { return P; } 389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline PT getFromVoidPointer(void *P) { 390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<PT>(P); 391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman enum { NumLowBitsAvailable = 2 }; 393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace 396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 398