Constant propagation after hiting llvm.assume
[oota-llvm.git] / test / Transforms / GVN / assume-equal.ll
1 ; RUN: opt < %s -gvn -S | FileCheck %s
2
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*
6
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(
11
12 define void @_Z1gb(i1 zeroext %p) {
13 entry:
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
22
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
26   
27   ; CHECK: call i32 @_ZN1A3fooEv(
28   %call2 = tail call i32 %2(%struct.A* %0) #1
29   
30   br label %if.end
31
32 if.else:                                          ; preds = %entry
33   %vfn47 = getelementptr inbounds i8*, i8** %vtable, i64 1
34   %vfn4 = bitcast i8** %vfn47 to i32 (%struct.A*)**
35   
36   ; CHECK: call i32 @_ZN1A3barEv(
37   %3 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn4, align 8
38   
39   %call5 = tail call i32 %3(%struct.A* %0) #1
40   br label %if.end
41
42 if.end:                                           ; preds = %if.else, %if.then
43   ret void
44 }
45
46 ; Checking const propagation in the same BB
47 ; CHECK-LABEL: define i32 @main()
48
49 define i32 @main() {
50 entry:
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*)**
59   
60   ; CHECK: call i32 @_ZN1A3fooEv(
61   %2 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vtable1.cast, align 8
62   
63   %call2 = tail call i32 %2(%struct.A* %0)
64   ret i32 0
65 }
66
67 ; This tests checks const propatation with fcmp instruction.
68 ; CHECK-LABEL: define float @_Z1gf(float %p)
69
70 define float @_Z1gf(float %p) {
71 entry:
72   %p.addr = alloca float, align 4
73   %f = alloca float, align 4
74   store float %p, float* %p.addr, align 4
75   
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)
81   
82   ; CHECK: ret float 3.000000e+00
83   ret float %0
84 }
85
86 ; CHECK-LABEL: define float @_Z1hf(float %p)
87
88 define float @_Z1hf(float %p) {
89 entry:
90   %p.addr = alloca float, align 4
91   store float %p, float* %p.addr, align 4
92   
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)
96   
97   ; CHECK: ret float 3.000000e+00
98   ret float %0
99 }
100
101 ; CHECK-LABEL: define float @_Z1if(float %p)
102
103
104 define float @_Z1if(float %p) {
105 entry:
106   %p.addr = alloca float, align 4
107   store float %p, float* %p.addr, align 4
108   
109   %0 = load float, float* %p.addr, align 4
110   %cmp = fcmp ueq float %0, 3.000000e+00 ; no nnan flag - can't propagate
111   call void @llvm.assume(i1 %cmp)
112   
113   ; CHECK-NOT: ret float 3.000000e+00
114   ret float %0
115 }
116
117 declare noalias i8* @_Znwm(i64)
118 declare void @_ZN1AC1Ev(%struct.A*)
119 declare void @llvm.assume(i1)
120 declare i32 @_ZN1A3fooEv(%struct.A*)
121 declare i32 @_ZN1A3barEv(%struct.A*)
122