1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -dse -enable-dse-partial-store-merging -S < %s | FileCheck %s
3 target datalayout = "E-m:e-i64:64-i128:128-n32:64-S128"
5 define void @byte_by_byte_replacement(i32 *%ptr) {
6 ; CHECK-LABEL: @byte_by_byte_replacement(
8 ; CHECK-NEXT: store i32 151653132, i32* [[PTR:%.*]]
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 0x090A0B0C == 151653132
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
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
32 define void @word_replacement(i64 *%ptr) {
33 ; CHECK-LABEL: @word_replacement(
35 ; CHECK-NEXT: store i64 72638273700655232, i64* [[PTR:%.*]]
36 ; CHECK-NEXT: ret void
39 store i64 72623859790382856, i64* %ptr ; 0x0102030405060708
41 %wptr = bitcast i64* %ptr to i16*
42 %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
43 %wptr2 = getelementptr inbounds i16, i16* %wptr, i64 2
44 %wptr3 = getelementptr inbounds i16, i16* %wptr, i64 3
46 ;; We should be able to merge these two stores with the i64 one above
47 ; value (and bytes) stored before ; 0x0102030405060708
48 store i16 4128, i16* %wptr1 ; 1020
49 store i16 28800, i16* %wptr3 ; 7080
56 define void @differently_sized_replacements(i64 *%ptr) {
57 ; CHECK-LABEL: @differently_sized_replacements(
59 ; CHECK-NEXT: store i64 289077004501059343, i64* [[PTR:%.*]]
60 ; CHECK-NEXT: ret void
63 store i64 579005069656919567, i64* %ptr ; 0x08090a0b0c0d0e0f
65 %bptr = bitcast i64* %ptr to i8*
66 %bptr6 = getelementptr inbounds i8, i8* %bptr, i64 6
67 %wptr = bitcast i64* %ptr to i16*
68 %wptr2 = getelementptr inbounds i16, i16* %wptr, i64 2
69 %dptr = bitcast i64* %ptr to i32*
71 ;; We should be able to merge all these stores with the i64 one above
72 ; value (and bytes) stored before ; 0x08090a0b0c0d0e0f
73 store i8 7, i8* %bptr6 ; 07
74 store i16 1541, i16* %wptr2 ; 0605
75 store i32 67305985, i32* %dptr ; 04030201
81 define void @multiple_replacements_to_same_byte(i64 *%ptr) {
82 ; CHECK-LABEL: @multiple_replacements_to_same_byte(
84 ; CHECK-NEXT: store i64 289077004602248719, i64* [[PTR:%.*]]
85 ; CHECK-NEXT: ret void
88 store i64 579005069656919567, i64* %ptr ; 0x08090a0b0c0d0e0f
90 %bptr = bitcast i64* %ptr to i8*
91 %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
92 %wptr = bitcast i64* %ptr to i16*
93 %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
94 %dptr = bitcast i64* %ptr to i32*
96 ;; We should be able to merge all these stores with the i64 one above
97 ; value (and bytes) stored before ; 0x08090a0b0c0d0e0f
98 store i8 7, i8* %bptr3 ; 07
99 store i16 1541, i16* %wptr1 ; 0605
100 store i32 67305985, i32* %dptr ; 04030201
105 define void @merged_merges(i64 *%ptr) {
106 ; CHECK-LABEL: @merged_merges(
108 ; CHECK-NEXT: store i64 289081428418563599, i64* [[PTR:%.*]]
109 ; CHECK-NEXT: ret void
112 store i64 579005069656919567, i64* %ptr ; 0x08090a0b0c0d0e0f
114 %bptr = bitcast i64* %ptr to i8*
115 %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
116 %wptr = bitcast i64* %ptr to i16*
117 %wptr1 = getelementptr inbounds i16, i16* %wptr, i64 1
118 %dptr = bitcast i64* %ptr to i32*
120 ;; We should be able to merge all these stores with the i64 one above
121 ; value (not bytes) stored before ; 0x08090a0b0c0d0e0f
122 store i32 67305985, i32* %dptr ; 04030201
123 store i16 1541, i16* %wptr1 ; 0605
124 store i8 7, i8* %bptr3 ; 07
129 define signext i8 @shouldnt_merge_since_theres_a_full_overlap(i64 *%ptr) {
130 ; CHECK-LABEL: @shouldnt_merge_since_theres_a_full_overlap(
132 ; CHECK-NEXT: [[BPTR:%.*]] = bitcast i64* [[PTR:%.*]] to i8*
133 ; CHECK-NEXT: [[BPTRM1:%.*]] = getelementptr inbounds i8, i8* [[BPTR]], i64 -1
134 ; CHECK-NEXT: [[BPTR3:%.*]] = getelementptr inbounds i8, i8* [[BPTR]], i64 3
135 ; CHECK-NEXT: [[DPTR:%.*]] = bitcast i8* [[BPTRM1]] to i32*
136 ; CHECK-NEXT: [[QPTR:%.*]] = bitcast i8* [[BPTR3]] to i64*
137 ; CHECK-NEXT: store i32 1234, i32* [[DPTR]], align 1
138 ; CHECK-NEXT: store i64 5678, i64* [[QPTR]], align 1
139 ; CHECK-NEXT: ret i8 0
143 store i64 0, i64* %ptr
145 %bptr = bitcast i64* %ptr to i8*
146 %bptrm1 = getelementptr inbounds i8, i8* %bptr, i64 -1
147 %bptr3 = getelementptr inbounds i8, i8* %bptr, i64 3
148 %dptr = bitcast i8* %bptrm1 to i32*
149 %qptr = bitcast i8* %bptr3 to i64*
151 store i32 1234, i32* %dptr, align 1
152 store i64 5678, i64* %qptr, align 1
157 ;; Test case from PR31777
158 %union.U = type { i64 }
160 define void @foo(%union.U* nocapture %u) {
163 ; CHECK-NEXT: [[I:%.*]] = getelementptr inbounds [[UNION_U:%.*]], %union.U* [[U:%.*]], i64 0, i32 0
164 ; CHECK-NEXT: store i64 11821949021847552, i64* [[I]], align 8
165 ; CHECK-NEXT: ret void
168 %i = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0
169 store i64 0, i64* %i, align 8
170 %s = bitcast %union.U* %u to i16*
171 store i16 42, i16* %s, align 8