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 does
25 // 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 /// addCase - Add an entry to the switch instruction...
183 void addCase(Constant *OnVal, BasicBlock *Dest);
185 /// removeCase - This method removes the specified successor from the switch
186 /// instruction. Note that this cannot be used to remove the default
187 /// destination (successor #0).
189 void removeCase(unsigned idx);
191 virtual const BasicBlock *getSuccessor(unsigned idx) const {
192 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
193 return cast<BasicBlock>(Operands[idx*2+1].get());
195 inline BasicBlock *getSuccessor(unsigned idx) {
196 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
197 return cast<BasicBlock>(Operands[idx*2+1].get());
200 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
201 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
202 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
205 // getSuccessorValue - Return the value associated with the specified
207 inline const Constant *getSuccessorValue(unsigned idx) const {
208 assert(idx < getNumSuccessors() && "Successor # out of range!");
209 return cast<Constant>(Operands[idx*2].get());
211 inline Constant *getSuccessorValue(unsigned idx) {
212 assert(idx < getNumSuccessors() && "Successor # out of range!");
213 return cast<Constant>(Operands[idx*2].get());
215 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
217 // Methods for support type inquiry through isa, cast, and dyn_cast:
218 static inline bool classof(const SwitchInst *) { return true; }
219 static inline bool classof(const Instruction *I) {
220 return (I->getOpcode() == Instruction::Switch);
222 static inline bool classof(const Value *V) {
223 return isa<Instruction>(V) && classof(cast<Instruction>(V));
228 //===---------------------------------------------------------------------------
229 // InvokeInst - Invoke instruction
231 class InvokeInst : public TerminatorInst {
232 InvokeInst(const InvokeInst &BI);
234 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
235 const std::vector<Value*> &Params, const std::string &Name = "",
236 Instruction *InsertBefore = 0);
237 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
238 const std::vector<Value*> &Params, const std::string &Name,
239 BasicBlock *InsertAtEnd);
241 virtual Instruction *clone() const { return new InvokeInst(*this); }
243 bool mayWriteToMemory() const { return true; }
245 /// getCalledFunction - Return the function called, or null if this is an
246 /// indirect function invocation...
248 /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
250 const Function *getCalledFunction() const;
251 Function *getCalledFunction();
253 // getCalledValue - Get a pointer to a function that is invoked by this inst.
254 inline const Value *getCalledValue() const { return Operands[0]; }
255 inline Value *getCalledValue() { return Operands[0]; }
257 // get*Dest - Return the destination basic blocks...
258 inline const BasicBlock *getNormalDest() const {
259 return cast<BasicBlock>(Operands[1].get());
261 inline BasicBlock *getNormalDest() {
262 return cast<BasicBlock>(Operands[1].get());
264 inline const BasicBlock *getExceptionalDest() const {
265 return cast<BasicBlock>(Operands[2].get());
267 inline BasicBlock *getExceptionalDest() {
268 return cast<BasicBlock>(Operands[2].get());
271 inline void setNormalDest(BasicBlock *B){
272 Operands[1] = reinterpret_cast<Value*>(B);
275 inline void setExceptionalDest(BasicBlock *B){
276 Operands[2] = reinterpret_cast<Value*>(B);
279 virtual const BasicBlock *getSuccessor(unsigned i) const {
280 assert(i < 2 && "Successor # out of range for invoke!");
281 return i == 0 ? getNormalDest() : getExceptionalDest();
283 inline BasicBlock *getSuccessor(unsigned i) {
284 assert(i < 2 && "Successor # out of range for invoke!");
285 return i == 0 ? getNormalDest() : getExceptionalDest();
288 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
289 assert(idx < 2 && "Successor # out of range for invoke!");
290 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
293 virtual unsigned getNumSuccessors() const { return 2; }
295 // Methods for support type inquiry through isa, cast, and dyn_cast:
296 static inline bool classof(const InvokeInst *) { return true; }
297 static inline bool classof(const Instruction *I) {
298 return (I->getOpcode() == Instruction::Invoke);
300 static inline bool classof(const Value *V) {
301 return isa<Instruction>(V) && classof(cast<Instruction>(V));
306 //===---------------------------------------------------------------------------
307 /// UnwindInst - Immediately exit the current function, unwinding the stack
308 /// until an invoke instruction is found.
310 struct UnwindInst : public TerminatorInst {
311 UnwindInst(Instruction *InsertBefore = 0)
312 : TerminatorInst(Instruction::Unwind, InsertBefore) {
314 UnwindInst(BasicBlock *InsertAtEnd)
315 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
318 virtual Instruction *clone() const { return new UnwindInst(); }
320 virtual const BasicBlock *getSuccessor(unsigned idx) const {
321 assert(0 && "UnwindInst has no successors!");
325 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
326 virtual unsigned getNumSuccessors() const { return 0; }
328 // Methods for support type inquiry through isa, cast, and dyn_cast:
329 static inline bool classof(const UnwindInst *) { return true; }
330 static inline bool classof(const Instruction *I) {
331 return I->getOpcode() == Instruction::Unwind;
333 static inline bool classof(const Value *V) {
334 return isa<Instruction>(V) && classof(cast<Instruction>(V));
338 } // End llvm namespace