|
| 1 | +; Check that the loop body frequency is maintained when LoopPeel both: |
| 2 | +; - Peels one unconditional iteration after the loop. |
| 3 | +; - Does not add a guard to sometimes skip the remaining loop because it has |
| 4 | +; proven the original loop always executes at least two iterations, which |
| 5 | +; become the initial iteration and the peeled iteration. |
| 6 | + |
| 7 | +; DEFINE: %{exitWeight} = |
| 8 | +; DEFINE: %{loopWeight} = |
| 9 | +; DEFINE: %{loopFreqOld} = |
| 10 | +; DEFINE: %{loopFreqNew} = |
| 11 | + |
| 12 | +; DEFINE: %{run} = \ |
| 13 | +; DEFINE: cp %s %t.ll && chmod +w %t.ll && \ |
| 14 | +; DEFINE: echo '!0 = !{!"branch_weights", i32 %{exitWeight}, ' \ |
| 15 | +; DEFINE: 'i32 %{loopWeight}}' >> %t.ll && \ |
| 16 | +; DEFINE: opt -p "print<block-freq>,loop-unroll,print<block-freq>" \ |
| 17 | +; DEFINE: -unroll-full-max-count=0 -S %t.ll 2>&1 | \ |
| 18 | +; DEFINE: FileCheck -DLOOP_FREQ_OLD='%{loopFreqOld}' \ |
| 19 | +; DEFINE: -DLOOP_FREQ_NEW='%{loopFreqNew}' %s |
| 20 | + |
| 21 | +; Branch weights give the original loop 10 iterations. We expect that |
| 22 | +; loopFreqOld = loopFreqNew + 1. |
| 23 | +; REDEFINE: %{exitWeight} = 1 |
| 24 | +; REDEFINE: %{loopWeight} = 9 |
| 25 | +; REDEFINE: %{loopFreqOld} = 10.0 |
| 26 | +; REDEFINE: %{loopFreqNew} = 9.0 |
| 27 | +; RUN: %{run} |
| 28 | + |
| 29 | +; Branch weights give the original loop 2 iterations. We expect that |
| 30 | +; loopFreqOld = loopFreqNew + 1. |
| 31 | +; REDEFINE: %{exitWeight} = 1 |
| 32 | +; REDEFINE: %{loopWeight} = 1 |
| 33 | +; REDEFINE: %{loopFreqOld} = 2.0 |
| 34 | +; REDEFINE: %{loopFreqNew} = 1.0 |
| 35 | +; RUN: %{run} |
| 36 | + |
| 37 | +; Branch weights give the original loop 1 iteration, but LoopPeel proved it has |
| 38 | +; at least 2. There is no loop probability that produces a frequency below 1, |
| 39 | +; so the original total frequency cannot be maintained. |
| 40 | +; REDEFINE: %{exitWeight} = 1 |
| 41 | +; REDEFINE: %{loopWeight} = 0 |
| 42 | +; REDEFINE: %{loopFreqOld} = 1.0 |
| 43 | +; REDEFINE: %{loopFreqNew} = 1.0 |
| 44 | +; RUN: %{run} |
| 45 | + |
| 46 | +; Branch weights say the original loop is infinite, maximizing the frequency, |
| 47 | +; so LoopPeel does not try to decrement it. |
| 48 | +; REDEFINE: %{exitWeight} = 0 |
| 49 | +; REDEFINE: %{loopWeight} = 1 |
| 50 | +; REDEFINE: %{loopFreqOld} = 2147483647.8 |
| 51 | +; REDEFINE: %{loopFreqNew} = 2147483647.8 |
| 52 | +; RUN: %{run} |
| 53 | + |
| 54 | +; Everything other than loop should be 1.0 because it is reached once. |
| 55 | +; |
| 56 | +; CHECK: block-frequency-info: test |
| 57 | +; CHECK-NEXT: - entry: float = 1.0, |
| 58 | +; CHECK-NEXT: - loop: float = [[LOOP_FREQ_OLD]], |
| 59 | +; CHECK-NEXT: - exit: float = 1.0, |
| 60 | +; |
| 61 | +; CHECK: block-frequency-info: test |
| 62 | +; CHECK-NEXT: - entry: float = 1.0, |
| 63 | +; CHECK-NEXT: - loop: float = [[LOOP_FREQ_NEW]], |
| 64 | +; CHECK-NEXT: - exit.peel.begin: float = 1.0, |
| 65 | +; CHECK-NEXT: - loop.peel: float = 1.0, |
| 66 | +; CHECK-NEXT: - exit.peel.next: float = 1.0, |
| 67 | +; CHECK-NEXT: - loop.peel.next: float = 1.0, |
| 68 | +; CHECK-NEXT: - exit: float = 1.0, |
| 69 | + |
| 70 | +declare void @f(i32) |
| 71 | + |
| 72 | +define void @test() { |
| 73 | +entry: |
| 74 | + br label %loop |
| 75 | + |
| 76 | +loop: |
| 77 | + %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 78 | + %isLast = icmp eq i32 %i, 20 |
| 79 | + %sel = select i1 %isLast, i32 1, i32 0 |
| 80 | + call void @f(i32 %sel) |
| 81 | + %inc = add i32 %i, 1 |
| 82 | + %isLast1 = icmp eq i32 %i, 20 |
| 83 | + br i1 %isLast1, label %exit, label %loop, !prof !0 |
| 84 | + |
| 85 | +exit: |
| 86 | + ret void |
| 87 | +} |
0 commit comments