blob: b383f1e1addc5e24797161a18a36fc1f1d988632 [file] [log] [blame]
Mark Mendell94991072015-10-06 14:58:32 -04001/*
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
Vladimir Marko0f7dca42015-11-02 14:36:43 +000017#include "pc_relative_fixups_x86.h"
Mark Mendell94991072015-10-06 14:58:32 -040018
19namespace art {
20namespace x86 {
21
22/**
23 * Finds instructions that need the constant area base as an input.
24 */
Vladimir Marko0f7dca42015-11-02 14:36:43 +000025class PCRelativeHandlerVisitor : public HGraphVisitor {
Mark Mendell94991072015-10-06 14:58:32 -040026 public:
Vladimir Marko0f7dca42015-11-02 14:36:43 +000027 explicit PCRelativeHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
Mark Mendell94991072015-10-06 14:58:32 -040028
Vladimir Markofb337ea2015-11-25 15:25:10 +000029 void MoveBaseIfNeeded() {
30 if (base_ != nullptr) {
31 // Bring the base closer to the first use (previously, it was in the
32 // entry block) and relieve some pressure on the register allocator
33 // while avoiding recalculation of the base in a loop.
34 base_->MoveBeforeFirstUserAndOutOfLoops();
35 }
36 }
37
Mark Mendell94991072015-10-06 14:58:32 -040038 private:
39 void VisitAdd(HAdd* add) OVERRIDE {
40 BinaryFP(add);
41 }
42
43 void VisitSub(HSub* sub) OVERRIDE {
44 BinaryFP(sub);
45 }
46
47 void VisitMul(HMul* mul) OVERRIDE {
48 BinaryFP(mul);
49 }
50
51 void VisitDiv(HDiv* div) OVERRIDE {
52 BinaryFP(div);
53 }
54
55 void VisitReturn(HReturn* ret) OVERRIDE {
56 HConstant* value = ret->InputAt(0)->AsConstant();
57 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
58 ReplaceInput(ret, value, 0, true);
59 }
60 }
61
62 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
63 HandleInvoke(invoke);
64 }
65
66 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
67 HandleInvoke(invoke);
68 }
69
70 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
71 HandleInvoke(invoke);
72 }
73
74 void BinaryFP(HBinaryOperation* bin) {
75 HConstant* rhs = bin->InputAt(1)->AsConstant();
76 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
77 ReplaceInput(bin, rhs, 1, false);
78 }
79 }
80
81 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
82 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
83 // address the constant area.
Vladimir Markofb337ea2015-11-25 15:25:10 +000084 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -040085 HGraph* graph = GetGraph();
86 HBasicBlock* block = switch_insn->GetBlock();
87 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
88 switch_insn->GetStartValue(),
89 switch_insn->GetNumEntries(),
90 switch_insn->InputAt(0),
91 base_,
92 switch_insn->GetDexPc());
93 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
94 }
95
Vladimir Markofb337ea2015-11-25 15:25:10 +000096 void InitializePCRelativeBasePointer() {
Mark Mendell94991072015-10-06 14:58:32 -040097 // Ensure we only initialize the pointer once.
98 if (base_ != nullptr) {
99 return;
100 }
101
Vladimir Markofb337ea2015-11-25 15:25:10 +0000102 // Insert the base at the start of the entry block, move it to a better
103 // position later in MoveBaseIfNeeded().
104 base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress();
105 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
106 entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction());
Mark Mendell94991072015-10-06 14:58:32 -0400107 DCHECK(base_ != nullptr);
108 }
109
110 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000111 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400112 HX86LoadFromConstantTable* load_constant =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000113 new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
114 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400115 insn->ReplaceInput(load_constant, input_index);
116 }
117
118 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000119 // If this is an invoke-static/-direct with PC-relative dex cache array
120 // addressing, we need the PC-relative address base.
121 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
122 if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasPcRelativeDexCache()) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000123 InitializePCRelativeBasePointer();
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000124 // Add the extra parameter base_.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000125 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000126 invoke_static_or_direct->AddSpecialInput(base_);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000127 }
Mark Mendell94991072015-10-06 14:58:32 -0400128 // Ensure that we can load FP arguments from the constant area.
129 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
130 HConstant* input = invoke->InputAt(i)->AsConstant();
131 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
132 ReplaceInput(invoke, input, i, true);
133 }
134 }
135 }
136
137 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
138 // input to the HX86LoadFromConstantTable instructions.
139 HX86ComputeBaseMethodAddress* base_;
140};
141
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000142void PcRelativeFixups::Run() {
143 PCRelativeHandlerVisitor visitor(graph_);
Mark Mendell94991072015-10-06 14:58:32 -0400144 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000145 visitor.MoveBaseIfNeeded();
Mark Mendell94991072015-10-06 14:58:32 -0400146}
147
148} // namespace x86
149} // namespace art