| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_arm.h" |
| 18 | |
| 19 | #include "base/arena_containers.h" |
| Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 20 | #include "code_generator_arm.h" |
| 21 | #include "intrinsics_arm.h" |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 22 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace arm { |
| 26 | |
| 27 | /** |
| 28 | * Finds instructions that need the dex cache arrays base as an input. |
| 29 | */ |
| 30 | class DexCacheArrayFixupsVisitor : public HGraphVisitor { |
| 31 | public: |
| Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 32 | DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen) |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 33 | : HGraphVisitor(graph), |
| Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 34 | codegen_(down_cast<CodeGeneratorARM*>(codegen)), |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 35 | dex_cache_array_bases_(std::less<const DexFile*>(), |
| 36 | // Attribute memory use to code generator. |
| 37 | graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {} |
| 38 | |
| Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 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 | HArmDexCacheArraysBase* base = entry.second; |
| 45 | base->MoveBeforeFirstUserAndOutOfLoops(); |
| 46 | } |
| 47 | } |
| 48 | |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 49 | private: |
| Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 50 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
| 51 | // If this is a load with PC-relative access to the dex cache types array, |
| 52 | // we need to add the dex cache arrays base as the special input. |
| 53 | if (load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCachePcRelative) { |
| 54 | // Initialize base for target dex file if needed. |
| 55 | const DexFile& dex_file = load_class->GetDexFile(); |
| 56 | HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file); |
| 57 | // Update the element offset in base. |
| 58 | DexCacheArraysLayout layout(kArmPointerSize, &dex_file); |
| 59 | base->UpdateElementOffset(layout.TypeOffset(load_class->GetTypeIndex())); |
| 60 | // Add the special argument base to the load. |
| 61 | load_class->AddSpecialInput(base); |
| 62 | } |
| 63 | } |
| 64 | |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 65 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
| Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 66 | // If this is a load with PC-relative access to the dex cache strings array, |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 67 | // we need to add the dex cache arrays base as the special input. |
| 68 | if (load_string->GetLoadKind() == HLoadString::LoadKind::kDexCachePcRelative) { |
| 69 | // Initialize base for target dex file if needed. |
| 70 | const DexFile& dex_file = load_string->GetDexFile(); |
| 71 | HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file); |
| 72 | // Update the element offset in base. |
| 73 | DexCacheArraysLayout layout(kArmPointerSize, &dex_file); |
| 74 | base->UpdateElementOffset(layout.StringOffset(load_string->GetStringIndex())); |
| 75 | // Add the special argument base to the load. |
| 76 | load_string->AddSpecialInput(base); |
| 77 | } |
| 78 | } |
| 79 | |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 80 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 81 | // If this is an invoke with PC-relative access to the dex cache methods array, |
| 82 | // 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] | 83 | if (invoke->HasPcRelativeDexCache() && |
| 84 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderARM>(invoke, codegen_)) { |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 85 | // Initialize base for target method dex file if needed. |
| 86 | MethodReference target_method = invoke->GetTargetMethod(); |
| Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 87 | HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file); |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 88 | // Update the element offset in base. |
| 89 | DexCacheArraysLayout layout(kArmPointerSize, target_method.dex_file); |
| 90 | base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index)); |
| 91 | // Add the special argument base to the method. |
| 92 | DCHECK(!invoke->HasCurrentMethodInput()); |
| 93 | invoke->AddSpecialInput(base); |
| 94 | } |
| 95 | } |
| 96 | |
| Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 97 | HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) { |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 98 | // Ensure we only initialize the pointer once for each dex file. |
| 99 | auto lb = dex_cache_array_bases_.lower_bound(&dex_file); |
| 100 | if (lb != dex_cache_array_bases_.end() && |
| 101 | !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) { |
| 102 | return lb->second; |
| 103 | } |
| 104 | |
| Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 105 | // Insert the base at the start of the entry block, move it to a better |
| 106 | // position later in MoveBaseIfNeeded(). |
| 107 | HArmDexCacheArraysBase* base = new (GetGraph()->GetArena()) HArmDexCacheArraysBase(dex_file); |
| 108 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 109 | entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction()); |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 110 | dex_cache_array_bases_.PutBefore(lb, &dex_file, base); |
| 111 | return base; |
| 112 | } |
| 113 | |
| Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 114 | CodeGeneratorARM* codegen_; |
| 115 | |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 116 | using DexCacheArraysBaseMap = |
| 117 | ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>; |
| 118 | DexCacheArraysBaseMap dex_cache_array_bases_; |
| 119 | }; |
| 120 | |
| 121 | void DexCacheArrayFixups::Run() { |
| Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 122 | if (graph_->HasIrreducibleLoops()) { |
| 123 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 124 | // that can be live-in at the irreducible loop header. |
| 125 | return; |
| 126 | } |
| Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 127 | DexCacheArrayFixupsVisitor visitor(graph_, codegen_); |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 128 | visitor.VisitInsertionOrder(); |
| Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 129 | visitor.MoveBasesIfNeeded(); |
| Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace arm |
| 133 | } // namespace art |