[llvm] [InstCombine] Convert load from LUT into a select (PR #98339)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 06:14:04 PDT 2024
================
@@ -0,0 +1,236 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+ at arr2 = constant [2 x i32] [i32 0, i32 1], align 4
+ at arr2_mutable = global [4 x i32] [i32 0, i32 0, i32 1, i32 1], align 4
+ at arr2_external = external constant [4 x i32], align 4
+ at arr2_uniform = constant [2 x i32] [i32 1, i32 1], align 4
+ at arr2_undef = constant [2 x i32] [i32 1, i32 undef], align 4
+ at arr3 = constant [3 x i32] [i32 0, i32 1, i32 1], align 4
+ at arr3_alt = constant [3 x i32] [i32 1, i32 0, i32 1], align 4
+ at arr3_uniform = constant [3 x i32] [i32 1, i32 1, i32 1], align 4
+ at arr3_var = constant [3 x i32] [i32 0, i32 3, i32 4], align 4
+ at arr4_multimap = constant [4 x i32] [i32 0, i32 0, i32 1, i32 1], align 4
+
+define i32 @fold_arr2(i64 %x) {
+; CHECK-LABEL: define i32 @fold_arr2(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ne i64 [[X]], 0
+; CHECK-NEXT: [[VAL:%.*]] = zext i1 [[TMP0]] to i32
+; CHECK-NEXT: ret i32 [[VAL]]
+;
+entry:
+ %arrayidx = getelementptr [2 x i32], ptr @arr2, i64 0, i64 %x
+ %val = load i32, ptr %arrayidx, align 4
+ ret i32 %val
+}
+
+define i32 @fold_arr2_uniform(i64 %x) {
+; CHECK-LABEL: define i32 @fold_arr2_uniform(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret i32 1
+;
+entry:
+ %arrayidx = getelementptr [2 x i32], ptr @arr2_uniform, i64 0, i64 %x
+ %val = load i32, ptr %arrayidx, align 4
+ ret i32 %val
+}
+
+define i32 @fold_arr3(i64 %x) {
+; CHECK-LABEL: define i32 @fold_arr3(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ne i64 [[X]], 0
+; CHECK-NEXT: [[VAL:%.*]] = zext i1 [[TMP0]] to i32
+; CHECK-NEXT: ret i32 [[VAL]]
+;
+entry:
+ %arrayidx = getelementptr [3 x i32], ptr @arr3, i64 0, i64 %x
+ %val = load i32, ptr %arrayidx, align 4
+ ret i32 %val
+}
+
+define i32 @fold_arr3_alt(i64 %x) {
+; CHECK-LABEL: define i32 @fold_arr3_alt(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ne i64 [[X]], 1
+; CHECK-NEXT: [[VAL:%.*]] = zext i1 [[TMP0]] to i32
+; CHECK-NEXT: ret i32 [[VAL]]
+;
+entry:
+ %arrayidx = getelementptr [3 x i32], ptr @arr3_alt, i64 0, i64 %x
+ %val = load i32, ptr %arrayidx, align 4
+ ret i32 %val
+}
+
+define i32 @fold_arr3_uniform(i64 %x) {
+; CHECK-LABEL: define i32 @fold_arr3_uniform(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret i32 1
+;
+entry:
+ %arrayidx = getelementptr [3 x i32], ptr @arr3_uniform, i64 0, i64 %x
+ %val = load i32, ptr %arrayidx, align 4
+ ret i32 %val
+}
+
+; TODO: Handle ptradd pattern
----------------
nikic wrote:
Yes, but you don't need to deal with it now. I think we'll add a helper to do a linear decomposition because converting gep into Scale*Var+Offset is common for this kind of fold.
https://github.com/llvm/llvm-project/pull/98339
More information about the llvm-commits
mailing list