dex_cache_array_fixups_arm.cc revision b4536b7de576b20c74c612406c5d3132998075ef
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
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
36 private:
37  void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
38    // If this is an invoke with PC-relative access to the dex cache methods array,
39    // we need to add the dex cache arrays base as the special input.
40    if (invoke->HasPcRelativeDexCache()) {
41      // Initialize base for target method dex file if needed.
42      MethodReference target_method = invoke->GetTargetMethod();
43      HArmDexCacheArraysBase* base = GetOrCreateDexCacheArrayBase(invoke, *target_method.dex_file);
44      // Update the element offset in base.
45      DexCacheArraysLayout layout(kArmPointerSize, target_method.dex_file);
46      base->UpdateElementOffset(layout.MethodOffset(target_method.dex_method_index));
47      // Add the special argument base to the method.
48      DCHECK(!invoke->HasCurrentMethodInput());
49      invoke->AddSpecialInput(base);
50    }
51  }
52
53  HArmDexCacheArraysBase* GetOrCreateDexCacheArrayBase(HInstruction* user,
54                                                       const DexFile& dex_file) {
55    // Ensure we only initialize the pointer once for each dex file.
56    auto lb = dex_cache_array_bases_.lower_bound(&dex_file);
57    if (lb != dex_cache_array_bases_.end() &&
58        !dex_cache_array_bases_.key_comp()(&dex_file, lb->first)) {
59      return lb->second;
60    }
61
62    HGraph* graph = GetGraph();
63    HBasicBlock* entry = graph->GetEntryBlock();
64    HArmDexCacheArraysBase* base = new (graph->GetArena()) HArmDexCacheArraysBase(dex_file);
65    HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
66    entry->InsertInstructionBefore(base, insert_pos);
67    dex_cache_array_bases_.PutBefore(lb, &dex_file, base);
68    return base;
69  }
70
71  using DexCacheArraysBaseMap =
72      ArenaSafeMap<const DexFile*, HArmDexCacheArraysBase*, std::less<const DexFile*>>;
73  DexCacheArraysBaseMap dex_cache_array_bases_;
74};
75
76void DexCacheArrayFixups::Run() {
77  DexCacheArrayFixupsVisitor visitor(graph_);
78  visitor.VisitInsertionOrder();
79}
80
81}  // namespace arm
82}  // namespace art
83