blob: 9e0c22c68c7603cb8ba930efb80967d5d0080622 [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
17#include <gtest/gtest.h>
18
19#include "compiled_method_storage.h"
20#include "compiled_method.h"
21#include "compiler_driver.h"
22#include "compiler_options.h"
23#include "dex/verification_results.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
25
26namespace art {
27
28TEST(CompiledMethodStorage, Deduplicate) {
29 CompilerOptions compiler_options;
30 VerificationResults verification_results(&compiler_options);
31 DexFileToMethodInlinerMap method_inliner_map;
32 CompilerDriver driver(&compiler_options,
33 &verification_results,
34 &method_inliner_map,
Vladimir Marko944da602016-02-19 12:27:55 +000035 Compiler::kOptimizing,
36 /* instruction_set_ */ kNone,
37 /* instruction_set_features */ nullptr,
38 /* boot_image */ false,
39 /* image_classes */ nullptr,
40 /* compiled_classes */ nullptr,
41 /* compiled_methods */ nullptr,
42 /* thread_count */ 1u,
43 /* dump_stats */ false,
44 /* dump_passes */ false,
45 /* timer */ nullptr,
46 /* swap_fd */ -1,
47 /* profile_compilation_info */ nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +010048 CompiledMethodStorage* storage = driver.GetCompiledMethodStorage();
49
50 ASSERT_TRUE(storage->DedupeEnabled()); // The default.
51
52 const uint8_t raw_code1[] = { 1u, 2u, 3u };
53 const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u };
54 ArrayRef<const uint8_t> code[] = {
55 ArrayRef<const uint8_t>(raw_code1),
56 ArrayRef<const uint8_t>(raw_code2),
57 };
58 const SrcMapElem raw_src_map1[] = { { 1u, 2u }, { 3u, 4u }, { 5u, 6u } };
59 const SrcMapElem raw_src_map2[] = { { 8u, 7u }, { 6u, 5u }, { 4u, 3u }, { 2u, 1u } };
60 ArrayRef<const SrcMapElem> src_map[] = {
61 ArrayRef<const SrcMapElem>(raw_src_map1),
62 ArrayRef<const SrcMapElem>(raw_src_map2),
63 };
Vladimir Marko35831e82015-09-11 11:59:18 +010064 const uint8_t raw_vmap_table1[] = { 2, 4, 6 };
65 const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 };
66 ArrayRef<const uint8_t> vmap_table[] = {
67 ArrayRef<const uint8_t>(raw_vmap_table1),
68 ArrayRef<const uint8_t>(raw_vmap_table2),
69 };
Vladimir Marko35831e82015-09-11 11:59:18 +010070 const uint8_t raw_cfi_info1[] = { 1, 3, 5 };
71 const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 };
72 ArrayRef<const uint8_t> cfi_info[] = {
73 ArrayRef<const uint8_t>(raw_cfi_info1),
74 ArrayRef<const uint8_t>(raw_cfi_info2),
75 };
76 const LinkerPatch raw_patches1[] = {
77 LinkerPatch::CodePatch(0u, nullptr, 1u),
78 LinkerPatch::MethodPatch(4u, nullptr, 1u),
79 };
80 const LinkerPatch raw_patches2[] = {
81 LinkerPatch::CodePatch(0u, nullptr, 1u),
82 LinkerPatch::MethodPatch(4u, nullptr, 2u),
83 };
84 ArrayRef<const LinkerPatch> patches[] = {
85 ArrayRef<const LinkerPatch>(raw_patches1),
86 ArrayRef<const LinkerPatch>(raw_patches2),
87 };
88
89 std::vector<CompiledMethod*> compiled_methods;
90 compiled_methods.reserve(1u << 7);
91 for (auto&& c : code) {
92 for (auto&& s : src_map) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010093 for (auto&& v : vmap_table) {
94 for (auto&& f : cfi_info) {
95 for (auto&& p : patches) {
96 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
97 &driver, kNone, c, 0u, 0u, 0u, s, v, f, p));
Vladimir Marko35831e82015-09-11 11:59:18 +010098 }
99 }
100 }
101 }
102 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100103 constexpr size_t code_bit = 1u << 4;
104 constexpr size_t src_map_bit = 1u << 3;
105 constexpr size_t vmap_table_bit = 1u << 2;
Vladimir Marko35831e82015-09-11 11:59:18 +0100106 constexpr size_t cfi_info_bit = 1u << 1;
107 constexpr size_t patches_bit = 1u << 0;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100108 CHECK_EQ(compiled_methods.size(), 1u << 5);
Vladimir Marko35831e82015-09-11 11:59:18 +0100109 for (size_t i = 0; i != compiled_methods.size(); ++i) {
110 for (size_t j = 0; j != compiled_methods.size(); ++j) {
111 CompiledMethod* lhs = compiled_methods[i];
112 CompiledMethod* rhs = compiled_methods[j];
113 bool same_code = ((i ^ j) & code_bit) == 0u;
114 bool same_src_map = ((i ^ j) & src_map_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +0100115 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +0100116 bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u;
117 bool same_patches = ((i ^ j) & patches_bit) == 0u;
118 ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data())
119 << i << " " << j;
120 ASSERT_EQ(same_src_map, lhs->GetSrcMappingTable().data() == rhs->GetSrcMappingTable().data())
121 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100122 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
123 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +0100124 ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data())
125 << i << " " << j;
126 ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data())
127 << i << " " << j;
128 }
129 }
130 for (CompiledMethod* method : compiled_methods) {
131 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method);
132 }
133}
134
135} // namespace art