1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=loop-interchange -cache-line-size=4 -loop-interchange-threshold=-100 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
4 ; Make sure the loops are in LCSSA form after loop interchange,
5 ; and loop interchange does not hit assertion errors and crash.
7 target triple = "x86_64-unknown-linux-gnu"
9 @b = external global [512 x [4 x i32]], align 1
10 @c = external global [2 x [4 x i32]], align 1
11 @d = external global [1024 x [512 x [4 x i32]]], align 1
13 define void @test1() {
14 ; CHECK-LABEL: @test1(
16 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER_PREHEADER:%.*]]
17 ; CHECK: for.cond33.preheader.preheader:
18 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER:%.*]]
19 ; CHECK: for.cond33.preheader:
20 ; CHECK-NEXT: [[I_011:%.*]] = phi i16 [ [[INC69:%.*]], [[FOR_END67:%.*]] ], [ 0, [[FOR_COND33_PREHEADER_PREHEADER:%.*]] ]
21 ; CHECK-NEXT: br label [[FOR_BODY42_SPLIT1:%.*]]
22 ; CHECK: for.body42.preheader:
23 ; CHECK-NEXT: br label [[FOR_BODY42:%.*]]
24 ; CHECK: for.cond37.preheader.preheader:
25 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER:%.*]]
26 ; CHECK: for.cond37.preheader:
27 ; CHECK-NEXT: [[J_010:%.*]] = phi i16 [ [[INC66:%.*]], [[FOR_END64:%.*]] ], [ 0, [[FOR_COND37_PREHEADER_PREHEADER]] ]
28 ; CHECK-NEXT: br label [[FOR_BODY42_PREHEADER:%.*]]
30 ; CHECK-NEXT: [[K_09:%.*]] = phi i16 [ [[TMP1:%.*]], [[FOR_BODY42_SPLIT:%.*]] ], [ -512, [[FOR_BODY42_PREHEADER]] ]
31 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER_PREHEADER]]
32 ; CHECK: for.body42.split1:
33 ; CHECK-NEXT: [[SUB51:%.*]] = add nsw i16 [[K_09]], 512
34 ; CHECK-NEXT: [[ARRAYIDX55:%.*]] = getelementptr inbounds [512 x [4 x i32]], ptr @b, i16 0, i16 [[SUB51]], i16 [[J_010]]
35 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX55]], align 1
36 ; CHECK-NEXT: [[ADD61:%.*]] = add i32 undef, undef
37 ; CHECK-NEXT: [[INC63:%.*]] = add nsw i16 [[K_09]], 1
38 ; CHECK-NEXT: br label [[FOR_END67]]
39 ; CHECK: for.body42.split:
40 ; CHECK-NEXT: [[ADD61_LCSSA:%.*]] = phi i32 [ [[ADD61]], [[FOR_END67]] ]
41 ; CHECK-NEXT: [[TMP1]] = add nsw i16 [[K_09]], 1
42 ; CHECK-NEXT: br i1 true, label [[FOR_END64]], label [[FOR_BODY42]]
44 ; CHECK-NEXT: [[ADD61_LCSSA_LCSSA:%.*]] = phi i32 [ [[ADD61_LCSSA]], [[FOR_BODY42_SPLIT]] ]
45 ; CHECK-NEXT: store i32 [[ADD61_LCSSA_LCSSA]], ptr undef, align 1
46 ; CHECK-NEXT: [[INC66]] = add nuw nsw i16 [[J_010]], 1
47 ; CHECK-NEXT: br i1 true, label [[FOR_COND75_PREHEADER:%.*]], label [[FOR_COND37_PREHEADER]]
49 ; CHECK-NEXT: [[INC69]] = add nuw nsw i16 [[I_011]], 1
50 ; CHECK-NEXT: [[EXITCOND13_NOT:%.*]] = icmp eq i16 [[INC69]], 2
51 ; CHECK-NEXT: br i1 [[EXITCOND13_NOT]], label [[FOR_BODY42_SPLIT]], label [[FOR_COND33_PREHEADER]]
52 ; CHECK: for.cond75.preheader:
53 ; CHECK-NEXT: br label [[FOR_COND75:%.*]]
55 ; CHECK-NEXT: br label [[FOR_COND75]]
58 br label %for.cond33.preheader
60 for.cond33.preheader: ; preds = %for.end67, %entry
61 %i.011 = phi i16 [ 0, %entry ], [ %inc69, %for.end67 ]
62 br label %for.cond37.preheader
64 for.cond37.preheader: ; preds = %for.end64, %for.cond33.preheader
65 %j.010 = phi i16 [ 0, %for.cond33.preheader ], [ %inc66, %for.end64 ]
68 for.body42: ; preds = %for.body42, %for.cond37.preheader
69 %k.09 = phi i16 [ -512, %for.cond37.preheader ], [ %inc63, %for.body42 ]
70 %sub51 = add nsw i16 %k.09, 512
71 %arrayidx55 = getelementptr inbounds [512 x [4 x i32]], ptr @b, i16 0, i16 %sub51, i16 %j.010
72 %0 = load i32, ptr %arrayidx55, align 1
73 %add61 = add i32 undef, undef
74 %inc63 = add nsw i16 %k.09, 1
75 br i1 true, label %for.end64, label %for.body42
77 for.end64: ; preds = %for.body42
78 store i32 %add61, ptr undef, align 1
79 %inc66 = add nuw nsw i16 %j.010, 1
80 br i1 true, label %for.end67, label %for.cond37.preheader
82 for.end67: ; preds = %for.end64
83 %inc69 = add nuw nsw i16 %i.011, 1
84 %exitcond13.not = icmp eq i16 %inc69, 2
85 br i1 %exitcond13.not, label %for.cond75, label %for.cond33.preheader
87 for.cond75: ; preds = %for.cond75, %for.end67
92 ; Make sure that we split the phi nodes in the middle loop header
93 ; into a separate basic block to avoid the situation where use of
94 ; the outermost indvar appears before its def after interchanging
95 ; the outermost and the middle loop. Otherwise loop interchange
98 define void @test2() {
99 ; CHECK-LABEL: @test2(
101 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER_PREHEADER:%.*]]
102 ; CHECK: for.cond33.preheader.preheader:
103 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER:%.*]]
104 ; CHECK: for.cond33.preheader:
105 ; CHECK-NEXT: [[I_166:%.*]] = phi i16 [ [[INC69:%.*]], [[FOR_INC68:%.*]] ], [ 0, [[FOR_COND33_PREHEADER_PREHEADER:%.*]] ]
106 ; CHECK-NEXT: [[ARRAYIDX60:%.*]] = getelementptr inbounds [2 x [4 x i32]], ptr @c, i16 0, i16 [[I_166]], i16 [[J_165:%.*]]
107 ; CHECK-NEXT: br label [[VECTOR_BODY85_SPLIT1:%.*]]
108 ; CHECK: for.cond37.preheader.preheader:
109 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER:%.*]]
110 ; CHECK: for.cond37.preheader:
111 ; CHECK-NEXT: [[J_165]] = phi i16 [ [[INC66:%.*]], [[MIDDLE_BLOCK80:%.*]] ], [ 0, [[FOR_COND37_PREHEADER_PREHEADER]] ]
112 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER_SPLIT:%.*]]
113 ; CHECK: for.cond37.preheader.split:
114 ; CHECK-NEXT: br label [[VECTOR_BODY85:%.*]]
115 ; CHECK: vector.body85:
116 ; CHECK-NEXT: [[INDEX86:%.*]] = phi i16 [ 0, [[FOR_COND37_PREHEADER_SPLIT]] ], [ [[TMP3:%.*]], [[VECTOR_BODY85_SPLIT:%.*]] ]
117 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER_PREHEADER]]
118 ; CHECK: vector.body85.split1:
119 ; CHECK-NEXT: [[TMP0:%.*]] = or disjoint i16 [[INDEX86]], 2
120 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [512 x [4 x i32]], ptr @b, i16 0, i16 [[TMP0]], i16 [[J_165]]
121 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 1
122 ; CHECK-NEXT: [[INDEX_NEXT87:%.*]] = add nuw i16 [[INDEX86]], 4
123 ; CHECK-NEXT: br label [[FOR_INC68]]
124 ; CHECK: vector.body85.split:
125 ; CHECK-NEXT: [[TMP3]] = add nuw i16 [[INDEX86]], 4
126 ; CHECK-NEXT: br i1 true, label [[MIDDLE_BLOCK80]], label [[VECTOR_BODY85]]
127 ; CHECK: middle.block80:
128 ; CHECK-NEXT: [[INC66]] = add nuw nsw i16 [[J_165]], 1
129 ; CHECK-NEXT: br i1 true, label [[FOR_COND75_PREHEADER:%.*]], label [[FOR_COND37_PREHEADER]]
131 ; CHECK-NEXT: [[INC69]] = add nuw nsw i16 [[I_166]], 1
132 ; CHECK-NEXT: [[EXITCOND77_NOT:%.*]] = icmp eq i16 [[INC69]], 2
133 ; CHECK-NEXT: br i1 [[EXITCOND77_NOT]], label [[VECTOR_BODY85_SPLIT]], label [[FOR_COND33_PREHEADER]]
134 ; CHECK: for.cond75.preheader:
135 ; CHECK-NEXT: unreachable
138 br label %for.cond33.preheader
140 for.cond33.preheader: ; preds = %for.inc68, %entry
141 %i.166 = phi i16 [ %inc69, %for.inc68 ], [ 0, %entry ]
142 br label %for.cond37.preheader
144 for.cond37.preheader: ; preds = %middle.block80, %for.cond33.preheader
145 %j.165 = phi i16 [ 0, %for.cond33.preheader ], [ %inc66, %middle.block80 ]
146 %arrayidx60 = getelementptr inbounds [2 x [4 x i32]], ptr @c, i16 0, i16 %i.166, i16 %j.165
147 br label %vector.body85
149 vector.body85: ; preds = %vector.body85, %for.cond37.preheader
150 %index86 = phi i16 [ 0, %for.cond37.preheader ], [ %index.next87, %vector.body85 ]
151 %0 = or disjoint i16 %index86, 2
152 %1 = getelementptr inbounds [512 x [4 x i32]], ptr @b, i16 0, i16 %0, i16 %j.165
153 %2 = load i32, ptr %1, align 1
154 %index.next87 = add nuw i16 %index86, 4
155 br i1 undef, label %middle.block80, label %vector.body85
157 middle.block80: ; preds = %vector.body85
158 %inc66 = add nuw nsw i16 %j.165, 1
159 br i1 undef, label %for.inc68, label %for.cond37.preheader
161 for.inc68: ; preds = %middle.block80
162 %inc69 = add nuw nsw i16 %i.166, 1
163 %exitcond77.not = icmp eq i16 %inc69, 2
164 br i1 %exitcond77.not, label %for.cond75.preheader, label %for.cond33.preheader
166 for.cond75.preheader: ; preds = %for.inc68
170 ; Same as test1, but with a third index on the GEP
171 define void @test3() {
172 ; CHECK-LABEL: @test3(
174 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER_PREHEADER:%.*]]
175 ; CHECK: for.cond33.preheader.preheader:
176 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER:%.*]]
177 ; CHECK: for.cond33.preheader:
178 ; CHECK-NEXT: [[I_011:%.*]] = phi i16 [ [[INC69:%.*]], [[FOR_END67:%.*]] ], [ 0, [[FOR_COND33_PREHEADER_PREHEADER:%.*]] ]
179 ; CHECK-NEXT: br label [[FOR_BODY42_SPLIT1:%.*]]
180 ; CHECK: for.body42.preheader:
181 ; CHECK-NEXT: br label [[FOR_BODY42:%.*]]
182 ; CHECK: for.cond38.preheader.preheader:
183 ; CHECK-NEXT: br label [[FOR_COND38_PREHEADER:%.*]]
184 ; CHECK: for.cond37.preheader.preheader:
185 ; CHECK-NEXT: br label [[FOR_COND37_PREHEADER:%.*]]
186 ; CHECK: for.cond37.preheader:
187 ; CHECK-NEXT: [[J_010:%.*]] = phi i16 [ [[INC66:%.*]], [[FOR_END64:%.*]] ], [ 0, [[FOR_COND37_PREHEADER_PREHEADER]] ]
188 ; CHECK-NEXT: br label [[FOR_COND38_PREHEADER_PREHEADER:%.*]]
189 ; CHECK: for.cond38.preheader:
190 ; CHECK-NEXT: [[K_010:%.*]] = phi i16 [ [[INC67:%.*]], [[FOR_END65:%.*]] ], [ 0, [[FOR_COND38_PREHEADER_PREHEADER]] ]
191 ; CHECK-NEXT: br label [[FOR_BODY42_PREHEADER:%.*]]
193 ; CHECK-NEXT: [[K_09:%.*]] = phi i16 [ [[TMP1:%.*]], [[FOR_BODY42_SPLIT:%.*]] ], [ -512, [[FOR_BODY42_PREHEADER]] ]
194 ; CHECK-NEXT: br label [[FOR_COND33_PREHEADER_PREHEADER]]
195 ; CHECK: for.body42.split1:
196 ; CHECK-NEXT: [[SUB51:%.*]] = add nsw i16 [[K_09]], 512
197 ; CHECK-NEXT: [[ARRAYIDX55:%.*]] = getelementptr inbounds [1024 x [512 x [4 x i32]]], ptr @d, i16 0, i16 [[SUB51]], i16 [[J_010]], i16 [[K_010]]
198 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX55]], align 1
199 ; CHECK-NEXT: [[ADD61:%.*]] = add i32 undef, undef
200 ; CHECK-NEXT: [[INC63:%.*]] = add nsw i16 [[K_09]], 1
201 ; CHECK-NEXT: br label [[FOR_END67]]
202 ; CHECK: for.body42.split:
203 ; CHECK-NEXT: [[ADD61_LCSSA:%.*]] = phi i32 [ [[ADD61]], [[FOR_END67]] ]
204 ; CHECK-NEXT: [[TMP1]] = add nsw i16 [[K_09]], 1
205 ; CHECK-NEXT: br i1 true, label [[FOR_END65]], label [[FOR_BODY42]]
207 ; CHECK-NEXT: [[ADD61_LCSSA_LCSSA:%.*]] = phi i32 [ [[ADD61_LCSSA]], [[FOR_BODY42_SPLIT]] ]
208 ; CHECK-NEXT: store i32 [[ADD61_LCSSA_LCSSA]], ptr undef, align 1
209 ; CHECK-NEXT: [[INC67]] = add nuw nsw i16 [[K_010]], 1
210 ; CHECK-NEXT: br i1 true, label [[FOR_END64]], label [[FOR_COND38_PREHEADER]]
212 ; CHECK-NEXT: [[INC66]] = add nuw nsw i16 [[J_010]], 1
213 ; CHECK-NEXT: br i1 true, label [[FOR_COND75_PREHEADER:%.*]], label [[FOR_COND37_PREHEADER]]
215 ; CHECK-NEXT: [[INC69]] = add nuw nsw i16 [[I_011]], 1
216 ; CHECK-NEXT: [[EXITCOND13_NOT:%.*]] = icmp eq i16 [[INC69]], 2
217 ; CHECK-NEXT: br i1 [[EXITCOND13_NOT]], label [[FOR_BODY42_SPLIT]], label [[FOR_COND33_PREHEADER]]
218 ; CHECK: for.cond75.preheader:
219 ; CHECK-NEXT: br label [[FOR_COND75:%.*]]
221 ; CHECK-NEXT: br label [[FOR_COND75]]
224 br label %for.cond33.preheader
226 for.cond33.preheader: ; preds = %for.end67, %entry
227 %i.011 = phi i16 [ 0, %entry ], [ %inc69, %for.end67 ]
228 br label %for.cond37.preheader
230 for.cond37.preheader: ; preds = %for.end64, %for.cond33.preheader
231 %j.010 = phi i16 [ 0, %for.cond33.preheader ], [ %inc66, %for.end64 ]
232 br label %for.cond38.preheader
234 for.cond38.preheader: ; preds = %for.end65, %for.cond37.preheader
235 %k.010 = phi i16 [ 0, %for.cond37.preheader ], [ %inc67, %for.end65 ]
238 for.body42: ; preds = %for.body42, %for.cond38.preheader
239 %k.09 = phi i16 [ -512, %for.cond38.preheader ], [ %inc63, %for.body42 ]
240 %sub51 = add nsw i16 %k.09, 512
241 %arrayidx55 = getelementptr inbounds [1024 x [512 x [4 x i32]]], ptr @d, i16 0, i16 %sub51, i16 %j.010, i16 %k.010
242 %0 = load i32, ptr %arrayidx55, align 1
243 %add61 = add i32 undef, undef
244 %inc63 = add nsw i16 %k.09, 1
245 br i1 true, label %for.end65, label %for.body42
247 for.end65: ; preds = %for.body42
248 store i32 %add61, ptr undef, align 1
249 %inc67 = add nuw nsw i16 %k.010, 1
250 br i1 true, label %for.end64, label %for.cond38.preheader
252 for.end64: ; preds = %for.end65
253 %inc66 = add nuw nsw i16 %j.010, 1
254 br i1 true, label %for.end67, label %for.cond37.preheader
256 for.end67: ; preds = %for.end64
257 %inc69 = add nuw nsw i16 %i.011, 1
258 %exitcond13.not = icmp eq i16 %inc69, 2
259 br i1 %exitcond13.not, label %for.cond75, label %for.cond33.preheader
261 for.cond75: ; preds = %for.cond75, %for.end67