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!");
54 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
55 assert(0 && "ReturnInst has no successors!");
57 virtual unsigned getNumSuccessors() const { return 0; }
59 // Methods for support type inquiry through isa, cast, and dyn_cast:
60 static inline bool classof(const ReturnInst *) { return true; }
61 static inline bool classof(const Instruction *I) {
62 return (I->getOpcode() == Instruction::Ret);
64 static inline bool classof(const Value *V) {
65 return isa<Instruction>(V) && classof(cast<Instruction>(V));
70 //===---------------------------------------------------------------------------
71 // BranchInst - Conditional or Unconditional Branch instruction.
73 class BranchInst : public TerminatorInst {
74 BranchInst(const BranchInst &BI);
76 // If cond = null, then is an unconditional br...
77 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
79 virtual Instruction *clone() const { return new BranchInst(*this); }
81 inline bool isUnconditional() const { return Operands.size() == 1; }
82 inline bool isConditional() const { return Operands.size() == 3; }
84 inline const Value *getCondition() const {
85 return isUnconditional() ? 0 : Operands[2].get();
87 inline Value *getCondition() {
88 return isUnconditional() ? 0 : Operands[2].get();
91 // setUnconditionalDest - Change the current branch to an unconditional branch
92 // targeting the specified block.
94 void setUnconditionalDest(BasicBlock *Dest) {
95 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
96 Operands[0] = (Value*)Dest;
99 virtual const BasicBlock *getSuccessor(unsigned i) const {
100 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
101 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
102 cast<BasicBlock>(Operands[1].get());
104 inline BasicBlock *getSuccessor(unsigned idx) {
105 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
108 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
109 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
110 Operands[idx] = (Value*)NewSucc;
113 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
115 // Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const BranchInst *) { return true; }
117 static inline bool classof(const Instruction *I) {
118 return (I->getOpcode() == Instruction::Br);
120 static inline bool classof(const Value *V) {
121 return isa<Instruction>(V) && classof(cast<Instruction>(V));
126 //===---------------------------------------------------------------------------
127 // SwitchInst - Multiway switch
129 class SwitchInst : public TerminatorInst {
130 // Operand[0] = Value to switch on
131 // Operand[1] = Default basic block destination
132 // Operand[2n ] = Value to match
133 // Operand[2n+1] = BasicBlock to go to on match
134 SwitchInst(const SwitchInst &RI);
136 SwitchInst(Value *Value, BasicBlock *Default);
138 virtual Instruction *clone() const { return new SwitchInst(*this); }
140 // Accessor Methods for Switch stmt
142 inline const Value *getCondition() const { return Operands[0]; }
143 inline Value *getCondition() { return Operands[0]; }
144 inline const BasicBlock *getDefaultDest() const {
145 return cast<BasicBlock>(Operands[1].get());
147 inline BasicBlock *getDefaultDest() {
148 return cast<BasicBlock>(Operands[1].get());
151 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
153 virtual const BasicBlock *getSuccessor(unsigned idx) const {
154 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
155 return cast<BasicBlock>(Operands[idx*2+1].get());
157 inline BasicBlock *getSuccessor(unsigned idx) {
158 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
159 return cast<BasicBlock>(Operands[idx*2+1].get());
162 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
163 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
164 Operands[idx*2+1] = (Value*)NewSucc;
167 // getSuccessorValue - Return the value associated with the specified
169 inline const Constant *getSuccessorValue(unsigned idx) const {
170 assert(idx < getNumSuccessors() && "Successor # out of range!");
171 return cast<Constant>(Operands[idx*2].get());
173 inline Constant *getSuccessorValue(unsigned idx) {
174 assert(idx < getNumSuccessors() && "Successor # out of range!");
175 return cast<Constant>(Operands[idx*2].get());
177 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
179 // Methods for support type inquiry through isa, cast, and dyn_cast:
180 static inline bool classof(const SwitchInst *) { return true; }
181 static inline bool classof(const Instruction *I) {
182 return (I->getOpcode() == Instruction::Switch);
184 static inline bool classof(const Value *V) {
185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
190 //===---------------------------------------------------------------------------
191 // InvokeInst - Invoke instruction
193 class InvokeInst : public TerminatorInst {
194 InvokeInst(const InvokeInst &BI);
196 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
197 const std::vector<Value*> &Params, const std::string &Name = "");
199 virtual Instruction *clone() const { return new InvokeInst(*this); }
201 bool hasSideEffects() const { return true; }
203 // getCalledFunction - Return the function called, or null if this is an
204 // indirect function invocation...
206 inline const Function *getCalledFunction() const {
207 return dyn_cast<Function>(Operands[0].get());
209 inline Function *getCalledFunction() {
210 return dyn_cast<Function>(Operands[0].get());
213 // getCalledValue - Get a pointer to a method that is invoked by this inst.
214 inline const Value *getCalledValue() const { return Operands[0]; }
215 inline Value *getCalledValue() { return Operands[0]; }
217 // get*Dest - Return the destination basic blocks...
218 inline const BasicBlock *getNormalDest() const {
219 return cast<BasicBlock>(Operands[1].get());
221 inline BasicBlock *getNormalDest() {
222 return cast<BasicBlock>(Operands[1].get());
224 inline const BasicBlock *getExceptionalDest() const {
225 return cast<BasicBlock>(Operands[2].get());
227 inline BasicBlock *getExceptionalDest() {
228 return cast<BasicBlock>(Operands[2].get());
231 inline void setNormalDest(BasicBlock *B){
232 Operands[1] = (Value*)B;
235 inline void setExceptionalDest(BasicBlock *B){
236 Operands[2] = (Value*)B;
239 virtual const BasicBlock *getSuccessor(unsigned i) const {
240 assert(i < 2 && "Successor # out of range for invoke!");
241 return i == 0 ? getNormalDest() : getExceptionalDest();
243 inline BasicBlock *getSuccessor(unsigned i) {
244 assert(i < 2 && "Successor # out of range for invoke!");
245 return i == 0 ? getNormalDest() : getExceptionalDest();
248 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
249 assert(idx < 2 && "Successor # out of range for invoke!");
250 Operands[idx+1] = (Value*)NewSucc;
253 virtual unsigned getNumSuccessors() const { return 2; }
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const InvokeInst *) { return true; }
257 static inline bool classof(const Instruction *I) {
258 return (I->getOpcode() == Instruction::Invoke);
260 static inline bool classof(const Value *V) {
261 return isa<Instruction>(V) && classof(cast<Instruction>(V));