[llvm] deadda7 - [InstCombine] Add more memrchr tests (NFC).
Martin Sebor via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 12 10:56:22 PDT 2022
Author: Martin Sebor
Date: 2022-04-12T11:55:33-06:00
New Revision: deadda749aef22dba4727f5c4d76090ecca559ac
URL: https://github.com/llvm/llvm-project/commit/deadda749aef22dba4727f5c4d76090ecca559ac
DIFF: https://github.com/llvm/llvm-project/commit/deadda749aef22dba4727f5c4d76090ecca559ac.diff
LOG: [InstCombine] Add more memrchr tests (NFC).
Added:
llvm/test/Transforms/InstCombine/memrchr-2.ll
llvm/test/Transforms/InstCombine/memrchr-3.ll
llvm/test/Transforms/InstCombine/memrchr-4.ll
Modified:
Removed:
################################################################################
diff --git a/llvm/test/Transforms/InstCombine/memrchr-2.ll b/llvm/test/Transforms/InstCombine/memrchr-2.ll
new file mode 100644
index 0000000000000..7c419d83f8469
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/memrchr-2.ll
@@ -0,0 +1,85 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+;
+; Verify that memrchr calls with an out of bounds size are folded to null.
+
+declare i8* @memrchr(i8*, i32, i64)
+
+ at ax = external global [0 x i8]
+ at ax1 = external global [1 x i8]
+ at a12345 = constant [5 x i8] c"\01\02\03\04\05"
+
+
+; Fold memrchr(a12345, 1, UINT32_MAX + 1LU) to null (and not to a12345
+; as might happen if the size were to be truncated to int32_t).
+
+define i8* @fold_memrchr_a12345_1_ui32max_p1(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_a12345_1_ui32max_p1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967296)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+; CHECK : ret i8* null
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4294967296)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(ax1, 1, UINT32_MAX + 2LU) to null (and not to *ax1 == 1).
+
+define i8* @fold_memrchr_ax1_1_ui32max_p2(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_ax1_1_ui32max_p2(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([1 x i8], [1 x i8]* @ax1, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+; CHECK : ret i8* null
+
+ %ptr = getelementptr [1 x i8], [1 x i8]* @ax1, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4294967297)
+ ret i8* %ret
+}
+
+
+; But don't fold memrchr(ax, 1, UINT32_MAX + 2LU) to *ax == 1.
+
+define i8* @fold_memrchr_ax_1_ui32max_p2(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_ax_1_ui32max_p2(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4294967297)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, c, 6) to null.
+
+define i8* @fold_memrchr_a12345_c_6(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_a12345_c_6(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 6)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+; CHECK : ret i8* null
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 6)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, c, SIZE_MAX) to null.
+
+define i8* @fold_memrchr_a12345_c_szmax(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_a12345_c_szmax(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 -1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+; CHECK : ret i8* null
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 18446744073709551615)
+ ret i8* %ret
+}
diff --git a/llvm/test/Transforms/InstCombine/memrchr-3.ll b/llvm/test/Transforms/InstCombine/memrchr-3.ll
new file mode 100644
index 0000000000000..a53e7bf66b9ec
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/memrchr-3.ll
@@ -0,0 +1,256 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+;
+; Verify that memrchr calls with one or more constant arguments are folded
+; as expected.
+
+declare i8* @memrchr(i8*, i32, i64)
+
+ at ax = external global [0 x i8]
+ at a12345 = constant [5 x i8] c"\01\02\03\04\05"
+ at a123123 = constant [6 x i8] c"\01\02\03\01\02\03"
+
+
+; Fold memrchr(ax, c, 0) to null.
+
+define i8* @fold_memrchr_ax_c_0(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_ax_c_0(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), i32 [[TMP0:%.*]], i64 0)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [0 x i8], [0 x i8]* @ax, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 0)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 3, 0) to null.
+
+define i8* @fold_memrchr_a12345_3_0() {
+; CHECK-LABEL: @fold_memrchr_a12345_3_0(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 3, i64 0)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 3, i64 0)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 1, 1) to a12345.
+
+define i8* @fold_memrchr_a12345_1_1() {
+; CHECK-LABEL: @fold_memrchr_a12345_1_1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 1, i64 1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 1, i64 1)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 5, 1) to null.
+
+define i8* @fold_memrchr_a12345_5_1() {
+; CHECK-LABEL: @fold_memrchr_a12345_5_1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 5, i64 1)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a123123, 1, 1) to a123123.
+
+define i8* @fold_memrchr_a123123_1_1() {
+; CHECK-LABEL: @fold_memrchr_a123123_1_1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 1, i64 1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 1, i64 1)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a123123, 3, 1) to null.
+
+define i8* @fold_memrchr_a123123_3_1() {
+; CHECK-LABEL: @fold_memrchr_a123123_3_1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 3, i64 1)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(ax, c, 1) to *ax == c ? ax : null.
+
+define i8* @fold_memrchr_ax_c_1(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_ax_c_1(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), i32 [[TMP0:%.*]], i64 1)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+ %ptr = getelementptr [0 x i8], [0 x i8]* @ax, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 1)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 5, 5) to a12345 + 4.
+
+define i8* @fold_memrchr_a12345_5_5() {
+; CHECK-LABEL: @fold_memrchr_a12345_5_5(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 5)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 5, i64 5)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 5, 4) to null.
+
+define i8* @fold_memrchr_a12345_5_4() {
+; CHECK-LABEL: @fold_memrchr_a12345_5_4(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 4)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 5, i64 4)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 4, 5) to a12345 + 3.
+
+define i8* @fold_memrchr_a12345_4_5() {
+; CHECK-LABEL: @fold_memrchr_a12345_4_5(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 4, i64 5)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 4, i64 5)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345 + 1, 1, 4) to null.
+
+define i8* @fold_memrchr_a12345p1_1_4() {
+; CHECK-LABEL: @fold_memrchr_a12345p1_1_4(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 1), i32 5, i64 4)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 1
+ %ret = call i8* @memrchr(i8* %ptr, i32 5, i64 4)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 2, 5) to a12345 + 1.
+
+define i8* @fold_memrchr_a12345_2_5() {
+; CHECK-LABEL: @fold_memrchr_a12345_2_5(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 2, i64 5)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 2, i64 5)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a12345, 0, %0) to null.
+
+define i8* @fold_memrchr_a12345_0_n(i64 %0) {
+; CHECK-LABEL: @fold_memrchr_a12345_0_n(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 0, i64 [[TMP0:%.*]])
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 0, i64 %0)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a123123, 3, 5) to a123123 + 2.
+
+define i8* @fold_memrchr_a123123_3_5() {
+; CHECK-LABEL: @fold_memrchr_a123123_3_5(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 5)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 3, i64 5)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a123123, 3, 6) to a123123 + 5.
+
+define i8* @fold_memrchr_a123123_3_6() {
+; CHECK-LABEL: @fold_memrchr_a123123_3_6(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 6)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 3, i64 6)
+ ret i8* %ret
+}
+
+; Fold memrchr(a123123, 2, 6) to a123123 + 4.
+
+define i8* @fold_memrchr_a123123_2_6() {
+; CHECK-LABEL: @fold_memrchr_a123123_2_6(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 2, i64 6)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 2, i64 6)
+ ret i8* %ret
+}
+
+; Fold memrchr(a123123, 1, 6) to a123123 + 3.
+
+define i8* @fold_memrchr_a123123_1_6() {
+; CHECK-LABEL: @fold_memrchr_a123123_1_6(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 1, i64 6)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 1, i64 6)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a123123, 0, 6) to null.
+
+define i8* @fold_memrchr_a123123_0_6() {
+; CHECK-LABEL: @fold_memrchr_a123123_0_6(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 0, i64 6)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 0, i64 6)
+ ret i8* %ret
+}
diff --git a/llvm/test/Transforms/InstCombine/memrchr-4.ll b/llvm/test/Transforms/InstCombine/memrchr-4.ll
new file mode 100644
index 0000000000000..c5e6fba3c3f1c
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/memrchr-4.ll
@@ -0,0 +1,66 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+;
+; Verify that memrchr calls with a string consisting of all the same
+; characters are folded.
+
+declare i8* @memrchr(i8*, i32, i64)
+
+ at a11111 = constant [5 x i8] c"\01\01\01\01\01"
+ at a1110111 = constant [7 x i8] c"\01\01\01\00\01\01\01"
+
+
+; Fold memrchr(a11111, c, 5) to *a11111 == c ? a11111 + 5 - 1 : null.
+
+define i8* @fold_memrchr_a11111_c_5(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_a11111_c_5(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 5)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 5)
+ ret i8* %ret
+}
+
+
+; Fold memrchr(a1110111, c, 3) to a1110111[2] == c ? a1110111 + 2 : null.
+
+define i8* @fold_memrchr_a1110111_c_3(i32 %0) {
+; CHECK-LABEL: @fold_memrchr_a1110111_c_3(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 3)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 3)
+ ret i8* %ret
+}
+
+
+; Don't fold memrchr(a1110111, c, 4).
+
+define i8* @call_memrchr_a1110111_c_4(i32 %0) {
+; CHECK-LABEL: @call_memrchr_a1110111_c_4(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4)
+ ret i8* %ret
+}
+
+
+; Don't fold memrchr(a1110111, c, 7).
+
+define i8* @call_memrchr_a11111_c_7(i32 %0) {
+; CHECK-LABEL: @call_memrchr_a11111_c_7(
+; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 7)
+; CHECK-NEXT: ret i8* [[RET]]
+;
+
+ %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
+ %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 7)
+ ret i8* %ret
+}
More information about the llvm-commits
mailing list