Skip to content

Commit 9be60dd

Browse files
authored
Merge pull request swiftlang#15684 from gottesmm/blotmapvector_cleanups
2 parents ff26c89 + c59a1e5 commit 9be60dd

File tree

7 files changed

+122
-129
lines changed

7 files changed

+122
-129
lines changed

include/swift/Basic/BlotMapVector.h

Lines changed: 102 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -31,120 +31,113 @@ template <typename KeyT, typename ValueT,
3131
typename MapT = llvm::DenseMap<KeyT, size_t>,
3232
typename VectorT = std::vector<Optional<std::pair<KeyT, ValueT>>>>
3333
class BlotMapVector {
34-
/// Map keys to indices in Vector.
35-
MapT Map;
34+
/// Map keys to indices in Vector.
35+
MapT Map;
3636

37-
/// Keys and values.
38-
VectorT Vector;
37+
/// Keys and values.
38+
VectorT Vector;
3939

40-
public:
41-
using iterator = typename VectorT::iterator;
42-
using const_iterator = typename VectorT::const_iterator;
43-
using key_type = KeyT;
44-
using mapped_type = ValueT;
45-
46-
iterator begin() { return Vector.begin(); }
47-
iterator end() { return Vector.end(); }
48-
const_iterator begin() const { return Vector.begin(); }
49-
const_iterator end() const { return Vector.end(); }
50-
51-
iterator_range<iterator> getItems() {
52-
return swift::make_range(begin(), end());
53-
}
54-
iterator_range<const_iterator> getItems() const {
55-
return swift::make_range(begin(), end());
56-
}
57-
58-
ValueT &operator[](const KeyT &Arg) {
59-
auto Pair = Map.insert(std::make_pair(Arg, size_t(0)));
60-
if (Pair.second) {
61-
size_t Num = Vector.size();
62-
Pair.first->second = Num;
63-
Vector.push_back({std::make_pair(Arg, ValueT())});
64-
return (*Vector[Num]).second;
65-
}
66-
return Vector[Pair.first->second].getValue().second;
67-
}
68-
69-
std::pair<iterator, bool>
70-
insert(const std::pair<KeyT, ValueT> &InsertPair) {
71-
auto Pair = Map.insert(std::make_pair(InsertPair.first, size_t(0)));
72-
if (Pair.second) {
73-
size_t Num = Vector.size();
74-
Pair.first->second = Num;
75-
Vector.push_back(InsertPair);
76-
return std::make_pair(Vector.begin() + Num, true);
77-
}
78-
return std::make_pair(Vector.begin() + Pair.first->second, false);
79-
}
80-
81-
iterator find(const KeyT &Key) {
82-
typename MapT::iterator It = Map.find(Key);
83-
if (It == Map.end()) return Vector.end();
84-
auto Iter = Vector.begin() + It->second;
85-
if (!Iter->hasValue())
86-
return Vector.end();
87-
return Iter;
88-
}
89-
90-
const_iterator find(const KeyT &Key) const {
91-
return const_cast<BlotMapVector &>(*this).find(Key);
92-
}
93-
94-
/// Eliminate the element at `Key`. Instead of removing the element from the
95-
/// vector, just zero out the key in the vector. This leaves iterators
96-
/// intact, but clients must be prepared for zeroed-out keys when iterating.
97-
///
98-
/// Return true if the element was erased.
99-
bool erase(const KeyT &Key) { return blot(Key); }
100-
101-
/// Eliminate the element at the given iterator. Instead of removing the
102-
/// element from the vector, just zero out the key in the vector. This
103-
/// leaves iterators intact, but clients must be prepared for zeroed-out
104-
/// keys when iterating.
105-
void erase(iterator I) { erase((*I)->first); }
106-
107-
/// Eliminate the element at `Key`. Instead of removing the element from the
108-
/// vector, it just zeros out the key in the vector. This leaves iterators
109-
/// intact, but clients must be prepared for zeroed-out keys when iterating.
110-
///
111-
/// Return true if the element was found and erased.
112-
bool blot(const KeyT &Key) {
113-
typename MapT::iterator It = Map.find(Key);
114-
if (It == Map.end())
115-
return false;
116-
Vector[It->second] = None;
117-
Map.erase(It);
118-
return true;
119-
}
120-
121-
void clear() {
122-
Map.clear();
123-
Vector.clear();
40+
public:
41+
using iterator = typename VectorT::iterator;
42+
using const_iterator = typename VectorT::const_iterator;
43+
using key_type = KeyT;
44+
using mapped_type = ValueT;
45+
46+
iterator begin() { return Vector.begin(); }
47+
iterator end() { return Vector.end(); }
48+
const_iterator begin() const { return Vector.begin(); }
49+
const_iterator end() const { return Vector.end(); }
50+
51+
iterator_range<iterator> getItems() {
52+
return swift::make_range(begin(), end());
53+
}
54+
iterator_range<const_iterator> getItems() const {
55+
return swift::make_range(begin(), end());
56+
}
57+
58+
ValueT &operator[](const KeyT &Arg) {
59+
auto Pair = Map.insert(std::make_pair(Arg, size_t(0)));
60+
if (Pair.second) {
61+
size_t Num = Vector.size();
62+
Pair.first->second = Num;
63+
Vector.push_back({std::make_pair(Arg, ValueT())});
64+
return (*Vector[Num]).second;
12465
}
125-
126-
unsigned size() const { return Map.size(); }
127-
128-
ValueT lookup(const KeyT &Val) const {
129-
auto Iter = Map.find(Val);
130-
if (Iter == Map.end())
131-
return ValueT();
132-
auto &P = Vector[Iter->second];
133-
if (!P.hasValue())
134-
return ValueT();
135-
return P->second;
66+
return Vector[Pair.first->second].getValue().second;
67+
}
68+
69+
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &InsertPair) {
70+
auto Pair = Map.insert(std::make_pair(InsertPair.first, size_t(0)));
71+
if (Pair.second) {
72+
size_t Num = Vector.size();
73+
Pair.first->second = Num;
74+
Vector.push_back(InsertPair);
75+
return std::make_pair(Vector.begin() + Num, true);
13676
}
77+
return std::make_pair(Vector.begin() + Pair.first->second, false);
78+
}
79+
80+
iterator find(const KeyT &Key) {
81+
typename MapT::iterator It = Map.find(Key);
82+
if (It == Map.end())
83+
return Vector.end();
84+
auto Iter = Vector.begin() + It->second;
85+
if (!Iter->hasValue())
86+
return Vector.end();
87+
return Iter;
88+
}
89+
90+
const_iterator find(const KeyT &Key) const {
91+
return const_cast<BlotMapVector &>(*this).find(Key);
92+
}
93+
94+
/// Eliminate the element at `Key`. Instead of removing the element from the
95+
/// vector, just zero out the key in the vector. This leaves iterators
96+
/// intact, but clients must be prepared for zeroed-out keys when iterating.
97+
///
98+
/// Return true if the element was found and erased.
99+
bool erase(const KeyT &Key) {
100+
typename MapT::iterator It = Map.find(Key);
101+
if (It == Map.end())
102+
return false;
103+
Vector[It->second] = None;
104+
Map.erase(It);
105+
return true;
106+
}
107+
108+
/// Eliminate the element at the given iterator. Instead of removing the
109+
/// element from the vector, just zero out the key in the vector. This
110+
/// leaves iterators intact, but clients must be prepared for zeroed-out
111+
/// keys when iterating.
112+
void erase(iterator I) { erase((*I)->first); }
113+
114+
void clear() {
115+
Map.clear();
116+
Vector.clear();
117+
}
118+
119+
unsigned size() const { return Map.size(); }
120+
121+
ValueT lookup(const KeyT &Val) const {
122+
auto Iter = Map.find(Val);
123+
if (Iter == Map.end())
124+
return ValueT();
125+
auto &P = Vector[Iter->second];
126+
if (!P.hasValue())
127+
return ValueT();
128+
return P->second;
129+
}
130+
131+
size_t count(const KeyT &Val) const { return Map.count(Val); }
132+
133+
bool empty() const { return Map.empty(); }
134+
};
137135

138-
size_t count(const KeyT &Val) const { return Map.count(Val); }
139-
140-
bool empty() const { return Map.empty(); }
141-
};
142-
143-
template <typename KeyT, typename ValueT, unsigned N,
144-
typename MapT = llvm::SmallDenseMap<KeyT, size_t, N>,
145-
typename VectorT =
146-
llvm::SmallVector<Optional<std::pair<KeyT, ValueT>>, N>>
147-
class SmallBlotMapVector : public BlotMapVector<KeyT, ValueT, MapT, VectorT> {
136+
template <typename KeyT, typename ValueT, unsigned N,
137+
typename MapT = llvm::SmallDenseMap<KeyT, size_t, N>,
138+
typename VectorT =
139+
llvm::SmallVector<Optional<std::pair<KeyT, ValueT>>, N>>
140+
class SmallBlotMapVector : public BlotMapVector<KeyT, ValueT, MapT, VectorT> {
148141
public:
149142
SmallBlotMapVector() {}
150143
};

lib/SILOptimizer/ARC/ARCBBState.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void ARCBBState::mergeSuccBottomUp(ARCBBState &SuccBBState) {
4848
// effect of an intersection.
4949
auto Other = SuccBBState.PtrToBottomUpState.find(RefCountedValue);
5050
if (Other == SuccBBState.PtrToBottomUpState.end()) {
51-
PtrToBottomUpState.blot(RefCountedValue);
51+
PtrToBottomUpState.erase(RefCountedValue);
5252
continue;
5353
}
5454

@@ -58,7 +58,7 @@ void ARCBBState::mergeSuccBottomUp(ARCBBState &SuccBBState) {
5858
// This has the effect of an intersection since we already checked earlier
5959
// that RefCountedValue was not blotted.
6060
if (!OtherRefCountedValue) {
61-
PtrToBottomUpState.blot(RefCountedValue);
61+
PtrToBottomUpState.erase(RefCountedValue);
6262
continue;
6363
}
6464

@@ -69,7 +69,7 @@ void ARCBBState::mergeSuccBottomUp(ARCBBState &SuccBBState) {
6969
// of instructions which together semantically act as one ref count
7070
// increment. Merge the two states together.
7171
if (!RefCountState.merge(OtherRefCountState)) {
72-
PtrToBottomUpState.blot(RefCountedValue);
72+
PtrToBottomUpState.erase(RefCountedValue);
7373
}
7474
}
7575
}
@@ -102,7 +102,7 @@ void ARCBBState::mergePredTopDown(ARCBBState &PredBBState) {
102102
// effect of an intersection.
103103
auto Other = PredBBState.PtrToTopDownState.find(RefCountedValue);
104104
if (Other == PredBBState.PtrToTopDownState.end()) {
105-
PtrToTopDownState.blot(RefCountedValue);
105+
PtrToTopDownState.erase(RefCountedValue);
106106
continue;
107107
}
108108

@@ -111,7 +111,7 @@ void ARCBBState::mergePredTopDown(ARCBBState &PredBBState) {
111111
// If the other ref count value was blotted, blot our value and continue.
112112
// This has the effect of an intersection.
113113
if (!OtherRefCountedValue) {
114-
PtrToTopDownState.blot(RefCountedValue);
114+
PtrToTopDownState.erase(RefCountedValue);
115115
continue;
116116
}
117117

@@ -124,7 +124,7 @@ void ARCBBState::mergePredTopDown(ARCBBState &PredBBState) {
124124
// ref counted value and continue.
125125
if (!RefCountState.merge(OtherRefCountState)) {
126126
DEBUG(llvm::dbgs() << "Failed to merge!\n");
127-
PtrToTopDownState.blot(RefCountedValue);
127+
PtrToTopDownState.erase(RefCountedValue);
128128
continue;
129129
}
130130
}

lib/SILOptimizer/ARC/ARCBBState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ class ARCSequenceDataflowEvaluator::ARCBBState {
103103

104104
/// Blot \p Ptr.
105105
void clearBottomUpRefCountState(SILValue Ptr) {
106-
PtrToBottomUpState.blot(Ptr);
106+
PtrToBottomUpState.erase(Ptr);
107107
}
108108

109109
/// Blot \p Ptr.
110-
void clearTopDownRefCountState(SILValue Ptr) { PtrToTopDownState.blot(Ptr); }
110+
void clearTopDownRefCountState(SILValue Ptr) { PtrToTopDownState.erase(Ptr); }
111111

112112
void clearTopDownState() { PtrToTopDownState.clear(); }
113113
void clearBottomUpState() { PtrToBottomUpState.clear(); }

lib/SILOptimizer/ARC/ARCRegionState.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ARCRegionState::mergeSuccBottomUp(ARCRegionState &SuccRegionState) {
6666
// effect of an intersection.
6767
auto Other = SuccRegionState.PtrToBottomUpState.find(RefCountedValue);
6868
if (Other == SuccRegionState.PtrToBottomUpState.end()) {
69-
PtrToBottomUpState.blot(RefCountedValue);
69+
PtrToBottomUpState.erase(RefCountedValue);
7070
continue;
7171
}
7272

@@ -76,7 +76,7 @@ void ARCRegionState::mergeSuccBottomUp(ARCRegionState &SuccRegionState) {
7676
// This has the effect of an intersection since we already checked earlier
7777
// that RefCountedValue was not blotted.
7878
if (!OtherRefCountedValue) {
79-
PtrToBottomUpState.blot(RefCountedValue);
79+
PtrToBottomUpState.erase(RefCountedValue);
8080
continue;
8181
}
8282

@@ -87,7 +87,7 @@ void ARCRegionState::mergeSuccBottomUp(ARCRegionState &SuccRegionState) {
8787
// of instructions which together semantically act as one ref count
8888
// increment. Merge the two states together.
8989
if (!RefCountState.merge(OtherRefCountState)) {
90-
PtrToBottomUpState.blot(RefCountedValue);
90+
PtrToBottomUpState.erase(RefCountedValue);
9191
}
9292
}
9393
}
@@ -123,7 +123,7 @@ void ARCRegionState::mergePredTopDown(ARCRegionState &PredRegionState) {
123123
// effect of an intersection.
124124
auto Other = PredRegionState.PtrToTopDownState.find(RefCountedValue);
125125
if (Other == PredRegionState.PtrToTopDownState.end()) {
126-
PtrToTopDownState.blot(RefCountedValue);
126+
PtrToTopDownState.erase(RefCountedValue);
127127
continue;
128128
}
129129

@@ -132,7 +132,7 @@ void ARCRegionState::mergePredTopDown(ARCRegionState &PredRegionState) {
132132
// If the other ref count value was blotted, blot our value and continue.
133133
// This has the effect of an intersection.
134134
if (!OtherRefCountedValue) {
135-
PtrToTopDownState.blot(RefCountedValue);
135+
PtrToTopDownState.erase(RefCountedValue);
136136
continue;
137137
}
138138

@@ -145,7 +145,7 @@ void ARCRegionState::mergePredTopDown(ARCRegionState &PredRegionState) {
145145
// ref counted value and continue.
146146
if (!RefCountState.merge(OtherRefCountState)) {
147147
DEBUG(llvm::dbgs() << "Failed to merge!\n");
148-
PtrToTopDownState.blot(RefCountedValue);
148+
PtrToTopDownState.erase(RefCountedValue);
149149
continue;
150150
}
151151
}

lib/SILOptimizer/ARC/ARCRegionState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ class ARCRegionState {
121121

122122
/// Blot \p Ptr.
123123
void clearBottomUpRefCountState(SILValue Ptr) {
124-
PtrToBottomUpState.blot(Ptr);
124+
PtrToBottomUpState.erase(Ptr);
125125
}
126126

127127
/// Blot \p Ptr.
128-
void clearTopDownRefCountState(SILValue Ptr) { PtrToTopDownState.blot(Ptr); }
128+
void clearTopDownRefCountState(SILValue Ptr) { PtrToTopDownState.erase(Ptr); }
129129

130130
void clearTopDownState() { PtrToTopDownState.clear(); }
131131
void clearBottomUpState() { PtrToBottomUpState.clear(); }

lib/SILOptimizer/ARC/ARCSequenceOpts.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ bool ARCPairingContext::performMatching(
9595
MatchedPair |= Builder.matchedPair();
9696
auto &Set = Builder.getResult();
9797
for (auto *I : Set.Increments)
98-
IncToDecStateMap.blot(I);
98+
IncToDecStateMap.erase(I);
9999
for (auto *I : Set.Decrements)
100-
DecToIncStateMap.blot(I);
100+
DecToIncStateMap.erase(I);
101101

102102
// Add the Set to the callback. *NOTE* No instruction destruction can
103103
// happen here since we may remove instructions that are insertion points

lib/SILOptimizer/Transforms/SILCodeMotion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,10 @@ void BBEnumTagDataflowState::mergePredecessorStates() {
506506
} while (PI != PE);
507507

508508
for (unsigned ID : CurBBValuesToBlot) {
509-
ValueToCaseMap.blot(ID);
509+
ValueToCaseMap.erase(ID);
510510
}
511511
for (unsigned ID : PredBBValuesToBlot) {
512-
EnumToEnumBBCaseListMap.blot(ID);
512+
EnumToEnumBBCaseListMap.erase(ID);
513513
}
514514
}
515515

0 commit comments

Comments
 (0)