blob: 6ad9b07f1a6875665e344aed97e58356d4f44b56 [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"
Vladimir Marko68c981f2016-08-26 13:13:33 +010020#include "code_generator_arm.h"
21#include "intrinsics_arm.h"
Vladimir Markob4536b72015-11-24 13:45:23 +000022#include "utils/dex_cache_arrays_layout-inl.h"
23
24namespace art {
25namespace arm {
26
27/**
28 * Finds instructions that need the dex cache arrays base as an input.
29 */
30class DexCacheArrayFixupsVisitor : public HGraphVisitor {
31 public:
Vladimir Marko68c981f2016-08-26 13:13:33 +010032 DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen)
Vladimir Markob4536b72015-11-24 13:45:23 +000033 : HGraphVisitor(graph),
Vladimir Marko68c981f2016-08-26 13:13:33 +010034 codegen_(down_cast<CodeGeneratorARM*>(codegen)),
Vladimir Markob4536b72015-11-24 13:45:23 +000035 dex_cache_array_bases_(std::less<const DexFile*>(),
36 // Attribute memory use to code generator.
37 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {}
38
Vladimir Markofb337ea2015-11-25 15:25:10 +000039 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 Markob4536b72015-11-24 13:45:23 +000049 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010050 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 Markocac5a7e2016-02-22 10:39:50 +000065 void VisitLoadString(HLoadString* load_string) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010066 // If this is a load with PC-relative access to the dex cache strings array,
Vladimir Markocac5a7e2016-02-22 10:39:50 +000067 // 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 Markob4536b72015-11-24 13:45:23 +000080 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 Marko68c981f2016-08-26 13:13:33 +010083 if (invoke->HasPcRelativeDexCache() &&
84 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderARM>(invoke, codegen_)) {
Vladimir Markob4536b72015-11-24 13:45:23 +000085 // Initialize base for target method dex file if needed.
86 MethodReference target_method = invoke->GetTargetMethod();
Vladimir Markofb337ea2015-11-25 15:25:10 +000087 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file);
Vladimir Markob4536b72015-11-24 13:45:23 +000088 // 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 Markofb337ea2015-11-25 15:25:10 +000097 HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
Vladimir Markob4536b72015-11-24 13:45:23 +000098 // 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 Markofb337ea2015-11-25 15:25:10 +0000105 // 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 Markob4536b72015-11-24 13:45:23 +0000110 dex_cache_array_bases_.PutBefore(lb, &dex_file, base);
111 return base;
112 }
113
Vladimir Marko68c981f2016-08-26 13:13:33 +0100114 CodeGeneratorARM* codegen_;
115
Vladimir Markob4536b72015-11-24 13:45:23 +0000116 using DexCacheArraysBaseMap =
117 ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>;
118 DexCacheArraysBaseMap dex_cache_array_bases_;
119};
120
121void DexCacheArrayFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000122 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 Marko68c981f2016-08-26 13:13:33 +0100127 DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
Vladimir Markob4536b72015-11-24 13:45:23 +0000128 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000129 visitor.MoveBasesIfNeeded();
Vladimir Markob4536b72015-11-24 13:45:23 +0000130}
131
132} // namespace arm
133} // namespace art