Cleaned up the code (spacing, not needed headers) and changed ostream function. Also...
[oota-llvm.git] / include / llvm / iTerminators.h
1 //===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
2 //
3 // This file contains the declarations for all the subclasses of the Instruction
4 // class which represent "terminator" instructions.  Terminator instructions are
5 // the only instructions allowed and required to terminate a BasicBlock.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_ITERMINATORS_H
10 #define LLVM_ITERMINATORS_H
11
12 #include "llvm/InstrTypes.h"
13
14 //===---------------------------------------------------------------------------
15 // ReturnInst - Return a value (possibly void), from a function.  Execution does
16 //              not continue in this function any longer.
17 //
18 class ReturnInst : public TerminatorInst {
19   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
20     if (RI.Operands.size()) {
21       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
22       Operands.reserve(1);
23       Operands.push_back(Use(RI.Operands[0], this));
24     }
25   }
26 public:
27   ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
28     : TerminatorInst(Instruction::Ret, InsertBefore) {
29     if (RetVal) {
30       Operands.reserve(1);
31       Operands.push_back(Use(RetVal, this));
32     }
33   }
34
35   virtual Instruction *clone() const { return new ReturnInst(*this); }
36
37   inline const Value *getReturnValue() const {
38     return Operands.size() ? Operands[0].get() : 0; 
39   }
40   inline       Value *getReturnValue()       {
41     return Operands.size() ? Operands[0].get() : 0;
42   }
43
44   virtual const BasicBlock *getSuccessor(unsigned idx) const {
45     assert(0 && "ReturnInst has no successors!");
46     abort();
47     return 0;
48   }
49   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
50     assert(0 && "ReturnInst has no successors!");
51   }
52   virtual unsigned getNumSuccessors() const { return 0; }
53
54   // Methods for support type inquiry through isa, cast, and dyn_cast:
55   static inline bool classof(const ReturnInst *) { return true; }
56   static inline bool classof(const Instruction *I) {
57     return (I->getOpcode() == Instruction::Ret);
58   }
59   static inline bool classof(const Value *V) {
60     return isa<Instruction>(V) && classof(cast<Instruction>(V));
61   }
62 };
63
64
65 //===---------------------------------------------------------------------------
66 // BranchInst - Conditional or Unconditional Branch instruction.
67 //
68 class BranchInst : public TerminatorInst {
69   BranchInst(const BranchInst &BI);
70 public:
71   // If cond = null, then is an unconditional br...
72   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond = 0,
73              Instruction *InsertBefore = 0);
74   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
75
76   virtual Instruction *clone() const { return new BranchInst(*this); }
77
78   inline bool isUnconditional() const { return Operands.size() == 1; }
79   inline bool isConditional()   const { return Operands.size() == 3; }
80
81   inline Value *getCondition() const {
82     return isUnconditional() ? 0 : (Value*)Operands[2].get();
83   }
84
85   void setCondition(Value *V) {
86     assert(isConditional() && "Cannot set condition of unconditional branch!");
87     setOperand(2, V);
88   }
89
90   // setUnconditionalDest - Change the current branch to an unconditional branch
91   // targeting the specified block.
92   //
93   void setUnconditionalDest(BasicBlock *Dest) {
94     if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
95     Operands[0] = (Value*)Dest;
96   }
97
98   virtual const BasicBlock *getSuccessor(unsigned i) const {
99     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
100     return (i == 0) ? cast<BasicBlock>(Operands[0].get()) : 
101                       cast<BasicBlock>(Operands[1].get());
102   }
103   inline BasicBlock *getSuccessor(unsigned idx) {
104     return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
105   }
106
107   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
108     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
109     Operands[idx] = (Value*)NewSucc;
110   }
111
112   virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
113
114   // Methods for support type inquiry through isa, cast, and dyn_cast:
115   static inline bool classof(const BranchInst *) { return true; }
116   static inline bool classof(const Instruction *I) {
117     return (I->getOpcode() == Instruction::Br);
118   }
119   static inline bool classof(const Value *V) {
120     return isa<Instruction>(V) && classof(cast<Instruction>(V));
121   }
122 };
123
124
125 //===---------------------------------------------------------------------------
126 // SwitchInst - Multiway switch
127 //
128 class SwitchInst : public TerminatorInst {
129   // Operand[0]    = Value to switch on
130   // Operand[1]    = Default basic block destination
131   // Operand[2n  ] = Value to match
132   // Operand[2n+1] = BasicBlock to go to on match
133   SwitchInst(const SwitchInst &RI);
134 public:
135   SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
136
137   virtual Instruction *clone() const { return new SwitchInst(*this); }
138
139   // Accessor Methods for Switch stmt
140   //
141   inline const Value *getCondition() const { return Operands[0]; }
142   inline       Value *getCondition()       { return Operands[0]; }
143   inline const BasicBlock *getDefaultDest() const {
144     return cast<BasicBlock>(Operands[1].get());
145   }
146   inline       BasicBlock *getDefaultDest()       {
147     return cast<BasicBlock>(Operands[1].get());
148   }
149
150   /// addCase - Add an entry to the switch instruction...
151   ///
152   void addCase(Constant *OnVal, BasicBlock *Dest);
153
154   /// removeCase - This method removes the specified successor from the switch
155   /// instruction.  Note that this cannot be used to remove the default
156   /// destination (successor #0).
157   ///
158   void removeCase(unsigned idx);
159
160   virtual const BasicBlock *getSuccessor(unsigned idx) const {
161     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
162     return cast<BasicBlock>(Operands[idx*2+1].get());
163   }
164   inline BasicBlock *getSuccessor(unsigned idx) {
165     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
166     return cast<BasicBlock>(Operands[idx*2+1].get());
167   }
168
169   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
170     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
171     Operands[idx*2+1] = (Value*)NewSucc;
172   }
173
174   // getSuccessorValue - Return the value associated with the specified
175   // successor.
176   inline const Constant *getSuccessorValue(unsigned idx) const {
177     assert(idx < getNumSuccessors() && "Successor # out of range!");
178     return cast<Constant>(Operands[idx*2].get());
179   }
180   inline Constant *getSuccessorValue(unsigned idx) {
181     assert(idx < getNumSuccessors() && "Successor # out of range!");
182     return cast<Constant>(Operands[idx*2].get());
183   }
184   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
185
186   // Methods for support type inquiry through isa, cast, and dyn_cast:
187   static inline bool classof(const SwitchInst *) { return true; }
188   static inline bool classof(const Instruction *I) {
189     return (I->getOpcode() == Instruction::Switch);
190   }
191   static inline bool classof(const Value *V) {
192     return isa<Instruction>(V) && classof(cast<Instruction>(V));
193   }
194 };
195
196
197 //===---------------------------------------------------------------------------
198 // InvokeInst - Invoke instruction
199 //
200 class InvokeInst : public TerminatorInst {
201   InvokeInst(const InvokeInst &BI);
202 public:
203   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
204              const std::vector<Value*> &Params, const std::string &Name = "",
205              Instruction *InsertBefore = 0);
206
207   virtual Instruction *clone() const { return new InvokeInst(*this); }
208
209   bool mayWriteToMemory() const { return true; }
210
211   // getCalledFunction - Return the function called, or null if this is an
212   // indirect function invocation...
213   //
214   inline const Function *getCalledFunction() const {
215     return dyn_cast<Function>(Operands[0].get());
216   }
217   inline Function *getCalledFunction() {
218     return dyn_cast<Function>(Operands[0].get());
219   }
220
221   // getCalledValue - Get a pointer to a function that is invoked by this inst.
222   inline const Value *getCalledValue() const { return Operands[0]; }
223   inline       Value *getCalledValue()       { return Operands[0]; }
224
225   // get*Dest - Return the destination basic blocks...
226   inline const BasicBlock *getNormalDest() const {
227     return cast<BasicBlock>(Operands[1].get());
228   }
229   inline       BasicBlock *getNormalDest() {
230     return cast<BasicBlock>(Operands[1].get());
231   }
232   inline const BasicBlock *getExceptionalDest() const {
233     return cast<BasicBlock>(Operands[2].get());
234   }
235   inline       BasicBlock *getExceptionalDest() {
236     return cast<BasicBlock>(Operands[2].get());
237   }
238
239   inline void setNormalDest(BasicBlock *B){
240     Operands[1] = (Value*)B;
241   }
242
243   inline void setExceptionalDest(BasicBlock *B){
244     Operands[2] = (Value*)B;
245   }
246
247   virtual const BasicBlock *getSuccessor(unsigned i) const {
248     assert(i < 2 && "Successor # out of range for invoke!");
249     return i == 0 ? getNormalDest() : getExceptionalDest();
250   }
251   inline BasicBlock *getSuccessor(unsigned i) {
252     assert(i < 2 && "Successor # out of range for invoke!");
253     return i == 0 ? getNormalDest() : getExceptionalDest();
254   }
255
256   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
257     assert(idx < 2 && "Successor # out of range for invoke!");
258     Operands[idx+1] = (Value*)NewSucc;
259   }
260
261   virtual unsigned getNumSuccessors() const { return 2; }
262
263   // Methods for support type inquiry through isa, cast, and dyn_cast:
264   static inline bool classof(const InvokeInst *) { return true; }
265   static inline bool classof(const Instruction *I) {
266     return (I->getOpcode() == Instruction::Invoke);
267   }
268   static inline bool classof(const Value *V) {
269     return isa<Instruction>(V) && classof(cast<Instruction>(V));
270   }
271 };
272
273 #endif