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 void operator=(const Instruction &); // Do not implement 00031 Instruction(const Instruction &); // Do not implement 00032 00033 BasicBlock *Parent; 00034 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list 00035 00036 void setNext(Instruction *N) { Next = N; } 00037 void setPrev(Instruction *N) { Prev = N; } 00038 00039 friend class SymbolTableListTraits<Instruction, BasicBlock, Function, 00040 ilist_traits<Instruction> >; 00041 void setParent(BasicBlock *P); 00042 00043 private: 00044 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC 00045 // into the opcode field. When they don't, this will be unneeded. 00046 void setOpcode(unsigned NewOpcode); 00047 friend class BinaryOperator; 00048 protected: 00049 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 00050 const std::string &Name = "", 00051 Instruction *InsertBefore = 0); 00052 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 00053 const std::string &Name, BasicBlock *InsertAtEnd); 00054 public: 00055 00056 ~Instruction() { 00057 assert(Parent == 0 && "Instruction still linked in the program!"); 00058 } 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 /// moveBefore - Unlink this instruction from its current basic block and 00100 /// insert it into the basic block that MovePos lives in, right before 00101 /// MovePos. 00102 void moveBefore(Instruction *MovePos); 00103 00104 // --------------------------------------------------------------------------- 00105 /// Subclass classification... getOpcode() returns a member of 00106 /// one of the enums that is coming soon (down below)... 00107 /// 00108 unsigned getOpcode() const { return getValueType() - InstructionVal; } 00109 virtual const char *getOpcodeName() const { 00110 return getOpcodeName(getOpcode()); 00111 } 00112 static const char* getOpcodeName(unsigned OpCode); 00113 00114 static inline bool isTerminator(unsigned OpCode) { 00115 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; 00116 } 00117 00118 inline bool isTerminator() const { // Instance of TerminatorInst? 00119 return isTerminator(getOpcode()); 00120 } 00121 00122 inline bool isBinaryOp() const { 00123 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd; 00124 } 00125 00126 /// isAssociative - Return true if the instruction is associative: 00127 /// 00128 /// Associative operators satisfy: x op (y op z) === (x op y) op z 00129 /// 00130 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when 00131 /// not applied to floating point types. 00132 /// 00133 bool isAssociative() const { return isAssociative(getOpcode(), getType()); } 00134 static bool isAssociative(unsigned op, const Type *Ty); 00135 00136 /// isCommutative - Return true if the instruction is commutative: 00137 /// 00138 /// Commutative operators satisfy: (x op y) === (y op x) 00139 /// 00140 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 00141 /// applied to any type. 00142 /// 00143 bool isCommutative() const { return isCommutative(getOpcode()); } 00144 static bool isCommutative(unsigned op); 00145 00146 /// isRelational - Return true if the instruction is a Set* instruction: 00147 /// 00148 bool isRelational() const { return isRelational(getOpcode()); } 00149 static bool isRelational(unsigned op); 00150 00151 00152 /// isTrappingInstruction - Return true if the instruction may trap. 00153 /// 00154 bool isTrapping() const { 00155 return isTrapping(getOpcode()); 00156 } 00157 static bool isTrapping(unsigned op); 00158 00159 virtual void print(std::ostream &OS) const { print(OS, 0); } 00160 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00161 00162 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00163 static inline bool classof(const Instruction *) { return true; } 00164 static inline bool classof(const Value *V) { 00165 return V->getValueType() >= Value::InstructionVal; 00166 } 00167 00168 //---------------------------------------------------------------------- 00169 // Exported enumerations... 00170 // 00171 enum TermOps { // These terminate basic blocks 00172 #define FIRST_TERM_INST(N) TermOpsBegin = N, 00173 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, 00174 #define LAST_TERM_INST(N) TermOpsEnd = N+1 00175 #include "llvm/Instruction.def" 00176 }; 00177 00178 enum BinaryOps { 00179 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N, 00180 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, 00181 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 00182 #include "llvm/Instruction.def" 00183 }; 00184 00185 enum MemoryOps { 00186 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, 00187 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, 00188 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 00189 #include "llvm/Instruction.def" 00190 }; 00191 00192 enum OtherOps { 00193 #define FIRST_OTHER_INST(N) OtherOpsBegin = N, 00194 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, 00195 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1 00196 #include "llvm/Instruction.def" 00197 }; 00198 }; 00199 00200 } // End llvm namespace 00201 00202 #endif