[llvm] 5910e8d - [DAG] visitUDIV - call SimplifyDemandedBits to handle hidden constant foldable cases

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 04:30:14 PDT 2024


Author: Simon Pilgrim
Date: 2024-09-15T12:29:28+01:00
New Revision: 5910e8d6075648d941c5723eab856c58857e345d

URL: https://github.com/llvm/llvm-project/commit/5910e8d6075648d941c5723eab856c58857e345d
DIFF: https://github.com/llvm/llvm-project/commit/5910e8d6075648d941c5723eab856c58857e345d.diff

LOG: [DAG] visitUDIV - call SimplifyDemandedBits to handle hidden constant foldable cases

Fixes #108728

Added: 
    llvm/test/CodeGen/X86/pr108728.ll

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index fe8ae5c9e9af6a..6dd711af03473c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -4897,6 +4897,12 @@ SDValue DAGCombiner::visitUDIV(SDNode *N) {
     if (SDValue DivRem = useDivRem(N))
         return DivRem;
 
+  // Simplify the operands using demanded-bits information.
+  // We don't have demanded bits support for UDIV so this just enables constant
+  // folding based on known bits.
+  if (SimplifyDemandedBits(SDValue(N, 0)))
+    return SDValue(N, 0);
+
   return SDValue();
 }
 

diff  --git a/llvm/test/CodeGen/X86/pr108728.ll b/llvm/test/CodeGen/X86/pr108728.ll
new file mode 100644
index 00000000000000..75a661891e726b
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pr108728.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s
+
+define i8 @PR108728(i1 %a0) {
+; CHECK-LABEL: PR108728:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    retq
+  %sel = select i1 %a0, i8 0, i8 1
+  %not = xor i8 %sel, -1
+  %udiv = udiv i8 1, %not
+  %cnt = tail call i8 @llvm.ctpop.i8(i8 %udiv)
+  ret i8 %cnt
+}


        


More information about the llvm-commits mailing list