blob: 808a1dc6c2ae82cf9de42e96d6845c6fd967aff3 [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
29 private:
30 void VisitAdd(HAdd* add) OVERRIDE {
31 BinaryFP(add);
32 }
33
34 void VisitSub(HSub* sub) OVERRIDE {
35 BinaryFP(sub);
36 }
37
38 void VisitMul(HMul* mul) OVERRIDE {
39 BinaryFP(mul);
40 }
41
42 void VisitDiv(HDiv* div) OVERRIDE {
43 BinaryFP(div);
44 }
45
46 void VisitReturn(HReturn* ret) OVERRIDE {
47 HConstant* value = ret->InputAt(0)->AsConstant();
48 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
49 ReplaceInput(ret, value, 0, true);
50 }
51 }
52
53 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
54 HandleInvoke(invoke);
55 }
56
57 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
58 HandleInvoke(invoke);
59 }
60
61 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
62 HandleInvoke(invoke);
63 }
64
65 void BinaryFP(HBinaryOperation* bin) {
66 HConstant* rhs = bin->InputAt(1)->AsConstant();
67 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
68 ReplaceInput(bin, rhs, 1, false);
69 }
70 }
71
72 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
73 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
74 // address the constant area.
Vladimir Marko0f7dca42015-11-02 14:36:43 +000075 InitializePCRelativeBasePointer(switch_insn);
Mark Mendell94991072015-10-06 14:58:32 -040076 HGraph* graph = GetGraph();
77 HBasicBlock* block = switch_insn->GetBlock();
78 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
79 switch_insn->GetStartValue(),
80 switch_insn->GetNumEntries(),
81 switch_insn->InputAt(0),
82 base_,
83 switch_insn->GetDexPc());
84 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
85 }
86
Vladimir Marko0f7dca42015-11-02 14:36:43 +000087 void InitializePCRelativeBasePointer(HInstruction* user) {
Mark Mendell94991072015-10-06 14:58:32 -040088 // Ensure we only initialize the pointer once.
89 if (base_ != nullptr) {
90 return;
91 }
92
93 HGraph* graph = GetGraph();
94 HBasicBlock* entry = graph->GetEntryBlock();
95 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
96 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
97 entry->InsertInstructionBefore(base_, insert_pos);
98 DCHECK(base_ != nullptr);
99 }
100
101 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000102 InitializePCRelativeBasePointer(insn);
Mark Mendell94991072015-10-06 14:58:32 -0400103 HX86LoadFromConstantTable* load_constant =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000104 new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
105 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400106 insn->ReplaceInput(load_constant, input_index);
107 }
108
109 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000110 // If this is an invoke-static/-direct with PC-relative dex cache array
111 // addressing, we need the PC-relative address base.
112 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
113 if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasPcRelativeDexCache()) {
114 InitializePCRelativeBasePointer(invoke);
115 // Add the extra parameter base_.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000116 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000117 invoke_static_or_direct->AddSpecialInput(base_);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000118 }
Mark Mendell94991072015-10-06 14:58:32 -0400119 // Ensure that we can load FP arguments from the constant area.
120 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
121 HConstant* input = invoke->InputAt(i)->AsConstant();
122 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
123 ReplaceInput(invoke, input, i, true);
124 }
125 }
126 }
127
128 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
129 // input to the HX86LoadFromConstantTable instructions.
130 HX86ComputeBaseMethodAddress* base_;
131};
132
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000133void PcRelativeFixups::Run() {
134 PCRelativeHandlerVisitor visitor(graph_);
Mark Mendell94991072015-10-06 14:58:32 -0400135 visitor.VisitInsertionOrder();
136}
137
138} // namespace x86
139} // namespace art