Create a static version of Instruction::getOpcodeName(opCode) that
[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   inline const Value *getReturnValue() const {
43     return Operands.size() ? Operands[0].get() : 0; 
44   }
45   inline       Value *getReturnValue()       {
46     return Operands.size() ? Operands[0].get() : 0;
47   }
48
49   virtual const BasicBlock *getSuccessor(unsigned idx) const {
50     assert(0 && "ReturnInst has no successors!");
51     abort();
52   }
53   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
54     assert(0 && "ReturnInst has no successors!");
55   }
56   virtual unsigned getNumSuccessors() const { return 0; }
57
58   // Methods for support type inquiry through isa, cast, and dyn_cast:
59   static inline bool classof(const ReturnInst *) { return true; }
60   static inline bool classof(const Instruction *I) {
61     return (I->getOpcode() == Instruction::Ret);
62   }
63   static inline bool classof(const Value *V) {
64     return isa<Instruction>(V) && classof(cast<Instruction>(V));
65   }
66 };
67
68
69 //===---------------------------------------------------------------------------
70 // BranchInst - Conditional or Unconditional Branch instruction.
71 //
72 class BranchInst : public TerminatorInst {
73   BranchInst(const BranchInst &BI);
74 public:
75   // If cond = null, then is an unconditional br...
76   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
77
78   virtual Instruction *clone() const { return new BranchInst(*this); }
79
80   inline bool isUnconditional() const { return Operands.size() == 1; }
81   inline bool isConditional()   const { return Operands.size() == 3; }
82
83   inline const Value *getCondition() const {
84     return isUnconditional() ? 0 : Operands[2].get();
85   }
86   inline       Value *getCondition()       {
87     return isUnconditional() ? 0 : Operands[2].get();
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);
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   void dest_push_back(Constant *OnVal, BasicBlock *Dest);
151
152   virtual const BasicBlock *getSuccessor(unsigned idx) const {
153     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
154     return cast<BasicBlock>(Operands[idx*2+1].get());
155   }
156   inline BasicBlock *getSuccessor(unsigned idx) {
157     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
158     return cast<BasicBlock>(Operands[idx*2+1].get());
159   }
160
161   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
162     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
163     Operands[idx*2+1] = (Value*)NewSucc;
164   }
165
166   // getSuccessorValue - Return the value associated with the specified
167   // successor.
168   inline const Constant *getSuccessorValue(unsigned idx) const {
169     assert(idx < getNumSuccessors() && "Successor # out of range!");
170     return cast<Constant>(Operands[idx*2].get());
171   }
172   inline Constant *getSuccessorValue(unsigned idx) {
173     assert(idx < getNumSuccessors() && "Successor # out of range!");
174     return cast<Constant>(Operands[idx*2].get());
175   }
176   virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
177
178   // Methods for support type inquiry through isa, cast, and dyn_cast:
179   static inline bool classof(const SwitchInst *) { return true; }
180   static inline bool classof(const Instruction *I) {
181     return (I->getOpcode() == Instruction::Switch);
182   }
183   static inline bool classof(const Value *V) {
184     return isa<Instruction>(V) && classof(cast<Instruction>(V));
185   }
186 };
187
188
189 //===---------------------------------------------------------------------------
190 // InvokeInst - Invoke instruction
191 //
192 class InvokeInst : public TerminatorInst {
193   InvokeInst(const InvokeInst &BI);
194 public:
195   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
196              const std::vector<Value*> &Params, const std::string &Name = "");
197
198   virtual Instruction *clone() const { return new InvokeInst(*this); }
199
200   bool hasSideEffects() const { return true; }
201
202   // getCalledFunction - Return the function called, or null if this is an
203   // indirect function invocation...
204   //
205   inline const Function *getCalledFunction() const {
206     return dyn_cast<Function>(Operands[0].get());
207   }
208   inline Function *getCalledFunction() {
209     return dyn_cast<Function>(Operands[0].get());
210   }
211
212   // getCalledValue - Get a pointer to a method that is invoked by this inst.
213   inline const Value *getCalledValue() const { return Operands[0]; }
214   inline       Value *getCalledValue()       { return Operands[0]; }
215
216   // get*Dest - Return the destination basic blocks...
217   inline const BasicBlock *getNormalDest() const {
218     return cast<BasicBlock>(Operands[1].get());
219   }
220   inline       BasicBlock *getNormalDest() {
221     return cast<BasicBlock>(Operands[1].get());
222   }
223   inline const BasicBlock *getExceptionalDest() const {
224     return cast<BasicBlock>(Operands[2].get());
225   }
226   inline       BasicBlock *getExceptionalDest() {
227     return cast<BasicBlock>(Operands[2].get());
228   }
229
230   inline void setNormalDest(BasicBlock *B){
231     Operands[1] = (Value*)B;
232   }
233
234   inline void setExceptionalDest(BasicBlock *B){
235     Operands[2] = (Value*)B;
236   }
237
238   virtual const BasicBlock *getSuccessor(unsigned i) const {
239     assert(i < 2 && "Successor # out of range for invoke!");
240     return i == 0 ? getNormalDest() : getExceptionalDest();
241   }
242   inline BasicBlock *getSuccessor(unsigned i) {
243     assert(i < 2 && "Successor # out of range for invoke!");
244     return i == 0 ? getNormalDest() : getExceptionalDest();
245   }
246
247   virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
248     assert(idx < 2 && "Successor # out of range for invoke!");
249     Operands[idx+1] = (Value*)NewSucc;
250   }
251
252   virtual unsigned getNumSuccessors() const { return 2; }
253
254   // Methods for support type inquiry through isa, cast, and dyn_cast:
255   static inline bool classof(const InvokeInst *) { return true; }
256   static inline bool classof(const Instruction *I) {
257     return (I->getOpcode() == Instruction::Invoke);
258   }
259   static inline bool classof(const Value *V) {
260     return isa<Instruction>(V) && classof(cast<Instruction>(V));
261   }
262 };
263
264 #endif