LLVM API Documentation
00001 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the Instruction class, which is the 00011 // base class for all of the LLVM instructions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_INSTRUCTION_H 00016 #define LLVM_INSTRUCTION_H 00017 00018 #include "llvm/User.h" 00019 00020 namespace llvm { 00021 00022 struct AssemblyAnnotationWriter; 00023 class BinaryOperator; 00024 00025 template<typename SC> struct ilist_traits; 00026 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass, 00027 typename SubClass> class SymbolTableListTraits; 00028 00029 class Instruction : public User { 00030 BasicBlock *Parent; 00031 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list 00032 00033 void setNext(Instruction *N) { Next = N; } 00034 void setPrev(Instruction *N) { Prev = N; } 00035 00036 friend class SymbolTableListTraits<Instruction, BasicBlock, Function, 00037 ilist_traits<Instruction> >; 00038 void setParent(BasicBlock *P); 00039 void init(); 00040 00041 private: 00042 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC 00043 // into the opcode field. When they don't, this will be unneeded. 00044 void setOpcode(unsigned NewOpcode); 00045 friend class BinaryOperator; 00046 protected: 00047 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "", 00048 Instruction *InsertBefore = 0); 00049 Instruction(const Type *Ty, unsigned iType, const std::string &Name, 00050 BasicBlock *InsertAtEnd); 00051 public: 00052 00053 ~Instruction() { 00054 assert(Parent == 0 && "Instruction still linked in the program!"); 00055 } 00056 00057 // Specialize setName to handle symbol table majik... 00058 virtual void setName(const std::string &name, SymbolTable *ST = 0); 00059 00060 /// mayWriteToMemory - Return true if this instruction may modify memory. 00061 /// 00062 virtual bool mayWriteToMemory() const { return false; } 00063 00064 /// clone() - Create a copy of 'this' instruction that is identical in all 00065 /// ways except the following: 00066 /// * The instruction has no parent 00067 /// * The instruction has no name 00068 /// 00069 virtual Instruction *clone() const = 0; 00070 00071 /// isIdenticalTo - Return true if the specified instruction is exactly 00072 /// identical to the current one. This means that all operands match and any 00073 /// extra information (e.g. load is volatile) agree. 00074 bool isIdenticalTo(Instruction *I) const; 00075 00076 00077 // Accessor methods... 00078 // 00079 inline const BasicBlock *getParent() const { return Parent; } 00080 inline BasicBlock *getParent() { return Parent; } 00081 00082 // getNext/Prev - Return the next or previous instruction in the list. The 00083 // last node in the list is a terminator instruction. 00084 Instruction *getNext() { return Next; } 00085 const Instruction *getNext() const { return Next; } 00086 Instruction *getPrev() { return Prev; } 00087 const Instruction *getPrev() const { return Prev; } 00088 00089 /// removeFromParent - This method unlinks 'this' from the containing basic 00090 /// block, but does not delete it. 00091 /// 00092 void removeFromParent(); 00093 00094 /// eraseFromParent - This method unlinks 'this' from the containing basic 00095 /// block and deletes it. 00096 /// 00097 void eraseFromParent(); 00098 00099 // --------------------------------------------------------------------------- 00100 /// Subclass classification... getOpcode() returns a member of 00101 /// one of the enums that is coming soon (down below)... 00102 /// 00103 unsigned getOpcode() const { return getValueType() - InstructionVal; } 00104 virtual const char *getOpcodeName() const { 00105 return getOpcodeName(getOpcode()); 00106 } 00107 static const char* getOpcodeName(unsigned OpCode); 00108 00109 static inline bool isTerminator(unsigned OpCode) { 00110 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; 00111 } 00112 00113 inline bool isTerminator() const { // Instance of TerminatorInst? 00114 return isTerminator(getOpcode()); 00115 } 00116 00117 inline bool isBinaryOp() const { 00118 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd; 00119 } 00120 00121 /// isAssociative - Return true if the instruction is associative: 00122 /// 00123 /// Associative operators satisfy: x op (y op z) === (x op y) op z 00124 /// 00125 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when 00126 /// not applied to floating point types. 00127 /// 00128 bool isAssociative() const { return isAssociative(getOpcode(), getType()); } 00129 static bool isAssociative(unsigned op, const Type *Ty); 00130 00131 /// isCommutative - Return true if the instruction is commutative: 00132 /// 00133 /// Commutative operators satisfy: (x op y) === (y op x) 00134 /// 00135 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 00136 /// applied to any type. 00137 /// 00138 bool isCommutative() const { return isCommutative(getOpcode()); } 00139 static bool isCommutative(unsigned op); 00140 00141 /// isRelational - Return true if the instruction is a Set* instruction: 00142 /// 00143 bool isRelational() const { return isRelational(getOpcode()); } 00144 static bool isRelational(unsigned op); 00145 00146 00147 /// isTrappingInstruction - Return true if the instruction may trap. 00148 /// 00149 bool isTrapping() const { 00150 return isTrapping(getOpcode()); 00151 } 00152 static bool isTrapping(unsigned op); 00153 00154 virtual void print(std::ostream &OS) const { print(OS, 0); } 00155 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00156 00157 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00158 static inline bool classof(const Instruction *I) { return true; } 00159 static inline bool classof(const Value *V) { 00160 return V->getValueType() >= Value::InstructionVal; 00161 } 00162 00163 //---------------------------------------------------------------------- 00164 // Exported enumerations... 00165 // 00166 enum TermOps { // These terminate basic blocks 00167 #define FIRST_TERM_INST(N) TermOpsBegin = N, 00168 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, 00169 #define LAST_TERM_INST(N) TermOpsEnd = N+1, 00170 #include "llvm/Instruction.def" 00171 }; 00172 00173 enum BinaryOps { 00174 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N, 00175 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, 00176 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1, 00177 #include "llvm/Instruction.def" 00178 }; 00179 00180 enum MemoryOps { 00181 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, 00182 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, 00183 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1, 00184 #include "llvm/Instruction.def" 00185 }; 00186 00187 enum OtherOps { 00188 #define FIRST_OTHER_INST(N) OtherOpsBegin = N, 00189 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, 00190 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1, 00191 #include "llvm/Instruction.def" 00192 }; 00193 }; 00194 00195 } // End llvm namespace 00196 00197 #endif