| OLD | NEW |
| 1 ; RUN: opt < %s -S -indvars -loop-unroll -verify-loop-info | FileCheck %s | 1 ; RUN: opt < %s -S -indvars -loop-unroll -verify-loop-info | FileCheck %s |
| 2 ; | 2 ; |
| 3 ; Unit tests for loop unrolling using ScalarEvolution to compute trip counts. | 3 ; Unit tests for loop unrolling using ScalarEvolution to compute trip counts. |
| 4 ; | 4 ; |
| 5 ; Indvars is run first to generate an "old" SCEV result. Some unit | 5 ; Indvars is run first to generate an "old" SCEV result. Some unit |
| 6 ; tests may check that SCEV is properly invalidated between passes. | 6 ; tests may check that SCEV is properly invalidated between passes. |
| 7 | 7 |
| 8 ; Completely unroll loops without a canonical IV. | 8 ; Completely unroll loops without a canonical IV. |
| 9 ; | 9 ; |
| 10 ; CHECK: @sansCanonical | 10 ; CHECK: @sansCanonical |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 exit1: | 60 exit1: |
| 61 ret i64 %s | 61 ret i64 %s |
| 62 | 62 |
| 63 exit2: | 63 exit2: |
| 64 ret i64 %s.next | 64 ret i64 %s.next |
| 65 } | 65 } |
| 66 | 66 |
| 67 ; SCEV properly unrolls multi-exit loops. | 67 ; SCEV properly unrolls multi-exit loops. |
| 68 ; | 68 ; |
| 69 ; SCEV cannot currently unroll this loop. | |
| 70 ; It should ideally detect a trip count of 5. | |
| 71 ; rdar:14038809 [SCEV]: Optimize trip count computation for multi-exit loops. | |
| 72 ; CHECK: @multiExit | 69 ; CHECK: @multiExit |
| 73 ; CHECKFIXME: getelementptr i32* %base, i32 10 | 70 ; CHECK: getelementptr i32* %base, i32 10 |
| 74 ; CHECKFIXME-NEXT: load i32* | 71 ; CHECK-NEXT: load i32* |
| 75 ; CHECKFIXME: br i1 false, label %l2.10, label %exit1 | 72 ; CHECK: br i1 false, label %l2.10, label %exit1 |
| 76 ; CHECKFIXME: l2.10: | 73 ; CHECK: l2.10: |
| 77 ; CHECKFIXME-NOT: br | 74 ; CHECK-NOT: br |
| 78 ; CHECKFIXME: ret i32 | 75 ; CHECK: ret i32 |
| 79 define i32 @multiExit(i32* %base) nounwind { | 76 define i32 @multiExit(i32* %base) nounwind { |
| 80 entry: | 77 entry: |
| 81 br label %l1 | 78 br label %l1 |
| 82 l1: | 79 l1: |
| 83 %iv1 = phi i32 [ 0, %entry ], [ %inc1, %l2 ] | 80 %iv1 = phi i32 [ 0, %entry ], [ %inc1, %l2 ] |
| 84 %iv2 = phi i32 [ 0, %entry ], [ %inc2, %l2 ] | 81 %iv2 = phi i32 [ 0, %entry ], [ %inc2, %l2 ] |
| 85 %inc1 = add i32 %iv1, 1 | 82 %inc1 = add i32 %iv1, 1 |
| 86 %inc2 = add i32 %iv2, 1 | 83 %inc2 = add i32 %iv2, 1 |
| 87 %adr = getelementptr i32* %base, i32 %iv1 | 84 %adr = getelementptr i32* %base, i32 %iv1 |
| 88 %val = load i32* %adr | 85 %val = load i32* %adr |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 %inc76 = add nsw i32 %i.113, 1 | 163 %inc76 = add nsw i32 %i.113, 1 |
| 167 br i1 false, label %for.body38, label %for.body87.preheader | 164 br i1 false, label %for.body38, label %for.body87.preheader |
| 168 | 165 |
| 169 for.body87.preheader: | 166 for.body87.preheader: |
| 170 br label %for.body87 | 167 br label %for.body87 |
| 171 | 168 |
| 172 for.body87: | 169 for.body87: |
| 173 br label %for.body87 | 170 br label %for.body87 |
| 174 } | 171 } |
| 175 | 172 |
| 176 ; PR16130: clang produces incorrect code with loop/expression at -O2 | |
| 177 ; rdar:14036816 loop-unroll makes assumptions about undefined behavior | |
| 178 ; | |
| 179 ; The loop latch is assumed to exit after the first iteration because | |
| 180 ; of the induction variable's NSW flag. However, the loop latch's | |
| 181 ; equality test is skipped and the loop exits after the second | |
| 182 ; iteration via the early exit. So loop unrolling cannot assume that | |
| 183 ; the loop latch's exit count of zero is an upper bound on the number | |
| 184 ; of iterations. | |
| 185 ; | |
| 186 ; CHECK: @nsw_latch | |
| 187 ; CHECK: for.body: | |
| 188 ; CHECK: %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ] | |
| 189 ; CHECK: return: | |
| 190 ; CHECK: %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ] | |
| 191 define void @nsw_latch(i32* %a) nounwind { | |
| 192 entry: | |
| 193 br label %for.body | |
| 194 | |
| 195 for.body: ; preds = %for.cond, %entry | |
| 196 %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ] | |
| 197 %tobool = icmp eq i32 %b.03, 0 | |
| 198 %add = add nsw i32 %b.03, 8 | |
| 199 br i1 %tobool, label %for.cond, label %return | |
| 200 | |
| 201 for.cond: ; preds = %for.body | |
| 202 %cmp = icmp eq i32 %add, 13 | |
| 203 br i1 %cmp, label %return, label %for.body | |
| 204 | |
| 205 return: ; preds = %for.body, %for.cond | |
| 206 %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ] | |
| 207 %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ] | |
| 208 store i32 %b.03.lcssa, i32* %a, align 4 | |
| 209 ret void | |
| 210 } | |
| OLD | NEW |