blob: 14c318e21f38e41c291a84852feec8ceae8dd135 [file] [log] [blame]
Vladimir Markob4536b72015-11-24 13:45:23 +00001/*
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
22namespace art {
23namespace arm {
24
25/**
26 * Finds instructions that need the dex cache arrays base as an input.
27 */
28class 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
Vladimir Markofb337ea2015-11-25 15:25:10 +000036 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
Vladimir Markob4536b72015-11-24 13:45:23 +000046 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010047 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
48 // If this is a load with PC-relative access to the dex cache types array,
49 // we need to add the dex cache arrays base as the special input.
50 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCachePcRelative) {
51 // Initialize base for target dex file if needed.
52 const DexFile& dex_file = load_class->GetDexFile();
53 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file);
54 // Update the element offset in base.
55 DexCacheArraysLayout layout(kArmPointerSize, &dex_file);
56 base->UpdateElementOffset(layout.TypeOffset(load_class->GetTypeIndex()));
57 // Add the special argument base to the load.
58 load_class->AddSpecialInput(base);
59 }
60 }
61
Vladimir Markocac5a7e2016-02-22 10:39:50 +000062 void VisitLoadString(HLoadString* load_string) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010063 // If this is a load with PC-relative access to the dex cache strings array,
Vladimir Markocac5a7e2016-02-22 10:39:50 +000064 // we need to add the dex cache arrays base as the special input.
65 if (load_string->GetLoadKind() == HLoadString::LoadKind::kDexCachePcRelative) {
66 // Initialize base for target dex file if needed.
67 const DexFile& dex_file = load_string->GetDexFile();
68 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(dex_file);
69 // Update the element offset in base.
70 DexCacheArraysLayout layout(kArmPointerSize, &dex_file);
71 base->UpdateElementOffset(layout.StringOffset(load_string->GetStringIndex()));
72 // Add the special argument base to the load.
73 load_string->AddSpecialInput(base);
74 }
75 }
76
Vladimir Markob4536b72015-11-24 13:45:23 +000077 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.
80 if (invoke->HasPcRelativeDexCache()) {
81 // Initialize base for target method dex file if needed.
82 MethodReference target_method = invoke->GetTargetMethod();
Vladimir Markofb337ea2015-11-25 15:25:10 +000083 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file);
Vladimir Markob4536b72015-11-24 13:45:23 +000084 // Update the element offset in base.
85 DexCacheArraysLayout layout(kArmPointerSize, target_method.dex_file);
86 base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index));
87 // Add the special argument base to the method.
88 DCHECK(!invoke->HasCurrentMethodInput());
89 invoke->AddSpecialInput(base);
90 }
91 }
92
Vladimir Markofb337ea2015-11-25 15:25:10 +000093 HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
Vladimir Markob4536b72015-11-24 13:45:23 +000094 // Ensure we only initialize the pointer once for each dex file.
95 auto lb = dex_cache_array_bases_.lower_bound(&dex_file);
96 if (lb != dex_cache_array_bases_.end() &&
97 !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) {
98 return lb->second;
99 }
100
Vladimir Markofb337ea2015-11-25 15:25:10 +0000101 // Insert the base at the start of the entry block, move it to a better
102 // position later in MoveBaseIfNeeded().
103 HArmDexCacheArraysBase* base = new (GetGraph()->GetArena()) HArmDexCacheArraysBase(dex_file);
104 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
105 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
Vladimir Markob4536b72015-11-24 13:45:23 +0000106 dex_cache_array_bases_.PutBefore(lb, &dex_file, base);
107 return base;
108 }
109
110 using DexCacheArraysBaseMap =
111 ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>;
112 DexCacheArraysBaseMap dex_cache_array_bases_;
113};
114
115void DexCacheArrayFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000116 if (graph_->HasIrreducibleLoops()) {
117 // Do not run this optimization, as irreducible loops do not work with an instruction
118 // that can be live-in at the irreducible loop header.
119 return;
120 }
Vladimir Markob4536b72015-11-24 13:45:23 +0000121 DexCacheArrayFixupsVisitor visitor(graph_);
122 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000123 visitor.MoveBasesIfNeeded();
Vladimir Markob4536b72015-11-24 13:45:23 +0000124}
125
126} // namespace arm
127} // namespace art