Skip to content

Commit 43ff75f

Browse files
committed
[AArch64][GlobalISel] Promote scalar G_SHL constant shift amounts to s64.
This was supposed to be done in the first place as is currently the case for G_ASHR and G_LSHR but was forgotten when the original shift legalization overhaul was done last year. This was exposed because we started falling back on s32 = s32, s64 SHLs due to a recent combiner change. Gives a very minor (0.1%) code size -O0 improvement on consumer-typeset.
1 parent 9caca72 commit 43ff75f

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,25 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
9797
.moreElementsToNextPow2(0);
9898

9999
getActionDefinitionsBuilder(G_SHL)
100-
.legalFor({{s32, s32}, {s64, s64},
101-
{v2s32, v2s32}, {v4s32, v4s32}, {v2s64, v2s64}})
102-
.clampScalar(1, s32, s64)
103-
.clampScalar(0, s32, s64)
104-
.widenScalarToNextPow2(0)
105-
.clampNumElements(0, v2s32, v4s32)
106-
.clampNumElements(0, v2s64, v2s64)
107-
.moreElementsToNextPow2(0)
108-
.minScalarSameAs(1, 0);
100+
.customIf([=](const LegalityQuery &Query) {
101+
const auto &SrcTy = Query.Types[0];
102+
const auto &AmtTy = Query.Types[1];
103+
return !SrcTy.isVector() && SrcTy.getSizeInBits() == 32 &&
104+
AmtTy.getSizeInBits() == 32;
105+
})
106+
.legalFor({{s32, s32},
107+
{s64, s64},
108+
{s32, s64},
109+
{v2s32, v2s32},
110+
{v4s32, v4s32},
111+
{v2s64, v2s64}})
112+
.clampScalar(1, s32, s64)
113+
.clampScalar(0, s32, s64)
114+
.widenScalarToNextPow2(0)
115+
.clampNumElements(0, v2s32, v4s32)
116+
.clampNumElements(0, v2s64, v2s64)
117+
.moreElementsToNextPow2(0)
118+
.minScalarSameAs(1, 0);
109119

110120
getActionDefinitionsBuilder(G_PTR_ADD)
111121
.legalFor({{p0, s64}, {v2p0, v2s64}})

llvm/test/CodeGen/AArch64/GlobalISel/legalize-merge-values.mir

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ name: test_merge_s4
66
body: |
77
bb.0:
88
; CHECK-LABEL: name: test_merge_s4
9-
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 4
9+
; CHECK: [[C:%[0-9]+]]:_(s8) = G_CONSTANT i8 4
1010
; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 15
1111
; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
1212
; CHECK: [[AND:%[0-9]+]]:_(s32) = G_AND [[C2]], [[C1]]
13-
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[AND]], [[C]](s32)
13+
; CHECK: [[C3:%[0-9]+]]:_(s64) = G_CONSTANT i64 4
14+
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[AND]], [[C3]](s64)
1415
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY [[C2]](s32)
1516
; CHECK: [[AND1:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C1]]
1617
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[SHL]](s32)

llvm/test/CodeGen/AArch64/GlobalISel/legalize-non-pow2-load-store.mir

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ body: |
2828
; CHECK: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 2
2929
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY]], [[C1]](s64)
3030
; CHECK: [[LOAD:%[0-9]+]]:_(s32) = G_LOAD [[PTR_ADD]](p0) :: (load 1 from %ir.ptr + 2, align 4)
31-
; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 16
32-
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[LOAD]], [[C2]](s32)
31+
; CHECK: [[C2:%[0-9]+]]:_(s64) = G_CONSTANT i64 16
32+
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[LOAD]], [[C2]](s64)
3333
; CHECK: [[OR:%[0-9]+]]:_(s32) = G_OR [[SHL]], [[ZEXTLOAD]]
3434
; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY [[OR]](s32)
35-
; CHECK: [[C3:%[0-9]+]]:_(s64) = G_CONSTANT i64 16
36-
; CHECK: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[COPY2]], [[C3]](s64)
35+
; CHECK: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[COPY2]], [[C2]](s64)
3736
; CHECK: [[PTR_ADD1:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY1]], [[C1]](s64)
3837
; CHECK: G_STORE [[COPY2]](s32), [[COPY1]](p0) :: (store 2 into %ir.ptr2, align 4)
3938
; CHECK: G_STORE [[LSHR]](s32), [[PTR_ADD1]](p0) :: (store 1 into %ir.ptr2 + 2, align 4)

llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ body: |
235235
236236
; CHECK-LABEL: name: shl_cimm_32
237237
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
238-
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 8
239-
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[COPY]], [[C]](s32)
238+
; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 8
239+
; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[COPY]], [[C]](s64)
240240
; CHECK: $w0 = COPY [[SHL]](s32)
241241
; CHECK: RET_ReallyLR implicit $w0
242242
%0:_(s32) = COPY $w0

llvm/test/CodeGen/AArch64/arm64-clrsb.ll

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ entry:
2121
; CHECK-LABEL: clrsb32
2222
; CHECK: cls [[TEMP:w[0-9]+]], [[TEMP]]
2323

24-
; FIXME: We should produce the same result here to save some code size. After
25-
; that, we can remove the GISEL special casing.
2624
; GISEL-LABEL: clrsb32
27-
; GISEL: clz
25+
; GISEL: cls [[TEMP:w[0-9]+]], [[TEMP]]
2826
}
2927

3028
; Function Attrs: nounwind ssp

0 commit comments

Comments
 (0)