[llvm] [DAGCombiner] Fold bswap of single-byte-known-nonzero value to a shift (PR #193473)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 23 05:35:05 PDT 2026
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>,
=?utf-8?q?Pawe=C5=82?= Bylica <pawel at hepcolgum.band>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/193473 at github.com>
================
@@ -12133,6 +12133,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;
----------------
RKSimon wrote:
```suggestion
unsigned DstByte = (BW / 8) - 1 - SrcByte;
```
https://github.com/llvm/llvm-project/pull/193473
More information about the llvm-commits
mailing list