@@ -207,6 +207,61 @@ define i1 @gep13_no_null_opt(ptr %ptr) #0 {
207
207
ret i1 %cmp
208
208
}
209
209
210
+ ; We can prove this GEP is non-null because it is nuw.
211
+ define i1 @gep_nuw_not_null (ptr %ptr ) {
212
+ ; CHECK-LABEL: @gep_nuw_not_null(
213
+ ; CHECK-NEXT: ret i1 false
214
+ ;
215
+ %x = getelementptr nuw i8 , ptr %ptr , i32 1
216
+ %cmp = icmp eq ptr %x , null
217
+ ret i1 %cmp
218
+ }
219
+
220
+ ; Unlike the inbounds case, this holds even if the null pointer is valid.
221
+ define i1 @gep_nuw_null_pointer_valid (ptr %ptr ) null_pointer_is_valid {
222
+ ; CHECK-LABEL: @gep_nuw_null_pointer_valid(
223
+ ; CHECK-NEXT: ret i1 false
224
+ ;
225
+ %x = getelementptr nuw i8 , ptr %ptr , i32 1
226
+ %cmp = icmp eq ptr %x , null
227
+ ret i1 %cmp
228
+ }
229
+
230
+ ; If the base pointer is non-null, the offset doesn't matter.
231
+ define i1 @gep_nuw_maybe_zero_offset (ptr nonnull %ptr , i32 %offset ) {
232
+ ; CHECK-LABEL: @gep_nuw_maybe_zero_offset(
233
+ ; CHECK-NEXT: ret i1 false
234
+ ;
235
+ %x = getelementptr nuw i8 , ptr %ptr , i32 %offset
236
+ %cmp = icmp eq ptr %x , null
237
+ ret i1 %cmp
238
+ }
239
+
240
+ ; We can not prove non-null if both the base pointer may be null and the
241
+ ; offset zero.
242
+ define i1 @gep13_nuw_maybe_zero_offset (ptr %ptr , i32 %offset ) {
243
+ ; CHECK-LABEL: @gep13_nuw_maybe_zero_offset(
244
+ ; CHECK-NEXT: [[X:%.*]] = getelementptr nuw i8, ptr [[PTR:%.*]], i32 [[OFFSET:%.*]]
245
+ ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[X]], null
246
+ ; CHECK-NEXT: ret i1 [[CMP]]
247
+ ;
248
+ %x = getelementptr nuw i8 , ptr %ptr , i32 %offset
249
+ %cmp = icmp eq ptr %x , null
250
+ ret i1 %cmp
251
+ }
252
+
253
+ ; For gep nusw we don't have any non-null information.
254
+ define i1 @gep_nusw_may_be_null (ptr %ptr ) {
255
+ ; CHECK-LABEL: @gep_nusw_may_be_null(
256
+ ; CHECK-NEXT: [[X:%.*]] = getelementptr nusw i8, ptr [[PTR:%.*]], i32 1
257
+ ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[X]], null
258
+ ; CHECK-NEXT: ret i1 [[CMP]]
259
+ ;
260
+ %x = getelementptr nusw i8 , ptr %ptr , i32 1
261
+ %cmp = icmp eq ptr %x , null
262
+ ret i1 %cmp
263
+ }
264
+
210
265
define i1 @gep14 (ptr %ptr ) {
211
266
; CHECK-LABEL: @gep14(
212
267
; CHECK-NEXT: [[X:%.*]] = getelementptr inbounds { {}, i8 }, ptr [[PTR:%.*]], i32 0, i32 1
0 commit comments