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!");
54 virtual unsigned getNumSuccessors() const { return 0; }
56 // Methods for support type inquiry through isa, cast, and dyn_cast:
57 static inline bool classof(const ReturnInst *) { return true; }
58 static inline bool classof(const Instruction *I) {
59 return (I->getOpcode() == Instruction::Ret);
61 static inline bool classof(const Value *V) {
62 return isa<Instruction>(V) && classof(cast<Instruction>(V));
67 //===---------------------------------------------------------------------------
68 // BranchInst - Conditional or Unconditional Branch instruction.
70 class BranchInst : public TerminatorInst {
71 BranchInst(const BranchInst &BI);
73 // If cond = null, then is an unconditional br...
74 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 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 inline Value *getCondition() {
85 return isUnconditional() ? 0 : Operands[2].get();
88 virtual const char *getOpcodeName() const { return "br"; }
90 // setUnconditionalDest - Change the current branch to an unconditional branch
91 // targeting the specified block.
93 void setUnconditionalDest(BasicBlock *Dest) {
94 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
95 Operands[0] = (Value*)Dest;
98 virtual const BasicBlock *getSuccessor(unsigned i) const {
99 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
100 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
101 cast<BasicBlock>(Operands[1].get());
103 inline BasicBlock *getSuccessor(unsigned idx) {
104 return (BasicBlock*)((const BranchInst *)this)->getSuccessor(idx);
107 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool classof(const BranchInst *) { return true; }
111 static inline bool classof(const Instruction *I) {
112 return (I->getOpcode() == Instruction::Br);
114 static inline bool classof(const Value *V) {
115 return isa<Instruction>(V) && classof(cast<Instruction>(V));
120 //===---------------------------------------------------------------------------
121 // SwitchInst - Multiway switch
123 class SwitchInst : public TerminatorInst {
124 // Operand[0] = Value to switch on
125 // Operand[1] = Default basic block destination
126 // Operand[2n ] = Value to match
127 // Operand[2n+1] = BasicBlock to go to on match
128 SwitchInst(const SwitchInst &RI);
130 SwitchInst(Value *Value, BasicBlock *Default);
132 virtual Instruction *clone() const { return new SwitchInst(*this); }
134 // Accessor Methods for Switch stmt
136 inline const Value *getCondition() const { return Operands[0]; }
137 inline Value *getCondition() { return Operands[0]; }
138 inline const BasicBlock *getDefaultDest() const {
139 return cast<BasicBlock>(Operands[1].get());
141 inline BasicBlock *getDefaultDest() {
142 return cast<BasicBlock>(Operands[1].get());
145 void dest_push_back(Constant *OnVal, BasicBlock *Dest);
147 virtual const char *getOpcodeName() const { return "switch"; }
149 virtual const BasicBlock *getSuccessor(unsigned idx) const {
150 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
151 return cast<BasicBlock>(Operands[idx*2+1].get());
153 inline BasicBlock *getSuccessor(unsigned idx) {
154 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
155 return cast<BasicBlock>(Operands[idx*2+1].get());
158 // getSuccessorValue - Return the value associated with the specified
160 inline const Constant *getSuccessorValue(unsigned idx) const {
161 assert(idx < getNumSuccessors() && "Successor # out of range!");
162 return cast<Constant>(Operands[idx*2].get());
164 inline Constant *getSuccessorValue(unsigned idx) {
165 assert(idx < getNumSuccessors() && "Successor # out of range!");
166 return cast<Constant>(Operands[idx*2].get());
168 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const SwitchInst *) { return true; }
172 static inline bool classof(const Instruction *I) {
173 return (I->getOpcode() == Instruction::Switch);
175 static inline bool classof(const Value *V) {
176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
181 //===---------------------------------------------------------------------------
182 // InvokeInst - Invoke instruction
184 class InvokeInst : public TerminatorInst {
185 InvokeInst(const InvokeInst &BI);
187 InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
188 const std::vector<Value*> &Params, const std::string &Name = "");
190 virtual Instruction *clone() const { return new InvokeInst(*this); }
192 // getCalledFunction - Return the function called, or null if this is an
193 // indirect function invocation...
195 inline const Function *getCalledFunction() const {
196 return dyn_cast<Function>(Operands[0].get());
198 inline Function *getCalledFunction() {
199 return dyn_cast<Function>(Operands[0].get());
202 // getCalledValue - Get a pointer to a method that is invoked by this inst.
203 inline const Value *getCalledValue() const { return Operands[0]; }
204 inline Value *getCalledValue() { return Operands[0]; }
206 // get*Dest - Return the destination basic blocks...
207 inline const BasicBlock *getNormalDest() const {
208 return cast<BasicBlock>(Operands[1].get());
210 inline BasicBlock *getNormalDest() {
211 return cast<BasicBlock>(Operands[1].get());
213 inline const BasicBlock *getExceptionalDest() const {
214 return cast<BasicBlock>(Operands[2].get());
216 inline BasicBlock *getExceptionalDest() {
217 return cast<BasicBlock>(Operands[2].get());
220 virtual const char *getOpcodeName() const { return "invoke"; }
222 virtual const BasicBlock *getSuccessor(unsigned i) const {
223 assert(i < 2 && "Successor # out of range for invoke!");
224 return i == 0 ? getNormalDest() : getExceptionalDest();
226 inline BasicBlock *getSuccessor(unsigned i) {
227 assert(i < 2 && "Successor # out of range for invoke!");
228 return i == 0 ? getNormalDest() : getExceptionalDest();
231 virtual unsigned getNumSuccessors() const { return 2; }
233 // Methods for support type inquiry through isa, cast, and dyn_cast:
234 static inline bool classof(const InvokeInst *) { return true; }
235 static inline bool classof(const Instruction *I) {
236 return (I->getOpcode() == Instruction::Invoke);
238 static inline bool classof(const Value *V) {
239 return isa<Instruction>(V) && classof(cast<Instruction>(V));