Added setSuccessor() method to terminator instructions
[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   virtual const char *getOpcodeName() const { return "ret"; }
43
44   inline const Value *getReturnValue() const {
45     return Operands.size() ? Operands[0].get() : 0; 
46   }
47   inline       Value *getReturnValue()       {
48     return Operands.size() ? Operands[0].get() : 0;
49   }
50
51   virtual const BasicBlock *getSuccessor(unsigned idx) const {
52     assert(0 && "ReturnInst has no successors!");
53     abort();
54   }
55   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
56     assert(0 && "ReturnInst has no successors!");
57   }
58   virtual unsigned getNumSuccessors() const { return 0; }
59
60   // Methods for support type inquiry through isa, cast, and dyn_cast:
61   static inline bool classof(const ReturnInst *) { return true; }
62   static inline bool classof(const Instruction *I) {
63     return (I->getOpcode() == Instruction::Ret);
64   }
65   static inline bool classof(const Value *V) {
66     return isa<Instruction>(V) && classof(cast<Instruction>(V));
67   }
68 };
69
70
71 //===---------------------------------------------------------------------------
72 // BranchInst - Conditional or Unconditional Branch instruction.
73 //
74 class BranchInst : public TerminatorInst {
75   BranchInst(const BranchInst &BI);
76 public:
77   // If cond = null, then is an unconditional br...
78   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
79
80   virtual Instruction *clone() const { return new BranchInst(*this); }
81
82   inline bool isUnconditional() const { return Operands.size() == 1; }
83   inline bool isConditional()   const { return Operands.size() == 3; }
84
85   inline const Value *getCondition() const {
86     return isUnconditional() ? 0 : Operands[2].get();
87   }
88   inline       Value *getCondition()       {
89     return isUnconditional() ? 0 : Operands[2].get();
90   }
91
92   virtual const char *getOpcodeName() const { return "br"; }
93
94   // setUnconditionalDest - Change the current branch to an unconditional branch
95   // targeting the specified block.
96   //
97   void setUnconditionalDest(BasicBlock *Dest) {
98     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
99     Operands[0] = (Value*)Dest;
100   }
101
102   virtual const BasicBlock *getSuccessor(unsigned i) const {
103     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
104     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
105                       cast<BasicBlock>(Operands[1].get());
106   }
107   inline BasicBlock *getSuccessor(unsigned idx) {
108     return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
109   }
110
111   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
112     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
113     Operands[idx] = (Value*)NewSucc;
114   }
115
116   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
117
118   // Methods for support type inquiry through isa, cast, and dyn_cast:
119   static inline bool classof(const BranchInst *) { return true; }
120   static inline bool classof(const Instruction *I) {
121     return (I->getOpcode() == Instruction::Br);
122   }
123   static inline bool classof(const Value *V) {
124     return isa<Instruction>(V) && classof(cast<Instruction>(V));
125   }
126 };
127
128
129 //===---------------------------------------------------------------------------
130 // SwitchInst - Multiway switch
131 //
132 class SwitchInst : public TerminatorInst {
133   // Operand[0]    = Value to switch on
134   // Operand[1]    = Default basic block destination
135   // Operand[2n  ] = Value to match
136   // Operand[2n+1] = BasicBlock to go to on match
137   SwitchInst(const SwitchInst &RI);
138 public:
139   SwitchInst(Value *Value, BasicBlock *Default);
140
141   virtual Instruction *clone() const { return new SwitchInst(*this); }
142
143   // Accessor Methods for Switch stmt
144   //
145   inline const Value *getCondition() const { return Operands[0]; }
146   inline       Value *getCondition()       { return Operands[0]; }
147   inline const BasicBlock *getDefaultDest() const {
148     return cast<BasicBlock>(Operands[1].get());
149   }
150   inline       BasicBlock *getDefaultDest()       {
151     return cast<BasicBlock>(Operands[1].get());
152   }
153
154   void dest_push_back(Constant *OnVal, BasicBlock *Dest);
155
156   virtual const char *getOpcodeName() const { return "switch"; }
157
158   virtual const BasicBlock *getSuccessor(unsigned idx) const {
159     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
160     return cast<BasicBlock>(Operands[idx*2+1].get());
161   }
162   inline BasicBlock *getSuccessor(unsigned idx) {
163     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
164     return cast<BasicBlock>(Operands[idx*2+1].get());
165   }
166
167   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
168     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
169     Operands[idx*2+1] = (Value*)NewSucc;
170   }
171
172   // getSuccessorValue - Return the value associated with the specified
173   // successor.
174   inline const Constant *getSuccessorValue(unsigned idx) const {
175     assert(idx < getNumSuccessors() && "Successor # out of range!");
176     return cast<Constant>(Operands[idx*2].get());
177   }
178   inline Constant *getSuccessorValue(unsigned idx) {
179     assert(idx < getNumSuccessors() && "Successor # out of range!");
180     return cast<Constant>(Operands[idx*2].get());
181   }
182   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
183
184   // Methods for support type inquiry through isa, cast, and dyn_cast:
185   static inline bool classof(const SwitchInst *) { return true; }
186   static inline bool classof(const Instruction *I) {
187     return (I->getOpcode() == Instruction::Switch);
188   }
189   static inline bool classof(const Value *V) {
190     return isa<Instruction>(V) && classof(cast<Instruction>(V));
191   }
192 };
193
194
195 //===---------------------------------------------------------------------------
196 // InvokeInst - Invoke instruction
197 //
198 class InvokeInst : public TerminatorInst {
199   InvokeInst(const InvokeInst &BI);
200 public:
201   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
202              const std::vector<Value*> &Params, const std::string &Name = "");
203
204   virtual Instruction *clone() const { return new InvokeInst(*this); }
205
206   bool hasSideEffects() const { return true; }
207
208   // getCalledFunction - Return the function called, or null if this is an
209   // indirect function invocation...
210   //
211   inline const Function *getCalledFunction() const {
212     return dyn_cast<Function>(Operands[0].get());
213   }
214   inline Function *getCalledFunction() {
215     return dyn_cast<Function>(Operands[0].get());
216   }
217
218   // getCalledValue - Get a pointer to a method that is invoked by this inst.
219   inline const Value *getCalledValue() const { return Operands[0]; }
220   inline       Value *getCalledValue()       { return Operands[0]; }
221
222   // get*Dest - Return the destination basic blocks...
223   inline const BasicBlock *getNormalDest() const {
224     return cast<BasicBlock>(Operands[1].get());
225   }
226   inline       BasicBlock *getNormalDest() {
227     return cast<BasicBlock>(Operands[1].get());
228   }
229   inline const BasicBlock *getExceptionalDest() const {
230     return cast<BasicBlock>(Operands[2].get());
231   }
232   inline       BasicBlock *getExceptionalDest() {
233     return cast<BasicBlock>(Operands[2].get());
234   }
235
236   inline void setNormalDest(BasicBlock *B){
237     Operands[1] = (Value*)B;
238   }
239
240   inline void setExceptionalDest(BasicBlock *B){
241     Operands[2] = (Value*)B;
242   }
243
244   virtual const char *getOpcodeName() const { return "invoke"; }
245
246   virtual const BasicBlock *getSuccessor(unsigned i) const {
247     assert(i < 2 && "Successor # out of range for invoke!");
248     return i == 0 ? getNormalDest() : getExceptionalDest();
249   }
250   inline BasicBlock *getSuccessor(unsigned i) {
251     assert(i < 2 && "Successor # out of range for invoke!");
252     return i == 0 ? getNormalDest() : getExceptionalDest();
253   }
254
255   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
256     assert(idx < 2 && "Successor # out of range for invoke!");
257     Operands[idx+1] = (Value*)NewSucc;
258   }
259
260   virtual unsigned getNumSuccessors() const { return 2; }
261
262   // Methods for support type inquiry through isa, cast, and dyn_cast:
263   static inline bool classof(const InvokeInst *) { return true; }
264   static inline bool classof(const Instruction *I) {
265     return (I->getOpcode() == Instruction::Invoke);
266   }
267   static inline bool classof(const Value *V) {
268     return isa<Instruction>(V) && classof(cast<Instruction>(V));
269   }
270 };
271
272 #endif