Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 1 | /* |
| 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 Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 17 | #include "pc_relative_fixups_x86.h" |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 18 | #include "code_generator_x86.h" |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 19 | #include "intrinsics_x86.h" |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | namespace x86 { |
| 23 | |
| 24 | /** |
| 25 | * Finds instructions that need the constant area base as an input. |
| 26 | */ |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 27 | class PCRelativeHandlerVisitor : public HGraphVisitor { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 28 | public: |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 29 | PCRelativeHandlerVisitor(HGraph* graph, CodeGenerator* codegen) |
| 30 | : HGraphVisitor(graph), |
| 31 | codegen_(down_cast<CodeGeneratorX86*>(codegen)), |
| 32 | base_(nullptr) {} |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 33 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 34 | 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 Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 43 | 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 Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 60 | void VisitCompare(HCompare* compare) OVERRIDE { |
| 61 | BinaryFP(compare); |
| 62 | } |
| 63 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 64 | void VisitReturn(HReturn* ret) OVERRIDE { |
| 65 | HConstant* value = ret->InputAt(0)->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 66 | if ((value != nullptr && DataType::IsFloatingPointType(value->GetType()))) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 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 Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 83 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
| 84 | HLoadClass::LoadKind load_kind = load_class->GetLoadKind(); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 85 | if (load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative || |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 86 | load_kind == HLoadClass::LoadKind::kBootImageClassTable || |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 87 | load_kind == HLoadClass::LoadKind::kBssEntry) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 88 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_class); |
| 89 | load_class->AddSpecialInput(method_address); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 93 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
| 94 | HLoadString::LoadKind load_kind = load_string->GetLoadKind(); |
| 95 | if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative || |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 96 | load_kind == HLoadString::LoadKind::kBootImageInternTable || |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 97 | load_kind == HLoadString::LoadKind::kBssEntry) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 98 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_string); |
| 99 | load_string->AddSpecialInput(method_address); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 103 | void BinaryFP(HBinaryOperation* bin) { |
| 104 | HConstant* rhs = bin->InputAt(1)->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 105 | if (rhs != nullptr && DataType::IsFloatingPointType(rhs->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 106 | ReplaceInput(bin, rhs, 1, false); |
| 107 | } |
| 108 | } |
| 109 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 110 | void VisitEqual(HEqual* cond) OVERRIDE { |
| 111 | BinaryFP(cond); |
| 112 | } |
| 113 | |
| 114 | void VisitNotEqual(HNotEqual* cond) OVERRIDE { |
| 115 | BinaryFP(cond); |
| 116 | } |
| 117 | |
| 118 | void VisitLessThan(HLessThan* cond) OVERRIDE { |
| 119 | BinaryFP(cond); |
| 120 | } |
| 121 | |
| 122 | void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE { |
| 123 | BinaryFP(cond); |
| 124 | } |
| 125 | |
| 126 | void VisitGreaterThan(HGreaterThan* cond) OVERRIDE { |
| 127 | BinaryFP(cond); |
| 128 | } |
| 129 | |
| 130 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE { |
| 131 | BinaryFP(cond); |
| 132 | } |
| 133 | |
| 134 | void VisitNeg(HNeg* neg) OVERRIDE { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 135 | if (DataType::IsFloatingPointType(neg->GetType())) { |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 136 | // We need to replace the HNeg with a HX86FPNeg in order to address the constant area. |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 137 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(neg); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 138 | HGraph* graph = GetGraph(); |
| 139 | HBasicBlock* block = neg->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 140 | HX86FPNeg* x86_fp_neg = new (graph->GetAllocator()) HX86FPNeg( |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 141 | neg->GetType(), |
| 142 | neg->InputAt(0), |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 143 | method_address, |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 144 | neg->GetDexPc()); |
| 145 | block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg); |
| 146 | } |
| 147 | } |
| 148 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 149 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 150 | if (switch_insn->GetNumEntries() <= |
| 151 | InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) { |
| 152 | return; |
| 153 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 154 | // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to |
| 155 | // address the constant area. |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 156 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(switch_insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 157 | HGraph* graph = GetGraph(); |
| 158 | HBasicBlock* block = switch_insn->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 159 | HX86PackedSwitch* x86_switch = new (graph->GetAllocator()) HX86PackedSwitch( |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 160 | switch_insn->GetStartValue(), |
| 161 | switch_insn->GetNumEntries(), |
| 162 | switch_insn->InputAt(0), |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 163 | method_address, |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 164 | switch_insn->GetDexPc()); |
| 165 | block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch); |
| 166 | } |
| 167 | |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 168 | HX86ComputeBaseMethodAddress* GetPCRelativeBasePointer(HInstruction* cursor) { |
| 169 | bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops(); |
| 170 | if (!has_irreducible_loops) { |
| 171 | // Ensure we only initialize the pointer once. |
| 172 | if (base_ != nullptr) { |
| 173 | return base_; |
| 174 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 175 | } |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 176 | // Insert the base at the start of the entry block, move it to a better |
| 177 | // position later in MoveBaseIfNeeded(). |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 178 | HX86ComputeBaseMethodAddress* method_address = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 179 | new (GetGraph()->GetAllocator()) HX86ComputeBaseMethodAddress(); |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 180 | if (has_irreducible_loops) { |
| 181 | cursor->GetBlock()->InsertInstructionBefore(method_address, cursor); |
| 182 | } else { |
| 183 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 184 | entry_block->InsertInstructionBefore(method_address, entry_block->GetFirstInstruction()); |
| 185 | base_ = method_address; |
| 186 | } |
| 187 | return method_address; |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 191 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 192 | HX86LoadFromConstantTable* load_constant = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 193 | new (GetGraph()->GetAllocator()) HX86LoadFromConstantTable(method_address, value); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 194 | if (!materialize) { |
| 195 | load_constant->MarkEmittedAtUseSite(); |
| 196 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 197 | insn->GetBlock()->InsertInstructionBefore(load_constant, insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 198 | insn->ReplaceInput(load_constant, input_index); |
| 199 | } |
| 200 | |
| 201 | void HandleInvoke(HInvoke* invoke) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 202 | // If this is an invoke-static/-direct with PC-relative dex cache array |
| 203 | // addressing, we need the PC-relative address base. |
| 204 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 205 | // We can't add a pointer to the constant area if we already have a current |
| 206 | // method pointer. This may arise when sharpening doesn't remove the current |
| 207 | // method pointer from the invoke. |
| 208 | if (invoke_static_or_direct != nullptr && |
| 209 | invoke_static_or_direct->HasCurrentMethodInput()) { |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 210 | DCHECK(!invoke_static_or_direct->HasPcRelativeMethodLoadKind()); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
| 214 | bool base_added = false; |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 215 | if (invoke_static_or_direct != nullptr && |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 216 | invoke_static_or_direct->HasPcRelativeMethodLoadKind() && |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 217 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 218 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke); |
| 219 | // Add the extra parameter. |
| 220 | invoke_static_or_direct->AddSpecialInput(method_address); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 221 | base_added = true; |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 222 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 223 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 224 | // Ensure that we can load FP arguments from the constant area. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 225 | HInputsRef inputs = invoke->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 226 | for (size_t i = 0; i < inputs.size(); i++) { |
| 227 | HConstant* input = inputs[i]->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 228 | if (input != nullptr && DataType::IsFloatingPointType(input->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 229 | ReplaceInput(invoke, input, i, true); |
| 230 | } |
| 231 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 232 | |
| 233 | // These intrinsics need the constant area. |
| 234 | switch (invoke->GetIntrinsic()) { |
| 235 | case Intrinsics::kMathAbsDouble: |
| 236 | case Intrinsics::kMathAbsFloat: |
| 237 | case Intrinsics::kMathMaxDoubleDouble: |
| 238 | case Intrinsics::kMathMaxFloatFloat: |
| 239 | case Intrinsics::kMathMinDoubleDouble: |
| 240 | case Intrinsics::kMathMinFloatFloat: |
Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 241 | case Intrinsics::kMathRoundFloat: |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 242 | if (!base_added) { |
| 243 | DCHECK(invoke_static_or_direct != nullptr); |
| 244 | DCHECK(!invoke_static_or_direct->HasCurrentMethodInput()); |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 245 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke); |
| 246 | invoke_static_or_direct->AddSpecialInput(method_address); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 247 | } |
| 248 | break; |
| 249 | default: |
| 250 | break; |
| 251 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 254 | CodeGeneratorX86* codegen_; |
| 255 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 256 | // The generated HX86ComputeBaseMethodAddress in the entry block needed as an |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 257 | // input to the HX86LoadFromConstantTable instructions. Only set for |
| 258 | // graphs with reducible loops. |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 259 | HX86ComputeBaseMethodAddress* base_; |
| 260 | }; |
| 261 | |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 262 | void PcRelativeFixups::Run() { |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 263 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 264 | visitor.VisitInsertionOrder(); |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 265 | visitor.MoveBaseIfNeeded(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | } // namespace x86 |
| 269 | } // namespace art |