1; RUN: opt -S -unroll-runtime -loop-unroll < %s | FileCheck %s
2
3target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
4
5;; Check that we don't emit expensive instructions to compute trip
6;; counts when unrolling loops.
7
8define i32 @test(i64 %v12, i8* %array, i64* %loc) {
9; CHECK-LABEL: @test(
10; CHECK-NOT: udiv
11entry:
12  %step = load i64, i64* %loc, !range !0
13  br label %loop
14
15loop:                                           ; preds = %entry, %loop
16  %k.015 = phi i64 [ %v15, %loop ], [ %v12, %entry ]
17  %v14 = getelementptr inbounds i8, i8* %array, i64 %k.015
18  store i8 0, i8* %v14
19  %v15 = add nuw nsw i64 %k.015, %step
20  %v16 = icmp slt i64 %v15, 8193
21  br i1 %v16, label %loop, label %loopexit
22
23loopexit:                             ; preds = %loop
24  ret i32 0
25}
26
27;; Though SCEV for loop tripcount contains division,
28;; it shouldn't be considered expensive, since the division already
29;; exists in the code and we don't need to expand it once more.
30;; Thus, it shouldn't prevent us from unrolling the loop.
31
32define i32 @test2(i64* %loc, i64 %conv7) {
33; CHECK-LABEL: @test2(
34; CHECK: udiv
35; CHECK: udiv
36; CHECK-NOT: udiv
37; CHECK-LABEL: for.body
38entry:
39  %rem0 = load i64, i64* %loc, align 8
40  %ExpensiveComputation = udiv i64 %rem0, 42 ; <<< Extra computations are added to the trip-count expression
41  br label %bb1
42bb1:
43  %div11 = udiv i64 %ExpensiveComputation, %conv7
44  %cmp.i38 = icmp ugt i64 %div11, 1
45  %div12 = select i1 %cmp.i38, i64 %div11, i64 1
46  br label %for.body
47for.body:
48  %rem1 = phi i64 [ %rem0, %bb1 ], [ %rem2, %for.body ]
49  %k1 = phi i64 [ %div12, %bb1 ], [ %dec, %for.body ]
50  %mul1 = mul i64 %rem1, 48271
51  %rem2 = urem i64 %mul1, 2147483647
52  %dec = add i64 %k1, -1
53  %cmp = icmp eq i64 %dec, 0
54  br i1 %cmp, label %exit, label %for.body
55exit:
56  %rem3 = phi i64 [ %rem2, %for.body ]
57  store i64 %rem3, i64* %loc, align 8
58  ret i32 0
59}
60
61!0 = !{i64 1, i64 100}
62