1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -instcombine -S < %s | FileCheck %s
3
4; Check that we can turn the icmp sle into icmp ult, regardless of the
5; order of the incoming values of the PHI node.
6
7declare i1 @cond()
8
9define void @phi_recurrence_start_first() {
10; CHECK-LABEL: @phi_recurrence_start_first(
11; CHECK-NEXT:  entry:
12; CHECK-NEXT:    br label [[WHILE_COND:%.*]]
13; CHECK:       while.cond:
14; CHECK-NEXT:    [[CELL_0:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[START:%.*]], [[FOR_COND26:%.*]] ]
15; CHECK-NEXT:    [[COND_V:%.*]] = call i1 @cond()
16; CHECK-NEXT:    br i1 [[COND_V]], label [[IF_THEN:%.*]], label [[WHILE_END:%.*]]
17; CHECK:       if.then:
18; CHECK-NEXT:    [[START]] = add nuw nsw i32 [[CELL_0]], 1
19; CHECK-NEXT:    br i1 [[COND_V]], label [[FOR_COND11:%.*]], label [[FOR_COND26]]
20; CHECK:       for.cond11:
21; CHECK-NEXT:    [[I_1:%.*]] = phi i32 [ [[START]], [[IF_THEN]] ], [ [[STEP:%.*]], [[FOR_COND11]] ]
22; CHECK-NEXT:    [[CMP13:%.*]] = icmp ult i32 [[I_1]], 100
23; CHECK-NEXT:    [[STEP]] = add nuw nsw i32 [[I_1]], 1
24; CHECK-NEXT:    br i1 [[CMP13]], label [[FOR_COND11]], label [[WHILE_END]]
25; CHECK:       for.cond26:
26; CHECK-NEXT:    br label [[WHILE_COND]]
27; CHECK:       while.end:
28; CHECK-NEXT:    ret void
29;
30entry:
31  br label %while.cond
32
33while.cond:                                       ; preds = %entry, %for.cond26
34  %cell.0 = phi i32 [ 0, %entry ], [ %start, %for.cond26 ]
35  %cond.v = call i1 @cond()
36  br i1 %cond.v, label %if.then, label %while.end
37
38if.then:                                          ; preds = %while.cond
39  %start = add nsw i32 %cell.0, 1
40  br i1 %cond.v, label %for.cond11, label %for.cond26
41
42for.cond11:                                       ; preds = %for.cond11, %if.then
43  %i.1 = phi i32 [ %start, %if.then ], [ %step, %for.cond11 ]
44  %cmp13 = icmp sle i32 %i.1, 99
45  %step = add nsw i32 %i.1, 1
46  br i1 %cmp13, label %for.cond11, label %while.end
47
48for.cond26:                                       ; preds = %if.then
49  br label %while.cond
50
51while.end:                                        ; preds = %while.cond, %for.cond11
52  ret void
53}
54
55define void @phi_recurrence_step_first() {
56; CHECK-LABEL: @phi_recurrence_step_first(
57; CHECK-NEXT:  entry:
58; CHECK-NEXT:    br label [[WHILE_COND:%.*]]
59; CHECK:       while.cond:
60; CHECK-NEXT:    [[CELL_0:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[START:%.*]], [[FOR_COND26:%.*]] ]
61; CHECK-NEXT:    [[COND_V:%.*]] = call i1 @cond()
62; CHECK-NEXT:    br i1 [[COND_V]], label [[IF_THEN:%.*]], label [[WHILE_END:%.*]]
63; CHECK:       if.then:
64; CHECK-NEXT:    [[START]] = add nuw nsw i32 [[CELL_0]], 1
65; CHECK-NEXT:    br i1 [[COND_V]], label [[FOR_COND11:%.*]], label [[FOR_COND26]]
66; CHECK:       for.cond11:
67; CHECK-NEXT:    [[I_1:%.*]] = phi i32 [ [[STEP:%.*]], [[FOR_COND11]] ], [ [[START]], [[IF_THEN]] ]
68; CHECK-NEXT:    [[CMP13:%.*]] = icmp ult i32 [[I_1]], 100
69; CHECK-NEXT:    [[STEP]] = add nuw nsw i32 [[I_1]], 1
70; CHECK-NEXT:    br i1 [[CMP13]], label [[FOR_COND11]], label [[WHILE_END]]
71; CHECK:       for.cond26:
72; CHECK-NEXT:    br label [[WHILE_COND]]
73; CHECK:       while.end:
74; CHECK-NEXT:    ret void
75;
76entry:
77  br label %while.cond
78
79while.cond:                                       ; preds = %entry, %for.cond26
80  %cell.0 = phi i32 [ 0, %entry ], [ %start, %for.cond26 ]
81  %cond.v = call i1 @cond()
82  br i1 %cond.v, label %if.then, label %while.end
83
84if.then:                                          ; preds = %while.cond
85  %start = add nsw i32 %cell.0, 1
86  br i1 %cond.v, label %for.cond11, label %for.cond26
87
88for.cond11:                                       ; preds = %for.cond11, %if.then
89  %i.1 = phi i32 [ %step, %for.cond11 ], [ %start, %if.then]
90  %cmp13 = icmp sle i32 %i.1, 99
91  %step = add nsw i32 %i.1, 1
92  br i1 %cmp13, label %for.cond11, label %while.end
93
94for.cond26:                                       ; preds = %if.then
95  br label %while.cond
96
97while.end:                                        ; preds = %while.cond, %for.cond11
98  ret void
99}
100