Skip to content

Commit 80792ad

Browse files
committed
Update cache and clear the cache in non-const func
1 parent dd45e4b commit 80792ad

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

llvm/include/llvm/ADT/GenericCycleImpl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ bool GenericCycle<ContextT>::contains(const GenericCycle *C) const {
4747
template <typename ContextT>
4848
void GenericCycle<ContextT>::getExitBlocks(
4949
SmallVectorImpl<BlockT *> &TmpStorage) const {
50-
if (!ExitBlocksCache->empty()) {
51-
TmpStorage = *ExitBlocksCache;
50+
if (!ExitBlocksCache.empty()) {
51+
TmpStorage = ExitBlocksCache;
5252
return;
5353
}
5454

@@ -70,7 +70,7 @@ void GenericCycle<ContextT>::getExitBlocks(
7070

7171
TmpStorage.resize(NumExitBlocks);
7272
}
73-
ExitBlocksCache->append(TmpStorage.begin(), TmpStorage.end());
73+
ExitBlocksCache.append(TmpStorage.begin(), TmpStorage.end());
7474
}
7575

7676
template <typename ContextT>
@@ -304,6 +304,8 @@ void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
304304
for (auto &It : BlockMapTopLevel)
305305
if (It.second == Child)
306306
It.second = NewParent;
307+
NewParent->clearCache();
308+
Child->clearCache();
307309
}
308310

309311
template <typename ContextT>
@@ -322,6 +324,7 @@ void GenericCycleInfo<ContextT>::addBlockToCycle(BlockT *Block, CycleT *Cycle) {
322324
}
323325

324326
BlockMapTopLevel.try_emplace(Block, Cycle);
327+
Cycle->clearCache();
325328
}
326329

327330
/// \brief Main function of the cycle info computations.

llvm/include/llvm/ADT/GenericCycleInfo.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,34 @@ template <typename ContextT> class GenericCycle {
7575
unsigned Depth = 0;
7676

7777
/// Cache for the results of GetExitBlocks
78-
std::unique_ptr<SmallVector<BlockT *, 4>> ExitBlocksCache;
78+
mutable SmallVector<BlockT *, 4> ExitBlocksCache;
7979

8080
void clear() {
8181
Entries.clear();
8282
Children.clear();
8383
Blocks.clear();
8484
Depth = 0;
8585
ParentCycle = nullptr;
86-
ExitBlocksCache->clear();
86+
clearCache();
8787
}
8888

89-
void appendEntry(BlockT *Block) { Entries.push_back(Block); }
90-
void appendBlock(BlockT *Block) { Blocks.insert(Block); }
89+
void appendEntry(BlockT *Block) {
90+
Entries.push_back(Block);
91+
clearCache();
92+
}
93+
94+
void appendBlock(BlockT *Block) {
95+
Blocks.insert(Block);
96+
clearCache();
97+
}
9198

9299
GenericCycle(const GenericCycle &) = delete;
93100
GenericCycle &operator=(const GenericCycle &) = delete;
94101
GenericCycle(GenericCycle &&Rhs) = delete;
95102
GenericCycle &operator=(GenericCycle &&Rhs) = delete;
96103

97104
public:
98-
GenericCycle()
99-
: ExitBlocksCache(std::make_unique<SmallVector<BlockT *, 4>>()) {};
105+
GenericCycle() = default;
100106

101107
/// \brief Whether the cycle is a natural loop.
102108
bool isReducible() const { return Entries.size() == 1; }
@@ -107,6 +113,13 @@ template <typename ContextT> class GenericCycle {
107113
return Entries;
108114
}
109115

116+
/// Clear the cache of the cycle.
117+
/// This should be run in all non-const function in GenericCycle
118+
/// and GenericCycleInfo.
119+
void clearCache() const {
120+
ExitBlocksCache.clear();
121+
}
122+
110123
/// \brief Return whether \p Block is an entry block of the cycle.
111124
bool isEntry(const BlockT *Block) const {
112125
return is_contained(Entries, Block);
@@ -117,6 +130,7 @@ template <typename ContextT> class GenericCycle {
117130
assert(contains(Block));
118131
Entries.clear();
119132
Entries.push_back(Block);
133+
clearCache();
120134
}
121135

122136
/// \brief Return whether \p Block is contained in the cycle.
@@ -129,7 +143,12 @@ template <typename ContextT> class GenericCycle {
129143
bool contains(const GenericCycle *C) const;
130144

131145
const GenericCycle *getParentCycle() const { return ParentCycle; }
132-
GenericCycle *getParentCycle() { return ParentCycle; }
146+
147+
GenericCycle *getParentCycle() {
148+
clearCache();
149+
return ParentCycle;
150+
}
151+
133152
unsigned getDepth() const { return Depth; }
134153

135154
/// Return all of the successor blocks of this cycle.

0 commit comments

Comments
 (0)