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