1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -gvn -S | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1"
5 target triple = "x86_64-unknown-linux-gnu"
7 declare void @llvm.memset.p0i8.i8(i8*, i8, i32, i1)
10 define i8 @test(i1 %cmp) {
13 ; CHECK-NEXT: [[P:%.*]] = alloca i8
14 ; CHECK-NEXT: store i8 5, i8* [[P]]
15 ; CHECK-NEXT: br label [[HEADER:%.*]]
17 ; CHECK-NEXT: [[V:%.*]] = phi i8 [ 5, [[ENTRY:%.*]] ], [ -5, [[ALIVE:%.*]] ]
18 ; CHECK-NEXT: [[I:%.*]] = phi i8 [ 0, [[ENTRY]] ], [ [[I_INC:%.*]], [[ALIVE]] ]
19 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[ALIVE]], label [[DEAD:%.*]]
21 ; CHECK-NEXT: call void @foo(i8* [[P]])
22 ; CHECK-NEXT: [[I_1:%.*]] = add i8 [[I]], [[V]]
23 ; CHECK-NEXT: br label [[ALIVE]]
25 ; CHECK-NEXT: [[I_2:%.*]] = phi i8 [ [[I]], [[HEADER]] ], [ [[I_1]], [[DEAD]] ]
26 ; CHECK-NEXT: store i8 -5, i8* [[P]]
27 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[P]], i8 0, i32 1, i1 false)
28 ; CHECK-NEXT: [[I_INC]] = add i8 [[I_2]], 1
29 ; CHECK-NEXT: [[CMP_LOOP:%.*]] = icmp ugt i8 [[I_INC]], 100
30 ; CHECK-NEXT: br i1 [[CMP_LOOP]], label [[EXIT:%.*]], label [[HEADER]]
32 ; CHECK-NEXT: ret i8 0
37 %addr = getelementptr inbounds i8, i8* %p, i64 0
41 %i = phi i8 [0, %entry], [%i.inc, %backedge]
42 br i1 %cmp, label %alive, label %dead
44 call void @foo(i8* %p)
45 %v = load i8, i8* %addr, !invariant.load !1
49 %i.2 = phi i8 [%i, %header], [%i.1, %dead]
50 store i8 -5, i8* %addr
53 call void @llvm.memset.p0i8.i8(i8 * align 1 %p, i8 0, i32 1, i1 false)
54 %i.inc = add i8 %i.2, 1
55 %cmp.loop = icmp ugt i8 %i.inc, 100
56 br i1 %cmp.loop, label %exit, label %header
58 %res = load i8, i8* %addr
62 ; Check that first two loads are not optimized out while the one marked with
63 ; invariant.load reuses %res1
64 define i8 @test2(i1 %cmp, i8 *%p) {
65 ; CHECK-LABEL: @test2(
67 ; CHECK-NEXT: [[RES1:%.*]] = load i8, i8* [[P:%.*]], align 1
68 ; CHECK-NEXT: call void @foo(i8* [[P]])
69 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[B2:%.*]], label [[B1:%.*]]
71 ; CHECK-NEXT: [[RES2:%.*]] = load i8, i8* [[P]]
72 ; CHECK-NEXT: [[RES3:%.*]] = add i8 [[RES1]], [[RES2]]
73 ; CHECK-NEXT: br label [[ALIVE:%.*]]
75 ; CHECK-NEXT: [[RES_DEAD:%.*]] = add i8 [[RES1]], [[RES1]]
76 ; CHECK-NEXT: br label [[ALIVE]]
78 ; CHECK-NEXT: [[RES_PHI:%.*]] = phi i8 [ [[RES3]], [[B1]] ], [ [[RES_DEAD]], [[B2]] ]
79 ; CHECK-NEXT: ret i8 [[RES_PHI]]
83 %res1 = load i8, i8* %p
84 call void @foo(i8 *%p)
85 br i1 %cmp, label %b2, label %b1
87 %res2 = load i8, i8* %p
88 %res3 = add i8 %res1, %res2
91 %v = load i8, i8* %p, !invariant.load !1
92 %res.dead = add i8 %v, %res1
95 %res.phi = phi i8 [%res3, %b1], [%res.dead, %b2]
99 ; This is essentially the same test case as the above one but with %b1 and %b2
100 ; swapped in "br i1 %cmp, label %b1, label %b2" instruction. That helps us to
101 ; ensure that results doesn't depend on visiting order.
102 define i8 @test3(i1 %cmp, i8 *%p) {
103 ; CHECK-LABEL: @test3(
105 ; CHECK-NEXT: [[RES1:%.*]] = load i8, i8* [[P:%.*]], align 1
106 ; CHECK-NEXT: call void @foo(i8* [[P]])
107 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[B1:%.*]], label [[B2:%.*]]
109 ; CHECK-NEXT: [[RES2:%.*]] = load i8, i8* [[P]]
110 ; CHECK-NEXT: [[RES3:%.*]] = add i8 [[RES1]], [[RES2]]
111 ; CHECK-NEXT: br label [[ALIVE:%.*]]
113 ; CHECK-NEXT: [[RES_DEAD:%.*]] = add i8 [[RES1]], [[RES1]]
114 ; CHECK-NEXT: br label [[ALIVE]]
116 ; CHECK-NEXT: [[RES_PHI:%.*]] = phi i8 [ [[RES3]], [[B1]] ], [ [[RES_DEAD]], [[B2]] ]
117 ; CHECK-NEXT: ret i8 [[RES_PHI]]
120 %res1 = load i8, i8* %p
121 call void @foo(i8 *%p)
122 br i1 %cmp, label %b1, label %b2
124 %res2 = load i8, i8* %p
125 %res3 = add i8 %res1, %res2
128 %v = load i8, i8* %p, !invariant.load !1
129 %res.dead = add i8 %v, %res1
132 %res.phi = phi i8 [%res3, %b1], [%res.dead, %b2]
137 ; This is reduced test case catching regression in the first version of the
138 ; fix for invariant loads (https://reviews.llvm.org/D64405).
139 define void @test4() {
140 ; CHECK-LABEL: @test4(
142 ; CHECK-NEXT: [[TMP0:%.*]] = load float, float* inttoptr (i64 8 to float*), align 4
143 ; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[TMP0]], [[TMP0]]
144 ; CHECK-NEXT: br label [[FUSION_LOOP_HEADER_DIM_1_PREHEADER:%.*]]
145 ; CHECK: fusion.loop_header.dim.1.preheader:
146 ; CHECK-NEXT: [[TMP2:%.*]] = phi float [ [[TMP0]], [[ENTRY:%.*]] ], [ [[DOTPRE:%.*]], [[FUSION_LOOP_HEADER_DIM_1_PREHEADER]] ]
147 ; CHECK-NEXT: [[FUSION_INVAR_ADDRESS_DIM_0_03:%.*]] = phi i64 [ 0, [[ENTRY]] ], [ [[INVAR_INC3:%.*]], [[FUSION_LOOP_HEADER_DIM_1_PREHEADER]] ]
148 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds [2 x [1 x [4 x float]]], [2 x [1 x [4 x float]]]* null, i64 0, i64 [[FUSION_INVAR_ADDRESS_DIM_0_03]], i64 0, i64 2
149 ; CHECK-NEXT: [[TMP4:%.*]] = fmul float [[TMP2]], [[TMP2]]
150 ; CHECK-NEXT: [[INVAR_INC3]] = add nuw nsw i64 [[FUSION_INVAR_ADDRESS_DIM_0_03]], 1
151 ; CHECK-NEXT: [[DOTPHI_TRANS_INSERT:%.*]] = getelementptr inbounds [2 x [1 x [4 x float]]], [2 x [1 x [4 x float]]]* null, i64 0, i64 [[INVAR_INC3]], i64 0, i64 2
152 ; CHECK-NEXT: [[DOTPRE]] = load float, float* [[DOTPHI_TRANS_INSERT]], align 4, !invariant.load !0
153 ; CHECK-NEXT: br label [[FUSION_LOOP_HEADER_DIM_1_PREHEADER]]
156 %0 = getelementptr inbounds [2 x [1 x [4 x float]]], [2 x [1 x [4 x float]]]* null, i64 0, i64 0, i64 0, i64 2
157 %1 = load float, float* %0, align 4
158 %2 = fmul float %1, %1
159 br label %fusion.loop_header.dim.1.preheader
161 fusion.loop_header.dim.1.preheader: ; preds = %fusion.loop_header.dim.1.preheader, %entry
162 %fusion.invar_address.dim.0.03 = phi i64 [ 0, %entry ], [ %invar.inc3, %fusion.loop_header.dim.1.preheader ]
163 %3 = getelementptr inbounds [2 x [1 x [4 x float]]], [2 x [1 x [4 x float]]]* null, i64 0, i64 %fusion.invar_address.dim.0.03, i64 0, i64 2
164 %4 = load float, float* %3, align 4, !invariant.load !1
165 %5 = fmul float %4, %4
166 %6 = getelementptr inbounds [2 x [1 x [4 x float]]], [2 x [1 x [4 x float]]]* null, i64 0, i64 %fusion.invar_address.dim.0.03, i64 0, i64 2
167 %7 = load float, float* %6, align 4, !invariant.load !1
168 %8 = fmul float %7, %7
169 %invar.inc3 = add nuw nsw i64 %fusion.invar_address.dim.0.03, 1
170 br label %fusion.loop_header.dim.1.preheader