Skip to content

Commit 174f80c

Browse files
authored
[DomTree] Avoid duplicate hash lookups in runDFS() (NFCI) (#96460)
runDFS() currently performs three hash table lookups. One in the main loop, one when checking whether a successor has already been visited and another when adding parent and reverse children to the successor. We can avoid the two additional lookups by making the parent number part of the stack, and then making the parent / reverse children update part of the main loop. The main loop already has a check for already visited nodes, so we don't have to check this in advance -- we can simply push the node to the worklist and skip it later.
1 parent 8153773 commit 174f80c

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

llvm/include/llvm/Support/GenericDomTreeConstruction.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,17 @@ struct SemiNCAInfo {
180180
unsigned AttachToNum,
181181
const NodeOrderMap *SuccOrder = nullptr) {
182182
assert(V);
183-
SmallVector<NodePtr, 64> WorkList = {V};
183+
SmallVector<std::pair<NodePtr, unsigned>, 64> WorkList = {{V, AttachToNum}};
184184
NodeToInfo[V].Parent = AttachToNum;
185185

186186
while (!WorkList.empty()) {
187-
const NodePtr BB = WorkList.pop_back_val();
187+
const auto [BB, ParentNum] = WorkList.pop_back_val();
188188
auto &BBInfo = NodeToInfo[BB];
189+
BBInfo.ReverseChildren.push_back(ParentNum);
189190

190191
// Visited nodes always have positive DFS numbers.
191192
if (BBInfo.DFSNum != 0) continue;
193+
BBInfo.Parent = ParentNum;
192194
BBInfo.DFSNum = BBInfo.Semi = BBInfo.Label = ++LastNum;
193195
NumToNode.push_back(BB);
194196

@@ -201,22 +203,9 @@ struct SemiNCAInfo {
201203
});
202204

203205
for (const NodePtr Succ : Successors) {
204-
const auto SIT = NodeToInfo.find(Succ);
205-
// Don't visit nodes more than once but remember to collect
206-
// ReverseChildren.
207-
if (SIT != NodeToInfo.end() && SIT->second.DFSNum != 0) {
208-
if (Succ != BB) SIT->second.ReverseChildren.push_back(LastNum);
209-
continue;
210-
}
211-
212206
if (!Condition(BB, Succ)) continue;
213207

214-
// It's fine to add Succ to the map, because we know that it will be
215-
// visited later.
216-
auto &SuccInfo = NodeToInfo[Succ];
217-
WorkList.push_back(Succ);
218-
SuccInfo.Parent = LastNum;
219-
SuccInfo.ReverseChildren.push_back(LastNum);
208+
WorkList.push_back({Succ, LastNum});
220209
}
221210
}
222211

0 commit comments

Comments
 (0)