/*========================== 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 85a1763970eeac7e3410ef3b12d6abb84b9324c4 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 14 Dec 2022 11:59:19 +0000 Subject: [PATCH 4/5] [SCEV] Cache folded SExt SCEV expressions. Use FoldID to cache SignExtendExprs that get folded to a different SCEV. Depends on D137505. Reviewed By: mkazantsev Differential Revision: https://reviews.llvm.org/D137849 --- llvm/include/llvm/Analysis/ScalarEvolution.h | 2 ++ llvm/lib/Analysis/ScalarEvolution.cpp | 25 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h index c8f9a871a5a7..fbc393ebb271 100644 --- a/llvm/include/llvm/Analysis/ScalarEvolution.h +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h @@ -565,6 +565,8 @@ public: const SCEV *getZeroExtendExprImpl(const SCEV *Op, Type *Ty, unsigned Depth = 0); const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0); + const SCEV *getSignExtendExprImpl(const SCEV *Op, Type *Ty, + unsigned Depth = 0); const SCEV *getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty); const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty); const SCEV *getAddExpr(SmallVectorImpl &Ops, diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 847d72828570..ba49e12f958c 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -1925,6 +1925,31 @@ ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); Ty = getEffectiveSCEVType(Ty); + FoldID ID; + ID.addInteger(scSignExtend); + ID.addPointer(Op); + ID.addPointer(Ty); + auto Iter = FoldCache.find(ID); + if (Iter != FoldCache.end()) + 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); + } + return S; +} + +const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty, + unsigned Depth) { + assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && + "This is not an extending conversion!"); + assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!"); + assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); + Ty = getEffectiveSCEVType(Ty); + // Fold if the operand is constant. if (const SCEVConstant *SC = dyn_cast(Op)) return getConstant( -- 2.34.1