[llvm] 023e2e6 - [DAGCombiner] Fold bswap of single-byte-known-nonzero value to a shift (#193473)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 23 08:34:18 PDT 2026


Author: Paweł Bylica
Date: 2026-04-23T17:34:13+02:00
New Revision: 023e2e6c9d995ba34158340d5b4f044caa1cdee8

URL: https://github.com/llvm/llvm-project/commit/023e2e6c9d995ba34158340d5b4f044caa1cdee8
DIFF: https://github.com/llvm/llvm-project/commit/023e2e6c9d995ba34158340d5b4f044caa1cdee8.diff

LOG: [DAGCombiner] Fold bswap of single-byte-known-nonzero value to a shift (#193473)

When computeKnownBits proves that a bswap operand has at most one byte
of possibly-nonzero bits at a known byte-aligned position, the bswap is
equivalent to a shift that moves that byte to the mirror position. This
is a producer-side known-bits rule; it fires in visitBSWAP regardless
of how the narrow-value provenance was established, covering shapes
such as
  bswap(and X, 0xFF)
  bswap(and X, 0xFF00)       ; all byte positions
  bswap(zext i8 X to iN)
  bswap(zext i16 X to i64)

Motivation. While investigating a RISCV codegen regression under
-combiner-topological-sorting (bswap-shift.ll), I traced the root cause
to the existing consumer-side rule in
TargetLowering::SimplifyDemandedBits
for ISD::BSWAP: when a consumer demands only one byte of the bswap
result, that rule rewrites the inner bswap as a shift. Under topological
(producer-first) worklist ordering, this rewrite fires on the inner
bswap of bswap(srl(bswap X, 8)) before visitBSWAP's syntactic pattern
bswap(srl X, 8) -> (bswap X) shl 8 can collapse the whole chain, so
the outer bswap is stranded as bswap(and(X, MASK)), which no existing
combine recognizes.

The new rule is orthogonal: it matches when the operand's known bits
(rather than the consumer's demand) characterize a single-byte value,
so it closes the gap in both topological and non-topological modes. It
also captures genuinely new missed optimizations that are independent
of the ordering problem: bswap(zext i8 -> i64) previously emitted
zext.b + rev8 on RISCV+zbb and is now a single slli 56.

Placement choice. The rule is a natural dual of the existing consumer-
side rule in TargetLowering::SimplifyDemandedBits for ISD::BSWAP; the
needed information (computeKnownBits of the source) is readily
available there. I prototyped that placement and found it strictly
weaker: SimplifyDemandedBits runs later in the pipeline, typically
after integer type promotion, so a rewrite of a native i16 bswap fires
only after the value has been widened to i64 and the outer i16->i64
promotion shifts block further simplification. Placing the rule in
visitBSWAP fires pre-type-legalize on native i16/i32 values, letting
the resulting shift compose cleanly with downstream combines. A
comment in visitBSWAP cross-references the dual rule for future
readers.

Potential extension. The same logic can also be added to
TargetLowering::SimplifyDemandedBits (as a second check after the
existing one-byte-demanded rule, using computeKnownBits(Src) rather
than the demand-restricted Known2) for cases where visitBSWAP is not
reached via the combiner worklist but the bswap is recursed into by a
parent's demand analysis. I did not include that duplicate here
because no pre-existing test exercises such a path; the visitBSWAP
placement alone closes every case I found.

Added: 
    llvm/test/CodeGen/RISCV/bswap-known-bits.ll

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/test/CodeGen/AArch64/bswap-known-bits.ll
    llvm/test/CodeGen/X86/known-pow2.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index c318c39ed2f86..01d7023799138 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12094,9 +12094,9 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
     return DAG.getNode(ISD::BITREVERSE, DL, VT, BSwap);
   }
 
+  unsigned BW = VT.getScalarSizeInBits();
   // fold (bswap shl(x,c)) -> (zext(bswap(trunc(shl(x,sub(c,bw/2))))))
   // iff x >= bw/2 (i.e. lower half is known zero)
-  unsigned BW = VT.getScalarSizeInBits();
   if (BW >= 32 && N0.getOpcode() == ISD::SHL && N0.hasOneUse()) {
     auto *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
     EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), BW / 2);
@@ -12132,6 +12132,32 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
   if (SDValue V = foldBitOrderCrossLogicOp(N, DAG))
     return V;
 
+  // Folds that depend on computeKnownBits of the operand.
+  KnownBits Known = DAG.computeKnownBits(N0);
+  // bswap(0) = 0. Catch cases that computeKnownBits can prove are zero but
+  // that structural combines haven't simplified to a constant yet
+  // (e.g. and of disjoint byte masks).
+  if (Known.isZero())
+    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;
+    // 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;
+      SDNodeFlags Flags =
+          Opc == ISD::SHL ? SDNodeFlags::NoUnsignedWrap : SDNodeFlags::Exact;
+      return DAG.getNode(Opc, DL, VT, N0,
+                         DAG.getShiftAmountConstant(Amt, VT, DL), Flags);
+    }
+  }
+
   return SDValue();
 }
 

diff  --git a/llvm/test/CodeGen/AArch64/bswap-known-bits.ll b/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
index 23619e47367d0..7e881198ced02 100644
--- a/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
+++ b/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
@@ -8,7 +8,7 @@ declare i64 @llvm.bswap.i64(i64)
 define i1 @test1(i16 %arg) {
 ; CHECK-LABEL: test1:
 ; CHECK:       ; %bb.0:
-; CHECK-NEXT:    mov w0, #1
+; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    ret
   %a = or i16 %arg, 511
   %b = call i16 @llvm.bswap.i16(i16 %a)
@@ -20,7 +20,7 @@ define i1 @test1(i16 %arg) {
 define i1 @test2(i16 %arg) {
 ; CHECK-LABEL: test2:
 ; CHECK:       ; %bb.0:
-; CHECK-NEXT:    mov w0, #1
+; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    ret
   %a = or i16 %arg, 1
   %b = call i16 @llvm.bswap.i16(i16 %a)
@@ -32,7 +32,7 @@ define i1 @test2(i16 %arg) {
 define i1 @test3(i16 %arg) {
 ; CHECK-LABEL: test3:
 ; CHECK:       ; %bb.0:
-; CHECK-NEXT:    mov w0, #1
+; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    ret
   %a = or i16 %arg, 256
   %b = call i16 @llvm.bswap.i16(i16 %a)
@@ -44,7 +44,7 @@ define i1 @test3(i16 %arg) {
 define i1 @test4(i32 %arg) {
 ; CHECK-LABEL: test4:
 ; CHECK:       ; %bb.0:
-; CHECK-NEXT:    mov w0, #1
+; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    ret
   %a = or i32 %arg, 2147483647  ; i32_MAX
   %b = call i32 @llvm.bswap.i32(i32 %a)
@@ -112,3 +112,149 @@ define void @demand_one_loaded_byte(ptr %xp, ptr %yp) {
   store i32 %r, ptr %yp, align 4
   ret void
 }
+
+; Source-side known-bits fold: when the bswap operand has at most one byte
+; of possibly-nonzero bits at a known byte-aligned position, the bswap is
+; equivalent to a shift moving that byte to the mirror byte.
+
+define i16 @bswap_src_and_lo_i16(i16 %x) {
+; CHECK-LABEL: bswap_src_and_lo_i16:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    and w8, w0, #0xff
+; CHECK-NEXT:    rev16 w0, w8
+; CHECK-NEXT:    ret
+  %m = and i16 %x, 255
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  ret i16 %b
+}
+
+define i16 @bswap_src_and_hi_i16(i16 %x) {
+; CHECK-LABEL: bswap_src_and_hi_i16:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    and w8, w0, #0xffffff00
+; CHECK-NEXT:    rev16 w0, w8
+; CHECK-NEXT:    ret
+  %m = and i16 %x, 65280
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  ret i16 %b
+}
+
+define i16 @bswap_src_zext_i8_to_i16(i8 %x) {
+; CHECK-LABEL: bswap_src_zext_i8_to_i16:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    and w8, w0, #0xff
+; CHECK-NEXT:    rev16 w0, w8
+; CHECK-NEXT:    ret
+  %z = zext i8 %x to i16
+  %b = call i16 @llvm.bswap.i16(i16 %z)
+  ret i16 %b
+}
+
+define i32 @bswap_src_and_byte0_i32(i32 %x) {
+; CHECK-LABEL: bswap_src_and_byte0_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    lsl w0, w0, #24
+; CHECK-NEXT:    ret
+  %m = and i32 %x, 255
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte1_i32(i32 %x) {
+; CHECK-LABEL: bswap_src_and_byte1_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    lsl w8, w0, #8
+; CHECK-NEXT:    and w0, w8, #0xff0000
+; CHECK-NEXT:    ret
+  %m = and i32 %x, 65280
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte2_i32(i32 %x) {
+; CHECK-LABEL: bswap_src_and_byte2_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    and w8, w0, #0xff0000
+; CHECK-NEXT:    lsr w0, w8, #8
+; CHECK-NEXT:    ret
+  %m = and i32 %x, 16711680
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte3_i32(i32 %x) {
+; CHECK-LABEL: bswap_src_and_byte3_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    lsr w0, w0, #24
+; CHECK-NEXT:    ret
+  %m = and i32 %x, 4278190080
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_zext_i8_to_i32(i8 %x) {
+; CHECK-LABEL: bswap_src_zext_i8_to_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    lsl w0, w0, #24
+; CHECK-NEXT:    ret
+  %z = zext i8 %x to i32
+  %b = call i32 @llvm.bswap.i32(i32 %z)
+  ret i32 %b
+}
+
+define i64 @bswap_src_zext_i8_to_i64(i8 %x) {
+; CHECK-LABEL: bswap_src_zext_i8_to_i64:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    ; kill: def $w0 killed $w0 def $x0
+; CHECK-NEXT:    lsl x0, x0, #56
+; CHECK-NEXT:    ret
+  %z = zext i8 %x to i64
+  %b = call i64 @llvm.bswap.i64(i64 %z)
+  ret i64 %b
+}
+
+define i64 @bswap_src_zext_i16_to_i64(i16 %x) {
+; CHECK-LABEL: bswap_src_zext_i16_to_i64:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    ; kill: def $w0 killed $w0 def $x0
+; CHECK-NEXT:    and x8, x0, #0xffff
+; CHECK-NEXT:    rev x0, x8
+; CHECK-NEXT:    ret
+  %z = zext i16 %x to i64
+  %b = call i64 @llvm.bswap.i64(i64 %z)
+  ret i64 %b
+}
+
+; Regression: the producer-side combine must not tag the produced shl
+; with nsw. For bswap(and X, 0xFF) on i16 we shift byte 0 into the top
+; byte, which becomes the sign bit. Bit 7 of the input freely flips the
+; result's sign, so nsw would be unsound and make `icmp slt 0` fold to
+; false. Here the comparison must remain: the result depends on bit 7
+; of the input.
+define i1 @bswap_lo_byte_sign_i16(i16 %x) {
+; CHECK-LABEL: bswap_lo_byte_sign_i16:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    ubfx w0, w0, #7, #1
+; CHECK-NEXT:    ret
+  %m = and i16 %x, 255
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  %neg = icmp slt i16 %b, 0
+  ret i1 %neg
+}
+
+; Regression test for the known-zero fold: two disjoint byte masks ANDed
+; together are always zero, but the DAG combiner doesn't structurally
+; simplify to a constant before visiting the bswap. computeKnownBits
+; correctly proves the operand is zero, and the combine must fold
+; bswap(known-zero) to 0.
+define i32 @bswap_nested_and_disjoint_i32(i32 %x, i32 %y) {
+; CHECK-LABEL: bswap_nested_and_disjoint_i32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    mov w0, wzr
+; CHECK-NEXT:    ret
+  %m1 = and i32 %x, 255
+  %m2 = and i32 %y, 65280
+  %m3 = and i32 %m1, %m2
+  %b = call i32 @llvm.bswap.i32(i32 %m3)
+  ret i32 %b
+}

diff  --git a/llvm/test/CodeGen/RISCV/bswap-known-bits.ll b/llvm/test/CodeGen/RISCV/bswap-known-bits.ll
new file mode 100644
index 0000000000000..62d332306395b
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/bswap-known-bits.ll
@@ -0,0 +1,227 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -mattr=+zbb -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefixes=RV32ZB
+; RUN: llc -mtriple=riscv64 -mattr=+zbb -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefixes=RV64ZB
+
+; Source-side known-bits fold: when the bswap operand has at most one byte
+; of possibly-nonzero bits at a known byte-aligned position, the bswap is
+; equivalent to a shift moving that byte to the mirror byte.
+
+declare i16 @llvm.bswap.i16(i16)
+declare i32 @llvm.bswap.i32(i32)
+declare i64 @llvm.bswap.i64(i64)
+
+define i16 @bswap_src_and_lo_i16(i16 %x) {
+; RV32ZB-LABEL: bswap_src_and_lo_i16:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 8
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_lo_i16:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slli a0, a0, 8
+; RV64ZB-NEXT:    ret
+  %m = and i16 %x, 255
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  ret i16 %b
+}
+
+define i16 @bswap_src_and_hi_i16(i16 %x) {
+; RV32ZB-LABEL: bswap_src_and_hi_i16:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 16
+; RV32ZB-NEXT:    srli a0, a0, 24
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_hi_i16:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slli a0, a0, 48
+; RV64ZB-NEXT:    srli a0, a0, 56
+; RV64ZB-NEXT:    ret
+  %m = and i16 %x, 65280
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  ret i16 %b
+}
+
+define i16 @bswap_src_zext_i8_to_i16(i8 %x) {
+; RV32ZB-LABEL: bswap_src_zext_i8_to_i16:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 8
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_zext_i8_to_i16:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slli a0, a0, 8
+; RV64ZB-NEXT:    ret
+  %z = zext i8 %x to i16
+  %b = call i16 @llvm.bswap.i16(i16 %z)
+  ret i16 %b
+}
+
+define i32 @bswap_src_and_byte0_i32(i32 %x) {
+; RV32ZB-LABEL: bswap_src_and_byte0_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 24
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_byte0_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slliw a0, a0, 24
+; RV64ZB-NEXT:    ret
+  %m = and i32 %x, 255
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte1_i32(i32 %x) {
+; RV32ZB-LABEL: bswap_src_and_byte1_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    lui a1, 16
+; RV32ZB-NEXT:    addi a1, a1, -256
+; RV32ZB-NEXT:    and a0, a0, a1
+; RV32ZB-NEXT:    slli a0, a0, 8
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_byte1_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    lui a1, 16
+; RV64ZB-NEXT:    addi a1, a1, -256
+; RV64ZB-NEXT:    and a0, a0, a1
+; RV64ZB-NEXT:    slli a0, a0, 8
+; RV64ZB-NEXT:    ret
+  %m = and i32 %x, 65280
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte2_i32(i32 %x) {
+; RV32ZB-LABEL: bswap_src_and_byte2_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    lui a1, 4080
+; RV32ZB-NEXT:    and a0, a0, a1
+; RV32ZB-NEXT:    srli a0, a0, 8
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_byte2_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    lui a1, 4080
+; RV64ZB-NEXT:    and a0, a0, a1
+; RV64ZB-NEXT:    srli a0, a0, 8
+; RV64ZB-NEXT:    ret
+  %m = and i32 %x, 16711680
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_and_byte3_i32(i32 %x) {
+; RV32ZB-LABEL: bswap_src_and_byte3_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    srli a0, a0, 24
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_and_byte3_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    srliw a0, a0, 24
+; RV64ZB-NEXT:    ret
+  %m = and i32 %x, 4278190080
+  %b = call i32 @llvm.bswap.i32(i32 %m)
+  ret i32 %b
+}
+
+define i32 @bswap_src_zext_i8_to_i32(i8 %x) {
+; RV32ZB-LABEL: bswap_src_zext_i8_to_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 24
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_zext_i8_to_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slliw a0, a0, 24
+; RV64ZB-NEXT:    ret
+  %z = zext i8 %x to i32
+  %b = call i32 @llvm.bswap.i32(i32 %z)
+  ret i32 %b
+}
+
+define i64 @bswap_src_zext_i8_to_i64(i8 %x) {
+; RV32ZB-LABEL: bswap_src_zext_i8_to_i64:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a1, a0, 24
+; RV32ZB-NEXT:    li a0, 0
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_zext_i8_to_i64:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slli a0, a0, 56
+; RV64ZB-NEXT:    ret
+  %z = zext i8 %x to i64
+  %b = call i64 @llvm.bswap.i64(i64 %z)
+  ret i64 %b
+}
+
+define i64 @bswap_src_zext_i16_to_i64(i16 %x) {
+; RV32ZB-LABEL: bswap_src_zext_i16_to_i64:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    zext.h a0, a0
+; RV32ZB-NEXT:    rev8 a1, a0
+; RV32ZB-NEXT:    li a0, 0
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_src_zext_i16_to_i64:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    zext.h a0, a0
+; RV64ZB-NEXT:    rev8 a0, a0
+; RV64ZB-NEXT:    ret
+  %z = zext i16 %x to i64
+  %b = call i64 @llvm.bswap.i64(i64 %z)
+  ret i64 %b
+}
+
+; Regression: the producer-side combine must not tag the produced shl
+; with nsw. For bswap(and X, 0xFF) on i16 we shift byte 0 into the top
+; byte, which becomes the sign bit. Bit 7 of the input freely flips the
+; result's sign, so nsw would be unsound and make `icmp slt 0` fold to
+; false. Here the comparison must remain: the result depends on bit 7
+; of the input.
+define i1 @bswap_lo_byte_sign_i16(i16 %x) {
+; RV32ZB-LABEL: bswap_lo_byte_sign_i16:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    slli a0, a0, 8
+; RV32ZB-NEXT:    sext.h a0, a0
+; RV32ZB-NEXT:    srli a0, a0, 31
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_lo_byte_sign_i16:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    slli a0, a0, 8
+; RV64ZB-NEXT:    sext.h a0, a0
+; RV64ZB-NEXT:    srli a0, a0, 63
+; RV64ZB-NEXT:    ret
+  %m = and i16 %x, 255
+  %b = call i16 @llvm.bswap.i16(i16 %m)
+  %neg = icmp slt i16 %b, 0
+  ret i1 %neg
+}
+
+; Regression test for the known-zero fold: two disjoint byte masks ANDed
+; together are always zero, but the DAG combiner doesn't structurally
+; simplify to a constant before visiting the bswap. computeKnownBits
+; correctly proves the operand is zero, and the combine must fold
+; bswap(known-zero) to 0.
+define i32 @bswap_nested_and_disjoint_i32(i32 %x, i32 %y) {
+; RV32ZB-LABEL: bswap_nested_and_disjoint_i32:
+; RV32ZB:       # %bb.0:
+; RV32ZB-NEXT:    li a0, 0
+; RV32ZB-NEXT:    ret
+;
+; RV64ZB-LABEL: bswap_nested_and_disjoint_i32:
+; RV64ZB:       # %bb.0:
+; RV64ZB-NEXT:    li a0, 0
+; RV64ZB-NEXT:    ret
+  %m1 = and i32 %x, 255
+  %m2 = and i32 %y, 65280
+  %m3 = and i32 %m1, %m2
+  %b = call i32 @llvm.bswap.i32(i32 %m3)
+  ret i32 %b
+}

diff  --git a/llvm/test/CodeGen/X86/known-pow2.ll b/llvm/test/CodeGen/X86/known-pow2.ll
index 47bf4e348e26a..fc368cb5a54ac 100644
--- a/llvm/test/CodeGen/X86/known-pow2.ll
+++ b/llvm/test/CodeGen/X86/known-pow2.ll
@@ -1161,12 +1161,10 @@ define i1 @pow2_and_i128(i128 %num, i128 %shift) {
 define i32 @pow2_bswap(i32 %a0, i32 %a1) {
 ; CHECK-LABEL: pow2_bswap:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    # kill: def $edi killed $edi def $rdi
-; CHECK-NEXT:    notl %edi
-; CHECK-NEXT:    shrl $31, %edi
-; CHECK-NEXT:    leal 4(,%rdi,4), %eax
-; CHECK-NEXT:    bswapl %eax
-; CHECK-NEXT:    decl %eax
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    notl %eax
+; CHECK-NEXT:    shrl $5, %eax
+; CHECK-NEXT:    orl $67108863, %eax # imm = 0x3FFFFFF
 ; CHECK-NEXT:    andl %esi, %eax
 ; CHECK-NEXT:    retq
   %cmp = icmp sgt i32 0, %a0
@@ -1213,12 +1211,10 @@ define i32 @pow2_bitreverse(i32 %a0, i32 %a1) {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-NEXT:    notl %edi
-; CHECK-NEXT:    shrl $31, %edi
-; CHECK-NEXT:    leal 4(,%rdi,4), %eax
-; CHECK-NEXT:    bswapl %eax
-; CHECK-NEXT:    movl %eax, %ecx
-; CHECK-NEXT:    shll $3, %eax
-; CHECK-NEXT:    leal (%rax,%rcx,2), %eax
+; CHECK-NEXT:    shrl $5, %edi
+; CHECK-NEXT:    andl $67108864, %edi # imm = 0x4000000
+; CHECK-NEXT:    leal 536870912(,%rdi,8), %eax
+; CHECK-NEXT:    leal 134217728(%rax,%rdi,2), %eax
 ; CHECK-NEXT:    andl $805306368, %eax # imm = 0x30000000
 ; CHECK-NEXT:    decl %eax
 ; CHECK-NEXT:    andl %esi, %eax


        


More information about the llvm-commits mailing list