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" 20 #include "utils/dex_cache_arrays_layout-inl.h" 21 22 namespace art { 23 namespace arm { 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 HArmDexCacheArraysBase* base = entry.second; 42 base->MoveBeforeFirstUserAndOutOfLoops(); 43 } 44 } 45 46 private: 47 void VisitLoadString(HLoadString* load_string) OVERRIDE { 48 // If this is a load 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 (load_string->GetLoadKind() == HLoadString::LoadKind::kDexCachePcRelative) { 51 // Initialize base for target dex file if needed. 52 const DexFile& dex_file = load_string->GetDexFile(); 53 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file); 54 // Update the element offset in base. 55 DexCacheArraysLayout layout(kArmPointerSize, &dex_file); 56 base->UpdateElementOffset(layout.StringOffset(load_string->GetStringIndex())); 57 // Add the special argument base to the load. 58 load_string->AddSpecialInput(base); 59 } 60 } 61 62 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { 63 // If this is an invoke with PC-relative access to the dex cache methods array, 64 // we need to add the dex cache arrays base as the special input. 65 if (invoke->HasPcRelativeDexCache()) { 66 // Initialize base for target method dex file if needed. 67 MethodReference target_method = invoke->GetTargetMethod(); 68 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file); 69 // Update the element offset in base. 70 DexCacheArraysLayout layout(kArmPointerSize, target_method.dex_file); 71 base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index)); 72 // Add the special argument base to the method. 73 DCHECK(!invoke->HasCurrentMethodInput()); 74 invoke->AddSpecialInput(base); 75 } 76 } 77 78 HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) { 79 // Ensure we only initialize the pointer once for each dex file. 80 auto lb = dex_cache_array_bases_.lower_bound(&dex_file); 81 if (lb != dex_cache_array_bases_.end() && 82 !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) { 83 return lb->second; 84 } 85 86 // Insert the base at the start of the entry block, move it to a better 87 // position later in MoveBaseIfNeeded(). 88 HArmDexCacheArraysBase* base = new (GetGraph()->GetArena()) HArmDexCacheArraysBase(dex_file); 89 HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); 90 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction()); 91 dex_cache_array_bases_.PutBefore(lb, &dex_file, base); 92 return base; 93 } 94 95 using DexCacheArraysBaseMap = 96 ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>; 97 DexCacheArraysBaseMap dex_cache_array_bases_; 98 }; 99 100 void DexCacheArrayFixups::Run() { 101 if (graph_->HasIrreducibleLoops()) { 102 // Do not run this optimization, as irreducible loops do not work with an instruction 103 // that can be live-in at the irreducible loop header. 104 return; 105 } 106 DexCacheArrayFixupsVisitor visitor(graph_); 107 visitor.VisitInsertionOrder(); 108 visitor.MoveBasesIfNeeded(); 109 } 110 111 } // namespace arm 112 } // namespace art 113