1 //===-- llvm/iTerminators.h - Termintator instruction nodes ------*- C++ -*--=//
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.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ITERMINATORS_H
11 #define LLVM_ITERMINATORS_H
13 #include "llvm/InstrTypes.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/ConstantVals.h"
17 //===----------------------------------------------------------------------===//
18 // Classes to represent Basic Block "Terminator" instructions
19 //===----------------------------------------------------------------------===//
22 //===---------------------------------------------------------------------------
23 // ReturnInst - Return a value (possibly void), from a method. Execution does
24 // not continue in this method any longer.
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!");
31 Operands.push_back(Use(RI.Operands[0], this));
35 ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
38 Operands.push_back(Use(RetVal, this));
42 virtual Instruction *clone() const { return new ReturnInst(*this); }
44 virtual const char *getOpcodeName() const { return "ret"; }
46 inline const Value *getReturnValue() const {
47 return Operands.size() ? Operands[0].get() : 0;
49 inline Value *getReturnValue() {
50 return Operands.size() ? Operands[0].get() : 0;
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
57 virtual const BasicBlock *getSuccessor(unsigned idx) const { return 0; }
58 virtual unsigned getNumSuccessors() const { return 0; }
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);
65 static inline bool classof(const Value *V) {
66 return isa<Instruction>(V) && classof(cast<Instruction>(V));
71 //===---------------------------------------------------------------------------
72 // BranchInst - Conditional or Unconditional Branch instruction.
74 class BranchInst : public TerminatorInst {
75 BranchInst(const BranchInst &BI);
77 // If cond = null, then is an unconditional br...
78 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
80 virtual Instruction *clone() const { return new BranchInst(*this); }
82 inline bool isUnconditional() const {
83 return Operands.size() == 1;
86 inline const Value *getCondition() const {
87 return isUnconditional() ? 0 : Operands[2].get();
89 inline Value *getCondition() {
90 return isUnconditional() ? 0 : Operands[2].get();
93 virtual const char *getOpcodeName() const { return "br"; }
95 // setUnconditionalDest - Change the current branch to an unconditional branch
96 // targeting the specified block.
98 void setUnconditionalDest(BasicBlock *Dest) {
99 if (Operands.size() == 3)
100 Operands.erase(Operands.begin()+1, Operands.end());
104 // Additionally, they must provide a method to get at the successors of this
105 // terminator instruction.
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);
112 inline BasicBlock *getSuccessor(unsigned idx) {
113 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
116 virtual unsigned getNumSuccessors() const { return 1+!isUnconditional(); }
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);
123 static inline bool classof(const Value *V) {
124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
129 //===---------------------------------------------------------------------------
130 // SwitchInst - Multiway switch
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);
139 SwitchInst(Value *Value, BasicBlock *Default);
141 virtual Instruction *clone() const { return new SwitchInst(*this); }
143 // Accessor Methods for Switch stmt
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]);
150 inline BasicBlock *getDefaultDest() {
151 return cast<BasicBlock>(Operands[1]);
154 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
156 virtual const char *getOpcodeName() const { return "switch"; }
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
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]);
166 inline BasicBlock *getSuccessor(unsigned idx) {
167 if (idx >= Operands.size()/2) return 0;
168 return cast<BasicBlock>(Operands[idx*2+1]);
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]);
177 inline Constant *getSuccessorValue(unsigned idx) {
178 assert(idx < getNumSuccessors() && "Successor # out of range!");
179 return cast<Constant>(Operands[idx*2]);
181 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
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);
188 static inline bool classof(const Value *V) {
189 return isa<Instruction>(V) && classof(cast<Instruction>(V));
194 //===---------------------------------------------------------------------------
195 // InvokeInst - Invoke instruction
197 class InvokeInst : public TerminatorInst {
198 InvokeInst(const InvokeInst &BI);
200 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
201 const std::vector<Value*> &Params, const std::string &Name = "");
203 virtual Instruction *clone() const { return new InvokeInst(*this); }
205 // getCalledMethod - Return the method called, or null if this is an indirect
206 // method invocation...
208 inline const Method *getCalledMethod() const {
209 return dyn_cast<Method>(Operands[0].get());
211 inline Method *getCalledMethod() {
212 return dyn_cast<Method>(Operands[0].get());
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]; }
219 // get*Dest - Return the destination basic blocks...
220 inline const BasicBlock *getNormalDest() const {
221 return cast<BasicBlock>(Operands[1]);
223 inline BasicBlock *getNormalDest() {
224 return cast<BasicBlock>(Operands[1]);
226 inline const BasicBlock *getExceptionalDest() const {
227 return cast<BasicBlock>(Operands[2]);
229 inline BasicBlock *getExceptionalDest() {
230 return cast<BasicBlock>(Operands[2]);
233 virtual const char *getOpcodeName() const { return "invoke"; }
235 // Additionally, they must provide a method to get at the successors of this
236 // terminator instruction.
238 virtual const BasicBlock *getSuccessor(unsigned i) const {
239 return (i == 0) ? getNormalDest() :
240 ((i == 1) ? getExceptionalDest() : 0);
242 inline BasicBlock *getSuccessor(unsigned i) {
243 return (i == 0) ? getNormalDest() :
244 ((i == 1) ? getExceptionalDest() : 0);
247 virtual unsigned getNumSuccessors() const { return 2; }
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);
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));