Skip to content

Commit b4e1466

Browse files
committed
[AArch64] Improve codegen for shifted mask op
The special case for bit extraction pattern is `((x >> C) & mask) << C`. It can be combined to `x & (mask << C)` by return true in isDesirableToCommuteWithShift. Fix: llvm#56427 Reviewed By: dmgreen Differential Revision: https://reviews.llvm.org/D136014
1 parent 91f62f0 commit b4e1466

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14442,15 +14442,23 @@ AArch64TargetLowering::isDesirableToCommuteWithShift(const SDNode *N,
1444214442
SDValue ShiftLHS = N->getOperand(0);
1444314443
EVT VT = N->getValueType(0);
1444414444

14445-
// If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not combine
14446-
// it with shift 'N' to let it be lowered to UBFX.
14445+
// If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not
14446+
// combine it with shift 'N' to let it be lowered to UBFX except:
14447+
// ((x >> C) & mask) << C.
1444714448
if (ShiftLHS.getOpcode() == ISD::AND && (VT == MVT::i32 || VT == MVT::i64) &&
1444814449
isa<ConstantSDNode>(ShiftLHS.getOperand(1))) {
1444914450
uint64_t TruncMask = ShiftLHS.getConstantOperandVal(1);
14450-
if (isMask_64(TruncMask) &&
14451-
ShiftLHS.getOperand(0).getOpcode() == ISD::SRL &&
14452-
isa<ConstantSDNode>(ShiftLHS.getOperand(0).getOperand(1)))
14453-
return false;
14451+
if (isMask_64(TruncMask)) {
14452+
SDValue AndLHS = ShiftLHS.getOperand(0);
14453+
if (AndLHS.getOpcode() == ISD::SRL) {
14454+
if (auto *SRLC = dyn_cast<ConstantSDNode>(AndLHS.getOperand(1))) {
14455+
if (N->getOpcode() == ISD::SHL)
14456+
if (auto *SHLC = dyn_cast<ConstantSDNode>(N->getOperand(1)))
14457+
return SRLC->getAPIntValue() == SHLC->getAPIntValue();
14458+
return false;
14459+
}
14460+
}
14461+
}
1445414462
}
1445514463
return true;
1445614464
}

llvm/test/CodeGen/AArch64/shift-logic.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,27 @@ define i32 @lshr_or_extra_use(i32 %x, i32 %y, i32* %p) nounwind {
151151
%sh1 = lshr i32 %r, 7
152152
ret i32 %sh1
153153
}
154+
155+
define i64 @desirable_to_commute1(i64 %x) {
156+
; CHECK-LABEL: desirable_to_commute1:
157+
; CHECK: // %bb.0:
158+
; CHECK-NEXT: and x0, x0, #0x7fff8
159+
; CHECK-NEXT: ret
160+
%s1 = lshr i64 %x, 3
161+
%a = and i64 %s1, 65535
162+
%s2 = shl i64 %a, 3
163+
ret i64 %s2
164+
}
165+
166+
define i64 @desirable_to_commute2(i64* %p, i64 %i) {
167+
; CHECK-LABEL: desirable_to_commute2:
168+
; CHECK: // %bb.0:
169+
; CHECK-NEXT: and x8, x1, #0x1ff8
170+
; CHECK-NEXT: ldr x0, [x0, x8]
171+
; CHECK-NEXT: ret
172+
%lshr = lshr i64 %i, 3
173+
%and = and i64 %lshr, 1023
174+
%pidx = getelementptr i64, i64* %p, i64 %and
175+
%r = load i64, i64* %pidx
176+
ret i64 %r
177+
}

0 commit comments

Comments
 (0)