Max Kazantsev | 751579c | 2017-04-17 09:52:02 +0000 | [diff] [blame] | 1 | ; RUN: opt < %s -S -loop-unroll -unroll-threshold=30 | FileCheck %s |
Sanjoy Das | 664c925 | 2017-03-03 18:19:15 +0000 | [diff] [blame] | 2 | |
| 3 | define i32 @invariant_backedge_1(i32 %a, i32 %b) { |
| 4 | ; CHECK-LABEL: @invariant_backedge_1 |
| 5 | ; CHECK-NOT: %plus = phi |
| 6 | ; CHECK: loop.peel: |
| 7 | ; CHECK: loop: |
| 8 | ; CHECK: %i = phi |
| 9 | ; CHECK: %sum = phi |
| 10 | entry: |
| 11 | br label %loop |
| 12 | |
| 13 | loop: |
| 14 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 15 | %sum = phi i32 [ 0, %entry ], [ %incsum, %loop ] |
| 16 | %plus = phi i32 [ %a, %entry ], [ %b, %loop ] |
| 17 | |
| 18 | %incsum = add i32 %sum, %plus |
| 19 | %inc = add i32 %i, 1 |
| 20 | %cmp = icmp slt i32 %i, 1000 |
Sanjoy Das | 30c3538 | 2017-03-07 06:03:15 +0000 | [diff] [blame] | 21 | |
| 22 | br i1 %cmp, label %loop, label %exit |
| 23 | |
| 24 | exit: |
| 25 | ret i32 %sum |
| 26 | } |
| 27 | |
Sanjoy Das | 30c3538 | 2017-03-07 06:03:15 +0000 | [diff] [blame] | 28 | define i32 @invariant_backedge_2(i32 %a, i32 %b) { |
Max Kazantsev | 751579c | 2017-04-17 09:52:02 +0000 | [diff] [blame] | 29 | ; This loop should be peeled twice because it has a Phi which becomes invariant |
| 30 | ; starting from 3rd iteration. |
Sanjoy Das | 30c3538 | 2017-03-07 06:03:15 +0000 | [diff] [blame] | 31 | ; CHECK-LABEL: @invariant_backedge_2 |
Max Kazantsev | 751579c | 2017-04-17 09:52:02 +0000 | [diff] [blame] | 32 | ; CHECK: loop.peel{{.*}}: |
| 33 | ; CHECK: loop.peel{{.*}}: |
| 34 | ; CHECK: %i = phi |
| 35 | ; CHECK: %sum = phi |
| 36 | ; CHECK-NOT: %half.inv = phi |
| 37 | ; CHECK-NOT: %plus = phi |
| 38 | entry: |
| 39 | br label %loop |
| 40 | |
| 41 | loop: |
| 42 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 43 | %sum = phi i32 [ 0, %entry ], [ %incsum, %loop ] |
| 44 | %half.inv = phi i32 [ %a, %entry ], [ %b, %loop ] |
| 45 | %plus = phi i32 [ %a, %entry ], [ %half.inv, %loop ] |
| 46 | |
| 47 | %incsum = add i32 %sum, %plus |
| 48 | %inc = add i32 %i, 1 |
| 49 | %cmp = icmp slt i32 %i, 1000 |
| 50 | |
| 51 | br i1 %cmp, label %loop, label %exit |
| 52 | |
| 53 | exit: |
| 54 | ret i32 %sum |
| 55 | } |
| 56 | |
| 57 | define i32 @invariant_backedge_3(i32 %a, i32 %b) { |
| 58 | ; This loop should be peeled thrice because it has a Phi which becomes invariant |
| 59 | ; starting from 4th iteration. |
| 60 | ; CHECK-LABEL: @invariant_backedge_3 |
| 61 | ; CHECK: loop.peel{{.*}}: |
| 62 | ; CHECK: loop.peel{{.*}}: |
| 63 | ; CHECK: loop.peel{{.*}}: |
| 64 | ; CHECK: %i = phi |
| 65 | ; CHECK: %sum = phi |
| 66 | ; CHECK-NOT: %half.inv = phi |
| 67 | ; CHECK-NOT: %half.inv.2 = phi |
| 68 | ; CHECK-NOT: %plus = phi |
| 69 | entry: |
| 70 | br label %loop |
| 71 | |
| 72 | loop: |
| 73 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 74 | %sum = phi i32 [ 0, %entry ], [ %incsum, %loop ] |
| 75 | %half.inv = phi i32 [ %a, %entry ], [ %b, %loop ] |
| 76 | %half.inv.2 = phi i32 [ %a, %entry ], [ %half.inv, %loop ] |
| 77 | %plus = phi i32 [ %a, %entry ], [ %half.inv.2, %loop ] |
| 78 | |
| 79 | %incsum = add i32 %sum, %plus |
| 80 | %inc = add i32 %i, 1 |
| 81 | %cmp = icmp slt i32 %i, 1000 |
| 82 | |
| 83 | br i1 %cmp, label %loop, label %exit |
| 84 | |
| 85 | exit: |
| 86 | ret i32 %sum |
| 87 | } |
| 88 | |
| 89 | define i32 @invariant_backedge_limited_by_size(i32 %a, i32 %b) { |
| 90 | ; This loop should normally be peeled thrice because it has a Phi which becomes |
| 91 | ; invariant starting from 4th iteration, but the size of the loop only allows |
| 92 | ; us to peel twice because we are restricted to 30 instructions in resulting |
| 93 | ; code. Thus, %plus Phi node should stay in loop even despite its backedge |
| 94 | ; input is an invariant. |
| 95 | ; CHECK-LABEL: @invariant_backedge_limited_by_size |
| 96 | ; CHECK: loop.peel{{.*}}: |
| 97 | ; CHECK: loop.peel{{.*}}: |
| 98 | ; CHECK: %i = phi |
| 99 | ; CHECK: %sum = phi |
| 100 | ; CHECK: %plus = phi i32 [ %a, {{.*}} ], [ %b, %loop ] |
| 101 | ; CHECK-NOT: %half.inv = phi |
| 102 | ; CHECK-NOT: %half.inv.2 = phi |
| 103 | entry: |
| 104 | br label %loop |
| 105 | |
| 106 | loop: |
| 107 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 108 | %sum = phi i32 [ 0, %entry ], [ %incsum, %loop ] |
| 109 | %half.inv = phi i32 [ %a, %entry ], [ %b, %loop ] |
| 110 | %half.inv.2 = phi i32 [ %a, %entry ], [ %half.inv, %loop ] |
| 111 | %plus = phi i32 [ %a, %entry ], [ %half.inv.2, %loop ] |
| 112 | |
| 113 | %incsum = add i32 %sum, %plus |
| 114 | %inc = add i32 %i, 1 |
| 115 | %cmp = icmp slt i32 %i, 1000 |
| 116 | |
| 117 | %incsum2 = add i32 %incsum, %plus |
| 118 | %incsum3 = add i32 %incsum, %plus |
| 119 | %incsum4 = add i32 %incsum, %plus |
| 120 | %incsum5 = add i32 %incsum, %plus |
| 121 | %incsum6 = add i32 %incsum, %plus |
| 122 | %incsum7 = add i32 %incsum, %plus |
| 123 | |
| 124 | br i1 %cmp, label %loop, label %exit |
| 125 | |
| 126 | exit: |
| 127 | ret i32 %sum |
| 128 | } |
| 129 | |
| 130 | ; Peeling should fail due to method size. |
| 131 | define i32 @invariant_backedge_negative(i32 %a, i32 %b) { |
| 132 | ; CHECK-LABEL: @invariant_backedge_negative |
| 133 | ; CHECK-NOT: loop.peel{{.*}}: |
Sanjoy Das | 30c3538 | 2017-03-07 06:03:15 +0000 | [diff] [blame] | 134 | ; CHECK: loop: |
| 135 | ; CHECK: %i = phi |
| 136 | ; CHECK: %sum = phi |
| 137 | ; CHECK: %plus = phi |
| 138 | entry: |
| 139 | br label %loop |
| 140 | |
| 141 | loop: |
| 142 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 143 | %sum = phi i32 [ 0, %entry ], [ %incsum2, %loop ] |
| 144 | %plus = phi i32 [ %a, %entry ], [ %b, %loop ] |
| 145 | |
| 146 | %incsum = add i32 %sum, %plus |
| 147 | %incsum2 = add i32 %incsum, %plus |
Max Kazantsev | 751579c | 2017-04-17 09:52:02 +0000 | [diff] [blame] | 148 | %incsum3 = add i32 %incsum, %plus |
| 149 | %incsum4 = add i32 %incsum, %plus |
| 150 | %incsum5 = add i32 %incsum, %plus |
| 151 | %incsum6 = add i32 %incsum, %plus |
| 152 | %incsum7 = add i32 %incsum, %plus |
| 153 | %incsum8 = add i32 %incsum, %plus |
| 154 | %incsum9 = add i32 %incsum, %plus |
| 155 | %incsum10 = add i32 %incsum, %plus |
| 156 | %incsum11 = add i32 %incsum, %plus |
| 157 | %incsum12 = add i32 %incsum, %plus |
| 158 | %incsum13 = add i32 %incsum, %plus |
| 159 | %incsum14 = add i32 %incsum, %plus |
| 160 | %incsum15 = add i32 %incsum, %plus |
| 161 | %inc = add i32 %i, 1 |
| 162 | %cmp = icmp slt i32 %i, 1000 |
| 163 | |
| 164 | br i1 %cmp, label %loop, label %exit |
| 165 | |
| 166 | exit: |
| 167 | ret i32 %sum |
| 168 | } |
| 169 | |
| 170 | define i32 @cycled_phis(i32 %a, i32 %b) { |
| 171 | ; Make sure that we do not crash working with cycled Phis and don't peel it. |
| 172 | ; TODO: Actually this loop should be partially unrolled with factor 2. |
| 173 | ; CHECK-LABEL: @cycled_phis |
| 174 | ; CHECK-NOT: loop.peel{{.*}}: |
| 175 | ; CHECK: loop: |
| 176 | ; CHECK: %i = phi |
| 177 | ; CHECK: %phi.a = phi |
| 178 | ; CHECK: %phi.b = phi |
| 179 | ; CHECK: %sum = phi |
| 180 | entry: |
| 181 | br label %loop |
| 182 | |
| 183 | loop: |
| 184 | %i = phi i32 [ 0, %entry ], [ %inc, %loop ] |
| 185 | %phi.a = phi i32 [ %a, %entry ], [ %phi.b, %loop ] |
| 186 | %phi.b = phi i32 [ %b, %entry ], [ %phi.a, %loop ] |
| 187 | %sum = phi i32 [ 0, %entry], [ %incsum, %loop ] |
| 188 | %incsum = add i32 %sum, %phi.a |
Sanjoy Das | 30c3538 | 2017-03-07 06:03:15 +0000 | [diff] [blame] | 189 | %inc = add i32 %i, 1 |
| 190 | %cmp = icmp slt i32 %i, 1000 |
| 191 | |
Sanjoy Das | 664c925 | 2017-03-03 18:19:15 +0000 | [diff] [blame] | 192 | br i1 %cmp, label %loop, label %exit |
| 193 | |
| 194 | exit: |
| 195 | ret i32 %sum |
| 196 | } |