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