1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4target datalayout = "p:32:32"
5
6%S = type { [2 x i32] }
7
8define i1 @test([0 x %S]* %p, i32 %n) {
9; CHECK-LABEL: @test(
10; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[N:%.*]], 1
11; CHECK-NEXT:    ret i1 [[CMP]]
12;
13  %start.cast = bitcast [0 x %S]* %p to %S*
14  %end = getelementptr inbounds [0 x %S], [0 x %S]* %p, i32 0, i32 %n, i32 0, i32 0
15  %end.cast = bitcast i32* %end to %S*
16  %last = getelementptr inbounds %S, %S* %end.cast, i32 -1
17  %cmp = icmp eq %S* %last, %start.cast
18  ret i1 %cmp
19}
20
21; Same test using 64-bit indices.
22define i1 @test64([0 x %S]* %p, i64 %n) {
23; CHECK-LABEL: @test64(
24; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
25; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
26; CHECK-NEXT:    ret i1 [[CMP]]
27;
28  %start.cast = bitcast [0 x %S]* %p to %S*
29  %end = getelementptr inbounds [0 x %S], [0 x %S]* %p, i64 0, i64 %n, i32 0, i64 0
30  %end.cast = bitcast i32* %end to %S*
31  %last = getelementptr inbounds %S, %S* %end.cast, i64 -1
32  %cmp = icmp eq %S* %last, %start.cast
33  ret i1 %cmp
34}
35
36; Here the offset overflows and is treated modulo 2^32. This is UB.
37define i1 @test64_overflow([0 x %S]* %p, i64 %n) {
38; CHECK-LABEL: @test64_overflow(
39; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
40; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
41; CHECK-NEXT:    ret i1 [[CMP]]
42;
43  %start.cast = bitcast [0 x %S]* %p to %S*
44  %end = getelementptr inbounds [0 x %S], [0 x %S]* %p, i64 0, i64 %n, i32 0, i64 8589934592
45  %end.cast = bitcast i32* %end to %S*
46  %last = getelementptr inbounds %S, %S* %end.cast, i64 -1
47  %cmp = icmp eq %S* %last, %start.cast
48  ret i1 %cmp
49}
50