1; RUN: opt < %s -basicaa -loop-interchange -pass-remarks-missed='loop-interchange' \
2; RUN:   -pass-remarks-output=%t -verify-loop-info -verify-dom-info -S | FileCheck -check-prefix=IR %s
3; RUN: FileCheck --input-file=%t %s
4
5target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6target triple = "x86_64-unknown-linux-gnu"
7
8@A = common global [100 x [100 x i32]] zeroinitializer
9@B = common global [100 x [100 x [100 x i32]]] zeroinitializer
10@C = common global [100 x [100 x i64]] zeroinitializer
11
12;;--------------------------------------Test case 01------------------------------------
13;; [FIXME] This loop though valid is currently not interchanged due to the limitation that we cannot split the inner loop latch due to multiple use of inner induction
14;; variable.(used to increment the loop counter and to access A[j+1][i+1]
15;;  for(int i=0;i<N-1;i++)
16;;    for(int j=1;j<N-1;j++)
17;;      A[j+1][i+1] = A[j+1][i+1] + k;
18
19; FIXME: Currently fails because of DA changes.
20; IR-LABEL: @interchange_01
21; IR-NOT: split
22
23; CHECK:      Name:            Dependence
24; CHECK-NEXT: Function:        interchange_01
25
26define void @interchange_01(i32 %k, i32 %N) {
27 entry:
28   %sub = add nsw i32 %N, -1
29   %cmp26 = icmp sgt i32 %N, 1
30   br i1 %cmp26, label %for.cond1.preheader.lr.ph, label %for.end17
31
32 for.cond1.preheader.lr.ph:
33   %cmp324 = icmp sgt i32 %sub, 1
34   %0 = add i32 %N, -2
35   %1 = sext i32 %sub to i64
36   br label %for.cond1.preheader
37
38 for.cond.loopexit:
39   %cmp = icmp slt i64 %indvars.iv.next29, %1
40   br i1 %cmp, label %for.cond1.preheader, label %for.end17
41
42 for.cond1.preheader:
43   %indvars.iv28 = phi i64 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next29, %for.cond.loopexit ]
44   %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
45   br i1 %cmp324, label %for.body4, label %for.cond.loopexit
46
47 for.body4:
48   %indvars.iv = phi i64 [ %indvars.iv.next, %for.body4 ], [ 1, %for.cond1.preheader ]
49   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
50   %arrayidx7 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %indvars.iv.next, i64 %indvars.iv.next29
51   %2 = load i32, i32* %arrayidx7
52   %add8 = add nsw i32 %2, %k
53   store i32 %add8, i32* %arrayidx7
54   %lftr.wideiv = trunc i64 %indvars.iv to i32
55   %exitcond = icmp eq i32 %lftr.wideiv, %0
56   br i1 %exitcond, label %for.cond.loopexit, label %for.body4
57
58 for.end17:
59   ret void
60}
61
62; When currently cannot interchange this loop, because transform currently
63; expects the latches to be the exiting blocks too.
64
65; IR-LABEL: @interchange_02
66; IR-NOT: split
67;
68; CHECK:      Name:            ExitingNotLatch
69; CHECK-NEXT: Function:        interchange_02
70define void @interchange_02(i64 %k, i64 %N) {
71entry:
72  br label %for1.header
73
74for1.header:
75  %j23 = phi i64 [ 0, %entry ], [ %j.next24, %for1.inc10 ]
76  br label %for2
77
78for2:
79  %j = phi i64 [ %j.next, %latch ], [ 0, %for1.header ]
80  %arrayidx5 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* @C, i64 0, i64 %j, i64 %j23
81  %lv = load i64, i64* %arrayidx5
82  %add = add nsw i64 %lv, %k
83  store i64 %add, i64* %arrayidx5
84  %exitcond = icmp eq i64 %j, 99
85  br i1 %exitcond, label %for1.inc10, label %latch
86latch:
87  %j.next = add nuw nsw i64 %j, 1
88  br label %for2
89
90for1.inc10:
91  %j.next24 = add nuw nsw i64 %j23, 1
92  %exitcond26 = icmp eq i64 %j23, 99
93  br i1 %exitcond26, label %for.end12, label %for1.header
94
95for.end12:
96  ret void
97}
98