blob: 9049457da5bf4bd7fcc8cda02c69cb6c1c18ffb7 [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();
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
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 {
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
Vladimir Markocac5a7e2016-02-22 10:39:50 +000090 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
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000104 void VisitEqual(HEqual* cond) OVERRIDE {
105 BinaryFP(cond);
106 }
107
108 void VisitNotEqual(HNotEqual* cond) OVERRIDE {
109 BinaryFP(cond);
110 }
111
112 void VisitLessThan(HLessThan* cond) OVERRIDE {
113 BinaryFP(cond);
114 }
115
116 void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE {
117 BinaryFP(cond);
118 }
119
120 void VisitGreaterThan(HGreaterThan* cond) OVERRIDE {
121 BinaryFP(cond);
122 }
123
124 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE {
125 BinaryFP(cond);
126 }
127
128 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
Mark Mendell94991072015-10-06 14:58:32 -0400143 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 // If this is an invoke-static/-direct with PC-relative dex cache array
197 // addressing, we need the PC-relative address base.
198 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000199 // We can't add a pointer to the constant area if we already have a current
200 // method pointer. This may arise when sharpening doesn't remove the current
201 // method pointer from the invoke.
202 if (invoke_static_or_direct != nullptr &&
203 invoke_static_or_direct->HasCurrentMethodInput()) {
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
208 bool base_added = false;
Aart Bikd1c40452016-03-02 16:06:13 -0800209 if (invoke_static_or_direct != nullptr &&
Vladimir Marko65979462017-05-19 17:25:12 +0100210 invoke_static_or_direct->HasPcRelativeMethodLoadKind() &&
Vladimir Marko68c981f2016-08-26 13:13:33 +0100211 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000212 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
213 // Add the extra parameter.
214 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000215 base_added = true;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000216 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000217
Mark Mendell94991072015-10-06 14:58:32 -0400218 // Ensure that we can load FP arguments from the constant area.
Vladimir Markoe9004912016-06-16 16:50:52 +0100219 HInputsRef inputs = invoke->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100220 for (size_t i = 0; i < inputs.size(); i++) {
221 HConstant* input = inputs[i]->AsConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 if (input != nullptr && DataType::IsFloatingPointType(input->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -0400223 ReplaceInput(invoke, input, i, true);
224 }
225 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000226
227 // These intrinsics need the constant area.
228 switch (invoke->GetIntrinsic()) {
229 case Intrinsics::kMathAbsDouble:
230 case Intrinsics::kMathAbsFloat:
231 case Intrinsics::kMathMaxDoubleDouble:
232 case Intrinsics::kMathMaxFloatFloat:
233 case Intrinsics::kMathMinDoubleDouble:
234 case Intrinsics::kMathMinFloatFloat:
Aart Bik1f8d51b2018-02-15 10:42:37 -0800235 LOG(FATAL) << "Unreachable min/max/abs: intrinsics should have been lowered "
236 "to IR nodes by instruction simplifier";
237 UNREACHABLE();
Aart Bik2c9f4952016-08-01 16:52:27 -0700238 case Intrinsics::kMathRoundFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000239 if (!base_added) {
240 DCHECK(invoke_static_or_direct != nullptr);
241 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000242 HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
243 invoke_static_or_direct->AddSpecialInput(method_address);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000244 }
245 break;
246 default:
247 break;
248 }
Mark Mendell94991072015-10-06 14:58:32 -0400249 }
250
Aart Bikd1c40452016-03-02 16:06:13 -0800251 CodeGeneratorX86* codegen_;
252
Mark Mendell94991072015-10-06 14:58:32 -0400253 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000254 // input to the HX86LoadFromConstantTable instructions. Only set for
255 // graphs with reducible loops.
Mark Mendell94991072015-10-06 14:58:32 -0400256 HX86ComputeBaseMethodAddress* base_;
257};
258
Aart Bik24773202018-04-26 10:28:51 -0700259bool PcRelativeFixups::Run() {
Aart Bikd1c40452016-03-02 16:06:13 -0800260 PCRelativeHandlerVisitor visitor(graph_, codegen_);
Mark Mendell94991072015-10-06 14:58:32 -0400261 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000262 visitor.MoveBaseIfNeeded();
Aart Bik24773202018-04-26 10:28:51 -0700263 return true;
Mark Mendell94991072015-10-06 14:58:32 -0400264}
265
266} // namespace x86
267} // namespace art