xref: /llvm-project/llvm/test/Transforms/DeadStoreElimination/merge-stores.ll (revision f497a00da968b0ff90d8c98caa184d14b9a92495)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -passes=dse -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(ptr %ptr) {
6; CHECK-LABEL: @byte_by_byte_replacement(
7; CHECK-NEXT:  entry:
8; CHECK-NEXT:    store i32 202050057, ptr [[PTR:%.*]]
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, ptr %ptr  ; 0x12345678
16  %bptr1 = getelementptr inbounds i8, ptr %ptr, i64 1
17  %bptr2 = getelementptr inbounds i8, ptr %ptr, i64 2
18  %bptr3 = getelementptr inbounds i8, ptr %ptr, i64 3
19
20  ;; We should be able to merge these four stores with the i32 above
21  ; value (and bytes) stored before  ; 0x12345678
22  store i8 9, ptr %ptr              ;         09
23  store i8 10, ptr %bptr1            ;       0A
24  store i8 11, ptr %bptr2            ;     0B
25  store i8 12, ptr %bptr3            ;   0C
26  ;                                    0x0C0B0A09
27  ret void
28}
29
30define void @word_replacement(ptr %ptr) {
31; CHECK-LABEL: @word_replacement(
32; CHECK-NEXT:  entry:
33; CHECK-NEXT:    store i64 8106482645252179720, ptr [[PTR:%.*]]
34; CHECK-NEXT:    ret void
35;
36entry:
37  store i64 72623859790382856, ptr %ptr  ; 0x0102030405060708
38
39  %wptr1 = getelementptr inbounds i16, ptr %ptr, i64 1
40  %wptr3 = getelementptr inbounds i16, ptr %ptr, i64 3
41
42  ;; We should be able to merge these two stores with the i64 one above
43  ; value (not bytes) stored before  ; 0x0102030405060708
44  store i16  4128, ptr %wptr1       ;           1020
45  store i16 28800, ptr %wptr3       ;   7080
46  ;                                    0x7080030410200708
47  ret void
48}
49
50
51define void @differently_sized_replacements(ptr %ptr) {
52; CHECK-LABEL: @differently_sized_replacements(
53; CHECK-NEXT:  entry:
54; CHECK-NEXT:    store i64 578437695752307201, ptr [[PTR:%.*]]
55; CHECK-NEXT:    ret void
56;
57entry:
58  store i64 579005069656919567, ptr %ptr  ; 0x08090a0b0c0d0e0f
59
60  %bptr6 = getelementptr inbounds i8, ptr %ptr, i64 6
61  %wptr2 = getelementptr inbounds i16, ptr %ptr, i64 2
62
63  ;; We should be able to merge all these stores with the i64 one above
64  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
65  store i8         7, ptr  %bptr6    ;     07
66  store i16     1541, ptr %wptr2    ;       0605
67  store i32 67305985, ptr %ptr     ;           04030201
68  ;                                    0x0807060504030201
69  ret void
70}
71
72
73define void @multiple_replacements_to_same_byte(ptr %ptr) {
74; CHECK-LABEL: @multiple_replacements_to_same_byte(
75; CHECK-NEXT:  entry:
76; CHECK-NEXT:    store i64 579005069522043393, ptr [[PTR:%.*]]
77; CHECK-NEXT:    ret void
78;
79entry:
80  store i64 579005069656919567, ptr %ptr  ; 0x08090a0b0c0d0e0f
81
82  %bptr3 = getelementptr inbounds i8, ptr %ptr, i64 3
83  %wptr1 = getelementptr inbounds i16, ptr %ptr, i64 1
84
85  ;; We should be able to merge all these stores with the i64 one above
86  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
87  store i8         7, ptr  %bptr3    ;           07
88  store i16     1541, ptr %wptr1    ;           0605
89  store i32 67305985, ptr %ptr     ;           04030201
90  ;                                    0x08090a0b04030201
91  ret void
92}
93
94define void @merged_merges(ptr %ptr) {
95; CHECK-LABEL: @merged_merges(
96; CHECK-NEXT:  entry:
97; CHECK-NEXT:    store i64 579005069572506113, ptr [[PTR:%.*]]
98; CHECK-NEXT:    ret void
99;
100entry:
101  store i64 579005069656919567, ptr %ptr  ; 0x08090a0b0c0d0e0f
102
103  %bptr3 = getelementptr inbounds i8, ptr %ptr, i64 3
104  %wptr1 = getelementptr inbounds i16, ptr %ptr, i64 1
105
106  ;; We should be able to merge all these stores with the i64 one above
107  ; value (not bytes) stored before  ; 0x08090a0b0c0d0e0f
108  store i32 67305985, ptr %ptr     ;           04030201
109  store i16     1541, ptr %wptr1    ;           0605
110  store i8         7, ptr  %bptr3    ;           07
111  ;                                    0x08090a0b07050201
112  ret void
113}
114
115define signext i8 @shouldnt_merge_since_theres_a_full_overlap(ptr %ptr) {
116; CHECK-LABEL: @shouldnt_merge_since_theres_a_full_overlap(
117; CHECK-NEXT:  entry:
118; CHECK-NEXT:    [[BPTRM1:%.*]] = getelementptr inbounds i8, ptr [[PTR:%.*]], i64 -1
119; CHECK-NEXT:    [[BPTR3:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 3
120; CHECK-NEXT:    store i32 1234, ptr [[BPTRM1]], align 1
121; CHECK-NEXT:    store i64 5678, ptr [[BPTR3]], align 1
122; CHECK-NEXT:    ret i8 0
123;
124entry:
125
126  ; Also check that alias.scope metadata doesn't get dropped
127  store i64 0, ptr %ptr, !alias.scope !32
128
129  %bptrm1 = getelementptr inbounds i8, ptr %ptr, i64 -1
130  %bptr3 = getelementptr inbounds i8, ptr %ptr, i64 3
131
132  store i32 1234, ptr %bptrm1, align 1
133  store i64 5678, ptr %bptr3, align 1
134
135  ret i8 0
136}
137
138;; Test case from PR31777
139%union.U = type { i64 }
140
141define void @foo(ptr nocapture %u) {
142; CHECK-LABEL: @foo(
143; CHECK-NEXT:  entry:
144; CHECK-NEXT:    store i64 42, ptr [[U:%.*]], align 8
145; CHECK-NEXT:    ret void
146;
147entry:
148  store i64 0, ptr %u, align 8, !dbg !22, !tbaa !26, !noalias !32, !nontemporal !29
149  store i16 42, ptr %u, align 8
150  ret void
151}
152
153; Don't crash by operating on stale data if we merge (kill) the last 2 stores.
154
155define void @PR34074(ptr %x, ptr %y) {
156; CHECK-LABEL: @PR34074(
157; CHECK-NEXT:    store i64 42, ptr %y
158; CHECK-NEXT:    store i32 4, ptr %x
159; CHECK-NEXT:    ret void
160;
161  store i64 42, ptr %y          ; independent store
162  store i32 0, ptr %x           ; big store of constant
163  store i8 4, ptr %x           ; small store with mergeable constant
164  ret void
165}
166
167; We can't eliminate the last store because P and Q may alias.
168
169define void @PR36129(ptr %P, ptr %Q) {
170; CHECK-LABEL: @PR36129(
171; CHECK-NEXT:    store i32 1, ptr [[P:%.*]], align 4
172; CHECK-NEXT:    store i32 2, ptr [[Q:%.*]], align 4
173; CHECK-NEXT:    store i8 3, ptr [[P]], align 1
174; CHECK-NEXT:    ret void
175;
176  store i32 1, ptr %P, align 4
177  store i32 2, ptr %Q, align 4
178  store i8 3, ptr %P, align 1
179  ret void
180}
181
182!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)
183!1 = !DIFile(filename: "me.cpp", directory: "/compiler-explorer")
184!2 = !{}
185!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)
186!8 = !DISubroutineType(types: !9)
187!9 = !{null, !10}
188!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
189!11 = distinct !DICompositeType(tag: DW_TAG_union_type, name: "U", file: !1, line: 4, size: 64, elements: !12, identifier: "typeinfo name for U")
190!12 = !{!13, !17}
191!13 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !11, file: !1, line: 5, baseType: !14, size: 64)
192!14 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint64_t", file: !15, line: 55, baseType: !16)
193!15 = !DIFile(filename: "/usr/include/stdint.h", directory: "/compiler-explorer")
194!16 = !DIBasicType(name: "long unsigned int", size: 64, encoding: DW_ATE_unsigned)
195!17 = !DIDerivedType(tag: DW_TAG_member, name: "s", scope: !11, file: !1, line: 6, baseType: !18, size: 16)
196!18 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint16_t", file: !15, line: 49, baseType: !19)
197!19 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
198!20 = !{!21}
199!21 = !DILocalVariable(name: "u", arg: 1, scope: !7, file: !1, line: 9, type: !10)
200!22 = !DILocation(line: 10, column: 8, scope: !7)
201
202!26 = !{!27, !27, i64 0}
203!27 = !{!"omnipotent char", !28, i64 0}
204!28 = !{!"Simple C++ TBAA"}
205
206!29 = !{i32 1}
207
208; Domains and scopes which might alias
209!30 = !{!30}
210!31 = !{!31, !30}
211!32 = !{!31}
212