/
sub-gep.ll
153 lines (142 loc) · 5.55 KB
/
sub-gep.ll
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -instcombine < %s | FileCheck %s
define i64 @test_inbounds([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub i64 %i2, %i1
ret i64 %d
}
define i64 @test_partial_inbounds1([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_partial_inbounds1(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub i64 %i2, %i1
ret i64 %d
}
define i64 @test_partial_inbounds2([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_partial_inbounds2(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub i64 %i2, %i1
ret i64 %d
}
define i64 @test_inbounds_nuw([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nuw nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
define i64 @test_nuw([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_nuw(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
define i32 @test_inbounds_nuw_trunc([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw_trunc(
; CHECK-NEXT: [[IDX_TR:%.*]] = trunc i64 [[IDX:%.*]] to i32
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[IDX_TR]], 2
; CHECK-NEXT: ret i32 [[TMP1]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%t1 = trunc i64 %i1 to i32
%t2 = trunc i64 %i2 to i32
%d = sub nuw i32 %t2, %t1
ret i32 %d
}
define i64 @test_inbounds_nuw_swapped([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw_swapped(
; CHECK-NEXT: [[P2_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
; CHECK-NEXT: ret i64 [[P2_IDX_NEG]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p2 to i64
%i2 = ptrtoint i32* %p1 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
define i64 @test_inbounds1_nuw_swapped([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds1_nuw_swapped(
; CHECK-NEXT: [[P2_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
; CHECK-NEXT: ret i64 [[P2_IDX_NEG]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p2 to i64
%i2 = ptrtoint i32* %p1 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
define i64 @test_inbounds2_nuw_swapped([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds2_nuw_swapped(
; CHECK-NEXT: [[P2_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
; CHECK-NEXT: ret i64 [[P2_IDX_NEG]]
;
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p2 to i64
%i2 = ptrtoint i32* %p1 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
; The sub and shl here could be nuw, but this is harder to handle.
define i64 @test_inbounds_nuw_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @test_inbounds_nuw_two_gep(
; CHECK-NEXT: [[P1_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
; CHECK-NEXT: [[P2_IDX_NEG_NEG:%.*]] = shl i64 [[IDX2:%.*]], 2
; CHECK-NEXT: [[GEPDIFF_NEG:%.*]] = add i64 [[P2_IDX_NEG_NEG]], [[P1_IDX_NEG]]
; CHECK-NEXT: ret i64 [[GEPDIFF_NEG]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx2
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}
define i64 @test_inbounds_nuw_multi_index([0 x [2 x i32]]* %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @test_inbounds_nuw_multi_index(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 3
; CHECK-NEXT: [[P2_IDX1:%.*]] = shl nsw i64 [[IDX2:%.*]], 2
; CHECK-NEXT: [[P2_OFFS2:%.*]] = add i64 [[P2_IDX1]], [[P2_IDX]]
; CHECK-NEXT: ret i64 [[P2_OFFS2]]
;
%p1 = getelementptr inbounds [0 x [2 x i32]], [0 x [2 x i32]]* %base, i64 0, i64 0, i64 0
%p2 = getelementptr inbounds [0 x [2 x i32]], [0 x [2 x i32]]* %base, i64 0, i64 %idx, i64 %idx2
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}