Add some out-of-line virtual dtors so that the class has a "home", preventing
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // i*.h files...
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
18
19 #include "llvm/Instruction.h"
20
21 namespace llvm {
22
23 //===----------------------------------------------------------------------===//
24 //                            TerminatorInst Class
25 //===----------------------------------------------------------------------===//
26
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.
29 ///
30 class TerminatorInst : public Instruction {
31 protected:
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) {}
38
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) {}
45
46   // Out of line virtual method, so the vtable, etc has a home.
47   ~TerminatorInst();
48
49   /// Virtual methods - Terminators should overload these and provide inline
50   /// overrides of non-V methods.
51   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
52   virtual unsigned getNumSuccessorsV() const = 0;
53   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
54 public:
55
56   virtual Instruction *clone() const = 0;
57
58   /// getNumSuccessors - Return the number of successors that this terminator
59   /// has.
60   unsigned getNumSuccessors() const {
61     return getNumSuccessorsV();
62   }
63
64   /// getSuccessor - Return the specified successor.
65   ///
66   BasicBlock *getSuccessor(unsigned idx) const {
67     return getSuccessorV(idx);
68   }
69
70   /// setSuccessor - Update the specified successor to point at the provided
71   /// block.
72   void setSuccessor(unsigned idx, BasicBlock *B) {
73     setSuccessorV(idx, B);
74   }
75
76   // Methods for support type inquiry through isa, cast, and dyn_cast:
77   static inline bool classof(const TerminatorInst *) { return true; }
78   static inline bool classof(const Instruction *I) {
79     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
80   }
81   static inline bool classof(const Value *V) {
82     return isa<Instruction>(V) && classof(cast<Instruction>(V));
83   }
84 };
85
86 //===----------------------------------------------------------------------===//
87 //                          UnaryInstruction Class
88 //===----------------------------------------------------------------------===//
89
90 class UnaryInstruction : public Instruction {
91   Use Op;
92 protected:
93   UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
94                    const std::string &Name = "", Instruction *IB = 0)
95     : Instruction(Ty, iType, &Op, 1, Name, IB), Op(V, this) {
96   }
97   UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
98                    const std::string &Name, BasicBlock *IAE)
99     : Instruction(Ty, iType, &Op, 1, Name, IAE), Op(V, this) {
100   }
101 public:
102   // Out of line virtual method, so the vtable, etc has a home.
103   ~UnaryInstruction();
104
105   // Transparently provide more efficient getOperand methods.
106   Value *getOperand(unsigned i) const {
107     assert(i == 0 && "getOperand() out of range!");
108     return Op;
109   }
110   void setOperand(unsigned i, Value *Val) {
111     assert(i == 0 && "setOperand() out of range!");
112     Op = Val;
113   }
114   unsigned getNumOperands() const { return 1; }
115 };
116
117 //===----------------------------------------------------------------------===//
118 //                           BinaryOperator Class
119 //===----------------------------------------------------------------------===//
120
121 class BinaryOperator : public Instruction {
122   Use Ops[2];
123 protected:
124   void init(BinaryOps iType);
125   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
126                  const std::string &Name, Instruction *InsertBefore)
127     : Instruction(Ty, iType, Ops, 2, Name, InsertBefore) {
128       Ops[0].init(S1, this);
129       Ops[1].init(S2, this);
130     init(iType);
131   }
132   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
133                  const std::string &Name, BasicBlock *InsertAtEnd)
134     : Instruction(Ty, iType, Ops, 2, Name, InsertAtEnd) {
135     Ops[0].init(S1, this);
136     Ops[1].init(S2, this);
137     init(iType);
138   }
139
140 public:
141
142   /// Transparently provide more efficient getOperand methods.
143   Value *getOperand(unsigned i) const {
144     assert(i < 2 && "getOperand() out of range!");
145     return Ops[i];
146   }
147   void setOperand(unsigned i, Value *Val) {
148     assert(i < 2 && "setOperand() out of range!");
149     Ops[i] = Val;
150   }
151   unsigned getNumOperands() const { return 2; }
152
153   /// create() - Construct a binary instruction, given the opcode and the two
154   /// operands.  Optionally (if InstBefore is specified) insert the instruction
155   /// into a BasicBlock right before the specified instruction.  The specified
156   /// Instruction is allowed to be a dereferenced end iterator.
157   ///
158   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
159                                 const std::string &Name = "",
160                                 Instruction *InsertBefore = 0);
161
162   /// create() - Construct a binary instruction, given the opcode and the two
163   /// operands.  Also automatically insert this instruction to the end of the
164   /// BasicBlock specified.
165   ///
166   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
167                                 const std::string &Name,
168                                 BasicBlock *InsertAtEnd);
169
170   /// create* - These methods just forward to create, and are useful when you
171   /// statically know what type of instruction you're going to create.  These
172   /// helpers just save some typing.
173 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
174   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
175                                      const std::string &Name = "") {\
176     return create(Instruction::OPC, V1, V2, Name);\
177   }
178 #include "llvm/Instruction.def"
179 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
180   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
181                                      const std::string &Name, BasicBlock *BB) {\
182     return create(Instruction::OPC, V1, V2, Name, BB);\
183   }
184 #include "llvm/Instruction.def"
185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
186   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
187                                      const std::string &Name, Instruction *I) {\
188     return create(Instruction::OPC, V1, V2, Name, I);\
189   }
190 #include "llvm/Instruction.def"
191
192
193   /// Helper functions to construct and inspect unary operations (NEG and NOT)
194   /// via binary operators SUB and XOR:
195   ///
196   /// createNeg, createNot - Create the NEG and NOT
197   ///     instructions out of SUB and XOR instructions.
198   ///
199   static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
200                                    Instruction *InsertBefore = 0);
201   static BinaryOperator *createNeg(Value *Op, const std::string &Name,
202                                    BasicBlock *InsertAtEnd);
203   static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
204                                    Instruction *InsertBefore = 0);
205   static BinaryOperator *createNot(Value *Op, const std::string &Name,
206                                    BasicBlock *InsertAtEnd);
207
208   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
209   ///
210   static bool            isNeg(const Value *V);
211   static bool            isNot(const Value *V);
212
213   /// getNegArgument, getNotArgument - Helper functions to extract the
214   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
215   ///
216   static const Value*    getNegArgument(const Value *BinOp);
217   static       Value*    getNegArgument(      Value *BinOp);
218   static const Value*    getNotArgument(const Value *BinOp);
219   static       Value*    getNotArgument(      Value *BinOp);
220
221   BinaryOps getOpcode() const {
222     return static_cast<BinaryOps>(Instruction::getOpcode());
223   }
224
225   virtual BinaryOperator *clone() const;
226
227   /// swapOperands - Exchange the two operands to this instruction.
228   /// This instruction is safe to use on any binary instruction and
229   /// does not modify the semantics of the instruction.  If the
230   /// instruction is order dependent (SetLT f.e.) the opcode is
231   /// changed.  If the instruction cannot be reversed (ie, it's a Div),
232   /// then return true.
233   ///
234   bool swapOperands();
235
236   // Methods for support type inquiry through isa, cast, and dyn_cast:
237   static inline bool classof(const BinaryOperator *) { return true; }
238   static inline bool classof(const Instruction *I) {
239     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
240   }
241   static inline bool classof(const Value *V) {
242     return isa<Instruction>(V) && classof(cast<Instruction>(V));
243   }
244 };
245
246 } // End llvm namespace
247
248 #endif