[llvm] [DAGCombine] Improve bswap lowering for machines that support bit rotates (PR #164848)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 24 10:32:37 PDT 2025
================
@@ -9899,6 +9899,17 @@ SDValue TargetLowering::expandBSWAP(SDNode *N, SelectionDAG &DAG) const {
// Use a rotate by 8. This can be further expanded if necessary.
return DAG.getNode(ISD::ROTL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
case MVT::i32:
+ if (isOperationLegal(ISD::ROTR, VT)) {
+ SDValue Mask = DAG.getConstant(0x00FF00FF, dl, VT);
+ // (x & 0x00FF00FF) rotr 8 | (x rotl 8) & 0x00FF00FF
+ SDValue And = DAG.getNode(ISD::AND, dl, VT, Op, Mask);
+ SDValue Rotr =
+ DAG.getNode(ISD::ROTR, dl, VT, And, DAG.getConstant(8, dl, SHVT));
+ SDValue Rotl =
+ DAG.getNode(ISD::ROTL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
----------------
topperc wrote:
If you only check legality of ROTR, you probably should use ROTR by 24 here instead of ROTL by 8.
https://github.com/llvm/llvm-project/pull/164848
More information about the llvm-commits
mailing list