diff --git a/src/script_opt/ReachingDefs.cc b/src/script_opt/ReachingDefs.cc index a39fff8275..9271ca49ed 100644 --- a/src/script_opt/ReachingDefs.cc +++ b/src/script_opt/ReachingDefs.cc @@ -21,25 +21,21 @@ ReachingDefs::ReachingDefs(RDPtr rd) void ReachingDefs::AddRD(const DefinitionItem* di, const DefinitionPoint& dp) { - if ( HasPair(di, dp) ) + auto points = FindItem(di); + + if ( points && HasPoint(dp, *points) ) return; - CopyMapIfNeeded(); - - auto curr_defs = my_rd_map->find(di); - - if ( curr_defs == my_rd_map->end() ) + if ( ! my_rd_map ) { - auto dps = std::make_unique>(); - dps->push_back(dp); - (*my_rd_map)[di] = std::move(dps); + CopyMap(); + points = FindItem(di); } + if ( points ) + points->push_back(dp); else - { - auto& dps = curr_defs->second; - dps->push_back(dp); - } + my_rd_map->emplace(di, std::make_unique(&dp, 1)); } void ReachingDefs::AddOrFullyReplace(const DefinitionItem* di, @@ -121,14 +117,25 @@ RDPtr ReachingDefs::IntersectWithConsolidation(const RDPtr& r, bool ReachingDefs::HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const + { + auto points = FindItem(di); + return points && HasPoint(dp, *points); + } + +DefPoints* ReachingDefs::FindItem(const DefinitionItem* di) const { const auto& map = RDMap(); + auto it = map->find(di); - auto l = map->find(di); - if ( l == map->end() ) - return false; + if ( it == map->end() ) + return nullptr; - for ( const auto& l_dp : *l->second ) + return it->second.get(); + } + +bool ReachingDefs::HasPoint(const DefinitionPoint& dp, const DefPoints& dps) const + { + for ( const auto& l_dp : dps ) if ( l_dp.SameAs(dp) ) return true; @@ -142,11 +149,8 @@ void ReachingDefs::AddRDs(const std::shared_ptr& rd_m) AddRD(one_rd.first, dp); } -void ReachingDefs::CopyMapIfNeeded() +void ReachingDefs::CopyMap() { - if ( my_rd_map ) - return; - my_rd_map = std::make_shared(); auto old_const_rd_map = std::move(const_rd_map); const_rd_map = nullptr; diff --git a/src/script_opt/ReachingDefs.h b/src/script_opt/ReachingDefs.h index 9beb9ca6b6..4b3e72e740 100644 --- a/src/script_opt/ReachingDefs.h +++ b/src/script_opt/ReachingDefs.h @@ -126,6 +126,10 @@ protected: // the given definition point. bool HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const; + DefPoints* FindItem(const DefinitionItem* di) const; + + bool HasPoint(const DefinitionPoint&, const DefPoints& dps) const; + // Adds in the given RDs if we don't already have them. void AddRDs(const std::shared_ptr& rd_m); @@ -134,7 +138,9 @@ protected: // If we don't already have our own map, copy the one we're using // so that we then do. - void CopyMapIfNeeded(); + void CopyMapIfNeeded() + { if ( ! my_rd_map ) CopyMap(); } + void CopyMap(); void PrintRD(const DefinitionItem* di, const DefPoints* dp) const; void PrintRD(const DefinitionItem* di, const DefinitionPoint& dp) const;