Change the semantics of getSuccessor to FAIL if an out of range successor # is attempted.
[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
4 // Instruction class, which is itself defined in the Instruction.h file.  In 
5 // between these definitions and the Instruction class are classes that expose
6 // the SSA properties of each instruction, and that form the SSA graph.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ITERMINATORS_H
11 #define LLVM_ITERMINATORS_H
12
13 #include "llvm/InstrTypes.h"
14
15 //===----------------------------------------------------------------------===//
16 //         Classes to represent Basic Block "Terminator" instructions
17 //===----------------------------------------------------------------------===//
18
19
20 //===---------------------------------------------------------------------------
21 // ReturnInst - Return a value (possibly void), from a method.  Execution does
22 //              not continue in this method any longer.
23 //
24 class ReturnInst : public TerminatorInst {
25   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
26     if (RI.Operands.size()) {
27       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
28       Operands.reserve(1);
29       Operands.push_back(Use(RI.Operands[0], this));
30     }
31   }
32 public:
33   ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
34     if (RetVal) {
35       Operands.reserve(1);
36       Operands.push_back(Use(RetVal, this));
37     }
38   }
39
40   virtual Instruction *clone() const { return new ReturnInst(*this); }
41
42   virtual const char *getOpcodeName() const { return "ret"; }
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   }
54   virtual unsigned getNumSuccessors() const { return 0; }
55
56   // Methods for support type inquiry through isa, cast, and dyn_cast:
57   static inline bool classof(const ReturnInst *) { return true; }
58   static inline bool classof(const Instruction *I) {
59     return (I->getOpcode() == Instruction::Ret);
60   }
61   static inline bool classof(const Value *V) {
62     return isa<Instruction>(V) && classof(cast<Instruction>(V));
63   }
64 };
65
66
67 //===---------------------------------------------------------------------------
68 // BranchInst - Conditional or Unconditional Branch instruction.
69 //
70 class BranchInst : public TerminatorInst {
71   BranchInst(const BranchInst &BI);
72 public:
73   // If cond = null, then is an unconditional br...
74   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 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 const Value *getCondition() const {
82     return isUnconditional() ? 0 : Operands[2].get();
83   }
84   inline       Value *getCondition()       {
85     return isUnconditional() ? 0 : Operands[2].get();
86   }
87
88   virtual const char *getOpcodeName() const { return "br"; }
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 unsigned getNumSuccessors() const { return 1+isConditional(); }
108
109   // Methods for support type inquiry through isa, cast, and dyn_cast:
110   static inline bool classof(const BranchInst *) { return true; }
111   static inline bool classof(const Instruction *I) {
112     return (I->getOpcode() == Instruction::Br);
113   }
114   static inline bool classof(const Value *V) {
115     return isa<Instruction>(V) && classof(cast<Instruction>(V));
116   }
117 };
118
119
120 //===---------------------------------------------------------------------------
121 // SwitchInst - Multiway switch
122 //
123 class SwitchInst : public TerminatorInst {
124   // Operand[0]    = Value to switch on
125   // Operand[1]    = Default basic block destination
126   // Operand[2n  ] = Value to match
127   // Operand[2n+1] = BasicBlock to go to on match
128   SwitchInst(const SwitchInst &RI);
129 public:
130   SwitchInst(Value *Value, BasicBlock *Default);
131
132   virtual Instruction *clone() const { return new SwitchInst(*this); }
133
134   // Accessor Methods for Switch stmt
135   //
136   inline const Value *getCondition() const { return Operands[0]; }
137   inline       Value *getCondition()       { return Operands[0]; }
138   inline const BasicBlock *getDefaultDest() const {
139     return cast<BasicBlock>(Operands[1].get());
140   }
141   inline       BasicBlock *getDefaultDest()       {
142     return cast<BasicBlock>(Operands[1].get());
143   }
144
145   void dest_push_back(Constant *OnVal, BasicBlock *Dest);
146
147   virtual const char *getOpcodeName() const { return "switch"; }
148
149   virtual const BasicBlock *getSuccessor(unsigned idx) const {
150     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
151     return cast<BasicBlock>(Operands[idx*2+1].get());
152   }
153   inline BasicBlock *getSuccessor(unsigned idx) {
154     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
155     return cast<BasicBlock>(Operands[idx*2+1].get());
156   }
157
158   // getSuccessorValue - Return the value associated with the specified
159   // successor.
160   inline const Constant *getSuccessorValue(unsigned idx) const {
161     assert(idx < getNumSuccessors() && "Successor # out of range!");
162     return cast<Constant>(Operands[idx*2].get());
163   }
164   inline Constant *getSuccessorValue(unsigned idx) {
165     assert(idx < getNumSuccessors() && "Successor # out of range!");
166     return cast<Constant>(Operands[idx*2].get());
167   }
168   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
169
170   // Methods for support type inquiry through isa, cast, and dyn_cast:
171   static inline bool classof(const SwitchInst *) { return true; }
172   static inline bool classof(const Instruction *I) {
173     return (I->getOpcode() == Instruction::Switch);
174   }
175   static inline bool classof(const Value *V) {
176     return isa<Instruction>(V) && classof(cast<Instruction>(V));
177   }
178 };
179
180
181 //===---------------------------------------------------------------------------
182 // InvokeInst - Invoke instruction
183 //
184 class InvokeInst : public TerminatorInst {
185   InvokeInst(const InvokeInst &BI);
186 public:
187   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
188              const std::vector<Value*> &Params, const std::string &Name = "");
189
190   virtual Instruction *clone() const { return new InvokeInst(*this); }
191
192   // getCalledFunction - Return the function called, or null if this is an
193   // indirect function invocation...
194   //
195   inline const Function *getCalledFunction() const {
196     return dyn_cast<Function>(Operands[0].get());
197   }
198   inline Function *getCalledFunction() {
199     return dyn_cast<Function>(Operands[0].get());
200   }
201
202   // getCalledValue - Get a pointer to a method that is invoked by this inst.
203   inline const Value *getCalledValue() const { return Operands[0]; }
204   inline       Value *getCalledValue()       { return Operands[0]; }
205
206   // get*Dest - Return the destination basic blocks...
207   inline const BasicBlock *getNormalDest() const {
208     return cast<BasicBlock>(Operands[1].get());
209   }
210   inline       BasicBlock *getNormalDest() {
211     return cast<BasicBlock>(Operands[1].get());
212   }
213   inline const BasicBlock *getExceptionalDest() const {
214     return cast<BasicBlock>(Operands[2].get());
215   }
216   inline       BasicBlock *getExceptionalDest() {
217     return cast<BasicBlock>(Operands[2].get());
218   }
219
220   virtual const char *getOpcodeName() const { return "invoke"; }
221
222   virtual const BasicBlock *getSuccessor(unsigned i) const {
223     assert(i < 2 && "Successor # out of range for invoke!");
224     return i == 0 ? getNormalDest() : getExceptionalDest();
225   }
226   inline BasicBlock *getSuccessor(unsigned i) {
227     assert(i < 2 && "Successor # out of range for invoke!");
228     return i == 0 ? getNormalDest() : getExceptionalDest();
229   }
230
231   virtual unsigned getNumSuccessors() const { return 2; }
232
233   // Methods for support type inquiry through isa, cast, and dyn_cast:
234   static inline bool classof(const InvokeInst *) { return true; }
235   static inline bool classof(const Instruction *I) {
236     return (I->getOpcode() == Instruction::Invoke);
237   }
238   static inline bool classof(const Value *V) {
239     return isa<Instruction>(V) && classof(cast<Instruction>(V));
240   }
241 };
242
243 #endif