blob: 1394dfaf5d1a9f06079b854ae61be3ff5db21000 [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"
Vladimir Markof3e0ee22015-12-17 15:23:13 +000018#include "code_generator_x86.h"
Mark Mendell94991072015-10-06 14:58:32 -040019
20namespace art {
21namespace x86 {
22
23/**
24 * Finds instructions that need the constant area base as an input.
25 */
Vladimir Marko0f7dca42015-11-02 14:36:43 +000026class PCRelativeHandlerVisitor : public HGraphVisitor {
Mark Mendell94991072015-10-06 14:58:32 -040027 public:
Vladimir Marko0f7dca42015-11-02 14:36:43 +000028 explicit PCRelativeHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
Mark Mendell94991072015-10-06 14:58:32 -040029
Vladimir Markofb337ea2015-11-25 15:25:10 +000030 void MoveBaseIfNeeded() {
31 if (base_ != nullptr) {
32 // Bring the base closer to the first use (previously, it was in the
33 // entry block) and relieve some pressure on the register allocator
34 // while avoiding recalculation of the base in a loop.
35 base_->MoveBeforeFirstUserAndOutOfLoops();
36 }
37 }
38
Mark Mendell94991072015-10-06 14:58:32 -040039 private:
40 void VisitAdd(HAdd* add) OVERRIDE {
41 BinaryFP(add);
42 }
43
44 void VisitSub(HSub* sub) OVERRIDE {
45 BinaryFP(sub);
46 }
47
48 void VisitMul(HMul* mul) OVERRIDE {
49 BinaryFP(mul);
50 }
51
52 void VisitDiv(HDiv* div) OVERRIDE {
53 BinaryFP(div);
54 }
55
56 void VisitReturn(HReturn* ret) OVERRIDE {
57 HConstant* value = ret->InputAt(0)->AsConstant();
58 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
59 ReplaceInput(ret, value, 0, true);
60 }
61 }
62
63 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
64 HandleInvoke(invoke);
65 }
66
67 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
68 HandleInvoke(invoke);
69 }
70
71 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
72 HandleInvoke(invoke);
73 }
74
75 void BinaryFP(HBinaryOperation* bin) {
76 HConstant* rhs = bin->InputAt(1)->AsConstant();
77 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
78 ReplaceInput(bin, rhs, 1, false);
79 }
80 }
81
82 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
Vladimir Markof3e0ee22015-12-17 15:23:13 +000083 if (switch_insn->GetNumEntries() <=
84 InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
85 return;
86 }
Mark Mendell94991072015-10-06 14:58:32 -040087 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
88 // address the constant area.
Vladimir Markofb337ea2015-11-25 15:25:10 +000089 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -040090 HGraph* graph = GetGraph();
91 HBasicBlock* block = switch_insn->GetBlock();
92 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
93 switch_insn->GetStartValue(),
94 switch_insn->GetNumEntries(),
95 switch_insn->InputAt(0),
96 base_,
97 switch_insn->GetDexPc());
98 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
99 }
100
Vladimir Markofb337ea2015-11-25 15:25:10 +0000101 void InitializePCRelativeBasePointer() {
Mark Mendell94991072015-10-06 14:58:32 -0400102 // Ensure we only initialize the pointer once.
103 if (base_ != nullptr) {
104 return;
105 }
106
Vladimir Markofb337ea2015-11-25 15:25:10 +0000107 // Insert the base at the start of the entry block, move it to a better
108 // position later in MoveBaseIfNeeded().
109 base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress();
110 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
111 entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction());
Mark Mendell94991072015-10-06 14:58:32 -0400112 DCHECK(base_ != nullptr);
113 }
114
115 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000116 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400117 HX86LoadFromConstantTable* load_constant =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000118 new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
119 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400120 insn->ReplaceInput(load_constant, input_index);
121 }
122
123 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000124 // If this is an invoke-static/-direct with PC-relative dex cache array
125 // addressing, we need the PC-relative address base.
126 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
127 if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasPcRelativeDexCache()) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000128 InitializePCRelativeBasePointer();
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000129 // Add the extra parameter base_.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000130 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000131 invoke_static_or_direct->AddSpecialInput(base_);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000132 }
Mark Mendell94991072015-10-06 14:58:32 -0400133 // Ensure that we can load FP arguments from the constant area.
134 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
135 HConstant* input = invoke->InputAt(i)->AsConstant();
136 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
137 ReplaceInput(invoke, input, i, true);
138 }
139 }
140 }
141
142 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
143 // input to the HX86LoadFromConstantTable instructions.
144 HX86ComputeBaseMethodAddress* base_;
145};
146
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000147void PcRelativeFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000148 if (graph_->HasIrreducibleLoops()) {
149 // Do not run this optimization, as irreducible loops do not work with an instruction
150 // that can be live-in at the irreducible loop header.
151 return;
152 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000153 PCRelativeHandlerVisitor visitor(graph_);
Mark Mendell94991072015-10-06 14:58:32 -0400154 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000155 visitor.MoveBaseIfNeeded();
Mark Mendell94991072015-10-06 14:58:32 -0400156}
157
158} // namespace x86
159} // namespace art