1; RUN: opt -instcombine -S < %s | FileCheck %s
2
3target datalayout = "e-i64:64-f80:128-n8:16:32:64"
4target triple = "x86_64-unknown-linux-gnu"
5
6%A__vtbl = type { i8*, i32 (%A*)* }
7%A = type { %A__vtbl* }
8%B = type { i8*, i64 }
9
10@A__vtblZ = constant %A__vtbl { i8* null, i32 (%A*)* @A.foo }
11
12declare i32 @A.foo(%A* nocapture %this)
13
14define void @storeA(%A* %a.ptr) {
15; CHECK-LABEL: storeA
16; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr %A, %A* %a.ptr, i64 0, i32 0
17; CHECK-NEXT: store %A__vtbl* @A__vtblZ, %A__vtbl** [[GEP]], align 8
18; CHECK-NEXT: ret void
19  store %A { %A__vtbl* @A__vtblZ }, %A* %a.ptr, align 8
20  ret void
21}
22
23define void @storeB(%B* %b.ptr) {
24; CHECK-LABEL: storeB
25; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 0
26; CHECK-NEXT: store i8* null, i8** [[GEP1]], align 8
27; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 1
28; CHECK-NEXT: store i64 42, i64* [[GEP2]], align 8
29; CHECK-NEXT: ret void
30  store %B { i8* null, i64 42 }, %B* %b.ptr, align 8
31  ret void
32}
33
34define void @storeStructOfA({ %A }* %sa.ptr) {
35; CHECK-LABEL: storeStructOfA
36; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr { %A }, { %A }* %sa.ptr, i64 0, i32 0, i32 0
37; CHECK-NEXT: store %A__vtbl* @A__vtblZ, %A__vtbl** [[GEP]], align 8
38; CHECK-NEXT: ret void
39  store { %A } { %A { %A__vtbl* @A__vtblZ } }, { %A }* %sa.ptr, align 8
40  ret void
41}
42
43define void @storeArrayOfA([1 x %A]* %aa.ptr) {
44; CHECK-LABEL: storeArrayOfA
45; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr [1 x %A], [1 x %A]* %aa.ptr, i64 0, i64 0, i32 0
46; CHECK-NEXT: store %A__vtbl* @A__vtblZ, %A__vtbl** [[GEP]], align 8
47; CHECK-NEXT: ret void
48  store [1 x %A] [%A { %A__vtbl* @A__vtblZ }], [1 x %A]* %aa.ptr, align 8
49  ret void
50}
51
52define void @storeLargeArrayOfA([2000 x %A]* %aa.ptr) {
53; CHECK-LABEL: storeLargeArrayOfA
54; CHECK-NEXT: store [2000 x %A]
55; CHECK-NEXT: ret void
56  %i1 = insertvalue [2000 x %A] undef, %A { %A__vtbl* @A__vtblZ }, 1
57  store [2000 x %A] %i1, [2000 x %A]* %aa.ptr, align 8
58  ret void
59}
60
61define void @storeStructOfArrayOfA({ [1 x %A] }* %saa.ptr) {
62; CHECK-LABEL: storeStructOfArrayOfA
63; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr { [1 x %A] }, { [1 x %A] }* %saa.ptr, i64 0, i32 0, i64 0, i32 0
64; CHECK-NEXT: store %A__vtbl* @A__vtblZ, %A__vtbl** [[GEP]], align 8
65; CHECK-NEXT: ret void
66  store { [1 x %A] } { [1 x %A] [%A { %A__vtbl* @A__vtblZ }] }, { [1 x %A] }* %saa.ptr, align 8
67  ret void
68}
69
70define void @storeArrayOfB([2 x %B]* %ab.ptr, [2 x %B] %ab) {
71; CHECK-LABEL: storeArrayOfB
72; CHECK-NEXT: [[EVB0:%[a-z0-9\.]+]] = extractvalue [2 x %B] %ab, 0
73; CHECK-NEXT: [[GEP0:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 0
74; CHECK-NEXT: [[EV0:%[a-z0-9\.]+]] = extractvalue %B [[EVB0]], 0
75; CHECK-NEXT: store i8* [[EV0]], i8** [[GEP0]], align 8
76; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 1
77; CHECK-NEXT: [[EV1:%[a-z0-9\.]+]] = extractvalue %B [[EVB0]], 1
78; CHECK-NEXT: store i64 [[EV1]], i64* [[GEP1]], align 8
79; CHECK-NEXT: [[EVB1:%[a-z0-9\.]+]] = extractvalue [2 x %B] %ab, 1
80; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 0
81; CHECK-NEXT: [[EV2:%[a-z0-9\.]+]] = extractvalue %B [[EVB1]], 0
82; CHECK-NEXT: store i8* [[EV2]], i8** [[GEP2]], align 8
83; CHECK-NEXT: [[GEP3:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 1
84; CHECK-NEXT: [[EV3:%[a-z0-9\.]+]] = extractvalue %B [[EVB1]], 1
85; CHECK-NEXT: store i64 [[EV3]], i64* [[GEP3]], align 8
86; CHECK-NEXT: ret void
87  store [2 x %B] %ab, [2 x %B]* %ab.ptr, align 8
88  ret void
89}
90
91define %A @loadA(%A* %a.ptr) {
92; CHECK-LABEL: loadA
93; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr %A, %A* %a.ptr, i64 0, i32 0
94; CHECK-NEXT: [[LOAD:%[a-z0-9\.]+]] = load %A__vtbl*, %A__vtbl** [[GEP]], align 8
95; CHECK-NEXT: [[IV:%[a-z0-9\.]+]] = insertvalue %A undef, %A__vtbl* [[LOAD]], 0
96; CHECK-NEXT: ret %A [[IV]]
97  %1 = load %A, %A* %a.ptr, align 8
98  ret %A %1
99}
100
101define %B @loadB(%B* %b.ptr) {
102; CHECK-LABEL: loadB
103; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 0
104; CHECK-NEXT: [[LOAD1:%[a-z0-9\.]+]] = load i8*, i8** [[GEP1]], align 8
105; CHECK-NEXT: [[IV1:%[a-z0-9\.]+]] = insertvalue %B undef, i8* [[LOAD1]], 0
106; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 1
107; CHECK-NEXT: [[LOAD2:%[a-z0-9\.]+]] = load i64, i64* [[GEP2]], align 8
108; CHECK-NEXT: [[IV2:%[a-z0-9\.]+]] = insertvalue %B [[IV1]], i64 [[LOAD2]], 1
109; CHECK-NEXT: ret %B [[IV2]]
110  %1 = load %B, %B* %b.ptr, align 8
111  ret %B %1
112}
113
114define { %A } @loadStructOfA({ %A }* %sa.ptr) {
115; CHECK-LABEL: loadStructOfA
116; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr { %A }, { %A }* %sa.ptr, i64 0, i32 0, i32 0
117; CHECK-NEXT: [[LOAD:%[a-z0-9\.]+]] = load %A__vtbl*, %A__vtbl** [[GEP]], align 8
118; CHECK-NEXT: [[IV1:%[a-z0-9\.]+]] = insertvalue %A undef, %A__vtbl* [[LOAD]], 0
119; CHECK-NEXT: [[IV2:%[a-z0-9\.]+]] = insertvalue { %A } undef, %A [[IV1]], 0
120; CHECK-NEXT: ret { %A } [[IV2]]
121  %1 = load { %A }, { %A }* %sa.ptr, align 8
122  ret { %A } %1
123}
124
125define [1 x %A] @loadArrayOfA([1 x %A]* %aa.ptr) {
126; CHECK-LABEL: loadArrayOfA
127; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr [1 x %A], [1 x %A]* %aa.ptr, i64 0, i64 0, i32 0
128; CHECK-NEXT: [[LOAD:%[a-z0-9\.]+]] = load %A__vtbl*, %A__vtbl** [[GEP]], align 8
129; CHECK-NEXT: [[IV1:%[a-z0-9\.]+]] = insertvalue %A undef, %A__vtbl* [[LOAD]], 0
130; CHECK-NEXT: [[IV2:%[a-z0-9\.]+]] = insertvalue [1 x %A] undef, %A [[IV1]], 0
131; CHECK-NEXT: ret [1 x %A] [[IV2]]
132  %1 = load [1 x %A], [1 x %A]* %aa.ptr, align 8
133  ret [1 x %A] %1
134}
135
136define { [1 x %A] } @loadStructOfArrayOfA({ [1 x %A] }* %saa.ptr) {
137; CHECK-LABEL: loadStructOfArrayOfA
138; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr { [1 x %A] }, { [1 x %A] }* %saa.ptr, i64 0, i32 0, i64 0, i32 0
139; CHECK-NEXT: [[LOAD:%[a-z0-9\.]+]] = load %A__vtbl*, %A__vtbl** [[GEP]], align 8
140; CHECK-NEXT: [[IV1:%[a-z0-9\.]+]] = insertvalue %A undef, %A__vtbl* [[LOAD]], 0
141; CHECK-NEXT: [[IV2:%[a-z0-9\.]+]] = insertvalue [1 x %A] undef, %A [[IV1]], 0
142; CHECK-NEXT: [[IV3:%[a-z0-9\.]+]] = insertvalue { [1 x %A] } undef, [1 x %A] [[IV2]], 0
143; CHECK-NEXT: ret { [1 x %A] } [[IV3]]
144  %1 = load { [1 x %A] }, { [1 x %A] }* %saa.ptr, align 8
145  ret { [1 x %A] } %1
146}
147
148define { %A } @structOfA({ %A }* %sa.ptr) {
149; CHECK-LABEL: structOfA
150; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr { %A }, { %A }* %sa.ptr, i64 0, i32 0, i32 0
151; CHECK-NEXT: store %A__vtbl* @A__vtblZ, %A__vtbl** [[GEP]], align 8
152; CHECK-NEXT: ret { %A } { %A { %A__vtbl* @A__vtblZ } }
153  store { %A } { %A { %A__vtbl* @A__vtblZ } }, { %A }* %sa.ptr, align 8
154  %1 = load { %A }, { %A }* %sa.ptr, align 8
155  ret { %A } %1
156}
157
158define %B @structB(%B* %b.ptr) {
159; CHECK-LABEL: structB
160; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 0
161; CHECK-NEXT: store i8* null, i8** [[GEP1]], align 8
162; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds %B, %B* %b.ptr, i64 0, i32 1
163; CHECK-NEXT: store i64 42, i64* [[GEP2]], align 8
164; CHECK-NEXT: ret %B { i8* null, i64 42 }
165  store %B { i8* null, i64 42 }, %B* %b.ptr, align 8
166  %1 = load %B, %B* %b.ptr, align 8
167  ret %B %1
168}
169
170define [2 x %B] @loadArrayOfB([2 x %B]* %ab.ptr) {
171; CHECK-LABEL: loadArrayOfB
172; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 0
173; CHECK-NEXT: [[LOAD1:%[a-z0-9\.]+]] = load i8*, i8** [[GEP1]], align 8
174; CHECK-NEXT: [[IV1:%[a-z0-9\.]+]] = insertvalue %B undef, i8* [[LOAD1]], 0
175; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 1
176; CHECK-NEXT: [[LOAD2:%[a-z0-9\.]+]] = load i64, i64* [[GEP2]], align 8
177; CHECK-NEXT: [[IV2:%[a-z0-9\.]+]] = insertvalue %B [[IV1]], i64 [[LOAD2]], 1
178; CHECK-NEXT: [[IV3:%[a-z0-9\.]+]] = insertvalue [2 x %B] undef, %B [[IV2]], 0
179; CHECK-NEXT: [[GEP3:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 0
180; CHECK-NEXT: [[LOAD3:%[a-z0-9\.]+]] = load i8*, i8** [[GEP3]], align 8
181; CHECK-NEXT: [[IV4:%[a-z0-9\.]+]] = insertvalue %B undef, i8* [[LOAD3]], 0
182; CHECK-NEXT: [[GEP4:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 1
183; CHECK-NEXT: [[LOAD4:%[a-z0-9\.]+]] = load i64, i64* [[GEP4]], align 8
184; CHECK-NEXT: [[IV5:%[a-z0-9\.]+]] = insertvalue %B [[IV4]], i64 [[LOAD4]], 1
185; CHECK-NEXT: [[IV6:%[a-z0-9\.]+]] = insertvalue [2 x %B] [[IV3]], %B [[IV5]], 1
186; CHECK-NEXT: ret [2 x %B] [[IV6]]
187  %1 = load [2 x %B], [2 x %B]* %ab.ptr, align 8
188  ret [2 x %B] %1
189}
190
191define [2000 x %B] @loadLargeArrayOfB([2000 x %B]* %ab.ptr) {
192; CHECK-LABEL: loadLargeArrayOfB
193; CHECK-NEXT: load [2000 x %B], [2000 x %B]* %ab.ptr, align 8
194; CHECK-NEXT: ret [2000 x %B]
195  %1 = load [2000 x %B], [2000 x %B]* %ab.ptr, align 8
196  ret [2000 x %B] %1
197}
198
199%struct.S = type <{ i8, %struct.T }>
200%struct.T = type { i32, i32 }
201
202; Make sure that we do not increase alignment of packed struct element
203define i32 @packed_alignment(%struct.S* dereferenceable(9) %s) {
204; CHECK-LABEL: packed_alignment
205; CHECK-NEXT: %tv.elt1 = getelementptr inbounds %struct.S, %struct.S* %s, i64 0, i32 1, i32 1
206; CHECK-NEXT: %tv.unpack2 = load i32, i32* %tv.elt1, align 1
207; CHECK-NEXT: ret i32 %tv.unpack2
208  %t = getelementptr inbounds %struct.S, %struct.S* %s, i32 0, i32 1
209  %tv = load %struct.T, %struct.T* %t, align 1
210  %v = extractvalue %struct.T %tv, 1
211  ret i32 %v
212}
213
214%struct.U = type {i8, i8, i8, i8, i8, i8, i8, i8, i64}
215
216define void @check_alignment(%struct.U* %u, %struct.U* %v) {
217; CHECK-LABEL: check_alignment
218; CHECK: load i8, i8* {{.*}}, align 8
219; CHECK: load i8, i8* {{.*}}, align 1
220; CHECK: load i8, i8* {{.*}}, align 2
221; CHECK: load i8, i8* {{.*}}, align 1
222; CHECK: load i8, i8* {{.*}}, align 4
223; CHECK: load i8, i8* {{.*}}, align 1
224; CHECK: load i8, i8* {{.*}}, align 2
225; CHECK: load i8, i8* {{.*}}, align 1
226; CHECK: load i64, i64* {{.*}}, align 8
227; CHECK: store i8 {{.*}}, i8* {{.*}}, align 8
228; CHECK: store i8 {{.*}}, i8* {{.*}}, align 1
229; CHECK: store i8 {{.*}}, i8* {{.*}}, align 2
230; CHECK: store i8 {{.*}}, i8* {{.*}}, align 1
231; CHECK: store i8 {{.*}}, i8* {{.*}}, align 4
232; CHECK: store i8 {{.*}}, i8* {{.*}}, align 1
233; CHECK: store i8 {{.*}}, i8* {{.*}}, align 2
234; CHECK: store i8 {{.*}}, i8* {{.*}}, align 1
235; CHECK: store i64 {{.*}}, i64* {{.*}}, align 8
236  %1 = load %struct.U, %struct.U* %u
237  store %struct.U %1, %struct.U* %v
238  ret void
239}
240