1; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+avx -fixup-byte-word-insts=1 < %s | FileCheck -check-prefix=CHECK -check-prefix=BWON %s
2; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+avx -fixup-byte-word-insts=0 < %s | FileCheck -check-prefix=CHECK -check-prefix=BWOFF %s
3
4%struct.A = type { i8, i8, i8, i8, i8, i8, i8, i8 }
5%struct.B = type { i32, i32, i32, i32, i32, i32, i32, i32 }
6
7; CHECK-LABEL: merge_const_store:
8; save 1,2,3 ... as one big integer.
9; CHECK: movabsq $578437695752307201
10; CHECK: ret
11define void @merge_const_store(i32 %count, %struct.A* nocapture %p) nounwind uwtable noinline ssp {
12  %1 = icmp sgt i32 %count, 0
13  br i1 %1, label %.lr.ph, label %._crit_edge
14.lr.ph:
15  %i.02 = phi i32 [ %10, %.lr.ph ], [ 0, %0 ]
16  %.01 = phi %struct.A* [ %11, %.lr.ph ], [ %p, %0 ]
17  %2 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 0
18  store i8 1, i8* %2, align 1
19  %3 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 1
20  store i8 2, i8* %3, align 1
21  %4 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 2
22  store i8 3, i8* %4, align 1
23  %5 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 3
24  store i8 4, i8* %5, align 1
25  %6 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 4
26  store i8 5, i8* %6, align 1
27  %7 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 5
28  store i8 6, i8* %7, align 1
29  %8 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 6
30  store i8 7, i8* %8, align 1
31  %9 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 7
32  store i8 8, i8* %9, align 1
33  %10 = add nsw i32 %i.02, 1
34  %11 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 1
35  %exitcond = icmp eq i32 %10, %count
36  br i1 %exitcond, label %._crit_edge, label %.lr.ph
37._crit_edge:
38  ret void
39}
40
41; No vectors because we use noimplicitfloat
42; CHECK-LABEL: merge_const_store_no_vec:
43; CHECK-NOT: vmovups
44; CHECK: ret
45define void @merge_const_store_no_vec(i32 %count, %struct.B* nocapture %p) noimplicitfloat{
46  %1 = icmp sgt i32 %count, 0
47  br i1 %1, label %.lr.ph, label %._crit_edge
48.lr.ph:
49  %i.02 = phi i32 [ %10, %.lr.ph ], [ 0, %0 ]
50  %.01 = phi %struct.B* [ %11, %.lr.ph ], [ %p, %0 ]
51  %2 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 0
52  store i32 0, i32* %2, align 4
53  %3 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 1
54  store i32 0, i32* %3, align 4
55  %4 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 2
56  store i32 0, i32* %4, align 4
57  %5 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 3
58  store i32 0, i32* %5, align 4
59  %6 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 4
60  store i32 0, i32* %6, align 4
61  %7 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 5
62  store i32 0, i32* %7, align 4
63  %8 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 6
64  store i32 0, i32* %8, align 4
65  %9 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 7
66  store i32 0, i32* %9, align 4
67  %10 = add nsw i32 %i.02, 1
68  %11 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 1
69  %exitcond = icmp eq i32 %10, %count
70  br i1 %exitcond, label %._crit_edge, label %.lr.ph
71._crit_edge:
72  ret void
73}
74
75; Move the constants using a single vector store.
76; CHECK-LABEL: merge_const_store_vec:
77; CHECK: vmovups
78; CHECK: ret
79define void @merge_const_store_vec(i32 %count, %struct.B* nocapture %p) nounwind uwtable noinline ssp {
80  %1 = icmp sgt i32 %count, 0
81  br i1 %1, label %.lr.ph, label %._crit_edge
82.lr.ph:
83  %i.02 = phi i32 [ %10, %.lr.ph ], [ 0, %0 ]
84  %.01 = phi %struct.B* [ %11, %.lr.ph ], [ %p, %0 ]
85  %2 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 0
86  store i32 0, i32* %2, align 4
87  %3 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 1
88  store i32 0, i32* %3, align 4
89  %4 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 2
90  store i32 0, i32* %4, align 4
91  %5 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 3
92  store i32 0, i32* %5, align 4
93  %6 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 4
94  store i32 0, i32* %6, align 4
95  %7 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 5
96  store i32 0, i32* %7, align 4
97  %8 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 6
98  store i32 0, i32* %8, align 4
99  %9 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 7
100  store i32 0, i32* %9, align 4
101  %10 = add nsw i32 %i.02, 1
102  %11 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 1
103  %exitcond = icmp eq i32 %10, %count
104  br i1 %exitcond, label %._crit_edge, label %.lr.ph
105._crit_edge:
106  ret void
107}
108
109; Move the first 4 constants as a single vector. Move the rest as scalars.
110; CHECK-LABEL: merge_nonconst_store:
111; CHECK: movl $67305985
112; CHECK: movb
113; CHECK: movw
114; CHECK: movb
115; CHECK: ret
116define void @merge_nonconst_store(i32 %count, i8 %zz, %struct.A* nocapture %p) nounwind uwtable noinline ssp {
117  %1 = icmp sgt i32 %count, 0
118  br i1 %1, label %.lr.ph, label %._crit_edge
119.lr.ph:
120  %i.02 = phi i32 [ %10, %.lr.ph ], [ 0, %0 ]
121  %.01 = phi %struct.A* [ %11, %.lr.ph ], [ %p, %0 ]
122  %2 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 0
123  store i8 1, i8* %2, align 1
124  %3 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 1
125  store i8 2, i8* %3, align 1
126  %4 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 2
127  store i8 3, i8* %4, align 1
128  %5 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 3
129  store i8 4, i8* %5, align 1
130  %6 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 4
131  store i8 %zz, i8* %6, align 1                     ;  <----------- Not a const;
132  %7 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 5
133  store i8 6, i8* %7, align 1
134  %8 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 6
135  store i8 7, i8* %8, align 1
136  %9 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 7
137  store i8 8, i8* %9, align 1
138  %10 = add nsw i32 %i.02, 1
139  %11 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 1
140  %exitcond = icmp eq i32 %10, %count
141  br i1 %exitcond, label %._crit_edge, label %.lr.ph
142._crit_edge:
143  ret void
144}
145
146
147; CHECK-LABEL: merge_loads_i16:
148;  load:
149; BWON:  movzwl
150; BWOFF: movw
151;  store:
152; CHECK: movw
153; CHECK: ret
154define void @merge_loads_i16(i32 %count, %struct.A* noalias nocapture %q, %struct.A* noalias nocapture %p) nounwind uwtable noinline ssp {
155  %1 = icmp sgt i32 %count, 0
156  br i1 %1, label %.lr.ph, label %._crit_edge
157
158.lr.ph:                                           ; preds = %0
159  %2 = getelementptr inbounds %struct.A, %struct.A* %q, i64 0, i32 0
160  %3 = getelementptr inbounds %struct.A, %struct.A* %q, i64 0, i32 1
161  br label %4
162
163; <label>:4                                       ; preds = %4, %.lr.ph
164  %i.02 = phi i32 [ 0, %.lr.ph ], [ %9, %4 ]
165  %.01 = phi %struct.A* [ %p, %.lr.ph ], [ %10, %4 ]
166  %5 = load i8, i8* %2, align 1
167  %6 = load i8, i8* %3, align 1
168  %7 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 0
169  store i8 %5, i8* %7, align 1
170  %8 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 1
171  store i8 %6, i8* %8, align 1
172  %9 = add nsw i32 %i.02, 1
173  %10 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 1
174  %exitcond = icmp eq i32 %9, %count
175  br i1 %exitcond, label %._crit_edge, label %4
176
177._crit_edge:                                      ; preds = %4, %0
178  ret void
179}
180
181; The loads and the stores are interleaved. Can't merge them.
182; CHECK-LABEL: no_merge_loads:
183; BWON:  movzbl
184; BWOFF: movb
185; CHECK: movb
186; BWON:  movzbl
187; BWOFF: movb
188; CHECK: movb
189; CHECK: ret
190define void @no_merge_loads(i32 %count, %struct.A* noalias nocapture %q, %struct.A* noalias nocapture %p) nounwind uwtable noinline ssp {
191  %1 = icmp sgt i32 %count, 0
192  br i1 %1, label %.lr.ph, label %._crit_edge
193
194.lr.ph:                                           ; preds = %0
195  %2 = getelementptr inbounds %struct.A, %struct.A* %q, i64 0, i32 0
196  %3 = getelementptr inbounds %struct.A, %struct.A* %q, i64 0, i32 1
197  br label %a4
198
199a4:                                       ; preds = %4, %.lr.ph
200  %i.02 = phi i32 [ 0, %.lr.ph ], [ %a9, %a4 ]
201  %.01 = phi %struct.A* [ %p, %.lr.ph ], [ %a10, %a4 ]
202  %a5 = load i8, i8* %2, align 1
203  %a7 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 0
204  store i8 %a5, i8* %a7, align 1
205  %a8 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 0, i32 1
206  %a6 = load i8, i8* %3, align 1
207  store i8 %a6, i8* %a8, align 1
208  %a9 = add nsw i32 %i.02, 1
209  %a10 = getelementptr inbounds %struct.A, %struct.A* %.01, i64 1
210  %exitcond = icmp eq i32 %a9, %count
211  br i1 %exitcond, label %._crit_edge, label %a4
212
213._crit_edge:                                      ; preds = %4, %0
214  ret void
215}
216
217
218; CHECK-LABEL: merge_loads_integer:
219;  load:
220; CHECK: movq
221;  store:
222; CHECK: movq
223; CHECK: ret
224define void @merge_loads_integer(i32 %count, %struct.B* noalias nocapture %q, %struct.B* noalias nocapture %p) nounwind uwtable noinline ssp {
225  %1 = icmp sgt i32 %count, 0
226  br i1 %1, label %.lr.ph, label %._crit_edge
227
228.lr.ph:                                           ; preds = %0
229  %2 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 0
230  %3 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 1
231  br label %4
232
233; <label>:4                                       ; preds = %4, %.lr.ph
234  %i.02 = phi i32 [ 0, %.lr.ph ], [ %9, %4 ]
235  %.01 = phi %struct.B* [ %p, %.lr.ph ], [ %10, %4 ]
236  %5 = load i32, i32* %2
237  %6 = load i32, i32* %3
238  %7 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 0
239  store i32 %5, i32* %7
240  %8 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 1
241  store i32 %6, i32* %8
242  %9 = add nsw i32 %i.02, 1
243  %10 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 1
244  %exitcond = icmp eq i32 %9, %count
245  br i1 %exitcond, label %._crit_edge, label %4
246
247._crit_edge:                                      ; preds = %4, %0
248  ret void
249}
250
251
252; CHECK-LABEL: merge_loads_vector:
253;  load:
254; CHECK: movups
255;  store:
256; CHECK: movups
257; CHECK: ret
258define void @merge_loads_vector(i32 %count, %struct.B* noalias nocapture %q, %struct.B* noalias nocapture %p) nounwind uwtable noinline ssp {
259  %a1 = icmp sgt i32 %count, 0
260  br i1 %a1, label %.lr.ph, label %._crit_edge
261
262.lr.ph:                                           ; preds = %0
263  %a2 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 0
264  %a3 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 1
265  %a4 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 2
266  %a5 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 3
267  br label %block4
268
269block4:                                       ; preds = %4, %.lr.ph
270  %i.02 = phi i32 [ 0, %.lr.ph ], [ %c9, %block4 ]
271  %.01 = phi %struct.B* [ %p, %.lr.ph ], [ %c10, %block4 ]
272  %a7 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 0
273  %a8 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 1
274  %a9 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 2
275  %a10 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 3
276  %b1 = load i32, i32* %a2
277  %b2 = load i32, i32* %a3
278  %b3 = load i32, i32* %a4
279  %b4 = load i32, i32* %a5
280  store i32 %b1, i32* %a7
281  store i32 %b2, i32* %a8
282  store i32 %b3, i32* %a9
283  store i32 %b4, i32* %a10
284  %c9 = add nsw i32 %i.02, 1
285  %c10 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 1
286  %exitcond = icmp eq i32 %c9, %count
287  br i1 %exitcond, label %._crit_edge, label %block4
288
289._crit_edge:                                      ; preds = %4, %0
290  ret void
291}
292
293;; On x86, even unaligned copies can be merged to vector ops.
294; CHECK-LABEL: merge_loads_no_align:
295;  load:
296; CHECK: vmovups
297;  store:
298; CHECK: vmovups
299; CHECK: ret
300define void @merge_loads_no_align(i32 %count, %struct.B* noalias nocapture %q, %struct.B* noalias nocapture %p) nounwind uwtable noinline ssp {
301  %a1 = icmp sgt i32 %count, 0
302  br i1 %a1, label %.lr.ph, label %._crit_edge
303
304.lr.ph:                                           ; preds = %0
305  %a2 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 0
306  %a3 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 1
307  %a4 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 2
308  %a5 = getelementptr inbounds %struct.B, %struct.B* %q, i64 0, i32 3
309  br label %block4
310
311block4:                                       ; preds = %4, %.lr.ph
312  %i.02 = phi i32 [ 0, %.lr.ph ], [ %c9, %block4 ]
313  %.01 = phi %struct.B* [ %p, %.lr.ph ], [ %c10, %block4 ]
314  %a7 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 0
315  %a8 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 1
316  %a9 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 2
317  %a10 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 0, i32 3
318  %b1 = load i32, i32* %a2, align 1
319  %b2 = load i32, i32* %a3, align 1
320  %b3 = load i32, i32* %a4, align 1
321  %b4 = load i32, i32* %a5, align 1
322  store i32 %b1, i32* %a7, align 1
323  store i32 %b2, i32* %a8, align 1
324  store i32 %b3, i32* %a9, align 1
325  store i32 %b4, i32* %a10, align 1
326  %c9 = add nsw i32 %i.02, 1
327  %c10 = getelementptr inbounds %struct.B, %struct.B* %.01, i64 1
328  %exitcond = icmp eq i32 %c9, %count
329  br i1 %exitcond, label %._crit_edge, label %block4
330
331._crit_edge:                                      ; preds = %4, %0
332  ret void
333}
334
335; Make sure that we merge the consecutive load/store sequence below and use a
336; word (16 bit) instead of a byte copy.
337; CHECK-LABEL: MergeLoadStoreBaseIndexOffset:
338; BWON: movzwl   (%{{.*}},%{{.*}}), %e[[REG:[a-z]+]]
339; BWOFF: movw    (%{{.*}},%{{.*}}), %[[REG:[a-z]+]]
340; CHECK: movw    %[[REG]], (%{{.*}})
341define void @MergeLoadStoreBaseIndexOffset(i64* %a, i8* %b, i8* %c, i32 %n) {
342  br label %1
343
344; <label>:1
345  %.09 = phi i32 [ %n, %0 ], [ %11, %1 ]
346  %.08 = phi i8* [ %b, %0 ], [ %10, %1 ]
347  %.0 = phi i64* [ %a, %0 ], [ %2, %1 ]
348  %2 = getelementptr inbounds i64, i64* %.0, i64 1
349  %3 = load i64, i64* %.0, align 1
350  %4 = getelementptr inbounds i8, i8* %c, i64 %3
351  %5 = load i8, i8* %4, align 1
352  %6 = add i64 %3, 1
353  %7 = getelementptr inbounds i8, i8* %c, i64 %6
354  %8 = load i8, i8* %7, align 1
355  store i8 %5, i8* %.08, align 1
356  %9 = getelementptr inbounds i8, i8* %.08, i64 1
357  store i8 %8, i8* %9, align 1
358  %10 = getelementptr inbounds i8, i8* %.08, i64 2
359  %11 = add nsw i32 %.09, -1
360  %12 = icmp eq i32 %11, 0
361  br i1 %12, label %13, label %1
362
363; <label>:13
364  ret void
365}
366
367; Make sure that we merge the consecutive load/store sequence below and use a
368; word (16 bit) instead of a byte copy for complicated address calculation.
369; .
370; CHECK-LABEL: MergeLoadStoreBaseIndexOffsetComplicated:
371; BWON: movzwl   (%{{.*}},%{{.*}}), %e[[REG:[a-z]+]]
372; BWOFF: movw    (%{{.*}},%{{.*}}), %[[REG:[a-z]+]]
373; CHECK: movw    %[[REG]], (%{{.*}})
374define void @MergeLoadStoreBaseIndexOffsetComplicated(i8* %a, i8* %b, i8* %c, i64 %n) {
375  br label %1
376
377; <label>:1
378  %.09 = phi i64 [ 0, %0 ], [ %13, %1 ]
379  %.08 = phi i8* [ %b, %0 ], [ %12, %1 ]
380  %2 = load i8, i8* %.08, align 1
381  %3 = sext i8 %2 to i64
382  %4 = getelementptr inbounds i8, i8* %c, i64 %3
383  %5 = load i8, i8* %4, align 1
384  %6 = add nsw i64 %3, 1
385  %7 = getelementptr inbounds i8, i8* %c, i64 %6
386  %8 = load i8, i8* %7, align 1
387  %9 = getelementptr inbounds i8, i8* %a, i64 %.09
388  store i8 %5, i8* %9, align 1
389  %10 = or i64 %.09, 1
390  %11 = getelementptr inbounds i8, i8* %a, i64 %10
391  store i8 %8, i8* %11, align 1
392  %12 = getelementptr inbounds i8, i8* %.08, i64 1
393  %13 = add nuw nsw i64 %.09, 2
394  %14 = icmp slt i64 %13, %n
395  br i1 %14, label %1, label %15
396
397; <label>:15
398  ret void
399}
400
401; Make sure that we merge the consecutive load/store sequence below and use a
402; word (16 bit) instead of a byte copy even if there are intermediate sign
403; extensions.
404; CHECK-LABEL: MergeLoadStoreBaseIndexOffsetSext:
405; BWON: movzwl   (%{{.*}},%{{.*}}), %e[[REG:[a-z]+]]
406; BWOFF: movw    (%{{.*}},%{{.*}}), %[[REG:[a-z]+]]
407; CHECK: movw    %[[REG]], (%{{.*}})
408define void @MergeLoadStoreBaseIndexOffsetSext(i8* %a, i8* %b, i8* %c, i32 %n) {
409  br label %1
410
411; <label>:1
412  %.09 = phi i32 [ %n, %0 ], [ %12, %1 ]
413  %.08 = phi i8* [ %b, %0 ], [ %11, %1 ]
414  %.0 = phi i8* [ %a, %0 ], [ %2, %1 ]
415  %2 = getelementptr inbounds i8, i8* %.0, i64 1
416  %3 = load i8, i8* %.0, align 1
417  %4 = sext i8 %3 to i64
418  %5 = getelementptr inbounds i8, i8* %c, i64 %4
419  %6 = load i8, i8* %5, align 1
420  %7 = add i64 %4, 1
421  %8 = getelementptr inbounds i8, i8* %c, i64 %7
422  %9 = load i8, i8* %8, align 1
423  store i8 %6, i8* %.08, align 1
424  %10 = getelementptr inbounds i8, i8* %.08, i64 1
425  store i8 %9, i8* %10, align 1
426  %11 = getelementptr inbounds i8, i8* %.08, i64 2
427  %12 = add nsw i32 %.09, -1
428  %13 = icmp eq i32 %12, 0
429  br i1 %13, label %14, label %1
430
431; <label>:14
432  ret void
433}
434
435; However, we can only merge ignore sign extensions when they are on all memory
436; computations;
437; CHECK-LABEL: loadStoreBaseIndexOffsetSextNoSex:
438; CHECK-NOT: movw    (%{{.*}},%{{.*}}), [[REG:%[a-z]+]]
439; CHECK-NOT: movw    [[REG]], (%{{.*}})
440define void @loadStoreBaseIndexOffsetSextNoSex(i8* %a, i8* %b, i8* %c, i32 %n) {
441  br label %1
442
443; <label>:1
444  %.09 = phi i32 [ %n, %0 ], [ %12, %1 ]
445  %.08 = phi i8* [ %b, %0 ], [ %11, %1 ]
446  %.0 = phi i8* [ %a, %0 ], [ %2, %1 ]
447  %2 = getelementptr inbounds i8, i8* %.0, i64 1
448  %3 = load i8, i8* %.0, align 1
449  %4 = sext i8 %3 to i64
450  %5 = getelementptr inbounds i8, i8* %c, i64 %4
451  %6 = load i8, i8* %5, align 1
452  %7 = add i8 %3, 1
453  %wrap.4 = sext i8 %7 to i64
454  %8 = getelementptr inbounds i8, i8* %c, i64 %wrap.4
455  %9 = load i8, i8* %8, align 1
456  store i8 %6, i8* %.08, align 1
457  %10 = getelementptr inbounds i8, i8* %.08, i64 1
458  store i8 %9, i8* %10, align 1
459  %11 = getelementptr inbounds i8, i8* %.08, i64 2
460  %12 = add nsw i32 %.09, -1
461  %13 = icmp eq i32 %12, 0
462  br i1 %13, label %14, label %1
463
464; <label>:14
465  ret void
466}
467
468; PR21711 ( http://llvm.org/bugs/show_bug.cgi?id=21711 )
469define void @merge_vec_element_store(<8 x float> %v, float* %ptr) {
470  %vecext0 = extractelement <8 x float> %v, i32 0
471  %vecext1 = extractelement <8 x float> %v, i32 1
472  %vecext2 = extractelement <8 x float> %v, i32 2
473  %vecext3 = extractelement <8 x float> %v, i32 3
474  %vecext4 = extractelement <8 x float> %v, i32 4
475  %vecext5 = extractelement <8 x float> %v, i32 5
476  %vecext6 = extractelement <8 x float> %v, i32 6
477  %vecext7 = extractelement <8 x float> %v, i32 7
478  %arrayidx1 = getelementptr inbounds float, float* %ptr, i64 1
479  %arrayidx2 = getelementptr inbounds float, float* %ptr, i64 2
480  %arrayidx3 = getelementptr inbounds float, float* %ptr, i64 3
481  %arrayidx4 = getelementptr inbounds float, float* %ptr, i64 4
482  %arrayidx5 = getelementptr inbounds float, float* %ptr, i64 5
483  %arrayidx6 = getelementptr inbounds float, float* %ptr, i64 6
484  %arrayidx7 = getelementptr inbounds float, float* %ptr, i64 7
485  store float %vecext0, float* %ptr, align 4
486  store float %vecext1, float* %arrayidx1, align 4
487  store float %vecext2, float* %arrayidx2, align 4
488  store float %vecext3, float* %arrayidx3, align 4
489  store float %vecext4, float* %arrayidx4, align 4
490  store float %vecext5, float* %arrayidx5, align 4
491  store float %vecext6, float* %arrayidx6, align 4
492  store float %vecext7, float* %arrayidx7, align 4
493  ret void
494
495; CHECK-LABEL: merge_vec_element_store
496; CHECK: vmovups %ymm0, (%rdi)
497; CHECK: vzeroupper
498; CHECK: retq
499}
500
501; PR21711 - Merge vector stores into wider vector stores.
502; These should be merged into 32-byte stores.
503define void @merge_vec_extract_stores(<8 x float> %v1, <8 x float> %v2, <4 x float>* %ptr) {
504  %idx0 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 3
505  %idx1 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 4
506  %idx2 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 5
507  %idx3 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 6
508  %shuffle0 = shufflevector <8 x float> %v1, <8 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
509  %shuffle1 = shufflevector <8 x float> %v1, <8 x float> undef, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
510  %shuffle2 = shufflevector <8 x float> %v2, <8 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
511  %shuffle3 = shufflevector <8 x float> %v2, <8 x float> undef, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
512  store <4 x float> %shuffle0, <4 x float>* %idx0, align 16
513  store <4 x float> %shuffle1, <4 x float>* %idx1, align 16
514  store <4 x float> %shuffle2, <4 x float>* %idx2, align 16
515  store <4 x float> %shuffle3, <4 x float>* %idx3, align 16
516  ret void
517
518; CHECK-LABEL: merge_vec_extract_stores
519; CHECK:      vmovups %ymm0, 48(%rdi)
520; CHECK-NEXT: vmovups %ymm1, 80(%rdi)
521; CHECK-NEXT: vzeroupper
522; CHECK-NEXT: retq
523}
524
525; Merging vector stores when sourced from vector loads.
526define void @merge_vec_stores_from_loads(<4 x float>* %v, <4 x float>* %ptr) {
527  %load_idx0 = getelementptr inbounds <4 x float>, <4 x float>* %v, i64 0
528  %load_idx1 = getelementptr inbounds <4 x float>, <4 x float>* %v, i64 1
529  %v0 = load <4 x float>, <4 x float>* %load_idx0
530  %v1 = load <4 x float>, <4 x float>* %load_idx1
531  %store_idx0 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 0
532  %store_idx1 = getelementptr inbounds <4 x float>, <4 x float>* %ptr, i64 1
533  store <4 x float> %v0, <4 x float>* %store_idx0, align 16
534  store <4 x float> %v1, <4 x float>* %store_idx1, align 16
535  ret void
536
537; CHECK-LABEL: merge_vec_stores_from_loads
538; CHECK:      vmovups (%rdi), %ymm0
539; CHECK-NEXT: vmovups %ymm0, (%rsi)
540; CHECK-NEXT: vzeroupper
541; CHECK-NEXT: retq
542}
543
544; Merging vector stores when sourced from a constant vector is not currently handled.
545define void @merge_vec_stores_of_constants(<4 x i32>* %ptr) {
546  %idx0 = getelementptr inbounds <4 x i32>, <4 x i32>* %ptr, i64 3
547  %idx1 = getelementptr inbounds <4 x i32>, <4 x i32>* %ptr, i64 4
548  store <4 x i32> <i32 0, i32 0, i32 0, i32 0>, <4 x i32>* %idx0, align 16
549  store <4 x i32> <i32 0, i32 0, i32 0, i32 0>, <4 x i32>* %idx1, align 16
550  ret void
551
552; CHECK-LABEL: merge_vec_stores_of_constants
553; CHECK:      vxorps
554; CHECK-NEXT: vmovaps
555; CHECK-NEXT: vmovaps
556; CHECK-NEXT: retq
557}
558
559; This is a minimized test based on real code that was failing.
560; This should now be merged.
561define void @merge_vec_element_and_scalar_load([6 x i64]* %array) {
562  %idx0 = getelementptr inbounds [6 x i64], [6 x i64]* %array, i64 0, i64 0
563  %idx1 = getelementptr inbounds [6 x i64], [6 x i64]* %array, i64 0, i64 1
564  %idx4 = getelementptr inbounds [6 x i64], [6 x i64]* %array, i64 0, i64 4
565  %idx5 = getelementptr inbounds [6 x i64], [6 x i64]* %array, i64 0, i64 5
566
567  %a0 = load i64, i64* %idx0, align 8
568  store i64 %a0, i64* %idx4, align 8
569
570  %b = bitcast i64* %idx1 to <2 x i64>*
571  %v = load <2 x i64>, <2 x i64>* %b, align 8
572  %a1 = extractelement <2 x i64> %v, i32 0
573  store i64 %a1, i64* %idx5, align 8
574  ret void
575
576; CHECK-LABEL: merge_vec_element_and_scalar_load
577; CHECK:      vmovups (%rdi), %xmm0
578; CHECK-NEXT: vmovups %xmm0, 32(%rdi)
579; CHECK-NEXT: retq
580}
581
582; Don't let a non-consecutive store thwart merging of the last two.
583define void @almost_consecutive_stores(i8* %p) {
584  store i8 0, i8* %p
585  %p1 = getelementptr i8, i8* %p, i64 42
586  store i8 1, i8* %p1
587  %p2 = getelementptr i8, i8* %p, i64 2
588  store i8 2, i8* %p2
589  %p3 = getelementptr i8, i8* %p, i64 3
590  store i8 3, i8* %p3
591  ret void
592; CHECK-LABEL: almost_consecutive_stores
593; CHECK-DAG: movb $0, (%rdi)
594; CHECK-DAG: movb $1, 42(%rdi)
595; CHECK-DAG: movw $770, 2(%rdi)
596; CHECK: retq
597}
598
599; We should be able to merge these.
600define void @merge_bitcast(<4 x i32> %v, float* %ptr) {
601  %fv = bitcast <4 x i32> %v to <4 x float>
602
603  %vecext1 = extractelement <4 x i32> %v, i32 1
604  %vecext2 = extractelement <4 x i32> %v, i32 2
605  %vecext3 = extractelement <4 x i32> %v, i32 3
606  %f0 = extractelement <4 x float> %fv, i32 0
607  %f1 = bitcast i32 %vecext1 to float
608  %f2 = bitcast i32 %vecext2 to float
609  %f3 = bitcast i32 %vecext3 to float
610  %idx0 = getelementptr inbounds float, float* %ptr, i64 0
611  %idx1 = getelementptr inbounds float, float* %ptr, i64 1
612  %idx2 = getelementptr inbounds float, float* %ptr, i64 2
613  %idx3 = getelementptr inbounds float, float* %ptr, i64 3
614  store float %f0, float* %idx0, align 4
615  store float %f1, float* %idx1, align 4
616  store float %f2, float* %idx2, align 4
617  store float %f3, float* %idx3, align 4
618  ret void
619
620; CHECK-LABEL: merge_bitcast
621; CHECK:      vmovups	%xmm0, (%rdi)
622; CHECK-NEXT: retq
623}
624