@@ -289,3 +289,49 @@ entry:
289289 store i32 %i , ptr %gep8
290290 ret i32 undef
291291}
292+
293+ ; Case where inbounds can't be preserved despite the fact that %shr is positive -
294+ ; byte offset (%shr * 8) is negative, so the GEP with %shr index can be outside of
295+ ; bounds of the allocated object.
296+ define i64 @test_inbounds1 (ptr %arr , i64 %x ) {
297+ ; CHECK-LABEL: @test_inbounds1(
298+ ; CHECK-NEXT: entry:
299+ ; CHECK-NEXT: [[MIN:%.*]] = tail call i64 @llvm.umin.i64(i64 [[X:%.*]], i64 4)
300+ ; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[MIN]], -1
301+ ; CHECK-NEXT: [[SHR:%.*]] = lshr i64 [[XOR]], 1
302+ ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds [6 x i64], ptr [[ARR:%.*]], i64 0, i64 [[SHR]]
303+ ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 40
304+ ; CHECK-NEXT: [[RES:%.*]] = load i64, ptr [[GEP2]], align 8
305+ ; CHECK-NEXT: ret i64 [[RES]]
306+ ;
307+ entry:
308+ %min = tail call i64 @llvm.umin.i64 (i64 %x , i64 4 )
309+ %xor = xor i64 %min , -1
310+ %shr = lshr i64 %xor , 1
311+ %sub = add nsw i64 %shr , -9223372036854775803
312+ %gep = getelementptr inbounds nuw [6 x i64 ], ptr %arr , i64 0 , i64 %sub
313+ %res = load i64 , ptr %gep , align 8
314+ ret i64 %res
315+ }
316+
317+ ; The same case as above - inbounds attribute can't be preserved due to negative
318+ ; GEP index (%sub).
319+ define i64 @test_inbounds2 (ptr %arr , i64 %x ) {
320+ ; CHECK-LABEL: @test_inbounds2(
321+ ; CHECK-NEXT: entry:
322+ ; CHECK-NEXT: [[MIN:%.*]] = tail call i64 @llvm.umin.i64(i64 [[X:%.*]], i64 4)
323+ ; CHECK-NEXT: [[SHR:%.*]] = lshr i64 [[MIN]], 1
324+ ; CHECK-NEXT: [[SUB1:%.*]] = sub i64 0, [[SHR]]
325+ ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [6 x i64], ptr [[ARR:%.*]], i64 0, i64 [[SUB1]]
326+ ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, ptr [[TMP0]], i64 32
327+ ; CHECK-NEXT: [[RES:%.*]] = load i64, ptr [[GEP2]], align 8
328+ ; CHECK-NEXT: ret i64 [[RES]]
329+ ;
330+ entry:
331+ %min = tail call i64 @llvm.umin.i64 (i64 %x , i64 4 )
332+ %shr = lshr i64 %min , 1
333+ %sub = sub nsw nuw i64 4 , %shr
334+ %gep = getelementptr inbounds nuw [6 x i64 ], ptr %arr , i64 0 , i64 %sub
335+ %res = load i64 , ptr %gep , align 8
336+ ret i64 %res
337+ }
0 commit comments