[llvm] [InstCombine] fold `sub(zext(ptrtoint), zext(ptrtoint))` (PR #115369)
Nikolay Panchenko via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 14 13:59:05 PST 2024
================
@@ -285,6 +315,98 @@ define i16 @test25_as1(ptr addrspace(1) %P, i64 %A) {
ret i16 %G
}
+ at Arr_as2 = external addrspace(2) global [42 x i16]
+define i64 @zext_ptrtoint_sub_ptrtoint_as2(i32 %offset) {
+; CHECK-LABEL: @zext_ptrtoint_sub_ptrtoint_as2(
+; CHECK-NEXT: %A.idx = shl nsw i32 %offset, 1
+; CHECK-NEXT: %D = sext i32 %A.idx to i64
+; CHECK-NEXT: ret i64 %D
+ %A = getelementptr nusw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
+ ret i64 %D
+}
+
+define i64 @zext_ptrtoint_sub_ptrtoint_as2_nuw(i32 %offset) {
+; CHECK-LABEL: @zext_ptrtoint_sub_ptrtoint_as2_nuw(
+; CHECK-NEXT: %A.idx = shl nuw i32 %offset, 1
+; CHECK-NEXT: %D = zext i32 %A.idx to i64
+; CHECK-NEXT: ret i64 %D
+ %A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
+ ret i64 %D
+}
+
+define i64 @zext_ptrtoint_sub_ptrtoint_as2_nusw_nuw(i32 %offset) {
+; CHECK-LABEL: @zext_ptrtoint_sub_ptrtoint_as2_nusw_nuw(
+; CHECK-NEXT: %A.idx = shl nuw nsw i32 %offset, 1
+; CHECK-NEXT: %D = zext nneg i32 %A.idx to i64
+; CHECK-NEXT: ret i64 %D
+ %A = getelementptr nusw nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
+ ret i64 %D
+}
+
+define i64 @zext_ptrtoint_sub_zext_ptrtoint_as2(i32 %offset) {
+; CHECK-LABEL: @zext_ptrtoint_sub_zext_ptrtoint_as2(
+; CHECK-NEXT: %A.idx = shl nsw i32 %offset, 1
+; CHECK-NEXT: %E = sext i32 %A.idx to i64
+; CHECK-NEXT: ret i64 %E
+ %A = getelementptr nusw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = zext i32 ptrtoint (ptr addrspace(2) @Arr_as2 to i32) to i64
+ %E = sub i64 %C, %D
+ ret i64 %E
+}
+
+define i64 @zext_ptrtoint_sub_zext_ptrtoint_as2_nuw(i32 %offset) {
+; CHECK-LABEL: @zext_ptrtoint_sub_zext_ptrtoint_as2_nuw(
+; CHECK-NEXT: %A.idx = shl nuw i32 %offset, 1
+; CHECK-NEXT: %E = zext i32 %A.idx to i64
+; CHECK-NEXT: ret i64 %E
+ %A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = zext i32 ptrtoint (ptr addrspace(2) @Arr_as2 to i32) to i64
+ %E = sub i64 %C, %D
+ ret i64 %E
+}
+
+define i64 @negative_zext_ptrtoint_sub_ptrtoint_as2_nuw(i32 %offset) {
+ ; CHECK-LABEL: @negative_zext_ptrtoint_sub_ptrtoint_as2_nuw(
+ ; CHECK-NEXT: %A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ ; CHECK-NEXT: %1 = ptrtoint ptr addrspace(2) %A to i32
+ ; CHECK-NEXT: %B.mask = and i32 %1, 65534
+ ; CHECK-NEXT: %C = zext nneg i32 %B.mask to i64
+ ; CHECK-NEXT: %D = sub nsw i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
+ ; CHECK-NEXT: ret i64 %D
+ %A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i16
+ %C = zext i16 %B to i64
+ %D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
+ ret i64 %D
+}
+
+define i64 @ptrtoint_sub_zext_ptrtoint_as2(i32 %offset) {
+; CHECK-LABEL: @ptrtoint_sub_zext_ptrtoint_as2(
+; CHECK-NEXT: %A = getelementptr inbounds bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+; CHECK-NEXT: %B = ptrtoint ptr addrspace(2) %A to i32
+; CHECK-NEXT: %C = zext i32 %B to i64
+; CHECK-NEXT: %D = sub nsw i64 ptrtoint (ptr addrspace(2) @Arr_as2 to i64), %C
+; CHECK-NEXT: ret i64 %D
+ %A = getelementptr inbounds bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
+ %B = ptrtoint ptr addrspace(2) %A to i32
+ %C = zext i32 %B to i64
+ %D = sub i64 ptrtoint (ptr addrspace(2) @Arr_as2 to i64), %C
+ ret i64 %D
+}
----------------
npanchen wrote:
Personally I'd prefer to keep it as is, as constant expr can only be done with global pointer.
https://github.com/llvm/llvm-project/pull/115369
More information about the llvm-commits
mailing list