From af760942e7d9d215a51cdb6d18993cdead70c9f6 Mon Sep 17 00:00:00 2001 From: Pietro Fezzardi Date: Tue, 14 Jun 2022 12:06:39 +0200 Subject: [PATCH] DLATypeSystem: add API to move edge target --- .../DataLayoutAnalysis/DLATypeSystem.h | 9 ++-- lib/DataLayoutAnalysis/DLATypeSystem.cpp | 44 +++++++++---------- .../DLAComputeNonInterferingComponents.cpp | 2 +- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/include/revng-c/DataLayoutAnalysis/DLATypeSystem.h b/include/revng-c/DataLayoutAnalysis/DLATypeSystem.h index fca7bc092..525c02fdd 100644 --- a/include/revng-c/DataLayoutAnalysis/DLATypeSystem.h +++ b/include/revng-c/DataLayoutAnalysis/DLATypeSystem.h @@ -119,6 +119,7 @@ struct LayoutTypeSystemNode { const uint64_t ID = 0ULL; using Link = std::pair; using NeighborsSet = std::set; + using NeighborIterator = NeighborsSet::iterator; NeighborsSet Successors{}; NeighborsSet Predecessors{}; uint64_t Size{}; @@ -287,10 +288,10 @@ public: void removeNode(LayoutTypeSystemNode *N); - void moveEdge(LayoutTypeSystemNode *OldSrc, - LayoutTypeSystemNode *NewSrc, - LayoutTypeSystemNode::NeighborsSet::iterator EdgeIt, - int64_t OffsetToSum); + void moveEdgeSource(LayoutTypeSystemNode *OldSrc, + LayoutTypeSystemNode *NewSrc, + LayoutTypeSystemNode::NeighborIterator EdgeIt, + int64_t OffsetToSum); private: uint64_t NID = 0ULL; diff --git a/lib/DataLayoutAnalysis/DLATypeSystem.cpp b/lib/DataLayoutAnalysis/DLATypeSystem.cpp index 82c0c1081..931b1ac94 100644 --- a/lib/DataLayoutAnalysis/DLATypeSystem.cpp +++ b/lib/DataLayoutAnalysis/DLATypeSystem.cpp @@ -321,47 +321,47 @@ void LayoutTypeSystem::removeNode(LayoutTypeSystemNode *ToRemove) { NodeAllocator.Deallocate(ToRemove); } -using NeighborIterator = LayoutTypeSystemNode::NeighborsSet::iterator; +using NeighborIterator = LayoutTypeSystemNode::NeighborIterator; -static void moveEdgeWithoutSumming(LayoutTypeSystemNode *OldSrc, - LayoutTypeSystemNode *NewSrc, - NeighborIterator EdgeIt) { - // First, move successor edge from OldSrc to NewSrc - auto SuccHandle = OldSrc->Successors.extract(EdgeIt); - revng_assert(not SuccHandle.empty()); - NewSrc->Successors.insert(std::move(SuccHandle)); - - // Then, move predecessor edge from OldSrc to NewSrc +static void moveEdgeSourceWithoutSumming(LayoutTypeSystemNode *OldSrc, + LayoutTypeSystemNode *NewSrc, + NeighborIterator EdgeIt) { + // First, move the predecessor edge from OldSrc to NewSrc. LayoutTypeSystemNode *Tgt = EdgeIt->first; auto PredHandle = Tgt->Predecessors.extract({ OldSrc, EdgeIt->second }); revng_assert(not PredHandle.empty()); PredHandle.value().first = NewSrc; Tgt->Predecessors.insert(std::move(PredHandle)); + + // Then, move the successor edge from OldSrc to NewSrc + auto SuccHandle = OldSrc->Successors.extract(EdgeIt); + revng_assert(not SuccHandle.empty()); + NewSrc->Successors.insert(std::move(SuccHandle)); } -void LayoutTypeSystem::moveEdge(LayoutTypeSystemNode *OldSrc, - LayoutTypeSystemNode *NewSrc, - NeighborIterator EdgeIt, - int64_t OffsetToSum) { +void LayoutTypeSystem::moveEdgeSource(LayoutTypeSystemNode *OldSrc, + LayoutTypeSystemNode *NewSrc, + NeighborIterator EdgeIt, + int64_t OffsetToSum) { if (not OldSrc or not NewSrc) return; if (not OffsetToSum) - return moveEdgeWithoutSumming(OldSrc, NewSrc, EdgeIt); + return moveEdgeSourceWithoutSumming(OldSrc, NewSrc, EdgeIt); LayoutTypeSystemNode *Tgt = EdgeIt->first; - // First, move successor edges from OldSrc to NewSrc + // Erase info in Tgt that represent the fact that OldSrc was a predecessor. + bool Erased = Tgt->Predecessors.erase({ OldSrc, EdgeIt->second }); + revng_assert(Erased); + + // Extract the successor edge to be moved from OldSrc to NewSrc auto OldSuccHandle = OldSrc->Successors.extract(EdgeIt); revng_assert(not OldSuccHandle.empty()); // Add new instance links with adjusted offsets from NewSrc to Tgt. - // Using the addInstanceLink methods already marks injects NewSrc among the - // predecessors of Tgt, so after this we only need to remove OldSrc from - // Tgt's predecessors and we're done. - const TypeLinkTag *EdgeTag = OldSuccHandle.value().second; switch (EdgeTag->getKind()) { @@ -377,10 +377,6 @@ void LayoutTypeSystem::moveEdge(LayoutTypeSystemNode *OldSrc, default: revng_unreachable("unexpected edge kind"); } - - // Then, remove all the remaining info in Tgt that represent the fact that - // OldSrc was a predecessor. - auto PredHandle = Tgt->Predecessors.extract({ OldSrc, EdgeIt->second }); } static Logger<> VerifyDLALog("dla-verify-strict"); diff --git a/lib/DataLayoutAnalysis/Middleend/DLAComputeNonInterferingComponents.cpp b/lib/DataLayoutAnalysis/Middleend/DLAComputeNonInterferingComponents.cpp index 18459e12d..eedf8494f 100644 --- a/lib/DataLayoutAnalysis/Middleend/DLAComputeNonInterferingComponents.cpp +++ b/lib/DataLayoutAnalysis/Middleend/DLAComputeNonInterferingComponents.cpp @@ -224,7 +224,7 @@ bool ComputeNonInterferingComponents::runOnTypeSystem(LayoutTypeSystem &TS) { using llvm::iterator_range; auto OrderedChildRange = iterator_range(C.StartChildIt, C.EndChildIt); for (auto &OrderedChild : OrderedChildRange) - TS.moveEdge(N, New, OrderedChild.ChildIt, -C.StartByte); + TS.moveEdgeSource(N, New, OrderedChild.ChildIt, -C.StartByte); // Add a link between N and the New node representing the component. // The component is at offset C.StartByte inside N.