1; RUN: opt < %s -loop-interchange -cache-line-size=64 -pass-remarks-output=%t -verify-dom-info -verify-loop-info \
2; RUN:     -pass-remarks=loop-interchange -pass-remarks-missed=loop-interchange
3; RUN: FileCheck -input-file %t %s
4
5;; We test profitability model in these test cases.
6
7target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8
9@A = common global [100 x [100 x i32]] zeroinitializer
10@B = common global [100 x [100 x i32]] zeroinitializer
11
12;;---------------------------------------Test case 01---------------------------------
13;; Loops interchange will result in code vectorization and hence profitable. Check for interchange.
14;;   for(int i=1;i<100;i++)
15;;     for(int j=1;j<100;j++)
16;;       A[j][i] = A[j - 1][i] + B[j][i];
17
18; CHECK:      Name:            Interchanged
19; CHECK-NEXT: Function:        interchange_01
20
21define void @interchange_01() {
22entry:
23  br label %for2.preheader
24
25for2.preheader:
26  %i30 = phi i64 [ 1, %entry ], [ %i.next31, %for1.inc14 ]
27  br label %for2
28
29for2:
30  %j = phi i64 [ %i.next, %for2 ], [ 1, %for2.preheader ]
31  %j.prev = add nsw i64 %j,  -1
32  %arrayidx5 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %j.prev, i64 %i30
33  %lv1 = load i32, i32* %arrayidx5
34  %arrayidx9 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @B, i64 0, i64 %j,  i64 %i30
35  %lv2 = load i32, i32* %arrayidx9
36  %add = add nsw i32 %lv1, %lv2
37  %arrayidx13 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %j,  i64 %i30
38  store i32 %add, i32* %arrayidx13
39  %i.next = add nuw nsw i64 %j,  1
40  %exitcond = icmp eq i64 %j,  99
41  br i1 %exitcond, label %for1.inc14, label %for2
42
43for1.inc14:
44  %i.next31 = add nuw nsw i64 %i30, 1
45  %exitcond33 = icmp eq i64 %i30, 99
46  br i1 %exitcond33, label %for.end16, label %for2.preheader
47
48for.end16:
49  ret void
50}
51
52;; ---------------------------------------Test case 02---------------------------------
53;; Check loop interchange profitability model.
54;; This tests profitability model when operands of getelementpointer and not exactly the induction variable but some
55;; arithmetic operation on them.
56;;   for(int i=1;i<N;i++)
57;;    for(int j=1;j<N;j++)
58;;       A[j-1][i-1] = A[j - 1][i-1] + B[j-1][i-1];
59
60; CHECK:      Name:            Interchanged
61; CHECK-NEXT: Function:        interchange_02
62define void @interchange_02() {
63entry:
64  br label %for1.header
65
66for1.header:
67  %i35 = phi i64 [ 1, %entry ], [ %i.next36, %for1.inc19 ]
68  %i.prev = add nsw i64 %i35, -1
69  br label %for2
70
71for2:
72  %j = phi i64 [ 1, %for1.header ], [ %i.next, %for2 ]
73  %j.prev = add nsw i64 %j,  -1
74  %arrayidx6 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %j.prev, i64 %i.prev
75  %lv1 = load i32, i32* %arrayidx6
76  %arrayidx12 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @B, i64 0, i64 %j.prev, i64 %i.prev
77  %lv2 = load i32, i32* %arrayidx12
78  %add = add nsw i32 %lv1, %lv2
79  store i32 %add, i32* %arrayidx6
80  %i.next = add nuw nsw i64 %j,  1
81  %exitcond = icmp eq i64 %j,  99
82  br i1 %exitcond, label %for1.inc19, label %for2
83
84for1.inc19:
85  %i.next36 = add nuw nsw i64 %i35, 1
86  %exitcond39 = icmp eq i64 %i35, 99
87  br i1 %exitcond39, label %for.end21, label %for1.header
88
89for.end21:
90  ret void
91}
92
93;;---------------------------------------Test case 03---------------------------------
94;; Loops interchange is not profitable.
95;;   for(int i=1;i<100;i++)
96;;     for(int j=1;j<100;j++)
97;;       A[i-1][j-1] = A[i - 1][j-1] + B[i][j];
98
99; CHECK:      Name:            InterchangeNotProfitable
100; CHECK-NEXT: Function:        interchange_03
101define void @interchange_03(){
102entry:
103  br label %for1.header
104
105for1.header:
106  %i34 = phi i64 [ 1, %entry ], [ %i.next35, %for1.inc17 ]
107  %i.prev = add nsw i64 %i34, -1
108  br label %for2
109
110for2:
111  %j = phi i64 [ 1, %for1.header ], [ %i.next, %for2 ]
112  %j.prev = add nsw i64 %j, -1
113  %arrayidx6 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %i.prev, i64 %j.prev
114  %lv1 = load i32, i32* %arrayidx6
115  %arrayidx10 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @B, i64 0, i64 %i34, i64 %j
116  %lv2 = load i32, i32* %arrayidx10
117  %add = add nsw i32 %lv1, %lv2
118  store i32 %add, i32* %arrayidx6
119  %i.next = add nuw nsw i64 %j,  1
120  %exitcond = icmp eq i64 %j,  99
121  br i1 %exitcond, label %for1.inc17, label %for2
122
123for1.inc17:
124  %i.next35 = add nuw nsw i64 %i34, 1
125  %exitcond38 = icmp eq i64 %i34, 99
126  br i1 %exitcond38, label %for.end19, label %for1.header
127
128for.end19:
129  ret void
130}
131
132;; Loops should not be interchanged in this case as it is not profitable.
133;;  for(int i=0;i<100;i++)
134;;    for(int j=0;j<100;j++)
135;;      A[i][j] = A[i][j]+k;
136
137; CHECK:      Name:            InterchangeNotProfitable
138; CHECK-NEXT: Function:        interchange_04
139define void @interchange_04(i32 %k) {
140entry:
141  br label %for.cond1.preheader
142
143for.cond1.preheader:
144  %indvars.iv21 = phi i64 [ 0, %entry ], [ %indvars.iv.next22, %for.inc10 ]
145  br label %for.body3
146
147for.body3:
148  %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next, %for.body3 ]
149  %arrayidx5 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %indvars.iv21, i64 %indvars.iv
150  %0 = load i32, i32* %arrayidx5
151  %add = add nsw i32 %0, %k
152  store i32 %add, i32* %arrayidx5
153  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
154  %exitcond = icmp eq i64 %indvars.iv.next, 100
155  br i1 %exitcond, label %for.inc10, label %for.body3
156
157for.inc10:
158  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
159  %exitcond23 = icmp eq i64 %indvars.iv.next22, 100
160  br i1 %exitcond23, label %for.end12, label %for.cond1.preheader
161
162for.end12:
163  ret void
164}
165