| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "bounds_check_elimination.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include "base/arena_allocator.h" |
| Vladimir Marko | e272715 | 2019-10-10 10:46:42 +0100 | [diff] [blame^] | 20 | #include "base/macros.h" |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 21 | #include "builder.h" |
| 22 | #include "gvn.h" |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 23 | #include "induction_var_analysis.h" |
| Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 24 | #include "instruction_simplifier.h" |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 25 | #include "nodes.h" |
| 26 | #include "optimizing_unit_test.h" |
| Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 27 | #include "side_effects_analysis.h" |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 28 | |
| 29 | #include "gtest/gtest.h" |
| 30 | |
| Vladimir Marko | e272715 | 2019-10-10 10:46:42 +0100 | [diff] [blame^] | 31 | namespace art HIDDEN { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 32 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Fixture class for the BoundsCheckElimination tests. |
| 35 | */ |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 36 | class BoundsCheckEliminationTest : public OptimizingUnitTest { |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 37 | public: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 38 | BoundsCheckEliminationTest() : graph_(CreateGraph()) { |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 39 | graph_->SetHasBoundsChecks(true); |
| 40 | } |
| 41 | |
| 42 | ~BoundsCheckEliminationTest() { } |
| 43 | |
| 44 | void RunBCE() { |
| 45 | graph_->BuildDominatorTree(); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 46 | |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 47 | InstructionSimplifier(graph_, /* codegen= */ nullptr).Run(); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 48 | |
| 49 | SideEffectsAnalysis side_effects(graph_); |
| 50 | side_effects.Run(); |
| 51 | |
| 52 | GVNOptimization(graph_, side_effects).Run(); |
| 53 | |
| 54 | HInductionVarAnalysis induction(graph_); |
| 55 | induction.Run(); |
| 56 | |
| Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 57 | BoundsCheckElimination(graph_, side_effects, &induction).Run(); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 60 | HGraph* graph_; |
| 61 | }; |
| 62 | |
| Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 63 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 64 | // if (i < 0) { array[i] = 1; // Can't eliminate. } |
| 65 | // else if (i >= array.length) { array[i] = 1; // Can't eliminate. } |
| 66 | // else { array[i] = 1; // Can eliminate. } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 67 | TEST_F(BoundsCheckEliminationTest, NarrowingRangeArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 68 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 69 | graph_->AddBlock(entry); |
| 70 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 71 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 72 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); // array |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 73 | HInstruction* parameter2 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 74 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); // i |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 75 | entry->AddInstruction(parameter1); |
| 76 | entry->AddInstruction(parameter2); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 77 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 78 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| 79 | HInstruction* constant_0 = graph_->GetIntConstant(0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 80 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 81 | HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 82 | graph_->AddBlock(block1); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 83 | HInstruction* cmp = new (GetAllocator()) HGreaterThanOrEqual(parameter2, constant_0); |
| 84 | HIf* if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 85 | block1->AddInstruction(cmp); |
| 86 | block1->AddInstruction(if_inst); |
| 87 | entry->AddSuccessor(block1); |
| 88 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 89 | HBasicBlock* block2 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 90 | graph_->AddBlock(block2); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 91 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 92 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 93 | HBoundsCheck* bounds_check2 = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 94 | HBoundsCheck(parameter2, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 95 | HArraySet* array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 96 | null_check, bounds_check2, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 97 | block2->AddInstruction(null_check); |
| 98 | block2->AddInstruction(array_length); |
| 99 | block2->AddInstruction(bounds_check2); |
| 100 | block2->AddInstruction(array_set); |
| 101 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 102 | HBasicBlock* block3 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 103 | graph_->AddBlock(block3); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 104 | null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 105 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 106 | cmp = new (GetAllocator()) HLessThan(parameter2, array_length); |
| 107 | if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 108 | block3->AddInstruction(null_check); |
| 109 | block3->AddInstruction(array_length); |
| 110 | block3->AddInstruction(cmp); |
| 111 | block3->AddInstruction(if_inst); |
| 112 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 113 | HBasicBlock* block4 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 114 | graph_->AddBlock(block4); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 115 | null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 116 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 117 | HBoundsCheck* bounds_check4 = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 118 | HBoundsCheck(parameter2, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 119 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 120 | null_check, bounds_check4, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 121 | block4->AddInstruction(null_check); |
| 122 | block4->AddInstruction(array_length); |
| 123 | block4->AddInstruction(bounds_check4); |
| 124 | block4->AddInstruction(array_set); |
| 125 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 126 | HBasicBlock* block5 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 127 | graph_->AddBlock(block5); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 128 | null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 129 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 130 | HBoundsCheck* bounds_check5 = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 131 | HBoundsCheck(parameter2, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 132 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 133 | null_check, bounds_check5, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 134 | block5->AddInstruction(null_check); |
| 135 | block5->AddInstruction(array_length); |
| 136 | block5->AddInstruction(bounds_check5); |
| 137 | block5->AddInstruction(array_set); |
| 138 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 139 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 140 | graph_->AddBlock(exit); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 141 | block2->AddSuccessor(exit); |
| 142 | block4->AddSuccessor(exit); |
| 143 | block5->AddSuccessor(exit); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 144 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 145 | |
| 146 | block1->AddSuccessor(block3); // True successor |
| 147 | block1->AddSuccessor(block2); // False successor |
| 148 | |
| 149 | block3->AddSuccessor(block5); // True successor |
| 150 | block3->AddSuccessor(block4); // False successor |
| 151 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 152 | RunBCE(); |
| 153 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 154 | ASSERT_FALSE(IsRemoved(bounds_check2)); |
| 155 | ASSERT_FALSE(IsRemoved(bounds_check4)); |
| 156 | ASSERT_TRUE(IsRemoved(bounds_check5)); |
| 157 | } |
| 158 | |
| 159 | // if (i > 0) { |
| 160 | // // Positive number plus MAX_INT will overflow and be negative. |
| 161 | // int j = i + Integer.MAX_VALUE; |
| 162 | // if (j < array.length) array[j] = 1; // Can't eliminate. |
| 163 | // } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 164 | TEST_F(BoundsCheckEliminationTest, OverflowArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 165 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 166 | graph_->AddBlock(entry); |
| 167 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 168 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 169 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); // array |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 170 | HInstruction* parameter2 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 171 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); // i |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 172 | entry->AddInstruction(parameter1); |
| 173 | entry->AddInstruction(parameter2); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 174 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 175 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| 176 | HInstruction* constant_0 = graph_->GetIntConstant(0); |
| 177 | HInstruction* constant_max_int = graph_->GetIntConstant(INT_MAX); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 178 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 179 | HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 180 | graph_->AddBlock(block1); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 181 | HInstruction* cmp = new (GetAllocator()) HLessThanOrEqual(parameter2, constant_0); |
| 182 | HIf* if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 183 | block1->AddInstruction(cmp); |
| 184 | block1->AddInstruction(if_inst); |
| 185 | entry->AddSuccessor(block1); |
| 186 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 187 | HBasicBlock* block2 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 188 | graph_->AddBlock(block2); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 189 | HInstruction* add = |
| 190 | new (GetAllocator()) HAdd(DataType::Type::kInt32, parameter2, constant_max_int); |
| 191 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 192 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 193 | HInstruction* cmp2 = new (GetAllocator()) HGreaterThanOrEqual(add, array_length); |
| 194 | if_inst = new (GetAllocator()) HIf(cmp2); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 195 | block2->AddInstruction(add); |
| 196 | block2->AddInstruction(null_check); |
| 197 | block2->AddInstruction(array_length); |
| 198 | block2->AddInstruction(cmp2); |
| 199 | block2->AddInstruction(if_inst); |
| 200 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 201 | HBasicBlock* block3 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 202 | graph_->AddBlock(block3); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 203 | HBoundsCheck* bounds_check = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 204 | HBoundsCheck(add, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 205 | HArraySet* array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 206 | null_check, bounds_check, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 207 | block3->AddInstruction(bounds_check); |
| 208 | block3->AddInstruction(array_set); |
| 209 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 210 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 211 | graph_->AddBlock(exit); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 212 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| Andreas Gampe | 0418b5b | 2014-12-04 17:24:50 -0800 | [diff] [blame] | 213 | block1->AddSuccessor(exit); // true successor |
| 214 | block1->AddSuccessor(block2); // false successor |
| 215 | block2->AddSuccessor(exit); // true successor |
| 216 | block2->AddSuccessor(block3); // false successor |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 217 | block3->AddSuccessor(exit); |
| 218 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 219 | RunBCE(); |
| 220 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 221 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| 222 | } |
| 223 | |
| 224 | // if (i < array.length) { |
| 225 | // int j = i - Integer.MAX_VALUE; |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 226 | // j = j - Integer.MAX_VALUE; // j is (i+2) after subtracting MAX_INT twice |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 227 | // if (j > 0) array[j] = 1; // Can't eliminate. |
| 228 | // } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 229 | TEST_F(BoundsCheckEliminationTest, UnderflowArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 230 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 231 | graph_->AddBlock(entry); |
| 232 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 233 | HInstruction* parameter1 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 234 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); // array |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 235 | HInstruction* parameter2 = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 236 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); // i |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 237 | entry->AddInstruction(parameter1); |
| 238 | entry->AddInstruction(parameter2); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 239 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 240 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| 241 | HInstruction* constant_0 = graph_->GetIntConstant(0); |
| 242 | HInstruction* constant_max_int = graph_->GetIntConstant(INT_MAX); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 243 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 244 | HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 245 | graph_->AddBlock(block1); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 246 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(parameter1, 0); |
| 247 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 248 | HInstruction* cmp = new (GetAllocator()) HGreaterThanOrEqual(parameter2, array_length); |
| 249 | HIf* if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 250 | block1->AddInstruction(null_check); |
| 251 | block1->AddInstruction(array_length); |
| 252 | block1->AddInstruction(cmp); |
| 253 | block1->AddInstruction(if_inst); |
| 254 | entry->AddSuccessor(block1); |
| 255 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 256 | HBasicBlock* block2 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 257 | graph_->AddBlock(block2); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 258 | HInstruction* sub1 = |
| 259 | new (GetAllocator()) HSub(DataType::Type::kInt32, parameter2, constant_max_int); |
| 260 | HInstruction* sub2 = new (GetAllocator()) HSub(DataType::Type::kInt32, sub1, constant_max_int); |
| 261 | HInstruction* cmp2 = new (GetAllocator()) HLessThanOrEqual(sub2, constant_0); |
| 262 | if_inst = new (GetAllocator()) HIf(cmp2); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 263 | block2->AddInstruction(sub1); |
| 264 | block2->AddInstruction(sub2); |
| 265 | block2->AddInstruction(cmp2); |
| 266 | block2->AddInstruction(if_inst); |
| 267 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 268 | HBasicBlock* block3 = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 269 | graph_->AddBlock(block3); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 270 | HBoundsCheck* bounds_check = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 271 | HBoundsCheck(sub2, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 272 | HArraySet* array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 273 | null_check, bounds_check, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 274 | block3->AddInstruction(bounds_check); |
| 275 | block3->AddInstruction(array_set); |
| 276 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 277 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 278 | graph_->AddBlock(exit); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 279 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| Andreas Gampe | 0418b5b | 2014-12-04 17:24:50 -0800 | [diff] [blame] | 280 | block1->AddSuccessor(exit); // true successor |
| 281 | block1->AddSuccessor(block2); // false successor |
| 282 | block2->AddSuccessor(exit); // true successor |
| 283 | block2->AddSuccessor(block3); // false successor |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 284 | block3->AddSuccessor(exit); |
| 285 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 286 | RunBCE(); |
| 287 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 288 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| 289 | } |
| 290 | |
| Andreas Gampe | 0ba6273 | 2015-03-24 02:39:46 +0000 | [diff] [blame] | 291 | // array[6] = 1; // Can't eliminate. |
| Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 292 | // array[5] = 1; // Can eliminate. |
| 293 | // array[4] = 1; // Can eliminate. |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 294 | TEST_F(BoundsCheckEliminationTest, ConstantArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 295 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 296 | graph_->AddBlock(entry); |
| 297 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 298 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 299 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 300 | entry->AddInstruction(parameter); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 301 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 302 | HInstruction* constant_5 = graph_->GetIntConstant(5); |
| 303 | HInstruction* constant_4 = graph_->GetIntConstant(4); |
| 304 | HInstruction* constant_6 = graph_->GetIntConstant(6); |
| 305 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 306 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 307 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 308 | graph_->AddBlock(block); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 309 | entry->AddSuccessor(block); |
| 310 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 311 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 312 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 313 | HBoundsCheck* bounds_check6 = new (GetAllocator()) |
| Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 314 | HBoundsCheck(constant_6, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 315 | HInstruction* array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 316 | null_check, bounds_check6, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 317 | block->AddInstruction(null_check); |
| 318 | block->AddInstruction(array_length); |
| 319 | block->AddInstruction(bounds_check6); |
| 320 | block->AddInstruction(array_set); |
| 321 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 322 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 323 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 324 | HBoundsCheck* bounds_check5 = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 325 | HBoundsCheck(constant_5, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 326 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 327 | null_check, bounds_check5, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 328 | block->AddInstruction(null_check); |
| 329 | block->AddInstruction(array_length); |
| 330 | block->AddInstruction(bounds_check5); |
| 331 | block->AddInstruction(array_set); |
| 332 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 333 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 334 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 335 | HBoundsCheck* bounds_check4 = new (GetAllocator()) |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 336 | HBoundsCheck(constant_4, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 337 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 338 | null_check, bounds_check4, constant_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 339 | block->AddInstruction(null_check); |
| 340 | block->AddInstruction(array_length); |
| 341 | block->AddInstruction(bounds_check4); |
| 342 | block->AddInstruction(array_set); |
| 343 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 344 | block->AddInstruction(new (GetAllocator()) HGoto()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 345 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 346 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 347 | graph_->AddBlock(exit); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 348 | block->AddSuccessor(exit); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 349 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 350 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 351 | RunBCE(); |
| 352 | |
| Andreas Gampe | 0ba6273 | 2015-03-24 02:39:46 +0000 | [diff] [blame] | 353 | ASSERT_FALSE(IsRemoved(bounds_check6)); |
| Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 354 | ASSERT_TRUE(IsRemoved(bounds_check5)); |
| 355 | ASSERT_TRUE(IsRemoved(bounds_check4)); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // for (int i=initial; i<array.length; i+=increment) { array[i] = 10; } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 359 | static HInstruction* BuildSSAGraph1(HGraph* graph, |
| 360 | ArenaAllocator* allocator, |
| 361 | int initial, |
| 362 | int increment, |
| 363 | IfCondition cond = kCondGE) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 364 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 365 | graph->AddBlock(entry); |
| 366 | graph->SetEntryBlock(entry); |
| Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 367 | HInstruction* parameter = new (allocator) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 368 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 369 | entry->AddInstruction(parameter); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 370 | |
| 371 | HInstruction* constant_initial = graph->GetIntConstant(initial); |
| 372 | HInstruction* constant_increment = graph->GetIntConstant(increment); |
| 373 | HInstruction* constant_10 = graph->GetIntConstant(10); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 374 | |
| 375 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 376 | graph->AddBlock(block); |
| 377 | entry->AddSuccessor(block); |
| 378 | block->AddInstruction(new (allocator) HGoto()); |
| 379 | |
| 380 | HBasicBlock* loop_header = new (allocator) HBasicBlock(graph); |
| 381 | HBasicBlock* loop_body = new (allocator) HBasicBlock(graph); |
| 382 | HBasicBlock* exit = new (allocator) HBasicBlock(graph); |
| 383 | |
| 384 | graph->AddBlock(loop_header); |
| 385 | graph->AddBlock(loop_body); |
| 386 | graph->AddBlock(exit); |
| 387 | block->AddSuccessor(loop_header); |
| 388 | loop_header->AddSuccessor(exit); // true successor |
| 389 | loop_header->AddSuccessor(loop_body); // false successor |
| 390 | loop_body->AddSuccessor(loop_header); |
| 391 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 392 | HPhi* phi = new (allocator) HPhi(allocator, 0, 0, DataType::Type::kInt32); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 393 | HInstruction* null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 394 | HInstruction* array_length = new (allocator) HArrayLength(null_check, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 395 | HInstruction* cmp = nullptr; |
| 396 | if (cond == kCondGE) { |
| 397 | cmp = new (allocator) HGreaterThanOrEqual(phi, array_length); |
| 398 | } else { |
| 399 | DCHECK(cond == kCondGT); |
| 400 | cmp = new (allocator) HGreaterThan(phi, array_length); |
| 401 | } |
| 402 | HInstruction* if_inst = new (allocator) HIf(cmp); |
| 403 | loop_header->AddPhi(phi); |
| 404 | loop_header->AddInstruction(null_check); |
| 405 | loop_header->AddInstruction(array_length); |
| 406 | loop_header->AddInstruction(cmp); |
| 407 | loop_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 408 | phi->AddInput(constant_initial); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 409 | |
| 410 | null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 411 | array_length = new (allocator) HArrayLength(null_check, 0); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 412 | HInstruction* bounds_check = new (allocator) HBoundsCheck(phi, array_length, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 413 | HInstruction* array_set = new (allocator) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 414 | null_check, bounds_check, constant_10, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 415 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 416 | HInstruction* add = new (allocator) HAdd(DataType::Type::kInt32, phi, constant_increment); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 417 | loop_body->AddInstruction(null_check); |
| 418 | loop_body->AddInstruction(array_length); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 419 | loop_body->AddInstruction(bounds_check); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 420 | loop_body->AddInstruction(array_set); |
| 421 | loop_body->AddInstruction(add); |
| 422 | loop_body->AddInstruction(new (allocator) HGoto()); |
| 423 | phi->AddInput(add); |
| 424 | |
| 425 | exit->AddInstruction(new (allocator) HExit()); |
| 426 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 427 | return bounds_check; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 430 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1a) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 431 | // for (int i=0; i<array.length; i++) { array[i] = 10; // Can eliminate with gvn. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 432 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), 0, 1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 433 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 434 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 435 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 436 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 437 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1b) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 438 | // for (int i=1; i<array.length; i++) { array[i] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 439 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), 1, 1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 440 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 442 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 443 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 444 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1c) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 445 | // for (int i=-1; i<array.length; i++) { array[i] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 446 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), -1, 1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 447 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 448 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 449 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 450 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 451 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1d) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 452 | // for (int i=0; i<=array.length; i++) { array[i] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 453 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), 0, 1, kCondGT); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 454 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 455 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 456 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 457 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 458 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1e) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 459 | // for (int i=0; i<array.length; i += 2) { |
| 460 | // array[i] = 10; // Can't eliminate due to overflow concern. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 461 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), 0, 2); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 462 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 463 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 464 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 465 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 466 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination1f) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 467 | // for (int i=1; i<array.length; i += 2) { array[i] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 468 | HInstruction* bounds_check = BuildSSAGraph1(graph_, GetAllocator(), 1, 2); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 469 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 470 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| 471 | } |
| 472 | |
| 473 | // for (int i=array.length; i>0; i+=increment) { array[i-1] = 10; } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 474 | static HInstruction* BuildSSAGraph2(HGraph *graph, |
| 475 | ArenaAllocator* allocator, |
| 476 | int initial, |
| 477 | int increment = -1, |
| 478 | IfCondition cond = kCondLE) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 479 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 480 | graph->AddBlock(entry); |
| 481 | graph->SetEntryBlock(entry); |
| Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 482 | HInstruction* parameter = new (allocator) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 483 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 484 | entry->AddInstruction(parameter); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 485 | |
| 486 | HInstruction* constant_initial = graph->GetIntConstant(initial); |
| 487 | HInstruction* constant_increment = graph->GetIntConstant(increment); |
| 488 | HInstruction* constant_minus_1 = graph->GetIntConstant(-1); |
| 489 | HInstruction* constant_10 = graph->GetIntConstant(10); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 490 | |
| 491 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 492 | graph->AddBlock(block); |
| 493 | entry->AddSuccessor(block); |
| 494 | HInstruction* null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 495 | HInstruction* array_length = new (allocator) HArrayLength(null_check, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 496 | block->AddInstruction(null_check); |
| 497 | block->AddInstruction(array_length); |
| 498 | block->AddInstruction(new (allocator) HGoto()); |
| 499 | |
| 500 | HBasicBlock* loop_header = new (allocator) HBasicBlock(graph); |
| 501 | HBasicBlock* loop_body = new (allocator) HBasicBlock(graph); |
| 502 | HBasicBlock* exit = new (allocator) HBasicBlock(graph); |
| 503 | |
| 504 | graph->AddBlock(loop_header); |
| 505 | graph->AddBlock(loop_body); |
| 506 | graph->AddBlock(exit); |
| 507 | block->AddSuccessor(loop_header); |
| 508 | loop_header->AddSuccessor(exit); // true successor |
| 509 | loop_header->AddSuccessor(loop_body); // false successor |
| 510 | loop_body->AddSuccessor(loop_header); |
| 511 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 512 | HPhi* phi = new (allocator) HPhi(allocator, 0, 0, DataType::Type::kInt32); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 513 | HInstruction* cmp = nullptr; |
| 514 | if (cond == kCondLE) { |
| 515 | cmp = new (allocator) HLessThanOrEqual(phi, constant_initial); |
| 516 | } else { |
| 517 | DCHECK(cond == kCondLT); |
| 518 | cmp = new (allocator) HLessThan(phi, constant_initial); |
| 519 | } |
| 520 | HInstruction* if_inst = new (allocator) HIf(cmp); |
| 521 | loop_header->AddPhi(phi); |
| 522 | loop_header->AddInstruction(cmp); |
| 523 | loop_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 524 | phi->AddInput(array_length); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 525 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 526 | HInstruction* add = new (allocator) HAdd(DataType::Type::kInt32, phi, constant_minus_1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 527 | null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 528 | array_length = new (allocator) HArrayLength(null_check, 0); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 529 | HInstruction* bounds_check = new (allocator) HBoundsCheck(add, array_length, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 530 | HInstruction* array_set = new (allocator) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 531 | null_check, bounds_check, constant_10, DataType::Type::kInt32, 0); |
| 532 | HInstruction* add_phi = new (allocator) HAdd(DataType::Type::kInt32, phi, constant_increment); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 533 | loop_body->AddInstruction(add); |
| 534 | loop_body->AddInstruction(null_check); |
| 535 | loop_body->AddInstruction(array_length); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 536 | loop_body->AddInstruction(bounds_check); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 537 | loop_body->AddInstruction(array_set); |
| 538 | loop_body->AddInstruction(add_phi); |
| 539 | loop_body->AddInstruction(new (allocator) HGoto()); |
| 540 | phi->AddInput(add); |
| 541 | |
| 542 | exit->AddInstruction(new (allocator) HExit()); |
| 543 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 544 | return bounds_check; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 547 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination2a) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 548 | // for (int i=array.length; i>0; i--) { array[i-1] = 10; // Can eliminate with gvn. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 549 | HInstruction* bounds_check = BuildSSAGraph2(graph_, GetAllocator(), 0); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 550 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 551 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 552 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 553 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 554 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination2b) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 555 | // for (int i=array.length; i>1; i--) { array[i-1] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 556 | HInstruction* bounds_check = BuildSSAGraph2(graph_, GetAllocator(), 1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 557 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 558 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 559 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 560 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 561 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination2c) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 562 | // for (int i=array.length; i>-1; i--) { array[i-1] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 563 | HInstruction* bounds_check = BuildSSAGraph2(graph_, GetAllocator(), -1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 564 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 565 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 566 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 567 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 568 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination2d) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 569 | // for (int i=array.length; i>=0; i--) { array[i-1] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 570 | HInstruction* bounds_check = BuildSSAGraph2(graph_, GetAllocator(), 0, -1, kCondLT); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 571 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 572 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 573 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 574 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 575 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination2e) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 576 | // for (int i=array.length; i>0; i-=2) { array[i-1] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 577 | HInstruction* bounds_check = BuildSSAGraph2(graph_, GetAllocator(), 0, -2); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 578 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 579 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| 580 | } |
| 581 | |
| Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 582 | // int[] array = new int[10]; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 583 | // for (int i=0; i<10; i+=increment) { array[i] = 10; } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 584 | static HInstruction* BuildSSAGraph3(HGraph* graph, |
| 585 | ArenaAllocator* allocator, |
| 586 | int initial, |
| 587 | int increment, |
| 588 | IfCondition cond) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 589 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 590 | graph->AddBlock(entry); |
| 591 | graph->SetEntryBlock(entry); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 592 | |
| 593 | HInstruction* constant_10 = graph->GetIntConstant(10); |
| 594 | HInstruction* constant_initial = graph->GetIntConstant(initial); |
| 595 | HInstruction* constant_increment = graph->GetIntConstant(increment); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 596 | |
| 597 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 598 | graph->AddBlock(block); |
| 599 | entry->AddSuccessor(block); |
| Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 600 | // We pass a bogus constant for the class to avoid mocking one. |
| Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 601 | HInstruction* new_array = new (allocator) HNewArray( |
| Vladimir Marko | b546163 | 2018-10-15 14:24:21 +0100 | [diff] [blame] | 602 | /* cls= */ constant_10, |
| 603 | /* length= */ constant_10, |
| 604 | /* dex_pc= */ 0, |
| 605 | /* component_size_shift= */ 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 606 | block->AddInstruction(new_array); |
| 607 | block->AddInstruction(new (allocator) HGoto()); |
| 608 | |
| 609 | HBasicBlock* loop_header = new (allocator) HBasicBlock(graph); |
| 610 | HBasicBlock* loop_body = new (allocator) HBasicBlock(graph); |
| 611 | HBasicBlock* exit = new (allocator) HBasicBlock(graph); |
| 612 | |
| 613 | graph->AddBlock(loop_header); |
| 614 | graph->AddBlock(loop_body); |
| 615 | graph->AddBlock(exit); |
| 616 | block->AddSuccessor(loop_header); |
| 617 | loop_header->AddSuccessor(exit); // true successor |
| 618 | loop_header->AddSuccessor(loop_body); // false successor |
| 619 | loop_body->AddSuccessor(loop_header); |
| 620 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 621 | HPhi* phi = new (allocator) HPhi(allocator, 0, 0, DataType::Type::kInt32); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 622 | HInstruction* cmp = nullptr; |
| 623 | if (cond == kCondGE) { |
| 624 | cmp = new (allocator) HGreaterThanOrEqual(phi, constant_10); |
| 625 | } else { |
| 626 | DCHECK(cond == kCondGT); |
| 627 | cmp = new (allocator) HGreaterThan(phi, constant_10); |
| 628 | } |
| 629 | HInstruction* if_inst = new (allocator) HIf(cmp); |
| 630 | loop_header->AddPhi(phi); |
| 631 | loop_header->AddInstruction(cmp); |
| 632 | loop_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 633 | phi->AddInput(constant_initial); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 634 | |
| 635 | HNullCheck* null_check = new (allocator) HNullCheck(new_array, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 636 | HArrayLength* array_length = new (allocator) HArrayLength(null_check, 0); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 637 | HInstruction* bounds_check = new (allocator) HBoundsCheck(phi, array_length, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 638 | HInstruction* array_set = new (allocator) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 639 | null_check, bounds_check, constant_10, DataType::Type::kInt32, 0); |
| 640 | HInstruction* add = new (allocator) HAdd(DataType::Type::kInt32, phi, constant_increment); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 641 | loop_body->AddInstruction(null_check); |
| 642 | loop_body->AddInstruction(array_length); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 643 | loop_body->AddInstruction(bounds_check); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 644 | loop_body->AddInstruction(array_set); |
| 645 | loop_body->AddInstruction(add); |
| 646 | loop_body->AddInstruction(new (allocator) HGoto()); |
| 647 | phi->AddInput(add); |
| 648 | |
| 649 | exit->AddInstruction(new (allocator) HExit()); |
| 650 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 651 | return bounds_check; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 654 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination3a) { |
| Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 655 | // int[] array = new int[10]; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 656 | // for (int i=0; i<10; i++) { array[i] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 657 | HInstruction* bounds_check = BuildSSAGraph3(graph_, GetAllocator(), 0, 1, kCondGE); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 658 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 659 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 660 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 661 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 662 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination3b) { |
| Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 663 | // int[] array = new int[10]; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 664 | // for (int i=1; i<10; i++) { array[i] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 665 | HInstruction* bounds_check = BuildSSAGraph3(graph_, GetAllocator(), 1, 1, kCondGE); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 666 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 667 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 668 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 669 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 670 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination3c) { |
| Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 671 | // int[] array = new int[10]; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 672 | // for (int i=0; i<=10; i++) { array[i] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 673 | HInstruction* bounds_check = BuildSSAGraph3(graph_, GetAllocator(), 0, 1, kCondGT); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 674 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 675 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 676 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 677 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 678 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination3d) { |
| Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 679 | // int[] array = new int[10]; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 680 | // for (int i=1; i<10; i+=8) { array[i] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 681 | HInstruction* bounds_check = BuildSSAGraph3(graph_, GetAllocator(), 1, 8, kCondGE); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 682 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 683 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| 684 | } |
| 685 | |
| 686 | // for (int i=initial; i<array.length; i++) { array[array.length-i-1] = 10; } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 687 | static HInstruction* BuildSSAGraph4(HGraph* graph, |
| 688 | ArenaAllocator* allocator, |
| 689 | int initial, |
| 690 | IfCondition cond = kCondGE) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 691 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 692 | graph->AddBlock(entry); |
| 693 | graph->SetEntryBlock(entry); |
| Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 694 | HInstruction* parameter = new (allocator) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 695 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 696 | entry->AddInstruction(parameter); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 697 | |
| 698 | HInstruction* constant_initial = graph->GetIntConstant(initial); |
| 699 | HInstruction* constant_1 = graph->GetIntConstant(1); |
| 700 | HInstruction* constant_10 = graph->GetIntConstant(10); |
| 701 | HInstruction* constant_minus_1 = graph->GetIntConstant(-1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 702 | |
| 703 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 704 | graph->AddBlock(block); |
| 705 | entry->AddSuccessor(block); |
| 706 | block->AddInstruction(new (allocator) HGoto()); |
| 707 | |
| 708 | HBasicBlock* loop_header = new (allocator) HBasicBlock(graph); |
| 709 | HBasicBlock* loop_body = new (allocator) HBasicBlock(graph); |
| 710 | HBasicBlock* exit = new (allocator) HBasicBlock(graph); |
| 711 | |
| 712 | graph->AddBlock(loop_header); |
| 713 | graph->AddBlock(loop_body); |
| 714 | graph->AddBlock(exit); |
| 715 | block->AddSuccessor(loop_header); |
| 716 | loop_header->AddSuccessor(exit); // true successor |
| 717 | loop_header->AddSuccessor(loop_body); // false successor |
| 718 | loop_body->AddSuccessor(loop_header); |
| 719 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 720 | HPhi* phi = new (allocator) HPhi(allocator, 0, 0, DataType::Type::kInt32); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 721 | HInstruction* null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 722 | HInstruction* array_length = new (allocator) HArrayLength(null_check, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 723 | HInstruction* cmp = nullptr; |
| 724 | if (cond == kCondGE) { |
| 725 | cmp = new (allocator) HGreaterThanOrEqual(phi, array_length); |
| 726 | } else if (cond == kCondGT) { |
| 727 | cmp = new (allocator) HGreaterThan(phi, array_length); |
| 728 | } |
| 729 | HInstruction* if_inst = new (allocator) HIf(cmp); |
| 730 | loop_header->AddPhi(phi); |
| 731 | loop_header->AddInstruction(null_check); |
| 732 | loop_header->AddInstruction(array_length); |
| 733 | loop_header->AddInstruction(cmp); |
| 734 | loop_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 735 | phi->AddInput(constant_initial); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 736 | |
| 737 | null_check = new (allocator) HNullCheck(parameter, 0); |
| Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 738 | array_length = new (allocator) HArrayLength(null_check, 0); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 739 | HInstruction* sub = new (allocator) HSub(DataType::Type::kInt32, array_length, phi); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 740 | HInstruction* add_minus_1 = new (allocator) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 741 | HAdd(DataType::Type::kInt32, sub, constant_minus_1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 742 | HInstruction* bounds_check = new (allocator) HBoundsCheck(add_minus_1, array_length, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 743 | HInstruction* array_set = new (allocator) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 744 | null_check, bounds_check, constant_10, DataType::Type::kInt32, 0); |
| 745 | HInstruction* add = new (allocator) HAdd(DataType::Type::kInt32, phi, constant_1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 746 | loop_body->AddInstruction(null_check); |
| 747 | loop_body->AddInstruction(array_length); |
| 748 | loop_body->AddInstruction(sub); |
| 749 | loop_body->AddInstruction(add_minus_1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 750 | loop_body->AddInstruction(bounds_check); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 751 | loop_body->AddInstruction(array_set); |
| 752 | loop_body->AddInstruction(add); |
| 753 | loop_body->AddInstruction(new (allocator) HGoto()); |
| 754 | phi->AddInput(add); |
| 755 | |
| 756 | exit->AddInstruction(new (allocator) HExit()); |
| 757 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 758 | return bounds_check; |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 761 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination4a) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 762 | // for (int i=0; i<array.length; i++) { array[array.length-i-1] = 10; // Can eliminate with gvn. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 763 | HInstruction* bounds_check = BuildSSAGraph4(graph_, GetAllocator(), 0); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 764 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 765 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 766 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 767 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 768 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination4b) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 769 | // for (int i=1; i<array.length; i++) { array[array.length-i-1] = 10; // Can eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 770 | HInstruction* bounds_check = BuildSSAGraph4(graph_, GetAllocator(), 1); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 771 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 772 | ASSERT_TRUE(IsRemoved(bounds_check)); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 773 | } |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 774 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 775 | TEST_F(BoundsCheckEliminationTest, LoopArrayBoundsElimination4c) { |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 776 | // for (int i=0; i<=array.length; i++) { array[array.length-i] = 10; // Can't eliminate. } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 777 | HInstruction* bounds_check = BuildSSAGraph4(graph_, GetAllocator(), 0, kCondGT); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 778 | RunBCE(); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 779 | ASSERT_FALSE(IsRemoved(bounds_check)); |
| 780 | } |
| 781 | |
| 782 | // Bubble sort: |
| 783 | // (Every array access bounds-check can be eliminated.) |
| 784 | // for (int i=0; i<array.length-1; i++) { |
| 785 | // for (int j=0; j<array.length-i-1; j++) { |
| 786 | // if (array[j] > array[j+1]) { |
| 787 | // int temp = array[j+1]; |
| 788 | // array[j+1] = array[j]; |
| 789 | // array[j] = temp; |
| 790 | // } |
| 791 | // } |
| 792 | // } |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 793 | TEST_F(BoundsCheckEliminationTest, BubbleSortArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 794 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 795 | graph_->AddBlock(entry); |
| 796 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 797 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 798 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 799 | entry->AddInstruction(parameter); |
| David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 800 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 801 | HInstruction* constant_0 = graph_->GetIntConstant(0); |
| 802 | HInstruction* constant_minus_1 = graph_->GetIntConstant(-1); |
| 803 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 804 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 805 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 806 | graph_->AddBlock(block); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 807 | entry->AddSuccessor(block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 808 | block->AddInstruction(new (GetAllocator()) HGoto()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 809 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 810 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 811 | graph_->AddBlock(exit); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 812 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 813 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 814 | HBasicBlock* outer_header = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 815 | graph_->AddBlock(outer_header); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 816 | HPhi* phi_i = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32); |
| 817 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 818 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 819 | HAdd* add = new (GetAllocator()) HAdd(DataType::Type::kInt32, array_length, constant_minus_1); |
| 820 | HInstruction* cmp = new (GetAllocator()) HGreaterThanOrEqual(phi_i, add); |
| 821 | HIf* if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 822 | outer_header->AddPhi(phi_i); |
| 823 | outer_header->AddInstruction(null_check); |
| 824 | outer_header->AddInstruction(array_length); |
| 825 | outer_header->AddInstruction(add); |
| 826 | outer_header->AddInstruction(cmp); |
| 827 | outer_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 828 | phi_i->AddInput(constant_0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 829 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 830 | HBasicBlock* inner_header = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 831 | graph_->AddBlock(inner_header); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 832 | HPhi* phi_j = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32); |
| 833 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 834 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 835 | HSub* sub = new (GetAllocator()) HSub(DataType::Type::kInt32, array_length, phi_i); |
| 836 | add = new (GetAllocator()) HAdd(DataType::Type::kInt32, sub, constant_minus_1); |
| 837 | cmp = new (GetAllocator()) HGreaterThanOrEqual(phi_j, add); |
| 838 | if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 839 | inner_header->AddPhi(phi_j); |
| 840 | inner_header->AddInstruction(null_check); |
| 841 | inner_header->AddInstruction(array_length); |
| 842 | inner_header->AddInstruction(sub); |
| 843 | inner_header->AddInstruction(add); |
| 844 | inner_header->AddInstruction(cmp); |
| 845 | inner_header->AddInstruction(if_inst); |
| David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 846 | phi_j->AddInput(constant_0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 847 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 848 | HBasicBlock* inner_body_compare = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 849 | graph_->AddBlock(inner_body_compare); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 850 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 851 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 852 | HBoundsCheck* bounds_check1 = new (GetAllocator()) HBoundsCheck(phi_j, array_length, 0); |
| 853 | HArrayGet* array_get_j = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 854 | HArrayGet(null_check, bounds_check1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 855 | inner_body_compare->AddInstruction(null_check); |
| 856 | inner_body_compare->AddInstruction(array_length); |
| 857 | inner_body_compare->AddInstruction(bounds_check1); |
| 858 | inner_body_compare->AddInstruction(array_get_j); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 859 | HInstruction* j_plus_1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi_j, constant_1); |
| 860 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 861 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 862 | HBoundsCheck* bounds_check2 = new (GetAllocator()) HBoundsCheck(j_plus_1, array_length, 0); |
| 863 | HArrayGet* array_get_j_plus_1 = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 864 | HArrayGet(null_check, bounds_check2, DataType::Type::kInt32, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 865 | cmp = new (GetAllocator()) HGreaterThanOrEqual(array_get_j, array_get_j_plus_1); |
| 866 | if_inst = new (GetAllocator()) HIf(cmp); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 867 | inner_body_compare->AddInstruction(j_plus_1); |
| 868 | inner_body_compare->AddInstruction(null_check); |
| 869 | inner_body_compare->AddInstruction(array_length); |
| 870 | inner_body_compare->AddInstruction(bounds_check2); |
| 871 | inner_body_compare->AddInstruction(array_get_j_plus_1); |
| 872 | inner_body_compare->AddInstruction(cmp); |
| 873 | inner_body_compare->AddInstruction(if_inst); |
| 874 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 875 | HBasicBlock* inner_body_swap = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 876 | graph_->AddBlock(inner_body_swap); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 877 | j_plus_1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi_j, constant_1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 878 | // temp = array[j+1] |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 879 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 880 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 881 | HInstruction* bounds_check3 = new (GetAllocator()) HBoundsCheck(j_plus_1, array_length, 0); |
| 882 | array_get_j_plus_1 = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 883 | HArrayGet(null_check, bounds_check3, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 884 | inner_body_swap->AddInstruction(j_plus_1); |
| 885 | inner_body_swap->AddInstruction(null_check); |
| 886 | inner_body_swap->AddInstruction(array_length); |
| 887 | inner_body_swap->AddInstruction(bounds_check3); |
| 888 | inner_body_swap->AddInstruction(array_get_j_plus_1); |
| 889 | // array[j+1] = array[j] |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 890 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 891 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 892 | HInstruction* bounds_check4 = new (GetAllocator()) HBoundsCheck(phi_j, array_length, 0); |
| 893 | array_get_j = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 894 | HArrayGet(null_check, bounds_check4, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 895 | inner_body_swap->AddInstruction(null_check); |
| 896 | inner_body_swap->AddInstruction(array_length); |
| 897 | inner_body_swap->AddInstruction(bounds_check4); |
| 898 | inner_body_swap->AddInstruction(array_get_j); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 899 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 900 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 901 | HInstruction* bounds_check5 = new (GetAllocator()) HBoundsCheck(j_plus_1, array_length, 0); |
| 902 | HArraySet* array_set_j_plus_1 = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 903 | HArraySet(null_check, bounds_check5, array_get_j, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 904 | inner_body_swap->AddInstruction(null_check); |
| 905 | inner_body_swap->AddInstruction(array_length); |
| 906 | inner_body_swap->AddInstruction(bounds_check5); |
| 907 | inner_body_swap->AddInstruction(array_set_j_plus_1); |
| 908 | // array[j] = temp |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 909 | null_check = new (GetAllocator()) HNullCheck(parameter, 0); |
| 910 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 911 | HInstruction* bounds_check6 = new (GetAllocator()) HBoundsCheck(phi_j, array_length, 0); |
| 912 | HArraySet* array_set_j = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 913 | HArraySet(null_check, bounds_check6, array_get_j_plus_1, DataType::Type::kInt32, 0); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 914 | inner_body_swap->AddInstruction(null_check); |
| 915 | inner_body_swap->AddInstruction(array_length); |
| 916 | inner_body_swap->AddInstruction(bounds_check6); |
| 917 | inner_body_swap->AddInstruction(array_set_j); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 918 | inner_body_swap->AddInstruction(new (GetAllocator()) HGoto()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 919 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 920 | HBasicBlock* inner_body_add = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 921 | graph_->AddBlock(inner_body_add); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 922 | add = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi_j, constant_1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 923 | inner_body_add->AddInstruction(add); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 924 | inner_body_add->AddInstruction(new (GetAllocator()) HGoto()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 925 | phi_j->AddInput(add); |
| 926 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 927 | HBasicBlock* outer_body_add = new (GetAllocator()) HBasicBlock(graph_); |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 928 | graph_->AddBlock(outer_body_add); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 929 | add = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi_i, constant_1); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 930 | outer_body_add->AddInstruction(add); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 931 | outer_body_add->AddInstruction(new (GetAllocator()) HGoto()); |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 932 | phi_i->AddInput(add); |
| 933 | |
| 934 | block->AddSuccessor(outer_header); |
| 935 | outer_header->AddSuccessor(exit); |
| 936 | outer_header->AddSuccessor(inner_header); |
| 937 | inner_header->AddSuccessor(outer_body_add); |
| 938 | inner_header->AddSuccessor(inner_body_compare); |
| 939 | inner_body_compare->AddSuccessor(inner_body_add); |
| 940 | inner_body_compare->AddSuccessor(inner_body_swap); |
| 941 | inner_body_swap->AddSuccessor(inner_body_add); |
| 942 | inner_body_add->AddSuccessor(inner_header); |
| 943 | outer_body_add->AddSuccessor(outer_header); |
| 944 | |
| Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 945 | RunBCE(); // gvn removes same bounds check already |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 946 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 947 | ASSERT_TRUE(IsRemoved(bounds_check1)); |
| 948 | ASSERT_TRUE(IsRemoved(bounds_check2)); |
| 949 | ASSERT_TRUE(IsRemoved(bounds_check3)); |
| 950 | ASSERT_TRUE(IsRemoved(bounds_check4)); |
| 951 | ASSERT_TRUE(IsRemoved(bounds_check5)); |
| 952 | ASSERT_TRUE(IsRemoved(bounds_check6)); |
| 953 | } |
| 954 | |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 955 | // int[] array = new int[10]; |
| 956 | // for (int i=0; i<200; i++) { |
| 957 | // array[i%10] = 10; // Can eliminate |
| 958 | // array[i%1] = 10; // Can eliminate |
| 959 | // array[i%200] = 10; // Cannot eliminate |
| 960 | // array[i%-10] = 10; // Can eliminate |
| 961 | // array[i%array.length] = 10; // Can eliminate |
| 962 | // array[param_i%10] = 10; // Can't eliminate, when param_i < 0 |
| 963 | // } |
| 964 | TEST_F(BoundsCheckEliminationTest, ModArrayBoundsElimination) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 965 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 966 | graph_->AddBlock(entry); |
| 967 | graph_->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 968 | HInstruction* param_i = new (GetAllocator()) |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 969 | HParameterValue(graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 970 | entry->AddInstruction(param_i); |
| 971 | |
| 972 | HInstruction* constant_0 = graph_->GetIntConstant(0); |
| 973 | HInstruction* constant_1 = graph_->GetIntConstant(1); |
| 974 | HInstruction* constant_10 = graph_->GetIntConstant(10); |
| 975 | HInstruction* constant_200 = graph_->GetIntConstant(200); |
| 976 | HInstruction* constant_minus_10 = graph_->GetIntConstant(-10); |
| 977 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 978 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 979 | graph_->AddBlock(block); |
| 980 | entry->AddSuccessor(block); |
| 981 | // We pass a bogus constant for the class to avoid mocking one. |
| Vladimir Marko | b546163 | 2018-10-15 14:24:21 +0100 | [diff] [blame] | 982 | HInstruction* new_array = new (GetAllocator()) HNewArray( |
| 983 | /* cls= */ constant_10, |
| 984 | /* length= */ constant_10, |
| 985 | /* dex_pc= */ 0, |
| 986 | /* component_size_shift= */ 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 987 | block->AddInstruction(new_array); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 988 | block->AddInstruction(new (GetAllocator()) HGoto()); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 989 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 990 | HBasicBlock* loop_header = new (GetAllocator()) HBasicBlock(graph_); |
| 991 | HBasicBlock* loop_body = new (GetAllocator()) HBasicBlock(graph_); |
| 992 | HBasicBlock* exit = new (GetAllocator()) HBasicBlock(graph_); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 993 | |
| 994 | graph_->AddBlock(loop_header); |
| 995 | graph_->AddBlock(loop_body); |
| 996 | graph_->AddBlock(exit); |
| 997 | block->AddSuccessor(loop_header); |
| 998 | loop_header->AddSuccessor(exit); // true successor |
| 999 | loop_header->AddSuccessor(loop_body); // false successor |
| 1000 | loop_body->AddSuccessor(loop_header); |
| 1001 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1002 | HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32); |
| 1003 | HInstruction* cmp = new (GetAllocator()) HGreaterThanOrEqual(phi, constant_200); |
| 1004 | HInstruction* if_inst = new (GetAllocator()) HIf(cmp); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1005 | loop_header->AddPhi(phi); |
| 1006 | loop_header->AddInstruction(cmp); |
| 1007 | loop_header->AddInstruction(if_inst); |
| 1008 | phi->AddInput(constant_0); |
| 1009 | |
| 1010 | ////////////////////////////////////////////////////////////////////////////////// |
| 1011 | // LOOP BODY: |
| 1012 | // array[i % 10] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1013 | HRem* i_mod_10 = new (GetAllocator()) HRem(DataType::Type::kInt32, phi, constant_10, 0); |
| 1014 | HBoundsCheck* bounds_check_i_mod_10 = new (GetAllocator()) HBoundsCheck(i_mod_10, constant_10, 0); |
| 1015 | HInstruction* array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1016 | new_array, bounds_check_i_mod_10, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1017 | loop_body->AddInstruction(i_mod_10); |
| 1018 | loop_body->AddInstruction(bounds_check_i_mod_10); |
| 1019 | loop_body->AddInstruction(array_set); |
| 1020 | |
| 1021 | // array[i % 1] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1022 | HRem* i_mod_1 = new (GetAllocator()) HRem(DataType::Type::kInt32, phi, constant_1, 0); |
| 1023 | HBoundsCheck* bounds_check_i_mod_1 = new (GetAllocator()) HBoundsCheck(i_mod_1, constant_10, 0); |
| 1024 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1025 | new_array, bounds_check_i_mod_1, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1026 | loop_body->AddInstruction(i_mod_1); |
| 1027 | loop_body->AddInstruction(bounds_check_i_mod_1); |
| 1028 | loop_body->AddInstruction(array_set); |
| 1029 | |
| 1030 | // array[i % 200] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1031 | HRem* i_mod_200 = new (GetAllocator()) HRem(DataType::Type::kInt32, phi, constant_1, 0); |
| 1032 | HBoundsCheck* bounds_check_i_mod_200 = new (GetAllocator()) HBoundsCheck( |
| 1033 | i_mod_200, constant_10, 0); |
| 1034 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1035 | new_array, bounds_check_i_mod_200, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1036 | loop_body->AddInstruction(i_mod_200); |
| 1037 | loop_body->AddInstruction(bounds_check_i_mod_200); |
| 1038 | loop_body->AddInstruction(array_set); |
| 1039 | |
| 1040 | // array[i % -10] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1041 | HRem* i_mod_minus_10 = new (GetAllocator()) HRem( |
| 1042 | DataType::Type::kInt32, phi, constant_minus_10, 0); |
| 1043 | HBoundsCheck* bounds_check_i_mod_minus_10 = new (GetAllocator()) HBoundsCheck( |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1044 | i_mod_minus_10, constant_10, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1045 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1046 | new_array, bounds_check_i_mod_minus_10, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1047 | loop_body->AddInstruction(i_mod_minus_10); |
| 1048 | loop_body->AddInstruction(bounds_check_i_mod_minus_10); |
| 1049 | loop_body->AddInstruction(array_set); |
| 1050 | |
| 1051 | // array[i%array.length] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1052 | HNullCheck* null_check = new (GetAllocator()) HNullCheck(new_array, 0); |
| 1053 | HArrayLength* array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 1054 | HRem* i_mod_array_length = new (GetAllocator()) HRem( |
| 1055 | DataType::Type::kInt32, phi, array_length, 0); |
| 1056 | HBoundsCheck* bounds_check_i_mod_array_len = new (GetAllocator()) HBoundsCheck( |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1057 | i_mod_array_length, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1058 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1059 | null_check, bounds_check_i_mod_array_len, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1060 | loop_body->AddInstruction(null_check); |
| 1061 | loop_body->AddInstruction(array_length); |
| 1062 | loop_body->AddInstruction(i_mod_array_length); |
| 1063 | loop_body->AddInstruction(bounds_check_i_mod_array_len); |
| 1064 | loop_body->AddInstruction(array_set); |
| 1065 | |
| 1066 | // array[param_i % 10] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1067 | HRem* param_i_mod_10 = new (GetAllocator()) HRem(DataType::Type::kInt32, param_i, constant_10, 0); |
| 1068 | HBoundsCheck* bounds_check_param_i_mod_10 = new (GetAllocator()) HBoundsCheck( |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1069 | param_i_mod_10, constant_10, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1070 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1071 | new_array, bounds_check_param_i_mod_10, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1072 | loop_body->AddInstruction(param_i_mod_10); |
| 1073 | loop_body->AddInstruction(bounds_check_param_i_mod_10); |
| 1074 | loop_body->AddInstruction(array_set); |
| 1075 | |
| 1076 | // array[param_i%array.length] = 10; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1077 | null_check = new (GetAllocator()) HNullCheck(new_array, 0); |
| 1078 | array_length = new (GetAllocator()) HArrayLength(null_check, 0); |
| 1079 | HRem* param_i_mod_array_length = new (GetAllocator()) HRem( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1080 | DataType::Type::kInt32, param_i, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1081 | HBoundsCheck* bounds_check_param_i_mod_array_len = new (GetAllocator()) HBoundsCheck( |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1082 | param_i_mod_array_length, array_length, 0); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1083 | array_set = new (GetAllocator()) HArraySet( |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1084 | null_check, bounds_check_param_i_mod_array_len, constant_10, DataType::Type::kInt32, 0); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1085 | loop_body->AddInstruction(null_check); |
| 1086 | loop_body->AddInstruction(array_length); |
| 1087 | loop_body->AddInstruction(param_i_mod_array_length); |
| 1088 | loop_body->AddInstruction(bounds_check_param_i_mod_array_len); |
| 1089 | loop_body->AddInstruction(array_set); |
| 1090 | |
| 1091 | // i++; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1092 | HInstruction* add = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi, constant_1); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1093 | loop_body->AddInstruction(add); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1094 | loop_body->AddInstruction(new (GetAllocator()) HGoto()); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1095 | phi->AddInput(add); |
| 1096 | ////////////////////////////////////////////////////////////////////////////////// |
| 1097 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1098 | exit->AddInstruction(new (GetAllocator()) HExit()); |
| xueliang.zhong | a22cae7 | 2017-06-26 17:49:48 +0100 | [diff] [blame] | 1099 | |
| 1100 | RunBCE(); |
| 1101 | |
| 1102 | ASSERT_TRUE(IsRemoved(bounds_check_i_mod_10)); |
| 1103 | ASSERT_TRUE(IsRemoved(bounds_check_i_mod_1)); |
| 1104 | ASSERT_TRUE(IsRemoved(bounds_check_i_mod_200)); |
| 1105 | ASSERT_TRUE(IsRemoved(bounds_check_i_mod_minus_10)); |
| 1106 | ASSERT_TRUE(IsRemoved(bounds_check_i_mod_array_len)); |
| 1107 | ASSERT_FALSE(IsRemoved(bounds_check_param_i_mod_10)); |
| 1108 | } |
| 1109 | |
| Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1110 | } // namespace art |