blob: 2e2d1f99f33f734d89810c87fbede74aa90450a7 [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 Marko45724f92016-02-17 17:46:10 +000035 Compiler::kOptimizing, kNone,
36 nullptr,
37 false,
38 nullptr,
39 nullptr,
40 nullptr,
41 1u,
42 false,
43 false,
44 nullptr,
45 -1,
46 nullptr,
47 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 };
64 const uint8_t raw_mapping_table1[] = { 5, 6, 7 };
65 const uint8_t raw_mapping_table2[] = { 7, 6, 5, 4 };
66 ArrayRef<const uint8_t> mapping_table[] = {
67 ArrayRef<const uint8_t>(raw_mapping_table1),
68 ArrayRef<const uint8_t>(raw_mapping_table2),
69 };
70 const uint8_t raw_vmap_table1[] = { 2, 4, 6 };
71 const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 };
72 ArrayRef<const uint8_t> vmap_table[] = {
73 ArrayRef<const uint8_t>(raw_vmap_table1),
74 ArrayRef<const uint8_t>(raw_vmap_table2),
75 };
76 const uint8_t raw_gc_map1[] = { 9, 8, 7 };
77 const uint8_t raw_gc_map2[] = { 6, 7, 8, 9 };
78 ArrayRef<const uint8_t> gc_map[] = {
79 ArrayRef<const uint8_t>(raw_gc_map1),
80 ArrayRef<const uint8_t>(raw_gc_map2),
81 };
82 const uint8_t raw_cfi_info1[] = { 1, 3, 5 };
83 const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 };
84 ArrayRef<const uint8_t> cfi_info[] = {
85 ArrayRef<const uint8_t>(raw_cfi_info1),
86 ArrayRef<const uint8_t>(raw_cfi_info2),
87 };
88 const LinkerPatch raw_patches1[] = {
89 LinkerPatch::CodePatch(0u, nullptr, 1u),
90 LinkerPatch::MethodPatch(4u, nullptr, 1u),
91 };
92 const LinkerPatch raw_patches2[] = {
93 LinkerPatch::CodePatch(0u, nullptr, 1u),
94 LinkerPatch::MethodPatch(4u, nullptr, 2u),
95 };
96 ArrayRef<const LinkerPatch> patches[] = {
97 ArrayRef<const LinkerPatch>(raw_patches1),
98 ArrayRef<const LinkerPatch>(raw_patches2),
99 };
100
101 std::vector<CompiledMethod*> compiled_methods;
102 compiled_methods.reserve(1u << 7);
103 for (auto&& c : code) {
104 for (auto&& s : src_map) {
105 for (auto&& m : mapping_table) {
106 for (auto&& v : vmap_table) {
107 for (auto&& g : gc_map) {
108 for (auto&& f : cfi_info) {
109 for (auto&& p : patches) {
110 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
111 &driver, kNone, c, 0u, 0u, 0u, s, m, v, g, f, p));
112 }
113 }
114 }
115 }
116 }
117 }
118 }
119 constexpr size_t code_bit = 1u << 6;
120 constexpr size_t src_map_bit = 1u << 5;
121 constexpr size_t mapping_table_bit = 1u << 4;
122 constexpr size_t vmap_table_bit = 1u << 3;
123 constexpr size_t gc_map_bit = 1u << 2;
124 constexpr size_t cfi_info_bit = 1u << 1;
125 constexpr size_t patches_bit = 1u << 0;
126 CHECK_EQ(compiled_methods.size(), 1u << 7);
127 for (size_t i = 0; i != compiled_methods.size(); ++i) {
128 for (size_t j = 0; j != compiled_methods.size(); ++j) {
129 CompiledMethod* lhs = compiled_methods[i];
130 CompiledMethod* rhs = compiled_methods[j];
131 bool same_code = ((i ^ j) & code_bit) == 0u;
132 bool same_src_map = ((i ^ j) & src_map_bit) == 0u;
133 bool same_mapping_table = ((i ^ j) & mapping_table_bit) == 0u;
134 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
135 bool same_gc_map = ((i ^ j) & gc_map_bit) == 0u;
136 bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u;
137 bool same_patches = ((i ^ j) & patches_bit) == 0u;
138 ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data())
139 << i << " " << j;
140 ASSERT_EQ(same_src_map, lhs->GetSrcMappingTable().data() == rhs->GetSrcMappingTable().data())
141 << i << " " << j;
142 ASSERT_EQ(same_mapping_table, lhs->GetMappingTable().data() == rhs->GetMappingTable().data())
143 << i << " " << j;
144 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
145 << i << " " << j;
146 ASSERT_EQ(same_gc_map, lhs->GetGcMap().data() == rhs->GetGcMap().data())
147 << i << " " << j;
148 ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data())
149 << i << " " << j;
150 ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data())
151 << i << " " << j;
152 }
153 }
154 for (CompiledMethod* method : compiled_methods) {
155 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method);
156 }
157}
158
159} // namespace art