1 //===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
3 // This file contains the declarations for all the subclasses of the Instruction
4 // class which represent "terminator" instructions. Terminator instructions are
5 // the only instructions allowed and required to terminate a BasicBlock.
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ITERMINATORS_H
10 #define LLVM_ITERMINATORS_H
12 #include "llvm/InstrTypes.h"
14 //===---------------------------------------------------------------------------
15 // ReturnInst - Return a value (possibly void), from a method. Execution does
16 // not continue in this method any longer.
18 class ReturnInst : public TerminatorInst {
19 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
20 if (RI.Operands.size()) {
21 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
23 Operands.push_back(Use(RI.Operands[0], this));
27 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
28 : TerminatorInst(Instruction::Ret, InsertBefore) {
31 Operands.push_back(Use(RetVal, this));
35 virtual Instruction *clone() const { return new ReturnInst(*this); }
37 inline const Value *getReturnValue() const {
38 return Operands.size() ? Operands[0].get() : 0;
40 inline Value *getReturnValue() {
41 return Operands.size() ? Operands[0].get() : 0;
44 virtual const BasicBlock *getSuccessor(unsigned idx) const {
45 assert(0 && "ReturnInst has no successors!");
49 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
50 assert(0 && "ReturnInst has no successors!");
52 virtual unsigned getNumSuccessors() const { return 0; }
54 // Methods for support type inquiry through isa, cast, and dyn_cast:
55 static inline bool classof(const ReturnInst *) { return true; }
56 static inline bool classof(const Instruction *I) {
57 return (I->getOpcode() == Instruction::Ret);
59 static inline bool classof(const Value *V) {
60 return isa<Instruction>(V) && classof(cast<Instruction>(V));
65 //===---------------------------------------------------------------------------
66 // BranchInst - Conditional or Unconditional Branch instruction.
68 class BranchInst : public TerminatorInst {
69 BranchInst(const BranchInst &BI);
71 // If cond = null, then is an unconditional br...
72 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond = 0,
73 Instruction *InsertBefore = 0);
74 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
76 virtual Instruction *clone() const { return new BranchInst(*this); }
78 inline bool isUnconditional() const { return Operands.size() == 1; }
79 inline bool isConditional() const { return Operands.size() == 3; }
81 inline const Value *getCondition() const {
82 return isUnconditional() ? 0 : Operands[2].get();
84 Value *getCondition() {
85 return isUnconditional() ? 0 : Operands[2].get();
88 // setUnconditionalDest - Change the current branch to an unconditional branch
89 // targeting the specified block.
91 void setUnconditionalDest(BasicBlock *Dest) {
92 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
93 Operands[0] = (Value*)Dest;
96 virtual const BasicBlock *getSuccessor(unsigned i) const {
97 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
98 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
99 cast<BasicBlock>(Operands[1].get());
101 inline BasicBlock *getSuccessor(unsigned idx) {
102 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
105 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
106 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
107 Operands[idx] = (Value*)NewSucc;
110 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const BranchInst *) { return true; }
114 static inline bool classof(const Instruction *I) {
115 return (I->getOpcode() == Instruction::Br);
117 static inline bool classof(const Value *V) {
118 return isa<Instruction>(V) && classof(cast<Instruction>(V));
123 //===---------------------------------------------------------------------------
124 // SwitchInst - Multiway switch
126 class SwitchInst : public TerminatorInst {
127 // Operand[0] = Value to switch on
128 // Operand[1] = Default basic block destination
129 // Operand[2n ] = Value to match
130 // Operand[2n+1] = BasicBlock to go to on match
131 SwitchInst(const SwitchInst &RI);
133 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
135 virtual Instruction *clone() const { return new SwitchInst(*this); }
137 // Accessor Methods for Switch stmt
139 inline const Value *getCondition() const { return Operands[0]; }
140 inline Value *getCondition() { return Operands[0]; }
141 inline const BasicBlock *getDefaultDest() const {
142 return cast<BasicBlock>(Operands[1].get());
144 inline BasicBlock *getDefaultDest() {
145 return cast<BasicBlock>(Operands[1].get());
148 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
150 virtual const BasicBlock *getSuccessor(unsigned idx) const {
151 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
152 return cast<BasicBlock>(Operands[idx*2+1].get());
154 inline BasicBlock *getSuccessor(unsigned idx) {
155 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
156 return cast<BasicBlock>(Operands[idx*2+1].get());
159 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
160 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
161 Operands[idx*2+1] = (Value*)NewSucc;
164 // getSuccessorValue - Return the value associated with the specified
166 inline const Constant *getSuccessorValue(unsigned idx) const {
167 assert(idx < getNumSuccessors() && "Successor # out of range!");
168 return cast<Constant>(Operands[idx*2].get());
170 inline Constant *getSuccessorValue(unsigned idx) {
171 assert(idx < getNumSuccessors() && "Successor # out of range!");
172 return cast<Constant>(Operands[idx*2].get());
174 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const SwitchInst *) { return true; }
178 static inline bool classof(const Instruction *I) {
179 return (I->getOpcode() == Instruction::Switch);
181 static inline bool classof(const Value *V) {
182 return isa<Instruction>(V) && classof(cast<Instruction>(V));
187 //===---------------------------------------------------------------------------
188 // InvokeInst - Invoke instruction
190 class InvokeInst : public TerminatorInst {
191 InvokeInst(const InvokeInst &BI);
193 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
194 const std::vector<Value*> &Params, const std::string &Name = "",
195 Instruction *InsertBefore = 0);
197 virtual Instruction *clone() const { return new InvokeInst(*this); }
199 bool mayWriteToMemory() const { return true; }
201 // getCalledFunction - Return the function called, or null if this is an
202 // indirect function invocation...
204 inline const Function *getCalledFunction() const {
205 return dyn_cast<Function>(Operands[0].get());
207 inline Function *getCalledFunction() {
208 return dyn_cast<Function>(Operands[0].get());
211 // getCalledValue - Get a pointer to a method that is invoked by this inst.
212 inline const Value *getCalledValue() const { return Operands[0]; }
213 inline Value *getCalledValue() { return Operands[0]; }
215 // get*Dest - Return the destination basic blocks...
216 inline const BasicBlock *getNormalDest() const {
217 return cast<BasicBlock>(Operands[1].get());
219 inline BasicBlock *getNormalDest() {
220 return cast<BasicBlock>(Operands[1].get());
222 inline const BasicBlock *getExceptionalDest() const {
223 return cast<BasicBlock>(Operands[2].get());
225 inline BasicBlock *getExceptionalDest() {
226 return cast<BasicBlock>(Operands[2].get());
229 inline void setNormalDest(BasicBlock *B){
230 Operands[1] = (Value*)B;
233 inline void setExceptionalDest(BasicBlock *B){
234 Operands[2] = (Value*)B;
237 virtual const BasicBlock *getSuccessor(unsigned i) const {
238 assert(i < 2 && "Successor # out of range for invoke!");
239 return i == 0 ? getNormalDest() : getExceptionalDest();
241 inline BasicBlock *getSuccessor(unsigned i) {
242 assert(i < 2 && "Successor # out of range for invoke!");
243 return i == 0 ? getNormalDest() : getExceptionalDest();
246 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
247 assert(idx < 2 && "Successor # out of range for invoke!");
248 Operands[idx+1] = (Value*)NewSucc;
251 virtual unsigned getNumSuccessors() const { return 2; }
253 // Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const InvokeInst *) { return true; }
255 static inline bool classof(const Instruction *I) {
256 return (I->getOpcode() == Instruction::Invoke);
258 static inline bool classof(const Value *V) {
259 return isa<Instruction>(V) && classof(cast<Instruction>(V));