Added support for type inquiry in subclasses of InstTreeNode.
[oota-llvm.git] / include / llvm / iTerminators.h
1 //===-- llvm/iTerminators.h - Termintator instruction nodes ------*- C++ -*--=//
2 //
3 // This file contains the declarations for all the subclasses of the
4 // Instruction class, which is itself defined in the Instruction.h file.  In 
5 // between these definitions and the Instruction class are classes that expose
6 // the SSA properties of each instruction, and that form the SSA graph.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ITERMINATORS_H
11 #define LLVM_ITERMINATORS_H
12
13 #include "llvm/InstrTypes.h"
14
15 //===----------------------------------------------------------------------===//
16 //         Classes to represent Basic Block "Terminator" instructions
17 //===----------------------------------------------------------------------===//
18
19
20 //===---------------------------------------------------------------------------
21 // ReturnInst - Return a value (possibly void), from a method.  Execution does
22 //              not continue in this method any longer.
23 //
24 class ReturnInst : public TerminatorInst {
25   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
26     if (RI.Operands.size()) {
27       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
28       Operands.reserve(1);
29       Operands.push_back(Use(RI.Operands[0], this));
30     }
31   }
32 public:
33   ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
34     if (RetVal) {
35       Operands.reserve(1);
36       Operands.push_back(Use(RetVal, this));
37     }
38   }
39
40   virtual Instruction *clone() const { return new ReturnInst(*this); }
41
42   inline const Value *getReturnValue() const {
43     return Operands.size() ? Operands[0].get() : 0; 
44   }
45   inline       Value *getReturnValue()       {
46     return Operands.size() ? Operands[0].get() : 0;
47   }
48
49   virtual const BasicBlock *getSuccessor(unsigned idx) const {
50     assert(0 && "ReturnInst has no successors!");
51     abort();
52     return 0;
53   }
54   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
55     assert(0 && "ReturnInst has no successors!");
56   }
57   virtual unsigned getNumSuccessors() const { return 0; }
58
59   // Methods for support type inquiry through isa, cast, and dyn_cast:
60   static inline bool classof(const ReturnInst *) { return true; }
61   static inline bool classof(const Instruction *I) {
62     return (I->getOpcode() == Instruction::Ret);
63   }
64   static inline bool classof(const Value *V) {
65     return isa<Instruction>(V) && classof(cast<Instruction>(V));
66   }
67 };
68
69
70 //===---------------------------------------------------------------------------
71 // BranchInst - Conditional or Unconditional Branch instruction.
72 //
73 class BranchInst : public TerminatorInst {
74   BranchInst(const BranchInst &BI);
75 public:
76   // If cond = null, then is an unconditional br...
77   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
78
79   virtual Instruction *clone() const { return new BranchInst(*this); }
80
81   inline bool isUnconditional() const { return Operands.size() == 1; }
82   inline bool isConditional()   const { return Operands.size() == 3; }
83
84   inline const Value *getCondition() const {
85     return isUnconditional() ? 0 : Operands[2].get();
86   }
87   inline       Value *getCondition()       {
88     return isUnconditional() ? 0 : Operands[2].get();
89   }
90
91   // setUnconditionalDest - Change the current branch to an unconditional branch
92   // targeting the specified block.
93   //
94   void setUnconditionalDest(BasicBlock *Dest) {
95     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
96     Operands[0] = (Value*)Dest;
97   }
98
99   virtual const BasicBlock *getSuccessor(unsigned i) const {
100     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
101     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
102                       cast<BasicBlock>(Operands[1].get());
103   }
104   inline BasicBlock *getSuccessor(unsigned idx) {
105     return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
106   }
107
108   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
109     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
110     Operands[idx] = (Value*)NewSucc;
111   }
112
113   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
114
115   // Methods for support type inquiry through isa, cast, and dyn_cast:
116   static inline bool classof(const BranchInst *) { return true; }
117   static inline bool classof(const Instruction *I) {
118     return (I->getOpcode() == Instruction::Br);
119   }
120   static inline bool classof(const Value *V) {
121     return isa<Instruction>(V) && classof(cast<Instruction>(V));
122   }
123 };
124
125
126 //===---------------------------------------------------------------------------
127 // SwitchInst - Multiway switch
128 //
129 class SwitchInst : public TerminatorInst {
130   // Operand[0]    = Value to switch on
131   // Operand[1]    = Default basic block destination
132   // Operand[2n  ] = Value to match
133   // Operand[2n+1] = BasicBlock to go to on match
134   SwitchInst(const SwitchInst &RI);
135 public:
136   SwitchInst(Value *Value, BasicBlock *Default);
137
138   virtual Instruction *clone() const { return new SwitchInst(*this); }
139
140   // Accessor Methods for Switch stmt
141   //
142   inline const Value *getCondition() const { return Operands[0]; }
143   inline       Value *getCondition()       { return Operands[0]; }
144   inline const BasicBlock *getDefaultDest() const {
145     return cast<BasicBlock>(Operands[1].get());
146   }
147   inline       BasicBlock *getDefaultDest()       {
148     return cast<BasicBlock>(Operands[1].get());
149   }
150
151   void dest_push_back(Constant *OnVal, BasicBlock *Dest);
152
153   virtual const BasicBlock *getSuccessor(unsigned idx) const {
154     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
155     return cast<BasicBlock>(Operands[idx*2+1].get());
156   }
157   inline BasicBlock *getSuccessor(unsigned idx) {
158     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
159     return cast<BasicBlock>(Operands[idx*2+1].get());
160   }
161
162   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
163     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
164     Operands[idx*2+1] = (Value*)NewSucc;
165   }
166
167   // getSuccessorValue - Return the value associated with the specified
168   // successor.
169   inline const Constant *getSuccessorValue(unsigned idx) const {
170     assert(idx < getNumSuccessors() && "Successor # out of range!");
171     return cast<Constant>(Operands[idx*2].get());
172   }
173   inline Constant *getSuccessorValue(unsigned idx) {
174     assert(idx < getNumSuccessors() && "Successor # out of range!");
175     return cast<Constant>(Operands[idx*2].get());
176   }
177   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
178
179   // Methods for support type inquiry through isa, cast, and dyn_cast:
180   static inline bool classof(const SwitchInst *) { return true; }
181   static inline bool classof(const Instruction *I) {
182     return (I->getOpcode() == Instruction::Switch);
183   }
184   static inline bool classof(const Value *V) {
185     return isa<Instruction>(V) && classof(cast<Instruction>(V));
186   }
187 };
188
189
190 //===---------------------------------------------------------------------------
191 // InvokeInst - Invoke instruction
192 //
193 class InvokeInst : public TerminatorInst {
194   InvokeInst(const InvokeInst &BI);
195 public:
196   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
197              const std::vector<Value*> &Params, const std::string &Name = "");
198
199   virtual Instruction *clone() const { return new InvokeInst(*this); }
200
201   bool hasSideEffects() const { return true; }
202
203   // getCalledFunction - Return the function called, or null if this is an
204   // indirect function invocation...
205   //
206   inline const Function *getCalledFunction() const {
207     return dyn_cast<Function>(Operands[0].get());
208   }
209   inline Function *getCalledFunction() {
210     return dyn_cast<Function>(Operands[0].get());
211   }
212
213   // getCalledValue - Get a pointer to a method that is invoked by this inst.
214   inline const Value *getCalledValue() const { return Operands[0]; }
215   inline       Value *getCalledValue()       { return Operands[0]; }
216
217   // get*Dest - Return the destination basic blocks...
218   inline const BasicBlock *getNormalDest() const {
219     return cast<BasicBlock>(Operands[1].get());
220   }
221   inline       BasicBlock *getNormalDest() {
222     return cast<BasicBlock>(Operands[1].get());
223   }
224   inline const BasicBlock *getExceptionalDest() const {
225     return cast<BasicBlock>(Operands[2].get());
226   }
227   inline       BasicBlock *getExceptionalDest() {
228     return cast<BasicBlock>(Operands[2].get());
229   }
230
231   inline void setNormalDest(BasicBlock *B){
232     Operands[1] = (Value*)B;
233   }
234
235   inline void setExceptionalDest(BasicBlock *B){
236     Operands[2] = (Value*)B;
237   }
238
239   virtual const BasicBlock *getSuccessor(unsigned i) const {
240     assert(i < 2 && "Successor # out of range for invoke!");
241     return i == 0 ? getNormalDest() : getExceptionalDest();
242   }
243   inline BasicBlock *getSuccessor(unsigned i) {
244     assert(i < 2 && "Successor # out of range for invoke!");
245     return i == 0 ? getNormalDest() : getExceptionalDest();
246   }
247
248   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
249     assert(idx < 2 && "Successor # out of range for invoke!");
250     Operands[idx+1] = (Value*)NewSucc;
251   }
252
253   virtual unsigned getNumSuccessors() const { return 2; }
254
255   // Methods for support type inquiry through isa, cast, and dyn_cast:
256   static inline bool classof(const InvokeInst *) { return true; }
257   static inline bool classof(const Instruction *I) {
258     return (I->getOpcode() == Instruction::Invoke);
259   }
260   static inline bool classof(const Value *V) {
261     return isa<Instruction>(V) && classof(cast<Instruction>(V));
262   }
263 };
264
265 #endif