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 // ReturnInst constructors:
37 // ReturnInst() - 'ret void' instruction
38 // ReturnInst(Value* X) - 'ret X' instruction
39 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
40 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
41 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
42 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
43 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
44 : TerminatorInst(Instruction::Ret, InsertBefore) {
47 Operands.push_back(Use(RetVal, this));
50 ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
51 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
54 Operands.push_back(Use(RetVal, this));
58 virtual Instruction *clone() const { return new ReturnInst(*this); }
60 inline const Value *getReturnValue() const {
61 return Operands.size() ? Operands[0].get() : 0;
63 inline Value *getReturnValue() {
64 return Operands.size() ? Operands[0].get() : 0;
67 virtual const BasicBlock *getSuccessor(unsigned idx) const {
68 assert(0 && "ReturnInst has no successors!");
72 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
73 virtual unsigned getNumSuccessors() const { return 0; }
75 // Methods for support type inquiry through isa, cast, and dyn_cast:
76 static inline bool classof(const ReturnInst *) { return true; }
77 static inline bool classof(const Instruction *I) {
78 return (I->getOpcode() == Instruction::Ret);
80 static inline bool classof(const Value *V) {
81 return isa<Instruction>(V) && classof(cast<Instruction>(V));
85 //===---------------------------------------------------------------------------
86 /// BranchInst - Conditional or Unconditional Branch instruction.
88 class BranchInst : public TerminatorInst {
89 BranchInst(const BranchInst &BI);
91 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
92 // BranchInst(BB *B) - 'br B'
93 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
94 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
95 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
96 // BranchInst(BB* B, BB *I) - 'br B' insert at end
97 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
98 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
99 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
100 Instruction *InsertBefore = 0);
101 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
102 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
103 BasicBlock *InsertAtEnd);
105 virtual Instruction *clone() const { return new BranchInst(*this); }
107 inline bool isUnconditional() const { return Operands.size() == 1; }
108 inline bool isConditional() const { return Operands.size() == 3; }
110 inline Value *getCondition() const {
111 return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
114 void setCondition(Value *V) {
115 assert(isConditional() && "Cannot set condition of unconditional branch!");
119 // setUnconditionalDest - Change the current branch to an unconditional branch
120 // targeting the specified block.
122 void setUnconditionalDest(BasicBlock *Dest) {
123 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
124 Operands[0] = reinterpret_cast<Value*>(Dest);
127 virtual const BasicBlock *getSuccessor(unsigned i) const {
128 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
129 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
130 cast<BasicBlock>(Operands[1].get());
132 inline BasicBlock *getSuccessor(unsigned idx) {
133 const BranchInst *BI = this;
134 return const_cast<BasicBlock*>(BI->getSuccessor(idx));
137 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
138 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
139 Operands[idx] = reinterpret_cast<Value*>(NewSucc);
142 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
144 // Methods for support type inquiry through isa, cast, and dyn_cast:
145 static inline bool classof(const BranchInst *) { return true; }
146 static inline bool classof(const Instruction *I) {
147 return (I->getOpcode() == Instruction::Br);
149 static inline bool classof(const Value *V) {
150 return isa<Instruction>(V) && classof(cast<Instruction>(V));
155 //===---------------------------------------------------------------------------
156 /// SwitchInst - Multiway switch
158 class SwitchInst : public TerminatorInst {
159 // Operand[0] = Value to switch on
160 // Operand[1] = Default basic block destination
161 // Operand[2n ] = Value to match
162 // Operand[2n+1] = BasicBlock to go to on match
163 SwitchInst(const SwitchInst &RI);
165 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
166 SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd);
168 virtual Instruction *clone() const { return new SwitchInst(*this); }
170 // Accessor Methods for Switch stmt
172 inline const Value *getCondition() const { return Operands[0]; }
173 inline Value *getCondition() { return Operands[0]; }
174 inline const BasicBlock *getDefaultDest() const {
175 return cast<BasicBlock>(Operands[1].get());
177 inline BasicBlock *getDefaultDest() {
178 return cast<BasicBlock>(Operands[1].get());
181 /// getNumCases - return the number of 'cases' in this switch instruction.
182 /// Note that case #0 is always the default case.
183 unsigned getNumCases() const {
184 return Operands.size()/2;
187 /// getCaseValue - Return the specified case value. Note that case #0, the
188 /// default destination, does not have a case value.
189 Constant *getCaseValue(unsigned i) {
190 assert(i && i < getNumCases() && "Illegal case value to get!");
191 return getSuccessorValue(i);
194 /// getCaseValue - Return the specified case value. Note that case #0, the
195 /// default destination, does not have a case value.
196 const Constant *getCaseValue(unsigned i) const {
197 assert(i && i < getNumCases() && "Illegal case value to get!");
198 return getSuccessorValue(i);
201 /// findCaseValue - Search all of the case values for the specified constant.
202 /// If it is explicitly handled, return the case number of it, otherwise
203 /// return 0 to indicate that it is handled by the default handler.
204 unsigned findCaseValue(const Constant *C) const {
205 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
206 if (getCaseValue(i) == C)
211 /// addCase - Add an entry to the switch instruction...
213 void addCase(Constant *OnVal, BasicBlock *Dest);
215 /// removeCase - This method removes the specified successor from the switch
216 /// instruction. Note that this cannot be used to remove the default
217 /// destination (successor #0).
219 void removeCase(unsigned idx);
221 virtual const BasicBlock *getSuccessor(unsigned idx) const {
222 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
223 return cast<BasicBlock>(Operands[idx*2+1].get());
225 inline BasicBlock *getSuccessor(unsigned idx) {
226 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
227 return cast<BasicBlock>(Operands[idx*2+1].get());
230 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
231 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
232 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
235 // getSuccessorValue - Return the value associated with the specified
237 inline const Constant *getSuccessorValue(unsigned idx) const {
238 assert(idx < getNumSuccessors() && "Successor # out of range!");
239 return cast<Constant>(Operands[idx*2].get());
241 inline Constant *getSuccessorValue(unsigned idx) {
242 assert(idx < getNumSuccessors() && "Successor # out of range!");
243 return cast<Constant>(Operands[idx*2].get());
245 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const SwitchInst *) { return true; }
249 static inline bool classof(const Instruction *I) {
250 return (I->getOpcode() == Instruction::Switch);
252 static inline bool classof(const Value *V) {
253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
257 //===---------------------------------------------------------------------------
258 /// InvokeInst - Invoke instruction
260 class InvokeInst : public TerminatorInst {
261 InvokeInst(const InvokeInst &BI);
263 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
264 const std::vector<Value*> &Params, const std::string &Name = "",
265 Instruction *InsertBefore = 0);
266 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
267 const std::vector<Value*> &Params, const std::string &Name,
268 BasicBlock *InsertAtEnd);
270 virtual Instruction *clone() const { return new InvokeInst(*this); }
272 bool mayWriteToMemory() const { return true; }
274 /// getCalledFunction - Return the function called, or null if this is an
275 /// indirect function invocation...
277 /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
279 const Function *getCalledFunction() const;
280 Function *getCalledFunction();
282 // getCalledValue - Get a pointer to a function that is invoked by this inst.
283 inline const Value *getCalledValue() const { return Operands[0]; }
284 inline Value *getCalledValue() { return Operands[0]; }
286 // get*Dest - Return the destination basic blocks...
287 inline const BasicBlock *getNormalDest() const {
288 return cast<BasicBlock>(Operands[1].get());
290 inline BasicBlock *getNormalDest() {
291 return cast<BasicBlock>(Operands[1].get());
293 inline const BasicBlock *getUnwindDest() const {
294 return cast<BasicBlock>(Operands[2].get());
296 inline BasicBlock *getUnwindDest() {
297 return cast<BasicBlock>(Operands[2].get());
300 inline void setNormalDest(BasicBlock *B){
301 Operands[1] = reinterpret_cast<Value*>(B);
304 inline void setUnwindDest(BasicBlock *B){
305 Operands[2] = reinterpret_cast<Value*>(B);
308 virtual const BasicBlock *getSuccessor(unsigned i) const {
309 assert(i < 2 && "Successor # out of range for invoke!");
310 return i == 0 ? getNormalDest() : getUnwindDest();
312 inline BasicBlock *getSuccessor(unsigned i) {
313 assert(i < 2 && "Successor # out of range for invoke!");
314 return i == 0 ? getNormalDest() : getUnwindDest();
317 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
318 assert(idx < 2 && "Successor # out of range for invoke!");
319 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
322 virtual unsigned getNumSuccessors() const { return 2; }
324 // Methods for support type inquiry through isa, cast, and dyn_cast:
325 static inline bool classof(const InvokeInst *) { return true; }
326 static inline bool classof(const Instruction *I) {
327 return (I->getOpcode() == Instruction::Invoke);
329 static inline bool classof(const Value *V) {
330 return isa<Instruction>(V) && classof(cast<Instruction>(V));
335 //===---------------------------------------------------------------------------
336 /// UnwindInst - Immediately exit the current function, unwinding the stack
337 /// until an invoke instruction is found.
339 struct UnwindInst : public TerminatorInst {
340 UnwindInst(Instruction *InsertBefore = 0)
341 : TerminatorInst(Instruction::Unwind, InsertBefore) {
343 UnwindInst(BasicBlock *InsertAtEnd)
344 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
347 virtual Instruction *clone() const { return new UnwindInst(); }
349 virtual const BasicBlock *getSuccessor(unsigned idx) const {
350 assert(0 && "UnwindInst has no successors!");
354 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
355 virtual unsigned getNumSuccessors() const { return 0; }
357 // Methods for support type inquiry through isa, cast, and dyn_cast:
358 static inline bool classof(const UnwindInst *) { return true; }
359 static inline bool classof(const Instruction *I) {
360 return I->getOpcode() == Instruction::Unwind;
362 static inline bool classof(const Value *V) {
363 return isa<Instruction>(V) && classof(cast<Instruction>(V));
367 } // End llvm namespace