[llvm] [InstCombine] Avoid self-replacing in `getUndefReplacement` (PR #161500)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 1 03:25:31 PDT 2025
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/161500
Self-replacing has a different meaning in InstCombine. It will replace all uses with poison.
Closes https://github.com/llvm/llvm-project/issues/161492.
>From b3adfafd91f586f571963cb2469dcdf17338ca98 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 1 Oct 2025 17:49:59 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/freeze.ll | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/freeze.ll b/llvm/test/Transforms/InstCombine/freeze.ll
index af5cb0c75537b..0e9b3cb42104b 100644
--- a/llvm/test/Transforms/InstCombine/freeze.ll
+++ b/llvm/test/Transforms/InstCombine/freeze.ll
@@ -1464,6 +1464,27 @@ define ptr @freeze_ptrmask_nonnull(ptr %p, i64 noundef %m) {
ret ptr %fr
}
+define i64 @pr161492_1(i1 %cond) {
+; CHECK-LABEL: define i64 @pr161492_1(
+; CHECK-SAME: i1 [[COND:%.*]]) {
+; CHECK-NEXT: ret i64 poison
+;
+ %fr1 = freeze i64 poison
+ %fr2 = freeze i64 poison
+ %ret = select i1 %cond, i64 %fr1, i64 %fr2
+ ret i64 %ret
+}
+
+define i64 @pr161492_2(i1 %cond) {
+; CHECK-LABEL: define i64 @pr161492_2(
+; CHECK-SAME: i1 [[COND:%.*]]) {
+; CHECK-NEXT: ret i64 poison
+;
+ %fr = freeze i64 poison
+ %ret = select i1 %cond, i64 %fr, i64 %fr
+ ret i64 %ret
+}
+
!0 = !{}
!1 = !{i64 4}
!2 = !{i32 0, i32 100}
>From 8ba650ae97dae11bb1b857036aeab3433cd51390 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 1 Oct 2025 17:52:10 +0800
Subject: [PATCH 2/2] [InstCombine] Avoid self-replacing in
`getUndefReplacement`
---
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 3 ++-
llvm/test/Transforms/InstCombine/freeze.ll | 4 ++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index ff063f929347f..5d2d79e420931 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -5212,7 +5212,7 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
V = ConstantInt::getTrue(Ty);
else if (match(U, m_c_Select(m_Specific(&I), m_Value(V)))) {
- if (!isGuaranteedNotToBeUndefOrPoison(V, &AC, &I, &DT))
+ if (V == &I || !isGuaranteedNotToBeUndefOrPoison(V, &AC, &I, &DT))
V = NullValue;
} else if (auto *PHI = dyn_cast<PHINode>(U)) {
if (Value *MaybeV = pickCommonConstantFromPHI(*PHI))
@@ -5225,6 +5225,7 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
BestValue = NullValue;
}
assert(BestValue && "Must have at least one use");
+ assert(BestValue != &I && "Cannot replace with itself");
return BestValue;
};
diff --git a/llvm/test/Transforms/InstCombine/freeze.ll b/llvm/test/Transforms/InstCombine/freeze.ll
index 0e9b3cb42104b..ac7d65c2a3c6a 100644
--- a/llvm/test/Transforms/InstCombine/freeze.ll
+++ b/llvm/test/Transforms/InstCombine/freeze.ll
@@ -1467,7 +1467,7 @@ define ptr @freeze_ptrmask_nonnull(ptr %p, i64 noundef %m) {
define i64 @pr161492_1(i1 %cond) {
; CHECK-LABEL: define i64 @pr161492_1(
; CHECK-SAME: i1 [[COND:%.*]]) {
-; CHECK-NEXT: ret i64 poison
+; CHECK-NEXT: ret i64 0
;
%fr1 = freeze i64 poison
%fr2 = freeze i64 poison
@@ -1478,7 +1478,7 @@ define i64 @pr161492_1(i1 %cond) {
define i64 @pr161492_2(i1 %cond) {
; CHECK-LABEL: define i64 @pr161492_2(
; CHECK-SAME: i1 [[COND:%.*]]) {
-; CHECK-NEXT: ret i64 poison
+; CHECK-NEXT: ret i64 0
;
%fr = freeze i64 poison
%ret = select i1 %cond, i64 %fr, i64 %fr
More information about the llvm-commits
mailing list