1 ; RUN: opt -basic-aa -loop-interchange -verify-dom-info -verify-loop-info -verify-loop-lcssa -S %s | FileCheck %s
3 @b = global [3 x [5 x [8 x i16]]] [[5 x [8 x i16]] zeroinitializer, [5 x [8 x i16]] [[8 x i16] zeroinitializer, [8 x i16] [i16 0, i16 0, i16 0, i16 6, i16 1, i16 6, i16 0, i16 0], [8 x i16] zeroinitializer, [8 x i16] zeroinitializer, [8 x i16] zeroinitializer], [5 x [8 x i16]] zeroinitializer], align 2
4 @a = common global i32 0, align 4
5 @d = common dso_local local_unnamed_addr global [1 x [6 x i32]] zeroinitializer, align 4
11 ;; short b[3][5][8] = {{}, {{}, 0, 0, 0, 6, 1, 6}};
14 ;; for (; c <= 2; c++) {
18 ;; for (; d <= 2; d++)
19 ;; a |= b[d][d][c + 5];
23 define void @test1() {
26 ;CHECK-NEXT: br label [[FOR_COND1_PREHEADER:%.*]]
27 ;CHECK: for.body.preheader:
28 ;CHECK-NEXT: br label [[FOR_BODY:%.*]]
30 ;CHECK-NEXT: [[INDVARS_IV22:%.*]] = phi i64 [ [[INDVARS_IV_NEXT23:%.*]], [[FOR_INC8:%.*]] ], [ 0, [[FOR_BODY_PREHEADER:%.*]] ]
31 ;CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i64 [[INDVARS_IV22:%.*]], 0
32 ;CHECK-NEXT: br i1 [[TOBOOL]], label [[FOR_BODY3_SPLIT1:%.*]], label [[FOR_BODY3_SPLIT:%.*]]
33 ;CHECK: for.cond1.preheader:
34 ;CHECK-NEXT: br label [[FOR_BODY3:%.*]]
36 ;CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_COND1_PREHEADER]] ], [ %3, [[FOR_BODY3_SPLIT]] ]
37 ;CHECK-NEXT: br label [[FOR_BODY_PREHEADER]]
38 ;CHECK: for.body3.split1:
39 ;CHECK-NEXT: [[TMP0:%.*]] = add nuw nsw i64 [[INDVARS_IV22]], 5
40 ;CHECK-NEXT: [[ARRAYIDX7:%.*]] = getelementptr inbounds [3 x [5 x [8 x i16]]], [3 x [5 x [8 x i16]]]* @b, i64 0, i64 [[INDVARS_IV]], i64 [[INDVARS_IV]], i64 [[TMP0]]
41 ;CHECK-NEXT: [[TMP1:%.*]] = load i16, i16* [[ARRAYIDX7]]
42 ;CHECK-NEXT: [[CONV:%.*]] = sext i16 [[TMP1]] to i32
43 ;CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* @a
44 ;CHECK-NEXT: [[TMP_OR:%.*]] = or i32 [[TMP2]], [[CONV]]
45 ;CHECK-NEXT: store i32 [[TMP_OR]], i32* @a
46 ;CHECK-NEXT: [[INDVARS_IV_NEXT:%.*]] = add nuw nsw i64 [[INDVARS_IV]], 1
47 ;CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], 3
48 ;CHECK-NEXT: br label [[FOR_INC8_LOOPEXIT:%.*]]
49 ;CHECK: for.body3.split:
50 ;CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i64 [[INDVARS_IV]], 1
51 ;CHECK-NEXT: [[TMP4:%.*]] = icmp ne i64 [[TMP3]], 3
52 ;CHECK-NEXT: br i1 %4, label [[FOR_BODY3]], label [[FOR_END10:%.*]]
53 ;CHECK: for.inc8.loopexit:
54 ;CHECK-NEXT: br label [[FOR_INC8]]
56 ;CHECK-NEXT: [[INDVARS_IV_NEXT23]] = add nuw nsw i64 [[INDVARS_IV22]], 1
57 ;CHECK-NEXT: [[EXITCOND25:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT23]], 3
58 ;CHECK-NEXT: br i1 [[EXITCOND25]], label [[FOR_BODY]], label [[FOR_BODY3_SPLIT]]
60 ;CHECK-NEXT: [[TMP5:%.*]] = load i32, i32* @a
66 for.body: ; preds = %entry, %for.inc8
67 %indvars.iv22 = phi i64 [ 0, %entry ], [ %indvars.iv.next23, %for.inc8 ]
68 %tobool = icmp eq i64 %indvars.iv22, 0
69 br i1 %tobool, label %for.cond1.preheader, label %for.inc8
71 for.cond1.preheader: ; preds = %for.body
74 for.body3: ; preds = %for.cond1.preheader, %for.body3
75 %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next, %for.body3 ]
76 %0 = add nuw nsw i64 %indvars.iv22, 5
77 %arrayidx7 = getelementptr inbounds [3 x [5 x [8 x i16]]], [3 x [5 x [8 x i16]]]* @b, i64 0, i64 %indvars.iv, i64 %indvars.iv, i64 %0
78 %1 = load i16, i16* %arrayidx7
79 %conv = sext i16 %1 to i32
80 %2 = load i32, i32* @a
81 %or = or i32 %2, %conv
82 store i32 %or, i32* @a
83 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
84 %exitcond = icmp ne i64 %indvars.iv.next, 3
85 br i1 %exitcond, label %for.body3, label %for.inc8.loopexit
87 for.inc8.loopexit: ; preds = %for.body3
90 for.inc8: ; preds = %for.inc8.loopexit, %for.body
91 %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
92 %exitcond25 = icmp ne i64 %indvars.iv.next23, 3
93 br i1 %exitcond25, label %for.body, label %for.end10
95 for.end10: ; preds = %for.inc8
96 %3 = load i32, i32* @a
101 ; The innermost and the middle loop are interchanged.
107 ;; for (; g; g = g - 5) {
120 define void @test2() {
121 ; CHECK-LABEL: @test2(
123 ; CHECK-NEXT: br label [[OUTERMOST_HEADER:%.*]]
124 ; CHECK: outermost.header:
125 ; CHECK-NEXT: [[INDVAR_OUTERMOST:%.*]] = phi i32 [ 10, [[ENTRY:%.*]] ], [ [[INDVAR_OUTERMOST_NEXT:%.*]], [[OUTERMOST_LATCH:%.*]] ]
126 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* @a, align 4
127 ; CHECK-NEXT: [[TOBOOL71_I:%.*]] = icmp eq i32 [[TMP0]], 0
128 ; CHECK-NEXT: br label [[INNERMOST_PREHEADER:%.*]]
129 ; CHECK: middle.header.preheader:
130 ; CHECK-NEXT: br label [[MIDDLE_HEADER:%.*]]
131 ; CHECK: middle.header:
132 ; CHECK-NEXT: [[INDVAR_MIDDLE:%.*]] = phi i64 [ [[INDVAR_MIDDLE_NEXT:%.*]], [[MIDDLE_LATCH:%.*]] ], [ 4, [[MIDDLE_HEADER_PREHEADER:%.*]] ]
133 ; CHECK-NEXT: br i1 [[TOBOOL71_I]], label [[INNERMOST_BODY_SPLIT1:%.*]], label [[INNERMOST_BODY_SPLIT:%.*]]
134 ; CHECK: innermost.preheader:
135 ; CHECK-NEXT: br label [[INNERMOST_BODY:%.*]]
136 ; CHECK: innermost.body:
137 ; CHECK-NEXT: [[INDVAR_INNERMOST:%.*]] = phi i64 [ [[TMP1:%.*]], [[INNERMOST_BODY_SPLIT]] ], [ 4, [[INNERMOST_PREHEADER]] ]
138 ; CHECK-NEXT: br label [[MIDDLE_HEADER_PREHEADER]]
139 ; CHECK: innermost.body.split1:
140 ; CHECK-NEXT: [[ARRAYIDX9_I:%.*]] = getelementptr inbounds [1 x [6 x i32]], [1 x [6 x i32]]* @d, i64 0, i64 [[INDVAR_INNERMOST]], i64 [[INDVAR_MIDDLE]]
141 ; CHECK-NEXT: store i32 0, i32* [[ARRAYIDX9_I]], align 4
142 ; CHECK-NEXT: [[INDVAR_INNERMOST_NEXT:%.*]] = add nsw i64 [[INDVAR_INNERMOST]], -1
143 ; CHECK-NEXT: [[TOBOOL5_I:%.*]] = icmp eq i64 [[INDVAR_INNERMOST_NEXT]], 0
144 ; CHECK-NEXT: br label [[MIDDLE_LATCH_LOOPEXIT:%.*]]
145 ; CHECK: innermost.body.split:
146 ; CHECK-NEXT: [[TMP1]] = add nsw i64 [[INDVAR_INNERMOST]], -1
147 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 0
148 ; CHECK-NEXT: br i1 [[TMP2]], label [[OUTERMOST_LATCH]], label [[INNERMOST_BODY]]
149 ; CHECK: innermost.loopexit:
150 ; CHECK-NEXT: br label [[MIDDLE_LATCH]]
151 ; CHECK: middle.latch:
152 ; CHECK-NEXT: [[INDVAR_MIDDLE_NEXT]] = add nsw i64 [[INDVAR_MIDDLE]], -1
153 ; CHECK-NEXT: [[TOBOOL2_I:%.*]] = icmp eq i64 [[INDVAR_MIDDLE_NEXT]], 0
154 ; CHECK-NEXT: br i1 [[TOBOOL2_I]], label [[INNERMOST_BODY_SPLIT]], label [[MIDDLE_HEADER]]
155 ; CHECK: outermost.latch:
156 ; CHECK-NEXT: [[INDVAR_OUTERMOST_NEXT]] = add nsw i32 [[INDVAR_OUTERMOST]], -5
157 ; CHECK-NEXT: [[TOBOOL_I:%.*]] = icmp eq i32 [[INDVAR_OUTERMOST_NEXT]], 0
158 ; CHECK-NEXT: br i1 [[TOBOOL_I]], label [[OUTERMOST_EXIT:%.*]], label [[OUTERMOST_HEADER]]
159 ; CHECK: outermost.exit:
160 ; CHECK-NEXT: ret void
164 br label %outermost.header
166 outermost.header: ; preds = %outermost.latch, %entry
167 %indvar.outermost = phi i32 [ 10, %entry ], [ %indvar.outermost.next, %outermost.latch ]
168 %0 = load i32, i32* @a, align 4
169 %tobool71.i = icmp eq i32 %0, 0
170 br label %middle.header
172 middle.header: ; preds = %middle.latch, %outermost.header
173 %indvar.middle = phi i64 [ 4, %outermost.header ], [ %indvar.middle.next, %middle.latch ]
174 br i1 %tobool71.i, label %innermost.preheader, label %middle.latch
176 innermost.preheader: ; preds = %middle.header
177 br label %innermost.body
179 innermost.body: ; preds = %innermost.preheader, %innermost.body
180 %indvar.innermost = phi i64 [ %indvar.innermost.next, %innermost.body ], [ 4, %innermost.preheader ]
181 %arrayidx9.i = getelementptr inbounds [1 x [6 x i32]], [1 x [6 x i32]]* @d, i64 0, i64 %indvar.innermost, i64 %indvar.middle
182 store i32 0, i32* %arrayidx9.i, align 4
183 %indvar.innermost.next = add nsw i64 %indvar.innermost, -1
184 %tobool5.i = icmp eq i64 %indvar.innermost.next, 0
185 br i1 %tobool5.i, label %innermost.loopexit, label %innermost.body
187 innermost.loopexit: ; preds = %innermost.body
188 br label %middle.latch
190 middle.latch: ; preds = %middle.latch.loopexit, %middle.header
191 %indvar.middle.next = add nsw i64 %indvar.middle, -1
192 %tobool2.i = icmp eq i64 %indvar.middle.next, 0
193 br i1 %tobool2.i, label %outermost.latch, label %middle.header
195 outermost.latch: ; preds = %middle.latch
196 %indvar.outermost.next = add nsw i32 %indvar.outermost, -5
197 %tobool.i = icmp eq i32 %indvar.outermost.next, 0
198 br i1 %tobool.i, label %outermost.exit, label %outermost.header
200 outermost.exit: ; preds = %outermost.latch