blob: 17f37f05c5845b0d0f9623d1b5ecdd246492a1b7 [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
Vladimir Marko0a516052019-10-14 13:00:44 +000021namespace art {
Mark Mendell94991072015-10-06 14:58:32 -040022namespace 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:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010044 void VisitAdd(HAdd* add) override {
Mark Mendell94991072015-10-06 14:58:32 -040045 BinaryFP(add);
46 }
47
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010048 void VisitSub(HSub* sub) override {
Mark Mendell94991072015-10-06 14:58:32 -040049 BinaryFP(sub);
50 }
51
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010052 void VisitMul(HMul* mul) override {
Mark Mendell94991072015-10-06 14:58:32 -040053 BinaryFP(mul);
54 }
55
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010056 void VisitDiv(HDiv* div) override {
Mark Mendell94991072015-10-06 14:58:32 -040057 BinaryFP(div);
58 }
59
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010060 void VisitCompare(HCompare* compare) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000061 BinaryFP(compare);
62 }
63
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010064 void VisitReturn(HReturn* ret) override {
Mark Mendell94991072015-10-06 14:58:32 -040065 HConstant* value = ret->InputAt(0)->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 if ((value != nullptr && DataType::IsFloatingPointType(value->GetType()))) {
Mark Mendell94991072015-10-06 14:58:32 -040067 ReplaceInput(ret, value, 0, true);
68 }
69 }
70
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010071 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) override {
Mark Mendell94991072015-10-06 14:58:32 -040072 HandleInvoke(invoke);
73 }
74
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010075 void VisitInvokeVirtual(HInvokeVirtual* invoke) override {
Mark Mendell94991072015-10-06 14:58:32 -040076 HandleInvoke(invoke);
77 }
78
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010079 void VisitInvokeInterface(HInvokeInterface* invoke) override {
Mark Mendell94991072015-10-06 14:58:32 -040080 HandleInvoke(invoke);
81 }
82
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010083 void VisitLoadClass(HLoadClass* load_class) override {
Vladimir Markoe47f60c2018-02-21 13:43:28 +000084 if (load_class->HasPcRelativeLoadKind()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +000085 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_class);
86 load_class->AddSpecialInput(method_address);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010087 }
88 }
89
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010090 void VisitLoadString(HLoadString* load_string) override {
Vladimir Markoe47f60c2018-02-21 13:43:28 +000091 if (load_string->HasPcRelativeLoadKind()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +000092 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_string);
93 load_string->AddSpecialInput(method_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000094 }
95 }
96
Mark Mendell94991072015-10-06 14:58:32 -040097 void BinaryFP(HBinaryOperation* bin) {
98 HConstant* rhs = bin->InputAt(1)->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010099 if (rhs != nullptr && DataType::IsFloatingPointType(rhs->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400100 ReplaceInput(bin, rhs, 1, false);
101 }
102 }
103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void VisitEqual(HEqual* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000105 BinaryFP(cond);
106 }
107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100108 void VisitNotEqual(HNotEqual* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000109 BinaryFP(cond);
110 }
111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 void VisitLessThan(HLessThan* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000113 BinaryFP(cond);
114 }
115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 void VisitLessThanOrEqual(HLessThanOrEqual* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000117 BinaryFP(cond);
118 }
119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100120 void VisitGreaterThan(HGreaterThan* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000121 BinaryFP(cond);
122 }
123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100124 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) override {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000125 BinaryFP(cond);
126 }
127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100128 void VisitNeg(HNeg* neg) override {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 if (DataType::IsFloatingPointType(neg->GetType())) {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000130 // We need to replace the HNeg with a HX86FPNeg in order to address the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000131 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(neg);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000132 HGraph* graph = GetGraph();
133 HBasicBlock* block = neg->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100134 HX86FPNeg* x86_fp_neg = new (graph->GetAllocator()) HX86FPNeg(
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000135 neg->GetType(),
136 neg->InputAt(0),
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000137 method_address,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000138 neg->GetDexPc());
139 block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg);
140 }
141 }
142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100143 void VisitPackedSwitch(HPackedSwitch* switch_insn) override {
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000144 if (switch_insn->GetNumEntries() <=
145 InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
146 return;
147 }
Mark Mendell94991072015-10-06 14:58:32 -0400148 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
149 // address the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000150 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(switch_insn);
Mark Mendell94991072015-10-06 14:58:32 -0400151 HGraph* graph = GetGraph();
152 HBasicBlock* block = switch_insn->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100153 HX86PackedSwitch* x86_switch = new (graph->GetAllocator()) HX86PackedSwitch(
Mark Mendell94991072015-10-06 14:58:32 -0400154 switch_insn->GetStartValue(),
155 switch_insn->GetNumEntries(),
156 switch_insn->InputAt(0),
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000157 method_address,
Mark Mendell94991072015-10-06 14:58:32 -0400158 switch_insn->GetDexPc());
159 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
160 }
161
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000162 HX86ComputeBaseMethodAddress* GetPCRelativeBasePointer(HInstruction* cursor) {
163 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
164 if (!has_irreducible_loops) {
165 // Ensure we only initialize the pointer once.
166 if (base_ != nullptr) {
167 return base_;
168 }
Mark Mendell94991072015-10-06 14:58:32 -0400169 }
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().
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000172 HX86ComputeBaseMethodAddress* method_address =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100173 new (GetGraph()->GetAllocator()) HX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000174 if (has_irreducible_loops) {
175 cursor->GetBlock()->InsertInstructionBefore(method_address, cursor);
176 } else {
177 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
178 entry_block->InsertInstructionBefore(method_address, entry_block->GetFirstInstruction());
179 base_ = method_address;
180 }
181 return method_address;
Mark Mendell94991072015-10-06 14:58:32 -0400182 }
183
184 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000185 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(insn);
Mark Mendell94991072015-10-06 14:58:32 -0400186 HX86LoadFromConstantTable* load_constant =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100187 new (GetGraph()->GetAllocator()) HX86LoadFromConstantTable(method_address, value);
David Brazdilb3e773e2016-01-26 11:28:37 +0000188 if (!materialize) {
189 load_constant->MarkEmittedAtUseSite();
190 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000191 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400192 insn->ReplaceInput(load_constant, input_index);
193 }
194
195 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000196 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
Vladimir Markoeebb8212018-06-05 14:57:24 +0100197
Vladimir Markoeebb8212018-06-05 14:57:24 +0100198 // If this is an invoke-static/-direct with PC-relative addressing (within boot image
199 // or using .bss or .data.bimg.rel.ro), we need the PC-relative address base.
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000200 bool base_added = false;
Aart Bikd1c40452016-03-02 16:06:13 -0800201 if (invoke_static_or_direct != nullptr &&
Vladimir Marko65979462017-05-19 17:25:12 +0100202 invoke_static_or_direct->HasPcRelativeMethodLoadKind() &&
Vladimir Marko68c981f2016-08-26 13:13:33 +0100203 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000204 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
205 // Add the extra parameter.
206 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000207 base_added = true;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000208 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000209
Nicolas Geoffray8d34a182020-09-16 09:46:58 +0100210 HInvokeInterface* invoke_interface = invoke->AsInvokeInterface();
211 if (invoke_interface != nullptr &&
212 IsPcRelativeMethodLoadKind(invoke_interface->GetHiddenArgumentLoadKind())) {
213 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
214 // Add the extra parameter.
215 invoke_interface->AddSpecialInput(method_address);
216 base_added = true;
217 }
218
Mark Mendell94991072015-10-06 14:58:32 -0400219 // Ensure that we can load FP arguments from the constant area.
Vladimir Markoe9004912016-06-16 16:50:52 +0100220 HInputsRef inputs = invoke->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100221 for (size_t i = 0; i < inputs.size(); i++) {
222 HConstant* input = inputs[i]->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100223 if (input != nullptr && DataType::IsFloatingPointType(input->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400224 ReplaceInput(invoke, input, i, true);
225 }
226 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000227
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000228 switch (invoke->GetIntrinsic()) {
Vladimir Markofc136522020-05-27 09:39:32 +0000229 case Intrinsics::kMathAbsDouble:
230 case Intrinsics::kMathAbsFloat:
231 case Intrinsics::kMathMaxDoubleDouble:
232 case Intrinsics::kMathMaxFloatFloat:
233 case Intrinsics::kMathMinDoubleDouble:
234 case Intrinsics::kMathMinFloatFloat:
235 LOG(FATAL) << "Unreachable min/max/abs: intrinsics should have been lowered "
236 "to IR nodes by instruction simplifier";
237 UNREACHABLE();
Vladimir Markoeebb8212018-06-05 14:57:24 +0100238 case Intrinsics::kIntegerValueOf:
239 // This intrinsic can be call free if it loads the address of the boot image object.
240 // If we're compiling PIC, we need the address base for loading from .data.bimg.rel.ro.
Vladimir Markoa2da9b92018-10-10 14:21:55 +0100241 if (!codegen_->GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +0100242 break;
243 }
244 FALLTHROUGH_INTENDED;
Aart Bik2c9f4952016-08-01 16:52:27 -0700245 case Intrinsics::kMathRoundFloat:
Vladimir Markoeebb8212018-06-05 14:57:24 +0100246 // This intrinsic needs the constant area.
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000247 if (!base_added) {
248 DCHECK(invoke_static_or_direct != nullptr);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000249 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
250 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000251 }
252 break;
253 default:
254 break;
255 }
Mark Mendell94991072015-10-06 14:58:32 -0400256 }
257
Aart Bikd1c40452016-03-02 16:06:13 -0800258 CodeGeneratorX86* codegen_;
259
Mark Mendell94991072015-10-06 14:58:32 -0400260 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000261 // input to the HX86LoadFromConstantTable instructions. Only set for
262 // graphs with reducible loops.
Mark Mendell94991072015-10-06 14:58:32 -0400263 HX86ComputeBaseMethodAddress* base_;
264};
265
Aart Bik24773202018-04-26 10:28:51 -0700266bool PcRelativeFixups::Run() {
Aart Bikd1c40452016-03-02 16:06:13 -0800267 PCRelativeHandlerVisitor visitor(graph_, codegen_);
Mark Mendell94991072015-10-06 14:58:32 -0400268 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000269 visitor.MoveBaseIfNeeded();
Aart Bik24773202018-04-26 10:28:51 -0700270 return true;
Mark Mendell94991072015-10-06 14:58:32 -0400271}
272
273} // namespace x86
274} // namespace art