1; RUN: opt -early-cse -S < %s | FileCheck %s
2; RUN: opt -basicaa -early-cse-memssa -S < %s | FileCheck %s
3
4; Can we CSE a known condition to a constant?
5define i1 @test(i8* %p) {
6; CHECK-LABEL: @test
7entry:
8  %cnd1 = icmp eq i8* %p, null
9  br i1 %cnd1, label %taken, label %untaken
10
11taken:
12; CHECK-LABEL: taken:
13; CHECK-NEXT: ret i1 true
14  %cnd2 = icmp eq i8* %p, null
15  ret i1 %cnd2
16
17untaken:
18; CHECK-LABEL: untaken:
19; CHECK-NEXT: ret i1 false
20  %cnd3 = icmp eq i8* %p, null
21  ret i1 %cnd3
22}
23
24; We can CSE the condition, but we *don't* know it's value after the merge
25define i1 @test_neg1(i8* %p) {
26; CHECK-LABEL: @test_neg1
27entry:
28  %cnd1 = icmp eq i8* %p, null
29  br i1 %cnd1, label %taken, label %untaken
30
31taken:
32  br label %merge
33
34untaken:
35  br label %merge
36
37merge:
38; CHECK-LABEL: merge:
39; CHECK-NEXT: ret i1 %cnd1
40  %cnd3 = icmp eq i8* %p, null
41  ret i1 %cnd3
42}
43
44; Check specifically for a case where we have a unique predecessor, but
45; not a single predecessor.  We can not know the value of the condition here.
46define i1 @test_neg2(i8* %p) {
47; CHECK-LABEL: @test_neg2
48entry:
49  %cnd1 = icmp eq i8* %p, null
50  br i1 %cnd1, label %merge, label %merge
51
52merge:
53; CHECK-LABEL: merge:
54; CHECK-NEXT: ret i1 %cnd1
55  %cnd3 = icmp eq i8* %p, null
56  ret i1 %cnd3
57}
58
59; Replace a use rather than CSE
60define i1 @test2(i8* %p) {
61; CHECK-LABEL: @test2
62entry:
63  %cnd = icmp eq i8* %p, null
64  br i1 %cnd, label %taken, label %untaken
65
66taken:
67; CHECK-LABEL: taken:
68; CHECK-NEXT: ret i1 true
69  ret i1 %cnd
70
71untaken:
72; CHECK-LABEL: untaken:
73; CHECK-NEXT: ret i1 false
74  ret i1 %cnd
75}
76
77; Not legal to replace use given it's not dominated by edge
78define i1 @test2_neg1(i8* %p) {
79; CHECK-LABEL: @test2_neg1
80entry:
81  %cnd1 = icmp eq i8* %p, null
82  br i1 %cnd1, label %taken, label %untaken
83
84taken:
85  br label %merge
86
87untaken:
88  br label %merge
89
90merge:
91; CHECK-LABEL: merge:
92; CHECK-NEXT: ret i1 %cnd1
93  ret i1 %cnd1
94}
95
96; Another single predecessor test, but for dominated use
97define i1 @test2_neg2(i8* %p) {
98; CHECK-LABEL: @test2_neg2
99entry:
100  %cnd1 = icmp eq i8* %p, null
101  br i1 %cnd1, label %merge, label %merge
102
103merge:
104; CHECK-LABEL: merge:
105; CHECK-NEXT: ret i1 %cnd1
106  ret i1 %cnd1
107}
108
109