summaryrefslogtreecommitdiff
path: root/include/llvm/iTerminators.h
blob: 0d1cde0d3fb0102b4feb66a502d95f2c6260a6cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//===-- llvm/iTerminators.h - Termintator instruction nodes ------*- C++ -*--=//
//
// This file contains the declarations for all the subclasses of the
// Instruction class, which is itself defined in the Instruction.h file.  In 
// between these definitions and the Instruction class are classes that expose
// the SSA properties of each instruction, and that form the SSA graph.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ITERMINATORS_H
#define LLVM_ITERMINATORS_H

#include "llvm/InstrTypes.h"
#include "llvm/BasicBlock.h"
#include "llvm/ConstPoolVals.h"

//===----------------------------------------------------------------------===//
//         Classes to represent Basic Block "Terminator" instructions
//===----------------------------------------------------------------------===//


//===---------------------------------------------------------------------------
// ReturnInst - Return a value (possibly void), from a method.  Execution does
//              not continue in this method any longer.
//
class ReturnInst : public TerminatorInst {
  Use Val;   // Will be null if returning void...
  ReturnInst(const ReturnInst &RI);
public:
  ReturnInst(Value *value = 0);
  inline ~ReturnInst() { dropAllReferences(); }

  virtual Instruction *clone() const { return new ReturnInst(*this); }

  virtual string getOpcode() const { return "ret"; }

  inline const Value *getReturnValue() const { return Val; }
  inline       Value *getReturnValue()       { return Val; }

  virtual void dropAllReferences();
  virtual const Value *getOperand(unsigned i) const {
    return (i == 0) ? Val : 0;
  }
  inline Value *getOperand(unsigned i) { return (i == 0) ? Val : 0;  }
  virtual bool setOperand(unsigned i, Value *Val);
  virtual unsigned getNumOperands() const { return Val != 0; }

  // Additionally, they must provide a method to get at the successors of this
  // terminator instruction.  If 'idx' is out of range, a null pointer shall be
  // returned.
  //
  virtual const BasicBlock *getSuccessor(unsigned idx) const { return 0; }
  virtual unsigned getNumSuccessors() const { return 0; }
};


//===---------------------------------------------------------------------------
// BranchInst - Conditional or Unconditional Branch instruction.
//
class BranchInst : public TerminatorInst {
  BasicBlockUse TrueDest, FalseDest;
  Use Condition;

  BranchInst(const BranchInst &BI);
public:
  // If cond = null, then is an unconditional br...
  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
  inline ~BranchInst() { dropAllReferences(); }

  virtual Instruction *clone() const { return new BranchInst(*this); }

  virtual void dropAllReferences();
  
  inline bool isUnconditional() const {
    return Condition == 0 || !FalseDest;
  }

  virtual string getOpcode() const { return "br"; }

  inline Value *getOperand(unsigned i) {
    return (Value*)((const BranchInst *)this)->getOperand(i);
  }
  virtual const Value *getOperand(unsigned i) const;
  virtual bool setOperand(unsigned i, Value *Val);
  virtual unsigned getNumOperands() const { return isUnconditional() ? 1 : 3; }

  // Additionally, they must provide a method to get at the successors of this
  // terminator instruction.  If 'idx' is out of range, a null pointer shall be
  // returned.
  //
  virtual const BasicBlock *getSuccessor(unsigned idx) const;
  virtual unsigned getNumSuccessors() const { return 1+!isUnconditional(); }
};


//===---------------------------------------------------------------------------
// SwitchInst - Multiway switch
//
class SwitchInst : public TerminatorInst {
public:
  typedef pair<ConstPoolUse, BasicBlockUse> dest_value;
private:
  BasicBlockUse DefaultDest;
  Use Val;
  vector<dest_value> Destinations;

  SwitchInst(const SwitchInst &RI);
public:
  typedef vector<dest_value>::iterator       dest_iterator;
  typedef vector<dest_value>::const_iterator dest_const_iterator;

  SwitchInst(Value *Value, BasicBlock *Default);
  inline ~SwitchInst() { dropAllReferences(); }

  virtual Instruction *clone() const { return new SwitchInst(*this); }

  void dest_push_back(ConstPoolVal *OnVal, BasicBlock *Dest);

  virtual string getOpcode() const { return "switch"; }
  inline Value *getOperand(unsigned i) {
    return (Value*)((const SwitchInst*)this)->getOperand(i);
  }
  virtual const Value *getOperand(unsigned i) const;
  virtual bool setOperand(unsigned i, Value *Val);
  virtual unsigned getNumOperands() const;
  virtual void dropAllReferences();  

  // Additionally, they must provide a method to get at the successors of this
  // terminator instruction.  If 'idx' is out of range, a null pointer shall be
  // returned.
  //
  virtual const BasicBlock *getSuccessor(unsigned idx) const;
  virtual unsigned getNumSuccessors() const { return 1+Destinations.size(); }
};

#endif