blob: 04a4294c48200c7836538ec45583588871c6337c [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:
56 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
57 // If this is an invoke with PC-relative access to the dex cache methods array,
58 // we need to add the dex cache arrays base as the special input.
Vladimir Marko68c981f2016-08-26 13:13:33 +010059 if (invoke->HasPcRelativeDexCache() &&
60 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -070061 // Initialize base for target method dex file if needed.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000062 HMipsDexCacheArraysBase* base =
63 GetOrCreateDexCacheArrayBase(invoke->GetDexFileForPcRelativeDexCache());
Alexey Frunzee3fb2452016-05-10 16:08:05 -070064 // Update the element offset in base.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000065 DexCacheArraysLayout layout(kMipsPointerSize, &invoke->GetDexFileForPcRelativeDexCache());
Nicolas Geoffrayb47d9c52016-09-23 12:37:48 +010066 base->UpdateElementOffset(layout.MethodOffset(invoke->GetDexMethodIndex()));
Alexey Frunzee3fb2452016-05-10 16:08:05 -070067 // Add the special argument base to the method.
68 DCHECK(!invoke->HasCurrentMethodInput());
69 invoke->AddSpecialInput(base);
70 }
71 }
72
73 HMipsDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
74 return dex_cache_array_bases_.GetOrCreate(
75 &dex_file,
76 [this, &dex_file]() {
77 HMipsDexCacheArraysBase* base =
78 new (GetGraph()->GetArena()) HMipsDexCacheArraysBase(dex_file);
79 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
80 // Insert the base at the start of the entry block, move it to a better
81 // position later in MoveBaseIfNeeded().
82 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
83 return base;
84 });
85 }
86
Alexey Frunze06a46c42016-07-19 15:00:40 -070087 CodeGeneratorMIPS* codegen_;
88
Alexey Frunzee3fb2452016-05-10 16:08:05 -070089 using DexCacheArraysBaseMap =
90 ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>;
91 DexCacheArraysBaseMap dex_cache_array_bases_;
92};
93
94void DexCacheArrayFixups::Run() {
95 if (graph_->HasIrreducibleLoops()) {
96 // Do not run this optimization, as irreducible loops do not work with an instruction
97 // that can be live-in at the irreducible loop header.
98 return;
99 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700100 DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700101 visitor.VisitInsertionOrder();
102 visitor.MoveBasesIfNeeded();
103}
104
105} // namespace mips
106} // namespace art