blob: 300284d04cf6f764471e47e49143da4f90ded876 [file] [log] [blame]
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001/*
2 * Copyright (C) 2016 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
Alexey Frunze06a46c42016-07-19 15:00:40 -070017#include "code_generator_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070018#include "dex_cache_array_fixups_mips.h"
19
20#include "base/arena_containers.h"
Vladimir Marko68c981f2016-08-26 13:13:33 +010021#include "intrinsics_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070022#include "utils/dex_cache_arrays_layout-inl.h"
23
24namespace art {
25namespace mips {
26
27/**
28 * Finds instructions that need the dex cache arrays base as an input.
29 */
30class DexCacheArrayFixupsVisitor : public HGraphVisitor {
31 public:
Alexey Frunze06a46c42016-07-19 15:00:40 -070032 explicit DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen)
Alexey Frunzee3fb2452016-05-10 16:08:05 -070033 : HGraphVisitor(graph),
Alexey Frunze06a46c42016-07-19 15:00:40 -070034 codegen_(down_cast<CodeGeneratorMIPS*>(codegen)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -070035 dex_cache_array_bases_(std::less<const DexFile*>(),
36 // Attribute memory use to code generator.
37 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {}
38
39 void MoveBasesIfNeeded() {
40 for (const auto& entry : dex_cache_array_bases_) {
41 // Bring the base closer to the first use (previously, it was in the
42 // entry block) and relieve some pressure on the register allocator
43 // while avoiding recalculation of the base in a loop.
44 HMipsDexCacheArraysBase* base = entry.second;
45 base->MoveBeforeFirstUserAndOutOfLoops();
46 }
Alexey Frunze06a46c42016-07-19 15:00:40 -070047 // Computing the dex cache base for PC-relative accesses will clobber RA with
48 // the NAL instruction on R2. Take a note of this before generating the method
49 // entry.
50 if (!dex_cache_array_bases_.empty() && !codegen_->GetInstructionSetFeatures().IsR6()) {
51 codegen_->ClobberRA();
52 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -070053 }
54
55 private:
Alexey Frunze06a46c42016-07-19 15:00:40 -070056 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
57 // If this is a load with PC-relative access to the dex cache types array,
58 // we need to add the dex cache arrays base as the special input.
59 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCachePcRelative) {
60 // Initialize base for target dex file if needed.
61 const DexFile& dex_file = load_class->GetDexFile();
62 HMipsDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file);
63 // Update the element offset in base.
64 DexCacheArraysLayout layout(kMipsPointerSize, &dex_file);
65 base->UpdateElementOffset(layout.TypeOffset(load_class->GetTypeIndex()));
66 // Add the special argument base to the load.
67 load_class->AddSpecialInput(base);
68 }
69 }
70
71 void VisitLoadString(HLoadString* load_string) OVERRIDE {
72 // If this is a load with PC-relative access to the dex cache strings array,
73 // we need to add the dex cache arrays base as the special input.
74 if (load_string->GetLoadKind() == HLoadString::LoadKind::kDexCachePcRelative) {
75 // Initialize base for target dex file if needed.
76 const DexFile& dex_file = load_string->GetDexFile();
77 HMipsDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file);
78 // Update the element offset in base.
79 DexCacheArraysLayout layout(kMipsPointerSize, &dex_file);
80 base->UpdateElementOffset(layout.StringOffset(load_string->GetStringIndex()));
81 // Add the special argument base to the load.
82 load_string->AddSpecialInput(base);
83 }
84 }
85
Alexey Frunzee3fb2452016-05-10 16:08:05 -070086 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
87 // If this is an invoke with PC-relative access to the dex cache methods array,
88 // we need to add the dex cache arrays base as the special input.
Vladimir Marko68c981f2016-08-26 13:13:33 +010089 if (invoke->HasPcRelativeDexCache() &&
90 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -070091 // Initialize base for target method dex file if needed.
92 MethodReference target_method = invoke->GetTargetMethod();
93 HMipsDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file);
94 // Update the element offset in base.
95 DexCacheArraysLayout layout(kMipsPointerSize, target_method.dex_file);
96 base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index));
97 // Add the special argument base to the method.
98 DCHECK(!invoke->HasCurrentMethodInput());
99 invoke->AddSpecialInput(base);
100 }
101 }
102
103 HMipsDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
104 return dex_cache_array_bases_.GetOrCreate(
105 &dex_file,
106 [this, &dex_file]() {
107 HMipsDexCacheArraysBase* base =
108 new (GetGraph()->GetArena()) HMipsDexCacheArraysBase(dex_file);
109 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
110 // Insert the base at the start of the entry block, move it to a better
111 // position later in MoveBaseIfNeeded().
112 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
113 return base;
114 });
115 }
116
Alexey Frunze06a46c42016-07-19 15:00:40 -0700117 CodeGeneratorMIPS* codegen_;
118
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700119 using DexCacheArraysBaseMap =
120 ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>;
121 DexCacheArraysBaseMap dex_cache_array_bases_;
122};
123
124void DexCacheArrayFixups::Run() {
125 if (graph_->HasIrreducibleLoops()) {
126 // Do not run this optimization, as irreducible loops do not work with an instruction
127 // that can be live-in at the irreducible loop header.
128 return;
129 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700130 DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700131 visitor.VisitInsertionOrder();
132 visitor.MoveBasesIfNeeded();
133}
134
135} // namespace mips
136} // namespace art