xref: /llvm-project/llvm/test/Transforms/LoopInterchange/loop-interchange-optimization-remarks.ll (revision ee7188c8b2ab06e740470cee368b137e0781958c)
1; Test optimization remarks generated by the LoopInterchange pass.
2;
3; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info \
4; RUN:     -pass-remarks-output=%t -pass-remarks-missed='loop-interchange' \
5; RUN:     -pass-remarks='loop-interchange' -S
6; RUN: cat %t |  FileCheck %s
7
8; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info \
9; RUN:     -pass-remarks-output=%t -pass-remarks-missed='loop-interchange' \
10; RUN:     -pass-remarks='loop-interchange' -S -da-disable-delinearization-checks
11; RUN: cat %t |  FileCheck --check-prefix=DELIN %s
12
13@A = common global [100 x [100 x i32]] zeroinitializer
14@B = common global [100 x [100 x i32]] zeroinitializer
15@C = common global [100 x i32] zeroinitializer
16
17;;---------------------------------------Test case 01---------------------------------
18;; Loops interchange is not profitable.
19;;   for(int i=1;i<N;i++)
20;;     for(int j=1;j<N;j++)
21;;       A[i-1][j-1] = A[i - 1][j-1] + B[i][j];
22
23define void @test01(i32 %N){
24entry:
25  %cmp31 = icmp sgt i32 %N, 1
26  br i1 %cmp31, label %for.cond1.preheader.lr.ph, label %for.end19
27
28for.cond1.preheader.lr.ph:
29  %0 = add i32 %N, -1
30  br label %for.body3.lr.ph
31
32for.body3.lr.ph:
33  %indvars.iv34 = phi i64 [ 1, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next35, %for.inc17 ]
34  %1 = add nsw i64 %indvars.iv34, -1
35  br label %for.body3
36
37for.body3:
38  %indvars.iv = phi i64 [ 1, %for.body3.lr.ph ], [ %indvars.iv.next, %for.body3 ]
39  %2 = add nsw i64 %indvars.iv, -1
40  %arrayidx6 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %1, i64 %2
41  %3 = load i32, ptr %arrayidx6
42  %arrayidx10 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %indvars.iv34, i64 %indvars.iv
43  %4 = load i32, ptr %arrayidx10
44  %add = add nsw i32 %4, %3
45  store i32 %add, ptr %arrayidx6
46  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
47  %lftr.wideiv = trunc i64 %indvars.iv to i32
48  %exitcond = icmp eq i32 %lftr.wideiv, %0
49  br i1 %exitcond, label %for.inc17, label %for.body3
50
51for.inc17:
52  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
53  %lftr.wideiv37 = trunc i64 %indvars.iv34 to i32
54  %exitcond38 = icmp eq i32 %lftr.wideiv37, %0
55  br i1 %exitcond38, label %for.end19, label %for.body3.lr.ph
56
57for.end19:
58  ret void
59}
60
61; CHECK: --- !Missed
62; CHECK-NEXT: Pass:            loop-interchange
63; CHECK-NEXT: Name:            Dependence
64; CHECK-NEXT: Function:        test01
65; CHECK-NEXT: Args:
66; CHECK-NEXT:   - String:          Cannot interchange loops due to dependences.
67; CHECK-NEXT: ...
68
69; DELIN: --- !Missed
70; DELIN-NEXT: Pass:            loop-interchange
71; DELIN-NEXT: Name:            InterchangeNotProfitable
72; DELIN-NEXT: Function:        test01
73; DELIN-NEXT: Args:
74; DELIN-NEXT:   - String:          Interchanging loops is not considered to improve cache locality nor vectorization.
75; DELIN-NEXT: ...
76
77;;--------------------------------------Test case 02------------------------------------
78;; [FIXME] This loop though valid is currently not interchanged due to the
79;; limitation that we cannot split the inner loop latch due to multiple use of inner induction
80;; variable.(used to increment the loop counter and to access A[j+1][i+1]
81;;  for(int i=0;i<N-1;i++)
82;;    for(int j=1;j<N-1;j++)
83;;      A[j+1][i+1] = A[j+1][i+1] + k;
84
85define void @test02(i32 %k, i32 %N) {
86 entry:
87   %sub = add nsw i32 %N, -1
88   %cmp26 = icmp sgt i32 %N, 1
89   br i1 %cmp26, label %for.cond1.preheader.lr.ph, label %for.end17
90
91 for.cond1.preheader.lr.ph:
92   %cmp324 = icmp sgt i32 %sub, 1
93   %0 = add i32 %N, -2
94   %1 = sext i32 %sub to i64
95   br label %for.cond1.preheader
96
97 for.cond.loopexit:
98   %cmp = icmp slt i64 %indvars.iv.next29, %1
99   br i1 %cmp, label %for.cond1.preheader, label %for.end17
100
101 for.cond1.preheader:
102   %indvars.iv28 = phi i64 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next29, %for.cond.loopexit ]
103   %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
104   br i1 %cmp324, label %for.body4, label %for.cond.loopexit
105
106 for.body4:
107   %indvars.iv = phi i64 [ %indvars.iv.next, %for.body4 ], [ 1, %for.cond1.preheader ]
108   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
109   %arrayidx7 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %indvars.iv.next, i64 %indvars.iv.next29
110   %2 = load i32, ptr %arrayidx7
111   %add8 = add nsw i32 %2, %k
112   store i32 %add8, ptr %arrayidx7
113   %lftr.wideiv = trunc i64 %indvars.iv to i32
114   %exitcond = icmp eq i32 %lftr.wideiv, %0
115   br i1 %exitcond, label %for.cond.loopexit, label %for.body4
116
117 for.end17:
118   ret void
119}
120
121; CHECK: --- !Missed
122; CHECK-NEXT: Pass:            loop-interchange
123; CHECK-NEXT: Name:            Dependence
124; CHECK-NEXT: Function:        test02
125; CHECK-NEXT: Args:
126; CHECK-NEXT:   - String:          Cannot interchange loops due to dependences.
127; CHECK-NEXT: ...
128
129; DELIN: --- !Passed
130; DELIN-NEXT: Pass:            loop-interchange
131; DELIN-NEXT: Name:            Interchanged
132; DELIN-NEXT: Function:        test02
133; DELIN-NEXT: Args:
134; DELIN-NEXT:   - String:           Loop interchanged with enclosing loop.
135; DELIN-NEXT: ...
136
137;;-----------------------------------Test case 03-------------------------------
138;; Test to make sure we can handle output dependencies.
139;;
140;;  for (int i = 0; i < 2; ++i)
141;;    for(int j = 0; j < 3; ++j) {
142;;      A[j][i] = i;
143;;      A[j][i+1] = j;
144;;    }
145
146@A10 = local_unnamed_addr global [3 x [3 x i32]] zeroinitializer, align 16
147
148define void @test03() {
149entry:
150  br label %for.cond1.preheader
151
152for.cond.loopexit:                                ; preds = %for.body4
153  %exitcond28 = icmp ne i64 %indvars.iv.next27, 2
154  br i1 %exitcond28, label %for.cond1.preheader, label %for.cond.cleanup
155
156for.cond1.preheader:                              ; preds = %for.cond.loopexit, %entry
157  %indvars.iv26 = phi i64 [ 0, %entry ], [ %indvars.iv.next27, %for.cond.loopexit ]
158  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
159  br label %for.body4
160
161for.cond.cleanup:                                 ; preds = %for.cond.loopexit
162  ret void
163
164for.body4:                                        ; preds = %for.body4, %for.cond1.preheader
165  %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next, %for.body4 ]
166  %arrayidx6 = getelementptr inbounds [3 x [3 x i32]], ptr @A10, i64 0, i64 %indvars.iv, i64 %indvars.iv26
167  %tmp = trunc i64 %indvars.iv26 to i32
168  store i32 %tmp, ptr %arrayidx6, align 4
169  %arrayidx10 = getelementptr inbounds [3 x [3 x i32]], ptr @A10, i64 0, i64 %indvars.iv, i64 %indvars.iv.next27
170  %tmp1 = trunc i64 %indvars.iv to i32
171  store i32 %tmp1, ptr %arrayidx10, align 4
172  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
173  %exitcond = icmp ne i64 %indvars.iv.next, 3
174  br i1 %exitcond, label %for.body4, label %for.cond.loopexit
175}
176
177; CHECK: --- !Passed
178; CHECK-NEXT: Pass:            loop-interchange
179; CHECK-NEXT: Name:            Interchanged
180; CHECK-NEXT: Function:        test03
181; CHECK-NEXT: Args:
182; CHECK-NEXT:   - String:          Loop interchanged with enclosing loop.
183; CHECK-NEXT: ...
184
185; DELIN: --- !Passed
186; DELIN-NEXT: Pass:            loop-interchange
187; DELIN-NEXT: Name:            Interchanged
188; DELIN-NEXT: Function:        test03
189; DELIN-NEXT: Args:
190; DELIN-NEXT:  - String:          Loop interchanged with enclosing loop.
191; DELIN-NEXT: ...
192
193;;--------------------------------------Test case 04-------------------------------------
194;; Loops not tightly nested are not interchanged
195;;  for(int j=0;j<N;j++) {
196;;    B[j] = j+k;
197;;    for(int i=0;i<N;i++)
198;;      A[j][i] = A[j][i]+B[j];
199;;  }
200
201define void @test04(i32 %k, i32 %N){
202entry:
203  %cmp30 = icmp sgt i32 %N, 0
204  br i1 %cmp30, label %for.body.lr.ph, label %for.end17
205
206for.body.lr.ph:
207  %0 = add i32 %N, -1
208  %1 = zext i32 %k to i64
209  br label %for.body
210
211for.body:
212  %indvars.iv32 = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next33, %for.inc15 ]
213  %2 = add nsw i64 %indvars.iv32, %1
214  %arrayidx = getelementptr inbounds [100 x i32], ptr @C, i64 0, i64 %indvars.iv32
215  %3 = trunc i64 %2 to i32
216  store i32 %3, ptr %arrayidx
217  br label %for.body3
218
219for.body3:
220  %indvars.iv = phi i64 [ 0, %for.body ], [ %indvars.iv.next, %for.body3 ]
221  %arrayidx7 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %indvars.iv32, i64 %indvars.iv
222  %4 = load i32, ptr %arrayidx7
223  %add10 = add nsw i32 %3, %4
224  store i32 %add10, ptr %arrayidx7
225  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
226  %lftr.wideiv = trunc i64 %indvars.iv to i32
227  %exitcond = icmp eq i32 %lftr.wideiv, %0
228  br i1 %exitcond, label %for.inc15, label %for.body3
229
230for.inc15:
231  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
232  %lftr.wideiv35 = trunc i64 %indvars.iv32 to i32
233  %exitcond36 = icmp eq i32 %lftr.wideiv35, %0
234  br i1 %exitcond36, label %for.end17, label %for.body
235
236for.end17:
237  ret void
238}
239
240; CHECK: --- !Missed
241; CHECK-NEXT: Pass:            loop-interchange
242; CHECK-NEXT: Name:            Dependence
243; CHECK-NEXT: Function:        test04
244; CHECK-NEXT: Args:
245; CHECK-NEXT:   - String:          Cannot interchange loops due to dependences.
246; CHECK-NEXT: ...
247
248; DELIN: --- !Missed
249; DELIN-NEXT: Pass:            loop-interchange
250; DELIN-NEXT: Name:            NotTightlyNested
251; DELIN-NEXT: Function:        test04
252; DELIN-NEXT: Args:
253; DELIN-NEXT:  - String:          Cannot interchange loops because they are not tightly nested.
254; DELIN-NEXT: ...
255