Link declaration lazily.
[oota-llvm.git] / test / Transforms / LoopUnroll / full-unroll-heuristics-2.ll
1 ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10  -unroll-percent-dynamic-cost-saved-threshold=50 -unroll-dynamic-cost-savings-discount=90 | FileCheck %s
2 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
3
4 @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
5 @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
6
7 ; Though @unknown_global is initialized with constant values, we can't consider
8 ; it as a constant, so we shouldn't unroll the loop.
9 ; CHECK-LABEL: @foo
10 ; CHECK: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @unknown_global, i64 0, i64 %iv
11 define i32 @foo(i32* noalias nocapture readonly %src) {
12 entry:
13   br label %loop
14
15 loop:                                                ; preds = %loop, %entry
16   %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
17   %r  = phi i32 [ 0, %entry ], [ %add, %loop ]
18   %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
19   %src_element = load i32, i32* %arrayidx, align 4
20   %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @unknown_global, i64 0, i64 %iv
21   %const_array_element = load i32, i32* %array_const_idx, align 4
22   %mul = mul nsw i32 %src_element, %const_array_element
23   %add = add nsw i32 %mul, %r
24   %inc = add nuw nsw i64 %iv, 1
25   %exitcond86.i = icmp eq i64 %inc, 9
26   br i1 %exitcond86.i, label %loop.end, label %loop
27
28 loop.end:                                            ; preds = %loop
29   %r.lcssa = phi i32 [ %r, %loop ]
30   ret i32 %r.lcssa
31 }
32
33 ; Similarly, we can't consider 'weak' symbols as a known constant value, so we
34 ; shouldn't unroll the loop.
35 ; CHECK-LABEL: @foo2
36 ; CHECK: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @weak_constant, i64 0, i64 %iv
37 define i32 @foo2(i32* noalias nocapture readonly %src) {
38 entry:
39   br label %loop
40
41 loop:                                                ; preds = %loop, %entry
42   %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
43   %r  = phi i32 [ 0, %entry ], [ %add, %loop ]
44   %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
45   %src_element = load i32, i32* %arrayidx, align 4
46   %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @weak_constant, i64 0, i64 %iv
47   %const_array_element = load i32, i32* %array_const_idx, align 4
48   %mul = mul nsw i32 %src_element, %const_array_element
49   %add = add nsw i32 %mul, %r
50   %inc = add nuw nsw i64 %iv, 1
51   %exitcond86.i = icmp eq i64 %inc, 9
52   br i1 %exitcond86.i, label %loop.end, label %loop
53
54 loop.end:                                            ; preds = %loop
55   %r.lcssa = phi i32 [ %r, %loop ]
56   ret i32 %r.lcssa
57 }