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