blob: 477a91a7343646d0324a8317f0bf04b176b29734 [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"
Aart Bikd1c40452016-03-02 16:06:13 -080019#include "intrinsics_x86.h"
Mark Mendell94991072015-10-06 14:58:32 -040020
21namespace art {
22namespace x86 {
23
24/**
25 * Finds instructions that need the constant area base as an input.
26 */
Vladimir Marko0f7dca42015-11-02 14:36:43 +000027class PCRelativeHandlerVisitor : public HGraphVisitor {
Mark Mendell94991072015-10-06 14:58:32 -040028 public:
Aart Bikd1c40452016-03-02 16:06:13 -080029 PCRelativeHandlerVisitor(HGraph* graph, CodeGenerator* codegen)
30 : HGraphVisitor(graph),
31 codegen_(down_cast<CodeGeneratorX86*>(codegen)),
32 base_(nullptr) {}
Mark Mendell94991072015-10-06 14:58:32 -040033
Vladimir Markofb337ea2015-11-25 15:25:10 +000034 void MoveBaseIfNeeded() {
35 if (base_ != nullptr) {
36 // Bring the base closer to the first use (previously, it was in the
37 // entry block) and relieve some pressure on the register allocator
38 // while avoiding recalculation of the base in a loop.
39 base_->MoveBeforeFirstUserAndOutOfLoops();
40 }
41 }
42
Mark Mendell94991072015-10-06 14:58:32 -040043 private:
44 void VisitAdd(HAdd* add) OVERRIDE {
45 BinaryFP(add);
46 }
47
48 void VisitSub(HSub* sub) OVERRIDE {
49 BinaryFP(sub);
50 }
51
52 void VisitMul(HMul* mul) OVERRIDE {
53 BinaryFP(mul);
54 }
55
56 void VisitDiv(HDiv* div) OVERRIDE {
57 BinaryFP(div);
58 }
59
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000060 void VisitCompare(HCompare* compare) OVERRIDE {
61 BinaryFP(compare);
62 }
63
Mark Mendell94991072015-10-06 14:58:32 -040064 void VisitReturn(HReturn* ret) OVERRIDE {
65 HConstant* value = ret->InputAt(0)->AsConstant();
66 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
67 ReplaceInput(ret, value, 0, true);
68 }
69 }
70
71 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
72 HandleInvoke(invoke);
73 }
74
75 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
76 HandleInvoke(invoke);
77 }
78
79 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
80 HandleInvoke(invoke);
81 }
82
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010083 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
84 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
Vladimir Marko48886c22017-01-06 11:45:47 +000085 if (load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010086 InitializePCRelativeBasePointer();
87 load_class->AddSpecialInput(base_);
88 }
89 }
90
Vladimir Markocac5a7e2016-02-22 10:39:50 +000091 void VisitLoadString(HLoadString* load_string) OVERRIDE {
92 HLoadString::LoadKind load_kind = load_string->GetLoadKind();
93 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +000094 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +000095 InitializePCRelativeBasePointer();
96 load_string->AddSpecialInput(base_);
97 }
98 }
99
Mark Mendell94991072015-10-06 14:58:32 -0400100 void BinaryFP(HBinaryOperation* bin) {
101 HConstant* rhs = bin->InputAt(1)->AsConstant();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000102 if (rhs != nullptr && Primitive::IsFloatingPointType(rhs->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400103 ReplaceInput(bin, rhs, 1, false);
104 }
105 }
106
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000107 void VisitEqual(HEqual* cond) OVERRIDE {
108 BinaryFP(cond);
109 }
110
111 void VisitNotEqual(HNotEqual* cond) OVERRIDE {
112 BinaryFP(cond);
113 }
114
115 void VisitLessThan(HLessThan* cond) OVERRIDE {
116 BinaryFP(cond);
117 }
118
119 void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE {
120 BinaryFP(cond);
121 }
122
123 void VisitGreaterThan(HGreaterThan* cond) OVERRIDE {
124 BinaryFP(cond);
125 }
126
127 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE {
128 BinaryFP(cond);
129 }
130
131 void VisitNeg(HNeg* neg) OVERRIDE {
132 if (Primitive::IsFloatingPointType(neg->GetType())) {
133 // We need to replace the HNeg with a HX86FPNeg in order to address the constant area.
134 InitializePCRelativeBasePointer();
135 HGraph* graph = GetGraph();
136 HBasicBlock* block = neg->GetBlock();
137 HX86FPNeg* x86_fp_neg = new (graph->GetArena()) HX86FPNeg(
138 neg->GetType(),
139 neg->InputAt(0),
140 base_,
141 neg->GetDexPc());
142 block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg);
143 }
144 }
145
Mark Mendell94991072015-10-06 14:58:32 -0400146 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000147 if (switch_insn->GetNumEntries() <=
148 InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
149 return;
150 }
Mark Mendell94991072015-10-06 14:58:32 -0400151 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
152 // address the constant area.
Vladimir Markofb337ea2015-11-25 15:25:10 +0000153 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400154 HGraph* graph = GetGraph();
155 HBasicBlock* block = switch_insn->GetBlock();
156 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
157 switch_insn->GetStartValue(),
158 switch_insn->GetNumEntries(),
159 switch_insn->InputAt(0),
160 base_,
161 switch_insn->GetDexPc());
162 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
163 }
164
Vladimir Markofb337ea2015-11-25 15:25:10 +0000165 void InitializePCRelativeBasePointer() {
Mark Mendell94991072015-10-06 14:58:32 -0400166 // Ensure we only initialize the pointer once.
167 if (base_ != nullptr) {
168 return;
169 }
Vladimir Markofb337ea2015-11-25 15:25:10 +0000170 // Insert the base at the start of the entry block, move it to a better
171 // position later in MoveBaseIfNeeded().
172 base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress();
173 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
174 entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction());
Mark Mendell94991072015-10-06 14:58:32 -0400175 DCHECK(base_ != nullptr);
176 }
177
178 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000179 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400180 HX86LoadFromConstantTable* load_constant =
David Brazdilb3e773e2016-01-26 11:28:37 +0000181 new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value);
182 if (!materialize) {
183 load_constant->MarkEmittedAtUseSite();
184 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000185 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400186 insn->ReplaceInput(load_constant, input_index);
187 }
188
189 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000190 // If this is an invoke-static/-direct with PC-relative dex cache array
191 // addressing, we need the PC-relative address base.
192 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000193 // We can't add a pointer to the constant area if we already have a current
194 // method pointer. This may arise when sharpening doesn't remove the current
195 // method pointer from the invoke.
196 if (invoke_static_or_direct != nullptr &&
197 invoke_static_or_direct->HasCurrentMethodInput()) {
198 DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache());
199 return;
200 }
201
202 bool base_added = false;
Aart Bikd1c40452016-03-02 16:06:13 -0800203 if (invoke_static_or_direct != nullptr &&
204 invoke_static_or_direct->HasPcRelativeDexCache() &&
Vladimir Marko68c981f2016-08-26 13:13:33 +0100205 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000206 InitializePCRelativeBasePointer();
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000207 // Add the extra parameter base_.
Vladimir Markoc53c0792015-11-19 15:48:33 +0000208 invoke_static_or_direct->AddSpecialInput(base_);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000209 base_added = true;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000210 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000211
Mark Mendell94991072015-10-06 14:58:32 -0400212 // Ensure that we can load FP arguments from the constant area.
Vladimir Markoe9004912016-06-16 16:50:52 +0100213 HInputsRef inputs = invoke->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100214 for (size_t i = 0; i < inputs.size(); i++) {
215 HConstant* input = inputs[i]->AsConstant();
Mark Mendell94991072015-10-06 14:58:32 -0400216 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
217 ReplaceInput(invoke, input, i, true);
218 }
219 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000220
221 // These intrinsics need the constant area.
222 switch (invoke->GetIntrinsic()) {
223 case Intrinsics::kMathAbsDouble:
224 case Intrinsics::kMathAbsFloat:
225 case Intrinsics::kMathMaxDoubleDouble:
226 case Intrinsics::kMathMaxFloatFloat:
227 case Intrinsics::kMathMinDoubleDouble:
228 case Intrinsics::kMathMinFloatFloat:
Aart Bik2c9f4952016-08-01 16:52:27 -0700229 case Intrinsics::kMathRoundFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000230 if (!base_added) {
231 DCHECK(invoke_static_or_direct != nullptr);
232 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
233 InitializePCRelativeBasePointer();
234 invoke_static_or_direct->AddSpecialInput(base_);
235 }
236 break;
237 default:
238 break;
239 }
Mark Mendell94991072015-10-06 14:58:32 -0400240 }
241
Aart Bikd1c40452016-03-02 16:06:13 -0800242 CodeGeneratorX86* codegen_;
243
Mark Mendell94991072015-10-06 14:58:32 -0400244 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
245 // input to the HX86LoadFromConstantTable instructions.
246 HX86ComputeBaseMethodAddress* base_;
247};
248
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000249void PcRelativeFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000250 if (graph_->HasIrreducibleLoops()) {
251 // Do not run this optimization, as irreducible loops do not work with an instruction
252 // that can be live-in at the irreducible loop header.
253 return;
254 }
Aart Bikd1c40452016-03-02 16:06:13 -0800255 PCRelativeHandlerVisitor visitor(graph_, codegen_);
Mark Mendell94991072015-10-06 14:58:32 -0400256 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000257 visitor.MoveBaseIfNeeded();
Mark Mendell94991072015-10-06 14:58:32 -0400258}
259
260} // namespace x86
261} // namespace art