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