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