[llvm] 25871b3 - [InstSimplify] Add tests for ptrtoint of gep fold (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 12 08:14:04 PDT 2024
Author: Nikita Popov
Date: 2024-07-12T17:13:56+02:00
New Revision: 25871b35050d255be67834286d035ffe6e2c2d72
URL: https://github.com/llvm/llvm-project/commit/25871b35050d255be67834286d035ffe6e2c2d72
DIFF: https://github.com/llvm/llvm-project/commit/25871b35050d255be67834286d035ffe6e2c2d72.diff
LOG: [InstSimplify] Add tests for ptrtoint of gep fold (NFC)
Added:
llvm/test/Transforms/InstSimplify/ptrtoint.ll
Modified:
Removed:
################################################################################
diff --git a/llvm/test/Transforms/InstSimplify/ptrtoint.ll b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
new file mode 100644
index 0000000000000..55a5a0d452f10
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/ptrtoint.ll
@@ -0,0 +1,146 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
+
+define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
+; CHECK-LABEL: define i64 @ptrtoint_gep_sub(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
+; CHECK-NEXT: ret i64 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i64
+ %size = sub i64 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i64
+ ret i64 %end.addr2
+}
+
+define <2 x i64> @ptrtoint_gep_sub_vector(<2 x ptr> %ptr, <2 x i64> %end.addr) {
+; CHECK-LABEL: define <2 x i64> @ptrtoint_gep_sub_vector(
+; CHECK-SAME: <2 x ptr> [[PTR:%.*]], <2 x i64> [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint <2 x ptr> [[PTR]] to <2 x i64>
+; CHECK-NEXT: [[SIZE:%.*]] = sub <2 x i64> [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, <2 x ptr> [[PTR]], <2 x i64> [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint <2 x ptr> [[END]] to <2 x i64>
+; CHECK-NEXT: ret <2 x i64> [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint <2 x ptr> %ptr to <2 x i64>
+ %size = sub <2 x i64> %end.addr, %ptr.addr
+ %end = getelementptr i8, <2 x ptr> %ptr, <2 x i64> %size
+ %end.addr2 = ptrtoint <2 x ptr> %end to <2 x i64>
+ ret <2 x i64> %end.addr2
+}
+
+define i64 @ptrtoint_gep_sub_wrong_gep_type(ptr %ptr, i64 %end.addr) {
+; CHECK-LABEL: define i64 @ptrtoint_gep_sub_wrong_gep_type(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i32, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
+; CHECK-NEXT: ret i64 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i64
+ %size = sub i64 %end.addr, %ptr.addr
+ %end = getelementptr i32, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i64
+ ret i64 %end.addr2
+}
+
+define i64 @ptrtoint_gep_sub_wrong_ptrtoint_op(ptr %ptr, i64 %end.addr, ptr %ptr2) {
+; CHECK-LABEL: define i64 @ptrtoint_gep_sub_wrong_ptrtoint_op(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]], ptr [[PTR2:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR2]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
+; CHECK-NEXT: ret i64 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr2 to i64
+ %size = sub i64 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i64
+ ret i64 %end.addr2
+}
+
+define i64 @ptrtoint_gep_sub_wrong_operation(ptr %ptr, i64 %end.addr) {
+; CHECK-LABEL: define i64 @ptrtoint_gep_sub_wrong_operation(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = add i64 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
+; CHECK-NEXT: ret i64 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i64
+ %size = add i64 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i64
+ ret i64 %end.addr2
+}
+
+define i64 @ptrtoint_gep_sub_wrong_operand_order(ptr %ptr, i64 %end.addr) {
+; CHECK-LABEL: define i64 @ptrtoint_gep_sub_wrong_operand_order(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[PTR_ADDR]], [[END_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i64
+; CHECK-NEXT: ret i64 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i64
+ %size = sub i64 %ptr.addr, %end.addr
+ %end = getelementptr i8, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i64
+ ret i64 %end.addr2
+}
+
+define i32 @ptrtoint_gep_sub_wrong_type(ptr %ptr, i64 %end.addr) {
+; CHECK-LABEL: define i32 @ptrtoint_gep_sub_wrong_type(
+; CHECK-SAME: ptr [[PTR:%.*]], i64 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i64
+; CHECK-NEXT: [[SIZE:%.*]] = sub i64 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i32
+; CHECK-NEXT: ret i32 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i64
+ %size = sub i64 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i64 %size
+ %end.addr2 = ptrtoint ptr %end to i32
+ ret i32 %end.addr2
+}
+
+define i32 @ptrtoint_gep_sub_narrow_type(ptr %ptr, i32 %end.addr) {
+; CHECK-LABEL: define i32 @ptrtoint_gep_sub_narrow_type(
+; CHECK-SAME: ptr [[PTR:%.*]], i32 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i32
+; CHECK-NEXT: [[SIZE:%.*]] = sub i32 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i32 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i32
+; CHECK-NEXT: ret i32 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i32
+ %size = sub i32 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i32 %size
+ %end.addr2 = ptrtoint ptr %end to i32
+ ret i32 %end.addr2
+}
+
+define i128 @ptrtoint_gep_sub_wide_type(ptr %ptr, i128 %end.addr) {
+; CHECK-LABEL: define i128 @ptrtoint_gep_sub_wide_type(
+; CHECK-SAME: ptr [[PTR:%.*]], i128 [[END_ADDR:%.*]]) {
+; CHECK-NEXT: [[PTR_ADDR:%.*]] = ptrtoint ptr [[PTR]] to i128
+; CHECK-NEXT: [[SIZE:%.*]] = sub i128 [[END_ADDR]], [[PTR_ADDR]]
+; CHECK-NEXT: [[END:%.*]] = getelementptr i8, ptr [[PTR]], i128 [[SIZE]]
+; CHECK-NEXT: [[END_ADDR2:%.*]] = ptrtoint ptr [[END]] to i128
+; CHECK-NEXT: ret i128 [[END_ADDR2]]
+;
+ %ptr.addr = ptrtoint ptr %ptr to i128
+ %size = sub i128 %end.addr, %ptr.addr
+ %end = getelementptr i8, ptr %ptr, i128 %size
+ %end.addr2 = ptrtoint ptr %end to i128
+ ret i128 %end.addr2
+}
More information about the llvm-commits
mailing list