2 ; RUN: opt < %s -newgvn -S | FileCheck %s
4 %struct.A = type { i32 (...)** }
5 @_ZTV1A = available_externally unnamed_addr constant [4 x i8*] [i8* null, i8* bitcast (i8** @_ZTI1A to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1A3fooEv to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1A3barEv to i8*)], align 8
6 @_ZTI1A = external constant i8*
8 ; Checks if indirect calls can be replaced with direct
9 ; assuming that %vtable == @_ZTV1A (with alignment).
10 ; Checking const propagation across other BBs
11 ; CHECK-LABEL: define void @_Z1gb(
13 define void @_Z1gb(i1 zeroext %p) {
15 %call = tail call noalias i8* @_Znwm(i64 8) #4
16 %0 = bitcast i8* %call to %struct.A*
17 tail call void @_ZN1AC1Ev(%struct.A* %0) #1
18 %1 = bitcast i8* %call to i8***
19 %vtable = load i8**, i8*** %1, align 8
20 %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([4 x i8*], [4 x i8*]* @_ZTV1A, i64 0, i64 2)
21 tail call void @llvm.assume(i1 %cmp.vtables)
22 br i1 %p, label %if.then, label %if.else
24 if.then: ; preds = %entry
25 %vtable1.cast = bitcast i8** %vtable to i32 (%struct.A*)**
26 %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
28 ; CHECK: call i32 @_ZN1A3fooEv(
29 %call2 = tail call i32 %2(%struct.A* %0) #1
33 if.else: ; preds = %entry
34 %vfn47 = getelementptr inbounds i8*, i8** %vtable, i64 1
35 %vfn4 = bitcast i8** %vfn47 to i32 (%struct.A*)**
37 ; CHECK: call i32 @_ZN1A3barEv(
38 %3 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn4, align 8
40 %call5 = tail call i32 %3(%struct.A* %0) #1
43 if.end: ; preds = %if.else, %if.then
47 ; Check integration with invariant.group handling
48 ; CHECK-LABEL: define void @invariantGroupHandling(i1 zeroext %p) {
49 define void @invariantGroupHandling(i1 zeroext %p) {
51 %call = tail call noalias i8* @_Znwm(i64 8) #4
52 %0 = bitcast i8* %call to %struct.A*
53 tail call void @_ZN1AC1Ev(%struct.A* %0) #1
54 %1 = bitcast i8* %call to i8***
55 %vtable = load i8**, i8*** %1, align 8, !invariant.group !0
56 %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([4 x i8*], [4 x i8*]* @_ZTV1A, i64 0, i64 2)
57 tail call void @llvm.assume(i1 %cmp.vtables)
58 br i1 %p, label %if.then, label %if.else
60 if.then: ; preds = %entry
61 %vtable1.cast = bitcast i8** %vtable to i32 (%struct.A*)**
62 %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
64 ; CHECK: call i32 @_ZN1A3fooEv(
65 %call2 = tail call i32 %2(%struct.A* %0) #1
66 %vtable1 = load i8**, i8*** %1, align 8, !invariant.group !0
67 %vtable2.cast = bitcast i8** %vtable1 to i32 (%struct.A*)**
68 %call1 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable2.cast, align 8
69 ; CHECK: call i32 @_ZN1A3fooEv(
70 %callx = tail call i32 %call1(%struct.A* %0) #1
72 %vtable2 = load i8**, i8*** %1, align 8, !invariant.group !0
73 %vtable3.cast = bitcast i8** %vtable2 to i32 (%struct.A*)**
74 %call4 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable3.cast, align 8
75 ; CHECK: call i32 @_ZN1A3fooEv(
76 %cally = tail call i32 %call4(%struct.A* %0) #1
78 %b = bitcast i8* %call to %struct.A**
79 %vtable3 = load %struct.A*, %struct.A** %b, align 8, !invariant.group !0
80 %vtable4.cast = bitcast %struct.A* %vtable3 to i32 (%struct.A*)**
81 %vfun = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable4.cast, align 8
82 ; CHECK: call i32 @_ZN1A3fooEv(
83 %unknown = tail call i32 %vfun(%struct.A* %0) #1
87 if.else: ; preds = %entry
88 %vfn47 = getelementptr inbounds i8*, i8** %vtable, i64 1
89 %vfn4 = bitcast i8** %vfn47 to i32 (%struct.A*)**
91 ; CHECK: call i32 @_ZN1A3barEv(
92 %3 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn4, align 8
94 %call5 = tail call i32 %3(%struct.A* %0) #1
97 if.end: ; preds = %if.else, %if.then
102 ; Checking const propagation in the same BB
103 ; CHECK-LABEL: define i32 @main()
107 %call = tail call noalias i8* @_Znwm(i64 8)
108 %0 = bitcast i8* %call to %struct.A*
109 tail call void @_ZN1AC1Ev(%struct.A* %0)
110 %1 = bitcast i8* %call to i8***
111 %vtable = load i8**, i8*** %1, align 8
112 %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([4 x i8*], [4 x i8*]* @_ZTV1A, i64 0, i64 2)
113 tail call void @llvm.assume(i1 %cmp.vtables)
114 %vtable1.cast = bitcast i8** %vtable to i32 (%struct.A*)**
116 ; CHECK: call i32 @_ZN1A3fooEv(
117 %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
119 %call2 = tail call i32 %2(%struct.A* %0)
123 ; This tests checks const propatation with fcmp instruction.
124 ; CHECK-LABEL: define float @_Z1gf(float %p)
126 define float @_Z1gf(float %p) {
128 %p.addr = alloca float, align 4
129 %f = alloca float, align 4
130 store float %p, float* %p.addr, align 4
132 store float 3.000000e+00, float* %f, align 4
133 %0 = load float, float* %p.addr, align 4
134 %1 = load float, float* %f, align 4
135 %cmp = fcmp oeq float %1, %0 ; note const on lhs
136 call void @llvm.assume(i1 %cmp)
138 ; CHECK: ret float 3.000000e+00
142 ; CHECK-LABEL: define float @_Z1hf(float %p)
144 define float @_Z1hf(float %p) {
146 %p.addr = alloca float, align 4
147 store float %p, float* %p.addr, align 4
149 %0 = load float, float* %p.addr, align 4
150 %cmp = fcmp nnan ueq float %0, 3.000000e+00
151 call void @llvm.assume(i1 %cmp)
153 ; CHECK: ret float 3.000000e+00
157 ; CHECK-LABEL: define float @_Z1if(float %p)
158 define float @_Z1if(float %p) {
160 %p.addr = alloca float, align 4
161 store float %p, float* %p.addr, align 4
163 %0 = load float, float* %p.addr, align 4
164 %cmp = fcmp ueq float %0, 3.000000e+00 ; no nnan flag - can't propagate
165 call void @llvm.assume(i1 %cmp)
167 ; CHECK-NOT: ret float 3.000000e+00
171 ; This test checks if constant propagation works for multiple node edges
172 ; CHECK-LABEL: define i32 @_Z1ii(i32 %p)
173 define i32 @_Z1ii(i32 %p) {
175 %cmp = icmp eq i32 %p, 42
176 call void @llvm.assume(i1 %cmp)
178 ; CHECK: br i1 true, label %bb2, label %bb2
179 br i1 %cmp, label %bb2, label %bb2
181 call void @llvm.assume(i1 true)
182 ; CHECK: br i1 true, label %bb2, label %bb2
183 br i1 %cmp, label %bb2, label %bb2
189 ; CHECK-LABEL: define i32 @_Z1ij(i32 %p)
190 define i32 @_Z1ij(i32 %p) {
192 %cmp = icmp eq i32 %p, 42
193 call void @llvm.assume(i1 %cmp)
195 ; CHECK: br i1 true, label %bb2, label %bb2
196 br i1 %cmp, label %bb2, label %bb2
199 %cmp2 = icmp eq i32 %p, 42
200 ; CHECK-NOT: call void @llvm.assume(
201 call void @llvm.assume(i1 %cmp2)
203 ; CHECK: br i1 true, label %bb2, label %bb2
204 br i1 %cmp, label %bb2, label %bb2
210 ; CHECK-LABEL: define i32 @_Z1ik(i32 %p)
211 define i32 @_Z1ik(i32 %p) {
213 %cmp = icmp eq i32 %p, 42
214 call void @llvm.assume(i1 %cmp)
216 ; CHECK: br i1 true, label %bb2, label %bb3
217 br i1 %cmp, label %bb2, label %bb3
220 %cmp3 = icmp eq i32 %p, 43
221 ; CHECK: store i8 undef, i8* null
222 call void @llvm.assume(i1 %cmp3)
228 ; This test checks if GVN can do the constant propagation correctly
229 ; when there are multiple uses of the same assume value in the
230 ; basic block that has a loop back-edge pointing to itself.
232 ; CHECK-LABEL: define i32 @_Z1il(i32 %val, i1 %k)
233 define i32 @_Z1il(i32 %val, i1 %k) {
237 ; CHECK: tail call void @llvm.assume(i1 %k)
238 ; CHECK-NEXT: %cmp = icmp eq i32 %val, 50
239 tail call void @llvm.assume(i1 %k)
240 tail call void @llvm.assume(i1 %k)
241 %cmp = icmp eq i32 %val, 50
242 br i1 %cmp, label %next, label %meh
248 ; This test checks if GVN can prevent the constant propagation correctly
249 ; in the successor blocks that are not dominated by the basic block
250 ; with the assume instruction.
252 ; CHECK-LABEL: define i1 @_z1im(i32 %val, i1 %k, i1 %j)
253 define i1 @_z1im(i32 %val, i1 %k, i1 %j) {
254 br i1 %j, label %next, label %meh
257 ; CHECK: tail call void @llvm.assume(i1 %k)
258 ; CHECK-NEXT: br label %meh
259 tail call void @llvm.assume(i1 %k)
260 tail call void @llvm.assume(i1 %k)
268 declare noalias i8* @_Znwm(i64)
269 declare void @_ZN1AC1Ev(%struct.A*)
270 declare void @llvm.assume(i1)
271 declare i32 @_ZN1A3fooEv(%struct.A*)
272 declare i32 @_ZN1A3barEv(%struct.A*)