18
18
#include < iterator>
19
19
20
20
namespace swift {
21
+
21
22
class SILBasicBlock ;
22
23
class TermInst ;
23
24
24
- // / SILSuccessor - This represents a reference to a SILBasicBlock in a
25
- // / terminator instruction, forming a list of TermInst references to
26
- // / BasicBlocks. This forms the predecessor list, ensuring that it is always
27
- // / kept up to date.
25
+ // / \brief An edge in the control flow graph.
26
+ // /
27
+ // / A SILSuccessor is stored in the terminator instruction of the tail block of
28
+ // / the CFG edge. Internally it has a back reference to the terminator that
29
+ // / contains it (ContainingInst). It also contains the SuccessorBlock that is
30
+ // / the "head" of the CFG edge. This makes it very simple to iterate over the
31
+ // / successors of a specific block.
32
+ // /
33
+ // / SILSuccessor also enables given a "head" edge the ability to iterate over
34
+ // / predecessors. This is done by using an ilist that is embedded into
35
+ // / SILSuccessors.
28
36
class SILSuccessor {
29
- friend class SILSuccessorIterator ;
30
- // / ContainingInst - This is the Terminator instruction that contains this
31
- // / successor.
37
+ // / The terminator instruction that contains this SILSuccessor.
32
38
TermInst *ContainingInst = nullptr ;
33
39
34
- // / SuccessorBlock - If non-null, this is the BasicBlock that the terminator
35
- // / branches to.
40
+ // / If non-null, this is the BasicBlock that the terminator branches to.
36
41
SILBasicBlock *SuccessorBlock = nullptr ;
37
-
38
- // / This is the prev and next terminator reference to SuccessorBlock, or
39
- // / null if SuccessorBlock is null.
40
- SILSuccessor **Prev = nullptr , *Next = nullptr ;
42
+
43
+ // / A pointer to the SILSuccessor that represents the previous SILSuccessor in the
44
+ // / predecessor list for SuccessorBlock.
45
+ // /
46
+ // / Must be nullptr if SuccessorBlock is.
47
+ SILSuccessor **Prev = nullptr ;
48
+
49
+ // / A pointer to the SILSuccessor that represents the next SILSuccessor in the
50
+ // / predecessor list for SuccessorBlock.
51
+ // /
52
+ // / Must be nullptr if SuccessorBlock is.
53
+ SILSuccessor *Next = nullptr ;
54
+
41
55
public:
42
56
SILSuccessor () {}
43
57
@@ -62,41 +76,40 @@ class SILSuccessor {
62
76
// Do not copy or move these.
63
77
SILSuccessor (const SILSuccessor &) = delete ;
64
78
SILSuccessor (SILSuccessor &&) = delete ;
65
- };
66
-
67
- // / SILSuccessorIterator - This is an iterator for walking the successor list of
68
- // / a SILBasicBlock.
69
- class SILSuccessorIterator {
70
- SILSuccessor *Cur;
71
- public:
72
- using difference_type = std::ptrdiff_t ;
73
- using value_type = SILBasicBlock *;
74
- using pointer = SILBasicBlock **;
75
- using reference = SILBasicBlock *&;
76
- using iterator_category = std::forward_iterator_tag;
77
-
78
- SILSuccessorIterator (SILSuccessor *Cur = 0 ) : Cur(Cur) {}
79
-
80
- bool operator ==(SILSuccessorIterator I2) const { return Cur == I2.Cur ; }
81
- bool operator !=(SILSuccessorIterator I2) const { return Cur != I2.Cur ; }
82
79
83
- SILSuccessorIterator &operator ++() {
84
- assert (Cur && " Trying to advance past end" );
85
- Cur = Cur->Next ;
86
- return *this ;
87
- }
80
+ // / This is an iterator for walking the predecessor list of a SILBasicBlock.
81
+ class pred_iterator {
82
+ SILSuccessor *Cur;
88
83
89
- SILSuccessorIterator operator ++(int ) {
90
- SILSuccessorIterator copy = *this ;
91
- ++*this ;
92
- return copy;
93
- }
84
+ public:
85
+ using difference_type = std::ptrdiff_t ;
86
+ using value_type = SILBasicBlock *;
87
+ using pointer = SILBasicBlock **;
88
+ using reference = SILBasicBlock *&;
89
+ using iterator_category = std::forward_iterator_tag;
90
+
91
+ pred_iterator (SILSuccessor *Cur = 0 ) : Cur(Cur) {}
94
92
95
- SILSuccessor *getSuccessorRef () const { return Cur; }
96
- SILBasicBlock *operator *();
97
- const SILBasicBlock *operator *() const ;
93
+ bool operator ==(pred_iterator I2) const { return Cur == I2.Cur ; }
94
+ bool operator !=(pred_iterator I2) const { return Cur != I2.Cur ; }
95
+
96
+ pred_iterator &operator ++() {
97
+ assert (Cur && " Trying to advance past end" );
98
+ Cur = Cur->Next ;
99
+ return *this ;
100
+ }
101
+
102
+ pred_iterator operator ++(int ) {
103
+ pred_iterator copy = *this ;
104
+ ++*this ;
105
+ return copy;
106
+ }
107
+
108
+ SILSuccessor *getSuccessorRef () const { return Cur; }
109
+ SILBasicBlock *operator *();
110
+ const SILBasicBlock *operator *() const ;
111
+ };
98
112
};
99
-
100
113
101
114
} // end swift namespace
102
115
0 commit comments