[llvm] [GVN] Freeze value if `undef` when forwarding local memset to loads (PR #91376)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Tue May 7 11:32:12 PDT 2024
https://github.com/antoniofrighetto created https://github.com/llvm/llvm-project/pull/91376
A miscompilation issue has been addressed with refined checking.
Proof: https://alive2.llvm.org/ce/z/3JVV7C.
>From c5fc4d14e8b3ddd4f09a1b9c1dbe56cc208b5880 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 7 May 2024 20:28:01 +0200
Subject: [PATCH] [GVN] Freeze value if `undef` when forwarding local memset to
loads
A miscompilation issue has been addressed with refined checking.
Proof: https://alive2.llvm.org/ce/z/3JVV7C.
---
llvm/lib/Transforms/Utils/VNCoercion.cpp | 9 ++++++---
llvm/test/Transforms/GVN/PRE/rle.ll | 3 ++-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/VNCoercion.cpp b/llvm/lib/Transforms/Utils/VNCoercion.cpp
index 7a597da2bc515..a085b1844a012 100644
--- a/llvm/lib/Transforms/Utils/VNCoercion.cpp
+++ b/llvm/lib/Transforms/Utils/VNCoercion.cpp
@@ -370,10 +370,13 @@ Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
// memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
// independently of what the offset is.
Value *Val = MSI->getValue();
+ Value *FrozenVal = Val;
+ if (!isGuaranteedNotToBeUndef(Val))
+ FrozenVal = Builder.CreateFreeze(Val, Val->getName() + ".frozen");
if (LoadSize != 1)
- Val =
- Builder.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
- Value *OneElt = Val;
+ Val = Builder.CreateZExtOrBitCast(FrozenVal,
+ IntegerType::get(Ctx, LoadSize * 8));
+ Value *OneElt = FrozenVal;
// Splat the value out to the right number of bits.
for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
diff --git a/llvm/test/Transforms/GVN/PRE/rle.ll b/llvm/test/Transforms/GVN/PRE/rle.ll
index fd4a9a081eab3..89fbfb1b800ec 100644
--- a/llvm/test/Transforms/GVN/PRE/rle.ll
+++ b/llvm/test/Transforms/GVN/PRE/rle.ll
@@ -201,7 +201,8 @@ define float @memset_to_float_local(ptr %A, i8 %Val) nounwind ssp {
; CHECK-LABEL: @memset_to_float_local(
; CHECK-NEXT: entry:
; CHECK-NEXT: tail call void @llvm.memset.p0.i64(ptr [[A:%.*]], i8 [[VAL:%.*]], i64 400, i1 false)
-; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[VAL]] to i32
+; CHECK-NEXT: [[VAL_FROZEN:%.*]] = freeze i8 [[VAL]]
+; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[VAL_FROZEN]] to i32
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[TMP0]], 8
; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP0]], [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = shl i32 [[TMP2]], 16
More information about the llvm-commits
mailing list