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"
15 //===----------------------------------------------------------------------===//
16 // Classes to represent Basic Block "Terminator" instructions
17 //===----------------------------------------------------------------------===//
20 //===---------------------------------------------------------------------------
21 // ReturnInst - Return a value (possibly void), from a method. Execution does
22 // not continue in this method any longer.
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!");
29 Operands.push_back(Use(RI.Operands[0], this));
33 ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
36 Operands.push_back(Use(RetVal, this));
40 virtual Instruction *clone() const { return new ReturnInst(*this); }
42 inline const Value *getReturnValue() const {
43 return Operands.size() ? Operands[0].get() : 0;
45 inline Value *getReturnValue() {
46 return Operands.size() ? Operands[0].get() : 0;
49 virtual const BasicBlock *getSuccessor(unsigned idx) const {
50 assert(0 && "ReturnInst has no successors!");
53 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
54 assert(0 && "ReturnInst has no successors!");
56 virtual unsigned getNumSuccessors() const { return 0; }
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);
63 static inline bool classof(const Value *V) {
64 return isa<Instruction>(V) && classof(cast<Instruction>(V));
69 //===---------------------------------------------------------------------------
70 // BranchInst - Conditional or Unconditional Branch instruction.
72 class BranchInst : public TerminatorInst {
73 BranchInst(const BranchInst &BI);
75 // If cond = null, then is an unconditional br...
76 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
78 virtual Instruction *clone() const { return new BranchInst(*this); }
80 inline bool isUnconditional() const { return Operands.size() == 1; }
81 inline bool isConditional() const { return Operands.size() == 3; }
83 inline const Value *getCondition() const {
84 return isUnconditional() ? 0 : Operands[2].get();
86 inline Value *getCondition() {
87 return isUnconditional() ? 0 : Operands[2].get();
90 // setUnconditionalDest - Change the current branch to an unconditional branch
91 // targeting the specified block.
93 void setUnconditionalDest(BasicBlock *Dest) {
94 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
95 Operands[0] = (Value*)Dest;
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());
103 inline BasicBlock *getSuccessor(unsigned idx) {
104 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
107 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
108 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
109 Operands[idx] = (Value*)NewSucc;
112 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
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);
119 static inline bool classof(const Value *V) {
120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
125 //===---------------------------------------------------------------------------
126 // SwitchInst - Multiway switch
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);
135 SwitchInst(Value *Value, BasicBlock *Default);
137 virtual Instruction *clone() const { return new SwitchInst(*this); }
139 // Accessor Methods for Switch stmt
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());
146 inline BasicBlock *getDefaultDest() {
147 return cast<BasicBlock>(Operands[1].get());
150 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
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());
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());
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;
166 // getSuccessorValue - Return the value associated with the specified
168 inline const Constant *getSuccessorValue(unsigned idx) const {
169 assert(idx < getNumSuccessors() && "Successor # out of range!");
170 return cast<Constant>(Operands[idx*2].get());
172 inline Constant *getSuccessorValue(unsigned idx) {
173 assert(idx < getNumSuccessors() && "Successor # out of range!");
174 return cast<Constant>(Operands[idx*2].get());
176 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
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);
183 static inline bool classof(const Value *V) {
184 return isa<Instruction>(V) && classof(cast<Instruction>(V));
189 //===---------------------------------------------------------------------------
190 // InvokeInst - Invoke instruction
192 class InvokeInst : public TerminatorInst {
193 InvokeInst(const InvokeInst &BI);
195 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
196 const std::vector<Value*> &Params, const std::string &Name = "");
198 virtual Instruction *clone() const { return new InvokeInst(*this); }
200 bool hasSideEffects() const { return true; }
202 // getCalledFunction - Return the function called, or null if this is an
203 // indirect function invocation...
205 inline const Function *getCalledFunction() const {
206 return dyn_cast<Function>(Operands[0].get());
208 inline Function *getCalledFunction() {
209 return dyn_cast<Function>(Operands[0].get());
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]; }
216 // get*Dest - Return the destination basic blocks...
217 inline const BasicBlock *getNormalDest() const {
218 return cast<BasicBlock>(Operands[1].get());
220 inline BasicBlock *getNormalDest() {
221 return cast<BasicBlock>(Operands[1].get());
223 inline const BasicBlock *getExceptionalDest() const {
224 return cast<BasicBlock>(Operands[2].get());
226 inline BasicBlock *getExceptionalDest() {
227 return cast<BasicBlock>(Operands[2].get());
230 inline void setNormalDest(BasicBlock *B){
231 Operands[1] = (Value*)B;
234 inline void setExceptionalDest(BasicBlock *B){
235 Operands[2] = (Value*)B;
238 virtual const BasicBlock *getSuccessor(unsigned i) const {
239 assert(i < 2 && "Successor # out of range for invoke!");
240 return i == 0 ? getNormalDest() : getExceptionalDest();
242 inline BasicBlock *getSuccessor(unsigned i) {
243 assert(i < 2 && "Successor # out of range for invoke!");
244 return i == 0 ? getNormalDest() : getExceptionalDest();
247 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
248 assert(idx < 2 && "Successor # out of range for invoke!");
249 Operands[idx+1] = (Value*)NewSucc;
252 virtual unsigned getNumSuccessors() const { return 2; }
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);
259 static inline bool classof(const Value *V) {
260 return isa<Instruction>(V) && classof(cast<Instruction>(V));