gep-inbounds-null.ll
7.04 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S < %s -instcombine | FileCheck %s
;; Start by showing the results of constant folding (which doesn't use
;; the poison implied by gep for the nonnull cases).
define i1 @test_ne_constants_null() {
; CHECK-LABEL: @test_ne_constants_null(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 false
;
entry:
%gep = getelementptr inbounds i8, i8* null, i64 0
%cnd = icmp ne i8* %gep, null
ret i1 %cnd
}
define i1 @test_ne_constants_nonnull() {
; CHECK-LABEL: @test_ne_constants_nonnull(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 true
;
entry:
%gep = getelementptr inbounds i8, i8* null, i64 1
%cnd = icmp ne i8* %gep, null
ret i1 %cnd
}
define i1 @test_eq_constants_null() {
; CHECK-LABEL: @test_eq_constants_null(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 true
;
entry:
%gep = getelementptr inbounds i8, i8* null, i64 0
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
define i1 @test_eq_constants_nonnull() {
; CHECK-LABEL: @test_eq_constants_nonnull(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 false
;
entry:
%gep = getelementptr inbounds i8, i8* null, i64 1
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
;; Then show the results for non-constants. These use the inbounds provided
;; UB fact to ignore the possible overflow cases.
define i1 @test_ne(i8* %base, i64 %idx) {
; CHECK-LABEL: @test_ne(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp ne i8* [[BASE:%.*]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds i8, i8* %base, i64 %idx
%cnd = icmp ne i8* %gep, null
ret i1 %cnd
}
define i1 @test_eq(i8* %base, i64 %idx) {
; CHECK-LABEL: @test_eq(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp eq i8* [[BASE:%.*]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds i8, i8* %base, i64 %idx
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
define <2 x i1> @test_vector_base(<2 x i8*> %base, i64 %idx) {
; CHECK-LABEL: @test_vector_base(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp eq <2 x i8*> [[BASE:%.*]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CND]]
;
entry:
%gep = getelementptr inbounds i8, <2 x i8*> %base, i64 %idx
%cnd = icmp eq <2 x i8*> %gep, zeroinitializer
ret <2 x i1> %cnd
}
define <2 x i1> @test_vector_index(i8* %base, <2 x i64> %idx) {
; CHECK-LABEL: @test_vector_index(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i8*> undef, i8* [[BASE:%.*]], i32 0
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <2 x i8*> [[DOTSPLATINSERT]], <2 x i8*> undef, <2 x i32> zeroinitializer
; CHECK-NEXT: [[CND:%.*]] = icmp eq <2 x i8*> [[DOTSPLAT]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CND]]
;
entry:
%gep = getelementptr inbounds i8, i8* %base, <2 x i64> %idx
%cnd = icmp eq <2 x i8*> %gep, zeroinitializer
ret <2 x i1> %cnd
}
define <2 x i1> @test_vector_both(<2 x i8*> %base, <2 x i64> %idx) {
; CHECK-LABEL: @test_vector_both(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp eq <2 x i8*> [[BASE:%.*]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CND]]
;
entry:
%gep = getelementptr inbounds i8, <2 x i8*> %base, <2 x i64> %idx
%cnd = icmp eq <2 x i8*> %gep, zeroinitializer
ret <2 x i1> %cnd
}
;; These two show instsimplify's reasoning getting to the non-zero offsets
;; before instcombine does.
define i1 @test_eq_pos_idx(i8* %base) {
; CHECK-LABEL: @test_eq_pos_idx(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 false
;
entry:
%gep = getelementptr inbounds i8, i8* %base, i64 1
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
define i1 @test_eq_neg_idx(i8* %base) {
; CHECK-LABEL: @test_eq_neg_idx(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 false
;
entry:
%gep = getelementptr inbounds i8, i8* %base, i64 -1
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
;; Show an example with a zero sized type since that's
;; a cornercase which keeps getting mentioned. The GEP
;; produces %base regardless of the value of the index
;; expression.
define i1 @test_size0({}* %base, i64 %idx) {
; CHECK-LABEL: @test_size0(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp ne {}* [[BASE:%.*]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds {}, {}* %base, i64 %idx
%cnd = icmp ne {}* %gep, null
ret i1 %cnd
}
define i1 @test_size0_nonzero_offset({}* %base) {
; CHECK-LABEL: @test_size0_nonzero_offset(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp ne {}* [[BASE:%.*]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds {}, {}* %base, i64 15
%cnd = icmp ne {}* %gep, null
ret i1 %cnd
}
define i1 @test_index_type([10 x i8]* %base, i64 %idx) {
; CHECK-LABEL: @test_index_type(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CND:%.*]] = icmp eq [10 x i8]* [[BASE:%.*]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds [10 x i8], [10 x i8]* %base, i64 %idx, i64 %idx
%cnd = icmp eq i8* %gep, null
ret i1 %cnd
}
;; Finally, some negative tests for sanity checking.
define i1 @neq_noinbounds(i8* %base, i64 %idx) {
; CHECK-LABEL: @neq_noinbounds(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, i8* [[BASE:%.*]], i64 [[IDX:%.*]]
; CHECK-NEXT: [[CND:%.*]] = icmp ne i8* [[GEP]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr i8, i8* %base, i64 %idx
%cnd = icmp ne i8* %gep, null
ret i1 %cnd
}
define i1 @neg_objectatnull(i8 addrspace(2)* %base, i64 %idx) {
; CHECK-LABEL: @neg_objectatnull(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8 addrspace(2)* [[BASE:%.*]], i64 [[IDX:%.*]]
; CHECK-NEXT: [[CND:%.*]] = icmp eq i8 addrspace(2)* [[GEP]], null
; CHECK-NEXT: ret i1 [[CND]]
;
entry:
%gep = getelementptr inbounds i8, i8 addrspace(2)* %base, i64 %idx
%cnd = icmp eq i8 addrspace(2)* %gep, null
ret i1 %cnd
}
; Test for an assert from trying to create an invalid constantexpr
; bitcast between different address spaces. The addrspacecast is
; stripped off and the addrspace(0) null can be treated as invalid.
; FIXME: This should be able to fold to ret i1 false
define i1 @invalid_bitcast_icmp_addrspacecast_as0_null(i32 addrspace(5)* %ptr) {
; CHECK-LABEL: @invalid_bitcast_icmp_addrspacecast_as0_null(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 addrspace(5)* [[PTR:%.*]], addrspacecast (i32* null to i32 addrspace(5)*)
; CHECK-NEXT: ret i1 [[TMP2]]
;
bb:
%tmp1 = getelementptr inbounds i32, i32 addrspace(5)* %ptr, i32 1
%tmp2 = icmp eq i32 addrspace(5)* %tmp1, addrspacecast (i32* null to i32 addrspace(5)*)
ret i1 %tmp2
}
define i1 @invalid_bitcast_icmp_addrspacecast_as0_null_var(i32 addrspace(5)* %ptr, i32 %idx) {
; CHECK-LABEL: @invalid_bitcast_icmp_addrspacecast_as0_null_var(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 addrspace(5)* [[PTR:%.*]], addrspacecast (i32* null to i32 addrspace(5)*)
; CHECK-NEXT: ret i1 [[TMP2]]
;
bb:
%tmp1 = getelementptr inbounds i32, i32 addrspace(5)* %ptr, i32 %idx
%tmp2 = icmp eq i32 addrspace(5)* %tmp1, addrspacecast (i32* null to i32 addrspace(5)*)
ret i1 %tmp2
}