[llvm] [LAA] Improve code in findForkedSCEVs (NFC) (PR #140384)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon May 19 11:06:15 PDT 2025


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/140384

>From 995f57d14b0e44384bb23f84fda8cfcd08bbc736 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Sat, 17 May 2025 18:33:34 +0100
Subject: [PATCH 1/2] [LAA] Improve code in findForkedSCEV (NFC)

---
 llvm/lib/Analysis/LoopAccessAnalysis.cpp | 52 +++++++++---------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 2a322a69a0dbf..f064919194129 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -999,24 +999,17 @@ static void findForkedSCEVs(
       break;
     }
 
-    // Find the pointer type we need to extend to.
-    Type *IntPtrTy = SE->getEffectiveSCEVType(
-        SE->getSCEV(GEP->getPointerOperand())->getType());
-
-    // Find the size of the type being pointed to. We only have a single
-    // index term (guarded above) so we don't need to index into arrays or
-    // structures, just get the size of the scalar value.
-    const SCEV *Size = SE->getSizeOfExpr(IntPtrTy, SourceTy);
-
-    // Scale up the offsets by the size of the type, then add to the bases.
-    const SCEV *Scaled1 = SE->getMulExpr(
-        Size, SE->getTruncateOrSignExtend(get<0>(OffsetScevs[0]), IntPtrTy));
-    const SCEV *Scaled2 = SE->getMulExpr(
-        Size, SE->getTruncateOrSignExtend(get<0>(OffsetScevs[1]), IntPtrTy));
-    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[0]), Scaled1),
-                          NeedsFreeze);
-    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[1]), Scaled2),
-                          NeedsFreeze);
+    Type *PtrTy = GEP->getPointerOperandType();
+    for (auto [B, O] : zip(BaseScevs, OffsetScevs)) {
+      const SCEV *Base = get<0>(B);
+      const SCEV *Offset = get<0>(O);
+
+      // Scale up the offsets by the size of the type, then add to the bases.
+      const SCEV *Scaled =
+          SE->getMulExpr(SE->getSizeOfExpr(PtrTy, SourceTy),
+                         SE->getTruncateOrSignExtend(Offset, PtrTy));
+      ScevList.emplace_back(SE->getAddExpr(Base, Scaled), NeedsFreeze);
+    }
     break;
   }
   case Instruction::Select: {
@@ -1026,10 +1019,9 @@ static void findForkedSCEVs(
     // then we just bail out and return the generic SCEV.
     findForkedSCEVs(SE, L, I->getOperand(1), ChildScevs, Depth);
     findForkedSCEVs(SE, L, I->getOperand(2), ChildScevs, Depth);
-    if (ChildScevs.size() == 2) {
-      ScevList.push_back(ChildScevs[0]);
-      ScevList.push_back(ChildScevs[1]);
-    } else
+    if (ChildScevs.size() == 2)
+      append_range(ScevList, ChildScevs);
+    else
       ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
     break;
   }
@@ -1042,10 +1034,9 @@ static void findForkedSCEVs(
       findForkedSCEVs(SE, L, I->getOperand(0), ChildScevs, Depth);
       findForkedSCEVs(SE, L, I->getOperand(1), ChildScevs, Depth);
     }
-    if (ChildScevs.size() == 2) {
-      ScevList.push_back(ChildScevs[0]);
-      ScevList.push_back(ChildScevs[1]);
-    } else
+    if (ChildScevs.size() == 2)
+      append_range(ScevList, ChildScevs);
+    else
       ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
     break;
   }
@@ -1072,12 +1063,9 @@ static void findForkedSCEVs(
       break;
     }
 
-    ScevList.emplace_back(
-        GetBinOpExpr(Opcode, get<0>(LScevs[0]), get<0>(RScevs[0])),
-        NeedsFreeze);
-    ScevList.emplace_back(
-        GetBinOpExpr(Opcode, get<0>(LScevs[1]), get<0>(RScevs[1])),
-        NeedsFreeze);
+    for (auto [L, R] : zip(LScevs, RScevs))
+      ScevList.emplace_back(GetBinOpExpr(Opcode, get<0>(L), get<0>(R)),
+                            NeedsFreeze);
     break;
   }
   default:

>From 74b711be109b8b02fbfe2bae4a5c6e3112fa9b4c Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 19 May 2025 18:54:25 +0100
Subject: [PATCH 2/2] [LAA] Hoist size, revert comment removal

---
 llvm/lib/Analysis/LoopAccessAnalysis.cpp | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index f064919194129..f14c68364b757 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1000,14 +1000,19 @@ static void findForkedSCEVs(
     }
 
     Type *PtrTy = GEP->getPointerOperandType();
+
+    // Find the size of the type being pointed to. We only have a single
+    // index term (guarded above) so we don't need to index into arrays or
+    // structures, just get the size of the scalar value.
+    const SCEV *Size = SE->getSizeOfExpr(PtrTy, SourceTy);
+
     for (auto [B, O] : zip(BaseScevs, OffsetScevs)) {
       const SCEV *Base = get<0>(B);
       const SCEV *Offset = get<0>(O);
 
       // Scale up the offsets by the size of the type, then add to the bases.
       const SCEV *Scaled =
-          SE->getMulExpr(SE->getSizeOfExpr(PtrTy, SourceTy),
-                         SE->getTruncateOrSignExtend(Offset, PtrTy));
+          SE->getMulExpr(Size, SE->getTruncateOrSignExtend(Offset, PtrTy));
       ScevList.emplace_back(SE->getAddExpr(Base, Scaled), NeedsFreeze);
     }
     break;



More information about the llvm-commits mailing list