1; RUN: opt < %s -indvars -S | FileCheck %s 2 3; These tests ensure that we can compute the trip count of various forms of 4; loops. If the trip count of the loop is computable, then we will know what 5; the exit value of the loop will be for some value, allowing us to substitute 6; it directly into users outside of the loop, making the loop dead. 7 8; CHECK-LABEL: @linear_setne( 9; CHECK: ret i32 100 10 11define i32 @linear_setne() { 12entry: 13 br label %loop 14 15loop: ; preds = %loop, %entry 16 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 17 %i.next = add i32 %i, 1 ; <i32> [#uses=1] 18 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1] 19 br i1 %c, label %loop, label %loopexit 20 21loopexit: ; preds = %loop 22 ret i32 %i 23} 24 25; CHECK-LABEL: @linear_setne_2( 26; CHECK: ret i32 100 27 28define i32 @linear_setne_2() { 29entry: 30 br label %loop 31 32loop: ; preds = %loop, %entry 33 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 34 %i.next = add i32 %i, 2 ; <i32> [#uses=1] 35 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1] 36 br i1 %c, label %loop, label %loopexit 37 38loopexit: ; preds = %loop 39 ret i32 %i 40} 41 42; CHECK-LABEL: @linear_setne_overflow( 43; CHECK: ret i32 0 44 45define i32 @linear_setne_overflow() { 46entry: 47 br label %loop 48 49loop: ; preds = %loop, %entry 50 %i = phi i32 [ 1024, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 51 %i.next = add i32 %i, 1024 ; <i32> [#uses=1] 52 %c = icmp ne i32 %i, 0 ; <i1> [#uses=1] 53 br i1 %c, label %loop, label %loopexit 54 55loopexit: ; preds = %loop 56 ret i32 %i 57} 58 59; CHECK-LABEL: @linear_setlt( 60; CHECK: ret i32 100 61 62define i32 @linear_setlt() { 63entry: 64 br label %loop 65 66loop: ; preds = %loop, %entry 67 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 68 %i.next = add i32 %i, 1 ; <i32> [#uses=1] 69 %c = icmp slt i32 %i, 100 ; <i1> [#uses=1] 70 br i1 %c, label %loop, label %loopexit 71 72loopexit: ; preds = %loop 73 ret i32 %i 74} 75 76; CHECK-LABEL: @quadratic_setlt( 77; CHECK: ret i32 34 78 79define i32 @quadratic_setlt() { 80entry: 81 br label %loop 82 83loop: ; preds = %loop, %entry 84 %i = phi i32 [ 7, %entry ], [ %i.next, %loop ] ; <i32> [#uses=4] 85 %i.next = add i32 %i, 3 ; <i32> [#uses=1] 86 %i2 = mul i32 %i, %i ; <i32> [#uses=1] 87 %c = icmp slt i32 %i2, 1000 ; <i1> [#uses=1] 88 br i1 %c, label %loop, label %loopexit 89 90loopexit: ; preds = %loop 91 ret i32 %i 92} 93 94; CHECK-LABEL: @chained( 95; CHECK: ret i32 200 96 97define i32 @chained() { 98entry: 99 br label %loop 100 101loop: ; preds = %loop, %entry 102 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 103 %i.next = add i32 %i, 1 ; <i32> [#uses=1] 104 %c = icmp ne i32 %i, 100 ; <i1> [#uses=1] 105 br i1 %c, label %loop, label %loopexit 106 107loopexit: ; preds = %loop 108 br label %loop2 109 110loop2: ; preds = %loop2, %loopexit 111 %j = phi i32 [ %i, %loopexit ], [ %j.next, %loop2 ] ; <i32> [#uses=3] 112 %j.next = add i32 %j, 1 ; <i32> [#uses=1] 113 %c2 = icmp ne i32 %j, 200 ; <i1> [#uses=1] 114 br i1 %c2, label %loop2, label %loopexit2 115 116loopexit2: ; preds = %loop2 117 ret i32 %j 118} 119 120; CHECK-LABEL: @chained4( 121; CHECK: ret i32 400 122 123define i32 @chained4() { 124entry: 125 br label %loop 126 127loop: ; preds = %loop, %entry 128 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; <i32> [#uses=3] 129 %i.next = add i32 %i, 1 ; <i32> [#uses=1] 130 %c = icmp ne i32 %i.next, 100 ; <i1> [#uses=1] 131 br i1 %c, label %loop, label %loopexit 132 133loopexit: ; preds = %loop 134 br label %loop2 135 136loop2: ; preds = %loop2, %loopexit 137 %j = phi i32 [ %i.next, %loopexit ], [ %j.next, %loop2 ] ; <i32> [#uses=3] 138 %j.next = add i32 %j, 1 ; <i32> [#uses=1] 139 %c2 = icmp ne i32 %j.next, 200 ; <i1> [#uses=1] 140 br i1 %c2, label %loop2, label %loopexit2 141 142loopexit2: ; preds = %loop 143 br label %loop8 144 145loop8: ; preds = %loop2, %loopexit 146 %k = phi i32 [ %j.next, %loopexit2 ], [ %k.next, %loop8 ] ; <i32> [#uses=3] 147 %k.next = add i32 %k, 1 ; <i32> [#uses=1] 148 %c8 = icmp ne i32 %k.next, 300 ; <i1> [#uses=1] 149 br i1 %c8, label %loop8, label %loopexit8 150 151loopexit8: ; preds = %loop2 152 br label %loop9 153 154loop9: ; preds = %loop2, %loopexit 155 %l = phi i32 [ %k.next, %loopexit8 ], [ %l.next, %loop9 ] ; <i32> [#uses=3] 156 %l.next = add i32 %l, 1 ; <i32> [#uses=1] 157 %c9 = icmp ne i32 %l.next, 400 ; <i1> [#uses=1] 158 br i1 %c9, label %loop9, label %loopexit9 159 160loopexit9: ; preds = %loop2 161 ret i32 %l.next 162} 163 164; PR18449. Check that the early exit is reduced to never taken. 165; 166; CHECK-LABEL: @twoexit 167; CHECK-LABEL: loop: 168; CHECK: phi 169; CHECK: br i1 false 170; CHECK: br 171; CHECK: ret 172define void @twoexit() { 173"function top level": 174 br label %loop 175 176loop: ; preds = %body, %"function top level" 177 %0 = phi i64 [ 0, %"function top level" ], [ %2, %body ] 178 %1 = icmp ugt i64 %0, 2 179 br i1 %1, label %fail, label %body 180 181fail: ; preds = %loop 182 tail call void @bounds_fail() 183 unreachable 184 185body: ; preds = %loop 186 %2 = add i64 %0, 1 187 %3 = icmp slt i64 %2, 3 188 br i1 %3, label %loop, label %out 189 190out: ; preds = %body 191 ret void 192} 193declare void @bounds_fail() 194