/*========================== begin_copyright_notice ============================ Copyright (C) 2024 Intel Corporation SPDX-License-Identifier: MIT ============================= end_copyright_notice ===========================*/ /*========================== begin_copyright_notice ============================ Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information. SPDX-License-Identifier: Apache-2.0 with LLVM-exception ============================= end_copyright_notice ===========================*/ From dfa7eaff486b1ec7778069cd22ffc440df208717 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 28 Dec 2022 00:09:52 +0000 Subject: [PATCH 5/5] [SCEV] Properly clean up duplicated FoldCacheUser ID entries. The current code did not properly handled duplicated FoldCacheUser ID entries when overwriting an existing entry in the FoldCache. This triggered verification failures reported by @uabelho and #59721. The patch fixes that by removing stale IDs when overwriting an existing entry in the cache. Fixes #59721. --- llvm/lib/Analysis/ScalarEvolution.cpp | 37 +++++++++++++++++++-------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index ba49e12f958c..cf76ab7efb65 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -1590,6 +1590,29 @@ static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, return APInt(BitWidth, 0); } +static void insertFoldCacheEntry( + const ScalarEvolution::FoldID &ID, const SCEV *S, + DenseMap &FoldCache, + DenseMap> + &FoldCacheUser) { + auto I = FoldCache.insert({ID, S}); + if (!I.second) { + // Remove FoldCacheUser entry for ID when replacing an existing FoldCache + // entry. + auto &UserIDs = FoldCacheUser[I.first->second]; + assert(count(UserIDs, ID) == 1 && "unexpected duplicates in UserIDs"); + for (unsigned I = 0; I != UserIDs.size(); ++I) + if (UserIDs[I] == ID) { + std::swap(UserIDs[I], UserIDs.back()); + break; + } + UserIDs.pop_back(); + I.first->second = S; + } + auto R = FoldCacheUser.insert({S, {}}); + R.first->second.push_back(ID); +} + const SCEV * ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && @@ -1608,11 +1631,8 @@ ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { return Iter->second; const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth); - if (!isa(S)) { - FoldCache.insert({ID, S}); - auto R = FoldCacheUser.insert({S, {}}); - R.first->second.push_back(ID); - } + if (!isa(S)) + insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); return S; } @@ -1934,11 +1954,8 @@ ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { return Iter->second; const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth); - if (!isa(S)) { - FoldCache.insert({ID, S}); - auto R = FoldCacheUser.insert({S, {}}); - R.first->second.push_back(ID); - } + if (!isa(S)) + insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); return S; } -- 2.34.1