1 //===-- llvm/iTerminators.h - Termintator instruction nodes ------*- C++ -*--=//
3 // This file contains the declarations for all the subclasses of the
4 // Instruction class, which is itself defined in the Instruction.h file. In
5 // between these definitions and the Instruction class are classes that expose
6 // the SSA properties of each instruction, and that form the SSA graph.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ITERMINATORS_H
11 #define LLVM_ITERMINATORS_H
13 #include "llvm/InstrTypes.h"
15 //===----------------------------------------------------------------------===//
16 // Classes to represent Basic Block "Terminator" instructions
17 //===----------------------------------------------------------------------===//
20 //===---------------------------------------------------------------------------
21 // ReturnInst - Return a value (possibly void), from a method. Execution does
22 // not continue in this method any longer.
24 class ReturnInst : public TerminatorInst {
25 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
26 if (RI.Operands.size()) {
27 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
29 Operands.push_back(Use(RI.Operands[0], this));
33 ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
36 Operands.push_back(Use(RetVal, this));
40 virtual Instruction *clone() const { return new ReturnInst(*this); }
42 virtual const char *getOpcodeName() const { return "ret"; }
44 inline const Value *getReturnValue() const {
45 return Operands.size() ? Operands[0].get() : 0;
47 inline Value *getReturnValue() {
48 return Operands.size() ? Operands[0].get() : 0;
51 virtual const BasicBlock *getSuccessor(unsigned idx) const {
52 assert(0 && "ReturnInst has no successors!");
55 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
56 assert(0 && "ReturnInst has no successors!");
58 virtual unsigned getNumSuccessors() const { return 0; }
60 // Methods for support type inquiry through isa, cast, and dyn_cast:
61 static inline bool classof(const ReturnInst *) { return true; }
62 static inline bool classof(const Instruction *I) {
63 return (I->getOpcode() == Instruction::Ret);
65 static inline bool classof(const Value *V) {
66 return isa<Instruction>(V) && classof(cast<Instruction>(V));
71 //===---------------------------------------------------------------------------
72 // BranchInst - Conditional or Unconditional Branch instruction.
74 class BranchInst : public TerminatorInst {
75 BranchInst(const BranchInst &BI);
77 // If cond = null, then is an unconditional br...
78 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
80 virtual Instruction *clone() const { return new BranchInst(*this); }
82 inline bool isUnconditional() const { return Operands.size() == 1; }
83 inline bool isConditional() const { return Operands.size() == 3; }
85 inline const Value *getCondition() const {
86 return isUnconditional() ? 0 : Operands[2].get();
88 inline Value *getCondition() {
89 return isUnconditional() ? 0 : Operands[2].get();
92 virtual const char *getOpcodeName() const { return "br"; }
94 // setUnconditionalDest - Change the current branch to an unconditional branch
95 // targeting the specified block.
97 void setUnconditionalDest(BasicBlock *Dest) {
98 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
99 Operands[0] = (Value*)Dest;
102 virtual const BasicBlock *getSuccessor(unsigned i) const {
103 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
104 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
105 cast<BasicBlock>(Operands[1].get());
107 inline BasicBlock *getSuccessor(unsigned idx) {
108 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
111 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
112 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
113 Operands[idx] = (Value*)NewSucc;
116 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const BranchInst *) { return true; }
120 static inline bool classof(const Instruction *I) {
121 return (I->getOpcode() == Instruction::Br);
123 static inline bool classof(const Value *V) {
124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
129 //===---------------------------------------------------------------------------
130 // SwitchInst - Multiway switch
132 class SwitchInst : public TerminatorInst {
133 // Operand[0] = Value to switch on
134 // Operand[1] = Default basic block destination
135 // Operand[2n ] = Value to match
136 // Operand[2n+1] = BasicBlock to go to on match
137 SwitchInst(const SwitchInst &RI);
139 SwitchInst(Value *Value, BasicBlock *Default);
141 virtual Instruction *clone() const { return new SwitchInst(*this); }
143 // Accessor Methods for Switch stmt
145 inline const Value *getCondition() const { return Operands[0]; }
146 inline Value *getCondition() { return Operands[0]; }
147 inline const BasicBlock *getDefaultDest() const {
148 return cast<BasicBlock>(Operands[1].get());
150 inline BasicBlock *getDefaultDest() {
151 return cast<BasicBlock>(Operands[1].get());
154 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
156 virtual const char *getOpcodeName() const { return "switch"; }
158 virtual const BasicBlock *getSuccessor(unsigned idx) const {
159 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
160 return cast<BasicBlock>(Operands[idx*2+1].get());
162 inline BasicBlock *getSuccessor(unsigned idx) {
163 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
164 return cast<BasicBlock>(Operands[idx*2+1].get());
167 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
168 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
169 Operands[idx*2+1] = (Value*)NewSucc;
172 // getSuccessorValue - Return the value associated with the specified
174 inline const Constant *getSuccessorValue(unsigned idx) const {
175 assert(idx < getNumSuccessors() && "Successor # out of range!");
176 return cast<Constant>(Operands[idx*2].get());
178 inline Constant *getSuccessorValue(unsigned idx) {
179 assert(idx < getNumSuccessors() && "Successor # out of range!");
180 return cast<Constant>(Operands[idx*2].get());
182 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
184 // Methods for support type inquiry through isa, cast, and dyn_cast:
185 static inline bool classof(const SwitchInst *) { return true; }
186 static inline bool classof(const Instruction *I) {
187 return (I->getOpcode() == Instruction::Switch);
189 static inline bool classof(const Value *V) {
190 return isa<Instruction>(V) && classof(cast<Instruction>(V));
195 //===---------------------------------------------------------------------------
196 // InvokeInst - Invoke instruction
198 class InvokeInst : public TerminatorInst {
199 InvokeInst(const InvokeInst &BI);
201 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
202 const std::vector<Value*> &Params, const std::string &Name = "");
204 virtual Instruction *clone() const { return new InvokeInst(*this); }
206 bool hasSideEffects() const { return true; }
208 // getCalledFunction - Return the function called, or null if this is an
209 // indirect function invocation...
211 inline const Function *getCalledFunction() const {
212 return dyn_cast<Function>(Operands[0].get());
214 inline Function *getCalledFunction() {
215 return dyn_cast<Function>(Operands[0].get());
218 // getCalledValue - Get a pointer to a method that is invoked by this inst.
219 inline const Value *getCalledValue() const { return Operands[0]; }
220 inline Value *getCalledValue() { return Operands[0]; }
222 // get*Dest - Return the destination basic blocks...
223 inline const BasicBlock *getNormalDest() const {
224 return cast<BasicBlock>(Operands[1].get());
226 inline BasicBlock *getNormalDest() {
227 return cast<BasicBlock>(Operands[1].get());
229 inline const BasicBlock *getExceptionalDest() const {
230 return cast<BasicBlock>(Operands[2].get());
232 inline BasicBlock *getExceptionalDest() {
233 return cast<BasicBlock>(Operands[2].get());
236 inline void setNormalDest(BasicBlock *B){
237 Operands[1] = (Value*)B;
240 inline void setExceptionalDest(BasicBlock *B){
241 Operands[2] = (Value*)B;
244 virtual const char *getOpcodeName() const { return "invoke"; }
246 virtual const BasicBlock *getSuccessor(unsigned i) const {
247 assert(i < 2 && "Successor # out of range for invoke!");
248 return i == 0 ? getNormalDest() : getExceptionalDest();
250 inline BasicBlock *getSuccessor(unsigned i) {
251 assert(i < 2 && "Successor # out of range for invoke!");
252 return i == 0 ? getNormalDest() : getExceptionalDest();
255 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
256 assert(idx < 2 && "Successor # out of range for invoke!");
257 Operands[idx+1] = (Value*)NewSucc;
260 virtual unsigned getNumSuccessors() const { return 2; }
262 // Methods for support type inquiry through isa, cast, and dyn_cast:
263 static inline bool classof(const InvokeInst *) { return true; }
264 static inline bool classof(const Instruction *I) {
265 return (I->getOpcode() == Instruction::Invoke);
267 static inline bool classof(const Value *V) {
268 return isa<Instruction>(V) && classof(cast<Instruction>(V));