• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10  -unroll-percent-dynamic-cost-saved-threshold=70 -unroll-dynamic-cost-savings-discount=90 | FileCheck %s
2target 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
11define i32 @foo(i32* noalias nocapture readonly %src) {
12entry:
13  br label %loop
14
15loop:                                                ; 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
28loop.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
37define i32 @foo2(i32* noalias nocapture readonly %src) {
38entry:
39  br label %loop
40
41loop:                                                ; 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
54loop.end:                                            ; preds = %loop
55  %r.lcssa = phi i32 [ %r, %loop ]
56  ret i32 %r.lcssa
57}
58
59; In this case the loaded value is used only to control branch.
60; If we missed that, we could've thought that it's unused and unrolling would
61; clean up almost entire loop. Make sure that we do not unroll such loop.
62; CHECK-LABEL: @foo3
63; CHECK: br i1 %exitcond, label %loop.end, label %loop.header
64define i32 @foo3(i32* noalias nocapture readonly %src) {
65entry:
66  br label %loop.header
67
68loop.header:
69  %iv = phi i64 [ 0, %entry ], [ %inc, %loop.latch ]
70  %r1  = phi i32 [ 0, %entry ], [ %r3, %loop.latch ]
71  %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
72  %src_element = load i32, i32* %arrayidx, align 4
73  %cmp = icmp eq i32 0, %src_element
74  br i1 %cmp, label %loop.if, label %loop.latch
75
76loop.if:
77  %r2 = add i32 %r1, 1
78  br label %loop.latch
79
80loop.latch:
81  %r3 = phi i32 [%r1, %loop.header], [%r2, %loop.if]
82  %inc = add nuw nsw i64 %iv, 1
83  %exitcond = icmp eq i64 %inc, 9
84  br i1 %exitcond, label %loop.end, label %loop.header
85
86loop.end:
87  %r.lcssa = phi i32 [ %r3, %loop.latch ]
88  ret i32 %r.lcssa
89}
90