1*cee313d2SEric Christopher; RUN: opt -S -jump-threading %s | FileCheck %s
2*cee313d2SEric Christopher; When simplify a branch based on LVI predicates, we should replace the
3*cee313d2SEric Christopher; comparison itself with a constant (when possible) in case it's otherwise used.
4*cee313d2SEric Christopher
5*cee313d2SEric Christopherdefine i32 @test(i32* %p) {
6*cee313d2SEric Christopher; CHECK-LABEL: @test
7*cee313d2SEric Christopher; CHECK: icmp eq
8*cee313d2SEric Christopher; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
9*cee313d2SEric Christopher; CHECK-NOT: icmp ne
10*cee313d2SEric Christopherentry:
11*cee313d2SEric Christopher  %cmp = icmp eq i32* %p, null
12*cee313d2SEric Christopher  br i1 %cmp, label %is_null, label %not_null
13*cee313d2SEric Christopheris_null:
14*cee313d2SEric Christopher  %cmp2 = icmp ne i32* %p, null
15*cee313d2SEric Christopher  br i1 %cmp2, label %exit1, label %exit2
16*cee313d2SEric Christophernot_null:
17*cee313d2SEric Christopher  %cmp3 = icmp ne i32* %p, null
18*cee313d2SEric Christopher  br i1 %cmp3, label %exit1, label %exit2
19*cee313d2SEric Christopherexit1:
20*cee313d2SEric Christopher  ret i32 0
21*cee313d2SEric Christopherexit2:
22*cee313d2SEric Christopher  ret i32 1
23*cee313d2SEric Christopher}
24*cee313d2SEric Christopher
25*cee313d2SEric Christopherdeclare void @use(i1)
26*cee313d2SEric Christopher
27*cee313d2SEric Christopher; It would not be legal to replace %cmp2 (well, in this case it actually is,
28*cee313d2SEric Christopher; but that's a CSE problem, not a LVI/jump threading problem)
29*cee313d2SEric Christopherdefine i32 @test_negative(i32* %p) {
30*cee313d2SEric Christopher; CHECK-LABEL: @test
31*cee313d2SEric Christopher; CHECK: icmp ne
32*cee313d2SEric Christopher; CHECK: icmp eq
33*cee313d2SEric Christopher; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
34*cee313d2SEric Christopher; CHECK-NOT: icmp ne
35*cee313d2SEric Christopherentry:
36*cee313d2SEric Christopher  %cmp2 = icmp ne i32* %p, null
37*cee313d2SEric Christopher  call void @use(i1 %cmp2)
38*cee313d2SEric Christopher  %cmp = icmp eq i32* %p, null
39*cee313d2SEric Christopher  br i1 %cmp, label %is_null, label %not_null
40*cee313d2SEric Christopheris_null:
41*cee313d2SEric Christopher  br i1 %cmp2, label %exit1, label %exit2
42*cee313d2SEric Christophernot_null:
43*cee313d2SEric Christopher  br i1 %cmp2, label %exit1, label %exit2
44*cee313d2SEric Christopherexit1:
45*cee313d2SEric Christopher  ret i32 0
46*cee313d2SEric Christopherexit2:
47*cee313d2SEric Christopher  ret i32 1
48*cee313d2SEric Christopher}
49*cee313d2SEric Christopher
50*cee313d2SEric Christopher; In this case, we can remove cmp2 because it's otherwise unused
51*cee313d2SEric Christopherdefine i32 @test2(i32* %p) {
52*cee313d2SEric Christopher; CHECK-LABEL: @test
53*cee313d2SEric Christopher; CHECK-LABEL: entry:
54*cee313d2SEric Christopher; CHECK-NEXT: icmp eq
55*cee313d2SEric Christopher; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
56*cee313d2SEric Christopher; CHECK-NOT: icmp ne
57*cee313d2SEric Christopherentry:
58*cee313d2SEric Christopher  %cmp2 = icmp ne i32* %p, null
59*cee313d2SEric Christopher  %cmp = icmp eq i32* %p, null
60*cee313d2SEric Christopher  br i1 %cmp, label %is_null, label %not_null
61*cee313d2SEric Christopheris_null:
62*cee313d2SEric Christopher  br i1 %cmp2, label %exit1, label %exit2
63*cee313d2SEric Christophernot_null:
64*cee313d2SEric Christopher  br i1 %cmp2, label %exit1, label %exit2
65*cee313d2SEric Christopherexit1:
66*cee313d2SEric Christopher  ret i32 0
67*cee313d2SEric Christopherexit2:
68*cee313d2SEric Christopher  ret i32 1
69*cee313d2SEric Christopher}
70