2 ; RUN: opt < %s -passes=newgvn -S | FileCheck %s
4 %struct.A = type { ptr }
5 @_ZTV1A = available_externally unnamed_addr constant [4 x ptr] [ptr null, ptr @_ZTI1A, ptr @_ZN1A3fooEv, ptr @_ZN1A3barEv], align 8
6 @_ZTI1A = external constant ptr
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 ptr @_Znwm(i64 8) #4
16 tail call void @_ZN1AC1Ev(ptr %call) #1
17 %vtable = load ptr, ptr %call, align 8
18 %cmp.vtables = icmp eq ptr %vtable, getelementptr inbounds ([4 x ptr], ptr @_ZTV1A, i64 0, i64 2)
19 tail call void @llvm.assume(i1 %cmp.vtables)
20 br i1 %p, label %if.then, label %if.else
22 if.then: ; preds = %entry
23 %0 = load ptr, ptr %vtable, align 8
25 ; CHECK: call i32 @_ZN1A3fooEv(
26 %call2 = tail call i32 %0(ptr %call) #1
30 if.else: ; preds = %entry
31 %vfn47 = getelementptr inbounds ptr, ptr %vtable, i64 1
33 ; CHECK: call i32 @_ZN1A3barEv(
34 %1 = load ptr, ptr %vfn47, align 8
36 %call5 = tail call i32 %1(ptr %call) #1
39 if.end: ; preds = %if.else, %if.then
43 ; Check integration with invariant.group handling
44 ; CHECK-LABEL: define void @invariantGroupHandling(i1 zeroext %p) {
45 define void @invariantGroupHandling(i1 zeroext %p) {
47 %call = tail call noalias ptr @_Znwm(i64 8) #4
48 tail call void @_ZN1AC1Ev(ptr %call) #1
49 %vtable = load ptr, ptr %call, align 8, !invariant.group !0
50 %cmp.vtables = icmp eq ptr %vtable, getelementptr inbounds ([4 x ptr], ptr @_ZTV1A, i64 0, i64 2)
51 tail call void @llvm.assume(i1 %cmp.vtables)
52 br i1 %p, label %if.then, label %if.else
54 if.then: ; preds = %entry
55 %0 = load ptr, ptr %vtable, align 8
57 ; CHECK: call i32 @_ZN1A3fooEv(
58 %call2 = tail call i32 %0(ptr %call) #1
59 %vtable1 = load ptr, ptr %call, align 8, !invariant.group !0
60 %call1 = load ptr, ptr %vtable1, align 8
61 ; CHECK: call i32 @_ZN1A3fooEv(
62 %callx = tail call i32 %call1(ptr %call) #1
64 %vtable2 = load ptr, ptr %call, align 8, !invariant.group !0
65 %call4 = load ptr, ptr %vtable2, align 8
66 ; CHECK: call i32 @_ZN1A3fooEv(
67 %cally = tail call i32 %call4(ptr %call) #1
69 %vtable3 = load ptr, ptr %call, align 8, !invariant.group !0
70 %vfun = load ptr, ptr %vtable3, align 8
71 ; CHECK: call i32 @_ZN1A3fooEv(
72 %unknown = tail call i32 %vfun(ptr %call) #1
76 if.else: ; preds = %entry
77 %vfn47 = getelementptr inbounds ptr, ptr %vtable, i64 1
79 ; CHECK: call i32 @_ZN1A3barEv(
80 %1 = load ptr, ptr %vfn47, align 8
82 %call5 = tail call i32 %1(ptr %call) #1
85 if.end: ; preds = %if.else, %if.then
90 ; Checking const propagation in the same BB
91 ; CHECK-LABEL: define i32 @main()
95 %call = tail call noalias ptr @_Znwm(i64 8)
96 tail call void @_ZN1AC1Ev(ptr %call)
97 %vtable = load ptr, ptr %call, align 8
98 %cmp.vtables = icmp eq ptr %vtable, getelementptr inbounds ([4 x ptr], ptr @_ZTV1A, i64 0, i64 2)
99 tail call void @llvm.assume(i1 %cmp.vtables)
101 ; CHECK: call i32 @_ZN1A3fooEv(
102 %0 = load ptr, ptr %vtable, align 8
104 %call2 = tail call i32 %0(ptr %call)
108 ; This tests checks const propatation with fcmp instruction.
109 ; CHECK-LABEL: define float @_Z1gf(float %p)
111 define float @_Z1gf(float %p) {
113 %p.addr = alloca float, align 4
114 %f = alloca float, align 4
115 store float %p, ptr %p.addr, align 4
117 store float 3.000000e+00, ptr %f, align 4
118 %0 = load float, ptr %p.addr, align 4
119 %1 = load float, ptr %f, align 4
120 %cmp = fcmp oeq float %1, %0 ; note const on lhs
121 call void @llvm.assume(i1 %cmp)
123 ; CHECK: ret float 3.000000e+00
127 ; CHECK-LABEL: define float @_Z1hf(float %p)
129 define float @_Z1hf(float %p) {
131 %p.addr = alloca float, align 4
132 store float %p, ptr %p.addr, align 4
134 %0 = load float, ptr %p.addr, align 4
135 %cmp = fcmp nnan ueq float %0, 3.000000e+00
136 call void @llvm.assume(i1 %cmp)
138 ; CHECK: ret float 3.000000e+00
142 declare noalias ptr @_Znwm(i64)
143 declare void @_ZN1AC1Ev(ptr)
144 declare void @llvm.assume(i1)
145 declare i32 @_ZN1A3fooEv(ptr)
146 declare i32 @_ZN1A3barEv(ptr)