Minor cleanups I missed
[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
14 //===----------------------------------------------------------------------===//
15 //                            TerminatorInst Class
16 //===----------------------------------------------------------------------===//
17
18 // TerminatorInst - Subclasses of this class are all able to terminate a basic 
19 // block.  Thus, these are all the flow control type of operations.
20 //
21 class TerminatorInst : public Instruction {
22 protected:
23   TerminatorInst(Instruction::TermOps iType);
24   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
25                  const std::string &Name = "");
26 public:
27
28   // Terminators must implement the methods required by Instruction...
29   virtual Instruction *clone() const = 0;
30   virtual const char *getOpcodeName() const = 0;
31
32   // Additionally, they must provide a method to get at the successors of this
33   // terminator instruction.  If 'idx' is out of range, a null pointer shall be
34   // returned.
35   //
36   virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
37   virtual unsigned getNumSuccessors() const = 0;
38
39   inline BasicBlock *getSuccessor(unsigned idx) {
40     return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
41   }
42
43   // Methods for support type inquiry through isa, cast, and dyn_cast:
44   static inline bool classof(const TerminatorInst *) { return true; }
45   static inline bool classof(const Instruction *I) {
46     return I->getOpcode() >= FirstTermOp && I->getOpcode() < NumTermOps; 
47   }
48   static inline bool classof(const Value *V) {
49     return isa<Instruction>(V) && classof(cast<Instruction>(V));
50   }
51 };
52
53
54 //===----------------------------------------------------------------------===//
55 //                            UnaryOperator Class
56 //===----------------------------------------------------------------------===//
57
58 class UnaryOperator : public Instruction {
59 protected:
60   UnaryOperator(Value *S, UnaryOps iType, const std::string &Name = "")
61       : Instruction(S->getType(), iType, Name) {
62     Operands.reserve(1);
63     Operands.push_back(Use(S, this));
64   }
65 public:
66
67   // create() - Construct a unary instruction, given the opcode
68   // and its operand.
69   //
70   static UnaryOperator *create(UnaryOps Op, Value *Source);
71
72   inline UnaryOps getOpcode() const { 
73     return (UnaryOps)Instruction::getOpcode();
74   }
75
76   virtual Instruction *clone() const { 
77     return create(getOpcode(), Operands[0]);
78   }
79
80   virtual const char *getOpcodeName() const = 0;
81
82   // Methods for support type inquiry through isa, cast, and dyn_cast:
83   static inline bool classof(const UnaryOperator *) { return true; }
84   static inline bool classof(const Instruction *I) {
85     return I->getOpcode() >= FirstUnaryOp && I->getOpcode() < NumUnaryOps; 
86   }
87   static inline bool classof(const Value *V) {
88     return isa<Instruction>(V) && classof(cast<Instruction>(V));
89   }
90 };
91
92
93
94 //===----------------------------------------------------------------------===//
95 //                           BinaryOperator Class
96 //===----------------------------------------------------------------------===//
97
98 class BinaryOperator : public Instruction {
99 protected:
100   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
101                  const std::string &Name = "") 
102     : Instruction(S1->getType(), iType, Name) {
103     Operands.reserve(2);
104     Operands.push_back(Use(S1, this));
105     Operands.push_back(Use(S2, this));
106     assert(Operands[0] && Operands[1] && 
107            Operands[0]->getType() == Operands[1]->getType());
108   }
109
110 public:
111
112   // create() - Construct a binary instruction, given the opcode
113   // and the two operands.
114   //
115   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
116                                 const std::string &Name = "");
117
118   inline BinaryOps getOpcode() const { 
119     return (BinaryOps)Instruction::getOpcode();
120   }
121
122   virtual Instruction *clone() const {
123     return create(getOpcode(), Operands[0], Operands[1]);
124   }
125
126   virtual const char *getOpcodeName() const = 0;
127
128   // swapOperands - Exchange the two operands to this instruction.
129   // This instruction is safe to use on any binary instruction and
130   // does not modify the semantics of the instruction.  If the
131   // instruction is order dependant (SetLT f.e.) the opcode is
132   // changed.  If the instruction cannot be reversed (ie, it's a Div),
133   // then return true.
134   //
135   bool swapOperands();
136
137   // Methods for support type inquiry through isa, cast, and dyn_cast:
138   static inline bool classof(const BinaryOperator *) { return true; }
139   static inline bool classof(const Instruction *I) {
140     return I->getOpcode() >= FirstBinaryOp && I->getOpcode() < NumBinaryOps; 
141   }
142   static inline bool classof(const Value *V) {
143     return isa<Instruction>(V) && classof(cast<Instruction>(V));
144   }
145 };
146
147 #endif