1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -dse -enable-dse-memoryssa=false -enable-dse-partial-store-merging -S < %s | FileCheck %s
3target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-f128:128:128-n8:16:32:64"
4
5define void @byte_by_byte_replacement(i32 *%ptr) {
6; CHECK-LABEL: @byte_by_byte_replacement(
7; CHECK-NEXT:  entry:
8; CHECK-NEXT:    store i32 202050057, i32* [[PTR:%.*]], align 4
9; CHECK-NEXT:    ret void
10;
11entry:
12  ;; This store's value should be modified as it should be better to use one
13  ;; larger store than several smaller ones.
14  ;; store will turn into 0x0C0B0A09 == 202050057
15  store i32 305419896, i32* %ptr  ; 0x12345678
16  %bptr = bitcast i32* %ptr to i8*
17  %bptr1 = getelementptr inbounds i8, i8* %bptr, i64 1
18  %bptr2 = getelementptr inbounds i8, i8* %bptr, i64 2
19  %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
20
21  ;; We should be able to merge these four stores with the i32 above
22  ; value (and bytes) stored before  ; 0x12345678
23  store i8 9, i8* %bptr              ;         09
24  store i8 10, i8* %bptr1            ;       0A
25  store i8 11, i8* %bptr2            ;     0B
26  store i8 12, i8* %bptr3            ;   0C
27  ;                                    0x0C0B0A09
28  ret void
29}
30
31define void @word_replacement(i64 *%ptr) {
32; CHECK-LABEL: @word_replacement(
33; CHECK-NEXT:  entry:
34; CHECK-NEXT:    store i64 8106482645252179720, i64* [[PTR:%.*]], align 8
35; CHECK-NEXT:    ret void
36;
37entry:
38  store i64 72623859790382856, i64* %ptr  ; 0x0102030405060708
39
40  %wptr = bitcast i64* %ptr to i16*
41  %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
42  %wptr3 = getelementptr inbounds i16, i16* %wptr, i64 3
43
44  ;; We should be able to merge these two stores with the i64 one above
45  ; value (not bytes) stored before  ; 0x0102030405060708
46  store i16  4128, i16* %wptr1       ;           1020
47  store i16 28800, i16* %wptr3       ;   7080
48  ;                                    0x7080030410200708
49  ret void
50}
51
52
53define void @differently_sized_replacements(i64 *%ptr) {
54; CHECK-LABEL: @differently_sized_replacements(
55; CHECK-NEXT:  entry:
56; CHECK-NEXT:    store i64 578437695752307201, i64* [[PTR:%.*]], align 8
57; CHECK-NEXT:    ret void
58;
59entry:
60  store i64 579005069656919567, i64* %ptr  ; 0x08090a0b0c0d0e0f
61
62  %bptr = bitcast i64* %ptr to i8*
63  %bptr6 = getelementptr inbounds i8, i8* %bptr, i64 6
64  %wptr = bitcast i64* %ptr to i16*
65  %wptr2 = getelementptr inbounds i16, i16* %wptr, i64 2
66  %dptr = bitcast i64* %ptr to i32*
67
68  ;; We should be able to merge all these stores with the i64 one above
69  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
70  store i8         7, i8*  %bptr6    ;     07
71  store i16     1541, i16* %wptr2    ;       0605
72  store i32 67305985, i32* %dptr     ;           04030201
73  ;                                    0x0807060504030201
74  ret void
75}
76
77
78define void @multiple_replacements_to_same_byte(i64 *%ptr) {
79; CHECK-LABEL: @multiple_replacements_to_same_byte(
80; CHECK-NEXT:  entry:
81; CHECK-NEXT:    store i64 579005069522043393, i64* [[PTR:%.*]], align 8
82; CHECK-NEXT:    ret void
83;
84entry:
85  store i64 579005069656919567, i64* %ptr  ; 0x08090a0b0c0d0e0f
86
87  %bptr = bitcast i64* %ptr to i8*
88  %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
89  %wptr = bitcast i64* %ptr to i16*
90  %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
91  %dptr = bitcast i64* %ptr to i32*
92
93  ;; We should be able to merge all these stores with the i64 one above
94  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
95  store i8         7, i8*  %bptr3    ;           07
96  store i16     1541, i16* %wptr1    ;           0605
97  store i32 67305985, i32* %dptr     ;           04030201
98  ;                                    0x08090a0b04030201
99  ret void
100}
101
102define void @merged_merges(i64 *%ptr) {
103; CHECK-LABEL: @merged_merges(
104; CHECK-NEXT:  entry:
105; CHECK-NEXT:    store i64 579005069572506113, i64* [[PTR:%.*]], align 8
106; CHECK-NEXT:    ret void
107;
108entry:
109  store i64 579005069656919567, i64* %ptr  ; 0x08090a0b0c0d0e0f
110
111  %bptr = bitcast i64* %ptr to i8*
112  %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
113  %wptr = bitcast i64* %ptr to i16*
114  %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
115  %dptr = bitcast i64* %ptr to i32*
116
117  ;; We should be able to merge all these stores with the i64 one above
118  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
119  store i32 67305985, i32* %dptr     ;           04030201
120  store i16     1541, i16* %wptr1    ;           0605
121  store i8         7, i8*  %bptr3    ;           07
122  ;                                    0x08090a0b07050201
123  ret void
124}
125
126define signext i8 @shouldnt_merge_since_theres_a_full_overlap(i64 *%ptr) {
127; CHECK-LABEL: @shouldnt_merge_since_theres_a_full_overlap(
128; CHECK-NEXT:  entry:
129; CHECK-NEXT:    [[BPTR:%.*]] = bitcast i64* [[PTR:%.*]] to i8*
130; CHECK-NEXT:    [[BPTRM1:%.*]] = getelementptr inbounds i8, i8* [[BPTR]], i64 -1
131; CHECK-NEXT:    [[BPTR3:%.*]] = getelementptr inbounds i8, i8* [[BPTR]], i64 3
132; CHECK-NEXT:    [[DPTR:%.*]] = bitcast i8* [[BPTRM1]] to i32*
133; CHECK-NEXT:    [[QPTR:%.*]] = bitcast i8* [[BPTR3]] to i64*
134; CHECK-NEXT:    store i32 1234, i32* [[DPTR]], align 1
135; CHECK-NEXT:    store i64 5678, i64* [[QPTR]], align 1
136; CHECK-NEXT:    ret i8 0
137;
138entry:
139
140  ; Also check that alias.scope metadata doesn't get dropped
141  store i64 0, i64* %ptr, !alias.scope !32
142
143  %bptr = bitcast i64* %ptr to i8*
144  %bptrm1 = getelementptr inbounds i8, i8* %bptr, i64 -1
145  %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
146  %dptr = bitcast i8* %bptrm1 to i32*
147  %qptr = bitcast i8* %bptr3 to i64*
148
149  store i32 1234, i32* %dptr, align 1
150  store i64 5678, i64* %qptr, align 1
151
152  ret i8 0
153}
154
155;; Test case from PR31777
156%union.U = type { i64 }
157
158define void @foo(%union.U* nocapture %u) {
159; CHECK-LABEL: @foo(
160; CHECK-NEXT:  entry:
161; CHECK-NEXT:    [[I:%.*]] = getelementptr inbounds [[UNION_U:%.*]], %union.U* [[U:%.*]], i64 0, i32 0
162; CHECK-NEXT:    store i64 42, i64* [[I]], align 8, !tbaa !0, !noalias !3, !nontemporal !4
163; CHECK-NEXT:    ret void
164;
165entry:
166  %i = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0
167  store i64 0, i64* %i, align 8, !dbg !22, !tbaa !26, !noalias !30, !nontemporal !29
168  %s = bitcast %union.U* %u to i16*
169  store i16 42, i16* %s, align 8
170  ret void
171}
172
173; Don't crash by operating on stale data if we merge (kill) the last 2 stores.
174
175define void @PR34074(i32* %x, i64* %y) {
176; CHECK-LABEL: @PR34074(
177; CHECK-NEXT:    store i64 42, i64* [[Y:%.*]], align 8
178; CHECK-NEXT:    store i32 4, i32* [[X:%.*]], align 4
179; CHECK-NEXT:    ret void
180;
181  store i64 42, i64* %y          ; independent store
182  %xbc = bitcast i32* %x to i8*
183  store i32 0, i32* %x           ; big store of constant
184  store i8 4, i8* %xbc           ; small store with mergeable constant
185  ret void
186}
187
188; We can't eliminate the last store because P and Q may alias.
189
190define void @PR36129(i32* %P, i32* %Q) {
191; CHECK-LABEL: @PR36129(
192; CHECK-NEXT:    store i32 1, i32* [[P:%.*]], align 4
193; CHECK-NEXT:    [[P2:%.*]] = bitcast i32* [[P]] to i8*
194; CHECK-NEXT:    store i32 2, i32* [[Q:%.*]], align 4
195; CHECK-NEXT:    store i8 3, i8* [[P2]], align 1
196; CHECK-NEXT:    ret void
197;
198  store i32 1, i32* %P
199  %P2 = bitcast i32* %P to i8*
200  store i32 2, i32* %Q
201  store i8 3, i8* %P2
202  ret void
203}
204
205!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 5.0.0 (trunk 306512)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
206!1 = !DIFile(filename: "me.cpp", directory: "/compiler-explorer")
207!2 = !{}
208!7 = distinct !DISubprogram(name: "foo", linkageName: "foo(U*)", scope: !1, file: !1, line: 9, type: !8, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !20)
209!8 = !DISubroutineType(types: !9)
210!9 = !{null, !10}
211!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
212!11 = distinct !DICompositeType(tag: DW_TAG_union_type, name: "U", file: !1, line: 4, size: 64, elements: !12, identifier: "typeinfo name for U")
213!12 = !{!13, !17}
214!13 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !11, file: !1, line: 5, baseType: !14, size: 64)
215!14 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint64_t", file: !15, line: 55, baseType: !16)
216!15 = !DIFile(filename: "/usr/include/stdint.h", directory: "/compiler-explorer")
217!16 = !DIBasicType(name: "long unsigned int", size: 64, encoding: DW_ATE_unsigned)
218!17 = !DIDerivedType(tag: DW_TAG_member, name: "s", scope: !11, file: !1, line: 6, baseType: !18, size: 16)
219!18 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint16_t", file: !15, line: 49, baseType: !19)
220!19 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
221!20 = !{!21}
222!21 = !DILocalVariable(name: "u", arg: 1, scope: !7, file: !1, line: 9, type: !10)
223!22 = !DILocation(line: 10, column: 8, scope: !7)
224
225!26 = !{!27, !27, i64 0}
226!27 = !{!"omnipotent char", !28, i64 0}
227!28 = !{!"Simple C++ TBAA"}
228
229!29 = !{i32 1}
230
231; Domains and scopes which might alias
232!30 = !{!30}
233!31 = !{!31, !30}
234
235!32 = !{!32}
236!33 = !{!33, !32}
237