xref: /llvm-project/llvm/test/Transforms/EarlyCSE/conditional.ll (revision ac696ac4530fb3df626195e94e83649bf7114754)
1*ac696ac4SBjorn Pettersson; RUN: opt -passes=early-cse -earlycse-debug-hash -S < %s | FileCheck %s
2c384b20bSArthur Eubanks; RUN: opt -passes='early-cse<memssa>' -S < %s | FileCheck %s
3cee313d2SEric Christopher
4cee313d2SEric Christopher; Can we CSE a known condition to a constant?
53c514d31SNikita Popovdefine i1 @test(ptr %p) {
6cee313d2SEric Christopher; CHECK-LABEL: @test
7cee313d2SEric Christopherentry:
83c514d31SNikita Popov  %cnd1 = icmp eq ptr %p, null
9cee313d2SEric Christopher  br i1 %cnd1, label %taken, label %untaken
10cee313d2SEric Christopher
11cee313d2SEric Christophertaken:
12cee313d2SEric Christopher; CHECK-LABEL: taken:
13cee313d2SEric Christopher; CHECK-NEXT: ret i1 true
143c514d31SNikita Popov  %cnd2 = icmp eq ptr %p, null
15cee313d2SEric Christopher  ret i1 %cnd2
16cee313d2SEric Christopher
17cee313d2SEric Christopheruntaken:
18cee313d2SEric Christopher; CHECK-LABEL: untaken:
19cee313d2SEric Christopher; CHECK-NEXT: ret i1 false
203c514d31SNikita Popov  %cnd3 = icmp eq ptr %p, null
21cee313d2SEric Christopher  ret i1 %cnd3
22cee313d2SEric Christopher}
23cee313d2SEric Christopher
24cee313d2SEric Christopher; We can CSE the condition, but we *don't* know it's value after the merge
253c514d31SNikita Popovdefine i1 @test_neg1(ptr %p) {
26cee313d2SEric Christopher; CHECK-LABEL: @test_neg1
27cee313d2SEric Christopherentry:
283c514d31SNikita Popov  %cnd1 = icmp eq ptr %p, null
29cee313d2SEric Christopher  br i1 %cnd1, label %taken, label %untaken
30cee313d2SEric Christopher
31cee313d2SEric Christophertaken:
32cee313d2SEric Christopher  br label %merge
33cee313d2SEric Christopher
34cee313d2SEric Christopheruntaken:
35cee313d2SEric Christopher  br label %merge
36cee313d2SEric Christopher
37cee313d2SEric Christophermerge:
38cee313d2SEric Christopher; CHECK-LABEL: merge:
39cee313d2SEric Christopher; CHECK-NEXT: ret i1 %cnd1
403c514d31SNikita Popov  %cnd3 = icmp eq ptr %p, null
41cee313d2SEric Christopher  ret i1 %cnd3
42cee313d2SEric Christopher}
43cee313d2SEric Christopher
44cee313d2SEric Christopher; Check specifically for a case where we have a unique predecessor, but
45cee313d2SEric Christopher; not a single predecessor.  We can not know the value of the condition here.
463c514d31SNikita Popovdefine i1 @test_neg2(ptr %p) {
47cee313d2SEric Christopher; CHECK-LABEL: @test_neg2
48cee313d2SEric Christopherentry:
493c514d31SNikita Popov  %cnd1 = icmp eq ptr %p, null
50cee313d2SEric Christopher  br i1 %cnd1, label %merge, label %merge
51cee313d2SEric Christopher
52cee313d2SEric Christophermerge:
53cee313d2SEric Christopher; CHECK-LABEL: merge:
54cee313d2SEric Christopher; CHECK-NEXT: ret i1 %cnd1
553c514d31SNikita Popov  %cnd3 = icmp eq ptr %p, null
56cee313d2SEric Christopher  ret i1 %cnd3
57cee313d2SEric Christopher}
58cee313d2SEric Christopher
59cee313d2SEric Christopher; Replace a use rather than CSE
603c514d31SNikita Popovdefine i1 @test2(ptr %p) {
61cee313d2SEric Christopher; CHECK-LABEL: @test2
62cee313d2SEric Christopherentry:
633c514d31SNikita Popov  %cnd = icmp eq ptr %p, null
64cee313d2SEric Christopher  br i1 %cnd, label %taken, label %untaken
65cee313d2SEric Christopher
66cee313d2SEric Christophertaken:
67cee313d2SEric Christopher; CHECK-LABEL: taken:
68cee313d2SEric Christopher; CHECK-NEXT: ret i1 true
69cee313d2SEric Christopher  ret i1 %cnd
70cee313d2SEric Christopher
71cee313d2SEric Christopheruntaken:
72cee313d2SEric Christopher; CHECK-LABEL: untaken:
73cee313d2SEric Christopher; CHECK-NEXT: ret i1 false
74cee313d2SEric Christopher  ret i1 %cnd
75cee313d2SEric Christopher}
76cee313d2SEric Christopher
77cee313d2SEric Christopher; Not legal to replace use given it's not dominated by edge
783c514d31SNikita Popovdefine i1 @test2_neg1(ptr %p) {
79cee313d2SEric Christopher; CHECK-LABEL: @test2_neg1
80cee313d2SEric Christopherentry:
813c514d31SNikita Popov  %cnd1 = icmp eq ptr %p, null
82cee313d2SEric Christopher  br i1 %cnd1, label %taken, label %untaken
83cee313d2SEric Christopher
84cee313d2SEric Christophertaken:
85cee313d2SEric Christopher  br label %merge
86cee313d2SEric Christopher
87cee313d2SEric Christopheruntaken:
88cee313d2SEric Christopher  br label %merge
89cee313d2SEric Christopher
90cee313d2SEric Christophermerge:
91cee313d2SEric Christopher; CHECK-LABEL: merge:
92cee313d2SEric Christopher; CHECK-NEXT: ret i1 %cnd1
93cee313d2SEric Christopher  ret i1 %cnd1
94cee313d2SEric Christopher}
95cee313d2SEric Christopher
96cee313d2SEric Christopher; Another single predecessor test, but for dominated use
973c514d31SNikita Popovdefine i1 @test2_neg2(ptr %p) {
98cee313d2SEric Christopher; CHECK-LABEL: @test2_neg2
99cee313d2SEric Christopherentry:
1003c514d31SNikita Popov  %cnd1 = icmp eq ptr %p, null
101cee313d2SEric Christopher  br i1 %cnd1, label %merge, label %merge
102cee313d2SEric Christopher
103cee313d2SEric Christophermerge:
104cee313d2SEric Christopher; CHECK-LABEL: merge:
105cee313d2SEric Christopher; CHECK-NEXT: ret i1 %cnd1
106cee313d2SEric Christopher  ret i1 %cnd1
107cee313d2SEric Christopher}
108cee313d2SEric Christopher
109