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