[llvm] [CGP] Drop poison generating flags when pushing freeze through cmp (PR #222314)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 05:35:49 PDT 2026


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

We need to clear the samesign flag when pushing freeze through icmp.

In the last commit, I also adjusted the handling of FMF on fcmp to work the same way -- previously the transform would be skipped instead if the fcmp had fast math flags. I think just clearing the poison-generating flags is preferable (and what we'd do anyway in InstCombine and DAGCombine).

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

>From 03f0313123cdc99e7d2b41a6d7be36fa7799677f Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 9 Sep 2026 14:31:04 +0200
Subject: [PATCH 1/3] Add test

---
 .../Transforms/CodeGenPrepare/X86/freeze-brcond.ll    | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
index e9ecfd1615d45..d93b739f5f6ec 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
@@ -319,5 +319,16 @@ EXIT:
   ret void
 }
 
+define i1 @freeze_samesign(i32 %x) {
+; CHECK-LABEL: @freeze_samesign(
+; CHECK-NEXT:    [[FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ult i32 [[FR]], 42
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign ult i32 %x, 42
+  %fr = freeze i1 %cmp
+  ret i1 %fr
+}
+
 declare void @g1()
 declare void @g2()

>From 8e7aea579d88b35dfdec1fe0cf79778331cbd625 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 9 Sep 2026 14:31:52 +0200
Subject: [PATCH 2/3] Drop samesign

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                      | 1 +
 llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 72e0209a1a2c9..2c179e38138f7 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -9105,6 +9105,7 @@ 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();
         }
         replaceAllUsesWith(FI, CmpI, FreshBBs, IsHugeFunc);
         FI->eraseFromParent();
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
index d93b739f5f6ec..d1a78c6863c87 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
@@ -322,7 +322,7 @@ EXIT:
 define i1 @freeze_samesign(i32 %x) {
 ; CHECK-LABEL: @freeze_samesign(
 ; CHECK-NEXT:    [[FR:%.*]] = freeze i32 [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ult i32 [[FR]], 42
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[FR]], 42
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %cmp = icmp samesign ult i32 %x, 42

>From 703c6345388be24bd8f2adc321c9ba65f57274fb Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 9 Sep 2026 14:32:55 +0200
Subject: [PATCH 3/3] Use the same handling for FMF

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                      | 7 +------
 llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll | 4 ++--
 2 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 2c179e38138f7..3bf99001c8b26 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -9088,12 +9088,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
   if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
     // freeze(icmp a, const)) -> icmp (freeze a), const
     // This helps generate efficient conditional jumps.
-    Instruction *CmpI = nullptr;
-    if (ICmpInst *II = dyn_cast<ICmpInst>(FI->getOperand(0)))
-      CmpI = II;
-    else if (FCmpInst *F = dyn_cast<FCmpInst>(FI->getOperand(0)))
-      CmpI = F->getFastMathFlags().none() ? F : nullptr;
-
+    CmpInst *CmpI = dyn_cast<CmpInst>(FI->getOperand(0));;
     if (CmpI && CmpI->hasOneUse()) {
       auto Op0 = CmpI->getOperand(0), Op1 = CmpI->getOperand(1);
       bool Const0 = isa<ConstantInt>(Op0) || isa<ConstantFP>(Op0) ||
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
index d1a78c6863c87..1ad10fd54c0f3 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-brcond.ll
@@ -96,8 +96,8 @@ define i1 @fcmp(float %a) {
 
 define i1 @fcmp_nan(float %a) {
 ; CHECK-LABEL: @fcmp_nan(
-; CHECK-NEXT:    [[C:%.*]] = fcmp nnan oeq float [[A:%.*]], 0.000000e+00
-; CHECK-NEXT:    [[FR:%.*]] = freeze i1 [[C]]
+; CHECK-NEXT:    [[FR1:%.*]] = freeze float [[A:%.*]]
+; CHECK-NEXT:    [[FR:%.*]] = fcmp oeq float [[FR1]], 0.000000e+00
 ; CHECK-NEXT:    ret i1 [[FR]]
 ;
   %c = fcmp nnan oeq float %a, 0.0



More information about the llvm-commits mailing list