Convert tabs to spaces
[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   /// 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;
51 public:
52
53   virtual Instruction *clone() const = 0;
54
55   /// getNumSuccessors - Return the number of successors that this terminator
56   /// has.
57   unsigned getNumSuccessors() const {
58     return getNumSuccessorsV();
59   }
60
61   /// getSuccessor - Return the specified successor.
62   ///
63   BasicBlock *getSuccessor(unsigned idx) const {
64     return getSuccessorV(idx);
65   }
66
67   /// setSuccessor - Update the specified successor to point at the provided
68   /// block.
69   void setSuccessor(unsigned idx, BasicBlock *B) {
70     setSuccessorV(idx, B);
71   }
72
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;
77   }
78   static inline bool classof(const Value *V) {
79     return isa<Instruction>(V) && classof(cast<Instruction>(V));
80   }
81 };
82
83 //===----------------------------------------------------------------------===//
84 //                          UnaryInstruction Class
85 //===----------------------------------------------------------------------===//
86
87 class UnaryInstruction : public Instruction {
88   Use Op;
89 protected:
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) {
93   }
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) {
97   }
98 public:
99
100   // Transparently provide more efficient getOperand methods.
101   Value *getOperand(unsigned i) const {
102     assert(i == 0 && "getOperand() out of range!");
103     return Op;
104   }
105   void setOperand(unsigned i, Value *Val) {
106     assert(i == 0 && "setOperand() out of range!");
107     Op = Val;
108   }
109   unsigned getNumOperands() const { return 1; }
110 };
111
112 //===----------------------------------------------------------------------===//
113 //                           BinaryOperator Class
114 //===----------------------------------------------------------------------===//
115
116 class BinaryOperator : public Instruction {
117   Use Ops[2];
118 protected:
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);
125     init(iType);
126   }
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);
132     init(iType);
133   }
134
135 public:
136
137   /// Transparently provide more efficient getOperand methods.
138   Value *getOperand(unsigned i) const {
139     assert(i < 2 && "getOperand() out of range!");
140     return Ops[i];
141   }
142   void setOperand(unsigned i, Value *Val) {
143     assert(i < 2 && "setOperand() out of range!");
144     Ops[i] = Val;
145   }
146   unsigned getNumOperands() const { return 2; }
147
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.
152   ///
153   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
154                                 const std::string &Name = "",
155                                 Instruction *InsertBefore = 0);
156
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.
160   ///
161   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
162                                 const std::string &Name,
163                                 BasicBlock *InsertAtEnd);
164
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);\
172   }
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);\
178   }
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);\
184   }
185 #include "llvm/Instruction.def"
186
187
188   /// Helper functions to construct and inspect unary operations (NEG and NOT)
189   /// via binary operators SUB and XOR:
190   ///
191   /// createNeg, createNot - Create the NEG and NOT
192   ///     instructions out of SUB and XOR instructions.
193   ///
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);
202
203   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
204   ///
205   static bool            isNeg(const Value *V);
206   static bool            isNot(const Value *V);
207
208   /// getNegArgument, getNotArgument - Helper functions to extract the
209   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
210   ///
211   static const Value*    getNegArgument(const BinaryOperator* Bop);
212   static       Value*    getNegArgument(      BinaryOperator* Bop);
213   static const Value*    getNotArgument(const BinaryOperator* Bop);
214   static       Value*    getNotArgument(      BinaryOperator* Bop);
215
216   BinaryOps getOpcode() const {
217     return static_cast<BinaryOps>(Instruction::getOpcode());
218   }
219
220   virtual BinaryOperator *clone() const;
221
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.
228   ///
229   bool swapOperands();
230
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;
235   }
236   static inline bool classof(const Value *V) {
237     return isa<Instruction>(V) && classof(cast<Instruction>(V));
238   }
239 };
240
241 } // End llvm namespace
242
243 #endif