Moved getBinaryOperator to the BinaryOperator class and the getUnaryOperator
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*--=//
2 //
3 // This file defines various meta classes of instructions that exist in the VM
4 // representation.  Specific concrete subclasses of these may be found in the 
5 // i*.h files...
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_INSTRUCTION_TYPES_H
10 #define LLVM_INSTRUCTION_TYPES_H
11
12 #include "llvm/Instruction.h"
13 #include <list>
14 #include <vector>
15
16 class Method;
17 class SymTabValue;
18
19 //===----------------------------------------------------------------------===//
20 //                            TerminatorInst Class
21 //===----------------------------------------------------------------------===//
22
23 // TerminatorInst - Subclasses of this class are all able to terminate a basic 
24 // block.  Thus, these are all the flow control type of operations.
25 //
26 class TerminatorInst : public Instruction {
27 public:
28   TerminatorInst(unsigned iType);
29   inline ~TerminatorInst() {}
30
31   // Terminators must implement the methods required by Instruction...
32   virtual Instruction *clone() const = 0;
33   virtual void dropAllReferences() = 0;
34   virtual string getOpcode() const = 0;
35
36   virtual bool setOperand(unsigned i, Value *Val) = 0;
37   virtual const Value *getOperand(unsigned i) const = 0;
38
39   // Additionally, they must provide a method to get at the successors of this
40   // terminator instruction.  If 'idx' is out of range, a null pointer shall be
41   // returned.
42   //
43   virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
44   virtual unsigned getNumSuccessors() const = 0;
45
46   inline BasicBlock *getSuccessor(unsigned idx) {
47     return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
48   }
49 };
50
51
52 //===----------------------------------------------------------------------===//
53 //                            UnaryOperator Class
54 //===----------------------------------------------------------------------===//
55
56 class UnaryOperator : public Instruction {
57   Use Source;
58 public:
59
60   // getUnaryOperator() - Construct a unary instruction, given the opcode
61   // and its operand.
62   //
63   static UnaryOperator *getUnaryOperator(unsigned Op, Value *Source);
64
65   UnaryOperator(Value *S, unsigned iType, const string &Name = "")
66       : Instruction(S->getType(), iType, Name), Source(S, this) {
67   }
68   inline ~UnaryOperator() { dropAllReferences(); }
69
70   virtual Instruction *clone() const { 
71     return getUnaryOperator(getInstType(), Source);
72   }
73
74   virtual void dropAllReferences() {
75     Source = 0;
76   }
77
78   virtual string getOpcode() const = 0;
79
80   virtual unsigned getNumOperands() const { return 1; }
81   virtual const Value *getOperand(unsigned i) const {
82     return (i == 0) ? Source : 0;
83   }
84   virtual bool setOperand(unsigned i, Value *Val) {
85     // assert(Val && "operand must not be null!");
86     if (i) return false;
87     Source = Val;
88     return true;
89   }
90 };
91
92
93
94 //===----------------------------------------------------------------------===//
95 //                           BinaryOperator Class
96 //===----------------------------------------------------------------------===//
97
98 class BinaryOperator : public Instruction {
99   Use Source1, Source2;
100 public:
101
102   // getBinaryOperator() - Construct a binary instruction, given the opcode
103   // and the two operands.
104   //
105   static BinaryOperator *getBinaryOperator(unsigned Op, Value *S1, Value *S2);
106
107   BinaryOperator(unsigned iType, Value *S1, Value *S2, 
108                  const string &Name = "") 
109     : Instruction(S1->getType(), iType, Name), Source1(S1, this), 
110       Source2(S2, this){
111     assert(S1 && S2 && S1->getType() == S2->getType());
112   }
113   inline ~BinaryOperator() { dropAllReferences(); }
114
115   virtual Instruction *clone() const { 
116     return getBinaryOperator(getInstType(), Source1, Source2);
117   }
118
119   virtual void dropAllReferences() {
120     Source1 = Source2 = 0;
121   }
122
123   virtual string getOpcode() const = 0;
124
125   virtual unsigned getNumOperands() const { return 2; }
126   virtual const Value *getOperand(unsigned i) const {
127     return (i == 0) ? Source1 : ((i == 1) ? Source2 : 0);
128   }
129
130   virtual bool setOperand(unsigned i, Value *Val) {
131     // assert(Val && "operand must not be null!");
132     if (i == 0) {
133       Source1 = Val; //assert(Val->getType() == Source2->getType());
134     } else if (i == 1) {
135       Source2 = Val; //assert(Val->getType() == Source1->getType());
136     } else {
137       return false;
138     }
139     return true;
140   }
141 };
142
143 #endif