* Rename get.*Operator to create seeing that it would have to be qualified
[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   inline Value *getOperand(unsigned i) {
39     return (Value*)((const Instruction *)this)->getOperand(i);
40   }
41
42   // Additionally, they must provide a method to get at the successors of this
43   // terminator instruction.  If 'idx' is out of range, a null pointer shall be
44   // returned.
45   //
46   virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
47   virtual unsigned getNumSuccessors() const = 0;
48
49   inline BasicBlock *getSuccessor(unsigned idx) {
50     return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
51   }
52 };
53
54
55 //===----------------------------------------------------------------------===//
56 //                            UnaryOperator Class
57 //===----------------------------------------------------------------------===//
58
59 class UnaryOperator : public Instruction {
60   Use Source;
61 public:
62
63   // create() - Construct a unary instruction, given the opcode
64   // and its operand.
65   //
66   static UnaryOperator *create(unsigned Op, Value *Source);
67
68   UnaryOperator(Value *S, unsigned iType, const string &Name = "")
69       : Instruction(S->getType(), iType, Name), Source(S, this) {
70   }
71   inline ~UnaryOperator() { dropAllReferences(); }
72
73   virtual Instruction *clone() const { 
74     return create(getInstType(), Source);
75   }
76
77   virtual void dropAllReferences() {
78     Source = 0;
79   }
80
81   virtual string getOpcode() const = 0;
82
83   virtual unsigned getNumOperands() const { return 1; }
84   inline Value *getOperand(unsigned i) {
85     return (i == 0) ? Source : 0;
86   }
87   virtual const Value *getOperand(unsigned i) const {
88     return (i == 0) ? Source : 0;
89   }
90   virtual bool setOperand(unsigned i, Value *Val) {
91     // assert(Val && "operand must not be null!");
92     if (i) return false;
93     Source = Val;
94     return true;
95   }
96 };
97
98
99
100 //===----------------------------------------------------------------------===//
101 //                           BinaryOperator Class
102 //===----------------------------------------------------------------------===//
103
104 class BinaryOperator : public Instruction {
105   Use Source1, Source2;
106 public:
107
108   // create() - Construct a binary instruction, given the opcode
109   // and the two operands.
110   //
111   static BinaryOperator *create(unsigned Op, Value *S1, Value *S2,
112                                 const string &Name = "");
113
114   BinaryOperator(unsigned iType, Value *S1, Value *S2, 
115                  const string &Name = "") 
116     : Instruction(S1->getType(), iType, Name), Source1(S1, this), 
117       Source2(S2, this){
118     assert(S1 && S2 && S1->getType() == S2->getType());
119   }
120   inline ~BinaryOperator() { dropAllReferences(); }
121
122   virtual Instruction *clone() const {
123     return create(getInstType(), Source1, Source2);
124   }
125
126   virtual void dropAllReferences() {
127     Source1 = Source2 = 0;
128   }
129
130   virtual string getOpcode() const = 0;
131
132   virtual unsigned getNumOperands() const { return 2; }
133   virtual const Value *getOperand(unsigned i) const {
134     return (i == 0) ? Source1 : ((i == 1) ? Source2 : 0);
135   }
136   inline Value *getOperand(unsigned i) {
137     return (i == 0) ? Source1 : ((i == 1) ? Source2 : 0);
138   }
139
140   virtual bool setOperand(unsigned i, Value *Val) {
141     // assert(Val && "operand must not be null!");
142     if (i == 0) {
143       Source1 = Val; //assert(Val->getType() == Source2->getType());
144     } else if (i == 1) {
145       Source2 = Val; //assert(Val->getType() == Source1->getType());
146     } else {
147       return false;
148     }
149     return true;
150   }
151 };
152
153 #endif