1 ; RUN: opt -instcombine -S < %s | FileCheck %s
3 target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128"
5 define i32 @test_load_cast_combine_tbaa(float* %ptr) {
6 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves TBAA.
7 ; CHECK-LABEL: @test_load_cast_combine_tbaa(
8 ; CHECK: load i32, i32* %{{.*}}, !tbaa !0
10 %l = load float, float* %ptr, !tbaa !0
11 %c = bitcast float %l to i32
15 define i32 @test_load_cast_combine_noalias(float* %ptr) {
16 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves no-alias metadata.
17 ; CHECK-LABEL: @test_load_cast_combine_noalias(
18 ; CHECK: load i32, i32* %{{.*}}, !alias.scope !3, !noalias !4
20 %l = load float, float* %ptr, !alias.scope !3, !noalias !4
21 %c = bitcast float %l to i32
25 define float @test_load_cast_combine_range(i32* %ptr) {
26 ; Ensure (cast (load (...))) -> (load (cast (...))) drops range metadata. It
27 ; would be nice to preserve or update it somehow but this is hard when moving
29 ; CHECK-LABEL: @test_load_cast_combine_range(
30 ; CHECK: load float, float* %{{.*}}
34 %l = load i32, i32* %ptr, !range !5
35 %c = bitcast i32 %l to float
39 define i32 @test_load_cast_combine_invariant(float* %ptr) {
40 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves invariant metadata.
41 ; CHECK-LABEL: @test_load_cast_combine_invariant(
42 ; CHECK: load i32, i32* %{{.*}}, !invariant.load !7
44 %l = load float, float* %ptr, !invariant.load !6
45 %c = bitcast float %l to i32
49 define i32 @test_load_cast_combine_nontemporal(float* %ptr) {
50 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves nontemporal
52 ; CHECK-LABEL: @test_load_cast_combine_nontemporal(
53 ; CHECK: load i32, i32* %{{.*}}, !nontemporal !8
55 %l = load float, float* %ptr, !nontemporal !7
56 %c = bitcast float %l to i32
60 define i8* @test_load_cast_combine_align(i32** %ptr) {
61 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves align
63 ; CHECK-LABEL: @test_load_cast_combine_align(
64 ; CHECK: load i8*, i8** %{{.*}}, !align !9
66 %l = load i32*, i32** %ptr, !align !8
67 %c = bitcast i32* %l to i8*
71 define i8* @test_load_cast_combine_deref(i32** %ptr) {
72 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves dereferenceable
74 ; CHECK-LABEL: @test_load_cast_combine_deref(
75 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable !9
77 %l = load i32*, i32** %ptr, !dereferenceable !8
78 %c = bitcast i32* %l to i8*
82 define i8* @test_load_cast_combine_deref_or_null(i32** %ptr) {
83 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves
84 ; dereferenceable_or_null metadata.
85 ; CHECK-LABEL: @test_load_cast_combine_deref_or_null(
86 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable_or_null !9
88 %l = load i32*, i32** %ptr, !dereferenceable_or_null !8
89 %c = bitcast i32* %l to i8*
93 define void @test_load_cast_combine_loop(float* %src, i32* %dst, i32 %n) {
94 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves loop access
96 ; CHECK-LABEL: @test_load_cast_combine_loop(
97 ; CHECK: load i32, i32* %{{.*}}, !llvm.access.group !6
102 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
103 %src.gep = getelementptr inbounds float, float* %src, i32 %i
104 %dst.gep = getelementptr inbounds i32, i32* %dst, i32 %i
105 %l = load float, float* %src.gep, !llvm.access.group !9
106 %c = bitcast float %l to i32
107 store i32 %c, i32* %dst.gep
108 %i.next = add i32 %i, 1
109 %cmp = icmp slt i32 %i.next, %n
110 br i1 %cmp, label %loop, label %exit, !llvm.loop !1
116 define void @test_load_cast_combine_nonnull(float** %ptr) {
117 ; We can't preserve nonnull metadata when converting a load of a pointer to
118 ; a load of an integer. Instead, we translate it to range metadata.
119 ; FIXME: We should also transform range metadata back into nonnull metadata.
120 ; FIXME: This test is very fragile. If any LABEL lines are added after
121 ; this point, the test will fail, because this test depends on a metadata tuple,
122 ; which is always emitted at the end of the file. At some point, we should
123 ; consider an option to the IR printer to emit MD tuples after the function
124 ; that first uses them--this will allow us to refer to them like this and not
125 ; have the tests break. For now, this function must always come last in this
126 ; file, and no LABEL lines are to be added after this point.
128 ; CHECK-LABEL: @test_load_cast_combine_nonnull(
129 ; CHECK: %[[V:.*]] = load i64, i64* %{{.*}}, !range ![[MD:[0-9]+]]
130 ; CHECK-NOT: !nonnull
131 ; CHECK: store i64 %[[V]], i64*
133 %p = load float*, float** %ptr, !nonnull !6
134 %gep = getelementptr float*, float** %ptr, i32 42
135 store float* %p, float** %gep
139 ; This is the metadata tuple that we reference above:
140 ; CHECK: ![[MD]] = !{i64 1, i64 0}
141 !0 = !{!1, !1, i64 0}
142 !1 = !{!"scalar type", !2}
144 !3 = distinct !{!3, !4}
145 !4 = distinct !{!4, !{!"llvm.loop.parallel_accesses", !9}}
146 !5 = !{i32 0, i32 42}