| 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 | |
| 17 | #include "dex_cache_array_fixups_mips.h" |
| 18 | |
| 19 | #include "base/arena_containers.h" |
| 20 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 21 | |
| 22 | namespace art { |
| 23 | namespace mips { |
| 24 | |
| 25 | /** |
| 26 | * Finds instructions that need the dex cache arrays base as an input. |
| 27 | */ |
| 28 | class DexCacheArrayFixupsVisitor : public HGraphVisitor { |
| 29 | public: |
| 30 | explicit DexCacheArrayFixupsVisitor(HGraph* graph) |
| 31 | : HGraphVisitor(graph), |
| 32 | dex_cache_array_bases_(std::less<const DexFile*>(), |
| 33 | // Attribute memory use to code generator. |
| 34 | graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {} |
| 35 | |
| 36 | void MoveBasesIfNeeded() { |
| 37 | for (const auto& entry : dex_cache_array_bases_) { |
| 38 | // Bring the base closer to the first use (previously, it was in the |
| 39 | // entry block) and relieve some pressure on the register allocator |
| 40 | // while avoiding recalculation of the base in a loop. |
| 41 | HMipsDexCacheArraysBase* base = entry.second; |
| 42 | base->MoveBeforeFirstUserAndOutOfLoops(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 48 | // If this is an invoke with PC-relative access to the dex cache methods array, |
| 49 | // we need to add the dex cache arrays base as the special input. |
| 50 | if (invoke->HasPcRelativeDexCache()) { |
| 51 | // Initialize base for target method dex file if needed. |
| 52 | MethodReference target_method = invoke->GetTargetMethod(); |
| 53 | HMipsDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file); |
| 54 | // Update the element offset in base. |
| 55 | DexCacheArraysLayout layout(kMipsPointerSize, target_method.dex_file); |
| 56 | base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index)); |
| 57 | // Add the special argument base to the method. |
| 58 | DCHECK(!invoke->HasCurrentMethodInput()); |
| 59 | invoke->AddSpecialInput(base); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | HMipsDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) { |
| 64 | return dex_cache_array_bases_.GetOrCreate( |
| 65 | &dex_file, |
| 66 | [this, &dex_file]() { |
| 67 | HMipsDexCacheArraysBase* base = |
| 68 | new (GetGraph()->GetArena()) HMipsDexCacheArraysBase(dex_file); |
| 69 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 70 | // Insert the base at the start of the entry block, move it to a better |
| 71 | // position later in MoveBaseIfNeeded(). |
| 72 | entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction()); |
| 73 | return base; |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | using DexCacheArraysBaseMap = |
| 78 | ArenaSafeMap<const DexFile*, HMipsDexCacheArraysBase*, std::less<const DexFile*>>; |
| 79 | DexCacheArraysBaseMap dex_cache_array_bases_; |
| 80 | }; |
| 81 | |
| 82 | void DexCacheArrayFixups::Run() { |
| 83 | if (graph_->HasIrreducibleLoops()) { |
| 84 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 85 | // that can be live-in at the irreducible loop header. |
| 86 | return; |
| 87 | } |
| 88 | DexCacheArrayFixupsVisitor visitor(graph_); |
| 89 | visitor.VisitInsertionOrder(); |
| 90 | visitor.MoveBasesIfNeeded(); |
| 91 | } |
| 92 | |
| 93 | } // namespace mips |
| 94 | } // namespace art |