xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "load_store_analysis.h" |
| 18 | #include "nodes.h" |
| 19 | #include "optimizing_unit_test.h" |
| 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class LoadStoreAnalysisTest : public CommonCompilerTest { |
| 26 | public: |
| 27 | LoadStoreAnalysisTest() : pool_(), allocator_(&pool_) { |
| 28 | graph_ = CreateGraph(&allocator_); |
| 29 | } |
| 30 | |
| 31 | ArenaPool pool_; |
| 32 | ArenaAllocator allocator_; |
| 33 | HGraph* graph_; |
| 34 | }; |
| 35 | |
| 36 | TEST_F(LoadStoreAnalysisTest, ArrayHeapLocations) { |
| 37 | HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_); |
| 38 | graph_->AddBlock(entry); |
| 39 | graph_->SetEntryBlock(entry); |
| 40 | |
| 41 | // entry: |
| 42 | // array ParameterValue |
| 43 | // index ParameterValue |
| 44 | // c1 IntConstant |
| 45 | // c2 IntConstant |
| 46 | // c3 IntConstant |
| 47 | // array_get1 ArrayGet [array, c1] |
| 48 | // array_get2 ArrayGet [array, c2] |
| 49 | // array_set1 ArraySet [array, c1, c3] |
| 50 | // array_set2 ArraySet [array, index, c3] |
| 51 | HInstruction* array = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 52 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 53 | HInstruction* index = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 54 | graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32); |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 55 | HInstruction* c1 = graph_->GetIntConstant(1); |
| 56 | HInstruction* c2 = graph_->GetIntConstant(2); |
| 57 | HInstruction* c3 = graph_->GetIntConstant(3); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 58 | HInstruction* array_get1 = new (&allocator_) HArrayGet(array, c1, DataType::Type::kInt32, 0); |
| 59 | HInstruction* array_get2 = new (&allocator_) HArrayGet(array, c2, DataType::Type::kInt32, 0); |
| 60 | HInstruction* array_set1 = new (&allocator_) HArraySet(array, c1, c3, DataType::Type::kInt32, 0); |
| 61 | HInstruction* array_set2 = |
| 62 | new (&allocator_) HArraySet(array, index, c3, DataType::Type::kInt32, 0); |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 63 | entry->AddInstruction(array); |
| 64 | entry->AddInstruction(index); |
| 65 | entry->AddInstruction(array_get1); |
| 66 | entry->AddInstruction(array_get2); |
| 67 | entry->AddInstruction(array_set1); |
| 68 | entry->AddInstruction(array_set2); |
| 69 | |
| 70 | // Test HeapLocationCollector initialization. |
| 71 | // Should be no heap locations, no operations on the heap. |
| 72 | HeapLocationCollector heap_location_collector(graph_); |
| 73 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 0U); |
| 74 | ASSERT_FALSE(heap_location_collector.HasHeapStores()); |
| 75 | |
| 76 | // Test that after visiting the graph_, it must see following heap locations |
| 77 | // array[c1], array[c2], array[index]; and it should see heap stores. |
| 78 | heap_location_collector.VisitBasicBlock(entry); |
| 79 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 3U); |
| 80 | ASSERT_TRUE(heap_location_collector.HasHeapStores()); |
| 81 | |
| 82 | // Test queries on HeapLocationCollector's ref info and index records. |
| 83 | ReferenceInfo* ref = heap_location_collector.FindReferenceInfoOf(array); |
| 84 | size_t field_off = HeapLocation::kInvalidFieldOffset; |
| 85 | size_t class_def = HeapLocation::kDeclaringClassDefIndexForArrays; |
| 86 | size_t loc1 = heap_location_collector.FindHeapLocationIndex(ref, field_off, c1, class_def); |
| 87 | size_t loc2 = heap_location_collector.FindHeapLocationIndex(ref, field_off, c2, class_def); |
| 88 | size_t loc3 = heap_location_collector.FindHeapLocationIndex(ref, field_off, index, class_def); |
| 89 | // must find this reference info for array in HeapLocationCollector. |
| 90 | ASSERT_TRUE(ref != nullptr); |
| 91 | // must find these heap locations; |
| 92 | // and array[1], array[2], array[3] should be different heap locations. |
| 93 | ASSERT_TRUE(loc1 != HeapLocationCollector::kHeapLocationNotFound); |
| 94 | ASSERT_TRUE(loc2 != HeapLocationCollector::kHeapLocationNotFound); |
| 95 | ASSERT_TRUE(loc3 != HeapLocationCollector::kHeapLocationNotFound); |
| 96 | ASSERT_TRUE(loc1 != loc2); |
| 97 | ASSERT_TRUE(loc2 != loc3); |
| 98 | ASSERT_TRUE(loc1 != loc3); |
| 99 | |
| 100 | // Test alias relationships after building aliasing matrix. |
| 101 | // array[1] and array[2] clearly should not alias; |
| 102 | // array[index] should alias with the others, because index is an unknow value. |
| 103 | heap_location_collector.BuildAliasingMatrix(); |
| 104 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 105 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc3)); |
| 106 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc3)); |
| 107 | } |
| 108 | |
| 109 | TEST_F(LoadStoreAnalysisTest, FieldHeapLocations) { |
| 110 | HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_); |
| 111 | graph_->AddBlock(entry); |
| 112 | graph_->SetEntryBlock(entry); |
| 113 | |
| 114 | // entry: |
| 115 | // object ParameterValue |
| 116 | // c1 IntConstant |
| 117 | // set_field10 InstanceFieldSet [object, c1, 10] |
| 118 | // get_field10 InstanceFieldGet [object, 10] |
| 119 | // get_field20 InstanceFieldGet [object, 20] |
| 120 | |
| 121 | HInstruction* c1 = graph_->GetIntConstant(1); |
| 122 | HInstruction* object = new (&allocator_) HParameterValue(graph_->GetDexFile(), |
| 123 | dex::TypeIndex(0), |
| 124 | 0, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 125 | DataType::Type::kReference); |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 126 | HInstanceFieldSet* set_field10 = new (&allocator_) HInstanceFieldSet(object, |
| 127 | c1, |
| 128 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 129 | DataType::Type::kInt32, |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 130 | MemberOffset(10), |
| 131 | false, |
| 132 | kUnknownFieldIndex, |
| 133 | kUnknownClassDefIndex, |
| 134 | graph_->GetDexFile(), |
| 135 | 0); |
| 136 | HInstanceFieldGet* get_field10 = new (&allocator_) HInstanceFieldGet(object, |
| 137 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 138 | DataType::Type::kInt32, |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 139 | MemberOffset(10), |
| 140 | false, |
| 141 | kUnknownFieldIndex, |
| 142 | kUnknownClassDefIndex, |
| 143 | graph_->GetDexFile(), |
| 144 | 0); |
| 145 | HInstanceFieldGet* get_field20 = new (&allocator_) HInstanceFieldGet(object, |
| 146 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 147 | DataType::Type::kInt32, |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 148 | MemberOffset(20), |
| 149 | false, |
| 150 | kUnknownFieldIndex, |
| 151 | kUnknownClassDefIndex, |
| 152 | graph_->GetDexFile(), |
| 153 | 0); |
| 154 | entry->AddInstruction(object); |
| 155 | entry->AddInstruction(set_field10); |
| 156 | entry->AddInstruction(get_field10); |
| 157 | entry->AddInstruction(get_field20); |
| 158 | |
| 159 | // Test HeapLocationCollector initialization. |
| 160 | // Should be no heap locations, no operations on the heap. |
| 161 | HeapLocationCollector heap_location_collector(graph_); |
| 162 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 0U); |
| 163 | ASSERT_FALSE(heap_location_collector.HasHeapStores()); |
| 164 | |
| 165 | // Test that after visiting the graph, it must see following heap locations |
| 166 | // object.field10, object.field20 and it should see heap stores. |
| 167 | heap_location_collector.VisitBasicBlock(entry); |
| 168 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 2U); |
| 169 | ASSERT_TRUE(heap_location_collector.HasHeapStores()); |
| 170 | |
| 171 | // Test queries on HeapLocationCollector's ref info and index records. |
| 172 | ReferenceInfo* ref = heap_location_collector.FindReferenceInfoOf(object); |
| 173 | size_t loc1 = heap_location_collector.FindHeapLocationIndex( |
| 174 | ref, 10, nullptr, kUnknownClassDefIndex); |
| 175 | size_t loc2 = heap_location_collector.FindHeapLocationIndex( |
| 176 | ref, 20, nullptr, kUnknownClassDefIndex); |
| 177 | // must find references info for object and in HeapLocationCollector. |
| 178 | ASSERT_TRUE(ref != nullptr); |
| 179 | // must find these heap locations. |
| 180 | ASSERT_TRUE(loc1 != HeapLocationCollector::kHeapLocationNotFound); |
| 181 | ASSERT_TRUE(loc2 != HeapLocationCollector::kHeapLocationNotFound); |
| 182 | // different fields of same object. |
| 183 | ASSERT_TRUE(loc1 != loc2); |
| 184 | // accesses to different fields of the same object should not alias. |
| 185 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 186 | } |
| 187 | |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 188 | TEST_F(LoadStoreAnalysisTest, ArrayIndexAliasingTest) { |
| 189 | HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_); |
| 190 | graph_->AddBlock(entry); |
| 191 | graph_->SetEntryBlock(entry); |
| 192 | graph_->BuildDominatorTree(); |
| 193 | |
| 194 | HInstruction* array = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 195 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 196 | HInstruction* index = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 197 | graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 198 | HInstruction* c0 = graph_->GetIntConstant(0); |
| 199 | HInstruction* c1 = graph_->GetIntConstant(1); |
| 200 | HInstruction* c_neg1 = graph_->GetIntConstant(-1); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 201 | HInstruction* add0 = new (&allocator_) HAdd(DataType::Type::kInt32, index, c0); |
| 202 | HInstruction* add1 = new (&allocator_) HAdd(DataType::Type::kInt32, index, c1); |
| 203 | HInstruction* sub0 = new (&allocator_) HSub(DataType::Type::kInt32, index, c0); |
| 204 | HInstruction* sub1 = new (&allocator_) HSub(DataType::Type::kInt32, index, c1); |
| 205 | HInstruction* sub_neg1 = new (&allocator_) HSub(DataType::Type::kInt32, index, c_neg1); |
| 206 | HInstruction* rev_sub1 = new (&allocator_) HSub(DataType::Type::kInt32, c1, index); |
| 207 | HInstruction* arr_set1 = new (&allocator_) HArraySet(array, c0, c0, DataType::Type::kInt32, 0); |
| 208 | HInstruction* arr_set2 = new (&allocator_) HArraySet(array, c1, c0, DataType::Type::kInt32, 0); |
| 209 | HInstruction* arr_set3 = new (&allocator_) HArraySet(array, add0, c0, DataType::Type::kInt32, 0); |
| 210 | HInstruction* arr_set4 = new (&allocator_) HArraySet(array, add1, c0, DataType::Type::kInt32, 0); |
| 211 | HInstruction* arr_set5 = new (&allocator_) HArraySet(array, sub0, c0, DataType::Type::kInt32, 0); |
| 212 | HInstruction* arr_set6 = new (&allocator_) HArraySet(array, sub1, c0, DataType::Type::kInt32, 0); |
| 213 | HInstruction* arr_set7 = |
| 214 | new (&allocator_) HArraySet(array, rev_sub1, c0, DataType::Type::kInt32, 0); |
| 215 | HInstruction* arr_set8 = |
| 216 | new (&allocator_) HArraySet(array, sub_neg1, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 217 | |
| 218 | entry->AddInstruction(array); |
| 219 | entry->AddInstruction(index); |
| 220 | entry->AddInstruction(add0); |
| 221 | entry->AddInstruction(add1); |
| 222 | entry->AddInstruction(sub0); |
| 223 | entry->AddInstruction(sub1); |
| 224 | entry->AddInstruction(sub_neg1); |
| 225 | entry->AddInstruction(rev_sub1); |
| 226 | |
| 227 | entry->AddInstruction(arr_set1); // array[0] = c0 |
| 228 | entry->AddInstruction(arr_set2); // array[1] = c0 |
| 229 | entry->AddInstruction(arr_set3); // array[i+0] = c0 |
| 230 | entry->AddInstruction(arr_set4); // array[i+1] = c0 |
| 231 | entry->AddInstruction(arr_set5); // array[i-0] = c0 |
| 232 | entry->AddInstruction(arr_set6); // array[i-1] = c0 |
| 233 | entry->AddInstruction(arr_set7); // array[1-i] = c0 |
| 234 | entry->AddInstruction(arr_set8); // array[i-(-1)] = c0 |
| 235 | |
| 236 | LoadStoreAnalysis lsa(graph_); |
| 237 | lsa.Run(); |
| 238 | const HeapLocationCollector& heap_location_collector = lsa.GetHeapLocationCollector(); |
| 239 | |
| 240 | // LSA/HeapLocationCollector should see those ArrayGet instructions. |
| 241 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 8U); |
| 242 | ASSERT_TRUE(heap_location_collector.HasHeapStores()); |
| 243 | |
| 244 | // Test queries on HeapLocationCollector's aliasing matrix after load store analysis. |
| 245 | size_t loc1 = HeapLocationCollector::kHeapLocationNotFound; |
| 246 | size_t loc2 = HeapLocationCollector::kHeapLocationNotFound; |
| 247 | |
| 248 | // Test alias: array[0] and array[1] |
| 249 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, c0); |
| 250 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, c1); |
| 251 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 252 | |
| 253 | // Test alias: array[i+0] and array[i-0] |
| 254 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add0); |
| 255 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub0); |
| 256 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 257 | |
| 258 | // Test alias: array[i+1] and array[i-1] |
| 259 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add1); |
| 260 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub1); |
| 261 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 262 | |
| 263 | // Test alias: array[i+1] and array[1-i] |
| 264 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add1); |
| 265 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, rev_sub1); |
| 266 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 267 | |
| 268 | // Test alias: array[i+1] and array[i-(-1)] |
| 269 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add1); |
| 270 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_neg1); |
| 271 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 272 | } |
| 273 | |
| 274 | TEST_F(LoadStoreAnalysisTest, ArrayIndexCalculationOverflowTest) { |
| 275 | HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_); |
| 276 | graph_->AddBlock(entry); |
| 277 | graph_->SetEntryBlock(entry); |
| 278 | graph_->BuildDominatorTree(); |
| 279 | |
| 280 | HInstruction* array = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 281 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 282 | HInstruction* index = new (&allocator_) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 283 | graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 284 | |
| 285 | HInstruction* c0 = graph_->GetIntConstant(0); |
| 286 | HInstruction* c_0x80000000 = graph_->GetIntConstant(0x80000000); |
| 287 | HInstruction* c_0x10 = graph_->GetIntConstant(0x10); |
| 288 | HInstruction* c_0xFFFFFFF0 = graph_->GetIntConstant(0xFFFFFFF0); |
| 289 | HInstruction* c_0x7FFFFFFF = graph_->GetIntConstant(0x7FFFFFFF); |
| 290 | HInstruction* c_0x80000001 = graph_->GetIntConstant(0x80000001); |
| 291 | |
| 292 | // `index+0x80000000` and `index-0x80000000` array indices MAY alias. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 293 | HInstruction* add_0x80000000 = new (&allocator_) HAdd( |
| 294 | DataType::Type::kInt32, index, c_0x80000000); |
| 295 | HInstruction* sub_0x80000000 = new (&allocator_) HSub( |
| 296 | DataType::Type::kInt32, index, c_0x80000000); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 297 | HInstruction* arr_set_1 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 298 | array, add_0x80000000, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 299 | HInstruction* arr_set_2 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 300 | array, sub_0x80000000, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 301 | |
| 302 | // `index+0x10` and `index-0xFFFFFFF0` array indices MAY alias. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 303 | HInstruction* add_0x10 = new (&allocator_) HAdd(DataType::Type::kInt32, index, c_0x10); |
| 304 | HInstruction* sub_0xFFFFFFF0 = new (&allocator_) HSub( |
| 305 | DataType::Type::kInt32, index, c_0xFFFFFFF0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 306 | HInstruction* arr_set_3 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 307 | array, add_0x10, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 308 | HInstruction* arr_set_4 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 309 | array, sub_0xFFFFFFF0, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 310 | |
| 311 | // `index+0x7FFFFFFF` and `index-0x80000001` array indices MAY alias. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 312 | HInstruction* add_0x7FFFFFFF = new (&allocator_) HAdd( |
| 313 | DataType::Type::kInt32, index, c_0x7FFFFFFF); |
| 314 | HInstruction* sub_0x80000001 = new (&allocator_) HSub( |
| 315 | DataType::Type::kInt32, index, c_0x80000001); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 316 | HInstruction* arr_set_5 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 317 | array, add_0x7FFFFFFF, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 318 | HInstruction* arr_set_6 = new (&allocator_) HArraySet( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 319 | array, sub_0x80000001, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 320 | |
| 321 | // `index+0` and `index-0` array indices MAY alias. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 322 | HInstruction* add_0 = new (&allocator_) HAdd(DataType::Type::kInt32, index, c0); |
| 323 | HInstruction* sub_0 = new (&allocator_) HSub(DataType::Type::kInt32, index, c0); |
| 324 | HInstruction* arr_set_7 = new (&allocator_) HArraySet( |
| 325 | array, add_0, c0, DataType::Type::kInt32, 0); |
| 326 | HInstruction* arr_set_8 = new (&allocator_) HArraySet( |
| 327 | array, sub_0, c0, DataType::Type::kInt32, 0); |
xueliang.zhong | 016c0f1 | 2017-05-12 18:16:31 +0100 | [diff] [blame] | 328 | |
| 329 | entry->AddInstruction(array); |
| 330 | entry->AddInstruction(index); |
| 331 | entry->AddInstruction(add_0x80000000); |
| 332 | entry->AddInstruction(sub_0x80000000); |
| 333 | entry->AddInstruction(add_0x10); |
| 334 | entry->AddInstruction(sub_0xFFFFFFF0); |
| 335 | entry->AddInstruction(add_0x7FFFFFFF); |
| 336 | entry->AddInstruction(sub_0x80000001); |
| 337 | entry->AddInstruction(add_0); |
| 338 | entry->AddInstruction(sub_0); |
| 339 | entry->AddInstruction(arr_set_1); |
| 340 | entry->AddInstruction(arr_set_2); |
| 341 | entry->AddInstruction(arr_set_3); |
| 342 | entry->AddInstruction(arr_set_4); |
| 343 | entry->AddInstruction(arr_set_5); |
| 344 | entry->AddInstruction(arr_set_6); |
| 345 | entry->AddInstruction(arr_set_7); |
| 346 | entry->AddInstruction(arr_set_8); |
| 347 | |
| 348 | LoadStoreAnalysis lsa(graph_); |
| 349 | lsa.Run(); |
| 350 | const HeapLocationCollector& heap_location_collector = lsa.GetHeapLocationCollector(); |
| 351 | |
| 352 | // LSA/HeapLocationCollector should see those ArrayGet instructions. |
| 353 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 8U); |
| 354 | ASSERT_TRUE(heap_location_collector.HasHeapStores()); |
| 355 | |
| 356 | // Test queries on HeapLocationCollector's aliasing matrix after load store analysis. |
| 357 | size_t loc1 = HeapLocationCollector::kHeapLocationNotFound; |
| 358 | size_t loc2 = HeapLocationCollector::kHeapLocationNotFound; |
| 359 | |
| 360 | // Test alias: array[i+0x80000000] and array[i-0x80000000] |
| 361 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add_0x80000000); |
| 362 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0x80000000); |
| 363 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 364 | |
| 365 | // Test alias: array[i+0x10] and array[i-0xFFFFFFF0] |
| 366 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add_0x10); |
| 367 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0xFFFFFFF0); |
| 368 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 369 | |
| 370 | // Test alias: array[i+0x7FFFFFFF] and array[i-0x80000001] |
| 371 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add_0x7FFFFFFF); |
| 372 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0x80000001); |
| 373 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 374 | |
| 375 | // Test alias: array[i+0] and array[i-0] |
| 376 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add_0); |
| 377 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0); |
| 378 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 379 | |
| 380 | // Should not alias: |
| 381 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0x80000000); |
| 382 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0x80000001); |
| 383 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 384 | |
| 385 | // Should not alias: |
| 386 | loc1 = heap_location_collector.GetArrayAccessHeapLocation(array, add_0); |
| 387 | loc2 = heap_location_collector.GetArrayAccessHeapLocation(array, sub_0x80000000); |
| 388 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 389 | } |
| 390 | |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 391 | } // namespace art |