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(unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s
3 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
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
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
16 define i32 @branch_folded(i32* noalias nocapture readonly %b) {
20 for.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
29 if.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
35 for.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
40 for.end: ; preds = %for.inc
44 ; Check that we don't crash when we analyze icmp with pointer-typed IV and a
46 ; CHECK-LABEL: @ptr_cmp_crash
48 define void @ptr_cmp_crash() {
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
62 ; Check that we don't crash when we analyze ptrtoint cast.
63 ; CHECK-LABEL: @ptrtoint_cast_crash
65 define void @ptrtoint_cast_crash(i8 * %a) {
67 %limit = getelementptr i8, i8* %a, i64 512
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