blob: 24fa58317a979daa1915dd1d3ee5367e1096adee [file] [log] [blame]
Mingyao Yangf384f882014-10-22 16:08:18 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070018#include "bounds_check_elimination.h"
19#include "builder.h"
20#include "gvn.h"
Mingyao Yang0304e182015-01-30 16:41:29 -080021#include "instruction_simplifier.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070022#include "nodes.h"
23#include "optimizing_unit_test.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000024#include "side_effects_analysis.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070025
26#include "gtest/gtest.h"
27
28namespace art {
29
Mingyao Yang0304e182015-01-30 16:41:29 -080030static void RunSimplifierAndGvn(HGraph* graph) {
31 InstructionSimplifier simplify(graph);
32 simplify.Run();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +000033 SideEffectsAnalysis side_effects(graph);
34 side_effects.Run();
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000035 GVNOptimization(graph, side_effects).Run();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +000036}
37
Mingyao Yangf384f882014-10-22 16:08:18 -070038// if (i < 0) { array[i] = 1; // Can't eliminate. }
39// else if (i >= array.length) { array[i] = 1; // Can't eliminate. }
40// else { array[i] = 1; // Can eliminate. }
41TEST(BoundsCheckEliminationTest, NarrowingRangeArrayBoundsElimination) {
42 ArenaPool pool;
43 ArenaAllocator allocator(&pool);
44
45 HGraph* graph = new (&allocator) HGraph(&allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -080046 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -070047
48 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
49 graph->AddBlock(entry);
50 graph->SetEntryBlock(entry);
51 HInstruction* parameter1 = new (&allocator)
52 HParameterValue(0, Primitive::kPrimNot); // array
53 HInstruction* parameter2 = new (&allocator)
54 HParameterValue(0, Primitive::kPrimInt); // i
55 HInstruction* constant_1 = new (&allocator) HIntConstant(1);
56 HInstruction* constant_0 = new (&allocator) HIntConstant(0);
57 entry->AddInstruction(parameter1);
58 entry->AddInstruction(parameter2);
59 entry->AddInstruction(constant_1);
60 entry->AddInstruction(constant_0);
61
62 HBasicBlock* block1 = new (&allocator) HBasicBlock(graph);
63 graph->AddBlock(block1);
64 HInstruction* cmp = new (&allocator) HGreaterThanOrEqual(parameter2, constant_0);
65 HIf* if_inst = new (&allocator) HIf(cmp);
66 block1->AddInstruction(cmp);
67 block1->AddInstruction(if_inst);
68 entry->AddSuccessor(block1);
69
70 HBasicBlock* block2 = new (&allocator) HBasicBlock(graph);
71 graph->AddBlock(block2);
72 HNullCheck* null_check = new (&allocator) HNullCheck(parameter1, 0);
73 HArrayLength* array_length = new (&allocator) HArrayLength(null_check);
74 HBoundsCheck* bounds_check2 = new (&allocator)
75 HBoundsCheck(parameter2, array_length, 0);
76 HArraySet* array_set = new (&allocator) HArraySet(
77 null_check, bounds_check2, constant_1, Primitive::kPrimInt, 0);
78 block2->AddInstruction(null_check);
79 block2->AddInstruction(array_length);
80 block2->AddInstruction(bounds_check2);
81 block2->AddInstruction(array_set);
82
83 HBasicBlock* block3 = new (&allocator) HBasicBlock(graph);
84 graph->AddBlock(block3);
85 null_check = new (&allocator) HNullCheck(parameter1, 0);
86 array_length = new (&allocator) HArrayLength(null_check);
87 cmp = new (&allocator) HLessThan(parameter2, array_length);
88 if_inst = new (&allocator) HIf(cmp);
89 block3->AddInstruction(null_check);
90 block3->AddInstruction(array_length);
91 block3->AddInstruction(cmp);
92 block3->AddInstruction(if_inst);
93
94 HBasicBlock* block4 = new (&allocator) HBasicBlock(graph);
95 graph->AddBlock(block4);
96 null_check = new (&allocator) HNullCheck(parameter1, 0);
97 array_length = new (&allocator) HArrayLength(null_check);
98 HBoundsCheck* bounds_check4 = new (&allocator)
99 HBoundsCheck(parameter2, array_length, 0);
100 array_set = new (&allocator) HArraySet(
101 null_check, bounds_check4, constant_1, Primitive::kPrimInt, 0);
102 block4->AddInstruction(null_check);
103 block4->AddInstruction(array_length);
104 block4->AddInstruction(bounds_check4);
105 block4->AddInstruction(array_set);
106
107 HBasicBlock* block5 = new (&allocator) HBasicBlock(graph);
108 graph->AddBlock(block5);
109 null_check = new (&allocator) HNullCheck(parameter1, 0);
110 array_length = new (&allocator) HArrayLength(null_check);
111 HBoundsCheck* bounds_check5 = new (&allocator)
112 HBoundsCheck(parameter2, array_length, 0);
113 array_set = new (&allocator) HArraySet(
114 null_check, bounds_check5, constant_1, Primitive::kPrimInt, 0);
115 block5->AddInstruction(null_check);
116 block5->AddInstruction(array_length);
117 block5->AddInstruction(bounds_check5);
118 block5->AddInstruction(array_set);
119
120 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
121 graph->AddBlock(exit);
122 block2->AddSuccessor(exit);
123 block4->AddSuccessor(exit);
124 block5->AddSuccessor(exit);
125 exit->AddInstruction(new (&allocator) HExit());
126
127 block1->AddSuccessor(block3); // True successor
128 block1->AddSuccessor(block2); // False successor
129
130 block3->AddSuccessor(block5); // True successor
131 block3->AddSuccessor(block4); // False successor
132
133 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800134 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700135 BoundsCheckElimination bounds_check_elimination(graph);
136 bounds_check_elimination.Run();
137 ASSERT_FALSE(IsRemoved(bounds_check2));
138 ASSERT_FALSE(IsRemoved(bounds_check4));
139 ASSERT_TRUE(IsRemoved(bounds_check5));
140}
141
142// if (i > 0) {
143// // Positive number plus MAX_INT will overflow and be negative.
144// int j = i + Integer.MAX_VALUE;
145// if (j < array.length) array[j] = 1; // Can't eliminate.
146// }
147TEST(BoundsCheckEliminationTest, OverflowArrayBoundsElimination) {
148 ArenaPool pool;
149 ArenaAllocator allocator(&pool);
150
151 HGraph* graph = new (&allocator) HGraph(&allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800152 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700153
154 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
155 graph->AddBlock(entry);
156 graph->SetEntryBlock(entry);
157 HInstruction* parameter1 = new (&allocator)
158 HParameterValue(0, Primitive::kPrimNot); // array
159 HInstruction* parameter2 = new (&allocator)
160 HParameterValue(0, Primitive::kPrimInt); // i
161 HInstruction* constant_1 = new (&allocator) HIntConstant(1);
162 HInstruction* constant_0 = new (&allocator) HIntConstant(0);
163 HInstruction* constant_max_int = new (&allocator) HIntConstant(INT_MAX);
164 entry->AddInstruction(parameter1);
165 entry->AddInstruction(parameter2);
166 entry->AddInstruction(constant_1);
167 entry->AddInstruction(constant_0);
168 entry->AddInstruction(constant_max_int);
169
170 HBasicBlock* block1 = new (&allocator) HBasicBlock(graph);
171 graph->AddBlock(block1);
172 HInstruction* cmp = new (&allocator) HLessThanOrEqual(parameter2, constant_0);
173 HIf* if_inst = new (&allocator) HIf(cmp);
174 block1->AddInstruction(cmp);
175 block1->AddInstruction(if_inst);
176 entry->AddSuccessor(block1);
177
178 HBasicBlock* block2 = new (&allocator) HBasicBlock(graph);
179 graph->AddBlock(block2);
180 HInstruction* add = new (&allocator) HAdd(Primitive::kPrimInt, parameter2, constant_max_int);
181 HNullCheck* null_check = new (&allocator) HNullCheck(parameter1, 0);
182 HArrayLength* array_length = new (&allocator) HArrayLength(null_check);
183 HInstruction* cmp2 = new (&allocator) HGreaterThanOrEqual(add, array_length);
184 if_inst = new (&allocator) HIf(cmp2);
185 block2->AddInstruction(add);
186 block2->AddInstruction(null_check);
187 block2->AddInstruction(array_length);
188 block2->AddInstruction(cmp2);
189 block2->AddInstruction(if_inst);
190
191 HBasicBlock* block3 = new (&allocator) HBasicBlock(graph);
192 graph->AddBlock(block3);
193 HBoundsCheck* bounds_check = new (&allocator)
194 HBoundsCheck(add, array_length, 0);
195 HArraySet* array_set = new (&allocator) HArraySet(
196 null_check, bounds_check, constant_1, Primitive::kPrimInt, 0);
197 block3->AddInstruction(bounds_check);
198 block3->AddInstruction(array_set);
199
200 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
201 graph->AddBlock(exit);
202 exit->AddInstruction(new (&allocator) HExit());
Andreas Gampe0418b5b2014-12-04 17:24:50 -0800203 block1->AddSuccessor(exit); // true successor
204 block1->AddSuccessor(block2); // false successor
205 block2->AddSuccessor(exit); // true successor
206 block2->AddSuccessor(block3); // false successor
Mingyao Yangf384f882014-10-22 16:08:18 -0700207 block3->AddSuccessor(exit);
208
209 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800210 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700211 BoundsCheckElimination bounds_check_elimination(graph);
212 bounds_check_elimination.Run();
213 ASSERT_FALSE(IsRemoved(bounds_check));
214}
215
216// if (i < array.length) {
217// int j = i - Integer.MAX_VALUE;
218// j = j - Integer.MAX_VALUE; // j is (i+2) after substracting MAX_INT twice
219// if (j > 0) array[j] = 1; // Can't eliminate.
220// }
221TEST(BoundsCheckEliminationTest, UnderflowArrayBoundsElimination) {
222 ArenaPool pool;
223 ArenaAllocator allocator(&pool);
224
225 HGraph* graph = new (&allocator) HGraph(&allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800226 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700227
228 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
229 graph->AddBlock(entry);
230 graph->SetEntryBlock(entry);
231 HInstruction* parameter1 = new (&allocator)
232 HParameterValue(0, Primitive::kPrimNot); // array
233 HInstruction* parameter2 = new (&allocator)
234 HParameterValue(0, Primitive::kPrimInt); // i
235 HInstruction* constant_1 = new (&allocator) HIntConstant(1);
236 HInstruction* constant_0 = new (&allocator) HIntConstant(0);
237 HInstruction* constant_max_int = new (&allocator) HIntConstant(INT_MAX);
238 entry->AddInstruction(parameter1);
239 entry->AddInstruction(parameter2);
240 entry->AddInstruction(constant_1);
241 entry->AddInstruction(constant_0);
242 entry->AddInstruction(constant_max_int);
243
244 HBasicBlock* block1 = new (&allocator) HBasicBlock(graph);
245 graph->AddBlock(block1);
246 HNullCheck* null_check = new (&allocator) HNullCheck(parameter1, 0);
247 HArrayLength* array_length = new (&allocator) HArrayLength(null_check);
248 HInstruction* cmp = new (&allocator) HGreaterThanOrEqual(parameter2, array_length);
249 HIf* if_inst = new (&allocator) HIf(cmp);
250 block1->AddInstruction(null_check);
251 block1->AddInstruction(array_length);
252 block1->AddInstruction(cmp);
253 block1->AddInstruction(if_inst);
254 entry->AddSuccessor(block1);
255
256 HBasicBlock* block2 = new (&allocator) HBasicBlock(graph);
257 graph->AddBlock(block2);
258 HInstruction* sub1 = new (&allocator) HSub(Primitive::kPrimInt, parameter2, constant_max_int);
259 HInstruction* sub2 = new (&allocator) HSub(Primitive::kPrimInt, sub1, constant_max_int);
260 HInstruction* cmp2 = new (&allocator) HLessThanOrEqual(sub2, constant_0);
261 if_inst = new (&allocator) HIf(cmp2);
262 block2->AddInstruction(sub1);
263 block2->AddInstruction(sub2);
264 block2->AddInstruction(cmp2);
265 block2->AddInstruction(if_inst);
266
267 HBasicBlock* block3 = new (&allocator) HBasicBlock(graph);
268 graph->AddBlock(block3);
269 HBoundsCheck* bounds_check = new (&allocator)
270 HBoundsCheck(sub2, array_length, 0);
271 HArraySet* array_set = new (&allocator) HArraySet(
272 null_check, bounds_check, constant_1, Primitive::kPrimInt, 0);
273 block3->AddInstruction(bounds_check);
274 block3->AddInstruction(array_set);
275
276 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
277 graph->AddBlock(exit);
278 exit->AddInstruction(new (&allocator) HExit());
Andreas Gampe0418b5b2014-12-04 17:24:50 -0800279 block1->AddSuccessor(exit); // true successor
280 block1->AddSuccessor(block2); // false successor
281 block2->AddSuccessor(exit); // true successor
282 block2->AddSuccessor(block3); // false successor
Mingyao Yangf384f882014-10-22 16:08:18 -0700283 block3->AddSuccessor(exit);
284
285 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800286 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700287 BoundsCheckElimination bounds_check_elimination(graph);
288 bounds_check_elimination.Run();
289 ASSERT_FALSE(IsRemoved(bounds_check));
290}
291
Andreas Gampe0ba62732015-03-24 02:39:46 +0000292// array[5] = 1; // Can't eliminate.
Mingyao Yange295e6e2015-03-07 06:37:59 -0800293// array[4] = 1; // Can eliminate.
Andreas Gampe0ba62732015-03-24 02:39:46 +0000294// array[6] = 1; // Can't eliminate.
Mingyao Yangf384f882014-10-22 16:08:18 -0700295TEST(BoundsCheckEliminationTest, ConstantArrayBoundsElimination) {
296 ArenaPool pool;
297 ArenaAllocator allocator(&pool);
298
299 HGraph* graph = new (&allocator) HGraph(&allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800300 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700301
302 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
303 graph->AddBlock(entry);
304 graph->SetEntryBlock(entry);
305 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
306 HInstruction* constant_5 = new (&allocator) HIntConstant(5);
307 HInstruction* constant_4 = new (&allocator) HIntConstant(4);
308 HInstruction* constant_6 = new (&allocator) HIntConstant(6);
309 HInstruction* constant_1 = new (&allocator) HIntConstant(1);
310 entry->AddInstruction(parameter);
311 entry->AddInstruction(constant_5);
312 entry->AddInstruction(constant_4);
313 entry->AddInstruction(constant_6);
314 entry->AddInstruction(constant_1);
315
316 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
317 graph->AddBlock(block);
318 entry->AddSuccessor(block);
319
320 HNullCheck* null_check = new (&allocator) HNullCheck(parameter, 0);
321 HArrayLength* array_length = new (&allocator) HArrayLength(null_check);
322 HBoundsCheck* bounds_check5 = new (&allocator)
323 HBoundsCheck(constant_5, array_length, 0);
Andreas Gampe0ba62732015-03-24 02:39:46 +0000324 HInstruction* array_set = new (&allocator) HArraySet(
Mingyao Yangf384f882014-10-22 16:08:18 -0700325 null_check, bounds_check5, constant_1, Primitive::kPrimInt, 0);
326 block->AddInstruction(null_check);
327 block->AddInstruction(array_length);
328 block->AddInstruction(bounds_check5);
329 block->AddInstruction(array_set);
330
331 null_check = new (&allocator) HNullCheck(parameter, 0);
332 array_length = new (&allocator) HArrayLength(null_check);
333 HBoundsCheck* bounds_check4 = new (&allocator)
334 HBoundsCheck(constant_4, array_length, 0);
335 array_set = new (&allocator) HArraySet(
336 null_check, bounds_check4, constant_1, Primitive::kPrimInt, 0);
337 block->AddInstruction(null_check);
338 block->AddInstruction(array_length);
339 block->AddInstruction(bounds_check4);
340 block->AddInstruction(array_set);
341
Andreas Gampe0ba62732015-03-24 02:39:46 +0000342 null_check = new (&allocator) HNullCheck(parameter, 0);
343 array_length = new (&allocator) HArrayLength(null_check);
344 HBoundsCheck* bounds_check6 = new (&allocator)
345 HBoundsCheck(constant_6, array_length, 0);
346 array_set = new (&allocator) HArraySet(
347 null_check, bounds_check6, constant_1, Primitive::kPrimInt, 0);
348 block->AddInstruction(null_check);
349 block->AddInstruction(array_length);
350 block->AddInstruction(bounds_check6);
351 block->AddInstruction(array_set);
352
Mingyao Yangf384f882014-10-22 16:08:18 -0700353 block->AddInstruction(new (&allocator) HGoto());
354
355 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
356 graph->AddBlock(exit);
357 block->AddSuccessor(exit);
358 exit->AddInstruction(new (&allocator) HExit());
359
360 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800361 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700362 BoundsCheckElimination bounds_check_elimination(graph);
363 bounds_check_elimination.Run();
Andreas Gampe0ba62732015-03-24 02:39:46 +0000364 ASSERT_FALSE(IsRemoved(bounds_check5));
Mingyao Yange295e6e2015-03-07 06:37:59 -0800365 ASSERT_TRUE(IsRemoved(bounds_check4));
Andreas Gampe0ba62732015-03-24 02:39:46 +0000366 ASSERT_FALSE(IsRemoved(bounds_check6));
Mingyao Yangf384f882014-10-22 16:08:18 -0700367}
368
369// for (int i=initial; i<array.length; i+=increment) { array[i] = 10; }
370static HGraph* BuildSSAGraph1(ArenaAllocator* allocator,
371 HInstruction** bounds_check,
372 int initial,
373 int increment,
374 IfCondition cond = kCondGE) {
375 HGraph* graph = new (allocator) HGraph(allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800376 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700377
378 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
379 graph->AddBlock(entry);
380 graph->SetEntryBlock(entry);
381 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
382 HInstruction* constant_initial = new (allocator) HIntConstant(initial);
383 HInstruction* constant_increment = new (allocator) HIntConstant(increment);
384 HInstruction* constant_10 = new (allocator) HIntConstant(10);
385 entry->AddInstruction(parameter);
386 entry->AddInstruction(constant_initial);
387 entry->AddInstruction(constant_increment);
388 entry->AddInstruction(constant_10);
389
390 HBasicBlock* block = new (allocator) HBasicBlock(graph);
391 graph->AddBlock(block);
392 entry->AddSuccessor(block);
393 block->AddInstruction(new (allocator) HGoto());
394
395 HBasicBlock* loop_header = new (allocator) HBasicBlock(graph);
396 HBasicBlock* loop_body = new (allocator) HBasicBlock(graph);
397 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
398
399 graph->AddBlock(loop_header);
400 graph->AddBlock(loop_body);
401 graph->AddBlock(exit);
402 block->AddSuccessor(loop_header);
403 loop_header->AddSuccessor(exit); // true successor
404 loop_header->AddSuccessor(loop_body); // false successor
405 loop_body->AddSuccessor(loop_header);
406
407 HPhi* phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700408 HInstruction* null_check = new (allocator) HNullCheck(parameter, 0);
409 HInstruction* array_length = new (allocator) HArrayLength(null_check);
410 HInstruction* cmp = nullptr;
411 if (cond == kCondGE) {
412 cmp = new (allocator) HGreaterThanOrEqual(phi, array_length);
413 } else {
414 DCHECK(cond == kCondGT);
415 cmp = new (allocator) HGreaterThan(phi, array_length);
416 }
417 HInstruction* if_inst = new (allocator) HIf(cmp);
418 loop_header->AddPhi(phi);
419 loop_header->AddInstruction(null_check);
420 loop_header->AddInstruction(array_length);
421 loop_header->AddInstruction(cmp);
422 loop_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000423 phi->AddInput(constant_initial);
Mingyao Yangf384f882014-10-22 16:08:18 -0700424
425 null_check = new (allocator) HNullCheck(parameter, 0);
426 array_length = new (allocator) HArrayLength(null_check);
427 *bounds_check = new (allocator) HBoundsCheck(phi, array_length, 0);
428 HInstruction* array_set = new (allocator) HArraySet(
429 null_check, *bounds_check, constant_10, Primitive::kPrimInt, 0);
430
431 HInstruction* add = new (allocator) HAdd(Primitive::kPrimInt, phi, constant_increment);
432 loop_body->AddInstruction(null_check);
433 loop_body->AddInstruction(array_length);
434 loop_body->AddInstruction(*bounds_check);
435 loop_body->AddInstruction(array_set);
436 loop_body->AddInstruction(add);
437 loop_body->AddInstruction(new (allocator) HGoto());
438 phi->AddInput(add);
439
440 exit->AddInstruction(new (allocator) HExit());
441
442 return graph;
443}
444
445TEST(BoundsCheckEliminationTest, LoopArrayBoundsElimination1) {
446 ArenaPool pool;
447 ArenaAllocator allocator(&pool);
448
449 // for (int i=0; i<array.length; i++) { array[i] = 10; // Can eliminate with gvn. }
450 HInstruction* bounds_check = nullptr;
451 HGraph* graph = BuildSSAGraph1(&allocator, &bounds_check, 0, 1);
452 graph->BuildDominatorTree();
453 BoundsCheckElimination bounds_check_elimination(graph);
454 bounds_check_elimination.Run();
455 ASSERT_FALSE(IsRemoved(bounds_check));
456
457 // This time add gvn. Need gvn to eliminate the second
458 // HArrayLength which uses the null check as its input.
459 graph = BuildSSAGraph1(&allocator, &bounds_check, 0, 1);
460 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800461 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700462 BoundsCheckElimination bounds_check_elimination_after_gvn(graph);
463 bounds_check_elimination_after_gvn.Run();
464 ASSERT_TRUE(IsRemoved(bounds_check));
465
466 // for (int i=1; i<array.length; i++) { array[i] = 10; // Can eliminate. }
467 graph = BuildSSAGraph1(&allocator, &bounds_check, 1, 1);
468 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800469 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700470 BoundsCheckElimination bounds_check_elimination_with_initial_1(graph);
471 bounds_check_elimination_with_initial_1.Run();
472 ASSERT_TRUE(IsRemoved(bounds_check));
473
474 // for (int i=-1; i<array.length; i++) { array[i] = 10; // Can't eliminate. }
475 graph = BuildSSAGraph1(&allocator, &bounds_check, -1, 1);
476 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800477 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700478 BoundsCheckElimination bounds_check_elimination_with_initial_minus_1(graph);
479 bounds_check_elimination_with_initial_minus_1.Run();
480 ASSERT_FALSE(IsRemoved(bounds_check));
481
482 // for (int i=0; i<=array.length; i++) { array[i] = 10; // Can't eliminate. }
483 graph = BuildSSAGraph1(&allocator, &bounds_check, 0, 1, kCondGT);
484 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800485 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700486 BoundsCheckElimination bounds_check_elimination_with_greater_than(graph);
487 bounds_check_elimination_with_greater_than.Run();
488 ASSERT_FALSE(IsRemoved(bounds_check));
489
490 // for (int i=0; i<array.length; i += 2) {
491 // array[i] = 10; // Can't eliminate due to overflow concern. }
492 graph = BuildSSAGraph1(&allocator, &bounds_check, 0, 2);
493 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800494 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700495 BoundsCheckElimination bounds_check_elimination_with_increment_2(graph);
496 bounds_check_elimination_with_increment_2.Run();
497 ASSERT_FALSE(IsRemoved(bounds_check));
498
499 // for (int i=1; i<array.length; i += 2) { array[i] = 10; // Can eliminate. }
500 graph = BuildSSAGraph1(&allocator, &bounds_check, 1, 2);
501 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800502 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700503 BoundsCheckElimination bounds_check_elimination_with_increment_2_from_1(graph);
504 bounds_check_elimination_with_increment_2_from_1.Run();
505 ASSERT_TRUE(IsRemoved(bounds_check));
506}
507
508// for (int i=array.length; i>0; i+=increment) { array[i-1] = 10; }
509static HGraph* BuildSSAGraph2(ArenaAllocator* allocator,
510 HInstruction** bounds_check,
511 int initial,
512 int increment = -1,
513 IfCondition cond = kCondLE) {
514 HGraph* graph = new (allocator) HGraph(allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800515 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700516
517 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
518 graph->AddBlock(entry);
519 graph->SetEntryBlock(entry);
520 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
521 HInstruction* constant_initial = new (allocator) HIntConstant(initial);
522 HInstruction* constant_increment = new (allocator) HIntConstant(increment);
523 HInstruction* constant_minus_1 = new (allocator) HIntConstant(-1);
524 HInstruction* constant_10 = new (allocator) HIntConstant(10);
525 entry->AddInstruction(parameter);
526 entry->AddInstruction(constant_initial);
527 entry->AddInstruction(constant_increment);
528 entry->AddInstruction(constant_minus_1);
529 entry->AddInstruction(constant_10);
530
531 HBasicBlock* block = new (allocator) HBasicBlock(graph);
532 graph->AddBlock(block);
533 entry->AddSuccessor(block);
534 HInstruction* null_check = new (allocator) HNullCheck(parameter, 0);
535 HInstruction* array_length = new (allocator) HArrayLength(null_check);
536 block->AddInstruction(null_check);
537 block->AddInstruction(array_length);
538 block->AddInstruction(new (allocator) HGoto());
539
540 HBasicBlock* loop_header = new (allocator) HBasicBlock(graph);
541 HBasicBlock* loop_body = new (allocator) HBasicBlock(graph);
542 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
543
544 graph->AddBlock(loop_header);
545 graph->AddBlock(loop_body);
546 graph->AddBlock(exit);
547 block->AddSuccessor(loop_header);
548 loop_header->AddSuccessor(exit); // true successor
549 loop_header->AddSuccessor(loop_body); // false successor
550 loop_body->AddSuccessor(loop_header);
551
552 HPhi* phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700553 HInstruction* cmp = nullptr;
554 if (cond == kCondLE) {
555 cmp = new (allocator) HLessThanOrEqual(phi, constant_initial);
556 } else {
557 DCHECK(cond == kCondLT);
558 cmp = new (allocator) HLessThan(phi, constant_initial);
559 }
560 HInstruction* if_inst = new (allocator) HIf(cmp);
561 loop_header->AddPhi(phi);
562 loop_header->AddInstruction(cmp);
563 loop_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000564 phi->AddInput(array_length);
Mingyao Yangf384f882014-10-22 16:08:18 -0700565
566 HInstruction* add = new (allocator) HAdd(Primitive::kPrimInt, phi, constant_minus_1);
567 null_check = new (allocator) HNullCheck(parameter, 0);
568 array_length = new (allocator) HArrayLength(null_check);
569 *bounds_check = new (allocator) HBoundsCheck(add, array_length, 0);
570 HInstruction* array_set = new (allocator) HArraySet(
571 null_check, *bounds_check, constant_10, Primitive::kPrimInt, 0);
572 HInstruction* add_phi = new (allocator) HAdd(Primitive::kPrimInt, phi, constant_increment);
573 loop_body->AddInstruction(add);
574 loop_body->AddInstruction(null_check);
575 loop_body->AddInstruction(array_length);
576 loop_body->AddInstruction(*bounds_check);
577 loop_body->AddInstruction(array_set);
578 loop_body->AddInstruction(add_phi);
579 loop_body->AddInstruction(new (allocator) HGoto());
580 phi->AddInput(add);
581
582 exit->AddInstruction(new (allocator) HExit());
583
584 return graph;
585}
586
587TEST(BoundsCheckEliminationTest, LoopArrayBoundsElimination2) {
588 ArenaPool pool;
589 ArenaAllocator allocator(&pool);
590
591 // for (int i=array.length; i>0; i--) { array[i-1] = 10; // Can eliminate with gvn. }
592 HInstruction* bounds_check = nullptr;
593 HGraph* graph = BuildSSAGraph2(&allocator, &bounds_check, 0);
594 graph->BuildDominatorTree();
595 BoundsCheckElimination bounds_check_elimination(graph);
596 bounds_check_elimination.Run();
597 ASSERT_FALSE(IsRemoved(bounds_check));
598
599 // This time add gvn. Need gvn to eliminate the second
600 // HArrayLength which uses the null check as its input.
601 graph = BuildSSAGraph2(&allocator, &bounds_check, 0);
602 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800603 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700604 BoundsCheckElimination bounds_check_elimination_after_gvn(graph);
605 bounds_check_elimination_after_gvn.Run();
606 ASSERT_TRUE(IsRemoved(bounds_check));
607
608 // for (int i=array.length; i>1; i--) { array[i-1] = 10; // Can eliminate. }
609 graph = BuildSSAGraph2(&allocator, &bounds_check, 1);
610 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800611 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700612 BoundsCheckElimination bounds_check_elimination_with_initial_1(graph);
613 bounds_check_elimination_with_initial_1.Run();
614 ASSERT_TRUE(IsRemoved(bounds_check));
615
616 // for (int i=array.length; i>-1; i--) { array[i-1] = 10; // Can't eliminate. }
617 graph = BuildSSAGraph2(&allocator, &bounds_check, -1);
618 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800619 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700620 BoundsCheckElimination bounds_check_elimination_with_initial_minus_1(graph);
621 bounds_check_elimination_with_initial_minus_1.Run();
622 ASSERT_FALSE(IsRemoved(bounds_check));
623
624 // for (int i=array.length; i>=0; i--) { array[i-1] = 10; // Can't eliminate. }
625 graph = BuildSSAGraph2(&allocator, &bounds_check, 0, -1, kCondLT);
626 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800627 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700628 BoundsCheckElimination bounds_check_elimination_with_less_than(graph);
629 bounds_check_elimination_with_less_than.Run();
630 ASSERT_FALSE(IsRemoved(bounds_check));
631
632 // for (int i=array.length; i>0; i-=2) { array[i-1] = 10; // Can eliminate. }
633 graph = BuildSSAGraph2(&allocator, &bounds_check, 0, -2);
634 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800635 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700636 BoundsCheckElimination bounds_check_elimination_increment_minus_2(graph);
637 bounds_check_elimination_increment_minus_2.Run();
638 ASSERT_TRUE(IsRemoved(bounds_check));
639}
640
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800641// int[] array = new int[10];
Mingyao Yangf384f882014-10-22 16:08:18 -0700642// for (int i=0; i<10; i+=increment) { array[i] = 10; }
643static HGraph* BuildSSAGraph3(ArenaAllocator* allocator,
644 HInstruction** bounds_check,
645 int initial,
646 int increment,
647 IfCondition cond) {
648 HGraph* graph = new (allocator) HGraph(allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800649 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700650
651 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
652 graph->AddBlock(entry);
653 graph->SetEntryBlock(entry);
654 HInstruction* constant_10 = new (allocator) HIntConstant(10);
655 HInstruction* constant_initial = new (allocator) HIntConstant(initial);
656 HInstruction* constant_increment = new (allocator) HIntConstant(increment);
657 entry->AddInstruction(constant_10);
658 entry->AddInstruction(constant_initial);
659 entry->AddInstruction(constant_increment);
660
661 HBasicBlock* block = new (allocator) HBasicBlock(graph);
662 graph->AddBlock(block);
663 entry->AddSuccessor(block);
664 HInstruction* new_array = new (allocator)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000665 HNewArray(constant_10, 0, Primitive::kPrimInt, kQuickAllocArray);
Mingyao Yangf384f882014-10-22 16:08:18 -0700666 block->AddInstruction(new_array);
667 block->AddInstruction(new (allocator) HGoto());
668
669 HBasicBlock* loop_header = new (allocator) HBasicBlock(graph);
670 HBasicBlock* loop_body = new (allocator) HBasicBlock(graph);
671 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
672
673 graph->AddBlock(loop_header);
674 graph->AddBlock(loop_body);
675 graph->AddBlock(exit);
676 block->AddSuccessor(loop_header);
677 loop_header->AddSuccessor(exit); // true successor
678 loop_header->AddSuccessor(loop_body); // false successor
679 loop_body->AddSuccessor(loop_header);
680
681 HPhi* phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700682 HInstruction* cmp = nullptr;
683 if (cond == kCondGE) {
684 cmp = new (allocator) HGreaterThanOrEqual(phi, constant_10);
685 } else {
686 DCHECK(cond == kCondGT);
687 cmp = new (allocator) HGreaterThan(phi, constant_10);
688 }
689 HInstruction* if_inst = new (allocator) HIf(cmp);
690 loop_header->AddPhi(phi);
691 loop_header->AddInstruction(cmp);
692 loop_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000693 phi->AddInput(constant_initial);
Mingyao Yangf384f882014-10-22 16:08:18 -0700694
695 HNullCheck* null_check = new (allocator) HNullCheck(new_array, 0);
696 HArrayLength* array_length = new (allocator) HArrayLength(null_check);
697 *bounds_check = new (allocator) HBoundsCheck(phi, array_length, 0);
698 HInstruction* array_set = new (allocator) HArraySet(
699 null_check, *bounds_check, constant_10, Primitive::kPrimInt, 0);
700 HInstruction* add = new (allocator) HAdd(Primitive::kPrimInt, phi, constant_increment);
701 loop_body->AddInstruction(null_check);
702 loop_body->AddInstruction(array_length);
703 loop_body->AddInstruction(*bounds_check);
704 loop_body->AddInstruction(array_set);
705 loop_body->AddInstruction(add);
706 loop_body->AddInstruction(new (allocator) HGoto());
707 phi->AddInput(add);
708
709 exit->AddInstruction(new (allocator) HExit());
710
711 return graph;
712}
713
714TEST(BoundsCheckEliminationTest, LoopArrayBoundsElimination3) {
715 ArenaPool pool;
716 ArenaAllocator allocator(&pool);
717
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800718 // int[] array = new int[10];
Mingyao Yangf384f882014-10-22 16:08:18 -0700719 // for (int i=0; i<10; i++) { array[i] = 10; // Can eliminate. }
720 HInstruction* bounds_check = nullptr;
721 HGraph* graph = BuildSSAGraph3(&allocator, &bounds_check, 0, 1, kCondGE);
722 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800723 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700724 BoundsCheckElimination bounds_check_elimination_after_gvn(graph);
725 bounds_check_elimination_after_gvn.Run();
726 ASSERT_TRUE(IsRemoved(bounds_check));
727
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800728 // int[] array = new int[10];
Mingyao Yangf384f882014-10-22 16:08:18 -0700729 // for (int i=1; i<10; i++) { array[i] = 10; // Can eliminate. }
730 graph = BuildSSAGraph3(&allocator, &bounds_check, 1, 1, kCondGE);
731 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800732 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700733 BoundsCheckElimination bounds_check_elimination_with_initial_1(graph);
734 bounds_check_elimination_with_initial_1.Run();
735 ASSERT_TRUE(IsRemoved(bounds_check));
736
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800737 // int[] array = new int[10];
Mingyao Yangf384f882014-10-22 16:08:18 -0700738 // for (int i=0; i<=10; i++) { array[i] = 10; // Can't eliminate. }
739 graph = BuildSSAGraph3(&allocator, &bounds_check, 0, 1, kCondGT);
740 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800741 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700742 BoundsCheckElimination bounds_check_elimination_with_greater_than(graph);
743 bounds_check_elimination_with_greater_than.Run();
744 ASSERT_FALSE(IsRemoved(bounds_check));
745
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800746 // int[] array = new int[10];
Mingyao Yangf384f882014-10-22 16:08:18 -0700747 // for (int i=1; i<10; i+=8) { array[i] = 10; // Can eliminate. }
748 graph = BuildSSAGraph3(&allocator, &bounds_check, 1, 8, kCondGE);
749 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800750 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700751 BoundsCheckElimination bounds_check_elimination_increment_8(graph);
752 bounds_check_elimination_increment_8.Run();
753 ASSERT_TRUE(IsRemoved(bounds_check));
754}
755
756// for (int i=initial; i<array.length; i++) { array[array.length-i-1] = 10; }
757static HGraph* BuildSSAGraph4(ArenaAllocator* allocator,
758 HInstruction** bounds_check,
759 int initial,
760 IfCondition cond = kCondGE) {
761 HGraph* graph = new (allocator) HGraph(allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800762 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700763
764 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
765 graph->AddBlock(entry);
766 graph->SetEntryBlock(entry);
767 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
768 HInstruction* constant_initial = new (allocator) HIntConstant(initial);
769 HInstruction* constant_1 = new (allocator) HIntConstant(1);
770 HInstruction* constant_10 = new (allocator) HIntConstant(10);
771 HInstruction* constant_minus_1 = new (allocator) HIntConstant(-1);
772 entry->AddInstruction(parameter);
773 entry->AddInstruction(constant_initial);
774 entry->AddInstruction(constant_1);
775 entry->AddInstruction(constant_10);
776 entry->AddInstruction(constant_minus_1);
777
778 HBasicBlock* block = new (allocator) HBasicBlock(graph);
779 graph->AddBlock(block);
780 entry->AddSuccessor(block);
781 block->AddInstruction(new (allocator) HGoto());
782
783 HBasicBlock* loop_header = new (allocator) HBasicBlock(graph);
784 HBasicBlock* loop_body = new (allocator) HBasicBlock(graph);
785 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
786
787 graph->AddBlock(loop_header);
788 graph->AddBlock(loop_body);
789 graph->AddBlock(exit);
790 block->AddSuccessor(loop_header);
791 loop_header->AddSuccessor(exit); // true successor
792 loop_header->AddSuccessor(loop_body); // false successor
793 loop_body->AddSuccessor(loop_header);
794
795 HPhi* phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700796 HInstruction* null_check = new (allocator) HNullCheck(parameter, 0);
797 HInstruction* array_length = new (allocator) HArrayLength(null_check);
798 HInstruction* cmp = nullptr;
799 if (cond == kCondGE) {
800 cmp = new (allocator) HGreaterThanOrEqual(phi, array_length);
801 } else if (cond == kCondGT) {
802 cmp = new (allocator) HGreaterThan(phi, array_length);
803 }
804 HInstruction* if_inst = new (allocator) HIf(cmp);
805 loop_header->AddPhi(phi);
806 loop_header->AddInstruction(null_check);
807 loop_header->AddInstruction(array_length);
808 loop_header->AddInstruction(cmp);
809 loop_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000810 phi->AddInput(constant_initial);
Mingyao Yangf384f882014-10-22 16:08:18 -0700811
812 null_check = new (allocator) HNullCheck(parameter, 0);
813 array_length = new (allocator) HArrayLength(null_check);
814 HInstruction* sub = new (allocator) HSub(Primitive::kPrimInt, array_length, phi);
815 HInstruction* add_minus_1 = new (allocator)
816 HAdd(Primitive::kPrimInt, sub, constant_minus_1);
817 *bounds_check = new (allocator) HBoundsCheck(add_minus_1, array_length, 0);
818 HInstruction* array_set = new (allocator) HArraySet(
819 null_check, *bounds_check, constant_10, Primitive::kPrimInt, 0);
820 HInstruction* add = new (allocator) HAdd(Primitive::kPrimInt, phi, constant_1);
821 loop_body->AddInstruction(null_check);
822 loop_body->AddInstruction(array_length);
823 loop_body->AddInstruction(sub);
824 loop_body->AddInstruction(add_minus_1);
825 loop_body->AddInstruction(*bounds_check);
826 loop_body->AddInstruction(array_set);
827 loop_body->AddInstruction(add);
828 loop_body->AddInstruction(new (allocator) HGoto());
829 phi->AddInput(add);
830
831 exit->AddInstruction(new (allocator) HExit());
832
833 return graph;
834}
835
836TEST(BoundsCheckEliminationTest, LoopArrayBoundsElimination4) {
837 ArenaPool pool;
838 ArenaAllocator allocator(&pool);
839
840 // for (int i=0; i<array.length; i++) { array[array.length-i-1] = 10; // Can eliminate with gvn. }
841 HInstruction* bounds_check = nullptr;
842 HGraph* graph = BuildSSAGraph4(&allocator, &bounds_check, 0);
843 graph->BuildDominatorTree();
844 BoundsCheckElimination bounds_check_elimination(graph);
845 bounds_check_elimination.Run();
846 ASSERT_FALSE(IsRemoved(bounds_check));
847
848 // This time add gvn. Need gvn to eliminate the second
849 // HArrayLength which uses the null check as its input.
850 graph = BuildSSAGraph4(&allocator, &bounds_check, 0);
851 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800852 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700853 BoundsCheckElimination bounds_check_elimination_after_gvn(graph);
854 bounds_check_elimination_after_gvn.Run();
855 ASSERT_TRUE(IsRemoved(bounds_check));
856
857 // for (int i=1; i<array.length; i++) { array[array.length-i-1] = 10; // Can eliminate. }
858 graph = BuildSSAGraph4(&allocator, &bounds_check, 1);
859 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800860 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700861 BoundsCheckElimination bounds_check_elimination_with_initial_1(graph);
862 bounds_check_elimination_with_initial_1.Run();
863 ASSERT_TRUE(IsRemoved(bounds_check));
864
865 // for (int i=0; i<=array.length; i++) { array[array.length-i] = 10; // Can't eliminate. }
866 graph = BuildSSAGraph4(&allocator, &bounds_check, 0, kCondGT);
867 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -0800868 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700869 BoundsCheckElimination bounds_check_elimination_with_greater_than(graph);
870 bounds_check_elimination_with_greater_than.Run();
871 ASSERT_FALSE(IsRemoved(bounds_check));
872}
873
874// Bubble sort:
875// (Every array access bounds-check can be eliminated.)
876// for (int i=0; i<array.length-1; i++) {
877// for (int j=0; j<array.length-i-1; j++) {
878// if (array[j] > array[j+1]) {
879// int temp = array[j+1];
880// array[j+1] = array[j];
881// array[j] = temp;
882// }
883// }
884// }
885TEST(BoundsCheckEliminationTest, BubbleSortArrayBoundsElimination) {
886 ArenaPool pool;
887 ArenaAllocator allocator(&pool);
888
889 HGraph* graph = new (&allocator) HGraph(&allocator);
Mingyao Yang6775ba52015-03-04 12:10:34 -0800890 graph->SetHasArrayAccesses(true);
Mingyao Yangf384f882014-10-22 16:08:18 -0700891
892 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
893 graph->AddBlock(entry);
894 graph->SetEntryBlock(entry);
895 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
896 HInstruction* constant_0 = new (&allocator) HIntConstant(0);
897 HInstruction* constant_minus_1 = new (&allocator) HIntConstant(-1);
898 HInstruction* constant_1 = new (&allocator) HIntConstant(1);
899 entry->AddInstruction(parameter);
900 entry->AddInstruction(constant_0);
901 entry->AddInstruction(constant_minus_1);
902 entry->AddInstruction(constant_1);
903
904 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
905 graph->AddBlock(block);
906 entry->AddSuccessor(block);
907 block->AddInstruction(new (&allocator) HGoto());
908
909 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
910 graph->AddBlock(exit);
911 exit->AddInstruction(new (&allocator) HExit());
912
913 HBasicBlock* outer_header = new (&allocator) HBasicBlock(graph);
914 graph->AddBlock(outer_header);
915 HPhi* phi_i = new (&allocator) HPhi(&allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700916 HNullCheck* null_check = new (&allocator) HNullCheck(parameter, 0);
917 HArrayLength* array_length = new (&allocator) HArrayLength(null_check);
918 HAdd* add = new (&allocator) HAdd(Primitive::kPrimInt, array_length, constant_minus_1);
919 HInstruction* cmp = new (&allocator) HGreaterThanOrEqual(phi_i, add);
920 HIf* if_inst = new (&allocator) HIf(cmp);
921 outer_header->AddPhi(phi_i);
922 outer_header->AddInstruction(null_check);
923 outer_header->AddInstruction(array_length);
924 outer_header->AddInstruction(add);
925 outer_header->AddInstruction(cmp);
926 outer_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000927 phi_i->AddInput(constant_0);
Mingyao Yangf384f882014-10-22 16:08:18 -0700928
929 HBasicBlock* inner_header = new (&allocator) HBasicBlock(graph);
930 graph->AddBlock(inner_header);
931 HPhi* phi_j = new (&allocator) HPhi(&allocator, 0, 0, Primitive::kPrimInt);
Mingyao Yangf384f882014-10-22 16:08:18 -0700932 null_check = new (&allocator) HNullCheck(parameter, 0);
933 array_length = new (&allocator) HArrayLength(null_check);
934 HSub* sub = new (&allocator) HSub(Primitive::kPrimInt, array_length, phi_i);
935 add = new (&allocator) HAdd(Primitive::kPrimInt, sub, constant_minus_1);
936 cmp = new (&allocator) HGreaterThanOrEqual(phi_j, add);
937 if_inst = new (&allocator) HIf(cmp);
938 inner_header->AddPhi(phi_j);
939 inner_header->AddInstruction(null_check);
940 inner_header->AddInstruction(array_length);
941 inner_header->AddInstruction(sub);
942 inner_header->AddInstruction(add);
943 inner_header->AddInstruction(cmp);
944 inner_header->AddInstruction(if_inst);
David Brazdil1abb4192015-02-17 18:33:36 +0000945 phi_j->AddInput(constant_0);
Mingyao Yangf384f882014-10-22 16:08:18 -0700946
947 HBasicBlock* inner_body_compare = new (&allocator) HBasicBlock(graph);
948 graph->AddBlock(inner_body_compare);
949 null_check = new (&allocator) HNullCheck(parameter, 0);
950 array_length = new (&allocator) HArrayLength(null_check);
951 HBoundsCheck* bounds_check1 = new (&allocator) HBoundsCheck(phi_j, array_length, 0);
952 HArrayGet* array_get_j = new (&allocator)
953 HArrayGet(null_check, bounds_check1, Primitive::kPrimInt);
954 inner_body_compare->AddInstruction(null_check);
955 inner_body_compare->AddInstruction(array_length);
956 inner_body_compare->AddInstruction(bounds_check1);
957 inner_body_compare->AddInstruction(array_get_j);
958 HInstruction* j_plus_1 = new (&allocator) HAdd(Primitive::kPrimInt, phi_j, constant_1);
959 null_check = new (&allocator) HNullCheck(parameter, 0);
960 array_length = new (&allocator) HArrayLength(null_check);
961 HBoundsCheck* bounds_check2 = new (&allocator) HBoundsCheck(j_plus_1, array_length, 0);
962 HArrayGet* array_get_j_plus_1 = new (&allocator)
963 HArrayGet(null_check, bounds_check2, Primitive::kPrimInt);
964 cmp = new (&allocator) HGreaterThanOrEqual(array_get_j, array_get_j_plus_1);
965 if_inst = new (&allocator) HIf(cmp);
966 inner_body_compare->AddInstruction(j_plus_1);
967 inner_body_compare->AddInstruction(null_check);
968 inner_body_compare->AddInstruction(array_length);
969 inner_body_compare->AddInstruction(bounds_check2);
970 inner_body_compare->AddInstruction(array_get_j_plus_1);
971 inner_body_compare->AddInstruction(cmp);
972 inner_body_compare->AddInstruction(if_inst);
973
974 HBasicBlock* inner_body_swap = new (&allocator) HBasicBlock(graph);
975 graph->AddBlock(inner_body_swap);
976 j_plus_1 = new (&allocator) HAdd(Primitive::kPrimInt, phi_j, constant_1);
977 // temp = array[j+1]
978 null_check = new (&allocator) HNullCheck(parameter, 0);
979 array_length = new (&allocator) HArrayLength(null_check);
980 HInstruction* bounds_check3 = new (&allocator) HBoundsCheck(j_plus_1, array_length, 0);
981 array_get_j_plus_1 = new (&allocator)
982 HArrayGet(null_check, bounds_check3, Primitive::kPrimInt);
983 inner_body_swap->AddInstruction(j_plus_1);
984 inner_body_swap->AddInstruction(null_check);
985 inner_body_swap->AddInstruction(array_length);
986 inner_body_swap->AddInstruction(bounds_check3);
987 inner_body_swap->AddInstruction(array_get_j_plus_1);
988 // array[j+1] = array[j]
989 null_check = new (&allocator) HNullCheck(parameter, 0);
990 array_length = new (&allocator) HArrayLength(null_check);
991 HInstruction* bounds_check4 = new (&allocator) HBoundsCheck(phi_j, array_length, 0);
992 array_get_j = new (&allocator)
993 HArrayGet(null_check, bounds_check4, Primitive::kPrimInt);
994 inner_body_swap->AddInstruction(null_check);
995 inner_body_swap->AddInstruction(array_length);
996 inner_body_swap->AddInstruction(bounds_check4);
997 inner_body_swap->AddInstruction(array_get_j);
998 null_check = new (&allocator) HNullCheck(parameter, 0);
999 array_length = new (&allocator) HArrayLength(null_check);
1000 HInstruction* bounds_check5 = new (&allocator) HBoundsCheck(j_plus_1, array_length, 0);
1001 HArraySet* array_set_j_plus_1 = new (&allocator)
1002 HArraySet(null_check, bounds_check5, array_get_j, Primitive::kPrimInt, 0);
1003 inner_body_swap->AddInstruction(null_check);
1004 inner_body_swap->AddInstruction(array_length);
1005 inner_body_swap->AddInstruction(bounds_check5);
1006 inner_body_swap->AddInstruction(array_set_j_plus_1);
1007 // array[j] = temp
1008 null_check = new (&allocator) HNullCheck(parameter, 0);
1009 array_length = new (&allocator) HArrayLength(null_check);
1010 HInstruction* bounds_check6 = new (&allocator) HBoundsCheck(phi_j, array_length, 0);
1011 HArraySet* array_set_j = new (&allocator)
1012 HArraySet(null_check, bounds_check6, array_get_j_plus_1, Primitive::kPrimInt, 0);
1013 inner_body_swap->AddInstruction(null_check);
1014 inner_body_swap->AddInstruction(array_length);
1015 inner_body_swap->AddInstruction(bounds_check6);
1016 inner_body_swap->AddInstruction(array_set_j);
1017 inner_body_swap->AddInstruction(new (&allocator) HGoto());
1018
1019 HBasicBlock* inner_body_add = new (&allocator) HBasicBlock(graph);
1020 graph->AddBlock(inner_body_add);
1021 add = new (&allocator) HAdd(Primitive::kPrimInt, phi_j, constant_1);
1022 inner_body_add->AddInstruction(add);
1023 inner_body_add->AddInstruction(new (&allocator) HGoto());
1024 phi_j->AddInput(add);
1025
1026 HBasicBlock* outer_body_add = new (&allocator) HBasicBlock(graph);
1027 graph->AddBlock(outer_body_add);
1028 add = new (&allocator) HAdd(Primitive::kPrimInt, phi_i, constant_1);
1029 outer_body_add->AddInstruction(add);
1030 outer_body_add->AddInstruction(new (&allocator) HGoto());
1031 phi_i->AddInput(add);
1032
1033 block->AddSuccessor(outer_header);
1034 outer_header->AddSuccessor(exit);
1035 outer_header->AddSuccessor(inner_header);
1036 inner_header->AddSuccessor(outer_body_add);
1037 inner_header->AddSuccessor(inner_body_compare);
1038 inner_body_compare->AddSuccessor(inner_body_add);
1039 inner_body_compare->AddSuccessor(inner_body_swap);
1040 inner_body_swap->AddSuccessor(inner_body_add);
1041 inner_body_add->AddSuccessor(inner_header);
1042 outer_body_add->AddSuccessor(outer_header);
1043
1044 graph->BuildDominatorTree();
Mingyao Yang0304e182015-01-30 16:41:29 -08001045 RunSimplifierAndGvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -07001046 // gvn should remove the same bounds check.
1047 ASSERT_FALSE(IsRemoved(bounds_check1));
1048 ASSERT_FALSE(IsRemoved(bounds_check2));
1049 ASSERT_TRUE(IsRemoved(bounds_check3));
1050 ASSERT_TRUE(IsRemoved(bounds_check4));
1051 ASSERT_TRUE(IsRemoved(bounds_check5));
1052 ASSERT_TRUE(IsRemoved(bounds_check6));
1053
1054 BoundsCheckElimination bounds_check_elimination(graph);
1055 bounds_check_elimination.Run();
1056 ASSERT_TRUE(IsRemoved(bounds_check1));
1057 ASSERT_TRUE(IsRemoved(bounds_check2));
1058 ASSERT_TRUE(IsRemoved(bounds_check3));
1059 ASSERT_TRUE(IsRemoved(bounds_check4));
1060 ASSERT_TRUE(IsRemoved(bounds_check5));
1061 ASSERT_TRUE(IsRemoved(bounds_check6));
1062}
1063
1064} // namespace art