Add a method to reserve space for operands
[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 Instruction
4 // class which represent "terminator" instructions.  Terminator instructions are
5 // the only instructions allowed and required to terminate a BasicBlock.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_ITERMINATORS_H
10 #define LLVM_ITERMINATORS_H
11
12 #include "llvm/InstrTypes.h"
13
14 //===---------------------------------------------------------------------------
15 // ReturnInst - Return a value (possibly void), from a function.  Execution does
16 //              not continue in this function any longer.
17 //
18 class ReturnInst : public TerminatorInst {
19   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
20     if (RI.Operands.size()) {
21       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
22       Operands.reserve(1);
23       Operands.push_back(Use(RI.Operands[0], this));
24     }
25   }
26 public:
27   ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
28     : TerminatorInst(Instruction::Ret, InsertBefore) {
29     if (RetVal) {
30       Operands.reserve(1);
31       Operands.push_back(Use(RetVal, this));
32     }
33   }
34
35   virtual Instruction *clone() const { return new ReturnInst(*this); }
36
37   inline const Value *getReturnValue() const {
38     return Operands.size() ? Operands[0].get() : 0; 
39   }
40   inline       Value *getReturnValue()       {
41     return Operands.size() ? Operands[0].get() : 0;
42   }
43
44   virtual const BasicBlock *getSuccessor(unsigned idx) const {
45     assert(0 && "ReturnInst has no successors!");
46     abort();
47     return 0;
48   }
49   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
50     assert(0 && "ReturnInst has no successors!");
51   }
52   virtual unsigned getNumSuccessors() const { return 0; }
53
54   // Methods for support type inquiry through isa, cast, and dyn_cast:
55   static inline bool classof(const ReturnInst *) { return true; }
56   static inline bool classof(const Instruction *I) {
57     return (I->getOpcode() == Instruction::Ret);
58   }
59   static inline bool classof(const Value *V) {
60     return isa<Instruction>(V) && classof(cast<Instruction>(V));
61   }
62 };
63
64 //===---------------------------------------------------------------------------
65 // BranchInst - Conditional or Unconditional Branch instruction.
66 //
67 class BranchInst : public TerminatorInst {
68   BranchInst(const BranchInst &BI);
69 public:
70   // If cond = null, then is an unconditional br...
71   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond = 0,
72              Instruction *InsertBefore = 0);
73   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
74
75   virtual Instruction *clone() const { return new BranchInst(*this); }
76
77   inline bool isUnconditional() const { return Operands.size() == 1; }
78   inline bool isConditional()   const { return Operands.size() == 3; }
79
80   inline Value *getCondition() const {
81     return isUnconditional() ? 0 : (Value*)Operands[2].get();
82   }
83
84   void setCondition(Value *V) {
85     assert(isConditional() && "Cannot set condition of unconditional branch!");
86     setOperand(2, V);
87   }
88
89   // setUnconditionalDest - Change the current branch to an unconditional branch
90   // targeting the specified block.
91   //
92   void setUnconditionalDest(BasicBlock *Dest) {
93     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
94     Operands[0] = (Value*)Dest;
95   }
96
97   virtual const BasicBlock *getSuccessor(unsigned i) const {
98     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
99     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
100                       cast<BasicBlock>(Operands[1].get());
101   }
102   inline BasicBlock *getSuccessor(unsigned idx) {
103     return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
104   }
105
106   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
107     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
108     Operands[idx] = (Value*)NewSucc;
109   }
110
111   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
112
113   // Methods for support type inquiry through isa, cast, and dyn_cast:
114   static inline bool classof(const BranchInst *) { return true; }
115   static inline bool classof(const Instruction *I) {
116     return (I->getOpcode() == Instruction::Br);
117   }
118   static inline bool classof(const Value *V) {
119     return isa<Instruction>(V) && classof(cast<Instruction>(V));
120   }
121 };
122
123
124 //===---------------------------------------------------------------------------
125 // SwitchInst - Multiway switch
126 //
127 class SwitchInst : public TerminatorInst {
128   // Operand[0]    = Value to switch on
129   // Operand[1]    = Default basic block destination
130   // Operand[2n  ] = Value to match
131   // Operand[2n+1] = BasicBlock to go to on match
132   SwitchInst(const SwitchInst &RI);
133 public:
134   SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
135
136   virtual Instruction *clone() const { return new SwitchInst(*this); }
137
138   // Accessor Methods for Switch stmt
139   //
140   inline const Value *getCondition() const { return Operands[0]; }
141   inline       Value *getCondition()       { return Operands[0]; }
142   inline const BasicBlock *getDefaultDest() const {
143     return cast<BasicBlock>(Operands[1].get());
144   }
145   inline       BasicBlock *getDefaultDest()       {
146     return cast<BasicBlock>(Operands[1].get());
147   }
148
149   /// addCase - Add an entry to the switch instruction...
150   ///
151   void addCase(Constant *OnVal, BasicBlock *Dest);
152
153   /// removeCase - This method removes the specified successor from the switch
154   /// instruction.  Note that this cannot be used to remove the default
155   /// destination (successor #0).
156   ///
157   void removeCase(unsigned idx);
158
159   virtual const BasicBlock *getSuccessor(unsigned idx) const {
160     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
161     return cast<BasicBlock>(Operands[idx*2+1].get());
162   }
163   inline BasicBlock *getSuccessor(unsigned idx) {
164     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
165     return cast<BasicBlock>(Operands[idx*2+1].get());
166   }
167
168   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
169     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
170     Operands[idx*2+1] = (Value*)NewSucc;
171   }
172
173   // getSuccessorValue - Return the value associated with the specified
174   // successor.
175   inline const Constant *getSuccessorValue(unsigned idx) const {
176     assert(idx < getNumSuccessors() && "Successor # out of range!");
177     return cast<Constant>(Operands[idx*2].get());
178   }
179   inline Constant *getSuccessorValue(unsigned idx) {
180     assert(idx < getNumSuccessors() && "Successor # out of range!");
181     return cast<Constant>(Operands[idx*2].get());
182   }
183   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
184
185   // Methods for support type inquiry through isa, cast, and dyn_cast:
186   static inline bool classof(const SwitchInst *) { return true; }
187   static inline bool classof(const Instruction *I) {
188     return (I->getOpcode() == Instruction::Switch);
189   }
190   static inline bool classof(const Value *V) {
191     return isa<Instruction>(V) && classof(cast<Instruction>(V));
192   }
193 };
194
195
196 //===---------------------------------------------------------------------------
197 // InvokeInst - Invoke instruction
198 //
199 class InvokeInst : public TerminatorInst {
200   InvokeInst(const InvokeInst &BI);
201 public:
202   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
203              const std::vector<Value*> &Params, const std::string &Name = "",
204              Instruction *InsertBefore = 0);
205
206   virtual Instruction *clone() const { return new InvokeInst(*this); }
207
208   bool mayWriteToMemory() const { return true; }
209
210   // getCalledFunction - Return the function called, or null if this is an
211   // indirect function invocation...
212   //
213   inline const Function *getCalledFunction() const {
214     return dyn_cast<Function>(Operands[0].get());
215   }
216   inline Function *getCalledFunction() {
217     return dyn_cast<Function>(Operands[0].get());
218   }
219
220   // getCalledValue - Get a pointer to a function that is invoked by this inst.
221   inline const Value *getCalledValue() const { return Operands[0]; }
222   inline       Value *getCalledValue()       { return Operands[0]; }
223
224   // get*Dest - Return the destination basic blocks...
225   inline const BasicBlock *getNormalDest() const {
226     return cast<BasicBlock>(Operands[1].get());
227   }
228   inline       BasicBlock *getNormalDest() {
229     return cast<BasicBlock>(Operands[1].get());
230   }
231   inline const BasicBlock *getExceptionalDest() const {
232     return cast<BasicBlock>(Operands[2].get());
233   }
234   inline       BasicBlock *getExceptionalDest() {
235     return cast<BasicBlock>(Operands[2].get());
236   }
237
238   inline void setNormalDest(BasicBlock *B){
239     Operands[1] = (Value*)B;
240   }
241
242   inline void setExceptionalDest(BasicBlock *B){
243     Operands[2] = (Value*)B;
244   }
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
273 //===---------------------------------------------------------------------------
274 /// UnwindInst - Immediately exit the current function, unwinding the stack
275 /// until an invoke instruction is found.
276 ///
277 struct UnwindInst : public TerminatorInst {
278   UnwindInst(Instruction *InsertBefore = 0)
279     : TerminatorInst(Instruction::Unwind, InsertBefore) {
280   }
281
282   virtual Instruction *clone() const { return new UnwindInst(); }
283
284   virtual const BasicBlock *getSuccessor(unsigned idx) const {
285     assert(0 && "UnwindInst has no successors!");
286     abort();
287     return 0;
288   }
289   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
290     assert(0 && "UnwindInst has no successors!");
291   }
292   virtual unsigned getNumSuccessors() const { return 0; }
293
294   // Methods for support type inquiry through isa, cast, and dyn_cast:
295   static inline bool classof(const UnwindInst *) { return true; }
296   static inline bool classof(const Instruction *I) {
297     return I->getOpcode() == Instruction::Unwind;
298   }
299   static inline bool classof(const Value *V) {
300     return isa<Instruction>(V) && classof(cast<Instruction>(V));
301   }
302 };
303
304 #endif