|
| 1 | +; REQUIRES: asserts |
| 2 | +; RUN: opt < %s -passes=loop-interchange -verify-dom-info -verify-loop-info \ |
| 3 | +; RUN: -disable-output -debug 2>&1 | FileCheck %s |
| 4 | + |
| 5 | +@a = dso_local global [20 x [20 x [20 x i32]]] zeroinitializer, align 4 |
| 6 | +@aa = dso_local global [256 x [256 x float]] zeroinitializer, align 64 |
| 7 | +@bb = dso_local global [256 x [256 x float]] zeroinitializer, align 64 |
| 8 | + |
| 9 | +;; for (int nl=0;nl<100;++nl) |
| 10 | +;; for (int i=0;i<256;++i) |
| 11 | +;; for (int j=1;j<256;++j) |
| 12 | +;; aa[j][i] = aa[j-1][i] + bb[j][i]; |
| 13 | +;; |
| 14 | +;; The direction vector of `aa` is [* = >]. We can interchange the innermost |
| 15 | +;; two loops, The direction vector after interchanging will be [* > =]. |
| 16 | + |
| 17 | +; CHECK: Dependency matrix before interchange: |
| 18 | +; CHECK-NEXT: * = > |
| 19 | +; CHECK-NEXT: * = = |
| 20 | +; CHECK-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1 |
| 21 | +; CHECK-NEXT: Checking if loops are tightly nested |
| 22 | +; CHECK-NEXT: Checking instructions in Loop header and Loop latch |
| 23 | +; CHECK-NEXT: Loops are perfectly nested |
| 24 | +; CHECK-NEXT: Loops are legal to interchange |
| 25 | +; CHECK: Dependency matrix after interchange: |
| 26 | +; CHECK-NEXT: * > = |
| 27 | +; CHECK-NEXT: * = = |
| 28 | + |
| 29 | +define void @all_eq_gt() { |
| 30 | +entry: |
| 31 | + br label %for.cond1.preheader |
| 32 | + |
| 33 | +for.cond1.preheader: |
| 34 | + %nl.036 = phi i32 [ 0, %entry ], [ %inc23, %for.cond.cleanup3 ] |
| 35 | + br label %for.cond5.preheader |
| 36 | + |
| 37 | +for.cond.cleanup3: |
| 38 | + %inc23 = add nuw nsw i32 %nl.036, 1 |
| 39 | + %exitcond43 = icmp ne i32 %inc23, 100 |
| 40 | + br i1 %exitcond43, label %for.cond1.preheader, label %for.cond.cleanup |
| 41 | + |
| 42 | +for.cond.cleanup7: |
| 43 | + %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1 |
| 44 | + %exitcond42 = icmp ne i64 %indvars.iv.next40, 256 |
| 45 | + br i1 %exitcond42, label %for.cond5.preheader, label %for.cond.cleanup3 |
| 46 | + |
| 47 | +for.body8: |
| 48 | + %indvars.iv = phi i64 [ 1, %for.cond5.preheader ], [ %indvars.iv.next, %for.body8 ] |
| 49 | + %0 = add nsw i64 %indvars.iv, -1 |
| 50 | + %arrayidx10 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %0, i64 %indvars.iv39 |
| 51 | + %1 = load float, ptr %arrayidx10, align 4 |
| 52 | + %arrayidx14 = getelementptr inbounds [256 x [256 x float]], ptr @bb, i64 0, i64 %indvars.iv, i64 %indvars.iv39 |
| 53 | + %2 = load float, ptr %arrayidx14, align 4 |
| 54 | + %add = fadd fast float %2, %1 |
| 55 | + %arrayidx18 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %indvars.iv, i64 %indvars.iv39 |
| 56 | + store float %add, ptr %arrayidx18, align 4 |
| 57 | + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 |
| 58 | + %exitcond = icmp ne i64 %indvars.iv.next, 256 |
| 59 | + br i1 %exitcond, label %for.body8, label %for.cond.cleanup7 |
| 60 | + |
| 61 | +for.cond5.preheader: |
| 62 | + %indvars.iv39 = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next40, %for.cond.cleanup7 ] |
| 63 | + br label %for.body8 |
| 64 | + |
| 65 | +for.cond.cleanup: |
| 66 | + ret void |
| 67 | +} |
| 68 | + |
| 69 | +;; for (int i=0;i<256;++i) |
| 70 | +;; for (int j=1;j<256;++j) |
| 71 | +;; aa[j][i] = aa[j-1][255-i] + bb[j][i]; |
| 72 | +;; |
| 73 | +;; The direction vector of `aa` is [* >]. We cannot interchange the loops |
| 74 | +;; because we must handle a `*` dependence conservatively. |
| 75 | + |
| 76 | +; CHECK: Dependency matrix before interchange: |
| 77 | +; CHECK-NEXT: * > |
| 78 | +; CHECK-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0 |
| 79 | +; CHECK-NEXT: Failed interchange InnerLoopId = 1 and OuterLoopId = 0 due to dependence |
| 80 | +; CHECK-NEXT: Not interchanging loops. Cannot prove legality. |
| 81 | + |
| 82 | +define void @all_gt() { |
| 83 | +entry: |
| 84 | + br label %for.cond1.preheader |
| 85 | + |
| 86 | +for.cond1.preheader: |
| 87 | + %indvars.iv31 = phi i64 [ 0, %entry ], [ %indvars.iv.next32, %for.cond.cleanup3 ] |
| 88 | + %0 = sub nuw nsw i64 255, %indvars.iv31 |
| 89 | + br label %for.body4 |
| 90 | + |
| 91 | +for.cond.cleanup3: |
| 92 | + %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1 |
| 93 | + %exitcond35 = icmp ne i64 %indvars.iv.next32, 256 |
| 94 | + br i1 %exitcond35, label %for.cond1.preheader, label %for.cond.cleanup |
| 95 | + |
| 96 | +for.body4: |
| 97 | + %indvars.iv = phi i64 [ 1, %for.cond1.preheader ], [ %indvars.iv.next, %for.body4 ] |
| 98 | + %1 = add nsw i64 %indvars.iv, -1 |
| 99 | + %arrayidx7 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %1, i64 %0 |
| 100 | + %2 = load float, ptr %arrayidx7, align 4 |
| 101 | + %arrayidx11 = getelementptr inbounds [256 x [256 x float]], ptr @bb, i64 0, i64 %indvars.iv, i64 %indvars.iv31 |
| 102 | + %3 = load float, ptr %arrayidx11, align 4 |
| 103 | + %add = fadd fast float %3, %2 |
| 104 | + %arrayidx15 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %indvars.iv, i64 %indvars.iv31 |
| 105 | + store float %add, ptr %arrayidx15, align 4 |
| 106 | + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 |
| 107 | + %exitcond = icmp ne i64 %indvars.iv.next, 256 |
| 108 | + br i1 %exitcond, label %for.body4, label %for.cond.cleanup3 |
| 109 | + |
| 110 | +for.cond.cleanup: |
| 111 | + ret void |
| 112 | +} |
| 113 | + |
| 114 | +;; for (int i=0;i<255;++i) |
| 115 | +;; for (int j=1;j<256;++j) |
| 116 | +;; aa[j][i] = aa[j-1][i+1] + bb[j][i]; |
| 117 | +;; |
| 118 | +;; The direciton vector of `aa` is [< >]. We cannot interchange the loops |
| 119 | +;; because the read/write order for `aa` cannot be changed. |
| 120 | + |
| 121 | +; CHECK: Dependency matrix before interchange: |
| 122 | +; CHECK-NEXT: < > |
| 123 | +; CHECK-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0 |
| 124 | +; CHECK-NEXT: Failed interchange InnerLoopId = 1 and OuterLoopId = 0 due to dependence |
| 125 | +; CHECK-NEXT: Not interchanging loops. Cannot prove legality. |
| 126 | + |
| 127 | +define void @lt_gt() { |
| 128 | +entry: |
| 129 | + br label %for.cond1.preheader |
| 130 | + |
| 131 | +for.cond1.preheader: |
| 132 | + %indvars.iv31 = phi i64 [ 0, %entry ], [ %indvars.iv.next32, %for.cond.cleanup3 ] |
| 133 | + %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1 |
| 134 | + br label %for.body4 |
| 135 | + |
| 136 | +for.cond.cleanup3: |
| 137 | + %exitcond34 = icmp ne i64 %indvars.iv.next32, 255 |
| 138 | + br i1 %exitcond34, label %for.cond1.preheader, label %for.cond.cleanup |
| 139 | + |
| 140 | +for.body4: |
| 141 | + %indvars.iv = phi i64 [ 1, %for.cond1.preheader ], [ %indvars.iv.next, %for.body4 ] |
| 142 | + %0 = add nsw i64 %indvars.iv, -1 |
| 143 | + %arrayidx6 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %0, i64 %indvars.iv.next32 |
| 144 | + %1 = load float, ptr %arrayidx6, align 4 |
| 145 | + %arrayidx10 = getelementptr inbounds [256 x [256 x float]], ptr @bb, i64 0, i64 %indvars.iv, i64 %indvars.iv31 |
| 146 | + %2 = load float, ptr %arrayidx10, align 4 |
| 147 | + %add11 = fadd fast float %2, %1 |
| 148 | + %arrayidx15 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %indvars.iv, i64 %indvars.iv31 |
| 149 | + store float %add11, ptr %arrayidx15, align 4 |
| 150 | + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 |
| 151 | + %exitcond = icmp ne i64 %indvars.iv.next, 256 |
| 152 | + br i1 %exitcond, label %for.body4, label %for.cond.cleanup3 |
| 153 | + |
| 154 | +for.cond.cleanup: |
| 155 | + ret void |
| 156 | +} |
| 157 | + |
| 158 | +;; for (int i=0;i<20;i++) |
| 159 | +;; for (int j=0;j<20;j++) |
| 160 | +;; for (int k=1;k<20;k++) |
| 161 | +;; a[i][j][k] = a[i][5][k-1]; |
| 162 | +;; |
| 163 | +;; The direction vector of `a` is [= * >]. We cannot interchange all the loops. |
| 164 | + |
| 165 | +; CHECK: Dependency matrix before interchange: |
| 166 | +; CHECK-NEXT: = * > |
| 167 | +; CHECK-NEXT: Processing InnerLoopId = 2 and OuterLoopId = 1 |
| 168 | +; CHECK-NEXT: Failed interchange InnerLoopId = 2 and OuterLoopId = 1 due to dependence |
| 169 | +; CHECK-NEXT: Not interchanging loops. Cannot prove legality. |
| 170 | +; CHECK-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0 |
| 171 | +; CHECK-NEXT: Failed interchange InnerLoopId = 1 and OuterLoopId = 0 due to dependence |
| 172 | +; CHECK-NEXT: Not interchanging loops. Cannot prove legality. |
| 173 | + |
| 174 | +define void @eq_all_gt() { |
| 175 | +entry: |
| 176 | + br label %for.cond1.preheader |
| 177 | + |
| 178 | +for.cond1.preheader: |
| 179 | + %indvars.iv44 = phi i64 [ 0, %entry ], [ %indvars.iv.next45, %for.cond.cleanup3 ] |
| 180 | + br label %for.cond5.preheader |
| 181 | + |
| 182 | +for.cond.cleanup3: |
| 183 | + %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1 |
| 184 | + %exitcond47 = icmp ne i64 %indvars.iv.next45, 20 |
| 185 | + br i1 %exitcond47, label %for.cond1.preheader, label %for.cond.cleanup |
| 186 | + |
| 187 | +for.cond.cleanup7: |
| 188 | + %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1 |
| 189 | + %exitcond43 = icmp ne i64 %indvars.iv.next41, 20 |
| 190 | + br i1 %exitcond43, label %for.cond5.preheader, label %for.cond.cleanup3 |
| 191 | + |
| 192 | +for.body8: |
| 193 | + %indvars.iv = phi i64 [ 1, %for.cond5.preheader ], [ %indvars.iv.next, %for.body8 ] |
| 194 | + %0 = add nsw i64 %indvars.iv, -1 |
| 195 | + %arrayidx11 = getelementptr inbounds [20 x [20 x [20 x i32]]], ptr @a, i64 0, i64 %indvars.iv44, i64 5, i64 %0 |
| 196 | + %1 = load i32, ptr %arrayidx11, align 4 |
| 197 | + %arrayidx17 = getelementptr inbounds nuw [20 x [20 x [20 x i32]]], ptr @a, i64 0, i64 %indvars.iv44, i64 %indvars.iv40, i64 %indvars.iv |
| 198 | + store i32 %1, ptr %arrayidx17, align 4 |
| 199 | + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 |
| 200 | + %exitcond = icmp ne i64 %indvars.iv.next, 20 |
| 201 | + br i1 %exitcond, label %for.body8, label %for.cond.cleanup7 |
| 202 | + |
| 203 | +for.cond5.preheader: |
| 204 | + %indvars.iv40 = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next41, %for.cond.cleanup7 ] |
| 205 | + br label %for.body8 |
| 206 | + |
| 207 | +for.cond.cleanup: |
| 208 | + ret void |
| 209 | +} |
0 commit comments