1 ; NOTE: Assertions have been autogenerated by update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3 target 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"
4 target triple = "x86_64-unknown-linux-gnu"
6 define i64 @ptrdiff1(ptr %ptr) {
7 ; CHECK-LABEL: @ptrdiff1(
10 %last = getelementptr inbounds i8, ptr %ptr, i32 42
11 %first.int = ptrtoint ptr %ptr to i64
12 %last.int = ptrtoint ptr %last to i64
13 %diff = sub i64 %last.int, %first.int
17 define i64 @ptrdiff2(ptr %ptr) {
18 ; CHECK-LABEL: @ptrdiff2(
21 %first2 = getelementptr inbounds i8, ptr %ptr, i32 1
22 %first3 = getelementptr inbounds i8, ptr %first2, i32 2
23 %first4 = getelementptr inbounds i8, ptr %first3, i32 4
24 %last1 = getelementptr inbounds i8, ptr %first2, i32 48
25 %last2 = getelementptr inbounds i8, ptr %last1, i32 8
26 %last3 = getelementptr inbounds i8, ptr %last2, i32 -4
27 %last4 = getelementptr inbounds i8, ptr %last3, i32 -4
28 %first.int = ptrtoint ptr %first4 to i64
29 %last.int = ptrtoint ptr %last4 to i64
30 %diff = sub i64 %last.int, %first.int
34 define i64 @ptrdiff3(ptr %ptr) {
35 ; Don't bother with non-inbounds GEPs.
36 ; CHECK-LABEL: @ptrdiff3(
37 ; CHECK: [[LAST:%.*]] = getelementptr i8, ptr %ptr, i32 42
38 ; CHECK-NEXT: [[FIRST_INT:%.*]] = ptrtoint ptr %ptr to i64
39 ; CHECK-NEXT: [[LAST_INT:%.*]] = ptrtoint ptr [[LAST]] to i64
40 ; CHECK-NEXT: [[DIFF:%.*]] = sub i64 [[LAST_INT]], [[FIRST_INT]]
41 ; CHECK-NEXT: ret i64 [[DIFF]]
43 %last = getelementptr i8, ptr %ptr, i32 42
44 %first.int = ptrtoint ptr %ptr to i64
45 %last.int = ptrtoint ptr %last to i64
46 %diff = sub i64 %last.int, %first.int
50 define <4 x i32> @ptrdiff4(<4 x ptr> %arg) nounwind {
51 ; Handle simple cases of vectors of pointers.
52 ; CHECK-LABEL: @ptrdiff4(
53 ; CHECK: ret <4 x i32> zeroinitializer
55 %p1 = ptrtoint <4 x ptr> %arg to <4 x i32>
56 %bc = bitcast <4 x ptr> %arg to <4 x ptr>
57 %p2 = ptrtoint <4 x ptr> %bc to <4 x i32>
58 %sub = sub <4 x i32> %p1, %p2
62 %struct.ham = type { i32, [2 x [2 x i32]] }
64 @global = internal global %struct.ham zeroinitializer, align 4
66 define i32 @ptrdiff5() nounwind {
67 ; CHECK-LABEL: @ptrdiff5(
69 ; CHECK-NEXT: ret i32 0
72 %tmp = getelementptr inbounds %struct.ham, ptr @global, i32 0, i32 1
73 %tmp3 = ptrtoint ptr %tmp to i32
74 %tmp4 = getelementptr inbounds %struct.ham, ptr @global, i32 0, i32 1
75 %tmp6 = ptrtoint ptr %tmp4 to i32
76 %tmp7 = sub i32 %tmp3, %tmp6