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(); |
| 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 | |
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(); |
| 85 | if (load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative || |
| 86 | load_kind == HLoadClass::LoadKind::kDexCachePcRelative) { |
| 87 | InitializePCRelativeBasePointer(); |
| 88 | load_class->AddSpecialInput(base_); |
| 89 | } |
| 90 | } |
| 91 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 92 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
| 93 | HLoadString::LoadKind load_kind = load_string->GetLoadKind(); |
| 94 | if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative || |
| 95 | load_kind == HLoadString::LoadKind::kDexCachePcRelative) { |
| 96 | InitializePCRelativeBasePointer(); |
| 97 | load_string->AddSpecialInput(base_); |
| 98 | } |
| 99 | } |
| 100 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 101 | void BinaryFP(HBinaryOperation* bin) { |
| 102 | HConstant* rhs = bin->InputAt(1)->AsConstant(); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 103 | if (rhs != nullptr && Primitive::IsFloatingPointType(rhs->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 104 | ReplaceInput(bin, rhs, 1, false); |
| 105 | } |
| 106 | } |
| 107 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 108 | void VisitEqual(HEqual* cond) OVERRIDE { |
| 109 | BinaryFP(cond); |
| 110 | } |
| 111 | |
| 112 | void VisitNotEqual(HNotEqual* cond) OVERRIDE { |
| 113 | BinaryFP(cond); |
| 114 | } |
| 115 | |
| 116 | void VisitLessThan(HLessThan* cond) OVERRIDE { |
| 117 | BinaryFP(cond); |
| 118 | } |
| 119 | |
| 120 | void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE { |
| 121 | BinaryFP(cond); |
| 122 | } |
| 123 | |
| 124 | void VisitGreaterThan(HGreaterThan* cond) OVERRIDE { |
| 125 | BinaryFP(cond); |
| 126 | } |
| 127 | |
| 128 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE { |
| 129 | BinaryFP(cond); |
| 130 | } |
| 131 | |
| 132 | void VisitNeg(HNeg* neg) OVERRIDE { |
| 133 | if (Primitive::IsFloatingPointType(neg->GetType())) { |
| 134 | // We need to replace the HNeg with a HX86FPNeg in order to address the constant area. |
| 135 | InitializePCRelativeBasePointer(); |
| 136 | HGraph* graph = GetGraph(); |
| 137 | HBasicBlock* block = neg->GetBlock(); |
| 138 | HX86FPNeg* x86_fp_neg = new (graph->GetArena()) HX86FPNeg( |
| 139 | neg->GetType(), |
| 140 | neg->InputAt(0), |
| 141 | base_, |
| 142 | neg->GetDexPc()); |
| 143 | block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg); |
| 144 | } |
| 145 | } |
| 146 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 147 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 148 | if (switch_insn->GetNumEntries() <= |
| 149 | InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) { |
| 150 | return; |
| 151 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 152 | // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to |
| 153 | // address the constant area. |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 154 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 155 | HGraph* graph = GetGraph(); |
| 156 | HBasicBlock* block = switch_insn->GetBlock(); |
| 157 | HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch( |
| 158 | switch_insn->GetStartValue(), |
| 159 | switch_insn->GetNumEntries(), |
| 160 | switch_insn->InputAt(0), |
| 161 | base_, |
| 162 | switch_insn->GetDexPc()); |
| 163 | block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch); |
| 164 | } |
| 165 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 166 | void InitializePCRelativeBasePointer() { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 167 | // Ensure we only initialize the pointer once. |
| 168 | if (base_ != nullptr) { |
| 169 | return; |
| 170 | } |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 171 | // Insert the base at the start of the entry block, move it to a better |
| 172 | // position later in MoveBaseIfNeeded(). |
| 173 | base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress(); |
| 174 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 175 | entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction()); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 176 | DCHECK(base_ != nullptr); |
| 177 | } |
| 178 | |
| 179 | void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 180 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 181 | HX86LoadFromConstantTable* load_constant = |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 182 | new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value); |
| 183 | if (!materialize) { |
| 184 | load_constant->MarkEmittedAtUseSite(); |
| 185 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 186 | insn->GetBlock()->InsertInstructionBefore(load_constant, insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 187 | insn->ReplaceInput(load_constant, input_index); |
| 188 | } |
| 189 | |
| 190 | void HandleInvoke(HInvoke* invoke) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 191 | // If this is an invoke-static/-direct with PC-relative dex cache array |
| 192 | // addressing, we need the PC-relative address base. |
| 193 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 194 | // We can't add a pointer to the constant area if we already have a current |
| 195 | // method pointer. This may arise when sharpening doesn't remove the current |
| 196 | // method pointer from the invoke. |
| 197 | if (invoke_static_or_direct != nullptr && |
| 198 | invoke_static_or_direct->HasCurrentMethodInput()) { |
| 199 | DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache()); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | bool base_added = false; |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 204 | if (invoke_static_or_direct != nullptr && |
| 205 | invoke_static_or_direct->HasPcRelativeDexCache() && |
| 206 | !WillHaveCallFreeIntrinsicsCodeGen(invoke)) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 207 | InitializePCRelativeBasePointer(); |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 208 | // Add the extra parameter base_. |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 209 | invoke_static_or_direct->AddSpecialInput(base_); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 210 | base_added = true; |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 211 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 212 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 213 | // Ensure that we can load FP arguments from the constant area. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 214 | HInputsRef inputs = invoke->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 215 | for (size_t i = 0; i < inputs.size(); i++) { |
| 216 | HConstant* input = inputs[i]->AsConstant(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 217 | if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) { |
| 218 | ReplaceInput(invoke, input, i, true); |
| 219 | } |
| 220 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 221 | |
| 222 | // These intrinsics need the constant area. |
| 223 | switch (invoke->GetIntrinsic()) { |
| 224 | case Intrinsics::kMathAbsDouble: |
| 225 | case Intrinsics::kMathAbsFloat: |
| 226 | case Intrinsics::kMathMaxDoubleDouble: |
| 227 | case Intrinsics::kMathMaxFloatFloat: |
| 228 | case Intrinsics::kMathMinDoubleDouble: |
| 229 | case Intrinsics::kMathMinFloatFloat: |
| 230 | if (!base_added) { |
| 231 | DCHECK(invoke_static_or_direct != nullptr); |
| 232 | DCHECK(!invoke_static_or_direct->HasCurrentMethodInput()); |
| 233 | InitializePCRelativeBasePointer(); |
| 234 | invoke_static_or_direct->AddSpecialInput(base_); |
| 235 | } |
| 236 | break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 242 | bool WillHaveCallFreeIntrinsicsCodeGen(HInvoke* invoke) { |
| 243 | if (invoke->GetIntrinsic() != Intrinsics::kNone) { |
| 244 | // This invoke may have intrinsic code generation defined. However, we must |
| 245 | // now also determine if this code generation is truly there and call-free |
| 246 | // (not unimplemented, no bail on instruction features, or call on slow path). |
| 247 | // This is done by actually calling the locations builder on the instruction |
| 248 | // and clearing out the locations once result is known. We assume this |
| 249 | // call only has creating locations as side effects! |
| 250 | IntrinsicLocationsBuilderX86 builder(codegen_); |
| 251 | bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall(); |
| 252 | invoke->SetLocations(nullptr); |
| 253 | return success; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | CodeGeneratorX86* codegen_; |
| 259 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 260 | // The generated HX86ComputeBaseMethodAddress in the entry block needed as an |
| 261 | // input to the HX86LoadFromConstantTable instructions. |
| 262 | HX86ComputeBaseMethodAddress* base_; |
| 263 | }; |
| 264 | |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 265 | void PcRelativeFixups::Run() { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 266 | if (graph_->HasIrreducibleLoops()) { |
| 267 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 268 | // that can be live-in at the irreducible loop header. |
| 269 | return; |
| 270 | } |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 271 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 272 | visitor.VisitInsertionOrder(); |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 273 | visitor.MoveBaseIfNeeded(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | } // namespace x86 |
| 277 | } // namespace art |