Fix PR266: Make Module Not Inherit From Annotable
[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
25 /// does 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   /// getNumCases - return the number of 'cases' in this switch instruction.
182   /// Note that case #0 is always the default case.
183   unsigned getNumCases() const {
184     return Operands.size()/2;
185   }
186
187   /// getCaseValue - Return the specified case value.  Note that case #0, the
188   /// default destination, does not have a case value.
189   Constant *getCaseValue(unsigned i) {
190     assert(i && i < getNumCases() && "Illegal case value to get!");
191     return getSuccessorValue(i);
192   }
193
194   /// getCaseValue - Return the specified case value.  Note that case #0, the
195   /// default destination, does not have a case value.
196   const Constant *getCaseValue(unsigned i) const {
197     assert(i && i < getNumCases() && "Illegal case value to get!");
198     return getSuccessorValue(i);
199   }
200
201   /// findCaseValue - Search all of the case values for the specified constant.
202   /// If it is explicitly handled, return the case number of it, otherwise
203   /// return 0 to indicate that it is handled by the default handler.
204   unsigned findCaseValue(const Constant *C) const {
205     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
206       if (getCaseValue(i) == C)
207         return i;
208     return 0;
209   }
210
211   /// addCase - Add an entry to the switch instruction...
212   ///
213   void addCase(Constant *OnVal, BasicBlock *Dest);
214
215   /// removeCase - This method removes the specified successor from the switch
216   /// instruction.  Note that this cannot be used to remove the default
217   /// destination (successor #0).
218   ///
219   void removeCase(unsigned idx);
220
221   virtual const BasicBlock *getSuccessor(unsigned idx) const {
222     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
223     return cast<BasicBlock>(Operands[idx*2+1].get());
224   }
225   inline BasicBlock *getSuccessor(unsigned idx) {
226     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
227     return cast<BasicBlock>(Operands[idx*2+1].get());
228   }
229
230   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
231     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
232     Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
233   }
234
235   // getSuccessorValue - Return the value associated with the specified
236   // successor.
237   inline const Constant *getSuccessorValue(unsigned idx) const {
238     assert(idx < getNumSuccessors() && "Successor # out of range!");
239     return cast<Constant>(Operands[idx*2].get());
240   }
241   inline Constant *getSuccessorValue(unsigned idx) {
242     assert(idx < getNumSuccessors() && "Successor # out of range!");
243     return cast<Constant>(Operands[idx*2].get());
244   }
245   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
246
247   // Methods for support type inquiry through isa, cast, and dyn_cast:
248   static inline bool classof(const SwitchInst *) { return true; }
249   static inline bool classof(const Instruction *I) {
250     return (I->getOpcode() == Instruction::Switch);
251   }
252   static inline bool classof(const Value *V) {
253     return isa<Instruction>(V) && classof(cast<Instruction>(V));
254   }
255 };
256
257 //===---------------------------------------------------------------------------
258 /// InvokeInst - Invoke instruction
259 ///
260 class InvokeInst : public TerminatorInst {
261   InvokeInst(const InvokeInst &BI);
262 public:
263   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
264              const std::vector<Value*> &Params, const std::string &Name = "",
265              Instruction *InsertBefore = 0);
266   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
267              const std::vector<Value*> &Params, const std::string &Name,
268              BasicBlock *InsertAtEnd);
269
270   virtual Instruction *clone() const { return new InvokeInst(*this); }
271
272   bool mayWriteToMemory() const { return true; }
273
274   /// getCalledFunction - Return the function called, or null if this is an
275   /// indirect function invocation... 
276   ///
277   /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
278   ///
279   const Function *getCalledFunction() const;
280   Function *getCalledFunction();
281
282   // getCalledValue - Get a pointer to a function that is invoked by this inst.
283   inline const Value *getCalledValue() const { return Operands[0]; }
284   inline       Value *getCalledValue()       { return Operands[0]; }
285
286   // get*Dest - Return the destination basic blocks...
287   inline const BasicBlock *getNormalDest() const {
288     return cast<BasicBlock>(Operands[1].get());
289   }
290   inline       BasicBlock *getNormalDest() {
291     return cast<BasicBlock>(Operands[1].get());
292   }
293   inline const BasicBlock *getUnwindDest() const {
294     return cast<BasicBlock>(Operands[2].get());
295   }
296   inline       BasicBlock *getUnwindDest() {
297     return cast<BasicBlock>(Operands[2].get());
298   }
299
300   inline void setNormalDest(BasicBlock *B){
301     Operands[1] = reinterpret_cast<Value*>(B);
302   }
303
304   inline void setUnwindDest(BasicBlock *B){
305     Operands[2] = reinterpret_cast<Value*>(B);
306   }
307
308   virtual const BasicBlock *getSuccessor(unsigned i) const {
309     assert(i < 2 && "Successor # out of range for invoke!");
310     return i == 0 ? getNormalDest() : getUnwindDest();
311   }
312   inline BasicBlock *getSuccessor(unsigned i) {
313     assert(i < 2 && "Successor # out of range for invoke!");
314     return i == 0 ? getNormalDest() : getUnwindDest();
315   }
316
317   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
318     assert(idx < 2 && "Successor # out of range for invoke!");
319     Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
320   }
321
322   virtual unsigned getNumSuccessors() const { return 2; }
323
324   // Methods for support type inquiry through isa, cast, and dyn_cast:
325   static inline bool classof(const InvokeInst *) { return true; }
326   static inline bool classof(const Instruction *I) {
327     return (I->getOpcode() == Instruction::Invoke);
328   }
329   static inline bool classof(const Value *V) {
330     return isa<Instruction>(V) && classof(cast<Instruction>(V));
331   }
332 };
333
334
335 //===---------------------------------------------------------------------------
336 /// UnwindInst - Immediately exit the current function, unwinding the stack
337 /// until an invoke instruction is found.
338 ///
339 struct UnwindInst : public TerminatorInst {
340   UnwindInst(Instruction *InsertBefore = 0)
341     : TerminatorInst(Instruction::Unwind, InsertBefore) {
342   }
343   UnwindInst(BasicBlock *InsertAtEnd)
344     : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
345   }
346
347   virtual Instruction *clone() const { return new UnwindInst(); }
348
349   virtual const BasicBlock *getSuccessor(unsigned idx) const {
350     assert(0 && "UnwindInst has no successors!");
351     abort();
352     return 0;
353   }
354   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
355   virtual unsigned getNumSuccessors() const { return 0; }
356
357   // Methods for support type inquiry through isa, cast, and dyn_cast:
358   static inline bool classof(const UnwindInst *) { return true; }
359   static inline bool classof(const Instruction *I) {
360     return I->getOpcode() == Instruction::Unwind;
361   }
362   static inline bool classof(const Value *V) {
363     return isa<Instruction>(V) && classof(cast<Instruction>(V));
364   }
365 };
366
367 } // End llvm namespace
368
369 #endif