xref: /llvm-project/llvm/test/Transforms/SimplifyCFG/branch-fold.ll (revision fc6bdb8549842613da51b9d570b29e27cc709f69)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
3
4define void @test(ptr %P, ptr %Q, i1 %A, i1 %B) {
5; CHECK-LABEL: @test(
6; CHECK-NEXT:  entry:
7; CHECK-NEXT:    [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
8; CHECK-NEXT:    [[BRMERGE:%.*]] = select i1 [[A_NOT]], i1 true, i1 [[B:%.*]]
9; CHECK-NEXT:    br i1 [[BRMERGE]], label [[B:%.*]], label [[COMMON_RET:%.*]]
10; CHECK:       common.ret:
11; CHECK-NEXT:    ret void
12; CHECK:       b:
13; CHECK-NEXT:    store i32 123, ptr [[P:%.*]], align 4
14; CHECK-NEXT:    br label [[COMMON_RET]]
15;
16
17entry:
18  br i1 %A, label %a, label %b
19a:
20  br i1 %B, label %b, label %c
21b:
22  store i32 123, ptr %P
23  ret void
24c:
25  ret void
26}
27
28; rdar://10554090
29define zeroext i1 @test2(i64 %i0, i64 %i1) nounwind uwtable readonly ssp {
30; CHECK-LABEL: @test2(
31; CHECK-NEXT:  entry:
32; CHECK-NEXT:    [[AND_I_I:%.*]] = and i64 [[I0:%.*]], 281474976710655
33; CHECK-NEXT:    [[AND_I11_I:%.*]] = and i64 [[I1:%.*]], 281474976710655
34; CHECK-NEXT:    [[OR_COND:%.*]] = icmp eq i64 [[AND_I_I]], [[AND_I11_I]]
35; CHECK-NEXT:    br i1 [[OR_COND]], label [[C:%.*]], label [[A:%.*]]
36; CHECK:       a:
37; CHECK-NEXT:    [[SHR_I4_I:%.*]] = lshr i64 [[I0]], 48
38; CHECK-NEXT:    [[AND_I5_I:%.*]] = and i64 [[SHR_I4_I]], 32767
39; CHECK-NEXT:    [[SHR_I_I:%.*]] = lshr i64 [[I1]], 48
40; CHECK-NEXT:    [[AND_I2_I:%.*]] = and i64 [[SHR_I_I]], 32767
41; CHECK-NEXT:    [[CMP9_I:%.*]] = icmp ult i64 [[AND_I5_I]], [[AND_I2_I]]
42; CHECK-NEXT:    br i1 [[CMP9_I]], label [[C]], label [[B:%.*]]
43; CHECK:       b:
44; CHECK-NEXT:    [[SHR_I13_I9:%.*]] = lshr i64 [[I1]], 48
45; CHECK-NEXT:    [[AND_I14_I10:%.*]] = and i64 [[SHR_I13_I9]], 32767
46; CHECK-NEXT:    [[SHR_I_I11:%.*]] = lshr i64 [[I0]], 48
47; CHECK-NEXT:    [[AND_I11_I12:%.*]] = and i64 [[SHR_I_I11]], 32767
48; CHECK-NEXT:    [[PHITMP:%.*]] = icmp uge i64 [[AND_I14_I10]], [[AND_I11_I12]]
49; CHECK-NEXT:    br label [[C]]
50; CHECK:       c:
51; CHECK-NEXT:    [[O2:%.*]] = phi i1 [ false, [[A]] ], [ [[PHITMP]], [[B]] ], [ false, [[ENTRY:%.*]] ]
52; CHECK-NEXT:    ret i1 [[O2]]
53;
54entry:
55  %and.i.i = and i64 %i0, 281474976710655
56  %and.i11.i = and i64 %i1, 281474976710655
57  %or.cond = icmp eq i64 %and.i.i, %and.i11.i
58  br i1 %or.cond, label %c, label %a
59
60a:
61  %shr.i4.i = lshr i64 %i0, 48
62  %and.i5.i = and i64 %shr.i4.i, 32767
63  %shr.i.i = lshr i64 %i1, 48
64  %and.i2.i = and i64 %shr.i.i, 32767
65  %cmp9.i = icmp ult i64 %and.i5.i, %and.i2.i
66  br i1 %cmp9.i, label %c, label %b
67
68b:
69  %shr.i13.i9 = lshr i64 %i1, 48
70  %and.i14.i10 = and i64 %shr.i13.i9, 32767
71  %shr.i.i11 = lshr i64 %i0, 48
72  %and.i11.i12 = and i64 %shr.i.i11, 32767
73  %phitmp = icmp uge i64 %and.i14.i10, %and.i11.i12
74  br label %c
75
76c:
77  %o2 = phi i1 [ false, %a ], [ %phitmp, %b ], [ false, %entry ]
78  ret i1 %o2
79}
80
81; PR13180
82define void @pr13180(i8 %p) {
83; CHECK-LABEL: @pr13180(
84; CHECK-NEXT:  entry:
85; CHECK-NEXT:    unreachable
86;
87entry:
88  %tobool = icmp eq i8 %p, 0
89  br i1 %tobool, label %cond.false, label %cond.true
90
91cond.true:                                        ; preds = %entry
92  br label %cond.end
93
94cond.false:                                       ; preds = %entry
95  %phitmp = icmp eq i8 %p, 0
96  br label %cond.end
97
98cond.end:                                         ; preds = %cond.false, %cond.true
99  %cond = phi i1 [ undef, %cond.true ], [ %phitmp, %cond.false ]
100  unreachable
101}
102
103declare void @foo()
104define void @test3() {
105; CHECK-LABEL: @test3(
106; CHECK-NEXT:  entry:
107; CHECK-NEXT:    call void @foo()
108; CHECK-NEXT:    ret void
109;
110entry:
111  br i1 0, label %bb0, label %bb0
112
113bb0:
114  call void @foo()
115  ret void
116}
117
118define i8 @common_pred(i8 noundef %arg, i1 %c1, i1 %c2) {
119; CHECK-LABEL: @common_pred(
120; CHECK-NEXT:  Pred:
121; CHECK-NEXT:    call void @dummy()
122; CHECK-NEXT:    br i1 [[C1:%.*]], label [[COMMONPRED:%.*]], label [[SUCC:%.*]]
123; CHECK:       CommonPred:
124; CHECK-NEXT:    call void @dummy()
125; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[C2:%.*]], i8 4, i8 1
126; CHECK-NEXT:    br label [[SUCC]]
127; CHECK:       Succ:
128; CHECK-NEXT:    [[PHI2:%.*]] = phi i8 [ 0, [[PRED:%.*]] ], [ [[SPEC_SELECT]], [[COMMONPRED]] ]
129; CHECK-NEXT:    ret i8 [[PHI2]]
130;
131Pred:
132call void @dummy()
133  br i1 %c1, label %CommonPred, label %BB
134
135CommonPred:
136call void @dummy()
137  br i1 %c2, label %Succ, label %BB
138
139BB:
140  %phi1 = phi i8 [ 0, %Pred ], [1, %CommonPred]
141  br label %Succ
142
143Succ:
144  %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
145  ret i8 %phi2
146}
147
148declare void @dummy()
149