diff options
author | Nikita Popov <npopov@redhat.com> | 2024-06-25 09:23:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-25 09:23:24 +0200 |
commit | 174f80c6030f9bc96df6ae8daeb4d6bce3f36fbb (patch) | |
tree | a9b4c0a445bbf43eff652bbe04947227ab8ceaa9 | |
parent | 8153773b23032177546944ec2524dce131b8a46e (diff) | |
download | llvm-174f80c6030f9bc96df6ae8daeb4d6bce3f36fbb.zip llvm-174f80c6030f9bc96df6ae8daeb4d6bce3f36fbb.tar.gz llvm-174f80c6030f9bc96df6ae8daeb4d6bce3f36fbb.tar.bz2 |
[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.
-rw-r--r-- | llvm/include/llvm/Support/GenericDomTreeConstruction.h | 21 |
1 files changed, 5 insertions, 16 deletions
diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h index 401cc4e..57cbe99 100644 --- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h +++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h @@ -180,15 +180,17 @@ struct SemiNCAInfo { unsigned AttachToNum, const NodeOrderMap *SuccOrder = nullptr) { assert(V); - SmallVector<NodePtr, 64> WorkList = {V}; + SmallVector<std::pair<NodePtr, unsigned>, 64> WorkList = {{V, AttachToNum}}; NodeToInfo[V].Parent = AttachToNum; while (!WorkList.empty()) { - const NodePtr BB = WorkList.pop_back_val(); + const auto [BB, ParentNum] = WorkList.pop_back_val(); auto &BBInfo = NodeToInfo[BB]; + BBInfo.ReverseChildren.push_back(ParentNum); // Visited nodes always have positive DFS numbers. if (BBInfo.DFSNum != 0) continue; + BBInfo.Parent = ParentNum; BBInfo.DFSNum = BBInfo.Semi = BBInfo.Label = ++LastNum; NumToNode.push_back(BB); @@ -201,22 +203,9 @@ struct SemiNCAInfo { }); for (const NodePtr Succ : Successors) { - const auto SIT = NodeToInfo.find(Succ); - // Don't visit nodes more than once but remember to collect - // ReverseChildren. - if (SIT != NodeToInfo.end() && SIT->second.DFSNum != 0) { - if (Succ != BB) SIT->second.ReverseChildren.push_back(LastNum); - continue; - } - if (!Condition(BB, Succ)) continue; - // It's fine to add Succ to the map, because we know that it will be - // visited later. - auto &SuccInfo = NodeToInfo[Succ]; - WorkList.push_back(Succ); - SuccInfo.Parent = LastNum; - SuccInfo.ReverseChildren.push_back(LastNum); + WorkList.push_back({Succ, LastNum}); } } |