blob: 41f2f776fca22b6377b620f77c209372b06b8168 [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"
Vladimir Marko8e524ad2018-07-13 10:27:43 +010020#include "runtime.h"
Mark Mendell94991072015-10-06 14:58:32 -040021
22namespace art {
23namespace x86 {
24
25/**
26 * Finds instructions that need the constant area base as an input.
27 */
Vladimir Marko0f7dca42015-11-02 14:36:43 +000028class PCRelativeHandlerVisitor : public HGraphVisitor {
Mark Mendell94991072015-10-06 14:58:32 -040029 public:
Aart Bikd1c40452016-03-02 16:06:13 -080030 PCRelativeHandlerVisitor(HGraph* graph, CodeGenerator* codegen)
31 : HGraphVisitor(graph),
32 codegen_(down_cast<CodeGeneratorX86*>(codegen)),
33 base_(nullptr) {}
Mark Mendell94991072015-10-06 14:58:32 -040034
Vladimir Markofb337ea2015-11-25 15:25:10 +000035 void MoveBaseIfNeeded() {
36 if (base_ != nullptr) {
37 // Bring the base closer to the first use (previously, it was in the
38 // entry block) and relieve some pressure on the register allocator
39 // while avoiding recalculation of the base in a loop.
40 base_->MoveBeforeFirstUserAndOutOfLoops();
41 }
42 }
43
Mark Mendell94991072015-10-06 14:58:32 -040044 private:
45 void VisitAdd(HAdd* add) OVERRIDE {
46 BinaryFP(add);
47 }
48
49 void VisitSub(HSub* sub) OVERRIDE {
50 BinaryFP(sub);
51 }
52
53 void VisitMul(HMul* mul) OVERRIDE {
54 BinaryFP(mul);
55 }
56
57 void VisitDiv(HDiv* div) OVERRIDE {
58 BinaryFP(div);
59 }
60
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000061 void VisitCompare(HCompare* compare) OVERRIDE {
62 BinaryFP(compare);
63 }
64
Mark Mendell94991072015-10-06 14:58:32 -040065 void VisitReturn(HReturn* ret) OVERRIDE {
66 HConstant* value = ret->InputAt(0)->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 if ((value != nullptr && DataType::IsFloatingPointType(value->GetType()))) {
Mark Mendell94991072015-10-06 14:58:32 -040068 ReplaceInput(ret, value, 0, true);
69 }
70 }
71
72 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
73 HandleInvoke(invoke);
74 }
75
76 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
77 HandleInvoke(invoke);
78 }
79
80 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
81 HandleInvoke(invoke);
82 }
83
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010084 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markoe47f60c2018-02-21 13:43:28 +000085 if (load_class->HasPcRelativeLoadKind()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +000086 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_class);
87 load_class->AddSpecialInput(method_address);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010088 }
89 }
90
Vladimir Markocac5a7e2016-02-22 10:39:50 +000091 void VisitLoadString(HLoadString* load_string) OVERRIDE {
Vladimir Markoe47f60c2018-02-21 13:43:28 +000092 if (load_string->HasPcRelativeLoadKind()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +000093 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_string);
94 load_string->AddSpecialInput(method_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000095 }
96 }
97
Mark Mendell94991072015-10-06 14:58:32 -040098 void BinaryFP(HBinaryOperation* bin) {
99 HConstant* rhs = bin->InputAt(1)->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100100 if (rhs != nullptr && DataType::IsFloatingPointType(rhs->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400101 ReplaceInput(bin, rhs, 1, false);
102 }
103 }
104
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000105 void VisitEqual(HEqual* cond) OVERRIDE {
106 BinaryFP(cond);
107 }
108
109 void VisitNotEqual(HNotEqual* cond) OVERRIDE {
110 BinaryFP(cond);
111 }
112
113 void VisitLessThan(HLessThan* cond) OVERRIDE {
114 BinaryFP(cond);
115 }
116
117 void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE {
118 BinaryFP(cond);
119 }
120
121 void VisitGreaterThan(HGreaterThan* cond) OVERRIDE {
122 BinaryFP(cond);
123 }
124
125 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE {
126 BinaryFP(cond);
127 }
128
129 void VisitNeg(HNeg* neg) OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 if (DataType::IsFloatingPointType(neg->GetType())) {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000131 // We need to replace the HNeg with a HX86FPNeg in order to address the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000132 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(neg);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000133 HGraph* graph = GetGraph();
134 HBasicBlock* block = neg->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100135 HX86FPNeg* x86_fp_neg = new (graph->GetAllocator()) HX86FPNeg(
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000136 neg->GetType(),
137 neg->InputAt(0),
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000138 method_address,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000139 neg->GetDexPc());
140 block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg);
141 }
142 }
143
Mark Mendell94991072015-10-06 14:58:32 -0400144 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000145 if (switch_insn->GetNumEntries() <=
146 InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
147 return;
148 }
Mark Mendell94991072015-10-06 14:58:32 -0400149 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
150 // address the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000151 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(switch_insn);
Mark Mendell94991072015-10-06 14:58:32 -0400152 HGraph* graph = GetGraph();
153 HBasicBlock* block = switch_insn->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 HX86PackedSwitch* x86_switch = new (graph->GetAllocator()) HX86PackedSwitch(
Mark Mendell94991072015-10-06 14:58:32 -0400155 switch_insn->GetStartValue(),
156 switch_insn->GetNumEntries(),
157 switch_insn->InputAt(0),
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000158 method_address,
Mark Mendell94991072015-10-06 14:58:32 -0400159 switch_insn->GetDexPc());
160 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
161 }
162
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000163 HX86ComputeBaseMethodAddress* GetPCRelativeBasePointer(HInstruction* cursor) {
164 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
165 if (!has_irreducible_loops) {
166 // Ensure we only initialize the pointer once.
167 if (base_ != nullptr) {
168 return base_;
169 }
Mark Mendell94991072015-10-06 14:58:32 -0400170 }
Vladimir Markofb337ea2015-11-25 15:25:10 +0000171 // Insert the base at the start of the entry block, move it to a better
172 // position later in MoveBaseIfNeeded().
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000173 HX86ComputeBaseMethodAddress* method_address =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100174 new (GetGraph()->GetAllocator()) HX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000175 if (has_irreducible_loops) {
176 cursor->GetBlock()->InsertInstructionBefore(method_address, cursor);
177 } else {
178 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
179 entry_block->InsertInstructionBefore(method_address, entry_block->GetFirstInstruction());
180 base_ = method_address;
181 }
182 return method_address;
Mark Mendell94991072015-10-06 14:58:32 -0400183 }
184
185 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000186 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(insn);
Mark Mendell94991072015-10-06 14:58:32 -0400187 HX86LoadFromConstantTable* load_constant =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100188 new (GetGraph()->GetAllocator()) HX86LoadFromConstantTable(method_address, value);
David Brazdilb3e773e2016-01-26 11:28:37 +0000189 if (!materialize) {
190 load_constant->MarkEmittedAtUseSite();
191 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000192 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400193 insn->ReplaceInput(load_constant, input_index);
194 }
195
196 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000197 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
Vladimir Markoeebb8212018-06-05 14:57:24 +0100198
199 // We can't add the method address if we already have a current method pointer.
200 // This may arise when sharpening doesn't remove the current method pointer from the invoke.
201 if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasCurrentMethodInput()) {
202 // Note: This happens only for recursive calls (including compiling an intrinsic
203 // by faking a call to itself; we use kRuntimeCall for this case).
Vladimir Marko65979462017-05-19 17:25:12 +0100204 DCHECK(!invoke_static_or_direct->HasPcRelativeMethodLoadKind());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000205 return;
206 }
207
Vladimir Markoeebb8212018-06-05 14:57:24 +0100208 // If this is an invoke-static/-direct with PC-relative addressing (within boot image
209 // or using .bss or .data.bimg.rel.ro), we need the PC-relative address base.
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000210 bool base_added = false;
Aart Bikd1c40452016-03-02 16:06:13 -0800211 if (invoke_static_or_direct != nullptr &&
Vladimir Marko65979462017-05-19 17:25:12 +0100212 invoke_static_or_direct->HasPcRelativeMethodLoadKind() &&
Vladimir Marko68c981f2016-08-26 13:13:33 +0100213 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000214 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
215 // Add the extra parameter.
216 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000217 base_added = true;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000218 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000219
Mark Mendell94991072015-10-06 14:58:32 -0400220 // Ensure that we can load FP arguments from the constant area.
Vladimir Markoe9004912016-06-16 16:50:52 +0100221 HInputsRef inputs = invoke->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100222 for (size_t i = 0; i < inputs.size(); i++) {
223 HConstant* input = inputs[i]->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100224 if (input != nullptr && DataType::IsFloatingPointType(input->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400225 ReplaceInput(invoke, input, i, true);
226 }
227 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000228
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000229 switch (invoke->GetIntrinsic()) {
230 case Intrinsics::kMathAbsDouble:
231 case Intrinsics::kMathAbsFloat:
232 case Intrinsics::kMathMaxDoubleDouble:
233 case Intrinsics::kMathMaxFloatFloat:
234 case Intrinsics::kMathMinDoubleDouble:
235 case Intrinsics::kMathMinFloatFloat:
Aart Bik1f8d51b2018-02-15 10:42:37 -0800236 LOG(FATAL) << "Unreachable min/max/abs: intrinsics should have been lowered "
237 "to IR nodes by instruction simplifier";
238 UNREACHABLE();
Vladimir Markoeebb8212018-06-05 14:57:24 +0100239 case Intrinsics::kIntegerValueOf:
240 // This intrinsic can be call free if it loads the address of the boot image object.
241 // If we're compiling PIC, we need the address base for loading from .data.bimg.rel.ro.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100242 if (Runtime::Current()->UseJitCompilation()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +0100243 break;
244 }
245 FALLTHROUGH_INTENDED;
Aart Bik2c9f4952016-08-01 16:52:27 -0700246 case Intrinsics::kMathRoundFloat:
Vladimir Markoeebb8212018-06-05 14:57:24 +0100247 // This intrinsic needs the constant area.
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000248 if (!base_added) {
249 DCHECK(invoke_static_or_direct != nullptr);
250 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000251 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
252 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000253 }
254 break;
255 default:
256 break;
257 }
Mark Mendell94991072015-10-06 14:58:32 -0400258 }
259
Aart Bikd1c40452016-03-02 16:06:13 -0800260 CodeGeneratorX86* codegen_;
261
Mark Mendell94991072015-10-06 14:58:32 -0400262 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000263 // input to the HX86LoadFromConstantTable instructions. Only set for
264 // graphs with reducible loops.
Mark Mendell94991072015-10-06 14:58:32 -0400265 HX86ComputeBaseMethodAddress* base_;
266};
267
Aart Bik24773202018-04-26 10:28:51 -0700268bool PcRelativeFixups::Run() {
Aart Bikd1c40452016-03-02 16:06:13 -0800269 PCRelativeHandlerVisitor visitor(graph_, codegen_);
Mark Mendell94991072015-10-06 14:58:32 -0400270 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000271 visitor.MoveBaseIfNeeded();
Aart Bik24773202018-04-26 10:28:51 -0700272 return true;
Mark Mendell94991072015-10-06 14:58:32 -0400273}
274
275} // namespace x86
276} // namespace art