[llvm] [DAGCombiner] Slightly simplify code in visitBSWAP. NFC (PR #193896)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 09:08:10 PDT 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/193896

>From 9c5599bbfd6f42411344890a595d5bf1a792a18f Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 23 Apr 2026 22:31:33 -0700
Subject: [PATCH 1/2] [DAGCombiner] Slight simplify code in visitBSWAP.

Use alignDown and do computation in bits instead of converting to
bytes and back to bits. This is similar to what we do in
InstCombineCalls for the same transform.
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 01d7023799138..2cff5b485fcfa 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12141,16 +12141,15 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
     return DAG.getConstant(0, DL, VT);
   // If only one byte of the operand may be nonzero, bswap becomes a shift
   // to the mirror byte.
-  unsigned Lo = Known.countMinTrailingZeros();
-  unsigned Hi = BW - Known.countMinLeadingZeros();
-  if (unsigned SrcByte = Lo / 8; SrcByte == (Hi - 1) / 8) {
-    unsigned DstByte = (BW / 8) - 1 - SrcByte;
-    unsigned Opc = DstByte > SrcByte ? ISD::SHL : ISD::SRL;
+  unsigned TZ = alignDown(Known.countMinTrailingZeros(), 8);
+  unsigned LZ = alignDown(Known.countMinLeadingZeros(), 8);
+  if (BW - LZ - TZ == 8) {
+    unsigned Opc = LZ > TZ ? ISD::SHL : ISD::SRL;
     // Skip if the target would re-expand the produced shift post-legalize.
     // Targets that custom-lower byte-multiple shifts via bswap (e.g. MSP430
     // for shl i16) would loop with this combine.
     if (!LegalOperations || hasOperation(Opc, VT)) {
-      unsigned Amt = AbsoluteDifference(DstByte, SrcByte) * 8;
+      unsigned Amt = AbsoluteDifference(LZ, TZ);
       SDNodeFlags Flags =
           Opc == ISD::SHL ? SDNodeFlags::NoUnsignedWrap : SDNodeFlags::Exact;
       return DAG.getNode(Opc, DL, VT, N0,

>From dccd27aa74c786cb866a3467e84fd49fffbf0aeb Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 29 Apr 2026 09:07:37 -0700
Subject: [PATCH 2/2] fixup! address review comment

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 2cff5b485fcfa..b397e1296169c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12143,7 +12143,7 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
   // to the mirror byte.
   unsigned TZ = alignDown(Known.countMinTrailingZeros(), 8);
   unsigned LZ = alignDown(Known.countMinLeadingZeros(), 8);
-  if (BW - LZ - TZ == 8) {
+  if (BW - (LZ + TZ) == 8) {
     unsigned Opc = LZ > TZ ? ISD::SHL : ISD::SRL;
     // Skip if the target would re-expand the produced shift post-legalize.
     // Targets that custom-lower byte-multiple shifts via bswap (e.g. MSP430



More information about the llvm-commits mailing list