1 /*
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 
17 #include "code_generator_mips.h"
18 #include "dex_cache_array_fixups_mips.h"
19 
20 #include "base/arena_containers.h"
21 #include "intrinsics_mips.h"
22 #include "utils/dex_cache_arrays_layout-inl.h"
23 
24 namespace art {
25 namespace mips {
26 
27 /**
28  * Finds instructions that need the dex cache arrays base as an input.
29  */
30 class DexCacheArrayFixupsVisitor : public HGraphVisitor {
31  public:
DexCacheArrayFixupsVisitor(HGraph * graph,CodeGenerator * codegen)32   explicit DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen)
33       : HGraphVisitor(graph),
34         codegen_(down_cast<CodeGeneratorMIPS*>(codegen)),
35         dex_cache_array_bases_(std::less<const DexFile*>(),
36                                // Attribute memory use to code generator.
37                                graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {}
38 
MoveBasesIfNeeded()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     }
47     // 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()) {
51       codegen_->ClobberRA();
52     }
53   }
54 
55  private:
VisitInvokeStaticOrDirect(HInvokeStaticOrDirect * invoke)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.
59     if (invoke->HasPcRelativeDexCache() &&
60         !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) {
61       // Initialize base for target method dex file if needed.
62       HMipsDexCacheArraysBase* base =
63           GetOrCreateDexCacheArrayBase(invoke->GetDexFileForPcRelativeDexCache());
64       // Update the element offset in base.
65       DexCacheArraysLayout layout(kMipsPointerSize, &invoke->GetDexFileForPcRelativeDexCache());
66       base->UpdateElementOffset(layout.MethodOffset(invoke->GetDexMethodIndex()));
67       // Add the special argument base to the method.
68       DCHECK(!invoke->HasCurrentMethodInput());
69       invoke->AddSpecialInput(base);
70     }
71   }
72 
GetOrCreateDexCacheArrayBase(const DexFile & dex_file)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 
87   CodeGeneratorMIPS* codegen_;
88 
89   using DexCacheArraysBaseMap =
90       ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>;
91   DexCacheArraysBaseMap dex_cache_array_bases_;
92 };
93 
Run()94 void DexCacheArrayFixups::Run() {
95   CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen_);
96   if (mips_codegen->GetInstructionSetFeatures().IsR6()) {
97     // Do nothing for R6 because it has PC-relative addressing.
98     return;
99   }
100   if (graph_->HasIrreducibleLoops()) {
101     // Do not run this optimization, as irreducible loops do not work with an instruction
102     // that can be live-in at the irreducible loop header.
103     return;
104   }
105   DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
106   visitor.VisitInsertionOrder();
107   visitor.MoveBasesIfNeeded();
108 }
109 
110 }  // namespace mips
111 }  // namespace art
112