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) {
38 assert(!isa<BasicBlock>(RetVal) &&
39 "Cannot return basic block. Probably using the incorrect ctor");
41 Operands.push_back(Use(RetVal, this));
46 // ReturnInst constructors:
47 // ReturnInst() - 'ret void' instruction
48 // ReturnInst(Value* X) - 'ret X' instruction
49 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
50 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
51 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
52 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
53 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
54 : TerminatorInst(Instruction::Ret, InsertBefore) {
57 ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
58 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
61 ReturnInst(BasicBlock *InsertAtEnd)
62 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
65 virtual Instruction *clone() const { return new ReturnInst(*this); }
67 inline const Value *getReturnValue() const {
68 return Operands.size() ? Operands[0].get() : 0;
70 inline Value *getReturnValue() {
71 return Operands.size() ? Operands[0].get() : 0;
74 virtual const BasicBlock *getSuccessor(unsigned idx) const {
75 assert(0 && "ReturnInst has no successors!");
79 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
80 virtual unsigned getNumSuccessors() const { return 0; }
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const ReturnInst *) { return true; }
84 static inline bool classof(const Instruction *I) {
85 return (I->getOpcode() == Instruction::Ret);
87 static inline bool classof(const Value *V) {
88 return isa<Instruction>(V) && classof(cast<Instruction>(V));
92 //===---------------------------------------------------------------------------
93 /// BranchInst - Conditional or Unconditional Branch instruction.
95 class BranchInst : public TerminatorInst {
96 BranchInst(const BranchInst &BI);
97 void init(BasicBlock *IfTrue);
98 void init(BasicBlock *True, BasicBlock *False, Value *Cond);
100 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
101 // BranchInst(BB *B) - 'br B'
102 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
103 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
104 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
105 // BranchInst(BB* B, BB *I) - 'br B' insert at end
106 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
107 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
108 : TerminatorInst(Instruction::Br, InsertBefore) {
111 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
112 Instruction *InsertBefore = 0)
113 : TerminatorInst(Instruction::Br, InsertBefore) {
114 init(IfTrue, IfFalse, Cond);
117 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
118 : TerminatorInst(Instruction::Br, InsertAtEnd) {
122 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
123 BasicBlock *InsertAtEnd)
124 : TerminatorInst(Instruction::Br, InsertAtEnd) {
125 init(IfTrue, IfFalse, Cond);
128 virtual Instruction *clone() const { return new BranchInst(*this); }
130 inline bool isUnconditional() const { return Operands.size() == 1; }
131 inline bool isConditional() const { return Operands.size() == 3; }
133 inline Value *getCondition() const {
134 assert(isConditional() && "Cannot get condition of an uncond branch!");
135 return Operands[2].get();
138 void setCondition(Value *V) {
139 assert(isConditional() && "Cannot set condition of unconditional branch!");
143 // setUnconditionalDest - Change the current branch to an unconditional branch
144 // targeting the specified block.
146 void setUnconditionalDest(BasicBlock *Dest) {
147 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
148 Operands[0] = reinterpret_cast<Value*>(Dest);
151 virtual const BasicBlock *getSuccessor(unsigned i) const {
152 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
153 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
154 cast<BasicBlock>(Operands[1].get());
156 inline BasicBlock *getSuccessor(unsigned idx) {
157 const BranchInst *BI = this;
158 return const_cast<BasicBlock*>(BI->getSuccessor(idx));
161 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
162 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
163 Operands[idx] = reinterpret_cast<Value*>(NewSucc);
166 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
168 // Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const BranchInst *) { return true; }
170 static inline bool classof(const Instruction *I) {
171 return (I->getOpcode() == Instruction::Br);
173 static inline bool classof(const Value *V) {
174 return isa<Instruction>(V) && classof(cast<Instruction>(V));
179 //===---------------------------------------------------------------------------
180 /// SwitchInst - Multiway switch
182 class SwitchInst : public TerminatorInst {
183 // Operand[0] = Value to switch on
184 // Operand[1] = Default basic block destination
185 // Operand[2n ] = Value to match
186 // Operand[2n+1] = BasicBlock to go to on match
187 SwitchInst(const SwitchInst &RI);
188 void init(Value *Value, BasicBlock *Default);
191 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0)
192 : TerminatorInst(Instruction::Switch, InsertBefore) {
193 init(Value, Default);
195 SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd)
196 : TerminatorInst(Instruction::Switch, InsertAtEnd) {
197 init(Value, Default);
200 virtual Instruction *clone() const { return new SwitchInst(*this); }
202 // Accessor Methods for Switch stmt
204 inline const Value *getCondition() const { return Operands[0]; }
205 inline Value *getCondition() { return Operands[0]; }
206 inline const BasicBlock *getDefaultDest() const {
207 return cast<BasicBlock>(Operands[1].get());
209 inline BasicBlock *getDefaultDest() {
210 return cast<BasicBlock>(Operands[1].get());
213 /// getNumCases - return the number of 'cases' in this switch instruction.
214 /// Note that case #0 is always the default case.
215 unsigned getNumCases() const {
216 return Operands.size()/2;
219 /// getCaseValue - Return the specified case value. Note that case #0, the
220 /// default destination, does not have a case value.
221 Constant *getCaseValue(unsigned i) {
222 assert(i && i < getNumCases() && "Illegal case value to get!");
223 return getSuccessorValue(i);
226 /// getCaseValue - Return the specified case value. Note that case #0, the
227 /// default destination, does not have a case value.
228 const Constant *getCaseValue(unsigned i) const {
229 assert(i && i < getNumCases() && "Illegal case value to get!");
230 return getSuccessorValue(i);
233 /// findCaseValue - Search all of the case values for the specified constant.
234 /// If it is explicitly handled, return the case number of it, otherwise
235 /// return 0 to indicate that it is handled by the default handler.
236 unsigned findCaseValue(const Constant *C) const {
237 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
238 if (getCaseValue(i) == C)
243 /// addCase - Add an entry to the switch instruction...
245 void addCase(Constant *OnVal, BasicBlock *Dest);
247 /// removeCase - This method removes the specified successor from the switch
248 /// instruction. Note that this cannot be used to remove the default
249 /// destination (successor #0).
251 void removeCase(unsigned idx);
253 virtual const BasicBlock *getSuccessor(unsigned idx) const {
254 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
255 return cast<BasicBlock>(Operands[idx*2+1].get());
257 inline BasicBlock *getSuccessor(unsigned idx) {
258 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
259 return cast<BasicBlock>(Operands[idx*2+1].get());
262 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
263 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
264 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
267 // getSuccessorValue - Return the value associated with the specified
269 inline const Constant *getSuccessorValue(unsigned idx) const {
270 assert(idx < getNumSuccessors() && "Successor # out of range!");
271 return cast<Constant>(Operands[idx*2].get());
273 inline Constant *getSuccessorValue(unsigned idx) {
274 assert(idx < getNumSuccessors() && "Successor # out of range!");
275 return cast<Constant>(Operands[idx*2].get());
277 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
279 // Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const SwitchInst *) { return true; }
281 static inline bool classof(const Instruction *I) {
282 return (I->getOpcode() == Instruction::Switch);
284 static inline bool classof(const Value *V) {
285 return isa<Instruction>(V) && classof(cast<Instruction>(V));
289 //===---------------------------------------------------------------------------
290 /// InvokeInst - Invoke instruction
292 class InvokeInst : public TerminatorInst {
293 InvokeInst(const InvokeInst &BI);
294 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
295 const std::vector<Value*> &Params);
297 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
298 const std::vector<Value*> &Params, const std::string &Name = "",
299 Instruction *InsertBefore = 0);
300 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
301 const std::vector<Value*> &Params, const std::string &Name,
302 BasicBlock *InsertAtEnd);
304 virtual Instruction *clone() const { return new InvokeInst(*this); }
306 bool mayWriteToMemory() const { return true; }
308 /// getCalledFunction - Return the function called, or null if this is an
309 /// indirect function invocation...
311 /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
313 const Function *getCalledFunction() const;
314 Function *getCalledFunction();
316 // getCalledValue - Get a pointer to a function that is invoked by this inst.
317 inline const Value *getCalledValue() const { return Operands[0]; }
318 inline Value *getCalledValue() { return Operands[0]; }
320 // get*Dest - Return the destination basic blocks...
321 inline const BasicBlock *getNormalDest() const {
322 return cast<BasicBlock>(Operands[1].get());
324 inline BasicBlock *getNormalDest() {
325 return cast<BasicBlock>(Operands[1].get());
327 inline const BasicBlock *getUnwindDest() const {
328 return cast<BasicBlock>(Operands[2].get());
330 inline BasicBlock *getUnwindDest() {
331 return cast<BasicBlock>(Operands[2].get());
334 inline void setNormalDest(BasicBlock *B){
335 Operands[1] = reinterpret_cast<Value*>(B);
338 inline void setUnwindDest(BasicBlock *B){
339 Operands[2] = reinterpret_cast<Value*>(B);
342 virtual const BasicBlock *getSuccessor(unsigned i) const {
343 assert(i < 2 && "Successor # out of range for invoke!");
344 return i == 0 ? getNormalDest() : getUnwindDest();
346 inline BasicBlock *getSuccessor(unsigned i) {
347 assert(i < 2 && "Successor # out of range for invoke!");
348 return i == 0 ? getNormalDest() : getUnwindDest();
351 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
352 assert(idx < 2 && "Successor # out of range for invoke!");
353 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
356 virtual unsigned getNumSuccessors() const { return 2; }
358 // Methods for support type inquiry through isa, cast, and dyn_cast:
359 static inline bool classof(const InvokeInst *) { return true; }
360 static inline bool classof(const Instruction *I) {
361 return (I->getOpcode() == Instruction::Invoke);
363 static inline bool classof(const Value *V) {
364 return isa<Instruction>(V) && classof(cast<Instruction>(V));
369 //===---------------------------------------------------------------------------
370 /// UnwindInst - Immediately exit the current function, unwinding the stack
371 /// until an invoke instruction is found.
373 struct UnwindInst : public TerminatorInst {
374 UnwindInst(Instruction *InsertBefore = 0)
375 : TerminatorInst(Instruction::Unwind, InsertBefore) {
377 UnwindInst(BasicBlock *InsertAtEnd)
378 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
381 virtual Instruction *clone() const { return new UnwindInst(); }
383 virtual const BasicBlock *getSuccessor(unsigned idx) const {
384 assert(0 && "UnwindInst has no successors!");
388 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
389 virtual unsigned getNumSuccessors() const { return 0; }
391 // Methods for support type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const UnwindInst *) { return true; }
393 static inline bool classof(const Instruction *I) {
394 return I->getOpcode() == Instruction::Unwind;
396 static inline bool classof(const Value *V) {
397 return isa<Instruction>(V) && classof(cast<Instruction>(V));
401 } // End llvm namespace