|
| 1 | +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| 2 | +; RUN: opt -S < %s -passes=sink | FileCheck %s |
| 3 | + |
| 4 | +; Test that loads can be sunk to a non-immediate successor block by analyzing |
| 5 | +; paths for conflicting stores. |
| 6 | + |
| 7 | +declare void @readfunc() readonly willreturn |
| 8 | +declare void @maywritefunc() willreturn |
| 9 | + |
| 10 | +; Load can be sunk to non-immediate successor |
| 11 | +define void @load_can_sink(i1 %condA, i1 %condB, ptr %a, ptr %b) { |
| 12 | +; CHECK-LABEL: @load_can_sink( |
| 13 | +; CHECK-NEXT: entry: |
| 14 | +; CHECK-NEXT: br label [[MERGEA:%.*]] |
| 15 | +; CHECK: mergeA: |
| 16 | +; CHECK-NEXT: br i1 [[CONDA:%.*]], label [[THENA:%.*]], label [[MERGEB:%.*]] |
| 17 | +; CHECK: thenA: |
| 18 | +; CHECK-NEXT: call void @readfunc() |
| 19 | +; CHECK-NEXT: br label [[MERGEB]] |
| 20 | +; CHECK: mergeB: |
| 21 | +; CHECK-NEXT: br i1 [[CONDB:%.*]], label [[THENB:%.*]], label [[MERGEC:%.*]] |
| 22 | +; CHECK: thenB: |
| 23 | +; CHECK-NEXT: [[VALUE:%.*]] = load i32, ptr [[A:%.*]], align 4 |
| 24 | +; CHECK-NEXT: store i32 [[VALUE]], ptr [[B:%.*]], align 4 |
| 25 | +; CHECK-NEXT: br label [[MERGEC]] |
| 26 | +; CHECK: mergeC: |
| 27 | +; CHECK-NEXT: ret void |
| 28 | +; |
| 29 | +entry: |
| 30 | + %value = load i32, ptr %a, align 4 |
| 31 | + br label %mergeA |
| 32 | +mergeA: |
| 33 | + br i1 %condA, label %thenA, label %mergeB |
| 34 | +thenA: |
| 35 | + call void @readfunc() |
| 36 | + br label %mergeB |
| 37 | +mergeB: |
| 38 | + br i1 %condB, label %thenB, label %mergeC |
| 39 | +thenB: |
| 40 | + store i32 %value, ptr %b |
| 41 | + br label %mergeC |
| 42 | +mergeC: |
| 43 | + ret void |
| 44 | +} |
| 45 | + |
| 46 | +; Call may store so load cannot be sunk |
| 47 | +define void @load_cannot_sink(i1 %condA, i1 %condB, ptr %a, ptr %b) { |
| 48 | +; CHECK-LABEL: @load_cannot_sink( |
| 49 | +; CHECK-NEXT: entry: |
| 50 | +; CHECK-NEXT: br label [[MERGEA:%.*]] |
| 51 | +; CHECK: mergeA: |
| 52 | +; CHECK-NEXT: [[VALUE:%.*]] = load i32, ptr [[A:%.*]], align 4 |
| 53 | +; CHECK-NEXT: br i1 [[CONDA:%.*]], label [[THENA:%.*]], label [[MERGEB:%.*]] |
| 54 | +; CHECK: thenA: |
| 55 | +; CHECK-NEXT: call void @maywritefunc() |
| 56 | +; CHECK-NEXT: br label [[MERGEB]] |
| 57 | +; CHECK: mergeB: |
| 58 | +; CHECK-NEXT: br i1 [[CONDB:%.*]], label [[THENB:%.*]], label [[MERGEC:%.*]] |
| 59 | +; CHECK: thenB: |
| 60 | +; CHECK-NEXT: store i32 [[VALUE]], ptr [[B:%.*]], align 4 |
| 61 | +; CHECK-NEXT: br label [[MERGEC]] |
| 62 | +; CHECK: mergeC: |
| 63 | +; CHECK-NEXT: ret void |
| 64 | +; |
| 65 | +entry: |
| 66 | + %value = load i32, ptr %a, align 4 |
| 67 | + br label %mergeA |
| 68 | +mergeA: |
| 69 | + br i1 %condA, label %thenA, label %mergeB |
| 70 | +thenA: |
| 71 | + call void @maywritefunc() |
| 72 | + br label %mergeB |
| 73 | +mergeB: |
| 74 | + br i1 %condB, label %thenB, label %mergeC |
| 75 | +thenB: |
| 76 | + store i32 %value, ptr %b |
| 77 | + br label %mergeC |
| 78 | +mergeC: |
| 79 | + ret void |
| 80 | +} |
| 81 | + |
| 82 | +; Load can be sunk to non-immediate successor because load ptr is noalias |
| 83 | +define void @load_can_sink_noalias(i1 %condA, i1 %condB, ptr noalias %a, ptr %b) { |
| 84 | +; CHECK-LABEL: @load_can_sink_noalias( |
| 85 | +; CHECK-NEXT: entry: |
| 86 | +; CHECK-NEXT: br label [[MERGEA:%.*]] |
| 87 | +; CHECK: mergeA: |
| 88 | +; CHECK-NEXT: br i1 [[CONDA:%.*]], label [[THENA:%.*]], label [[MERGEB:%.*]] |
| 89 | +; CHECK: thenA: |
| 90 | +; CHECK-NEXT: store i32 0, ptr [[B:%.*]], align 4 |
| 91 | +; CHECK-NEXT: br label [[MERGEB]] |
| 92 | +; CHECK: mergeB: |
| 93 | +; CHECK-NEXT: br i1 [[CONDB:%.*]], label [[THENB:%.*]], label [[MERGEC:%.*]] |
| 94 | +; CHECK: thenB: |
| 95 | +; CHECK-NEXT: [[VALUE:%.*]] = load i32, ptr [[A:%.*]], align 4 |
| 96 | +; CHECK-NEXT: store i32 [[VALUE]], ptr [[B]], align 4 |
| 97 | +; CHECK-NEXT: br label [[MERGEC]] |
| 98 | +; CHECK: mergeC: |
| 99 | +; CHECK-NEXT: ret void |
| 100 | +; |
| 101 | +entry: |
| 102 | + %value = load i32, ptr %a, align 4 |
| 103 | + br label %mergeA |
| 104 | +mergeA: |
| 105 | + br i1 %condA, label %thenA, label %mergeB |
| 106 | +thenA: |
| 107 | + store i32 0, ptr %b |
| 108 | + br label %mergeB |
| 109 | +mergeB: |
| 110 | + br i1 %condB, label %thenB, label %mergeC |
| 111 | +thenB: |
| 112 | + store i32 %value, ptr %b |
| 113 | + br label %mergeC |
| 114 | +mergeC: |
| 115 | + ret void |
| 116 | +} |
| 117 | + |
| 118 | +; Load cannot be sunk to non-immediate successor because load ptr may alias |
| 119 | +define void @load_cannot_sink_alias(i1 %condA, i1 %condB, ptr %a, ptr %b) { |
| 120 | +; CHECK-LABEL: @load_cannot_sink_alias( |
| 121 | +; CHECK-NEXT: entry: |
| 122 | +; CHECK-NEXT: br label [[MERGEA:%.*]] |
| 123 | +; CHECK: mergeA: |
| 124 | +; CHECK-NEXT: [[VALUE:%.*]] = load i32, ptr [[A:%.*]], align 4 |
| 125 | +; CHECK-NEXT: br i1 [[CONDA:%.*]], label [[THENA:%.*]], label [[MERGEB:%.*]] |
| 126 | +; CHECK: thenA: |
| 127 | +; CHECK-NEXT: store i32 0, ptr [[B:%.*]], align 4 |
| 128 | +; CHECK-NEXT: br label [[MERGEB]] |
| 129 | +; CHECK: mergeB: |
| 130 | +; CHECK-NEXT: br i1 [[CONDB:%.*]], label [[THENB:%.*]], label [[MERGEC:%.*]] |
| 131 | +; CHECK: thenB: |
| 132 | +; CHECK-NEXT: store i32 [[VALUE]], ptr [[B]], align 4 |
| 133 | +; CHECK-NEXT: br label [[MERGEC]] |
| 134 | +; CHECK: mergeC: |
| 135 | +; CHECK-NEXT: ret void |
| 136 | +; |
| 137 | +entry: |
| 138 | + %value = load i32, ptr %a, align 4 |
| 139 | + br label %mergeA |
| 140 | +mergeA: |
| 141 | + br i1 %condA, label %thenA, label %mergeB |
| 142 | +thenA: |
| 143 | + store i32 0, ptr %b |
| 144 | + br label %mergeB |
| 145 | +mergeB: |
| 146 | + br i1 %condB, label %thenB, label %mergeC |
| 147 | +thenB: |
| 148 | + store i32 %value, ptr %b |
| 149 | + br label %mergeC |
| 150 | +mergeC: |
| 151 | + ret void |
| 152 | +} |
0 commit comments