[llvm] [CGP] Clear poison generating flags when freezing icmp of constants (PR #223326)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 01:27:43 PDT 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/223326

Followup to https://github.com/llvm/llvm-project/pull/222314. That
PR only dropped the flags for the case where a new freeze instruction
was created, but of course we also need to do this if both operands
are constant and the freeze is dropped entirely.

>From e47ee26c003bcd289ae169f13f3da680b5226deb Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 14 Sep 2026 10:26:13 +0200
Subject: [PATCH] [CGP] Clear poison generating flags when freezing icmp of
 constants

Followup to https://github.com/llvm/llvm-project/pull/222314. That
PR only dropped the flags for the case where a new freeze instruction
was created, but of course we also need to do this if both operands
are constant and the freeze is dropped entirely.
---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                    |  2 +-
 .../Transforms/CodeGenPrepare/X86/freeze-brcond.ll     | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 52ed1255ab9c4..3e0b8a956ca81 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -9100,8 +9100,8 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
           auto *F = new FreezeInst(Const0 ? Op1 : Op0, "", CmpI->getIterator());
           F->takeName(FI);
           CmpI->setOperand(Const0 ? 1 : 0, F);
-          CmpI->dropPoisonGeneratingFlags();
         }
+        CmpI->dropPoisonGeneratingFlags();
         replaceAllUsesWith(FI, CmpI, FreshBBs, IsHugeFunc);
         FI->eraseFromParent();
         return true;
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
index 1ad10fd54c0f3..ab46e26cd0ab7 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
@@ -330,5 +330,15 @@ define i1 @freeze_samesign(i32 %x) {
   ret i1 %fr
 }
 
+define i1 @freeze_samesign_consts(i32 %x) {
+; CHECK-LABEL: @freeze_samesign_consts(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 -42, 42
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign ult i32 -42, 42
+  %fr = freeze i1 %cmp
+  ret i1 %fr
+}
+
 declare void @g1()
 declare void @g2()



More information about the llvm-commits mailing list