s/Method/Function
[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 #include "llvm/BasicBlock.h"
15 #include "llvm/ConstantVals.h"
16
17 //===----------------------------------------------------------------------===//
18 //         Classes to represent Basic Block "Terminator" instructions
19 //===----------------------------------------------------------------------===//
20
21
22 //===---------------------------------------------------------------------------
23 // ReturnInst - Return a value (possibly void), from a method.  Execution does
24 //              not continue in this method any longer.
25 //
26 class ReturnInst : public TerminatorInst {
27   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
28     if (RI.Operands.size()) {
29       assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
30       Operands.reserve(1);
31       Operands.push_back(Use(RI.Operands[0], this));
32     }
33   }
34 public:
35   ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
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   virtual const char *getOpcodeName() const { return "ret"; }
45
46   inline const Value *getReturnValue() const {
47     return Operands.size() ? Operands[0].get() : 0; 
48   }
49   inline       Value *getReturnValue()       {
50     return Operands.size() ? Operands[0].get() : 0;
51   }
52
53   // Additionally, they must provide a method to get at the successors of this
54   // terminator instruction.  If 'idx' is out of range, a null pointer shall be
55   // returned.
56   //
57   virtual const BasicBlock *getSuccessor(unsigned idx) const { return 0; }
58   virtual unsigned getNumSuccessors() const { return 0; }
59
60   // Methods for support type inquiry through isa, cast, and dyn_cast:
61   static inline bool classof(const ReturnInst *) { return true; }
62   static inline bool classof(const Instruction *I) {
63     return (I->getOpcode() == Instruction::Ret);
64   }
65   static inline bool classof(const Value *V) {
66     return isa<Instruction>(V) && classof(cast<Instruction>(V));
67   }
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 = 0, Value *cond = 0);
79
80   virtual Instruction *clone() const { return new BranchInst(*this); }
81
82   inline bool isUnconditional() const {
83     return Operands.size() == 1;
84   }
85
86   inline const Value *getCondition() const {
87     return isUnconditional() ? 0 : Operands[2].get();
88   }
89   inline       Value *getCondition()       {
90     return isUnconditional() ? 0 : Operands[2].get();
91   }
92
93   virtual const char *getOpcodeName() const { return "br"; }
94
95   // setUnconditionalDest - Change the current branch to an unconditional branch
96   // targeting the specified block.
97   //
98   void setUnconditionalDest(BasicBlock *Dest) {
99     if (Operands.size() == 3)
100       Operands.erase(Operands.begin()+1, Operands.end());
101     Operands[0] = Dest;
102   }
103
104   // Additionally, they must provide a method to get at the successors of this
105   // terminator instruction.
106   //
107   virtual const BasicBlock *getSuccessor(unsigned i) const {
108     return (i == 0) ? cast<const BasicBlock>(Operands[0]) : 
109           ((i == 1 && Operands.size() > 1) 
110                ? cast<const BasicBlock>(Operands[1]) : 0);
111   }
112   inline BasicBlock *getSuccessor(unsigned idx) {
113     return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
114   }
115
116   virtual unsigned getNumSuccessors() const { return 1+!isUnconditional(); }
117
118   // Methods for support type inquiry through isa, cast, and dyn_cast:
119   static inline bool classof(const BranchInst *) { return true; }
120   static inline bool classof(const Instruction *I) {
121     return (I->getOpcode() == Instruction::Br);
122   }
123   static inline bool classof(const Value *V) {
124     return isa<Instruction>(V) && classof(cast<Instruction>(V));
125   }
126 };
127
128
129 //===---------------------------------------------------------------------------
130 // SwitchInst - Multiway switch
131 //
132 class SwitchInst : public TerminatorInst {
133   // Operand[0]    = Value to switch on
134   // Operand[1]    = Default basic block destination
135   // Operand[2n  ] = Value to match
136   // Operand[2n+1] = BasicBlock to go to on match
137   SwitchInst(const SwitchInst &RI);
138 public:
139   SwitchInst(Value *Value, BasicBlock *Default);
140
141   virtual Instruction *clone() const { return new SwitchInst(*this); }
142
143   // Accessor Methods for Switch stmt
144   //
145   inline const Value *getCondition() const { return Operands[0]; }
146   inline       Value *getCondition()       { return Operands[0]; }
147   inline const BasicBlock *getDefaultDest() const {
148     return cast<const BasicBlock>(Operands[1]);
149   }
150   inline       BasicBlock *getDefaultDest()       {
151     return cast<BasicBlock>(Operands[1]);
152   }
153
154   void dest_push_back(Constant *OnVal, BasicBlock *Dest);
155
156   virtual const char *getOpcodeName() const { return "switch"; }
157
158   // Additionally, they must provide a method to get at the successors of this
159   // terminator instruction.  If 'idx' is out of range, a null pointer shall be
160   // returned.
161   //
162   virtual const BasicBlock *getSuccessor(unsigned idx) const {
163     if (idx >= Operands.size()/2) return 0;
164     return cast<const BasicBlock>(Operands[idx*2+1]);
165   }
166   inline BasicBlock *getSuccessor(unsigned idx) {
167     if (idx >= Operands.size()/2) return 0;
168     return cast<BasicBlock>(Operands[idx*2+1]);
169   }
170
171   // getSuccessorValue - Return the value associated with the specified
172   // successor. WARNING: This does not gracefully accept idx's out of range!
173   inline const Constant *getSuccessorValue(unsigned idx) const {
174     assert(idx < getNumSuccessors() && "Successor # out of range!");
175     return cast<const Constant>(Operands[idx*2]);
176   }
177   inline Constant *getSuccessorValue(unsigned idx) {
178     assert(idx < getNumSuccessors() && "Successor # out of range!");
179     return cast<Constant>(Operands[idx*2]);
180   }
181   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
182
183   // Methods for support type inquiry through isa, cast, and dyn_cast:
184   static inline bool classof(const SwitchInst *) { return true; }
185   static inline bool classof(const Instruction *I) {
186     return (I->getOpcode() == Instruction::Switch);
187   }
188   static inline bool classof(const Value *V) {
189     return isa<Instruction>(V) && classof(cast<Instruction>(V));
190   }
191 };
192
193
194 //===---------------------------------------------------------------------------
195 // InvokeInst - Invoke instruction
196 //
197 class InvokeInst : public TerminatorInst {
198   InvokeInst(const InvokeInst &BI);
199 public:
200   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
201              const std::vector<Value*> &Params, const std::string &Name = "");
202
203   virtual Instruction *clone() const { return new InvokeInst(*this); }
204
205   // getCalledFunction - Return the function called, or null if this is an
206   // indirect function invocation...
207   //
208   inline const Function *getCalledFunction() const {
209     return dyn_cast<Function>(Operands[0].get());
210   }
211   inline Function *getCalledFunction() {
212     return dyn_cast<Function>(Operands[0].get());
213   }
214
215   // getCalledValue - Get a pointer to a method that is invoked by this inst.
216   inline const Value *getCalledValue() const { return Operands[0]; }
217   inline       Value *getCalledValue()       { return Operands[0]; }
218
219   // get*Dest - Return the destination basic blocks...
220   inline const BasicBlock *getNormalDest() const {
221     return cast<BasicBlock>(Operands[1]);
222   }
223   inline       BasicBlock *getNormalDest() {
224     return cast<BasicBlock>(Operands[1]);
225   }
226   inline const BasicBlock *getExceptionalDest() const {
227     return cast<BasicBlock>(Operands[2]);
228   }
229   inline       BasicBlock *getExceptionalDest() {
230     return cast<BasicBlock>(Operands[2]);
231   }
232
233   virtual const char *getOpcodeName() const { return "invoke"; }
234
235   // Additionally, they must provide a method to get at the successors of this
236   // terminator instruction.
237   //
238   virtual const BasicBlock *getSuccessor(unsigned i) const {
239     return (i == 0) ? getNormalDest() :
240           ((i == 1) ? getExceptionalDest() : 0);
241   }
242   inline BasicBlock *getSuccessor(unsigned i) {
243     return (i == 0) ? getNormalDest() :
244           ((i == 1) ? getExceptionalDest() : 0);
245   }
246
247   virtual unsigned getNumSuccessors() const { return 2; }
248
249   // Methods for support type inquiry through isa, cast, and dyn_cast:
250   static inline bool classof(const InvokeInst *) { return true; }
251   static inline bool classof(const Instruction *I) {
252     return (I->getOpcode() == Instruction::Invoke);
253   }
254   static inline bool classof(const Value *V) {
255     return isa<Instruction>(V) && classof(cast<Instruction>(V));
256   }
257 };
258
259 #endif