Skip to content

Commit d100631

Browse files
authored
[AArch64] Lower for power of 2 signed divides with scalar type (#97879)
Expected same assemble for code which doesn't use sve registers when we compile it with/without -msve-vector-bits=256. Fix #97821
1 parent 0e7590a commit d100631

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17732,13 +17732,14 @@ AArch64TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
1773217732
SmallVectorImpl<SDNode *> &Created) const {
1773317733
AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
1773417734
if (isIntDivCheap(N->getValueType(0), Attr))
17735-
return SDValue(N,0); // Lower SDIV as SDIV
17735+
return SDValue(N, 0); // Lower SDIV as SDIV
1773617736

1773717737
EVT VT = N->getValueType(0);
1773817738

1773917739
// For scalable and fixed types, mark them as cheap so we can handle it much
1774017740
// later. This allows us to handle larger than legal types.
17741-
if (VT.isScalableVector() || Subtarget->useSVEForFixedLengthVectors())
17741+
if (VT.isScalableVector() ||
17742+
(VT.isFixedLengthVector() && Subtarget->useSVEForFixedLengthVectors()))
1774217743
return SDValue(N, 0);
1774317744

1774417745
// fold (sdiv X, pow2)

llvm/test/CodeGen/AArch64/sdivpow2.ll

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ define i64 @test6(i64 %x) {
7777
define i64 @test7(i64 %x) {
7878
; CHECK-LABEL: test7:
7979
; CHECK: // %bb.0:
80-
; CHECK-NEXT: mov x8, #281474976710655
80+
; CHECK-NEXT: mov x8, #281474976710655 // =0xffffffffffff
8181
; CHECK-NEXT: cmp x0, #0
8282
; CHECK-NEXT: add x8, x0, x8
8383
; CHECK-NEXT: csel x8, x8, x0, lt
@@ -106,3 +106,32 @@ define i64 @test8(i64 %x) {
106106
ret i64 %div
107107
}
108108

109+
define i32 @sdiv_int(i32 %begin, i32 %first) #0 {
110+
; ISEL-LABEL: sdiv_int:
111+
; ISEL: // %bb.0:
112+
; ISEL-NEXT: sub w8, w0, w1
113+
; ISEL-NEXT: add w9, w8, #1
114+
; ISEL-NEXT: add w10, w8, #2
115+
; ISEL-NEXT: cmp w9, #0
116+
; ISEL-NEXT: csinc w8, w10, w8, lt
117+
; ISEL-NEXT: sub w0, w0, w8, asr #1
118+
; ISEL-NEXT: ret
119+
;
120+
; FAST-LABEL: sdiv_int:
121+
; FAST: // %bb.0:
122+
; FAST-NEXT: add w8, w0, #1
123+
; FAST-NEXT: sub w8, w8, w1
124+
; FAST-NEXT: add w9, w8, #1
125+
; FAST-NEXT: cmp w8, #0
126+
; FAST-NEXT: csel w8, w9, w8, lt
127+
; FAST-NEXT: neg w8, w8, asr #1
128+
; FAST-NEXT: add w0, w8, w0
129+
; FAST-NEXT: ret
130+
%sub = add i32 %begin, 1
131+
%add = sub i32 %sub, %first
132+
%div.neg = sdiv i32 %add, -2
133+
%sub1 = add i32 %div.neg, %begin
134+
ret i32 %sub1
135+
}
136+
137+
attributes #0 = { "target-features"="+sve" vscale_range(2,2) }

0 commit comments

Comments
 (0)