1 ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -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=1000 -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 @unknown_global = internal unnamed_addr global [9 x i32] [i32 0, i32 -1, i32 0, i32 -1, i32 5, i32 -1, i32 0, i32 -1, i32 0], align 16
6 @weak_constant = weak unnamed_addr constant [9 x i32] [i32 0, i32 -1, i32 0, i32 -1, i32 5, i32 -1, i32 0, i32 -1, i32 0], align 16
8 ; Though @unknown_global is initialized with constant values, we can't consider
9 ; it as a constant, so we shouldn't unroll the loop.
11 ; CHECK: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @unknown_global, i64 0, i64 %iv
12 define i32 @foo(i32* noalias nocapture readonly %src) {
16 loop: ; preds = %loop, %entry
17 %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
18 %r = phi i32 [ 0, %entry ], [ %add, %loop ]
19 %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
20 %src_element = load i32, i32* %arrayidx, align 4
21 %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @unknown_global, i64 0, i64 %iv
22 %const_array_element = load i32, i32* %array_const_idx, align 4
23 %mul = mul nsw i32 %src_element, %const_array_element
24 %add = add nsw i32 %mul, %r
25 %inc = add nuw nsw i64 %iv, 1
26 %exitcond86.i = icmp eq i64 %inc, 9
27 br i1 %exitcond86.i, label %loop.end, label %loop
29 loop.end: ; preds = %loop
30 %r.lcssa = phi i32 [ %r, %loop ]
34 ; Similarly, we can't consider 'weak' symbols as a known constant value, so we
35 ; shouldn't unroll the loop.
37 ; CHECK: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @weak_constant, i64 0, i64 %iv
38 define i32 @foo2(i32* noalias nocapture readonly %src) {
42 loop: ; preds = %loop, %entry
43 %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
44 %r = phi i32 [ 0, %entry ], [ %add, %loop ]
45 %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
46 %src_element = load i32, i32* %arrayidx, align 4
47 %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @weak_constant, i64 0, i64 %iv
48 %const_array_element = load i32, i32* %array_const_idx, align 4
49 %mul = mul nsw i32 %src_element, %const_array_element
50 %add = add nsw i32 %mul, %r
51 %inc = add nuw nsw i64 %iv, 1
52 %exitcond86.i = icmp eq i64 %inc, 9
53 br i1 %exitcond86.i, label %loop.end, label %loop
55 loop.end: ; preds = %loop
56 %r.lcssa = phi i32 [ %r, %loop ]
60 ; In this case the loaded value is used only to control branch.
61 ; If we missed that, we could've thought that it's unused and unrolling would
62 ; clean up almost entire loop. Make sure that we do not unroll such loop.
64 ; CHECK: br i1 %exitcond, label %loop.end, label %loop.header
65 define i32 @foo3(i32* noalias nocapture readonly %src) {
70 %iv = phi i64 [ 0, %entry ], [ %inc, %loop.latch ]
71 %r1 = phi i32 [ 0, %entry ], [ %r3, %loop.latch ]
72 %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
73 %src_element = load i32, i32* %arrayidx, align 4
74 %cmp = icmp eq i32 0, %src_element
75 br i1 %cmp, label %loop.if, label %loop.latch
82 %r3 = phi i32 [%r1, %loop.header], [%r2, %loop.if]
83 %inc = add nuw nsw i64 %iv, 1
84 %exitcond = icmp eq i64 %inc, 9
85 br i1 %exitcond, label %loop.end, label %loop.header
88 %r.lcssa = phi i32 [ %r3, %loop.latch ]