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