Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -9968,6 +9968,35 @@ : DAG.getZExtOrTrunc(Result, DL, WideVT)); } +// fold (bswap (logic_op(bswap(x),y))) -> logic_op(x,bswap(y)) +// This helper function accept ISD::BSWAP and ISD::BITREVERSE in Opcode +// parameter +static SDValue foldBitOrderCrossLogicOp(SDNode *N, SelectionDAG &DAG) { + unsigned Opcode = N->getOpcode(); + if (Opcode != ISD::BSWAP && Opcode != ISD::BITREVERSE) + return SDValue(); + + SDValue N0 = N->getOperand(0); + EVT VT = N->getValueType(0); + SDLoc DL(N); + if (ISD::isBitwiseLogicOp(N0.getOpcode()) && N0.hasOneUse()) { + SDValue OldLHS = N0.getOperand(0); + SDValue OldRHS = N0.getOperand(1); + + // Need to ensure logic_op and bswap/bitreverse(x) doesn't have other uses + if (OldLHS.getOpcode() == Opcode && OldLHS.hasOneUse()) { + SDValue NewBitReorder = DAG.getNode(Opcode, DL, VT, OldRHS); + return DAG.getNode(N0.getOpcode(), DL, VT, OldLHS.getOperand(0), + NewBitReorder); + } else if (OldRHS.getOpcode() == Opcode && OldRHS.hasOneUse()) { + SDValue NewBitReorder = DAG.getNode(Opcode, DL, VT, OldLHS); + return DAG.getNode(N0.getOpcode(), DL, VT, OldRHS.getOperand(0), + NewBitReorder); + } + } + return SDValue(); +} + SDValue DAGCombiner::visitSRA(SDNode *N) { SDValue N0 = N->getOperand(0); SDValue N1 = N->getOperand(1); @@ -10753,6 +10782,9 @@ } } + if (SDValue V = foldBitOrderCrossLogicOp(N, DAG)) + return V; + return SDValue(); } Index: llvm/test/CodeGen/X86/combine-bswap.ll =================================================================== --- llvm/test/CodeGen/X86/combine-bswap.ll +++ llvm/test/CodeGen/X86/combine-bswap.ll @@ -261,15 +261,13 @@ ; X86-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-NEXT: bswapl %eax ; X86-NEXT: andl {{[0-9]+}}(%esp), %eax -; X86-NEXT: bswapl %eax ; X86-NEXT: retl ; ; X64-LABEL: bs_and_lhs_bs32: ; X64: # %bb.0: -; X64-NEXT: movl %edi, %eax -; X64-NEXT: bswapl %eax -; X64-NEXT: andl %esi, %eax +; X64-NEXT: movl %esi, %eax ; X64-NEXT: bswapl %eax +; X64-NEXT: andl %edi, %eax ; X64-NEXT: retq %1 = tail call i32 @llvm.bswap.i32(i32 %a) %2 = and i32 %1, %b @@ -280,22 +278,19 @@ define i64 @bs_and_lhs_bs64(i64 %a, i64 %b) #0 { ; X86-LABEL: bs_and_lhs_bs64: ; X86: # %bb.0: -; X86-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-NEXT: movl {{[0-9]+}}(%esp), %edx +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-NEXT: bswapl %eax -; X86-NEXT: bswapl %edx -; X86-NEXT: andl {{[0-9]+}}(%esp), %edx ; X86-NEXT: andl {{[0-9]+}}(%esp), %eax -; X86-NEXT: bswapl %eax ; X86-NEXT: bswapl %edx +; X86-NEXT: andl {{[0-9]+}}(%esp), %edx ; X86-NEXT: retl ; ; X64-LABEL: bs_and_lhs_bs64: ; X64: # %bb.0: -; X64-NEXT: movq %rdi, %rax -; X64-NEXT: bswapq %rax -; X64-NEXT: andq %rsi, %rax +; X64-NEXT: movq %rsi, %rax ; X64-NEXT: bswapq %rax +; X64-NEXT: andq %rdi, %rax ; X64-NEXT: retq %1 = tail call i64 @llvm.bswap.i64(i64 %a) %2 = and i64 %1, %b @@ -306,22 +301,19 @@ define i64 @bs_and_rhs_bs64(i64 %a, i64 %b) #0 { ; X86-LABEL: bs_and_rhs_bs64: ; X86: # %bb.0: -; X86-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-NEXT: movl {{[0-9]+}}(%esp), %edx +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-NEXT: bswapl %eax -; X86-NEXT: bswapl %edx -; X86-NEXT: andl {{[0-9]+}}(%esp), %edx ; X86-NEXT: andl {{[0-9]+}}(%esp), %eax -; X86-NEXT: bswapl %eax ; X86-NEXT: bswapl %edx +; X86-NEXT: andl {{[0-9]+}}(%esp), %edx ; X86-NEXT: retl ; ; X64-LABEL: bs_and_rhs_bs64: ; X64: # %bb.0: -; X64-NEXT: movq %rsi, %rax -; X64-NEXT: bswapq %rax -; X64-NEXT: andq %rdi, %rax +; X64-NEXT: movq %rdi, %rax ; X64-NEXT: bswapq %rax +; X64-NEXT: andq %rsi, %rax ; X64-NEXT: retq %1 = tail call i64 @llvm.bswap.i64(i64 %b) %2 = and i64 %1, %a