Skip to content

Commit b853412

Browse files
authored
Merge pull request swiftlang#9037 from gottesmm/small_succ_updates
2 parents 0ce898b + 913920c commit b853412

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
286286
return makeTransformRange(getSuccessors(), F);
287287
}
288288

289-
using pred_iterator = SILSuccessorIterator;
289+
using pred_iterator = SILSuccessor::pred_iterator;
290290

291291
bool pred_empty() const { return PredList == nullptr; }
292292
pred_iterator pred_begin() const { return pred_iterator(PredList); }

include/swift/SIL/SILSuccessor.h

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,40 @@
1818
#include <iterator>
1919

2020
namespace swift {
21+
2122
class SILBasicBlock;
2223
class TermInst;
2324

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.
2836
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.
3238
TermInst *ContainingInst = nullptr;
3339

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.
3641
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+
4155
public:
4256
SILSuccessor() {}
4357

@@ -62,41 +76,40 @@ class SILSuccessor {
6276
// Do not copy or move these.
6377
SILSuccessor(const SILSuccessor &) = delete;
6478
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; }
8279

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;
8883

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) {}
9492

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+
};
98112
};
99-
100113

101114
} // end swift namespace
102115

lib/SIL/SILSuccessor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ void SILSuccessor::operator=(SILBasicBlock *BB) {
3838
SuccessorBlock = BB;
3939
}
4040

41-
// Dereferencing the SuccIterator returns the predecessor's SILBasicBlock.
42-
SILBasicBlock *SILSuccessorIterator::operator*() {
41+
// Dereferencing the pred_iterator returns the predecessor's SILBasicBlock.
42+
SILBasicBlock *SILSuccessor::pred_iterator::operator*() {
4343
assert(Cur && "Can't deference end (or default constructed) iterator");
4444
return Cur->ContainingInst->getParent();
4545
}
4646

47-
// Dereferencing the SuccIterator returns the predecessor's SILBasicBlock.
48-
const SILBasicBlock *SILSuccessorIterator::operator*() const {
47+
// Dereferencing the pred_iterator returns the predecessor's SILBasicBlock.
48+
const SILBasicBlock *SILSuccessor::pred_iterator::operator*() const {
4949
assert(Cur && "Can't deference end (or default constructed) iterator");
5050
return Cur->ContainingInst->getParent();
5151
}

0 commit comments

Comments
 (0)