blob: a3140d00cb40ddf482ad33315d0f56c290988c42 [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"
Artem Serovd4cc5b22016-11-04 11:19:09 +000020#ifdef ART_USE_VIXL_ARM_BACKEND
21#include "code_generator_arm_vixl.h"
22#include "intrinsics_arm_vixl.h"
23#else
Vladimir Marko68c981f2016-08-26 13:13:33 +010024#include "code_generator_arm.h"
25#include "intrinsics_arm.h"
Artem Serovd4cc5b22016-11-04 11:19:09 +000026#endif
Vladimir Markob4536b72015-11-24 13:45:23 +000027#include "utils/dex_cache_arrays_layout-inl.h"
28
29namespace art {
30namespace arm {
Artem Serovd4cc5b22016-11-04 11:19:09 +000031#ifdef ART_USE_VIXL_ARM_BACKEND
32typedef CodeGeneratorARMVIXL CodeGeneratorARMType;
33typedef IntrinsicLocationsBuilderARMVIXL IntrinsicLocationsBuilderARMType;
34#else
35typedef CodeGeneratorARM CodeGeneratorARMType;
36typedef IntrinsicLocationsBuilderARM IntrinsicLocationsBuilderARMType;
37#endif
Vladimir Markob4536b72015-11-24 13:45:23 +000038
39/**
40 * Finds instructions that need the dex cache arrays base as an input.
41 */
42class DexCacheArrayFixupsVisitor : public HGraphVisitor {
43 public:
Vladimir Marko68c981f2016-08-26 13:13:33 +010044 DexCacheArrayFixupsVisitor(HGraph* graph, CodeGenerator* codegen)
Vladimir Markob4536b72015-11-24 13:45:23 +000045 : HGraphVisitor(graph),
Artem Serovd4cc5b22016-11-04 11:19:09 +000046 codegen_(down_cast<CodeGeneratorARMType*>(codegen)),
Vladimir Markob4536b72015-11-24 13:45:23 +000047 dex_cache_array_bases_(std::less<const DexFile*>(),
48 // Attribute memory use to code generator.
49 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {}
50
Vladimir Markofb337ea2015-11-25 15:25:10 +000051 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 Markob4536b72015-11-24 13:45:23 +000061 private:
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.
Vladimir Marko68c981f2016-08-26 13:13:33 +010065 if (invoke->HasPcRelativeDexCache() &&
Artem Serovd4cc5b22016-11-04 11:19:09 +000066 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderARMType>(invoke, codegen_)) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010067 HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(invoke->GetDexFile());
Vladimir Markob4536b72015-11-24 13:45:23 +000068 // Update the element offset in base.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010069 DexCacheArraysLayout layout(kArmPointerSize, &invoke->GetDexFile());
70 base->UpdateElementOffset(layout.MethodOffset(invoke->GetDexMethodIndex()));
Vladimir Markob4536b72015-11-24 13:45:23 +000071 // Add the special argument base to the method.
72 DCHECK(!invoke->HasCurrentMethodInput());
73 invoke->AddSpecialInput(base);
74 }
75 }
76
Vladimir Markofb337ea2015-11-25 15:25:10 +000077 HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(const DexFile& dex_file) {
Vladimir Markob4536b72015-11-24 13:45:23 +000078 // Ensure we only initialize the pointer once for each dex file.
79 auto lb = dex_cache_array_bases_.lower_bound(&dex_file);
80 if (lb != dex_cache_array_bases_.end() &&
81 !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) {
82 return lb->second;
83 }
84
Vladimir Markofb337ea2015-11-25 15:25:10 +000085 // Insert the base at the start of the entry block, move it to a better
86 // position later in MoveBaseIfNeeded().
87 HArmDexCacheArraysBase* base = new (GetGraph()->GetArena()) HArmDexCacheArraysBase(dex_file);
88 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
89 entry_block->InsertInstructionBefore(base, entry_block->GetFirstInstruction());
Vladimir Markob4536b72015-11-24 13:45:23 +000090 dex_cache_array_bases_.PutBefore(lb, &dex_file, base);
91 return base;
92 }
93
Artem Serovd4cc5b22016-11-04 11:19:09 +000094 CodeGeneratorARMType* codegen_;
Vladimir Marko68c981f2016-08-26 13:13:33 +010095
Vladimir Markob4536b72015-11-24 13:45:23 +000096 using DexCacheArraysBaseMap =
97 ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>;
98 DexCacheArraysBaseMap dex_cache_array_bases_;
99};
100
101void DexCacheArrayFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102 if (graph_->HasIrreducibleLoops()) {
103 // Do not run this optimization, as irreducible loops do not work with an instruction
104 // that can be live-in at the irreducible loop header.
105 return;
106 }
Vladimir Marko68c981f2016-08-26 13:13:33 +0100107 DexCacheArrayFixupsVisitor visitor(graph_, codegen_);
Vladimir Markob4536b72015-11-24 13:45:23 +0000108 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000109 visitor.MoveBasesIfNeeded();
Vladimir Markob4536b72015-11-24 13:45:23 +0000110}
111
112} // namespace arm
113} // namespace art