Add new BinaryOperator::createAdd/Sub/... methods to avoid having to type
[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, Instruction *InsertBefore = 0);
33   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
34                  const std::string &Name = "", Instruction *InsertBefore = 0)
35     : Instruction(Ty, iType, Name, InsertBefore) {}
36
37   TerminatorInst(Instruction::TermOps iType, BasicBlock *InsertAtEnd);
38   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39                  const std::string &Name, BasicBlock *InsertAtEnd)
40     : Instruction(Ty, iType, Name, InsertAtEnd) {}
41
42 public:
43
44   /// Terminators must implement the methods required by Instruction...
45   virtual Instruction *clone() const = 0;
46
47   /// Additionally, they must provide a method to get at the successors of this
48   /// terminator instruction.  'idx' may not be >= the number of successors
49   /// returned by getNumSuccessors()!
50   ///
51   virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
52   virtual unsigned getNumSuccessors() const = 0;
53   
54   /// Set a successor at a given index
55   virtual void setSuccessor(unsigned idx, BasicBlock *B) = 0;
56
57   inline BasicBlock *getSuccessor(unsigned idx) {
58     const TerminatorInst *TI = this;
59     return const_cast<BasicBlock*>(TI->getSuccessor(idx));
60   }
61
62   // Methods for support type inquiry through isa, cast, and dyn_cast:
63   static inline bool classof(const TerminatorInst *) { return true; }
64   static inline bool classof(const Instruction *I) {
65     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
66   }
67   static inline bool classof(const Value *V) {
68     return isa<Instruction>(V) && classof(cast<Instruction>(V));
69   }
70 };
71
72
73 //===----------------------------------------------------------------------===//
74 //                           BinaryOperator Class
75 //===----------------------------------------------------------------------===//
76
77 class BinaryOperator : public Instruction {
78 protected:
79   void init(BinaryOps iType, Value *S1, Value *S2);
80   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
81                  const std::string &Name, Instruction *InsertBefore)
82     : Instruction(Ty, iType, Name, InsertBefore) {
83     init(iType, S1, S2);
84   }
85   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
86                  const std::string &Name, BasicBlock *InsertAtEnd)
87     : Instruction(Ty, iType, Name, InsertAtEnd) {
88     init(iType, S1, S2);
89   }
90
91 public:
92
93   /// create() - Construct a binary instruction, given the opcode and the two
94   /// operands.  Optionally (if InstBefore is specified) insert the instruction
95   /// into a BasicBlock right before the specified instruction.  The specified
96   /// Instruction is allowed to be a dereferenced end iterator.
97   ///
98   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
99                                 const std::string &Name = "",
100                                 Instruction *InsertBefore = 0);
101                                
102   /// create() - Construct a binary instruction, given the opcode and the two
103   /// operands.  Also automatically insert this instruction to the end of the
104   /// BasicBlock specified.
105   ///
106   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
107                                 const std::string &Name,
108                                 BasicBlock *InsertAtEnd);
109
110   /// create* - These methods just forward to create, and are useful when you
111   /// statically know what type of instruction you're going to create.  These
112   /// helpers just save some typing.
113 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
114   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
115                                      const std::string &Name = "") {\
116     return create(Instruction::OPC, V1, V2, Name);\
117   }
118 #include "llvm/Instruction.def"
119 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
120   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
121                                      const std::string &Name, BasicBlock *BB) {\
122     return create(Instruction::OPC, V1, V2, Name, BB);\
123   }
124 #include "llvm/Instruction.def"
125                                
126
127   /// Helper functions to construct and inspect unary operations (NEG and NOT)
128   /// via binary operators SUB and XOR:
129   /// 
130   /// createNeg, createNot - Create the NEG and NOT
131   ///     instructions out of SUB and XOR instructions.
132   ///
133   static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
134                                    Instruction *InsertBefore = 0);
135   static BinaryOperator *createNeg(Value *Op, const std::string &Name,
136                                    BasicBlock *InsertAtEnd);
137   static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
138                                    Instruction *InsertBefore = 0);
139   static BinaryOperator *createNot(Value *Op, const std::string &Name,
140                                    BasicBlock *InsertAtEnd);
141
142   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
143   ///
144   static bool            isNeg(const Value *V);
145   static bool            isNot(const Value *V);
146
147   /// getNegArgument, getNotArgument - Helper functions to extract the
148   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
149   /// 
150   static const Value*    getNegArgument(const BinaryOperator* Bop);
151   static       Value*    getNegArgument(      BinaryOperator* Bop);
152   static const Value*    getNotArgument(const BinaryOperator* Bop);
153   static       Value*    getNotArgument(      BinaryOperator* Bop);
154
155   BinaryOps getOpcode() const { 
156     return static_cast<BinaryOps>(Instruction::getOpcode());
157   }
158
159   virtual Instruction *clone() const {
160     return create(getOpcode(), Operands[0], Operands[1]);
161   }
162
163   /// swapOperands - Exchange the two operands to this instruction.
164   /// This instruction is safe to use on any binary instruction and
165   /// does not modify the semantics of the instruction.  If the
166   /// instruction is order dependent (SetLT f.e.) the opcode is
167   /// changed.  If the instruction cannot be reversed (ie, it's a Div),
168   /// then return true.
169   ///
170   bool swapOperands();
171
172   // Methods for support type inquiry through isa, cast, and dyn_cast:
173   static inline bool classof(const BinaryOperator *) { return true; }
174   static inline bool classof(const Instruction *I) {
175     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd; 
176   }
177   static inline bool classof(const Value *V) {
178     return isa<Instruction>(V) && classof(cast<Instruction>(V));
179   }
180 };
181
182 } // End llvm namespace
183
184 #endif