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