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