Index: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp =================================================================== --- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -14381,15 +14381,24 @@ SDValue ShiftLHS = N->getOperand(0); EVT VT = N->getValueType(0); - // If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not combine - // it with shift 'N' to let it be lowered to UBFX. + // If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not + // combine it with shift 'N' to let it be lowered to UBFX. + // The special case is ((x >> C) & mask) << C. + // It can be combine to x & (mask << C) by return true if (ShiftLHS.getOpcode() == ISD::AND && (VT == MVT::i32 || VT == MVT::i64) && isa(ShiftLHS.getOperand(1))) { uint64_t TruncMask = ShiftLHS.getConstantOperandVal(1); - if (isMask_64(TruncMask) && - ShiftLHS.getOperand(0).getOpcode() == ISD::SRL && - isa(ShiftLHS.getOperand(0).getOperand(1))) - return false; + if (isMask_64(TruncMask)) { + SDValue AndLHS = ShiftLHS.getOperand(0); + if (AndLHS.getOpcode() == ISD::SRL) { + if (auto *SRLC = dyn_cast(AndLHS.getOperand(1))) { + if (N->getOpcode() == ISD::SHL) + if (auto *SHLC = dyn_cast(N->getOperand(1))) + return SRLC->getAPIntValue() == SHLC->getAPIntValue(); + return false; + } + } + } } return true; } Index: llvm/test/CodeGen/AArch64/pr56427.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/AArch64/pr56427.ll @@ -0,0 +1,13 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc < %s -mtriple=aarch64-none-linux-gnu | FileCheck %s + +define i64 @src(i64 %x) { +; CHECK-LABEL: src: +; CHECK: // %bb.0: +; CHECK-NEXT: and x0, x0, #0x7fff8 +; CHECK-NEXT: ret + %s1 = lshr i64 %x, 3 + %a = and i64 %s1, 65535 + %s2 = shl i64 %a, 3 + ret i64 %s2 +}