Instruction.h revision 12ddd409535b52a7fa5157ded9a4cedd161fedb6
1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===// 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The LLVM Compiler Infrastructure 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This file is distributed under the University of Illinois Open Source 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// License. See LICENSE.TXT for details. 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//===----------------------------------------------------------------------===// 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This file contains the declaration of the Instruction class, which is the 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// base class for all of the LLVM instructions. 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//===----------------------------------------------------------------------===// 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef LLVM_INSTRUCTION_H 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define LLVM_INSTRUCTION_H 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "llvm/User.h" 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "llvm/ADT/ilist_node.h" 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace llvm { 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass LLVMContext; 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<typename ValueSubClass, typename ItemParentClass> 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org class SymbolTableListTraits; 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass Instruction : public User, public ilist_node<Instruction> { 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void operator=(const Instruction &); // Do not implement 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Instruction(const Instruction &); // Do not implement 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org BasicBlock *Parent; 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org friend class SymbolTableListTraits<Instruction, BasicBlock>; 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void setParent(BasicBlock *P); 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprotected: 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Instruction *InsertBefore = 0); 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org BasicBlock *InsertAtEnd); 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic: 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Out of line virtual method, so the vtable, etc has a home. 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ~Instruction(); 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// clone() - Create a copy of 'this' instruction that is identical in all 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// ways except the following: 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// * The instruction has no parent 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// * The instruction has no name 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual Instruction *clone(LLVMContext &Context) const = 0; 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// isIdenticalTo - Return true if the specified instruction is exactly 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// identical to the current one. This means that all operands match and any 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// extra information (e.g. load is volatile) agree. 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isIdenticalTo(const Instruction *I) const; 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// This function determines if the specified instruction executes the same 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// operation as the current one. This means that the opcodes, type, operand 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// types and any other factors affecting the operation must be the same. This 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// is similar to isIdenticalTo except the operands themselves don't have to 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// be identical. 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// @returns true if the specified instruction is the same operation as 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// the current one. 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// @brief Determine if one instruction is the same operation as another. 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isSameOperationAs(const Instruction *I) const; 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// isUsedOutsideOfBlock - Return true if there are any uses of this 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// instruction in blocks other than the specified block. Note that PHI nodes 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// are considered to evaluate their operands in the corresponding predecessor 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// block. 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isUsedOutsideOfBlock(const BasicBlock *BB) const; 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// use_back - Specialize the methods defined in Value, as we know that an 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// instruction can only be used by other instructions. 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Instruction *use_back() { return cast<Instruction>(*use_begin());} 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Instruction *use_back() const { return cast<Instruction>(*use_begin());} 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Accessor methods... 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org inline const BasicBlock *getParent() const { return Parent; } 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org inline BasicBlock *getParent() { return Parent; } 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// removeFromParent - This method unlinks 'this' from the containing basic 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// block, but does not delete it. 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void removeFromParent(); 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// eraseFromParent - This method unlinks 'this' from the containing basic 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// block and deletes it. 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void eraseFromParent(); 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// insertBefore - Insert an unlinked instructions into a basic block 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// immediately before the specified instruction. 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void insertBefore(Instruction *InsertPos); 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// insertAfter - Insert an unlinked instructions into a basic block 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// immediately after the specified instruction. 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void insertAfter(Instruction *InsertPos); 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// moveBefore - Unlink this instruction from its current basic block and 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// insert it into the basic block that MovePos lives in, right before 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// MovePos. 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void moveBefore(Instruction *MovePos); 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // --------------------------------------------------------------------------- 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// Subclass classification... getOpcode() returns a member of 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// one of the enums that is coming soon (down below)... 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned getOpcode() const { return getValueID() - InstructionVal; } 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char *getOpcodeName() const { return getOpcodeName(getOpcode()); } 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isTerminator() const { return isTerminator(getOpcode()); } 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isBinaryOp() const { return isBinaryOp(getOpcode()); } 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isShift() { return isShift(getOpcode()); } 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool isCast() const { return isCast(getOpcode()); } 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static const char* getOpcodeName(unsigned OpCode); 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static inline bool isTerminator(unsigned OpCode) { 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static inline bool isBinaryOp(unsigned Opcode) { 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd; 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// @brief Determine if the Opcode is one of the shift instructions. 131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static inline bool isShift(unsigned Opcode) { 132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return Opcode >= Shl && Opcode <= AShr; 133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /// isLogicalShift - Return true if this is a logical shift left or a logical 136 /// shift right. 137 inline bool isLogicalShift() const { 138 return getOpcode() == Shl || getOpcode() == LShr; 139 } 140 141 /// isArithmeticShift - Return true if this is an arithmetic shift right. 142 inline bool isArithmeticShift() const { 143 return getOpcode() == AShr; 144 } 145 146 /// @brief Determine if the OpCode is one of the CastInst instructions. 147 static inline bool isCast(unsigned OpCode) { 148 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd; 149 } 150 151 /// isAssociative - Return true if the instruction is associative: 152 /// 153 /// Associative operators satisfy: x op (y op z) === (x op y) op z 154 /// 155 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when 156 /// not applied to floating point types. 157 /// 158 bool isAssociative() const { return isAssociative(getOpcode(), getType()); } 159 static bool isAssociative(unsigned op, const Type *Ty); 160 161 /// isCommutative - Return true if the instruction is commutative: 162 /// 163 /// Commutative operators satisfy: (x op y) === (y op x) 164 /// 165 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 166 /// applied to any type. 167 /// 168 bool isCommutative() const { return isCommutative(getOpcode()); } 169 static bool isCommutative(unsigned op); 170 171 /// mayWriteToMemory - Return true if this instruction may modify memory. 172 /// 173 bool mayWriteToMemory() const; 174 175 /// mayReadFromMemory - Return true if this instruction may read memory. 176 /// 177 bool mayReadFromMemory() const; 178 179 /// mayThrow - Return true if this instruction may throw an exception. 180 /// 181 bool mayThrow() const; 182 183 /// mayHaveSideEffects - Return true if the instruction may have side effects. 184 /// 185 /// Note that this does not consider malloc and alloca to have side 186 /// effects because the newly allocated memory is completely invisible to 187 /// instructions which don't used the returned value. For cases where this 188 /// matters, isSafeToSpeculativelyExecute may be more appropriate. 189 bool mayHaveSideEffects() const { 190 return mayWriteToMemory() || mayThrow(); 191 } 192 193 /// isSafeToSpeculativelyExecute - Return true if the instruction does not 194 /// have any effects besides calculating the result and does not have 195 /// undefined behavior. 196 /// 197 /// This method never returns true for an instruction that returns true for 198 /// mayHaveSideEffects; however, this method also does some other checks in 199 /// addition. It checks for undefined behavior, like dividing by zero or 200 /// loading from an invalid pointer (but not for undefined results, like a 201 /// shift with a shift amount larger than the width of the result). It checks 202 /// for malloc and alloca because speculatively executing them might cause a 203 /// memory leak. It also returns false for instructions related to control 204 /// flow, specifically terminators and PHI nodes. 205 /// 206 /// This method only looks at the instruction itself and its operands, so if 207 /// this method returns true, it is safe to move the instruction as long as 208 /// the correct dominance relationships for the operands and users hold. 209 /// However, this method can return true for instructions that read memory; 210 /// for such instructions, moving them may change the resulting value. 211 bool isSafeToSpeculativelyExecute() const; 212 213 /// Methods for support type inquiry through isa, cast, and dyn_cast: 214 static inline bool classof(const Instruction *) { return true; } 215 static inline bool classof(const Value *V) { 216 return V->getValueID() >= Value::InstructionVal; 217 } 218 219 //---------------------------------------------------------------------- 220 // Exported enumerations... 221 // 222 enum TermOps { // These terminate basic blocks 223#define FIRST_TERM_INST(N) TermOpsBegin = N, 224#define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, 225#define LAST_TERM_INST(N) TermOpsEnd = N+1 226#include "llvm/Instruction.def" 227 }; 228 229 enum BinaryOps { 230#define FIRST_BINARY_INST(N) BinaryOpsBegin = N, 231#define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, 232#define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 233#include "llvm/Instruction.def" 234 }; 235 236 enum MemoryOps { 237#define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, 238#define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, 239#define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 240#include "llvm/Instruction.def" 241 }; 242 243 enum CastOps { 244#define FIRST_CAST_INST(N) CastOpsBegin = N, 245#define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N, 246#define LAST_CAST_INST(N) CastOpsEnd = N+1 247#include "llvm/Instruction.def" 248 }; 249 250 enum OtherOps { 251#define FIRST_OTHER_INST(N) OtherOpsBegin = N, 252#define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, 253#define LAST_OTHER_INST(N) OtherOpsEnd = N+1 254#include "llvm/Instruction.def" 255 }; 256}; 257 258// Instruction* is only 4-byte aligned. 259template<> 260class PointerLikeTypeTraits<Instruction*> { 261 typedef Instruction* PT; 262public: 263 static inline void *getAsVoidPointer(PT P) { return P; } 264 static inline PT getFromVoidPointer(void *P) { 265 return static_cast<PT>(P); 266 } 267 enum { NumLowBitsAvailable = 2 }; 268}; 269 270} // End llvm namespace 271 272#endif 273