Initial revision
[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   UnaryOperator(Value *S, unsigned iType, const string &Name = "")
60       : Instruction(S->getType(), iType, Name), Source(S, this) {
61   }
62   inline ~UnaryOperator() { dropAllReferences(); }
63
64   virtual Instruction *clone() const { 
65     return Instruction::getUnaryOperator(getInstType(), Source);
66   }
67
68   virtual void dropAllReferences() {
69     Source = 0;
70   }
71
72   virtual string getOpcode() const = 0;
73
74   virtual unsigned getNumOperands() const { return 1; }
75   virtual const Value *getOperand(unsigned i) const {
76     return (i == 0) ? Source : 0;
77   }
78   virtual bool setOperand(unsigned i, Value *Val) {
79     // assert(Val && "operand must not be null!");
80     if (i) return false;
81     Source = Val;
82     return true;
83   }
84 };
85
86
87
88 //===----------------------------------------------------------------------===//
89 //                           BinaryOperator Class
90 //===----------------------------------------------------------------------===//
91
92 class BinaryOperator : public Instruction {
93   Use Source1, Source2;
94 public:
95   BinaryOperator(unsigned iType, Value *S1, Value *S2, 
96                  const string &Name = "") 
97     : Instruction(S1->getType(), iType, Name), Source1(S1, this), 
98       Source2(S2, this){
99     assert(S1 && S2 && S1->getType() == S2->getType());
100   }
101   inline ~BinaryOperator() { dropAllReferences(); }
102
103   virtual Instruction *clone() const { 
104     return Instruction::getBinaryOperator(getInstType(), Source1, Source2);
105   }
106
107   virtual void dropAllReferences() {
108     Source1 = Source2 = 0;
109   }
110
111   virtual string getOpcode() const = 0;
112
113   virtual unsigned getNumOperands() const { return 2; }
114   virtual const Value *getOperand(unsigned i) const {
115     return (i == 0) ? Source1 : ((i == 1) ? Source2 : 0);
116   }
117
118   virtual bool setOperand(unsigned i, Value *Val) {
119     // assert(Val && "operand must not be null!");
120     if (i == 0) {
121       Source1 = Val; //assert(Val->getType() == Source2->getType());
122     } else if (i == 1) {
123       Source2 = Val; //assert(Val->getType() == Source1->getType());
124     } else {
125       return false;
126     }
127     return true;
128   }
129 };
130
131 #endif