Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport llvm-project#97148][AArch64] Avoid overflow when using shl lower mul #174

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17153,16 +17153,27 @@ static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG,
unsigned ShiftAmt;

auto Shl = [&](SDValue N0, unsigned N1) {
if (!N0.getNode())
return SDValue();
// If shift causes overflow, ignore this combine.
if (N1 >= N0.getValueSizeInBits())
return SDValue();
SDValue RHS = DAG.getConstant(N1, DL, MVT::i64);
return DAG.getNode(ISD::SHL, DL, VT, N0, RHS);
};
auto Add = [&](SDValue N0, SDValue N1) {
if (!N0.getNode() || !N1.getNode())
return SDValue();
return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
};
auto Sub = [&](SDValue N0, SDValue N1) {
if (!N0.getNode() || !N1.getNode())
return SDValue();
return DAG.getNode(ISD::SUB, DL, VT, N0, N1);
};
auto Negate = [&](SDValue N) {
if (!N0.getNode())
return SDValue();
SDValue Zero = DAG.getConstant(0, DL, VT);
return DAG.getNode(ISD::SUB, DL, VT, Zero, N);
};
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/CodeGen/AArch64/mul_pow2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,41 @@ define <4 x i32> @muladd_demand_commute(<4 x i32> %x, <4 x i32> %y) {
%r = and <4 x i32> %a, <i32 131071, i32 131071, i32 131071, i32 131071>
ret <4 x i32> %r
}

; Transforming `(mul x, -(2^(N-M) - 1) * 2^M)` to `(sub (shl x, M), (shl x, N))`
; will cause overflow when N is 32 and M is 31.
define i32 @shift_overflow(i32 %x) {
; CHECK-LABEL: shift_overflow:
; CHECK: // %bb.0:
; CHECK-NEXT: mov w8, #-2147483648 // =0x80000000
; CHECK-NEXT: mul w0, w0, w8
; CHECK-NEXT: ret
;
; GISEL-LABEL: shift_overflow:
; GISEL: // %bb.0:
; GISEL-NEXT: mov w8, #-2147483648 // =0x80000000
; GISEL-NEXT: mul w0, w0, w8
; GISEL-NEXT: ret
%const = bitcast i32 2147483648 to i32
%r = mul i32 %x, %const
ret i32 %r
}

; Transforming `(mul x, -(2^(N-M) - 1) * 2^M)` to `(sub (shl x, M), (shl x, N))`
; will not cause overflow when N is 31 and M is 30.
define i32 @shift_no_overflow(i32 %x) {
; CHECK-LABEL: shift_no_overflow:
; CHECK: // %bb.0:
; CHECK-NEXT: lsl w8, w0, #31
; CHECK-NEXT: sub w0, w8, w0, lsl #30
; CHECK-NEXT: ret
;
; GISEL-LABEL: shift_no_overflow:
; GISEL: // %bb.0:
; GISEL-NEXT: mov w8, #1073741824 // =0x40000000
; GISEL-NEXT: mul w0, w0, w8
; GISEL-NEXT: ret
%const = bitcast i32 1073741824 to i32
%r = mul i32 %x, %const
ret i32 %r
}