[llvm] [InstCombine] Fold values to 0 on eq-dominated paths (PR #174083)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 03:49:53 PST 2026


https://github.com/arrowten updated https://github.com/llvm/llvm-project/pull/174083

>From 3b9bfec1a16dc41c866a7e330ac3863887dcb034 Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Wed, 31 Dec 2025 06:13:47 -0500
Subject: [PATCH] [GVN] Fold values to 0 on eq-dominated paths

InstCombine can miss simplifications when an instruction is only used in a block that is reachable only if 'Op0 == Op1'. This patch folds such values to 0.

Fixes: #143658
---
 llvm/lib/Transforms/Scalar/GVN.cpp            | 17 +++++++++++++++++
 llvm/test/Transforms/GVN/fold-on-eq-branch.ll | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 llvm/test/Transforms/GVN/fold-on-eq-branch.ll

diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 72e1131a54a86..123a9a55ec0c5 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2744,6 +2744,23 @@ bool GVNPass::processInstruction(Instruction *I) {
     return Changed;
   }
 
+  if (auto *CI = dyn_cast<CallInst>(I)) {
+    SimplifyQuery SQ = {DL, TLI, DT, AC}; 
+
+    for (unsigned i = 0; i < CI->arg_size(); ++i) {
+      Value *Arg = CI->getArgOperand(i);
+      auto *Sub = dyn_cast<BinaryOperator>(Arg);
+
+      if (!Sub || Sub->getOpcode() != Instruction::Sub)
+        continue;
+
+      if (!isKnownNonEqual(Sub->getOperand(0), Sub->getOperand(1), SQ)) {
+        CI->setArgOperand(i, ConstantInt::get(Sub->getType(), 0)); 
+        return true;
+      }    
+    }    
+  }
+
   // Instructions with void type don't return a value, so there's
   // no point in trying to find redundancies in them.
   if (I->getType()->isVoidTy())
diff --git a/llvm/test/Transforms/GVN/fold-on-eq-branch.ll b/llvm/test/Transforms/GVN/fold-on-eq-branch.ll
new file mode 100644
index 0000000000000..79d0d8c73f2f7
--- /dev/null
+++ b/llvm/test/Transforms/GVN/fold-on-eq-branch.ll
@@ -0,0 +1,18 @@
+; RUN: opt -passes='default<O2>' -S < %s | FileCheck %s
+; RUN: opt -passes='default<O3>' -S < %s | FileCheck %s
+
+declare void @use(i64)
+
+define i64 @ftp_state_list(i64 %0, i64 %1) {
+  %3 = sub i64 %0, %1
+  %4 = icmp eq i64 %0, %1
+  br i1 %4, label %5, label %common.ret
+
+5:
+; CHECK: tail call void @use(i64 0)
+  tail call void @use(i64 %3)
+  br label %common.ret
+
+common.ret:
+  ret i64 %3
+}



More information about the llvm-commits mailing list