7cf14ac11500aefba185739cf97d7e5fd9576042
[oota-llvm.git] / include / llvm / iTerminators.h
1 //===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations for all the subclasses of the Instruction
11 // class which represent "terminator" instructions.  Terminator instructions are
12 // the only instructions allowed and required to terminate a BasicBlock.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ITERMINATORS_H
17 #define LLVM_ITERMINATORS_H
18
19 #include "llvm/InstrTypes.h"
20
21 namespace llvm {
22
23 //===---------------------------------------------------------------------------
24 // ReturnInst - Return a value (possibly void), from a function.  Execution does
25 //              not continue in this function any longer.
26 //
27 class ReturnInst : public TerminatorInst {
28   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
29     if (RI.Operands.size()) {
30       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
31       Operands.reserve(1);
32       Operands.push_back(Use(RI.Operands[0], this));
33     }
34   }
35 public:
36   // ReturnInst constructors:
37   // ReturnInst()                  - 'ret void' instruction
38   // ReturnInst(Value* X)          - 'ret X'    instruction
39   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
40   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
41   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
42   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
43   ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
44     : TerminatorInst(Instruction::Ret, InsertBefore) {
45     if (RetVal) {
46       Operands.reserve(1);
47       Operands.push_back(Use(RetVal, this));
48     }
49   }
50   ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
51     : TerminatorInst(Instruction::Ret, InsertAtEnd) {
52     if (RetVal) {
53       Operands.reserve(1);
54       Operands.push_back(Use(RetVal, this));
55     }
56   }
57
58   virtual Instruction *clone() const { return new ReturnInst(*this); }
59
60   inline const Value *getReturnValue() const {
61     return Operands.size() ? Operands[0].get() : 0; 
62   }
63   inline       Value *getReturnValue()       {
64     return Operands.size() ? Operands[0].get() : 0;
65   }
66
67   virtual const BasicBlock *getSuccessor(unsigned idx) const {
68     assert(0 && "ReturnInst has no successors!");
69     abort();
70     return 0;
71   }
72   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
73   virtual unsigned getNumSuccessors() const { return 0; }
74
75   // Methods for support type inquiry through isa, cast, and dyn_cast:
76   static inline bool classof(const ReturnInst *) { return true; }
77   static inline bool classof(const Instruction *I) {
78     return (I->getOpcode() == Instruction::Ret);
79   }
80   static inline bool classof(const Value *V) {
81     return isa<Instruction>(V) && classof(cast<Instruction>(V));
82   }
83 };
84
85 //===---------------------------------------------------------------------------
86 // BranchInst - Conditional or Unconditional Branch instruction.
87 //
88 class BranchInst : public TerminatorInst {
89   BranchInst(const BranchInst &BI);
90 public:
91   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
92   // BranchInst(BB *B)                           - 'br B'
93   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
94   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
95   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
96   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
97   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
98   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
99   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
100              Instruction *InsertBefore = 0);
101   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
102   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
103              BasicBlock *InsertAtEnd);
104
105   virtual Instruction *clone() const { return new BranchInst(*this); }
106
107   inline bool isUnconditional() const { return Operands.size() == 1; }
108   inline bool isConditional()   const { return Operands.size() == 3; }
109
110   inline Value *getCondition() const {
111     return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
112   }
113
114   void setCondition(Value *V) {
115     assert(isConditional() && "Cannot set condition of unconditional branch!");
116     setOperand(2, V);
117   }
118
119   // setUnconditionalDest - Change the current branch to an unconditional branch
120   // targeting the specified block.
121   //
122   void setUnconditionalDest(BasicBlock *Dest) {
123     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
124     Operands[0] = reinterpret_cast<Value*>(Dest);
125   }
126
127   virtual const BasicBlock *getSuccessor(unsigned i) const {
128     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
129     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
130                       cast<BasicBlock>(Operands[1].get());
131   }
132   inline BasicBlock *getSuccessor(unsigned idx) {
133     const BranchInst *BI = this;
134     return const_cast<BasicBlock*>(BI->getSuccessor(idx));
135   }
136
137   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
138     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
139     Operands[idx] = reinterpret_cast<Value*>(NewSucc);
140   }
141
142   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
143
144   // Methods for support type inquiry through isa, cast, and dyn_cast:
145   static inline bool classof(const BranchInst *) { return true; }
146   static inline bool classof(const Instruction *I) {
147     return (I->getOpcode() == Instruction::Br);
148   }
149   static inline bool classof(const Value *V) {
150     return isa<Instruction>(V) && classof(cast<Instruction>(V));
151   }
152 };
153
154
155 //===---------------------------------------------------------------------------
156 // SwitchInst - Multiway switch
157 //
158 class SwitchInst : public TerminatorInst {
159   // Operand[0]    = Value to switch on
160   // Operand[1]    = Default basic block destination
161   // Operand[2n  ] = Value to match
162   // Operand[2n+1] = BasicBlock to go to on match
163   SwitchInst(const SwitchInst &RI);
164 public:
165   SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
166   SwitchInst(Value *Value, BasicBlock *Default, BasicBlock  *InsertAtEnd);
167
168   virtual Instruction *clone() const { return new SwitchInst(*this); }
169
170   // Accessor Methods for Switch stmt
171   //
172   inline const Value *getCondition() const { return Operands[0]; }
173   inline       Value *getCondition()       { return Operands[0]; }
174   inline const BasicBlock *getDefaultDest() const {
175     return cast<BasicBlock>(Operands[1].get());
176   }
177   inline       BasicBlock *getDefaultDest()       {
178     return cast<BasicBlock>(Operands[1].get());
179   }
180
181   /// addCase - Add an entry to the switch instruction...
182   ///
183   void addCase(Constant *OnVal, BasicBlock *Dest);
184
185   /// removeCase - This method removes the specified successor from the switch
186   /// instruction.  Note that this cannot be used to remove the default
187   /// destination (successor #0).
188   ///
189   void removeCase(unsigned idx);
190
191   virtual const BasicBlock *getSuccessor(unsigned idx) const {
192     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
193     return cast<BasicBlock>(Operands[idx*2+1].get());
194   }
195   inline BasicBlock *getSuccessor(unsigned idx) {
196     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
197     return cast<BasicBlock>(Operands[idx*2+1].get());
198   }
199
200   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
201     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
202     Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
203   }
204
205   // getSuccessorValue - Return the value associated with the specified
206   // successor.
207   inline const Constant *getSuccessorValue(unsigned idx) const {
208     assert(idx < getNumSuccessors() && "Successor # out of range!");
209     return cast<Constant>(Operands[idx*2].get());
210   }
211   inline Constant *getSuccessorValue(unsigned idx) {
212     assert(idx < getNumSuccessors() && "Successor # out of range!");
213     return cast<Constant>(Operands[idx*2].get());
214   }
215   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
216
217   // Methods for support type inquiry through isa, cast, and dyn_cast:
218   static inline bool classof(const SwitchInst *) { return true; }
219   static inline bool classof(const Instruction *I) {
220     return (I->getOpcode() == Instruction::Switch);
221   }
222   static inline bool classof(const Value *V) {
223     return isa<Instruction>(V) && classof(cast<Instruction>(V));
224   }
225 };
226
227
228 //===---------------------------------------------------------------------------
229 // InvokeInst - Invoke instruction
230 //
231 class InvokeInst : public TerminatorInst {
232   InvokeInst(const InvokeInst &BI);
233 public:
234   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
235              const std::vector<Value*> &Params, const std::string &Name = "",
236              Instruction *InsertBefore = 0);
237   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
238              const std::vector<Value*> &Params, const std::string &Name,
239              BasicBlock *InsertAtEnd);
240
241   virtual Instruction *clone() const { return new InvokeInst(*this); }
242
243   bool mayWriteToMemory() const { return true; }
244
245   /// getCalledFunction - Return the function called, or null if this is an
246   /// indirect function invocation... 
247   ///
248   /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
249   ///
250   const Function *getCalledFunction() const;
251   Function *getCalledFunction();
252
253   // getCalledValue - Get a pointer to a function that is invoked by this inst.
254   inline const Value *getCalledValue() const { return Operands[0]; }
255   inline       Value *getCalledValue()       { return Operands[0]; }
256
257   // get*Dest - Return the destination basic blocks...
258   inline const BasicBlock *getNormalDest() const {
259     return cast<BasicBlock>(Operands[1].get());
260   }
261   inline       BasicBlock *getNormalDest() {
262     return cast<BasicBlock>(Operands[1].get());
263   }
264   inline const BasicBlock *getExceptionalDest() const {
265     return cast<BasicBlock>(Operands[2].get());
266   }
267   inline       BasicBlock *getExceptionalDest() {
268     return cast<BasicBlock>(Operands[2].get());
269   }
270
271   inline void setNormalDest(BasicBlock *B){
272     Operands[1] = reinterpret_cast<Value*>(B);
273   }
274
275   inline void setExceptionalDest(BasicBlock *B){
276     Operands[2] = reinterpret_cast<Value*>(B);
277   }
278
279   virtual const BasicBlock *getSuccessor(unsigned i) const {
280     assert(i < 2 && "Successor # out of range for invoke!");
281     return i == 0 ? getNormalDest() : getExceptionalDest();
282   }
283   inline BasicBlock *getSuccessor(unsigned i) {
284     assert(i < 2 && "Successor # out of range for invoke!");
285     return i == 0 ? getNormalDest() : getExceptionalDest();
286   }
287
288   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
289     assert(idx < 2 && "Successor # out of range for invoke!");
290     Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
291   }
292
293   virtual unsigned getNumSuccessors() const { return 2; }
294
295   // Methods for support type inquiry through isa, cast, and dyn_cast:
296   static inline bool classof(const InvokeInst *) { return true; }
297   static inline bool classof(const Instruction *I) {
298     return (I->getOpcode() == Instruction::Invoke);
299   }
300   static inline bool classof(const Value *V) {
301     return isa<Instruction>(V) && classof(cast<Instruction>(V));
302   }
303 };
304
305
306 //===---------------------------------------------------------------------------
307 /// UnwindInst - Immediately exit the current function, unwinding the stack
308 /// until an invoke instruction is found.
309 ///
310 struct UnwindInst : public TerminatorInst {
311   UnwindInst(Instruction *InsertBefore = 0)
312     : TerminatorInst(Instruction::Unwind, InsertBefore) {
313   }
314
315   virtual Instruction *clone() const { return new UnwindInst(); }
316
317   virtual const BasicBlock *getSuccessor(unsigned idx) const {
318     assert(0 && "UnwindInst has no successors!");
319     abort();
320     return 0;
321   }
322   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
323     assert(0 && "UnwindInst has no successors!");
324   }
325   virtual unsigned getNumSuccessors() const { return 0; }
326
327   // Methods for support type inquiry through isa, cast, and dyn_cast:
328   static inline bool classof(const UnwindInst *) { return true; }
329   static inline bool classof(const Instruction *I) {
330     return I->getOpcode() == Instruction::Unwind;
331   }
332   static inline bool classof(const Value *V) {
333     return isa<Instruction>(V) && classof(cast<Instruction>(V));
334   }
335 };
336
337 } // End llvm namespace
338
339 #endif