blob: 3db254a004e62df967ed9695019d2ecc5aa50d84 [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:
47 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
48 // If this is an invoke 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 (invoke->HasPcRelativeDexCache()) {
51 // Initialize base for target method dex file if needed.
52 MethodReference target_method = invoke->GetTargetMethod();
Vladimir Markofb337ea2015-11-25 15:25:10 +000053 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(*target_method.dex_file);
Vladimir Markob4536b72015-11-24 13:45:23 +000054 // Update the element offset in base.
55 DexCacheArraysLayout layout(kArmPointerSize, target_method.dex_file);
56 base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index));
57 // Add the special argument base to the method.
58 DCHECK(!invoke->HasCurrentMethodInput());
59 invoke->AddSpecialInput(base);
60 }
61 }
62
Vladimir Markofb337ea2015-11-25 15:25:10 +000063 HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
Vladimir Markob4536b72015-11-24 13:45:23 +000064 // Ensure we only initialize the pointer once for each dex file.
65 auto lb = dex_cache_array_bases_.lower_bound(&dex_file);
66 if (lb != dex_cache_array_bases_.end() &&
67 !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) {
68 return lb->second;
69 }
70
Vladimir Markofb337ea2015-11-25 15:25:10 +000071 // Insert the base at the start of the entry block, move it to a better
72 // position later in MoveBaseIfNeeded().
73 HArmDexCacheArraysBase* base = new (GetGraph()->GetArena()) HArmDexCacheArraysBase(dex_file);
74 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
75 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
Vladimir Markob4536b72015-11-24 13:45:23 +000076 dex_cache_array_bases_.PutBefore(lb, &dex_file, base);
77 return base;
78 }
79
80 using DexCacheArraysBaseMap =
81 ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>;
82 DexCacheArraysBaseMap dex_cache_array_bases_;
83};
84
85void DexCacheArrayFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000086 if (graph_->HasIrreducibleLoops()) {
87 // Do not run this optimization, as irreducible loops do not work with an instruction
88 // that can be live-in at the irreducible loop header.
89 return;
90 }
Vladimir Markob4536b72015-11-24 13:45:23 +000091 DexCacheArrayFixupsVisitor visitor(graph_);
92 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +000093 visitor.MoveBasesIfNeeded();
Vladimir Markob4536b72015-11-24 13:45:23 +000094}
95
96} // namespace arm
97} // namespace art