1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals smart
2 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
4 target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128"
6 define i32 @test_load_cast_combine_tbaa(ptr %ptr) {
7 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves TBAA.
8 ; CHECK-LABEL: @test_load_cast_combine_tbaa(
10 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4, !tbaa [[TBAA0:![0-9]+]]
11 ; CHECK-NEXT: ret i32 [[L1]]
14 %l = load float, ptr %ptr, !tbaa !0
15 %c = bitcast float %l to i32
19 define i32 @test_load_cast_combine_noalias(ptr %ptr) {
20 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves no-alias metadata.
21 ; CHECK-LABEL: @test_load_cast_combine_noalias(
23 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4, !alias.scope [[META3:![0-9]+]], !noalias [[META3]]
24 ; CHECK-NEXT: ret i32 [[L1]]
27 %l = load float, ptr %ptr, !alias.scope !3, !noalias !3
28 %c = bitcast float %l to i32
32 define float @test_load_cast_combine_range(ptr %ptr) {
33 ; Ensure (cast (load (...))) -> (load (cast (...))) drops range metadata. It
34 ; would be nice to preserve or update it somehow but this is hard when moving
36 ; CHECK-LABEL: @test_load_cast_combine_range(
38 ; CHECK-NEXT: [[L1:%.*]] = load float, ptr [[PTR:%.*]], align 4
39 ; CHECK-NEXT: ret float [[L1]]
42 %l = load i32, ptr %ptr, !range !6
43 %c = bitcast i32 %l to float
47 define i32 @test_load_cast_combine_invariant(ptr %ptr) {
48 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves invariant metadata.
49 ; CHECK-LABEL: @test_load_cast_combine_invariant(
51 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4, !invariant.load [[META6:![0-9]+]]
52 ; CHECK-NEXT: ret i32 [[L1]]
55 %l = load float, ptr %ptr, !invariant.load !7
56 %c = bitcast float %l to i32
60 define i32 @test_load_cast_combine_nontemporal(ptr %ptr) {
61 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves nontemporal
63 ; CHECK-LABEL: @test_load_cast_combine_nontemporal(
65 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4, !nontemporal [[META7:![0-9]+]]
66 ; CHECK-NEXT: ret i32 [[L1]]
69 %l = load float, ptr %ptr, !nontemporal !8
70 %c = bitcast float %l to i32
74 define ptr @test_load_cast_combine_align(ptr %ptr) {
75 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves align
77 ; CHECK-LABEL: @test_load_cast_combine_align(
79 ; CHECK-NEXT: [[L:%.*]] = load ptr, ptr [[PTR:%.*]], align 8, !align [[META8:![0-9]+]]
80 ; CHECK-NEXT: ret ptr [[L]]
83 %l = load ptr, ptr %ptr, !align !9
87 define ptr @test_load_cast_combine_deref(ptr %ptr) {
88 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves dereferenceable
90 ; CHECK-LABEL: @test_load_cast_combine_deref(
92 ; CHECK-NEXT: [[L:%.*]] = load ptr, ptr [[PTR:%.*]], align 8, !dereferenceable [[META8]]
93 ; CHECK-NEXT: ret ptr [[L]]
96 %l = load ptr, ptr %ptr, !dereferenceable !9
100 define ptr @test_load_cast_combine_deref_or_null(ptr %ptr) {
101 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves
102 ; dereferenceable_or_null metadata.
103 ; CHECK-LABEL: @test_load_cast_combine_deref_or_null(
105 ; CHECK-NEXT: [[L:%.*]] = load ptr, ptr [[PTR:%.*]], align 8, !dereferenceable_or_null [[META8]]
106 ; CHECK-NEXT: ret ptr [[L]]
109 %l = load ptr, ptr %ptr, !dereferenceable_or_null !9
113 define void @test_load_cast_combine_loop(ptr %src, ptr %dst, i32 %n) {
114 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves loop access
116 ; CHECK-LABEL: @test_load_cast_combine_loop(
118 ; CHECK-NEXT: br label [[LOOP:%.*]]
120 ; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
121 ; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[I]] to i64
122 ; CHECK-NEXT: [[SRC_GEP:%.*]] = getelementptr inbounds float, ptr [[SRC:%.*]], i64 [[TMP0]]
123 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[I]] to i64
124 ; CHECK-NEXT: [[DST_GEP:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[TMP1]]
125 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[SRC_GEP]], align 4, !llvm.access.group [[ACC_GRP9:![0-9]+]]
126 ; CHECK-NEXT: store i32 [[L1]], ptr [[DST_GEP]], align 4
127 ; CHECK-NEXT: [[I_NEXT]] = add i32 [[I]], 1
128 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[I_NEXT]], [[N:%.*]]
129 ; CHECK-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]], !llvm.loop [[LOOP1:![0-9]+]]
131 ; CHECK-NEXT: ret void
137 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
138 %src.gep = getelementptr inbounds float, ptr %src, i32 %i
139 %dst.gep = getelementptr inbounds i32, ptr %dst, i32 %i
140 %l = load float, ptr %src.gep, !llvm.access.group !10
141 %c = bitcast float %l to i32
142 store i32 %c, ptr %dst.gep
143 %i.next = add i32 %i, 1
144 %cmp = icmp slt i32 %i.next, %n
145 br i1 %cmp, label %loop, label %exit, !llvm.loop !1
151 define void @test_load_cast_combine_nonnull(ptr %ptr) {
152 ; CHECK-LABEL: @test_load_cast_combine_nonnull(
154 ; CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[PTR:%.*]], align 8, !nonnull [[META6]]
155 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[PTR]], i64 336
156 ; CHECK-NEXT: store ptr [[P]], ptr [[GEP]], align 8
157 ; CHECK-NEXT: ret void
160 %p = load ptr, ptr %ptr, !nonnull !{}
161 %gep = getelementptr ptr, ptr %ptr, i32 42
162 store ptr %p, ptr %gep
166 define i32 @test_load_cast_combine_noundef(ptr %ptr) {
167 ; CHECK-LABEL: @test_load_cast_combine_noundef(
168 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4, !noundef [[META6]]
169 ; CHECK-NEXT: ret i32 [[L1]]
171 %l = load float, ptr %ptr, !noundef !{}
172 %c = bitcast float %l to i32
176 define i32 @test_load_cast_combine_noalias_addrspace(ptr %ptr) {
177 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves TBAA.
178 ; CHECK-LABEL: @test_load_cast_combine_noalias_addrspace(
180 ; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[PTR:%.*]], align 4
181 ; CHECK-NEXT: ret i32 [[L1]]
184 %l = load float, ptr %ptr, align 4, !noalias.addrspace !11
185 %c = bitcast float %l to i32
189 ; FIXME: Should preserve metadata on loads, except !noundef and !invariant.load.
190 define ptr @preserve_load_metadata_after_select_transform1(i1 %c, ptr dereferenceable(8) %a, ptr dereferenceable(8) %b) {
191 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform1(
193 ; CHECK-NEXT: [[B_VAL:%.*]] = load ptr, ptr [[B:%.*]], align 1
194 ; CHECK-NEXT: [[A_VAL:%.*]] = load ptr, ptr [[A:%.*]], align 1
195 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[C:%.*]], ptr [[B_VAL]], ptr [[A_VAL]]
196 ; CHECK-NEXT: ret ptr [[L_SEL]]
199 %ptr.sel = select i1 %c, ptr %b, ptr %a
200 %l.sel = load ptr, ptr %ptr.sel, align 1, !tbaa !0, !llvm.access.group !7, !dereferenceable !9, !noundef !{}, !invariant.load !7
204 define double @preserve_load_metadata_after_select_transform2(ptr %a, ptr %b) {
205 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform2(
207 ; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
208 ; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
209 ; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
210 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
211 ; CHECK-NEXT: ret double [[L_SEL]]
214 %l.a = load double, ptr %a, align 8, !tbaa !0, !llvm.access.group !7
215 %l.b = load double, ptr %b, align 8, !tbaa !0, !llvm.access.group !7
216 %cmp.i = fcmp fast olt double %l.a, %l.b
217 %ptr.sel = select i1 %cmp.i, ptr %b, ptr %a
218 %l.sel = load double, ptr %ptr.sel, align 8, !tbaa !0, !llvm.access.group !7
222 define double @preserve_load_metadata_after_select_transform_metadata_missing_1(ptr %a, ptr %b) {
223 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform_metadata_missing_1(
225 ; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !llvm.access.group [[META6]]
226 ; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
227 ; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
228 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
229 ; CHECK-NEXT: ret double [[L_SEL]]
232 %l.a = load double, ptr %a, align 8, !llvm.access.group !7
233 %l.b = load double, ptr %b, align 8, !tbaa !0, !llvm.access.group !7
234 %cmp.i = fcmp fast olt double %l.a, %l.b
235 %ptr.sel = select i1 %cmp.i, ptr %b, ptr %a
236 %l.sel = load double, ptr %ptr.sel, align 8, !tbaa !0, !llvm.access.group !7
240 define double @preserve_load_metadata_after_select_transform_metadata_missing_2(ptr %a, ptr %b) {
241 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform_metadata_missing_2(
243 ; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !llvm.access.group [[META6]]
244 ; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !llvm.access.group [[META6]]
245 ; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
246 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
247 ; CHECK-NEXT: ret double [[L_SEL]]
250 %l.a = load double, ptr %a, align 8, !llvm.access.group !7
251 %l.b = load double, ptr %b, align 8, !llvm.access.group !7
252 %cmp.i = fcmp fast olt double %l.a, %l.b
253 %ptr.sel = select i1 %cmp.i, ptr %b, ptr %a
254 %l.sel = load double, ptr %ptr.sel, align 8, !tbaa !0, !llvm.access.group !12
258 define double @preserve_load_metadata_after_select_transform_metadata_missing_3(ptr %a, ptr %b) {
259 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform_metadata_missing_3(
261 ; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
262 ; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
263 ; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
264 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
265 ; CHECK-NEXT: ret double [[L_SEL]]
268 %l.a = load double, ptr %a, align 8, !tbaa !0, !llvm.access.group !7
269 %l.b = load double, ptr %b, align 8, !tbaa !0, !llvm.access.group !7
270 %cmp.i = fcmp fast olt double %l.a, %l.b
271 %ptr.sel = select i1 %cmp.i, ptr %b, ptr %a
272 %l.sel = load double, ptr %ptr.sel, align 8, !tbaa !0, !llvm.access.group !13
276 ; Like preserve_load_metadata_after_select_transform_metadata_missing_3, but
277 ; with different access groups on all loads.
278 define double @preserve_load_metadata_after_select_transform_metadata_missing_4(ptr %a, ptr %b) {
279 ; CHECK-LABEL: @preserve_load_metadata_after_select_transform_metadata_missing_4(
281 ; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[META6]]
282 ; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !llvm.access.group [[ACC_GRP10:![0-9]+]]
283 ; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
284 ; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
285 ; CHECK-NEXT: ret double [[L_SEL]]
288 %l.a = load double, ptr %a, align 8, !tbaa !0, !llvm.access.group !7
289 %l.b = load double, ptr %b, align 8, !tbaa !0, !llvm.access.group !12
290 %cmp.i = fcmp fast olt double %l.a, %l.b
291 %ptr.sel = select i1 %cmp.i, ptr %b, ptr %a
292 %l.sel = load double, ptr %ptr.sel, align 8, !tbaa !0, !llvm.access.group !13
296 !0 = !{!1, !1, i64 0}
297 !1 = !{!"scalar type", !2}
300 !4 = distinct !{!4, !5}
302 !6 = !{i32 0, i32 42}
307 !11 = !{i32 5, i32 6}
311 ; CHECK: [[TBAA0]] = !{[[LOOP1]], [[LOOP1]], i64 0}
312 ; CHECK: [[LOOP1]] = !{!"scalar type", [[META2:![0-9]+]]}
313 ; CHECK: [[META2]] = !{!"root"}
314 ; CHECK: [[META3]] = !{[[META4:![0-9]+]]}
315 ; CHECK: [[META4]] = distinct !{[[META4]], [[META5:![0-9]+]]}
316 ; CHECK: [[META5]] = distinct !{[[META5]]}
317 ; CHECK: [[META6]] = !{}
318 ; CHECK: [[META7]] = !{i32 1}
319 ; CHECK: [[META8]] = !{i64 8}
320 ; CHECK: [[ACC_GRP9]] = distinct !{}
321 ; CHECK: [[ACC_GRP10]] = distinct !{}