1 ; RUN: opt < %s -gvn -S | FileCheck %s
3 %struct.A = type { i32 (...)** }
4 @_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
5 @_ZTI1A = external constant i8*
7 ; Checks if indirect calls can be replaced with direct
8 ; assuming that %vtable == @_ZTV1A (with alignment).
9 ; Checking const propagation across other BBs
10 ; CHECK-LABEL: define void @_Z1gb(
12 define void @_Z1gb(i1 zeroext %p) {
14 %call = tail call noalias i8* @_Znwm(i64 8) #4
15 %0 = bitcast i8* %call to %struct.A*
16 tail call void @_ZN1AC1Ev(%struct.A* %0) #1
17 %1 = bitcast i8* %call to i8***
18 %vtable = load i8**, i8*** %1, align 8
19 %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([4 x i8*], [4 x i8*]* @_ZTV1A, i64 0, i64 2)
20 tail call void @llvm.assume(i1 %cmp.vtables)
21 br i1 %p, label %if.then, label %if.else
23 if.then: ; preds = %entry
24 %vtable1.cast = bitcast i8** %vtable to i32 (%struct.A*)**
25 %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
27 ; CHECK: call i32 @_ZN1A3fooEv(
28 %call2 = tail call i32 %2(%struct.A* %0) #1
32 if.else: ; preds = %entry
33 %vfn47 = getelementptr inbounds i8*, i8** %vtable, i64 1
34 %vfn4 = bitcast i8** %vfn47 to i32 (%struct.A*)**
36 ; CHECK: call i32 @_ZN1A3barEv(
37 %3 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn4, align 8
39 %call5 = tail call i32 %3(%struct.A* %0) #1
42 if.end: ; preds = %if.else, %if.then
46 ; Checking const propagation in the same BB
47 ; CHECK-LABEL: define i32 @main()
51 %call = tail call noalias i8* @_Znwm(i64 8)
52 %0 = bitcast i8* %call to %struct.A*
53 tail call void @_ZN1AC1Ev(%struct.A* %0)
54 %1 = bitcast i8* %call to i8***
55 %vtable = load i8**, i8*** %1, align 8
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 %vtable1.cast = bitcast i8** %vtable to i32 (%struct.A*)**
60 ; CHECK: call i32 @_ZN1A3fooEv(
61 %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
63 %call2 = tail call i32 %2(%struct.A* %0)
67 ; This tests checks const propatation with fcmp instruction.
68 ; CHECK-LABEL: define float @_Z1gf(float %p)
70 define float @_Z1gf(float %p) {
72 %p.addr = alloca float, align 4
73 %f = alloca float, align 4
74 store float %p, float* %p.addr, align 4
76 store float 3.000000e+00, float* %f, align 4
77 %0 = load float, float* %p.addr, align 4
78 %1 = load float, float* %f, align 4
79 %cmp = fcmp oeq float %1, %0 ; note const on lhs
80 call void @llvm.assume(i1 %cmp)
82 ; CHECK: ret float 3.000000e+00
86 ; CHECK-LABEL: define float @_Z1hf(float %p)
88 define float @_Z1hf(float %p) {
90 %p.addr = alloca float, align 4
91 store float %p, float* %p.addr, align 4
93 %0 = load float, float* %p.addr, align 4
94 %cmp = fcmp nnan ueq float %0, 3.000000e+00
95 call void @llvm.assume(i1 %cmp)
97 ; CHECK: ret float 3.000000e+00
101 ; CHECK-LABEL: define float @_Z1if(float %p)
102 define float @_Z1if(float %p) {
104 %p.addr = alloca float, align 4
105 store float %p, float* %p.addr, align 4
107 %0 = load float, float* %p.addr, align 4
108 %cmp = fcmp ueq float %0, 3.000000e+00 ; no nnan flag - can't propagate
109 call void @llvm.assume(i1 %cmp)
111 ; CHECK-NOT: ret float 3.000000e+00
115 ; This test checks if constant propagation works for multiple node edges
116 ; CHECK-LABEL: define i32 @_Z1ii(i32 %p)
117 define i32 @_Z1ii(i32 %p) {
119 %cmp = icmp eq i32 %p, 42
120 call void @llvm.assume(i1 %cmp)
122 ; CHECK: br i1 true, label %bb2, label %bb2
123 br i1 %cmp, label %bb2, label %bb2
125 call void @llvm.assume(i1 true)
126 ; CHECK: br i1 true, label %bb2, label %bb2
127 br i1 %cmp, label %bb2, label %bb2
133 ; CHECK-LABEL: define i32 @_Z1ij(i32 %p)
134 define i32 @_Z1ij(i32 %p) {
136 %cmp = icmp eq i32 %p, 42
137 call void @llvm.assume(i1 %cmp)
139 ; CHECK: br i1 true, label %bb2, label %bb2
140 br i1 %cmp, label %bb2, label %bb2
143 %cmp2 = icmp eq i32 %p, 42
144 ; CHECK-NOT: call void @llvm.assume(
145 call void @llvm.assume(i1 %cmp2)
147 ; CHECK: br i1 true, label %bb2, label %bb2
148 br i1 %cmp, label %bb2, label %bb2
154 ; CHECK-LABEL: define i32 @_Z1ik(i32 %p)
155 define i32 @_Z1ik(i32 %p) {
157 %cmp = icmp eq i32 %p, 42
158 call void @llvm.assume(i1 %cmp)
160 ; CHECK: br i1 true, label %bb2, label %bb3
161 br i1 %cmp, label %bb2, label %bb3
164 %cmp3 = icmp eq i32 %p, 43
165 ; CHECK: store i8 undef, i8* null
166 call void @llvm.assume(i1 %cmp3)
172 declare noalias i8* @_Znwm(i64)
173 declare void @_ZN1AC1Ev(%struct.A*)
174 declare void @llvm.assume(i1)
175 declare i32 @_ZN1A3fooEv(%struct.A*)
176 declare i32 @_ZN1A3barEv(%struct.A*)