Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "compiled_method_storage.h" |
| 18 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 21 | #include "compiled_method-inl.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 22 | #include "compiler_driver.h" |
| 23 | #include "compiler_options.h" |
| 24 | #include "dex/verification_results.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | TEST(CompiledMethodStorage, Deduplicate) { |
| 29 | CompilerOptions compiler_options; |
| 30 | VerificationResults verification_results(&compiler_options); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 31 | CompilerDriver driver(&compiler_options, |
| 32 | &verification_results, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 33 | Compiler::kOptimizing, |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 34 | /* instruction_set_ */ InstructionSet::kNone, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 35 | /* instruction_set_features */ nullptr, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 36 | /* image_classes */ nullptr, |
| 37 | /* compiled_classes */ nullptr, |
| 38 | /* compiled_methods */ nullptr, |
| 39 | /* thread_count */ 1u, |
| 40 | /* dump_stats */ false, |
| 41 | /* dump_passes */ false, |
| 42 | /* timer */ nullptr, |
| 43 | /* swap_fd */ -1, |
| 44 | /* profile_compilation_info */ nullptr); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 45 | CompiledMethodStorage* storage = driver.GetCompiledMethodStorage(); |
| 46 | |
| 47 | ASSERT_TRUE(storage->DedupeEnabled()); // The default. |
| 48 | |
| 49 | const uint8_t raw_code1[] = { 1u, 2u, 3u }; |
| 50 | const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u }; |
| 51 | ArrayRef<const uint8_t> code[] = { |
| 52 | ArrayRef<const uint8_t>(raw_code1), |
| 53 | ArrayRef<const uint8_t>(raw_code2), |
| 54 | }; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 55 | const uint8_t raw_method_info_map1[] = { 1u, 2u, 3u, 4u, 5u, 6u }; |
| 56 | const uint8_t raw_method_info_map2[] = { 8u, 7u, 6u, 5u, 4u, 3u, 2u, 1u }; |
| 57 | ArrayRef<const uint8_t> method_info[] = { |
| 58 | ArrayRef<const uint8_t>(raw_method_info_map1), |
| 59 | ArrayRef<const uint8_t>(raw_method_info_map2), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 60 | }; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 61 | const uint8_t raw_vmap_table1[] = { 2, 4, 6 }; |
| 62 | const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 }; |
| 63 | ArrayRef<const uint8_t> vmap_table[] = { |
| 64 | ArrayRef<const uint8_t>(raw_vmap_table1), |
| 65 | ArrayRef<const uint8_t>(raw_vmap_table2), |
| 66 | }; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 67 | const uint8_t raw_cfi_info1[] = { 1, 3, 5 }; |
| 68 | const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 }; |
| 69 | ArrayRef<const uint8_t> cfi_info[] = { |
| 70 | ArrayRef<const uint8_t>(raw_cfi_info1), |
| 71 | ArrayRef<const uint8_t>(raw_cfi_info2), |
| 72 | }; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 73 | const linker::LinkerPatch raw_patches1[] = { |
| 74 | linker::LinkerPatch::CodePatch(0u, nullptr, 1u), |
| 75 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 76 | }; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 77 | const linker::LinkerPatch raw_patches2[] = { |
| 78 | linker::LinkerPatch::CodePatch(0u, nullptr, 1u), |
| 79 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 80 | }; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 81 | ArrayRef<const linker::LinkerPatch> patches[] = { |
| 82 | ArrayRef<const linker::LinkerPatch>(raw_patches1), |
| 83 | ArrayRef<const linker::LinkerPatch>(raw_patches2), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | std::vector<CompiledMethod*> compiled_methods; |
| 87 | compiled_methods.reserve(1u << 7); |
| 88 | for (auto&& c : code) { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 89 | for (auto&& s : method_info) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 90 | for (auto&& v : vmap_table) { |
| 91 | for (auto&& f : cfi_info) { |
| 92 | for (auto&& p : patches) { |
| 93 | compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod( |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 94 | &driver, InstructionSet::kNone, c, 0u, 0u, 0u, s, v, f, p)); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 100 | constexpr size_t code_bit = 1u << 4; |
| 101 | constexpr size_t src_map_bit = 1u << 3; |
| 102 | constexpr size_t vmap_table_bit = 1u << 2; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 103 | constexpr size_t cfi_info_bit = 1u << 1; |
| 104 | constexpr size_t patches_bit = 1u << 0; |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 105 | CHECK_EQ(compiled_methods.size(), 1u << 5); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 106 | for (size_t i = 0; i != compiled_methods.size(); ++i) { |
| 107 | for (size_t j = 0; j != compiled_methods.size(); ++j) { |
| 108 | CompiledMethod* lhs = compiled_methods[i]; |
| 109 | CompiledMethod* rhs = compiled_methods[j]; |
| 110 | bool same_code = ((i ^ j) & code_bit) == 0u; |
| 111 | bool same_src_map = ((i ^ j) & src_map_bit) == 0u; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 112 | bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 113 | bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u; |
| 114 | bool same_patches = ((i ^ j) & patches_bit) == 0u; |
| 115 | ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data()) |
| 116 | << i << " " << j; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 117 | ASSERT_EQ(same_src_map, lhs->GetMethodInfo().data() == rhs->GetMethodInfo().data()) |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 118 | << i << " " << j; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 119 | ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data()) |
| 120 | << i << " " << j; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 121 | ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data()) |
| 122 | << i << " " << j; |
| 123 | ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data()) |
| 124 | << i << " " << j; |
| 125 | } |
| 126 | } |
| 127 | for (CompiledMethod* method : compiled_methods) { |
| 128 | CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace art |