[llvm] SROA generate a noundef instead of splatting a noundef int (PR #145122)

Ben Kimock via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 20 16:55:54 PDT 2025


https://github.com/saethlin created https://github.com/llvm/llvm-project/pull/145122

Fixes https://github.com/llvm/llvm-project/issues/145121

This is just a fix for the SROA behavior, and I think that will also avoid what EarlyCSE is doing. Which is a bit unfortunate, because I'd like to be well-motivated to fix both passes.

>From 331e1c2a0e1efdff119720de9bd7bc010e3f3642 Mon Sep 17 00:00:00 2001
From: Ben Kimock <kimockb at gmail.com>
Date: Fri, 20 Jun 2025 19:39:57 -0400
Subject: [PATCH] SROA generate a noundef instead of splatting a noundef int

---
 llvm/lib/Transforms/Scalar/SROA.cpp | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 42d1d9a437bb2..028bcde5a51d7 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -3221,12 +3221,16 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
       return V;
 
     Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8);
-    V = IRB.CreateMul(
-        IRB.CreateZExt(V, SplatIntTy, "zext"),
-        IRB.CreateUDiv(Constant::getAllOnesValue(SplatIntTy),
-                       IRB.CreateZExt(Constant::getAllOnesValue(V->getType()),
-                                      SplatIntTy)),
-        "isplat");
+    if (isa<UndefValue>(V)) {
+        V = UndefValue::get(VTy);
+    } else {
+        V = IRB.CreateMul(
+            IRB.CreateZExt(V, SplatIntTy, "zext"),
+            IRB.CreateUDiv(Constant::getAllOnesValue(SplatIntTy),
+                           IRB.CreateZExt(Constant::getAllOnesValue(V->getType()),
+                                          SplatIntTy)),
+            "isplat");
+    }
     return V;
   }
 



More information about the llvm-commits mailing list