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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon May 19 03:17:42 PDT 2025


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

>From 75a85c88a6fbaa4ad89f4d115ff97409fbcae469 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/4] [LAA] Simplify GEP SCEV in findForkedSCEVs (NFC)

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

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index ab407e945bc53..58083cb06f5f6 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -999,23 +999,16 @@ 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 *Offset0 = get<0>(OffsetScevs[0]);
+    const SCEV *Offset1 = get<0>(OffsetScevs[1]);
+    const SCEV *Scaled0 = SE->getMulExpr(
+        SE->getSizeOfExpr(Offset0->getType(), SourceTy), Offset0);
     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),
+        SE->getSizeOfExpr(Offset1->getType(), SourceTy), Offset1);
+    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[0]), Scaled0),
                           NeedsFreeze);
-    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[1]), Scaled2),
+    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[1]), Scaled1),
                           NeedsFreeze);
     break;
   }

>From a450bd52692434b33901ce362c183fb8b3662aac Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Sat, 17 May 2025 19:13:08 +0100
Subject: [PATCH 2/4] [LAA] Fix thinko

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

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 58083cb06f5f6..309d1353f8737 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1000,16 +1000,20 @@ static void findForkedSCEVs(
     }
 
     // Scale up the offsets by the size of the type, then add to the bases.
+    const SCEV *Base0 = get<0>(BaseScevs[0]);
+    const SCEV *Base1 = get<0>(BaseScevs[1]);
     const SCEV *Offset0 = get<0>(OffsetScevs[0]);
     const SCEV *Offset1 = get<0>(OffsetScevs[1]);
-    const SCEV *Scaled0 = SE->getMulExpr(
-        SE->getSizeOfExpr(Offset0->getType(), SourceTy), Offset0);
-    const SCEV *Scaled1 = SE->getMulExpr(
-        SE->getSizeOfExpr(Offset1->getType(), SourceTy), Offset1);
-    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[0]), Scaled0),
-                          NeedsFreeze);
-    ScevList.emplace_back(SE->getAddExpr(get<0>(BaseScevs[1]), Scaled1),
-                          NeedsFreeze);
+    const SCEV *Scaled0 = SE->getTruncateOrSignExtend(
+        SE->getMulExpr(SE->getSizeOfExpr(Offset0->getType(), SourceTy),
+                       Offset0),
+        Base0->getType());
+    const SCEV *Scaled1 = SE->getTruncateOrSignExtend(
+        SE->getMulExpr(SE->getSizeOfExpr(Offset1->getType(), SourceTy),
+                       Offset1),
+        Base1->getType());
+    ScevList.emplace_back(SE->getAddExpr(Base0, Scaled0), NeedsFreeze);
+    ScevList.emplace_back(SE->getAddExpr(Base1, Scaled1), NeedsFreeze);
     break;
   }
   case Instruction::Select: {

>From df24c96ef7dacf5f33c01bf2eefe9d29c8f1b043 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Sat, 17 May 2025 19:35:57 +0100
Subject: [PATCH 3/4] [LAA] Improve code in findForkedSCEV

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

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 309d1353f8737..1d8bf5ae84da3 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -999,21 +999,17 @@ static void findForkedSCEVs(
       break;
     }
 
-    // Scale up the offsets by the size of the type, then add to the bases.
-    const SCEV *Base0 = get<0>(BaseScevs[0]);
-    const SCEV *Base1 = get<0>(BaseScevs[1]);
-    const SCEV *Offset0 = get<0>(OffsetScevs[0]);
-    const SCEV *Offset1 = get<0>(OffsetScevs[1]);
-    const SCEV *Scaled0 = SE->getTruncateOrSignExtend(
-        SE->getMulExpr(SE->getSizeOfExpr(Offset0->getType(), SourceTy),
-                       Offset0),
-        Base0->getType());
-    const SCEV *Scaled1 = SE->getTruncateOrSignExtend(
-        SE->getMulExpr(SE->getSizeOfExpr(Offset1->getType(), SourceTy),
-                       Offset1),
-        Base1->getType());
-    ScevList.emplace_back(SE->getAddExpr(Base0, Scaled0), NeedsFreeze);
-    ScevList.emplace_back(SE->getAddExpr(Base1, Scaled1), NeedsFreeze);
+    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->getTruncateOrSignExtend(
+          SE->getMulExpr(SE->getSizeOfExpr(Offset->getType(), SourceTy),
+                         Offset),
+          Base->getType());
+      ScevList.emplace_back(SE->getAddExpr(Base, Scaled), NeedsFreeze);
+    }
     break;
   }
   case Instruction::Select: {
@@ -1023,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;
   }
@@ -1039,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;
   }
@@ -1069,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 0fc49fc9b75888f51a36ae452af25f42b3467eb0 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 19 May 2025 11:16:04 +0100
Subject: [PATCH 4/4] [LAA] Fix SCEV mul at different bitwidth

---
 llvm/lib/Analysis/LoopAccessAnalysis.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 1d8bf5ae84da3..a006f11ef130a 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -999,15 +999,15 @@ static void findForkedSCEVs(
       break;
     }
 
+    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->getTruncateOrSignExtend(
-          SE->getMulExpr(SE->getSizeOfExpr(Offset->getType(), SourceTy),
-                         Offset),
-          Base->getType());
+      const SCEV *Scaled =
+          SE->getMulExpr(SE->getSizeOfExpr(PtrTy, SourceTy),
+                         SE->getTruncateOrSignExtend(Offset, PtrTy));
       ScevList.emplace_back(SE->getAddExpr(Base, Scaled), NeedsFreeze);
     }
     break;



More information about the llvm-commits mailing list