xref: /llvm-project/llvm/test/Transforms/InstSimplify/ptr_diff.ll (revision 04b944e23050e4e0c6ee983cc9bc17740315ea4f)
1cee313d2SEric Christopher; NOTE: Assertions have been autogenerated by update_test_checks.py
2b280ee1dSBjorn Pettersson; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3cee313d2SEric Christophertarget datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
4cee313d2SEric Christophertarget triple = "x86_64-unknown-linux-gnu"
5cee313d2SEric Christopher
6*04b944e2SNikita Popovdefine i64 @ptrdiff1(ptr %ptr) {
7cee313d2SEric Christopher; CHECK-LABEL: @ptrdiff1(
8cee313d2SEric Christopher; CHECK:         ret i64 42
9cee313d2SEric Christopher;
10*04b944e2SNikita Popov  %last = getelementptr inbounds i8, ptr %ptr, i32 42
11*04b944e2SNikita Popov  %first.int = ptrtoint ptr %ptr to i64
12*04b944e2SNikita Popov  %last.int = ptrtoint ptr %last to i64
13cee313d2SEric Christopher  %diff = sub i64 %last.int, %first.int
14cee313d2SEric Christopher  ret i64 %diff
15cee313d2SEric Christopher}
16cee313d2SEric Christopher
17*04b944e2SNikita Popovdefine i64 @ptrdiff2(ptr %ptr) {
18cee313d2SEric Christopher; CHECK-LABEL: @ptrdiff2(
19cee313d2SEric Christopher; CHECK:         ret i64 42
20cee313d2SEric Christopher;
21*04b944e2SNikita Popov  %first2 = getelementptr inbounds i8, ptr %ptr, i32 1
22*04b944e2SNikita Popov  %first3 = getelementptr inbounds i8, ptr %first2, i32 2
23*04b944e2SNikita Popov  %first4 = getelementptr inbounds i8, ptr %first3, i32 4
24*04b944e2SNikita Popov  %last1 = getelementptr inbounds i8, ptr %first2, i32 48
25*04b944e2SNikita Popov  %last2 = getelementptr inbounds i8, ptr %last1, i32 8
26*04b944e2SNikita Popov  %last3 = getelementptr inbounds i8, ptr %last2, i32 -4
27*04b944e2SNikita Popov  %last4 = getelementptr inbounds i8, ptr %last3, i32 -4
28*04b944e2SNikita Popov  %first.int = ptrtoint ptr %first4 to i64
29*04b944e2SNikita Popov  %last.int = ptrtoint ptr %last4 to i64
30cee313d2SEric Christopher  %diff = sub i64 %last.int, %first.int
31cee313d2SEric Christopher  ret i64 %diff
32cee313d2SEric Christopher}
33cee313d2SEric Christopher
34*04b944e2SNikita Popovdefine i64 @ptrdiff3(ptr %ptr) {
35cee313d2SEric Christopher; Don't bother with non-inbounds GEPs.
36cee313d2SEric Christopher; CHECK-LABEL: @ptrdiff3(
37*04b944e2SNikita Popov; CHECK:         [[LAST:%.*]] = getelementptr i8, ptr %ptr, i32 42
38*04b944e2SNikita Popov; CHECK-NEXT:    [[FIRST_INT:%.*]] = ptrtoint ptr %ptr to i64
39*04b944e2SNikita Popov; CHECK-NEXT:    [[LAST_INT:%.*]] = ptrtoint ptr [[LAST]] to i64
40cee313d2SEric Christopher; CHECK-NEXT:    [[DIFF:%.*]] = sub i64 [[LAST_INT]], [[FIRST_INT]]
41cee313d2SEric Christopher; CHECK-NEXT:    ret i64 [[DIFF]]
42cee313d2SEric Christopher;
43*04b944e2SNikita Popov  %last = getelementptr i8, ptr %ptr, i32 42
44*04b944e2SNikita Popov  %first.int = ptrtoint ptr %ptr to i64
45*04b944e2SNikita Popov  %last.int = ptrtoint ptr %last to i64
46cee313d2SEric Christopher  %diff = sub i64 %last.int, %first.int
47cee313d2SEric Christopher  ret i64 %diff
48cee313d2SEric Christopher}
49cee313d2SEric Christopher
50*04b944e2SNikita Popovdefine <4 x i32> @ptrdiff4(<4 x ptr> %arg) nounwind {
51cee313d2SEric Christopher; Handle simple cases of vectors of pointers.
52cee313d2SEric Christopher; CHECK-LABEL: @ptrdiff4(
53cee313d2SEric Christopher; CHECK:         ret <4 x i32> zeroinitializer
54cee313d2SEric Christopher;
55*04b944e2SNikita Popov  %p1 = ptrtoint <4 x ptr> %arg to <4 x i32>
56*04b944e2SNikita Popov  %bc = bitcast <4 x ptr> %arg to <4 x ptr>
57*04b944e2SNikita Popov  %p2 = ptrtoint <4 x ptr> %bc to <4 x i32>
58cee313d2SEric Christopher  %sub = sub <4 x i32> %p1, %p2
59cee313d2SEric Christopher  ret <4 x i32> %sub
60cee313d2SEric Christopher}
61cee313d2SEric Christopher
62cee313d2SEric Christopher%struct.ham = type { i32, [2 x [2 x i32]] }
63cee313d2SEric Christopher
64cee313d2SEric Christopher@global = internal global %struct.ham zeroinitializer, align 4
65cee313d2SEric Christopher
66cee313d2SEric Christopherdefine i32 @ptrdiff5() nounwind {
67cee313d2SEric Christopher; CHECK-LABEL: @ptrdiff5(
68cee313d2SEric Christopher; CHECK:       bb:
69cee313d2SEric Christopher; CHECK-NEXT:    ret i32 0
70cee313d2SEric Christopher;
71cee313d2SEric Christopherbb:
72*04b944e2SNikita Popov  %tmp = getelementptr inbounds %struct.ham, ptr @global, i32 0, i32 1
73*04b944e2SNikita Popov  %tmp3 = ptrtoint ptr %tmp to i32
74*04b944e2SNikita Popov  %tmp4 = getelementptr inbounds %struct.ham, ptr @global, i32 0, i32 1
75*04b944e2SNikita Popov  %tmp6 = ptrtoint ptr %tmp4 to i32
76cee313d2SEric Christopher  %tmp7 = sub i32 %tmp3, %tmp6
77cee313d2SEric Christopher  ret i32 %tmp7
78cee313d2SEric Christopher}
79