blob: de481caf07865cf0130ce3272aa527c8506b3412 [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 Marko33bff252017-11-01 14:35:42 +000034 /* instruction_set_ */ InstructionSet::kNone,
Vladimir Marko944da602016-02-19 12:27:55 +000035 /* instruction_set_features */ nullptr,
Vladimir Marko944da602016-02-19 12:27:55 +000036 /* 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 Marko35831e82015-09-11 11:59:18 +010045 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 Chartiercbcedbf2017-03-12 22:24:50 -070055 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 Marko35831e82015-09-11 11:59:18 +010060 };
Vladimir Marko35831e82015-09-11 11:59:18 +010061 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 Marko35831e82015-09-11 11:59:18 +010067 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 Markod8dbc8d2017-09-20 13:37:47 +010073 const linker::LinkerPatch raw_patches1[] = {
74 linker::LinkerPatch::CodePatch(0u, nullptr, 1u),
75 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u),
Vladimir Marko35831e82015-09-11 11:59:18 +010076 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010077 const linker::LinkerPatch raw_patches2[] = {
78 linker::LinkerPatch::CodePatch(0u, nullptr, 1u),
79 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u),
Vladimir Marko35831e82015-09-11 11:59:18 +010080 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010081 ArrayRef<const linker::LinkerPatch> patches[] = {
82 ArrayRef<const linker::LinkerPatch>(raw_patches1),
83 ArrayRef<const linker::LinkerPatch>(raw_patches2),
Vladimir Marko35831e82015-09-11 11:59:18 +010084 };
85
86 std::vector<CompiledMethod*> compiled_methods;
87 compiled_methods.reserve(1u << 7);
88 for (auto&& c : code) {
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070089 for (auto&& s : method_info) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010090 for (auto&& v : vmap_table) {
91 for (auto&& f : cfi_info) {
92 for (auto&& p : patches) {
93 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
Vladimir Marko33bff252017-11-01 14:35:42 +000094 &driver, InstructionSet::kNone, c, 0u, 0u, 0u, s, v, f, p));
Vladimir Marko35831e82015-09-11 11:59:18 +010095 }
96 }
97 }
98 }
99 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100100 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 Marko35831e82015-09-11 11:59:18 +0100103 constexpr size_t cfi_info_bit = 1u << 1;
104 constexpr size_t patches_bit = 1u << 0;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100105 CHECK_EQ(compiled_methods.size(), 1u << 5);
Vladimir Marko35831e82015-09-11 11:59:18 +0100106 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 Marko35831e82015-09-11 11:59:18 +0100112 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +0100113 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 Chartiercbcedbf2017-03-12 22:24:50 -0700117 ASSERT_EQ(same_src_map, lhs->GetMethodInfo().data() == rhs->GetMethodInfo().data())
Vladimir Marko35831e82015-09-11 11:59:18 +0100118 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100119 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
120 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100121 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