blob: d281a9fc6c68f8ded01fed3b83c98e0d4aeb70a8 [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
83 void BinaryFP(HBinaryOperation* bin) {
84 HConstant* rhs = bin->InputAt(1)->AsConstant();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000085 if (rhs != nullptr && Primitive::IsFloatingPointType(rhs->GetType())) {
Mark Mendell94991072015-10-06 14:58:32 -040086 ReplaceInput(bin, rhs, 1, false);
87 }
88 }
89
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000090 void VisitEqual(HEqual* cond) OVERRIDE {
91 BinaryFP(cond);
92 }
93
94 void VisitNotEqual(HNotEqual* cond) OVERRIDE {
95 BinaryFP(cond);
96 }
97
98 void VisitLessThan(HLessThan* cond) OVERRIDE {
99 BinaryFP(cond);
100 }
101
102 void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE {
103 BinaryFP(cond);
104 }
105
106 void VisitGreaterThan(HGreaterThan* cond) OVERRIDE {
107 BinaryFP(cond);
108 }
109
110 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE {
111 BinaryFP(cond);
112 }
113
114 void VisitNeg(HNeg* neg) OVERRIDE {
115 if (Primitive::IsFloatingPointType(neg->GetType())) {
116 // We need to replace the HNeg with a HX86FPNeg in order to address the constant area.
117 InitializePCRelativeBasePointer();
118 HGraph* graph = GetGraph();
119 HBasicBlock* block = neg->GetBlock();
120 HX86FPNeg* x86_fp_neg = new (graph->GetArena()) HX86FPNeg(
121 neg->GetType(),
122 neg->InputAt(0),
123 base_,
124 neg->GetDexPc());
125 block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg);
126 }
127 }
128
Mark Mendell94991072015-10-06 14:58:32 -0400129 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000130 if (switch_insn->GetNumEntries() <=
131 InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
132 return;
133 }
Mark Mendell94991072015-10-06 14:58:32 -0400134 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
135 // address the constant area.
Vladimir Markofb337ea2015-11-25 15:25:10 +0000136 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400137 HGraph* graph = GetGraph();
138 HBasicBlock* block = switch_insn->GetBlock();
139 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
140 switch_insn->GetStartValue(),
141 switch_insn->GetNumEntries(),
142 switch_insn->InputAt(0),
143 base_,
144 switch_insn->GetDexPc());
145 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
146 }
147
Vladimir Markofb337ea2015-11-25 15:25:10 +0000148 void InitializePCRelativeBasePointer() {
Mark Mendell94991072015-10-06 14:58:32 -0400149 // Ensure we only initialize the pointer once.
150 if (base_ != nullptr) {
151 return;
152 }
Vladimir Markofb337ea2015-11-25 15:25:10 +0000153 // Insert the base at the start of the entry block, move it to a better
154 // position later in MoveBaseIfNeeded().
155 base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress();
156 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
157 entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction());
Mark Mendell94991072015-10-06 14:58:32 -0400158 DCHECK(base_ != nullptr);
159 }
160
161 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000162 InitializePCRelativeBasePointer();
Mark Mendell94991072015-10-06 14:58:32 -0400163 HX86LoadFromConstantTable* load_constant =
David Brazdilb3e773e2016-01-26 11:28:37 +0000164 new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value);
165 if (!materialize) {
166 load_constant->MarkEmittedAtUseSite();
167 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000168 insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
Mark Mendell94991072015-10-06 14:58:32 -0400169 insn->ReplaceInput(load_constant, input_index);
170 }
171
172 void HandleInvoke(HInvoke* invoke) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000173 // If this is an invoke-static/-direct with PC-relative dex cache array
174 // addressing, we need the PC-relative address base.
175 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000176 // We can't add a pointer to the constant area if we already have a current
177 // method pointer. This may arise when sharpening doesn't remove the current
178 // method pointer from the invoke.
179 if (invoke_static_or_direct != nullptr &&
180 invoke_static_or_direct->HasCurrentMethodInput()) {
181 DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache());
182 return;
183 }
184
185 bool base_added = false;
Aart Bikd1c40452016-03-02 16:06:13 -0800186 if (invoke_static_or_direct != nullptr &&
187 invoke_static_or_direct->HasPcRelativeDexCache() &&
188 !WillHaveCallFreeIntrinsicsCodeGen(invoke)) {
Vladimir Markofb337ea2015-11-25 15:25:10 +0000189 InitializePCRelativeBasePointer();
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000190 // Add the extra parameter base_.
Vladimir Markoc53c0792015-11-19 15:48:33 +0000191 invoke_static_or_direct->AddSpecialInput(base_);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000192 base_added = true;
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000193 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000194
Mark Mendell94991072015-10-06 14:58:32 -0400195 // Ensure that we can load FP arguments from the constant area.
196 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
197 HConstant* input = invoke->InputAt(i)->AsConstant();
198 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
199 ReplaceInput(invoke, input, i, true);
200 }
201 }
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000202
203 // These intrinsics need the constant area.
204 switch (invoke->GetIntrinsic()) {
205 case Intrinsics::kMathAbsDouble:
206 case Intrinsics::kMathAbsFloat:
207 case Intrinsics::kMathMaxDoubleDouble:
208 case Intrinsics::kMathMaxFloatFloat:
209 case Intrinsics::kMathMinDoubleDouble:
210 case Intrinsics::kMathMinFloatFloat:
211 if (!base_added) {
212 DCHECK(invoke_static_or_direct != nullptr);
213 DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
214 InitializePCRelativeBasePointer();
215 invoke_static_or_direct->AddSpecialInput(base_);
216 }
217 break;
218 default:
219 break;
220 }
Mark Mendell94991072015-10-06 14:58:32 -0400221 }
222
Aart Bikd1c40452016-03-02 16:06:13 -0800223 bool WillHaveCallFreeIntrinsicsCodeGen(HInvoke* invoke) {
224 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
225 // This invoke may have intrinsic code generation defined. However, we must
226 // now also determine if this code generation is truly there and call-free
227 // (not unimplemented, no bail on instruction features, or call on slow path).
228 // This is done by actually calling the locations builder on the instruction
229 // and clearing out the locations once result is known. We assume this
230 // call only has creating locations as side effects!
231 IntrinsicLocationsBuilderX86 builder(codegen_);
232 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
233 invoke->SetLocations(nullptr);
234 return success;
235 }
236 return false;
237 }
238
239 CodeGeneratorX86* codegen_;
240
Mark Mendell94991072015-10-06 14:58:32 -0400241 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
242 // input to the HX86LoadFromConstantTable instructions.
243 HX86ComputeBaseMethodAddress* base_;
244};
245
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000246void PcRelativeFixups::Run() {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000247 if (graph_->HasIrreducibleLoops()) {
248 // Do not run this optimization, as irreducible loops do not work with an instruction
249 // that can be live-in at the irreducible loop header.
250 return;
251 }
Aart Bikd1c40452016-03-02 16:06:13 -0800252 PCRelativeHandlerVisitor visitor(graph_, codegen_);
Mark Mendell94991072015-10-06 14:58:32 -0400253 visitor.VisitInsertionOrder();
Vladimir Markofb337ea2015-11-25 15:25:10 +0000254 visitor.MoveBaseIfNeeded();
Mark Mendell94991072015-10-06 14:58:32 -0400255}
256
257} // namespace x86
258} // namespace art