From 34a16392fa4f131a4329c4ffb5f62a199d4fcd66 Mon Sep 17 00:00:00 2001 From: Ryotaro Kasuga Date: Tue, 7 Apr 2026 00:54:34 +0900 Subject: [PATCH] [DA] Use SmallVector instead of raw new/delete (NFC) (#190586) Some functions used `new`/`delete` to allocate/free arrays. To avoid memory leaks, it would be better to avoid using raw pointers. This patch replaces the use of them with `SmallVector`. --- .../llvm/Analysis/DependenceAnalysis.h | 34 ++++++----- llvm/lib/Analysis/DependenceAnalysis.cpp | 60 ++++++++++--------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h index a52f7bf09cc4..eed89878bc4e 100644 --- a/llvm/include/llvm/Analysis/DependenceAnalysis.h +++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h @@ -612,8 +612,9 @@ private: /// collectCoeffInfo - Walks through the subscript, collecting each /// coefficient, the associated loop bounds, and recording its positive and /// negative parts for later use. - CoefficientInfo *collectCoeffInfo(const SCEV *Subscript, bool SrcFlag, - const SCEV *&Constant) const; + void collectCoeffInfo(const SCEV *Subscript, bool SrcFlag, + const SCEV *&Constant, + SmallVectorImpl &CI) const; /// Given \p Expr of the form /// @@ -643,46 +644,47 @@ private: /// getLowerBound - Looks through all the bounds info and /// computes the lower bound given the current direction settings /// at each level. - const SCEV *getLowerBound(BoundInfo *Bound) const; + const SCEV *getLowerBound(ArrayRef Bound) const; /// getUpperBound - Looks through all the bounds info and /// computes the upper bound given the current direction settings /// at each level. - const SCEV *getUpperBound(BoundInfo *Bound) const; + const SCEV *getUpperBound(ArrayRef Bound) const; /// exploreDirections - Hierarchically expands the direction vector /// search space, combining the directions of discovered dependences /// in the DirSet field of Bound. Returns the number of distinct /// dependences discovered. If the dependence is disproved, /// it will return 0. - unsigned exploreDirections(unsigned Level, CoefficientInfo *A, - CoefficientInfo *B, BoundInfo *Bound, + unsigned exploreDirections(unsigned Level, ArrayRef A, + ArrayRef B, + MutableArrayRef Bound, const SmallBitVector &Loops, unsigned &DepthExpanded, const SCEV *Delta) const; /// testBounds - Returns true iff the current bounds are plausible. - bool testBounds(unsigned char DirKind, unsigned Level, BoundInfo *Bound, - const SCEV *Delta) const; + bool testBounds(unsigned char DirKind, unsigned Level, + MutableArrayRef Bound, const SCEV *Delta) const; /// findBoundsALL - Computes the upper and lower bounds for level K /// using the * direction. Records them in Bound. - void findBoundsALL(CoefficientInfo *A, CoefficientInfo *B, BoundInfo *Bound, - unsigned K) const; + void findBoundsALL(ArrayRef A, ArrayRef B, + MutableArrayRef Bound, unsigned K) const; /// findBoundsLT - Computes the upper and lower bounds for level K /// using the < direction. Records them in Bound. - void findBoundsLT(CoefficientInfo *A, CoefficientInfo *B, BoundInfo *Bound, - unsigned K) const; + void findBoundsLT(ArrayRef A, ArrayRef B, + MutableArrayRef Bound, unsigned K) const; /// findBoundsGT - Computes the upper and lower bounds for level K /// using the > direction. Records them in Bound. - void findBoundsGT(CoefficientInfo *A, CoefficientInfo *B, BoundInfo *Bound, - unsigned K) const; + void findBoundsGT(ArrayRef A, ArrayRef B, + MutableArrayRef Bound, unsigned K) const; /// findBoundsEQ - Computes the upper and lower bounds for level K /// using the = direction. Records them in Bound. - void findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B, BoundInfo *Bound, - unsigned K) const; + void findBoundsEQ(ArrayRef A, ArrayRef B, + MutableArrayRef Bound, unsigned K) const; /// Given a linear access function, tries to recover subscripts /// for each dimension of the array element access. diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp index ab09b2095dcc..0bfdd60e66a2 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -2246,11 +2246,13 @@ bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst, ++BanerjeeApplications; LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n'); const SCEV *A0; - CoefficientInfo *A = collectCoeffInfo(Src, true, A0); + SmallVector A; + collectCoeffInfo(Src, true, A0, A); LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n'); const SCEV *B0; - CoefficientInfo *B = collectCoeffInfo(Dst, false, B0); - BoundInfo *Bound = new BoundInfo[MaxLevels + 1]; + SmallVector B; + collectCoeffInfo(Dst, false, B0, B); + SmallVector Bound(MaxLevels + 1); const SCEV *Delta = SE->getMinusSCEV(B0, A0); LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n'); @@ -2305,9 +2307,6 @@ bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst, ++BanerjeeIndependence; Disproved = true; } - delete[] Bound; - delete[] A; - delete[] B; return Disproved; } @@ -2316,11 +2315,10 @@ bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst, // in the DirSet field of Bound. Returns the number of distinct // dependences discovered. If the dependence is disproved, // it will return 0. -unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A, - CoefficientInfo *B, BoundInfo *Bound, - const SmallBitVector &Loops, - unsigned &DepthExpanded, - const SCEV *Delta) const { +unsigned DependenceInfo::exploreDirections( + unsigned Level, ArrayRef A, ArrayRef B, + MutableArrayRef Bound, const SmallBitVector &Loops, + unsigned &DepthExpanded, const SCEV *Delta) const { // This algorithm has worst case complexity of O(3^n), where 'n' is the number // of common loop levels. To avoid excessive compile-time, pessimize all the // results and immediately return when the number of common levels is beyond @@ -2434,7 +2432,8 @@ unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A, // Returns true iff the current bounds are plausible. bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level, - BoundInfo *Bound, const SCEV *Delta) const { + MutableArrayRef Bound, + const SCEV *Delta) const { Bound[Level].Direction = DirKind; if (const SCEV *LowerBound = getLowerBound(Bound)) if (SE->isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta)) @@ -2460,8 +2459,10 @@ bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level, // We must be careful to handle the case where the upper bound is unknown. // Note that the lower bound is always <= 0 // and the upper bound is always >= 0. -void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B, - BoundInfo *Bound, unsigned K) const { +void DependenceInfo::findBoundsALL(ArrayRef A, + ArrayRef B, + MutableArrayRef Bound, + unsigned K) const { Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity. Bound[K].Upper[Dependence::DVEntry::ALL] = @@ -2497,8 +2498,10 @@ void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B, // We must be careful to handle the case where the upper bound is unknown. // Note that the lower bound is always <= 0 // and the upper bound is always >= 0. -void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B, - BoundInfo *Bound, unsigned K) const { +void DependenceInfo::findBoundsEQ(ArrayRef A, + ArrayRef B, + MutableArrayRef Bound, + unsigned K) const { Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity. Bound[K].Upper[Dependence::DVEntry::EQ] = @@ -2537,8 +2540,10 @@ void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B, // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k // // We must be careful to handle the case where the upper bound is unknown. -void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B, - BoundInfo *Bound, unsigned K) const { +void DependenceInfo::findBoundsLT(ArrayRef A, + ArrayRef B, + MutableArrayRef Bound, + unsigned K) const { Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity. Bound[K].Upper[Dependence::DVEntry::LT] = @@ -2581,8 +2586,10 @@ void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B, // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k // // We must be careful to handle the case where the upper bound is unknown. -void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B, - BoundInfo *Bound, unsigned K) const { +void DependenceInfo::findBoundsGT(ArrayRef A, + ArrayRef B, + MutableArrayRef Bound, + unsigned K) const { Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity. Bound[K].Upper[Dependence::DVEntry::GT] = @@ -2625,11 +2632,11 @@ const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const { // Walks through the subscript, // collecting each coefficient, the associated loop bounds, // and recording its positive and negative parts for later use. -DependenceInfo::CoefficientInfo * -DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag, - const SCEV *&Constant) const { +void DependenceInfo::collectCoeffInfo( + const SCEV *Subscript, bool SrcFlag, const SCEV *&Constant, + SmallVectorImpl &CI) const { const SCEV *Zero = SE->getZero(Subscript->getType()); - CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1]; + CI.resize(MaxLevels + 1); for (unsigned K = 1; K <= MaxLevels; ++K) { CI[K].Coeff = Zero; CI[K].PosPart = Zero; @@ -2663,14 +2670,13 @@ DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag, } LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n'); #endif - return CI; } // Looks through all the bounds info and // computes the lower bound given the current direction settings // at each level. If the lower bound for any level is -inf, // the result is -inf. -const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const { +const SCEV *DependenceInfo::getLowerBound(ArrayRef Bound) const { const SCEV *Sum = Bound[1].Lower[Bound[1].Direction]; for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { if (Bound[K].Lower[Bound[K].Direction]) @@ -2685,7 +2691,7 @@ const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const { // computes the upper bound given the current direction settings // at each level. If the upper bound at any level is +inf, // the result is +inf. -const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const { +const SCEV *DependenceInfo::getUpperBound(ArrayRef Bound) const { const SCEV *Sum = Bound[1].Upper[Bound[1].Direction]; for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { if (Bound[K].Upper[Bound[K].Direction])