blob: 31fff26dd5d66df788a2f1a0de227d9e431d67ef [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
Alexey Frunzee3fb2452016-05-10 16:08:05 -070071 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
72 // If this is an invoke with PC-relative access to the dex cache methods array,
73 // we need to add the dex cache arrays base as the special input.
Vladimir Marko68c981f2016-08-26 13:13:33 +010074 if (invoke->HasPcRelativeDexCache() &&
75 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -070076 // Initialize base for target method dex file if needed.
Nicolas Geoffrayb47d9c52016-09-23 12:37:48 +010077 HMipsDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(invoke->GetDexFile());
Alexey Frunzee3fb2452016-05-10 16:08:05 -070078 // Update the element offset in base.
Nicolas Geoffrayb47d9c52016-09-23 12:37:48 +010079 DexCacheArraysLayout layout(kMipsPointerSize, &invoke->GetDexFile());
80 base->UpdateElementOffset(layout.MethodOffset(invoke->GetDexMethodIndex()));
Alexey Frunzee3fb2452016-05-10 16:08:05 -070081 // Add the special argument base to the method.
82 DCHECK(!invoke->HasCurrentMethodInput());
83 invoke->AddSpecialInput(base);
84 }
85 }
86
87 HMipsDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
88 return dex_cache_array_bases_.GetOrCreate(
89 &dex_file,
90 [this, &dex_file]() {
91 HMipsDexCacheArraysBase* base =
92 new (GetGraph()->GetArena()) HMipsDexCacheArraysBase(dex_file);
93 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
94 // Insert the base at the start of the entry block, move it to a better
95 // position later in MoveBaseIfNeeded().
96 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
97 return base;
98 });
99 }
100
Alexey Frunze06a46c42016-07-19 15:00:40 -0700101 CodeGeneratorMIPS* codegen_;
102
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700103 using DexCacheArraysBaseMap =
104 ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>;
105 DexCacheArraysBaseMap dex_cache_array_bases_;
106};
107
108void DexCacheArrayFixups::Run() {
109 if (graph_->HasIrreducibleLoops()) {
110 // Do not run this optimization, as irreducible loops do not work with an instruction
111 // that can be live-in at the irreducible loop header.
112 return;
113 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700114 DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700115 visitor.VisitInsertionOrder();
116 visitor.MoveBasesIfNeeded();
117}
118
119} // namespace mips
120} // namespace art