[llvm] 1fdd8cb - Revert "[Transform] Clean up strlen loop idiom (#132421)"

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 03:47:05 PDT 2025


Author: Martin Storsjö
Date: 2025-03-22T12:40:40+02:00
New Revision: 1fdd8cb91e88cf9e71701bd286e2dee98e48540f

URL: https://github.com/llvm/llvm-project/commit/1fdd8cb91e88cf9e71701bd286e2dee98e48540f
DIFF: https://github.com/llvm/llvm-project/commit/1fdd8cb91e88cf9e71701bd286e2dee98e48540f.diff

LOG: Revert "[Transform] Clean up strlen loop idiom (#132421)"

This reverts commit 7c52886700a5a70d873400ec022a99d7dce8b03b.

Reverting this as I have to revert another preceding commit,
ac9049df7e62e2ca4dc5d103593b51639b5715e3.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll
    llvm/test/Transforms/LoopIdiom/strlen.ll
    llvm/test/Transforms/LoopIdiom/wcslen16.ll
    llvm/test/Transforms/LoopIdiom/wcslen32.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index a0b257b7d3ea3..b91cdc0748581 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -1515,6 +1515,16 @@ bool LoopIdiomRecognize::runOnNoncountableLoop() {
          recognizeShiftUntilLessThan() || recognizeAndInsertStrLen();
 }
 
+/// Check if a Value is either a nullptr or a constant int zero
+static bool isZeroConstant(const Value *Val) {
+  if (isa<ConstantPointerNull>(Val))
+    return true;
+  const ConstantInt *CmpZero = dyn_cast<ConstantInt>(Val);
+  if (!CmpZero || !CmpZero->isZero())
+    return false;
+  return true;
+}
+
 /// Check if the given conditional branch is based on the comparison between
 /// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
 /// true), the control yields to the loop entry. If the branch matches the
@@ -1530,8 +1540,7 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
   if (!Cond)
     return nullptr;
 
-  auto *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
-  if (!CmpZero || !CmpZero->isZero())
+  if (!isZeroConstant(Cond->getOperand(1)))
     return nullptr;
 
   BasicBlock *TrueSucc = BI->getSuccessor(0);
@@ -1602,7 +1611,11 @@ class StrlenVerifier {
       return false;
     LoadBaseEv = LoadEv->getStart();
 
-    LLVM_DEBUG(dbgs() << "pointer load scev: " << *LoadEv << "\n");
+    LLVM_DEBUG({
+      dbgs() << "pointer load scev: ";
+      LoadEv->print(outs());
+      dbgs() << "\n";
+    });
 
     const SCEVConstant *Step =
         dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence(*SE));
@@ -1643,7 +1656,11 @@ class StrlenVerifier {
       if (!Ev)
         return false;
 
-      LLVM_DEBUG(dbgs() << "loop exit phi scev: " << *Ev << "\n");
+      LLVM_DEBUG({
+        dbgs() << "loop exit phi scev: ";
+        Ev->print(dbgs());
+        dbgs() << "\n";
+      });
 
       // Since we verified that the loop trip count will be a valid strlen
       // idiom, we can expand all lcssa phi with {n,+,1} as (n + strlen) and use
@@ -1746,18 +1763,6 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
   BasicBlock *Preheader = CurLoop->getLoopPreheader();
   BasicBlock *LoopExitBB = CurLoop->getExitBlock();
 
-  if (Verifier.OpWidth == 8) {
-    if (DisableLIRP::Strlen)
-      return false;
-    if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
-      return false;
-  } else {
-    if (DisableLIRP::Wcslen)
-      return false;
-    if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
-      return false;
-  }
-
   IRBuilder<> Builder(Preheader->getTerminator());
   SCEVExpander Expander(*SE, Preheader->getModule()->getDataLayout(),
                         "strlen_idiom");
@@ -1767,8 +1772,16 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
 
   Value *StrLenFunc = nullptr;
   if (Verifier.OpWidth == 8) {
+    if (DisableLIRP::Strlen)
+      return false;
+    if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
+      return false;
     StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
   } else {
+    if (DisableLIRP::Wcslen)
+      return false;
+    if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
+      return false;
     StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
   }
   assert(StrLenFunc && "Failed to emit strlen function.");

diff  --git a/llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll b/llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll
index 00fbe89846a30..0be76cbd42a72 100644
--- a/llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll
+++ b/llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll
@@ -1,4 +1,4 @@
-; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
+; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"

diff  --git a/llvm/test/Transforms/LoopIdiom/strlen.ll b/llvm/test/Transforms/LoopIdiom/strlen.ll
index c1141177c659f..137a17f541cd4 100644
--- a/llvm/test/Transforms/LoopIdiom/strlen.ll
+++ b/llvm/test/Transforms/LoopIdiom/strlen.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
+; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
 
 declare void @other()
 declare void @use(ptr)

diff  --git a/llvm/test/Transforms/LoopIdiom/wcslen16.ll b/llvm/test/Transforms/LoopIdiom/wcslen16.ll
index 8be51869f1f2e..d3b0b8d208cd8 100644
--- a/llvm/test/Transforms/LoopIdiom/wcslen16.ll
+++ b/llvm/test/Transforms/LoopIdiom/wcslen16.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
+; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"

diff  --git a/llvm/test/Transforms/LoopIdiom/wcslen32.ll b/llvm/test/Transforms/LoopIdiom/wcslen32.ll
index 2c7ceb4d187e6..488afff86c245 100644
--- a/llvm/test/Transforms/LoopIdiom/wcslen32.ll
+++ b/llvm/test/Transforms/LoopIdiom/wcslen32.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
+; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"


        


More information about the llvm-commits mailing list