xref: /aosp_15_r20/art/compiler/optimizing/jit_patches_arm64.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "code_generation_data.h"
18*795d594fSAndroid Build Coastguard Worker #include "gc_root.h"
19*795d594fSAndroid Build Coastguard Worker #include "jit_patches_arm64.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker namespace arm64 {
24*795d594fSAndroid Build Coastguard Worker 
DeduplicateUint32Literal(uint32_t value)25*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateUint32Literal(
26*795d594fSAndroid Build Coastguard Worker     uint32_t value) {
27*795d594fSAndroid Build Coastguard Worker   return uint32_literals_.GetOrCreate(
28*795d594fSAndroid Build Coastguard Worker       value,
29*795d594fSAndroid Build Coastguard Worker       [this, value]() {
30*795d594fSAndroid Build Coastguard Worker         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(value);
31*795d594fSAndroid Build Coastguard Worker       });
32*795d594fSAndroid Build Coastguard Worker }
33*795d594fSAndroid Build Coastguard Worker 
DeduplicateUint64Literal(uint64_t value)34*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint64_t>* JitPatchesARM64::DeduplicateUint64Literal(
35*795d594fSAndroid Build Coastguard Worker     uint64_t value) {
36*795d594fSAndroid Build Coastguard Worker   return uint64_literals_.GetOrCreate(
37*795d594fSAndroid Build Coastguard Worker       value,
38*795d594fSAndroid Build Coastguard Worker       [this, value]() {
39*795d594fSAndroid Build Coastguard Worker         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint64_t>(value);
40*795d594fSAndroid Build Coastguard Worker       });
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker 
PatchJitRootUse(uint8_t * code,const uint8_t * roots_data,vixl::aarch64::Literal<uint32_t> * literal,uint64_t index_in_table)43*795d594fSAndroid Build Coastguard Worker static void PatchJitRootUse(uint8_t* code,
44*795d594fSAndroid Build Coastguard Worker                             const uint8_t* roots_data,
45*795d594fSAndroid Build Coastguard Worker                             vixl::aarch64::Literal<uint32_t>* literal,
46*795d594fSAndroid Build Coastguard Worker                             uint64_t index_in_table) {
47*795d594fSAndroid Build Coastguard Worker   uint32_t literal_offset = literal->GetOffset();
48*795d594fSAndroid Build Coastguard Worker   uintptr_t address =
49*795d594fSAndroid Build Coastguard Worker       reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
50*795d594fSAndroid Build Coastguard Worker   uint8_t* data = code + literal_offset;
51*795d594fSAndroid Build Coastguard Worker   reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker 
EmitJitRootPatches(uint8_t * code,const uint8_t * roots_data,const CodeGenerationData & code_generation_data) const54*795d594fSAndroid Build Coastguard Worker void JitPatchesARM64::EmitJitRootPatches(
55*795d594fSAndroid Build Coastguard Worker     uint8_t* code,
56*795d594fSAndroid Build Coastguard Worker     const uint8_t* roots_data,
57*795d594fSAndroid Build Coastguard Worker     const CodeGenerationData& code_generation_data) const {
58*795d594fSAndroid Build Coastguard Worker   for (const auto& entry : jit_string_patches_) {
59*795d594fSAndroid Build Coastguard Worker     const StringReference& string_reference = entry.first;
60*795d594fSAndroid Build Coastguard Worker     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
61*795d594fSAndroid Build Coastguard Worker     uint64_t index_in_table = code_generation_data.GetJitStringRootIndex(string_reference);
62*795d594fSAndroid Build Coastguard Worker     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker   for (const auto& entry : jit_class_patches_) {
65*795d594fSAndroid Build Coastguard Worker     const TypeReference& type_reference = entry.first;
66*795d594fSAndroid Build Coastguard Worker     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
67*795d594fSAndroid Build Coastguard Worker     uint64_t index_in_table = code_generation_data.GetJitClassRootIndex(type_reference);
68*795d594fSAndroid Build Coastguard Worker     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
69*795d594fSAndroid Build Coastguard Worker   }
70*795d594fSAndroid Build Coastguard Worker   for (const auto& entry : jit_method_type_patches_) {
71*795d594fSAndroid Build Coastguard Worker     const ProtoReference& proto_reference = entry.first;
72*795d594fSAndroid Build Coastguard Worker     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
73*795d594fSAndroid Build Coastguard Worker     uint64_t index_in_table = code_generation_data.GetJitMethodTypeRootIndex(proto_reference);
74*795d594fSAndroid Build Coastguard Worker     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
DeduplicateBootImageAddressLiteral(uint64_t address)78*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateBootImageAddressLiteral(
79*795d594fSAndroid Build Coastguard Worker     uint64_t address) {
80*795d594fSAndroid Build Coastguard Worker   return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker 
DeduplicateJitStringLiteral(const DexFile & dex_file,dex::StringIndex string_index,Handle<mirror::String> handle,CodeGenerationData * code_generation_data)83*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitStringLiteral(
84*795d594fSAndroid Build Coastguard Worker     const DexFile& dex_file,
85*795d594fSAndroid Build Coastguard Worker     dex::StringIndex string_index,
86*795d594fSAndroid Build Coastguard Worker     Handle<mirror::String> handle,
87*795d594fSAndroid Build Coastguard Worker     CodeGenerationData* code_generation_data) {
88*795d594fSAndroid Build Coastguard Worker   code_generation_data->ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
89*795d594fSAndroid Build Coastguard Worker   return jit_string_patches_.GetOrCreate(
90*795d594fSAndroid Build Coastguard Worker       StringReference(&dex_file, string_index),
91*795d594fSAndroid Build Coastguard Worker       [this]() {
92*795d594fSAndroid Build Coastguard Worker         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
93*795d594fSAndroid Build Coastguard Worker       });
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker 
DeduplicateJitClassLiteral(const DexFile & dex_file,dex::TypeIndex type_index,Handle<mirror::Class> handle,CodeGenerationData * code_generation_data)96*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitClassLiteral(
97*795d594fSAndroid Build Coastguard Worker     const DexFile& dex_file,
98*795d594fSAndroid Build Coastguard Worker     dex::TypeIndex type_index,
99*795d594fSAndroid Build Coastguard Worker     Handle<mirror::Class> handle,
100*795d594fSAndroid Build Coastguard Worker     CodeGenerationData* code_generation_data) {
101*795d594fSAndroid Build Coastguard Worker   code_generation_data->ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
102*795d594fSAndroid Build Coastguard Worker   return jit_class_patches_.GetOrCreate(
103*795d594fSAndroid Build Coastguard Worker       TypeReference(&dex_file, type_index),
104*795d594fSAndroid Build Coastguard Worker       [this]() {
105*795d594fSAndroid Build Coastguard Worker         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
106*795d594fSAndroid Build Coastguard Worker       });
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker 
DeduplicateJitMethodTypeLiteral(const DexFile & dex_file,dex::ProtoIndex proto_index,Handle<mirror::MethodType> handle,CodeGenerationData * code_generation_data)109*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitMethodTypeLiteral(
110*795d594fSAndroid Build Coastguard Worker     const DexFile& dex_file,
111*795d594fSAndroid Build Coastguard Worker     dex::ProtoIndex proto_index,
112*795d594fSAndroid Build Coastguard Worker     Handle<mirror::MethodType> handle,
113*795d594fSAndroid Build Coastguard Worker     CodeGenerationData* code_generation_data) {
114*795d594fSAndroid Build Coastguard Worker   code_generation_data->ReserveJitMethodTypeRoot(ProtoReference(&dex_file, proto_index), handle);
115*795d594fSAndroid Build Coastguard Worker   return jit_method_type_patches_.GetOrCreate(
116*795d594fSAndroid Build Coastguard Worker       ProtoReference(&dex_file, proto_index),
117*795d594fSAndroid Build Coastguard Worker       [this]() {
118*795d594fSAndroid Build Coastguard Worker         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
119*795d594fSAndroid Build Coastguard Worker       });
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker }  // namespace arm64
123*795d594fSAndroid Build Coastguard Worker }  // namespace art
124