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"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/ConstPoolVals.h"
17 //===----------------------------------------------------------------------===//
18 // Classes to represent Basic Block "Terminator" instructions
19 //===----------------------------------------------------------------------===//
22 //===---------------------------------------------------------------------------
23 // ReturnInst - Return a value (possibly void), from a method. Execution does
24 // not continue in this method any longer.
26 class ReturnInst : public TerminatorInst {
27 Use Val; // Will be null if returning void...
28 ReturnInst(const ReturnInst &RI);
30 ReturnInst(Value *value = 0);
31 inline ~ReturnInst() { dropAllReferences(); }
33 virtual Instruction *clone() const { return new ReturnInst(*this); }
35 virtual string getOpcode() const { return "ret"; }
37 inline const Value *getReturnValue() const { return Val; }
38 inline Value *getReturnValue() { return Val; }
40 virtual void dropAllReferences();
41 virtual const Value *getOperand(unsigned i) const {
42 return (i == 0) ? Val : 0;
44 inline Value *getOperand(unsigned i) { return (i == 0) ? Val : 0; }
45 virtual bool setOperand(unsigned i, Value *Val);
46 virtual unsigned getNumOperands() const { return Val != 0; }
48 // Additionally, they must provide a method to get at the successors of this
49 // terminator instruction. If 'idx' is out of range, a null pointer shall be
52 virtual const BasicBlock *getSuccessor(unsigned idx) const { return 0; }
53 virtual unsigned getNumSuccessors() const { return 0; }
57 //===---------------------------------------------------------------------------
58 // BranchInst - Conditional or Unconditional Branch instruction.
60 class BranchInst : public TerminatorInst {
61 BasicBlockUse TrueDest, FalseDest;
64 BranchInst(const BranchInst &BI);
66 // If cond = null, then is an unconditional br...
67 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
68 inline ~BranchInst() { dropAllReferences(); }
70 virtual Instruction *clone() const { return new BranchInst(*this); }
72 virtual void dropAllReferences();
74 inline bool isUnconditional() const {
75 return Condition == 0 || !FalseDest;
78 virtual string getOpcode() const { return "br"; }
80 inline Value *getOperand(unsigned i) {
81 return (Value*)((const BranchInst *)this)->getOperand(i);
83 virtual const Value *getOperand(unsigned i) const;
84 virtual bool setOperand(unsigned i, Value *Val);
85 virtual unsigned getNumOperands() const { return isUnconditional() ? 1 : 3; }
87 // Additionally, they must provide a method to get at the successors of this
88 // terminator instruction. If 'idx' is out of range, a null pointer shall be
91 virtual const BasicBlock *getSuccessor(unsigned idx) const;
92 virtual unsigned getNumSuccessors() const { return 1+!isUnconditional(); }
96 //===---------------------------------------------------------------------------
97 // SwitchInst - Multiway switch
99 class SwitchInst : public TerminatorInst {
101 typedef pair<ConstPoolUse, BasicBlockUse> dest_value;
103 BasicBlockUse DefaultDest;
105 vector<dest_value> Destinations;
107 SwitchInst(const SwitchInst &RI);
109 typedef vector<dest_value>::iterator dest_iterator;
110 typedef vector<dest_value>::const_iterator dest_const_iterator;
112 SwitchInst(Value *Value, BasicBlock *Default);
113 inline ~SwitchInst() { dropAllReferences(); }
115 virtual Instruction *clone() const { return new SwitchInst(*this); }
117 void dest_push_back(ConstPoolVal *OnVal, BasicBlock *Dest);
119 virtual string getOpcode() const { return "switch"; }
120 inline Value *getOperand(unsigned i) {
121 return (Value*)((const SwitchInst*)this)->getOperand(i);
123 virtual const Value *getOperand(unsigned i) const;
124 virtual bool setOperand(unsigned i, Value *Val);
125 virtual unsigned getNumOperands() const;
126 virtual void dropAllReferences();
128 // Additionally, they must provide a method to get at the successors of this
129 // terminator instruction. If 'idx' is out of range, a null pointer shall be
132 virtual const BasicBlock *getSuccessor(unsigned idx) const;
133 virtual unsigned getNumSuccessors() const { return 1+Destinations.size(); }