1; RUN: opt -verify-loop-info -irce-print-changed-loops -irce -S
2; RUN: opt -verify-loop-info -irce-print-changed-loops -passes='require<branch-prob>,irce' -S
3
4; Make sure that IRCE doesn't apply in case of empty ranges.
5; (i + 30 < 40) if i in [-30, 10).
6; Intersected with iteration space, it is [0, 10).
7; (i - 60 < 40) if i in [60 , 100).
8; The intersection with safe iteration space is the empty range [60, 10).
9; It is better to eliminate one range check than attempt to eliminate both given
10; that we will never go to the main loop in the latter case and basically
11; only duplicate code with no benefits.
12
13define void @test_01(i32* %arr, i32* %a_len_ptr) #0 {
14
15; CHECK-LABEL: test_01(
16; CHECK-NOT:   preloop
17; CHECK:       entry:
18; CHECK-NEXT:    br i1 true, label %loop.preheader, label %main.pseudo.exit
19; CHECK:       in.bounds.1:
20; CHECK-NEXT:    %addr = getelementptr i32, i32* %arr, i32 %idx
21; CHECK-NEXT:    store i32 0, i32* %addr
22; CHECK-NEXT:    %off1 = add i32 %idx, 30
23; CHECK-NEXT:    %c2 = icmp slt i32 %off1, 40
24; CHECK-NEXT:    br i1 true, label %in.bounds.2, label %exit.loopexit2
25; CHECK:       in.bounds.2:
26; CHECK-NEXT:    %off2 = add i32 %idx, -60
27; CHECK-NEXT:    %c3 = icmp slt i32 %off2, 40
28; CHECK-NEXT:    br i1 %c3, label %in.bounds.3, label %exit.loopexit2
29; CHECK:       in.bounds.3:
30; CHECK-NEXT:    %next = icmp ult i32 %idx.next, 100
31; CHECK-NEXT:    [[COND1:%[^ ]+]] = icmp ult i32 %idx.next, 10
32; CHECK-NEXT:    br i1 [[COND1]], label %loop, label %main.exit.selector
33; CHECK:       main.exit.selector:
34; CHECK-NEXT:    %idx.next.lcssa = phi i32 [ %idx.next, %in.bounds.3 ]
35; CHECK-NEXT:    [[COND2:%[^ ]+]] = icmp ult i32 %idx.next.lcssa, 100
36; CHECK-NEXT:    br i1 [[COND2]], label %main.pseudo.exit, label %exit
37; CHECK:       postloop:
38
39entry:
40  br label %loop
41
42loop:
43  %idx = phi i32 [ 0, %entry ], [ %idx.next, %in.bounds.3 ]
44  %idx.next = add nsw nuw i32 %idx, 1
45  %c1 = icmp slt i32 %idx, 20
46  br i1 %c1, label %in.bounds.1, label %out.of.bounds
47
48in.bounds.1:
49  %addr = getelementptr i32, i32* %arr, i32 %idx
50  store i32 0, i32* %addr
51  %off1 = add i32 %idx, 30
52  %c2 = icmp slt i32 %off1, 40
53  br i1 %c2, label %in.bounds.2, label %exit
54
55in.bounds.2:
56  %off2 = add i32 %idx, -60
57  %c3 = icmp slt i32 %off2, 40
58  br i1 %c3, label %in.bounds.3, label %exit
59
60in.bounds.3:
61  %next = icmp ult i32 %idx.next, 100
62  br i1 %next, label %loop, label %exit
63
64out.of.bounds:
65  ret void
66
67exit:
68  ret void
69}
70