LLVM API Documentation
00001 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- 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 defines various meta classes of instructions that exist in the VM 00011 // representation. Specific concrete subclasses of these may be found in the 00012 // i*.h files... 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_INSTRUCTION_TYPES_H 00017 #define LLVM_INSTRUCTION_TYPES_H 00018 00019 #include "llvm/Instruction.h" 00020 00021 namespace llvm { 00022 00023 //===----------------------------------------------------------------------===// 00024 // TerminatorInst Class 00025 //===----------------------------------------------------------------------===// 00026 00027 /// TerminatorInst - Subclasses of this class are all able to terminate a basic 00028 /// block. Thus, these are all the flow control type of operations. 00029 /// 00030 class TerminatorInst : public Instruction { 00031 protected: 00032 TerminatorInst(Instruction::TermOps iType, Instruction *InsertBefore = 0); 00033 TerminatorInst(const Type *Ty, Instruction::TermOps iType, 00034 const std::string &Name = "", Instruction *InsertBefore = 0) 00035 : Instruction(Ty, iType, Name, InsertBefore) {} 00036 00037 TerminatorInst(Instruction::TermOps iType, BasicBlock *InsertAtEnd); 00038 TerminatorInst(const Type *Ty, Instruction::TermOps iType, 00039 const std::string &Name, BasicBlock *InsertAtEnd) 00040 : Instruction(Ty, iType, Name, InsertAtEnd) {} 00041 00042 public: 00043 00044 /// Terminators must implement the methods required by Instruction... 00045 virtual Instruction *clone() const = 0; 00046 00047 /// Additionally, they must provide a method to get at the successors of this 00048 /// terminator instruction. 'idx' may not be >= the number of successors 00049 /// returned by getNumSuccessors()! 00050 /// 00051 virtual const BasicBlock *getSuccessor(unsigned idx) const = 0; 00052 virtual unsigned getNumSuccessors() const = 0; 00053 00054 /// Set a successor at a given index 00055 virtual void setSuccessor(unsigned idx, BasicBlock *B) = 0; 00056 00057 inline BasicBlock *getSuccessor(unsigned idx) { 00058 const TerminatorInst *TI = this; 00059 return const_cast<BasicBlock*>(TI->getSuccessor(idx)); 00060 } 00061 00062 // Methods for support type inquiry through isa, cast, and dyn_cast: 00063 static inline bool classof(const TerminatorInst *) { return true; } 00064 static inline bool classof(const Instruction *I) { 00065 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd; 00066 } 00067 static inline bool classof(const Value *V) { 00068 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00069 } 00070 }; 00071 00072 00073 //===----------------------------------------------------------------------===// 00074 // BinaryOperator Class 00075 //===----------------------------------------------------------------------===// 00076 00077 class BinaryOperator : public Instruction { 00078 protected: 00079 void init(BinaryOps iType, Value *S1, Value *S2); 00080 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, 00081 const std::string &Name, Instruction *InsertBefore) 00082 : Instruction(Ty, iType, Name, InsertBefore) { 00083 init(iType, S1, S2); 00084 } 00085 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, 00086 const std::string &Name, BasicBlock *InsertAtEnd) 00087 : Instruction(Ty, iType, Name, InsertAtEnd) { 00088 init(iType, S1, S2); 00089 } 00090 00091 public: 00092 00093 /// create() - Construct a binary instruction, given the opcode and the two 00094 /// operands. Optionally (if InstBefore is specified) insert the instruction 00095 /// into a BasicBlock right before the specified instruction. The specified 00096 /// Instruction is allowed to be a dereferenced end iterator. 00097 /// 00098 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2, 00099 const std::string &Name = "", 00100 Instruction *InsertBefore = 0); 00101 00102 /// create() - Construct a binary instruction, given the opcode and the two 00103 /// operands. Also automatically insert this instruction to the end of the 00104 /// BasicBlock specified. 00105 /// 00106 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2, 00107 const std::string &Name, 00108 BasicBlock *InsertAtEnd); 00109 00110 /// create* - These methods just forward to create, and are useful when you 00111 /// statically know what type of instruction you're going to create. These 00112 /// helpers just save some typing. 00113 #define HANDLE_BINARY_INST(N, OPC, CLASS) \ 00114 static BinaryOperator *create##OPC(Value *V1, Value *V2, \ 00115 const std::string &Name = "") {\ 00116 return create(Instruction::OPC, V1, V2, Name);\ 00117 } 00118 #include "llvm/Instruction.def" 00119 #define HANDLE_BINARY_INST(N, OPC, CLASS) \ 00120 static BinaryOperator *create##OPC(Value *V1, Value *V2, \ 00121 const std::string &Name, BasicBlock *BB) {\ 00122 return create(Instruction::OPC, V1, V2, Name, BB);\ 00123 } 00124 #include "llvm/Instruction.def" 00125 #define HANDLE_BINARY_INST(N, OPC, CLASS) \ 00126 static BinaryOperator *create##OPC(Value *V1, Value *V2, \ 00127 const std::string &Name, Instruction *I) {\ 00128 return create(Instruction::OPC, V1, V2, Name, I);\ 00129 } 00130 #include "llvm/Instruction.def" 00131 00132 00133 /// Helper functions to construct and inspect unary operations (NEG and NOT) 00134 /// via binary operators SUB and XOR: 00135 /// 00136 /// createNeg, createNot - Create the NEG and NOT 00137 /// instructions out of SUB and XOR instructions. 00138 /// 00139 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "", 00140 Instruction *InsertBefore = 0); 00141 static BinaryOperator *createNeg(Value *Op, const std::string &Name, 00142 BasicBlock *InsertAtEnd); 00143 static BinaryOperator *createNot(Value *Op, const std::string &Name = "", 00144 Instruction *InsertBefore = 0); 00145 static BinaryOperator *createNot(Value *Op, const std::string &Name, 00146 BasicBlock *InsertAtEnd); 00147 00148 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction. 00149 /// 00150 static bool isNeg(const Value *V); 00151 static bool isNot(const Value *V); 00152 00153 /// getNegArgument, getNotArgument - Helper functions to extract the 00154 /// unary argument of a NEG or NOT operation implemented via Sub or Xor. 00155 /// 00156 static const Value* getNegArgument(const BinaryOperator* Bop); 00157 static Value* getNegArgument( BinaryOperator* Bop); 00158 static const Value* getNotArgument(const BinaryOperator* Bop); 00159 static Value* getNotArgument( BinaryOperator* Bop); 00160 00161 BinaryOps getOpcode() const { 00162 return static_cast<BinaryOps>(Instruction::getOpcode()); 00163 } 00164 00165 virtual BinaryOperator *clone() const; 00166 00167 /// swapOperands - Exchange the two operands to this instruction. 00168 /// This instruction is safe to use on any binary instruction and 00169 /// does not modify the semantics of the instruction. If the 00170 /// instruction is order dependent (SetLT f.e.) the opcode is 00171 /// changed. If the instruction cannot be reversed (ie, it's a Div), 00172 /// then return true. 00173 /// 00174 bool swapOperands(); 00175 00176 // Methods for support type inquiry through isa, cast, and dyn_cast: 00177 static inline bool classof(const BinaryOperator *) { return true; } 00178 static inline bool classof(const Instruction *I) { 00179 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd; 00180 } 00181 static inline bool classof(const Value *V) { 00182 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00183 } 00184 }; 00185 00186 } // End llvm namespace 00187 00188 #endif