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