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"
23 //===---------------------------------------------------------------------------
24 /// ReturnInst - Return a value (possibly void), from a function. Execution
25 /// does not continue in this function any longer.
27 class ReturnInst : public TerminatorInst {
28 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
29 if (RI.Operands.size()) {
30 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
32 Operands.push_back(Use(RI.Operands[0], this));
36 void init(Value *RetVal) {
39 Operands.push_back(Use(RetVal, this));
44 // ReturnInst constructors:
45 // ReturnInst() - 'ret void' instruction
46 // ReturnInst(Value* X) - 'ret X' instruction
47 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
48 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
49 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
50 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
51 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
52 : TerminatorInst(Instruction::Ret, InsertBefore) {
55 ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
56 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
60 virtual Instruction *clone() const { return new ReturnInst(*this); }
62 inline const Value *getReturnValue() const {
63 return Operands.size() ? Operands[0].get() : 0;
65 inline Value *getReturnValue() {
66 return Operands.size() ? Operands[0].get() : 0;
69 virtual const BasicBlock *getSuccessor(unsigned idx) const {
70 assert(0 && "ReturnInst has no successors!");
74 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
75 virtual unsigned getNumSuccessors() const { return 0; }
77 // Methods for support type inquiry through isa, cast, and dyn_cast:
78 static inline bool classof(const ReturnInst *) { return true; }
79 static inline bool classof(const Instruction *I) {
80 return (I->getOpcode() == Instruction::Ret);
82 static inline bool classof(const Value *V) {
83 return isa<Instruction>(V) && classof(cast<Instruction>(V));
87 //===---------------------------------------------------------------------------
88 /// BranchInst - Conditional or Unconditional Branch instruction.
90 class BranchInst : public TerminatorInst {
91 BranchInst(const BranchInst &BI);
92 void init(BasicBlock *IfTrue);
93 void init(BasicBlock *True, BasicBlock *False, Value *Cond);
95 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
96 // BranchInst(BB *B) - 'br B'
97 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
98 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
99 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
100 // BranchInst(BB* B, BB *I) - 'br B' insert at end
101 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
102 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
103 : TerminatorInst(Instruction::Br, InsertBefore) {
106 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
107 Instruction *InsertBefore = 0)
108 : TerminatorInst(Instruction::Br, InsertBefore) {
109 init(IfTrue, IfFalse, Cond);
112 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
113 : TerminatorInst(Instruction::Br, InsertAtEnd) {
117 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
118 BasicBlock *InsertAtEnd)
119 : TerminatorInst(Instruction::Br, InsertAtEnd) {
120 init(IfTrue, IfFalse, Cond);
123 virtual Instruction *clone() const { return new BranchInst(*this); }
125 inline bool isUnconditional() const { return Operands.size() == 1; }
126 inline bool isConditional() const { return Operands.size() == 3; }
128 inline Value *getCondition() const {
129 return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
132 void setCondition(Value *V) {
133 assert(isConditional() && "Cannot set condition of unconditional branch!");
137 // setUnconditionalDest - Change the current branch to an unconditional branch
138 // targeting the specified block.
140 void setUnconditionalDest(BasicBlock *Dest) {
141 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
142 Operands[0] = reinterpret_cast<Value*>(Dest);
145 virtual const BasicBlock *getSuccessor(unsigned i) const {
146 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
147 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
148 cast<BasicBlock>(Operands[1].get());
150 inline BasicBlock *getSuccessor(unsigned idx) {
151 const BranchInst *BI = this;
152 return const_cast<BasicBlock*>(BI->getSuccessor(idx));
155 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
156 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
157 Operands[idx] = reinterpret_cast<Value*>(NewSucc);
160 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
162 // Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const BranchInst *) { return true; }
164 static inline bool classof(const Instruction *I) {
165 return (I->getOpcode() == Instruction::Br);
167 static inline bool classof(const Value *V) {
168 return isa<Instruction>(V) && classof(cast<Instruction>(V));
173 //===---------------------------------------------------------------------------
174 /// SwitchInst - Multiway switch
176 class SwitchInst : public TerminatorInst {
177 // Operand[0] = Value to switch on
178 // Operand[1] = Default basic block destination
179 // Operand[2n ] = Value to match
180 // Operand[2n+1] = BasicBlock to go to on match
181 SwitchInst(const SwitchInst &RI);
182 void init(Value *Value, BasicBlock *Default);
185 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0)
186 : TerminatorInst(Instruction::Switch, InsertBefore) {
187 init(Value, Default);
189 SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd)
190 : TerminatorInst(Instruction::Switch, InsertAtEnd) {
191 init(Value, Default);
194 virtual Instruction *clone() const { return new SwitchInst(*this); }
196 // Accessor Methods for Switch stmt
198 inline const Value *getCondition() const { return Operands[0]; }
199 inline Value *getCondition() { return Operands[0]; }
200 inline const BasicBlock *getDefaultDest() const {
201 return cast<BasicBlock>(Operands[1].get());
203 inline BasicBlock *getDefaultDest() {
204 return cast<BasicBlock>(Operands[1].get());
207 /// getNumCases - return the number of 'cases' in this switch instruction.
208 /// Note that case #0 is always the default case.
209 unsigned getNumCases() const {
210 return Operands.size()/2;
213 /// getCaseValue - Return the specified case value. Note that case #0, the
214 /// default destination, does not have a case value.
215 Constant *getCaseValue(unsigned i) {
216 assert(i && i < getNumCases() && "Illegal case value to get!");
217 return getSuccessorValue(i);
220 /// getCaseValue - Return the specified case value. Note that case #0, the
221 /// default destination, does not have a case value.
222 const Constant *getCaseValue(unsigned i) const {
223 assert(i && i < getNumCases() && "Illegal case value to get!");
224 return getSuccessorValue(i);
227 /// findCaseValue - Search all of the case values for the specified constant.
228 /// If it is explicitly handled, return the case number of it, otherwise
229 /// return 0 to indicate that it is handled by the default handler.
230 unsigned findCaseValue(const Constant *C) const {
231 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
232 if (getCaseValue(i) == C)
237 /// addCase - Add an entry to the switch instruction...
239 void addCase(Constant *OnVal, BasicBlock *Dest);
241 /// removeCase - This method removes the specified successor from the switch
242 /// instruction. Note that this cannot be used to remove the default
243 /// destination (successor #0).
245 void removeCase(unsigned idx);
247 virtual const BasicBlock *getSuccessor(unsigned idx) const {
248 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
249 return cast<BasicBlock>(Operands[idx*2+1].get());
251 inline BasicBlock *getSuccessor(unsigned idx) {
252 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
253 return cast<BasicBlock>(Operands[idx*2+1].get());
256 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
257 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
258 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
261 // getSuccessorValue - Return the value associated with the specified
263 inline const Constant *getSuccessorValue(unsigned idx) const {
264 assert(idx < getNumSuccessors() && "Successor # out of range!");
265 return cast<Constant>(Operands[idx*2].get());
267 inline Constant *getSuccessorValue(unsigned idx) {
268 assert(idx < getNumSuccessors() && "Successor # out of range!");
269 return cast<Constant>(Operands[idx*2].get());
271 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const SwitchInst *) { return true; }
275 static inline bool classof(const Instruction *I) {
276 return (I->getOpcode() == Instruction::Switch);
278 static inline bool classof(const Value *V) {
279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
283 //===---------------------------------------------------------------------------
284 /// InvokeInst - Invoke instruction
286 class InvokeInst : public TerminatorInst {
287 InvokeInst(const InvokeInst &BI);
288 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
289 const std::vector<Value*> &Params);
291 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
292 const std::vector<Value*> &Params, const std::string &Name = "",
293 Instruction *InsertBefore = 0);
294 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
295 const std::vector<Value*> &Params, const std::string &Name,
296 BasicBlock *InsertAtEnd);
298 virtual Instruction *clone() const { return new InvokeInst(*this); }
300 bool mayWriteToMemory() const { return true; }
302 /// getCalledFunction - Return the function called, or null if this is an
303 /// indirect function invocation...
305 /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
307 const Function *getCalledFunction() const;
308 Function *getCalledFunction();
310 // getCalledValue - Get a pointer to a function that is invoked by this inst.
311 inline const Value *getCalledValue() const { return Operands[0]; }
312 inline Value *getCalledValue() { return Operands[0]; }
314 // get*Dest - Return the destination basic blocks...
315 inline const BasicBlock *getNormalDest() const {
316 return cast<BasicBlock>(Operands[1].get());
318 inline BasicBlock *getNormalDest() {
319 return cast<BasicBlock>(Operands[1].get());
321 inline const BasicBlock *getUnwindDest() const {
322 return cast<BasicBlock>(Operands[2].get());
324 inline BasicBlock *getUnwindDest() {
325 return cast<BasicBlock>(Operands[2].get());
328 inline void setNormalDest(BasicBlock *B){
329 Operands[1] = reinterpret_cast<Value*>(B);
332 inline void setUnwindDest(BasicBlock *B){
333 Operands[2] = reinterpret_cast<Value*>(B);
336 virtual const BasicBlock *getSuccessor(unsigned i) const {
337 assert(i < 2 && "Successor # out of range for invoke!");
338 return i == 0 ? getNormalDest() : getUnwindDest();
340 inline BasicBlock *getSuccessor(unsigned i) {
341 assert(i < 2 && "Successor # out of range for invoke!");
342 return i == 0 ? getNormalDest() : getUnwindDest();
345 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
346 assert(idx < 2 && "Successor # out of range for invoke!");
347 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
350 virtual unsigned getNumSuccessors() const { return 2; }
352 // Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const InvokeInst *) { return true; }
354 static inline bool classof(const Instruction *I) {
355 return (I->getOpcode() == Instruction::Invoke);
357 static inline bool classof(const Value *V) {
358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 //===---------------------------------------------------------------------------
364 /// UnwindInst - Immediately exit the current function, unwinding the stack
365 /// until an invoke instruction is found.
367 struct UnwindInst : public TerminatorInst {
368 UnwindInst(Instruction *InsertBefore = 0)
369 : TerminatorInst(Instruction::Unwind, InsertBefore) {
371 UnwindInst(BasicBlock *InsertAtEnd)
372 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
375 virtual Instruction *clone() const { return new UnwindInst(); }
377 virtual const BasicBlock *getSuccessor(unsigned idx) const {
378 assert(0 && "UnwindInst has no successors!");
382 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
383 virtual unsigned getNumSuccessors() const { return 0; }
385 // Methods for support type inquiry through isa, cast, and dyn_cast:
386 static inline bool classof(const UnwindInst *) { return true; }
387 static inline bool classof(const Instruction *I) {
388 return I->getOpcode() == Instruction::Unwind;
390 static inline bool classof(const Value *V) {
391 return isa<Instruction>(V) && classof(cast<Instruction>(V));
395 } // End llvm namespace