[llvm] 4d58566 - [GlobalISel][AArch64] Improve legalization of shift amounts (#208676)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 10 23:41:59 PDT 2026


Author: Ömer Sinan Ağacan
Date: 2026-07-11T08:41:54+02:00
New Revision: 4d5856669efa7a1271d2f5201bb3ebf32420ffde

URL: https://github.com/llvm/llvm-project/commit/4d5856669efa7a1271d2f5201bb3ebf32420ffde
DIFF: https://github.com/llvm/llvm-project/commit/4d5856669efa7a1271d2f5201bb3ebf32420ffde.diff

LOG: [GlobalISel][AArch64] Improve legalization of shift amounts (#208676)

Fixes crashes when the shift amount type is already between s32 and s64,
but not s32 or s64.

The shift amount should have the same type with the shifted value for
the shift instructions, so add the same `widenScalarToNextPow2`
legalization that we apply to the shifted value, to the shift amount.

Fixes crashes in programs like:

    define i8 @test(i48 %a) {
    entry:
      %b = lshr i48 %a, 15
      %c = trunc i48 %b to i8
      ret i8 %c
    }

The new test crashes before this PR.

Added: 
    

Modified: 
    llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
    llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
index e4e58327ead53..2e1387c31b7d7 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
@@ -252,6 +252,7 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
           {v4i32, v4i32},
           {v2i64, v2i64},
       })
+      .widenScalarToNextPow2(1)
       .widenScalarToNextPow2(0)
       .clampScalar(1, s32, s64)
       .clampScalar(0, s32, s64)

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir
index 9f316b5125c04..272cb114580d0 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-shift.mir
@@ -552,3 +552,27 @@ body:             |
     $w0 = COPY %d2(s32)
 
 ...
+---
+name: test_lshr_i48
+body:             |
+  bb.1.entry:
+    liveins: $x0
+
+    ; CHECK-LABEL: name: test_lshr_i48
+    ; CHECK: liveins: $x0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(i64) = COPY $x0
+    ; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(i32) = G_TRUNC [[COPY]](i64)
+    ; CHECK-NEXT: [[C:%[0-9]+]]:_(i64) = G_CONSTANT i64 1
+    ; CHECK-NEXT: [[LSHR:%[0-9]+]]:_(i32) = G_LSHR [[TRUNC]], [[C]](i64)
+    ; CHECK-NEXT: $w0 = COPY [[LSHR]](i32)
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %1:_(i64) = COPY $x0
+    %0:_(i48) = G_TRUNC %1(i64)
+    %6:_(i32) = G_TRUNC %0(i48)
+    %2:_(i48) = G_CONSTANT i48 1
+    %7:_(i32) = G_LSHR %6, %2(i48)
+    $w0 = COPY %7(i32)
+    RET_ReallyLR implicit $w0
+
+...


        


More information about the llvm-commits mailing list