An "autoconf wrapper" for the infamous windows.h file
[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
36   void init(Value *RetVal) {
37     if (RetVal) {
38       Operands.reserve(1);
39       Operands.push_back(Use(RetVal, this));
40     }
41   }
42
43 public:
44   // ReturnInst constructors:
45   // ReturnInst()                  - 'ret void' instruction
46   // ReturnInst(Value* X)          - 'ret X'    instruction
47   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
48   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
49   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
50   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
51   ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
52     : TerminatorInst(Instruction::Ret, InsertBefore) {
53     init(RetVal);
54   }
55   ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
56     : TerminatorInst(Instruction::Ret, InsertAtEnd) {
57     init(RetVal);
58   }
59
60   virtual Instruction *clone() const { return new ReturnInst(*this); }
61
62   inline const Value *getReturnValue() const {
63     return Operands.size() ? Operands[0].get() : 0; 
64   }
65   inline       Value *getReturnValue()       {
66     return Operands.size() ? Operands[0].get() : 0;
67   }
68
69   virtual const BasicBlock *getSuccessor(unsigned idx) const {
70     assert(0 && "ReturnInst has no successors!");
71     abort();
72     return 0;
73   }
74   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
75   virtual unsigned getNumSuccessors() const { return 0; }
76
77   // Methods for support type inquiry through isa, cast, and dyn_cast:
78   static inline bool classof(const ReturnInst *) { return true; }
79   static inline bool classof(const Instruction *I) {
80     return (I->getOpcode() == Instruction::Ret);
81   }
82   static inline bool classof(const Value *V) {
83     return isa<Instruction>(V) && classof(cast<Instruction>(V));
84   }
85 };
86
87 //===---------------------------------------------------------------------------
88 /// BranchInst - Conditional or Unconditional Branch instruction.
89 ///
90 class BranchInst : public TerminatorInst {
91   BranchInst(const BranchInst &BI);
92   void init(BasicBlock *IfTrue);
93   void init(BasicBlock *True, BasicBlock *False, Value *Cond);
94 public:
95   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
96   // BranchInst(BB *B)                           - 'br B'
97   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
98   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
99   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
100   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
101   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
102   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
103     : TerminatorInst(Instruction::Br, InsertBefore) {
104     init(IfTrue);
105   }
106   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
107              Instruction *InsertBefore = 0)
108     : TerminatorInst(Instruction::Br, InsertBefore) {
109     init(IfTrue, IfFalse, Cond);
110   }
111
112   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
113     : TerminatorInst(Instruction::Br, InsertAtEnd) {
114     init(IfTrue);
115   }
116
117   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
118              BasicBlock *InsertAtEnd)
119     : TerminatorInst(Instruction::Br, InsertAtEnd) {
120     init(IfTrue, IfFalse, Cond);
121   }
122
123   virtual Instruction *clone() const { return new BranchInst(*this); }
124
125   inline bool isUnconditional() const { return Operands.size() == 1; }
126   inline bool isConditional()   const { return Operands.size() == 3; }
127
128   inline Value *getCondition() const {
129     return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
130   }
131
132   void setCondition(Value *V) {
133     assert(isConditional() && "Cannot set condition of unconditional branch!");
134     setOperand(2, V);
135   }
136
137   // setUnconditionalDest - Change the current branch to an unconditional branch
138   // targeting the specified block.
139   //
140   void setUnconditionalDest(BasicBlock *Dest) {
141     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
142     Operands[0] = reinterpret_cast<Value*>(Dest);
143   }
144
145   virtual const BasicBlock *getSuccessor(unsigned i) const {
146     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
147     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
148                       cast<BasicBlock>(Operands[1].get());
149   }
150   inline BasicBlock *getSuccessor(unsigned idx) {
151     const BranchInst *BI = this;
152     return const_cast<BasicBlock*>(BI->getSuccessor(idx));
153   }
154
155   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
156     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
157     Operands[idx] = reinterpret_cast<Value*>(NewSucc);
158   }
159
160   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
161
162   // Methods for support type inquiry through isa, cast, and dyn_cast:
163   static inline bool classof(const BranchInst *) { return true; }
164   static inline bool classof(const Instruction *I) {
165     return (I->getOpcode() == Instruction::Br);
166   }
167   static inline bool classof(const Value *V) {
168     return isa<Instruction>(V) && classof(cast<Instruction>(V));
169   }
170 };
171
172
173 //===---------------------------------------------------------------------------
174 /// SwitchInst - Multiway switch
175 ///
176 class SwitchInst : public TerminatorInst {
177   // Operand[0]    = Value to switch on
178   // Operand[1]    = Default basic block destination
179   // Operand[2n  ] = Value to match
180   // Operand[2n+1] = BasicBlock to go to on match
181   SwitchInst(const SwitchInst &RI);
182   void init(Value *Value, BasicBlock *Default);
183
184 public:
185   SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0) 
186     : TerminatorInst(Instruction::Switch, InsertBefore) {
187     init(Value, Default);
188   }
189   SwitchInst(Value *Value, BasicBlock *Default, BasicBlock  *InsertAtEnd) 
190     : TerminatorInst(Instruction::Switch, InsertAtEnd) {
191     init(Value, Default);
192   }
193
194   virtual Instruction *clone() const { return new SwitchInst(*this); }
195
196   // Accessor Methods for Switch stmt
197   //
198   inline const Value *getCondition() const { return Operands[0]; }
199   inline       Value *getCondition()       { return Operands[0]; }
200   inline const BasicBlock *getDefaultDest() const {
201     return cast<BasicBlock>(Operands[1].get());
202   }
203   inline       BasicBlock *getDefaultDest()       {
204     return cast<BasicBlock>(Operands[1].get());
205   }
206
207   /// getNumCases - return the number of 'cases' in this switch instruction.
208   /// Note that case #0 is always the default case.
209   unsigned getNumCases() const {
210     return Operands.size()/2;
211   }
212
213   /// getCaseValue - Return the specified case value.  Note that case #0, the
214   /// default destination, does not have a case value.
215   Constant *getCaseValue(unsigned i) {
216     assert(i && i < getNumCases() && "Illegal case value to get!");
217     return getSuccessorValue(i);
218   }
219
220   /// getCaseValue - Return the specified case value.  Note that case #0, the
221   /// default destination, does not have a case value.
222   const Constant *getCaseValue(unsigned i) const {
223     assert(i && i < getNumCases() && "Illegal case value to get!");
224     return getSuccessorValue(i);
225   }
226
227   /// findCaseValue - Search all of the case values for the specified constant.
228   /// If it is explicitly handled, return the case number of it, otherwise
229   /// return 0 to indicate that it is handled by the default handler.
230   unsigned findCaseValue(const Constant *C) const {
231     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
232       if (getCaseValue(i) == C)
233         return i;
234     return 0;
235   }
236
237   /// addCase - Add an entry to the switch instruction...
238   ///
239   void addCase(Constant *OnVal, BasicBlock *Dest);
240
241   /// removeCase - This method removes the specified successor from the switch
242   /// instruction.  Note that this cannot be used to remove the default
243   /// destination (successor #0).
244   ///
245   void removeCase(unsigned idx);
246
247   virtual const BasicBlock *getSuccessor(unsigned idx) const {
248     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
249     return cast<BasicBlock>(Operands[idx*2+1].get());
250   }
251   inline BasicBlock *getSuccessor(unsigned idx) {
252     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
253     return cast<BasicBlock>(Operands[idx*2+1].get());
254   }
255
256   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
257     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
258     Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
259   }
260
261   // getSuccessorValue - Return the value associated with the specified
262   // successor.
263   inline const Constant *getSuccessorValue(unsigned idx) const {
264     assert(idx < getNumSuccessors() && "Successor # out of range!");
265     return cast<Constant>(Operands[idx*2].get());
266   }
267   inline Constant *getSuccessorValue(unsigned idx) {
268     assert(idx < getNumSuccessors() && "Successor # out of range!");
269     return cast<Constant>(Operands[idx*2].get());
270   }
271   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
272
273   // Methods for support type inquiry through isa, cast, and dyn_cast:
274   static inline bool classof(const SwitchInst *) { return true; }
275   static inline bool classof(const Instruction *I) {
276     return (I->getOpcode() == Instruction::Switch);
277   }
278   static inline bool classof(const Value *V) {
279     return isa<Instruction>(V) && classof(cast<Instruction>(V));
280   }
281 };
282
283 //===---------------------------------------------------------------------------
284 /// InvokeInst - Invoke instruction
285 ///
286 class InvokeInst : public TerminatorInst {
287   InvokeInst(const InvokeInst &BI);
288   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
289             const std::vector<Value*> &Params);
290 public:
291   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
292              const std::vector<Value*> &Params, const std::string &Name = "",
293              Instruction *InsertBefore = 0);
294   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
295              const std::vector<Value*> &Params, const std::string &Name,
296              BasicBlock *InsertAtEnd);
297
298   virtual Instruction *clone() const { return new InvokeInst(*this); }
299
300   bool mayWriteToMemory() const { return true; }
301
302   /// getCalledFunction - Return the function called, or null if this is an
303   /// indirect function invocation... 
304   ///
305   /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
306   ///
307   const Function *getCalledFunction() const;
308   Function *getCalledFunction();
309
310   // getCalledValue - Get a pointer to a function that is invoked by this inst.
311   inline const Value *getCalledValue() const { return Operands[0]; }
312   inline       Value *getCalledValue()       { return Operands[0]; }
313
314   // get*Dest - Return the destination basic blocks...
315   inline const BasicBlock *getNormalDest() const {
316     return cast<BasicBlock>(Operands[1].get());
317   }
318   inline       BasicBlock *getNormalDest() {
319     return cast<BasicBlock>(Operands[1].get());
320   }
321   inline const BasicBlock *getUnwindDest() const {
322     return cast<BasicBlock>(Operands[2].get());
323   }
324   inline       BasicBlock *getUnwindDest() {
325     return cast<BasicBlock>(Operands[2].get());
326   }
327
328   inline void setNormalDest(BasicBlock *B){
329     Operands[1] = reinterpret_cast<Value*>(B);
330   }
331
332   inline void setUnwindDest(BasicBlock *B){
333     Operands[2] = reinterpret_cast<Value*>(B);
334   }
335
336   virtual const BasicBlock *getSuccessor(unsigned i) const {
337     assert(i < 2 && "Successor # out of range for invoke!");
338     return i == 0 ? getNormalDest() : getUnwindDest();
339   }
340   inline BasicBlock *getSuccessor(unsigned i) {
341     assert(i < 2 && "Successor # out of range for invoke!");
342     return i == 0 ? getNormalDest() : getUnwindDest();
343   }
344
345   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
346     assert(idx < 2 && "Successor # out of range for invoke!");
347     Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
348   }
349
350   virtual unsigned getNumSuccessors() const { return 2; }
351
352   // Methods for support type inquiry through isa, cast, and dyn_cast:
353   static inline bool classof(const InvokeInst *) { return true; }
354   static inline bool classof(const Instruction *I) {
355     return (I->getOpcode() == Instruction::Invoke);
356   }
357   static inline bool classof(const Value *V) {
358     return isa<Instruction>(V) && classof(cast<Instruction>(V));
359   }
360 };
361
362
363 //===---------------------------------------------------------------------------
364 /// UnwindInst - Immediately exit the current function, unwinding the stack
365 /// until an invoke instruction is found.
366 ///
367 struct UnwindInst : public TerminatorInst {
368   UnwindInst(Instruction *InsertBefore = 0)
369     : TerminatorInst(Instruction::Unwind, InsertBefore) {
370   }
371   UnwindInst(BasicBlock *InsertAtEnd)
372     : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
373   }
374
375   virtual Instruction *clone() const { return new UnwindInst(); }
376
377   virtual const BasicBlock *getSuccessor(unsigned idx) const {
378     assert(0 && "UnwindInst has no successors!");
379     abort();
380     return 0;
381   }
382   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
383   virtual unsigned getNumSuccessors() const { return 0; }
384
385   // Methods for support type inquiry through isa, cast, and dyn_cast:
386   static inline bool classof(const UnwindInst *) { return true; }
387   static inline bool classof(const Instruction *I) {
388     return I->getOpcode() == Instruction::Unwind;
389   }
390   static inline bool classof(const Value *V) {
391     return isa<Instruction>(V) && classof(cast<Instruction>(V));
392   }
393 };
394
395 } // End llvm namespace
396
397 #endif