Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 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 | |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 17 | #include "code_generator_mips.h" |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 18 | #include "dex_cache_array_fixups_mips.h" |
| 19 | |
| 20 | #include "base/arena_containers.h" |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 21 | #include "intrinsics_mips.h" |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 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: |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 32 | explicit DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen) |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 33 | : HGraphVisitor(graph), |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 34 | codegen_(down_cast<CodeGeneratorMIPS*>(codegen)), |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 35 | 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 Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 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() && !codegen_->GetInstructionSetFeatures().IsR6()) { |
| 51 | codegen_->ClobberRA(); |
| 52 | } |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 53 | } |
| 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 Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 59 | if (invoke->HasPcRelativeDexCache() && |
| 60 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) { |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 61 | // Initialize base for target method dex file if needed. |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 62 | HMipsDexCacheArraysBase* base = |
| 63 | GetOrCreateDexCacheArrayBase(invoke->GetDexFileForPcRelativeDexCache()); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 64 | // Update the element offset in base. |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 65 | DexCacheArraysLayout layout(kMipsPointerSize, &invoke->GetDexFileForPcRelativeDexCache()); |
Nicolas Geoffray | b47d9c5 | 2016-09-23 12:37:48 +0100 | [diff] [blame] | 66 | base->UpdateElementOffset(layout.MethodOffset(invoke->GetDexMethodIndex())); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 67 | // 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 Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 87 | CodeGeneratorMIPS* codegen_; |
| 88 | |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 89 | using DexCacheArraysBaseMap = |
| 90 | ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>; |
| 91 | DexCacheArraysBaseMap dex_cache_array_bases_; |
| 92 | }; |
| 93 | |
| 94 | void 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 Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 100 | DexCacheArrayFixupsVisitor visitor(graph_, codegen_); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 101 | visitor.VisitInsertionOrder(); |
| 102 | visitor.MoveBasesIfNeeded(); |
| 103 | } |
| 104 | |
| 105 | } // namespace mips |
| 106 | } // namespace art |