blob: 5e2f444a2471e802db98a2682b53e8c6d9287e9a [file] [log] [blame]
Vladimir Marko35831e82015-09-11 11:59:18 +01001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070017#include "compiled_method_storage.h"
18
Vladimir Marko35831e82015-09-11 11:59:18 +010019#include <gtest/gtest.h>
20
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010021#include "compiled_method-inl.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010022#include "compiler_driver.h"
23#include "compiler_options.h"
24#include "dex/verification_results.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010025
26namespace art {
27
28TEST(CompiledMethodStorage, Deduplicate) {
29 CompilerOptions compiler_options;
30 VerificationResults verification_results(&compiler_options);
Vladimir Marko35831e82015-09-11 11:59:18 +010031 CompilerDriver driver(&compiler_options,
32 &verification_results,
Vladimir Marko944da602016-02-19 12:27:55 +000033 Compiler::kOptimizing,
Vladimir Marko944da602016-02-19 12:27:55 +000034 /* image_classes */ nullptr,
Vladimir Marko944da602016-02-19 12:27:55 +000035 /* thread_count */ 1u,
Vladimir Marko944da602016-02-19 12:27:55 +000036 /* swap_fd */ -1,
37 /* profile_compilation_info */ nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +010038 CompiledMethodStorage* storage = driver.GetCompiledMethodStorage();
39
40 ASSERT_TRUE(storage->DedupeEnabled()); // The default.
41
42 const uint8_t raw_code1[] = { 1u, 2u, 3u };
43 const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u };
44 ArrayRef<const uint8_t> code[] = {
45 ArrayRef<const uint8_t>(raw_code1),
46 ArrayRef<const uint8_t>(raw_code2),
47 };
Vladimir Marko35831e82015-09-11 11:59:18 +010048 const uint8_t raw_vmap_table1[] = { 2, 4, 6 };
49 const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 };
50 ArrayRef<const uint8_t> vmap_table[] = {
51 ArrayRef<const uint8_t>(raw_vmap_table1),
52 ArrayRef<const uint8_t>(raw_vmap_table2),
53 };
Vladimir Marko35831e82015-09-11 11:59:18 +010054 const uint8_t raw_cfi_info1[] = { 1, 3, 5 };
55 const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 };
56 ArrayRef<const uint8_t> cfi_info[] = {
57 ArrayRef<const uint8_t>(raw_cfi_info1),
58 ArrayRef<const uint8_t>(raw_cfi_info2),
59 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010060 const linker::LinkerPatch raw_patches1[] = {
Vladimir Marko8e524ad2018-07-13 10:27:43 +010061 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010062 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u),
Vladimir Marko35831e82015-09-11 11:59:18 +010063 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010064 const linker::LinkerPatch raw_patches2[] = {
Vladimir Marko8e524ad2018-07-13 10:27:43 +010065 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010066 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u),
Vladimir Marko35831e82015-09-11 11:59:18 +010067 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010068 ArrayRef<const linker::LinkerPatch> patches[] = {
69 ArrayRef<const linker::LinkerPatch>(raw_patches1),
70 ArrayRef<const linker::LinkerPatch>(raw_patches2),
Vladimir Marko35831e82015-09-11 11:59:18 +010071 };
72
73 std::vector<CompiledMethod*> compiled_methods;
David Srbecky8cd54542018-07-15 23:58:44 +010074 compiled_methods.reserve(1u << 4);
Vladimir Marko35831e82015-09-11 11:59:18 +010075 for (auto&& c : code) {
David Srbecky8cd54542018-07-15 23:58:44 +010076 for (auto&& v : vmap_table) {
77 for (auto&& f : cfi_info) {
78 for (auto&& p : patches) {
79 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
80 &driver, InstructionSet::kNone, c, v, f, p));
Vladimir Marko35831e82015-09-11 11:59:18 +010081 }
82 }
83 }
84 }
David Srbecky8cd54542018-07-15 23:58:44 +010085 constexpr size_t code_bit = 1u << 3;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010086 constexpr size_t vmap_table_bit = 1u << 2;
Vladimir Marko35831e82015-09-11 11:59:18 +010087 constexpr size_t cfi_info_bit = 1u << 1;
88 constexpr size_t patches_bit = 1u << 0;
David Srbecky8cd54542018-07-15 23:58:44 +010089 CHECK_EQ(compiled_methods.size(), 1u << 4);
Vladimir Marko35831e82015-09-11 11:59:18 +010090 for (size_t i = 0; i != compiled_methods.size(); ++i) {
91 for (size_t j = 0; j != compiled_methods.size(); ++j) {
92 CompiledMethod* lhs = compiled_methods[i];
93 CompiledMethod* rhs = compiled_methods[j];
94 bool same_code = ((i ^ j) & code_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +010095 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +010096 bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u;
97 bool same_patches = ((i ^ j) & patches_bit) == 0u;
98 ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data())
99 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100100 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
101 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100102 ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data())
103 << i << " " << j;
104 ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data())
105 << i << " " << j;
106 }
107 }
108 for (CompiledMethod* method : compiled_methods) {
109 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method);
110 }
111}
112
113} // namespace art