|
| 1 | +; RUN: opt < %s -instcombine -S | FileCheck %s |
| 2 | + |
| 3 | +; Induction variable is known to be non-negative |
| 4 | +; when its initial value is non-negative and |
| 5 | +; increments by non-negative value |
| 6 | +define i32 @test_indvar_nonnegative_add() { |
| 7 | +; CHECK-LABEL: @test_indvar_nonnegative_add( |
| 8 | +; CHECK: br i1 true, label %for.end, label %for.body |
| 9 | +entry: |
| 10 | + br label %for.body |
| 11 | + |
| 12 | +for.body: |
| 13 | + %i = phi i32 [0, %entry], [%inc, %for.body] |
| 14 | + %inc = add nsw i32 %i, 1 |
| 15 | + %cmp = icmp sge i32 %i, 0 |
| 16 | + br i1 %cmp, label %for.end, label %for.body |
| 17 | + |
| 18 | +for.end: |
| 19 | + ret i32 %i |
| 20 | +} |
| 21 | + |
| 22 | +; Induction variable is known to be non-negative |
| 23 | +; when its initial value is non-negative and |
| 24 | +; is multiplied by a non-negative value in each |
| 25 | +; iteration |
| 26 | +define i32 @test_indvar_nonnegative_mul() { |
| 27 | +; CHECK-LABEL: @test_indvar_nonnegative_mul( |
| 28 | +; CHECK: br i1 true, label %for.end, label %for.body |
| 29 | +entry: |
| 30 | + br label %for.body |
| 31 | + |
| 32 | +for.body: |
| 33 | + %i = phi i32 [1, %entry], [%inc, %for.body] |
| 34 | + %inc = mul nsw i32 %i, 3 |
| 35 | + %cmp = icmp sge i32 %i, 0 |
| 36 | + br i1 %cmp, label %for.end, label %for.body |
| 37 | + |
| 38 | +for.end: |
| 39 | + ret i32 %i |
| 40 | +} |
| 41 | + |
| 42 | +; Induction variable is known to be non-negative, |
| 43 | +; Similar to add |
| 44 | +define i32 @test_indvar_nonnegative_sub(i32 %a) { |
| 45 | +; CHECK-LABEL: @test_indvar_nonnegative_sub( |
| 46 | +; CHECK: br i1 true, label %for.end, label %for.body |
| 47 | +entry: |
| 48 | + br label %for.body |
| 49 | + |
| 50 | +for.body: |
| 51 | + %i = phi i32 [0, %entry], [%inc, %for.body] |
| 52 | + %b = or i32 %a, -2147483648 |
| 53 | + %inc = sub nsw i32 %i, %b |
| 54 | + %cmp = icmp sge i32 %i, 0 |
| 55 | + br i1 %cmp, label %for.end, label %for.body |
| 56 | + |
| 57 | +for.end: |
| 58 | + ret i32 %i |
| 59 | +} |
| 60 | + |
| 61 | +; Induction variable is known to be negative when |
| 62 | +; its initial value is negative and decrements by |
| 63 | +; a non-negative value |
| 64 | +define i32 @test_indvar_negative_add() { |
| 65 | +; CHECK-LABEL: @test_indvar_negative_add( |
| 66 | +; CHECK: br i1 true, label %for.end, label %for.body |
| 67 | +entry: |
| 68 | + br label %for.body |
| 69 | + |
| 70 | +for.body: |
| 71 | + %i = phi i32 [-1, %entry], [%inc, %for.body] |
| 72 | + %inc = add nsw i32 %i, -1 |
| 73 | + %cmp = icmp slt i32 %i, 0 |
| 74 | + br i1 %cmp, label %for.end, label %for.body |
| 75 | + |
| 76 | +for.end: |
| 77 | + ret i32 %i |
| 78 | +} |
| 79 | + |
| 80 | +; Induction variable is known to be negative, |
| 81 | +; similar to add |
| 82 | +define i32 @test_indvar_negative_sub(i32 %a) { |
| 83 | +; CHECK-LABEL: @test_indvar_negative_sub( |
| 84 | +; CHECK: br i1 true, label %for.end, label %for.body |
| 85 | +entry: |
| 86 | + br label %for.body |
| 87 | + |
| 88 | +for.body: |
| 89 | + %i = phi i32 [-1, %entry], [%inc, %for.body] |
| 90 | + %b = and i32 %a, 2147483647 |
| 91 | + %inc = sub nsw i32 %i, %b |
| 92 | + %cmp = icmp slt i32 %i, 0 |
| 93 | + br i1 %cmp, label %for.end, label %for.body |
| 94 | + |
| 95 | +for.end: |
| 96 | + ret i32 %i |
| 97 | +} |
0 commit comments