1 //===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations for all the subclasses of the Instruction
11 // class which represent "terminator" instructions. Terminator instructions are
12 // the only instructions allowed and required to terminate a BasicBlock.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ITERMINATORS_H
17 #define LLVM_ITERMINATORS_H
19 #include "llvm/InstrTypes.h"
21 //===---------------------------------------------------------------------------
22 // ReturnInst - Return a value (possibly void), from a function. Execution does
23 // not continue in this function any longer.
25 class ReturnInst : public TerminatorInst {
26 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
27 if (RI.Operands.size()) {
28 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
30 Operands.push_back(Use(RI.Operands[0], this));
34 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
35 : TerminatorInst(Instruction::Ret, InsertBefore) {
38 Operands.push_back(Use(RetVal, this));
42 virtual Instruction *clone() const { return new ReturnInst(*this); }
44 inline const Value *getReturnValue() const {
45 return Operands.size() ? Operands[0].get() : 0;
47 inline Value *getReturnValue() {
48 return Operands.size() ? Operands[0].get() : 0;
51 virtual const BasicBlock *getSuccessor(unsigned idx) const {
52 assert(0 && "ReturnInst has no successors!");
56 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
57 assert(0 && "ReturnInst has no successors!");
59 virtual unsigned getNumSuccessors() const { return 0; }
61 // Methods for support type inquiry through isa, cast, and dyn_cast:
62 static inline bool classof(const ReturnInst *) { return true; }
63 static inline bool classof(const Instruction *I) {
64 return (I->getOpcode() == Instruction::Ret);
66 static inline bool classof(const Value *V) {
67 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, Value *cond = 0,
79 Instruction *InsertBefore = 0);
80 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
82 virtual Instruction *clone() const { return new BranchInst(*this); }
84 inline bool isUnconditional() const { return Operands.size() == 1; }
85 inline bool isConditional() const { return Operands.size() == 3; }
87 inline Value *getCondition() const {
88 return isUnconditional() ? 0 : (Value*)Operands[2].get();
91 void setCondition(Value *V) {
92 assert(isConditional() && "Cannot set condition of unconditional branch!");
96 // setUnconditionalDest - Change the current branch to an unconditional branch
97 // targeting the specified block.
99 void setUnconditionalDest(BasicBlock *Dest) {
100 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
101 Operands[0] = (Value*)Dest;
104 virtual const BasicBlock *getSuccessor(unsigned i) const {
105 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
106 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
107 cast<BasicBlock>(Operands[1].get());
109 inline BasicBlock *getSuccessor(unsigned idx) {
110 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
113 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
114 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
115 Operands[idx] = (Value*)NewSucc;
118 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
120 // Methods for support type inquiry through isa, cast, and dyn_cast:
121 static inline bool classof(const BranchInst *) { return true; }
122 static inline bool classof(const Instruction *I) {
123 return (I->getOpcode() == Instruction::Br);
125 static inline bool classof(const Value *V) {
126 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 //===---------------------------------------------------------------------------
132 // SwitchInst - Multiway switch
134 class SwitchInst : public TerminatorInst {
135 // Operand[0] = Value to switch on
136 // Operand[1] = Default basic block destination
137 // Operand[2n ] = Value to match
138 // Operand[2n+1] = BasicBlock to go to on match
139 SwitchInst(const SwitchInst &RI);
141 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
143 virtual Instruction *clone() const { return new SwitchInst(*this); }
145 // Accessor Methods for Switch stmt
147 inline const Value *getCondition() const { return Operands[0]; }
148 inline Value *getCondition() { return Operands[0]; }
149 inline const BasicBlock *getDefaultDest() const {
150 return cast<BasicBlock>(Operands[1].get());
152 inline BasicBlock *getDefaultDest() {
153 return cast<BasicBlock>(Operands[1].get());
156 /// addCase - Add an entry to the switch instruction...
158 void addCase(Constant *OnVal, BasicBlock *Dest);
160 /// removeCase - This method removes the specified successor from the switch
161 /// instruction. Note that this cannot be used to remove the default
162 /// destination (successor #0).
164 void removeCase(unsigned idx);
166 virtual const BasicBlock *getSuccessor(unsigned idx) const {
167 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
168 return cast<BasicBlock>(Operands[idx*2+1].get());
170 inline BasicBlock *getSuccessor(unsigned idx) {
171 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
172 return cast<BasicBlock>(Operands[idx*2+1].get());
175 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
176 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
177 Operands[idx*2+1] = (Value*)NewSucc;
180 // getSuccessorValue - Return the value associated with the specified
182 inline const Constant *getSuccessorValue(unsigned idx) const {
183 assert(idx < getNumSuccessors() && "Successor # out of range!");
184 return cast<Constant>(Operands[idx*2].get());
186 inline Constant *getSuccessorValue(unsigned idx) {
187 assert(idx < getNumSuccessors() && "Successor # out of range!");
188 return cast<Constant>(Operands[idx*2].get());
190 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
192 // Methods for support type inquiry through isa, cast, and dyn_cast:
193 static inline bool classof(const SwitchInst *) { return true; }
194 static inline bool classof(const Instruction *I) {
195 return (I->getOpcode() == Instruction::Switch);
197 static inline bool classof(const Value *V) {
198 return isa<Instruction>(V) && classof(cast<Instruction>(V));
203 //===---------------------------------------------------------------------------
204 // InvokeInst - Invoke instruction
206 class InvokeInst : public TerminatorInst {
207 InvokeInst(const InvokeInst &BI);
209 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
210 const std::vector<Value*> &Params, const std::string &Name = "",
211 Instruction *InsertBefore = 0);
213 virtual Instruction *clone() const { return new InvokeInst(*this); }
215 bool mayWriteToMemory() const { return true; }
217 // getCalledFunction - Return the function called, or null if this is an
218 // indirect function invocation...
220 inline const Function *getCalledFunction() const {
221 return dyn_cast<Function>(Operands[0].get());
223 inline Function *getCalledFunction() {
224 return dyn_cast<Function>(Operands[0].get());
227 // getCalledValue - Get a pointer to a function that is invoked by this inst.
228 inline const Value *getCalledValue() const { return Operands[0]; }
229 inline Value *getCalledValue() { return Operands[0]; }
231 // get*Dest - Return the destination basic blocks...
232 inline const BasicBlock *getNormalDest() const {
233 return cast<BasicBlock>(Operands[1].get());
235 inline BasicBlock *getNormalDest() {
236 return cast<BasicBlock>(Operands[1].get());
238 inline const BasicBlock *getExceptionalDest() const {
239 return cast<BasicBlock>(Operands[2].get());
241 inline BasicBlock *getExceptionalDest() {
242 return cast<BasicBlock>(Operands[2].get());
245 inline void setNormalDest(BasicBlock *B){
246 Operands[1] = (Value*)B;
249 inline void setExceptionalDest(BasicBlock *B){
250 Operands[2] = (Value*)B;
253 virtual const BasicBlock *getSuccessor(unsigned i) const {
254 assert(i < 2 && "Successor # out of range for invoke!");
255 return i == 0 ? getNormalDest() : getExceptionalDest();
257 inline BasicBlock *getSuccessor(unsigned i) {
258 assert(i < 2 && "Successor # out of range for invoke!");
259 return i == 0 ? getNormalDest() : getExceptionalDest();
262 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
263 assert(idx < 2 && "Successor # out of range for invoke!");
264 Operands[idx+1] = (Value*)NewSucc;
267 virtual unsigned getNumSuccessors() const { return 2; }
269 // Methods for support type inquiry through isa, cast, and dyn_cast:
270 static inline bool classof(const InvokeInst *) { return true; }
271 static inline bool classof(const Instruction *I) {
272 return (I->getOpcode() == Instruction::Invoke);
274 static inline bool classof(const Value *V) {
275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
280 //===---------------------------------------------------------------------------
281 /// UnwindInst - Immediately exit the current function, unwinding the stack
282 /// until an invoke instruction is found.
284 struct UnwindInst : public TerminatorInst {
285 UnwindInst(Instruction *InsertBefore = 0)
286 : TerminatorInst(Instruction::Unwind, InsertBefore) {
289 virtual Instruction *clone() const { return new UnwindInst(); }
291 virtual const BasicBlock *getSuccessor(unsigned idx) const {
292 assert(0 && "UnwindInst has no successors!");
296 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
297 assert(0 && "UnwindInst has no successors!");
299 virtual unsigned getNumSuccessors() const { return 0; }
301 // Methods for support type inquiry through isa, cast, and dyn_cast:
302 static inline bool classof(const UnwindInst *) { return true; }
303 static inline bool classof(const Instruction *I) {
304 return I->getOpcode() == Instruction::Unwind;
306 static inline bool classof(const Value *V) {
307 return isa<Instruction>(V) && classof(cast<Instruction>(V));