From 8a9c6a346c53f310c310526e0a5ba2b89dd569a0 Mon Sep 17 00:00:00 2001 From: Ruoyu Qiu Date: Wed, 11 Mar 2026 15:43:43 +0800 Subject: [PATCH] [DA] refactor bounds inference in exactSIVtest and exactRDIVtest (NFC) (#185719) Replaces the `SmallVector`-based approach for computing the min/max of affine domain bounds with `GetMaxOrMin` lambda returning `std::optional` for better readability. Previously, the code allocated a `SmallVector` to collect valid bounds and relied on `smax(front(), back())` to handle the single-element case, which may cause misunderstanding. --------- Signed-off-by: Ruoyu Qiu --- llvm/lib/Analysis/DependenceAnalysis.cpp | 50 +++++++++++++----------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp index ab8a43df2f25..202e8c725863 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -1703,26 +1703,28 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, auto [TL0, TU0] = inferDomainOfAffine(TB, TX, UM); auto [TL1, TU1] = inferDomainOfAffine(TA, TY, UM); - auto CreateVec = [](const OverflowSafeSignedAPInt &V0, - const OverflowSafeSignedAPInt &V1) { - SmallVector Vec; + auto GetMaxOrMin = [](const OverflowSafeSignedAPInt &V0, + const OverflowSafeSignedAPInt &V1, + bool IsMin) -> std::optional { + if (V0 && V1) + return IsMin ? APIntOps::smin(*V0, *V1) : APIntOps::smax(*V0, *V1); if (V0) - Vec.push_back(*V0); + return *V0; if (V1) - Vec.push_back(*V1); - return Vec; + return *V1; + return std::nullopt; }; - SmallVector TLVec = CreateVec(TL0, TL1); - SmallVector TUVec = CreateVec(TU0, TU1); - LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n"); LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n"); - if (TLVec.empty() || TUVec.empty()) + std::optional OptTL = GetMaxOrMin(TL0, TL1, false); + std::optional OptTU = GetMaxOrMin(TU0, TU1, true); + if (!OptTL || !OptTU) return false; - TL = APIntOps::smax(TLVec.front(), TLVec.back()); - TU = APIntOps::smin(TUVec.front(), TUVec.back()); + + TL = std::move(*OptTL); + TU = std::move(*OptTU); LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n"); LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n"); @@ -2088,23 +2090,25 @@ bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n"); LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n"); - auto CreateVec = [](const OverflowSafeSignedAPInt &V0, - const OverflowSafeSignedAPInt &V1) { - SmallVector Vec; + auto GetMaxOrMin = [](const OverflowSafeSignedAPInt &V0, + const OverflowSafeSignedAPInt &V1, + bool IsMin) -> std::optional { + if (V0 && V1) + return IsMin ? APIntOps::smin(*V0, *V1) : APIntOps::smax(*V0, *V1); if (V0) - Vec.push_back(*V0); + return *V0; if (V1) - Vec.push_back(*V1); - return Vec; + return *V1; + return std::nullopt; }; - SmallVector TLVec = CreateVec(TL0, TL1); - SmallVector TUVec = CreateVec(TU0, TU1); - if (TLVec.empty() || TUVec.empty()) + std::optional OptTL = GetMaxOrMin(TL0, TL1, false); + std::optional OptTU = GetMaxOrMin(TU0, TU1, true); + if (!OptTL || !OptTU) return false; - TL = APIntOps::smax(TLVec.front(), TLVec.back()); - TU = APIntOps::smin(TUVec.front(), TUVec.back()); + TL = std::move(*OptTL); + TU = std::move(*OptTU); LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n"); LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");