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