1; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s
2; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(loop-unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s
3target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
4
5@known_constant = internal unnamed_addr constant [10 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1], align 16
6
7; If we can figure out result of comparison on each iteration, we can resolve
8; the depending branch. That means, that the unrolled version of the loop would
9; have less code, because we don't need not-taken basic blocks there.
10; This test checks that this is taken into consideration.
11; We expect this loop to be unrolled, because the most complicated part of its
12; body (if.then block) is never actually executed.
13; CHECK-LABEL: @branch_folded
14; CHECK-NOT: br i1 %
15; CHECK: ret i32
16define i32 @branch_folded(i32* noalias nocapture readonly %b) {
17entry:
18  br label %for.body
19
20for.body:                                         ; preds = %for.inc, %entry
21  %iv.0 = phi i64 [ 0, %entry ], [ %iv.1, %for.inc ]
22  %r.0 = phi i32 [ 0, %entry ], [ %r.1, %for.inc ]
23  %arrayidx1 = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv.0
24  %x1 = load i32, i32* %arrayidx1, align 4
25  %cmp = icmp eq i32 %x1, 0
26  %iv.1 = add nuw nsw i64 %iv.0, 1
27  br i1 %cmp, label %if.then, label %for.inc
28
29if.then:                                          ; preds = %for.body
30  %arrayidx2 = getelementptr inbounds i32, i32* %b, i64 %iv.0
31  %x2 = load i32, i32* %arrayidx2, align 4
32  %add = add nsw i32 %x2, %r.0
33  br label %for.inc
34
35for.inc:                                          ; preds = %for.body, %if.then
36  %r.1 = phi i32 [ %add, %if.then ], [ %x1, %for.body ]
37  %exitcond = icmp eq i64 %iv.1, 10
38  br i1 %exitcond, label %for.end, label %for.body
39
40for.end:                                          ; preds = %for.inc
41  ret i32 %r.1
42}
43
44; Check that we don't crash when we analyze icmp with pointer-typed IV and a
45; pointer.
46; CHECK-LABEL: @ptr_cmp_crash
47; CHECK:   ret void
48define void @ptr_cmp_crash() {
49entry:
50  br label %while.body
51
52while.body:
53  %iv.0 = phi i32* [ getelementptr inbounds ([10 x i32], [10 x i32]* @known_constant, i64 0, i64 0), %entry ], [ %iv.1, %while.body ]
54  %iv.1 = getelementptr inbounds i32, i32* %iv.0, i64 1
55  %exitcond = icmp eq i32* %iv.1, getelementptr inbounds ([10 x i32], [10 x i32]* @known_constant, i64 0, i64 9)
56  br i1 %exitcond, label %loop.exit, label %while.body
57
58loop.exit:
59  ret void
60}
61
62; Check that we don't crash when we analyze ptrtoint cast.
63; CHECK-LABEL: @ptrtoint_cast_crash
64; CHECK:   ret void
65define void @ptrtoint_cast_crash(i8 * %a) {
66entry:
67  %limit = getelementptr i8, i8* %a, i64 512
68  br label %loop.body
69
70loop.body:
71  %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]
72  %cast = ptrtoint i8* %iv.0 to i64
73  %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1
74  %exitcond = icmp ne i8* %iv.1, %limit
75  br i1 %exitcond, label %loop.body, label %loop.exit
76
77loop.exit:
78  ret void
79}
80