xref: /aosp_15_r20/external/llvm/test/Transforms/LoopUnroll/full-unroll-heuristics.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; In this test we check how heuristics for complete unrolling work. We have
2*9880d681SAndroid Build Coastguard Worker; three knobs:
3*9880d681SAndroid Build Coastguard Worker;  1) -unroll-threshold
4*9880d681SAndroid Build Coastguard Worker;  3) -unroll-percent-dynamic-cost-saved-threshold and
5*9880d681SAndroid Build Coastguard Worker;  2) -unroll-dynamic-cost-savings-discount
6*9880d681SAndroid Build Coastguard Worker;
7*9880d681SAndroid Build Coastguard Worker; They control loop-unrolling according to the following rules:
8*9880d681SAndroid Build Coastguard Worker;  * If size of unrolled loop exceeds the absoulte threshold, we don't unroll
9*9880d681SAndroid Build Coastguard Worker;    this loop under any circumstances.
10*9880d681SAndroid Build Coastguard Worker;  * If size of unrolled loop is below the '-unroll-threshold', then we'll
11*9880d681SAndroid Build Coastguard Worker;    consider this loop as a very small one, and completely unroll it.
12*9880d681SAndroid Build Coastguard Worker;  * If a loop size is between these two tresholds, we only do complete unroll
13*9880d681SAndroid Build Coastguard Worker;    it if estimated number of potentially optimized instructions is high (we
14*9880d681SAndroid Build Coastguard Worker;    specify the minimal percent of such instructions).
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker; In this particular test-case, complete unrolling will allow later
17*9880d681SAndroid Build Coastguard Worker; optimizations to remove ~55% of the instructions, the loop body size is 9,
18*9880d681SAndroid Build Coastguard Worker; and unrolled size is 65.
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10  -unroll-percent-dynamic-cost-saved-threshold=20 -unroll-dynamic-cost-savings-discount=0 | FileCheck %s -check-prefix=TEST1
21*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10  -unroll-percent-dynamic-cost-saved-threshold=20 -unroll-dynamic-cost-savings-discount=90 | FileCheck %s -check-prefix=TEST2
22*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10  -unroll-percent-dynamic-cost-saved-threshold=80 -unroll-dynamic-cost-savings-discount=90 | FileCheck %s -check-prefix=TEST3
23*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=100 -unroll-percent-dynamic-cost-saved-threshold=80 -unroll-dynamic-cost-savings-discount=0 | FileCheck %s -check-prefix=TEST4
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker; If the absolute threshold is too low, or if we can't optimize away requested
26*9880d681SAndroid Build Coastguard Worker; percent of instructions, we shouldn't unroll:
27*9880d681SAndroid Build Coastguard Worker; TEST1: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv
28*9880d681SAndroid Build Coastguard Worker; TEST3: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker; Otherwise, we should:
31*9880d681SAndroid Build Coastguard Worker; TEST2-NOT: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker; Also, we should unroll if the 'unroll-threshold' is big enough:
34*9880d681SAndroid Build Coastguard Worker; TEST4-NOT: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker; And check that we don't crash when we're not allowed to do any analysis.
37*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -loop-unroll -unroll-max-iteration-count-to-analyze=0 -disable-output
38*9880d681SAndroid Build Coastguard Workertarget datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker@known_constant = internal 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
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Workerdefine i32 @foo(i32* noalias nocapture readonly %src) {
43*9880d681SAndroid Build Coastguard Workerentry:
44*9880d681SAndroid Build Coastguard Worker  br label %loop
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workerloop:                                                ; preds = %loop, %entry
47*9880d681SAndroid Build Coastguard Worker  %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
48*9880d681SAndroid Build Coastguard Worker  %r  = phi i32 [ 0, %entry ], [ %add, %loop ]
49*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
50*9880d681SAndroid Build Coastguard Worker  %src_element = load i32, i32* %arrayidx, align 4
51*9880d681SAndroid Build Coastguard Worker  %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv
52*9880d681SAndroid Build Coastguard Worker  %const_array_element = load i32, i32* %array_const_idx, align 4
53*9880d681SAndroid Build Coastguard Worker  %mul = mul nsw i32 %src_element, %const_array_element
54*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %mul, %r
55*9880d681SAndroid Build Coastguard Worker  %inc = add nuw nsw i64 %iv, 1
56*9880d681SAndroid Build Coastguard Worker  %exitcond86.i = icmp eq i64 %inc, 9
57*9880d681SAndroid Build Coastguard Worker  br i1 %exitcond86.i, label %loop.end, label %loop
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Workerloop.end:                                            ; preds = %loop
60*9880d681SAndroid Build Coastguard Worker  %r.lcssa = phi i32 [ %r, %loop ]
61*9880d681SAndroid Build Coastguard Worker  ret i32 %r.lcssa
62*9880d681SAndroid Build Coastguard Worker}
63