blob: c57fab99786685d02d6801d887a4f9d3707486a9 [file] [log] [blame]
Sanjay Patel69632442016-03-25 20:12:25 +00001; NOTE: Assertions have been autogenerated by update_test_checks.py
Chandler Carrutha0796552012-03-12 11:19:31 +00002; RUN: opt < %s -instsimplify -S | FileCheck %s
3target 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"
4target triple = "x86_64-unknown-linux-gnu"
5
6define i64 @ptrdiff1(i8* %ptr) {
Stephen Linc1c7a132013-07-14 01:42:54 +00007; CHECK-LABEL: @ptrdiff1(
Sanjay Patel69632442016-03-25 20:12:25 +00008; CHECK: ret i64 42
9;
David Blaikie79e6c742015-02-27 19:29:02 +000010 %first = getelementptr inbounds i8, i8* %ptr, i32 0
11 %last = getelementptr inbounds i8, i8* %ptr, i32 42
Chandler Carrutha0796552012-03-12 11:19:31 +000012 %first.int = ptrtoint i8* %first to i64
13 %last.int = ptrtoint i8* %last to i64
14 %diff = sub i64 %last.int, %first.int
15 ret i64 %diff
16}
17
18define i64 @ptrdiff2(i8* %ptr) {
Stephen Linc1c7a132013-07-14 01:42:54 +000019; CHECK-LABEL: @ptrdiff2(
Sanjay Patel69632442016-03-25 20:12:25 +000020; CHECK: ret i64 42
21;
David Blaikie79e6c742015-02-27 19:29:02 +000022 %first1 = getelementptr inbounds i8, i8* %ptr, i32 0
23 %first2 = getelementptr inbounds i8, i8* %first1, i32 1
24 %first3 = getelementptr inbounds i8, i8* %first2, i32 2
25 %first4 = getelementptr inbounds i8, i8* %first3, i32 4
26 %last1 = getelementptr inbounds i8, i8* %first2, i32 48
27 %last2 = getelementptr inbounds i8, i8* %last1, i32 8
28 %last3 = getelementptr inbounds i8, i8* %last2, i32 -4
29 %last4 = getelementptr inbounds i8, i8* %last3, i32 -4
Chandler Carrutha0796552012-03-12 11:19:31 +000030 %first.int = ptrtoint i8* %first4 to i64
31 %last.int = ptrtoint i8* %last4 to i64
32 %diff = sub i64 %last.int, %first.int
33 ret i64 %diff
34}
Chandler Carruth2741aae2012-03-25 20:43:07 +000035
36define i64 @ptrdiff3(i8* %ptr) {
37; Don't bother with non-inbounds GEPs.
Stephen Linc1c7a132013-07-14 01:42:54 +000038; CHECK-LABEL: @ptrdiff3(
Sanjay Patel69632442016-03-25 20:12:25 +000039; CHECK: [[LAST:%.*]] = getelementptr i8, i8* %ptr, i32 42
40; CHECK-NEXT: [[FIRST_INT:%.*]] = ptrtoint i8* %ptr to i64
41; CHECK-NEXT: [[LAST_INT:%.*]] = ptrtoint i8* [[LAST]] to i64
42; CHECK-NEXT: [[DIFF:%.*]] = sub i64 [[LAST_INT]], [[FIRST_INT]]
43; CHECK-NEXT: ret i64 [[DIFF]]
44;
David Blaikie79e6c742015-02-27 19:29:02 +000045 %first = getelementptr i8, i8* %ptr, i32 0
46 %last = getelementptr i8, i8* %ptr, i32 42
Chandler Carruth2741aae2012-03-25 20:43:07 +000047 %first.int = ptrtoint i8* %first to i64
48 %last.int = ptrtoint i8* %last to i64
49 %diff = sub i64 %last.int, %first.int
50 ret i64 %diff
51}
Benjamin Kramerc05aa952013-02-01 15:21:10 +000052
53define <4 x i32> @ptrdiff4(<4 x i8*> %arg) nounwind {
54; Handle simple cases of vectors of pointers.
Stephen Linc1c7a132013-07-14 01:42:54 +000055; CHECK-LABEL: @ptrdiff4(
Sanjay Patel69632442016-03-25 20:12:25 +000056; CHECK: ret <4 x i32> zeroinitializer
57;
Benjamin Kramerc05aa952013-02-01 15:21:10 +000058 %p1 = ptrtoint <4 x i8*> %arg to <4 x i32>
59 %bc = bitcast <4 x i8*> %arg to <4 x i32*>
60 %p2 = ptrtoint <4 x i32*> %bc to <4 x i32>
61 %sub = sub <4 x i32> %p1, %p2
62 ret <4 x i32> %sub
63}
Benjamin Kramera5a9ec52013-02-05 19:04:36 +000064
65%struct.ham = type { i32, [2 x [2 x i32]] }
66
67@global = internal global %struct.ham zeroinitializer, align 4
68
69define i32 @ptrdiff5() nounwind {
Sanjay Patel69632442016-03-25 20:12:25 +000070; CHECK-LABEL: @ptrdiff5(
71; CHECK: bb:
72; CHECK-NEXT: ret i32 0
73;
Benjamin Kramera5a9ec52013-02-05 19:04:36 +000074bb:
David Blaikie79e6c742015-02-27 19:29:02 +000075 %tmp = getelementptr inbounds %struct.ham, %struct.ham* @global, i32 0, i32 1
76 %tmp1 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %tmp, i32 0, i32 0
Benjamin Kramera5a9ec52013-02-05 19:04:36 +000077 %tmp2 = bitcast [2 x i32]* %tmp1 to i32*
78 %tmp3 = ptrtoint i32* %tmp2 to i32
David Blaikie79e6c742015-02-27 19:29:02 +000079 %tmp4 = getelementptr inbounds %struct.ham, %struct.ham* @global, i32 0, i32 1
80 %tmp5 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %tmp4, i32 0, i32 0
Benjamin Kramera5a9ec52013-02-05 19:04:36 +000081 %tmp6 = ptrtoint [2 x i32]* %tmp5 to i32
82 %tmp7 = sub i32 %tmp3, %tmp6
83 ret i32 %tmp7
Benjamin Kramera5a9ec52013-02-05 19:04:36 +000084}