[llvm] [LLVM][SimplifyCFG] Make validLookupTableConstant explicitly reject vector types. (PR #182555)
Paul Walker via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 09:50:38 PST 2026
https://github.com/paulwalker-arm created https://github.com/llvm/llvm-project/pull/182555
validLookupTableConstant indirectly rejects vectors through its selection of supported Constant classes. However, this is not sufficient when ContantInt/FP are used to represent constant vector splats and so I've added an explicit bailout.
>From 1cd3b0ec917628160c8a303c813f4cd452c52f4c Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Fri, 20 Feb 2026 11:32:02 +0000
Subject: [PATCH 1/2] Add tests showing change of switch-to-lookup-table
behaviour when using ConstantFP based splats.
---
.../switch-to-lookup-table-vector-splat.ll | 56 +++++++++++++++++++
1 file changed, 56 insertions(+)
create mode 100644 llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
diff --git a/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll b/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
new file mode 100644
index 0000000000000..581b6ee24d828
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes='simplifycfg<switch-to-lookup>' -S < %s | FileCheck %s --check-prefix=CHECK
+; RUN: opt -passes='simplifycfg<switch-to-lookup>' -use-constant-fp-for-fixed-length-splat -S < %s | FileCheck %s --check-prefix=CHECK-CFP
+
+target triple = "aarch64-unknown-linux-gnu"
+
+define <4 x float> @no_switch_to_lookup_table_vector_splat(i32 %c) {
+; CHECK-LABEL: define <4 x float> @no_switch_to_lookup_table_vector_splat(
+; CHECK-SAME: i32 [[C:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: switch i32 [[C]], label %[[DEFAULT:.*]] [
+; CHECK-NEXT: i32 0, label %[[RET:.*]]
+; CHECK-NEXT: i32 1, label %[[BB1:.*]]
+; CHECK-NEXT: i32 2, label %[[BB2:.*]]
+; CHECK-NEXT: ]
+; CHECK: [[BB1]]:
+; CHECK-NEXT: br label %[[RET]]
+; CHECK: [[BB2]]:
+; CHECK-NEXT: br label %[[RET]]
+; CHECK: [[DEFAULT]]:
+; CHECK-NEXT: unreachable
+; CHECK: [[RET]]:
+; CHECK-NEXT: [[PHI:%.*]] = phi <4 x float> [ splat (float 3.000000e+00), %[[BB2]] ], [ splat (float 2.000000e+00), %[[BB1]] ], [ splat (float 1.000000e+00), %[[ENTRY]] ]
+; CHECK-NEXT: ret <4 x float> [[PHI]]
+;
+; CHECK-CFP-LABEL: define <4 x float> @no_switch_to_lookup_table_vector_splat(
+; CHECK-CFP-SAME: i32 [[C:%.*]]) {
+; CHECK-CFP-NEXT: [[SWITCH_LOOKUP:.*:]]
+; CHECK-CFP-NEXT: [[TMP1:%.*]] = zext nneg i32 [[C]] to i64
+; CHECK-CFP-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [3 x <4 x float>], ptr @switch.table.no_switch_to_lookup_table_vector_splat, i64 0, i64 [[TMP1]]
+; CHECK-CFP-NEXT: [[SWITCH_LOAD:%.*]] = load <4 x float>, ptr [[SWITCH_GEP]], align 16
+; CHECK-CFP-NEXT: ret <4 x float> [[SWITCH_LOAD]]
+;
+entry:
+ switch i32 %c, label %default [
+ i32 0, label %bb0
+ i32 1, label %bb1
+ i32 2, label %bb2
+ ]
+
+bb0:
+ br label %ret
+
+bb1:
+ br label %ret
+
+bb2:
+ br label %ret
+
+default:
+ unreachable
+
+ret:
+ %phi = phi <4 x float> [ splat (float 1.0), %bb0 ], [ splat (float 2.0), %bb1 ], [ splat (float 3.0), %bb2 ]
+ ret <4 x float> %phi
+}
>From df4cb168a907ecebb8a8a3fa044bcb171062de76 Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Fri, 20 Feb 2026 11:36:26 +0000
Subject: [PATCH 2/2] [LLVM][SimplifyCFG] Make validLookupTableConstant
explicitly reject vector types.
validLookupTableConstant indirectly rejects vectors through its
selection of supported Constant classes. However, this is not
sufficient when ContantInt/FP are used to represent constant vector
splats and so I've added an explicit bailout.
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 3 +++
.../AArch64/switch-to-lookup-table-vector-splat.ll | 13 ++-----------
2 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index b912baef6188d..fd9aff0f9fef2 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6324,6 +6324,9 @@ static bool validLookupTableConstant(Constant *C, const TargetTransformInfo &TTI
!isa<UndefValue>(C) && !isa<ConstantExpr>(C))
return false;
+ if (C->getType()->isVectorTy())
+ return false;
+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
// Pointer casts and in-bounds GEPs will not prohibit the backend from
// materializing the array of constants.
diff --git a/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll b/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
index 581b6ee24d828..6a14c6a909f92 100644
--- a/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
+++ b/llvm/test/Transforms/SimplifyCFG/AArch64/switch-to-lookup-table-vector-splat.ll
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt -passes='simplifycfg<switch-to-lookup>' -S < %s | FileCheck %s --check-prefix=CHECK
-; RUN: opt -passes='simplifycfg<switch-to-lookup>' -use-constant-fp-for-fixed-length-splat -S < %s | FileCheck %s --check-prefix=CHECK-CFP
+; RUN: opt -passes='simplifycfg<switch-to-lookup>' -S < %s | FileCheck %s
+; RUN: opt -passes='simplifycfg<switch-to-lookup>' -use-constant-fp-for-fixed-length-splat -S < %s | FileCheck %s
target triple = "aarch64-unknown-linux-gnu"
@@ -22,15 +22,6 @@ define <4 x float> @no_switch_to_lookup_table_vector_splat(i32 %c) {
; CHECK: [[RET]]:
; CHECK-NEXT: [[PHI:%.*]] = phi <4 x float> [ splat (float 3.000000e+00), %[[BB2]] ], [ splat (float 2.000000e+00), %[[BB1]] ], [ splat (float 1.000000e+00), %[[ENTRY]] ]
; CHECK-NEXT: ret <4 x float> [[PHI]]
-;
-; CHECK-CFP-LABEL: define <4 x float> @no_switch_to_lookup_table_vector_splat(
-; CHECK-CFP-SAME: i32 [[C:%.*]]) {
-; CHECK-CFP-NEXT: [[SWITCH_LOOKUP:.*:]]
-; CHECK-CFP-NEXT: [[TMP1:%.*]] = zext nneg i32 [[C]] to i64
-; CHECK-CFP-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [3 x <4 x float>], ptr @switch.table.no_switch_to_lookup_table_vector_splat, i64 0, i64 [[TMP1]]
-; CHECK-CFP-NEXT: [[SWITCH_LOAD:%.*]] = load <4 x float>, ptr [[SWITCH_GEP]], align 16
-; CHECK-CFP-NEXT: ret <4 x float> [[SWITCH_LOAD]]
-;
entry:
switch i32 %c, label %default [
i32 0, label %bb0
More information about the llvm-commits
mailing list