1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various meta classes of instructions that exist in the VM
11 // representation. Specific concrete subclasses of these may be found in the
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
19 #include "llvm/Instruction.h"
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
27 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
28 /// block. Thus, these are all the flow control type of operations.
30 class TerminatorInst : public Instruction {
32 TerminatorInst(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
33 Instruction *InsertBefore = 0);
34 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
35 Use *Ops, unsigned NumOps,
36 const std::string &Name = "", Instruction *InsertBefore = 0)
37 : Instruction(Ty, iType, Ops, NumOps, Name, InsertBefore) {}
39 TerminatorInst(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
40 BasicBlock *InsertAtEnd);
41 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
42 Use *Ops, unsigned NumOps,
43 const std::string &Name, BasicBlock *InsertAtEnd)
44 : Instruction(Ty, iType, Ops, NumOps, Name, InsertAtEnd) {}
46 /// Virtual methods - Terminators should overload these and provide inline
47 /// overrides of non-V methods.
48 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
49 virtual unsigned getNumSuccessorsV() const = 0;
50 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
53 virtual Instruction *clone() const = 0;
55 /// getNumSuccessors - Return the number of successors that this terminator
57 unsigned getNumSuccessors() const {
58 return getNumSuccessorsV();
61 /// getSuccessor - Return the specified successor.
63 BasicBlock *getSuccessor(unsigned idx) const {
64 return getSuccessorV(idx);
67 /// setSuccessor - Update the specified successor to point at the provided
69 void setSuccessor(unsigned idx, BasicBlock *B) {
70 setSuccessorV(idx, B);
73 // Methods for support type inquiry through isa, cast, and dyn_cast:
74 static inline bool classof(const TerminatorInst *) { return true; }
75 static inline bool classof(const Instruction *I) {
76 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
78 static inline bool classof(const Value *V) {
79 return isa<Instruction>(V) && classof(cast<Instruction>(V));
83 //===----------------------------------------------------------------------===//
84 // UnaryInstruction Class
85 //===----------------------------------------------------------------------===//
87 class UnaryInstruction : public Instruction {
90 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
91 const std::string &Name = "", Instruction *IB = 0)
92 : Instruction(Ty, iType, &Op, 1, Name, IB), Op(V, this) {
94 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
95 const std::string &Name, BasicBlock *IAE)
96 : Instruction(Ty, iType, &Op, 1, Name, IAE), Op(V, this) {
100 // Transparently provide more efficient getOperand methods.
101 Value *getOperand(unsigned i) const {
102 assert(i == 0 && "getOperand() out of range!");
105 void setOperand(unsigned i, Value *Val) {
106 assert(i == 0 && "setOperand() out of range!");
109 unsigned getNumOperands() const { return 1; }
112 //===----------------------------------------------------------------------===//
113 // BinaryOperator Class
114 //===----------------------------------------------------------------------===//
116 class BinaryOperator : public Instruction {
119 void init(BinaryOps iType);
120 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
121 const std::string &Name, Instruction *InsertBefore)
122 : Instruction(Ty, iType, Ops, 2, Name, InsertBefore) {
123 Ops[0].init(S1, this);
124 Ops[1].init(S2, this);
127 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
128 const std::string &Name, BasicBlock *InsertAtEnd)
129 : Instruction(Ty, iType, Ops, 2, Name, InsertAtEnd) {
130 Ops[0].init(S1, this);
131 Ops[1].init(S2, this);
137 /// Transparently provide more efficient getOperand methods.
138 Value *getOperand(unsigned i) const {
139 assert(i < 2 && "getOperand() out of range!");
142 void setOperand(unsigned i, Value *Val) {
143 assert(i < 2 && "setOperand() out of range!");
146 unsigned getNumOperands() const { return 2; }
148 /// create() - Construct a binary instruction, given the opcode and the two
149 /// operands. Optionally (if InstBefore is specified) insert the instruction
150 /// into a BasicBlock right before the specified instruction. The specified
151 /// Instruction is allowed to be a dereferenced end iterator.
153 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
154 const std::string &Name = "",
155 Instruction *InsertBefore = 0);
157 /// create() - Construct a binary instruction, given the opcode and the two
158 /// operands. Also automatically insert this instruction to the end of the
159 /// BasicBlock specified.
161 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
162 const std::string &Name,
163 BasicBlock *InsertAtEnd);
165 /// create* - These methods just forward to create, and are useful when you
166 /// statically know what type of instruction you're going to create. These
167 /// helpers just save some typing.
168 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
169 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
170 const std::string &Name = "") {\
171 return create(Instruction::OPC, V1, V2, Name);\
173 #include "llvm/Instruction.def"
174 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
175 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
176 const std::string &Name, BasicBlock *BB) {\
177 return create(Instruction::OPC, V1, V2, Name, BB);\
179 #include "llvm/Instruction.def"
180 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
181 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
182 const std::string &Name, Instruction *I) {\
183 return create(Instruction::OPC, V1, V2, Name, I);\
185 #include "llvm/Instruction.def"
188 /// Helper functions to construct and inspect unary operations (NEG and NOT)
189 /// via binary operators SUB and XOR:
191 /// createNeg, createNot - Create the NEG and NOT
192 /// instructions out of SUB and XOR instructions.
194 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
195 Instruction *InsertBefore = 0);
196 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
197 BasicBlock *InsertAtEnd);
198 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
199 Instruction *InsertBefore = 0);
200 static BinaryOperator *createNot(Value *Op, const std::string &Name,
201 BasicBlock *InsertAtEnd);
203 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
205 static bool isNeg(const Value *V);
206 static bool isNot(const Value *V);
208 /// getNegArgument, getNotArgument - Helper functions to extract the
209 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
211 static const Value* getNegArgument(const Value *BinOp);
212 static Value* getNegArgument( Value *BinOp);
213 static const Value* getNotArgument(const Value *BinOp);
214 static Value* getNotArgument( Value *BinOp);
216 BinaryOps getOpcode() const {
217 return static_cast<BinaryOps>(Instruction::getOpcode());
220 virtual BinaryOperator *clone() const;
222 /// swapOperands - Exchange the two operands to this instruction.
223 /// This instruction is safe to use on any binary instruction and
224 /// does not modify the semantics of the instruction. If the
225 /// instruction is order dependent (SetLT f.e.) the opcode is
226 /// changed. If the instruction cannot be reversed (ie, it's a Div),
227 /// then return true.
231 // Methods for support type inquiry through isa, cast, and dyn_cast:
232 static inline bool classof(const BinaryOperator *) { return true; }
233 static inline bool classof(const Instruction *I) {
234 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
236 static inline bool classof(const Value *V) {
237 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 } // End llvm namespace