1; RUN: opt < %s -basicaa -gvn -instcombine -S 2>&1 | FileCheck %s
2
3target datalayout = "e-p:32:32:32-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
4
5; Make sure that basicaa thinks R and r are must aliases.
6define i32 @test1(i8 * %P) {
7entry:
8	%Q = bitcast i8* %P to {i32, i32}*
9	%R = getelementptr {i32, i32}, {i32, i32}* %Q, i32 0, i32 1
10	%S = load i32, i32* %R
11
12	%q = bitcast i8* %P to {i32, i32}*
13	%r = getelementptr {i32, i32}, {i32, i32}* %q, i32 0, i32 1
14	%s = load i32, i32* %r
15
16	%t = sub i32 %S, %s
17	ret i32 %t
18; CHECK-LABEL: @test1(
19; CHECK: ret i32 0
20}
21
22define i32 @test2(i8 * %P) {
23entry:
24	%Q = bitcast i8* %P to {i32, i32, i32}*
25	%R = getelementptr {i32, i32, i32}, {i32, i32, i32}* %Q, i32 0, i32 1
26	%S = load i32, i32* %R
27
28	%r = getelementptr {i32, i32, i32}, {i32, i32, i32}* %Q, i32 0, i32 2
29  store i32 42, i32* %r
30
31	%s = load i32, i32* %R
32
33	%t = sub i32 %S, %s
34	ret i32 %t
35; CHECK-LABEL: @test2(
36; CHECK: ret i32 0
37}
38
39
40; This was a miscompilation.
41define i32 @test3({float, {i32, i32, i32}}* %P) {
42entry:
43  %P2 = getelementptr {float, {i32, i32, i32}}, {float, {i32, i32, i32}}* %P, i32 0, i32 1
44	%R = getelementptr {i32, i32, i32}, {i32, i32, i32}* %P2, i32 0, i32 1
45	%S = load i32, i32* %R
46
47	%r = getelementptr {i32, i32, i32}, {i32, i32, i32}* %P2, i32 0, i32 2
48  store i32 42, i32* %r
49
50	%s = load i32, i32* %R
51
52	%t = sub i32 %S, %s
53	ret i32 %t
54; CHECK-LABEL: @test3(
55; CHECK: ret i32 0
56}
57
58
59;; This is reduced from the SmallPtrSet constructor.
60%SmallPtrSetImpl = type { i8**, i32, i32, i32, [1 x i8*] }
61%SmallPtrSet64 = type { %SmallPtrSetImpl, [64 x i8*] }
62
63define i32 @test4(%SmallPtrSet64* %P) {
64entry:
65  %tmp2 = getelementptr inbounds %SmallPtrSet64, %SmallPtrSet64* %P, i64 0, i32 0, i32 1
66  store i32 64, i32* %tmp2, align 8
67  %tmp3 = getelementptr inbounds %SmallPtrSet64, %SmallPtrSet64* %P, i64 0, i32 0, i32 4, i64 64
68  store i8* null, i8** %tmp3, align 8
69  %tmp4 = load i32, i32* %tmp2, align 8
70	ret i32 %tmp4
71; CHECK-LABEL: @test4(
72; CHECK: ret i32 64
73}
74
75; P[i] != p[i+1]
76define i32 @test5(i32* %p, i64 %i) {
77  %pi = getelementptr i32, i32* %p, i64 %i
78  %i.next = add i64 %i, 1
79  %pi.next = getelementptr i32, i32* %p, i64 %i.next
80  %x = load i32, i32* %pi
81  store i32 42, i32* %pi.next
82  %y = load i32, i32* %pi
83  %z = sub i32 %x, %y
84  ret i32 %z
85; CHECK-LABEL: @test5(
86; CHECK: ret i32 0
87}
88
89define i32 @test5_as1_smaller_size(i32 addrspace(1)* %p, i8 %i) {
90  %pi = getelementptr i32, i32 addrspace(1)* %p, i8 %i
91  %i.next = add i8 %i, 1
92  %pi.next = getelementptr i32, i32 addrspace(1)* %p, i8 %i.next
93  %x = load i32, i32 addrspace(1)* %pi
94  store i32 42, i32 addrspace(1)* %pi.next
95  %y = load i32, i32 addrspace(1)* %pi
96  %z = sub i32 %x, %y
97  ret i32 %z
98; CHECK-LABEL: @test5_as1_smaller_size(
99; CHECK: sext
100; CHECK: ret i32 0
101}
102
103define i32 @test5_as1_same_size(i32 addrspace(1)* %p, i16 %i) {
104  %pi = getelementptr i32, i32 addrspace(1)* %p, i16 %i
105  %i.next = add i16 %i, 1
106  %pi.next = getelementptr i32, i32 addrspace(1)* %p, i16 %i.next
107  %x = load i32, i32 addrspace(1)* %pi
108  store i32 42, i32 addrspace(1)* %pi.next
109  %y = load i32, i32 addrspace(1)* %pi
110  %z = sub i32 %x, %y
111  ret i32 %z
112; CHECK-LABEL: @test5_as1_same_size(
113; CHECK: ret i32 0
114}
115
116; P[i] != p[(i*4)|1]
117define i32 @test6(i32* %p, i64 %i1) {
118  %i = shl i64 %i1, 2
119  %pi = getelementptr i32, i32* %p, i64 %i
120  %i.next = or i64 %i, 1
121  %pi.next = getelementptr i32, i32* %p, i64 %i.next
122  %x = load i32, i32* %pi
123  store i32 42, i32* %pi.next
124  %y = load i32, i32* %pi
125  %z = sub i32 %x, %y
126  ret i32 %z
127; CHECK-LABEL: @test6(
128; CHECK: ret i32 0
129}
130
131; P[1] != P[i*4]
132define i32 @test7(i32* %p, i64 %i) {
133  %pi = getelementptr i32, i32* %p, i64 1
134  %i.next = shl i64 %i, 2
135  %pi.next = getelementptr i32, i32* %p, i64 %i.next
136  %x = load i32, i32* %pi
137  store i32 42, i32* %pi.next
138  %y = load i32, i32* %pi
139  %z = sub i32 %x, %y
140  ret i32 %z
141; CHECK-LABEL: @test7(
142; CHECK: ret i32 0
143}
144
145; P[zext(i)] != p[zext(i+1)]
146; PR1143
147define i32 @test8(i32* %p, i16 %i) {
148  %i1 = zext i16 %i to i32
149  %pi = getelementptr i32, i32* %p, i32 %i1
150  %i.next = add i16 %i, 1
151  %i.next2 = zext i16 %i.next to i32
152  %pi.next = getelementptr i32, i32* %p, i32 %i.next2
153  %x = load i32, i32* %pi
154  store i32 42, i32* %pi.next
155  %y = load i32, i32* %pi
156  %z = sub i32 %x, %y
157  ret i32 %z
158; CHECK-LABEL: @test8(
159; CHECK: ret i32 0
160}
161
162define i8 @test9([4 x i8] *%P, i32 %i, i32 %j) {
163  %i2 = shl i32 %i, 2
164  %i3 = add i32 %i2, 1
165  ; P2 = P + 1 + 4*i
166  %P2 = getelementptr [4 x i8], [4 x i8] *%P, i32 0, i32 %i3
167
168  %j2 = shl i32 %j, 2
169
170  ; P4 = P + 4*j
171  %P4 = getelementptr [4 x i8], [4 x i8]* %P, i32 0, i32 %j2
172
173  %x = load i8, i8* %P2
174  store i8 42, i8* %P4
175  %y = load i8, i8* %P2
176  %z = sub i8 %x, %y
177  ret i8 %z
178; CHECK-LABEL: @test9(
179; CHECK: ret i8 0
180}
181
182define i8 @test10([4 x i8] *%P, i32 %i) {
183  %i2 = shl i32 %i, 2
184  %i3 = add i32 %i2, 4
185  ; P2 = P + 4 + 4*i
186  %P2 = getelementptr [4 x i8], [4 x i8] *%P, i32 0, i32 %i3
187
188  ; P4 = P + 4*i
189  %P4 = getelementptr [4 x i8], [4 x i8]* %P, i32 0, i32 %i2
190
191  %x = load i8, i8* %P2
192  store i8 42, i8* %P4
193  %y = load i8, i8* %P2
194  %z = sub i8 %x, %y
195  ret i8 %z
196; CHECK-LABEL: @test10(
197; CHECK: ret i8 0
198}
199
200; (This was a miscompilation.)
201define float @test11(i32 %indvar, [4 x [2 x float]]* %q) nounwind ssp {
202  %tmp = mul i32 %indvar, -1
203  %dec = add i32 %tmp, 3
204  %scevgep = getelementptr [4 x [2 x float]], [4 x [2 x float]]* %q, i32 0, i32 %dec
205  %scevgep35 = bitcast [2 x float]* %scevgep to i64*
206  %arrayidx28 = getelementptr inbounds [4 x [2 x float]], [4 x [2 x float]]* %q, i32 0, i32 0
207  %y29 = getelementptr inbounds [2 x float], [2 x float]* %arrayidx28, i32 0, i32 1
208  store float 1.0, float* %y29, align 4
209  store i64 0, i64* %scevgep35, align 4
210  %tmp30 = load float, float* %y29, align 4
211  ret float %tmp30
212; CHECK-LABEL: @test11(
213; CHECK: ret float %tmp30
214}
215
216; (This was a miscompilation.)
217define i32 @test12(i32 %x, i32 %y, i8* %p) nounwind {
218  %a = bitcast i8* %p to [13 x i8]*
219  %b = getelementptr [13 x i8], [13 x i8]* %a, i32 %x
220  %c = bitcast [13 x i8]* %b to [15 x i8]*
221  %d = getelementptr [15 x i8], [15 x i8]* %c, i32 %y, i32 8
222  %castd = bitcast i8* %d to i32*
223  %castp = bitcast i8* %p to i32*
224  store i32 1, i32* %castp
225  store i32 0, i32* %castd
226  %r = load i32, i32* %castp
227  ret i32 %r
228; CHECK-LABEL: @test12(
229; CHECK: ret i32 %r
230}
231
232@P = internal global i32 715827882, align 4
233@Q = internal global i32 715827883, align 4
234@.str = private unnamed_addr constant [7 x i8] c"%u %u\0A\00", align 1
235
236; Make sure we recognize that u[0] and u[Global + Cst] may alias
237; when the addition has wrapping semantic.
238; PR24468.
239; CHECK-LABEL: @test13(
240; Make sure the stores appear before the related loads.
241; CHECK: store i8 42,
242; CHECK: store i8 99,
243; Find the loads and make sure they are used in the arguments to the printf.
244; CHECK: [[T0ADDR:%[a-zA-Z0-9_]+]] = getelementptr inbounds [3 x i8], [3 x i8]* %t, i32 0, i32 0
245; CHECK: [[T0:%[a-zA-Z0-9_]+]] = load i8, i8* [[T0ADDR]], align 1
246; CHECK: [[T0ARG:%[a-zA-Z0-9_]+]] = zext i8 [[T0]] to i32
247; CHECK: [[U0ADDR:%[a-zA-Z0-9_]+]] = getelementptr inbounds [3 x i8], [3 x i8]* %u, i32 0, i32 0
248; CHECK: [[U0:%[a-zA-Z0-9_]+]] = load i8, i8* [[U0ADDR]], align 1
249; CHECK: [[U0ARG:%[a-zA-Z0-9_]+]] = zext i8 [[U0]] to i32
250; CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i32 [[T0ARG]], i32 [[U0ARG]])
251; CHECK: ret
252define void @test13() {
253entry:
254  %t = alloca [3 x i8], align 1
255  %u = alloca [3 x i8], align 1
256  %tmp = load i32, i32* @P, align 4
257  %tmp1 = mul i32 %tmp, 3
258  %mul = add i32 %tmp1, -2147483646
259  %idxprom = zext i32 %mul to i64
260  %arrayidx = getelementptr inbounds [3 x i8], [3 x i8]* %t, i64 0, i64 %idxprom
261  store i8 42, i8* %arrayidx, align 1
262  %tmp2 = load i32, i32* @Q, align 4
263  %tmp3 = mul i32 %tmp2, 3
264  %mul2 = add i32 %tmp3, 2147483647
265  %idxprom3 = zext i32 %mul2 to i64
266  %arrayidx4 = getelementptr inbounds [3 x i8], [3 x i8]* %u, i64 0, i64 %idxprom3
267  store i8 99, i8* %arrayidx4, align 1
268  %arrayidx5 = getelementptr inbounds [3 x i8], [3 x i8]* %t, i64 0, i64 0
269  %tmp4 = load i8, i8* %arrayidx5, align 1
270  %conv = zext i8 %tmp4 to i32
271  %arrayidx6 = getelementptr inbounds [3 x i8], [3 x i8]* %u, i64 0, i64 0
272  %tmp5 = load i8, i8* %arrayidx6, align 1
273  %conv7 = zext i8 %tmp5 to i32
274  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %conv, i32 %conv7)
275  ret void
276}
277
278declare i32 @printf(i8*, ...)
279