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" |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | namespace x86 { |
| 22 | |
| 23 | /** |
| 24 | * Finds instructions that need the constant area base as an input. |
| 25 | */ |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 26 | class PCRelativeHandlerVisitor : public HGraphVisitor { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 27 | public: |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 28 | explicit PCRelativeHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {} |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 29 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 30 | void MoveBaseIfNeeded() { |
| 31 | if (base_ != nullptr) { |
| 32 | // Bring the base closer to the first use (previously, it was in the |
| 33 | // entry block) and relieve some pressure on the register allocator |
| 34 | // while avoiding recalculation of the base in a loop. |
| 35 | base_->MoveBeforeFirstUserAndOutOfLoops(); |
| 36 | } |
| 37 | } |
| 38 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 39 | private: |
| 40 | void VisitAdd(HAdd* add) OVERRIDE { |
| 41 | BinaryFP(add); |
| 42 | } |
| 43 | |
| 44 | void VisitSub(HSub* sub) OVERRIDE { |
| 45 | BinaryFP(sub); |
| 46 | } |
| 47 | |
| 48 | void VisitMul(HMul* mul) OVERRIDE { |
| 49 | BinaryFP(mul); |
| 50 | } |
| 51 | |
| 52 | void VisitDiv(HDiv* div) OVERRIDE { |
| 53 | BinaryFP(div); |
| 54 | } |
| 55 | |
| 56 | void VisitReturn(HReturn* ret) OVERRIDE { |
| 57 | HConstant* value = ret->InputAt(0)->AsConstant(); |
| 58 | if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) { |
| 59 | ReplaceInput(ret, value, 0, true); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 64 | HandleInvoke(invoke); |
| 65 | } |
| 66 | |
| 67 | void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE { |
| 68 | HandleInvoke(invoke); |
| 69 | } |
| 70 | |
| 71 | void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE { |
| 72 | HandleInvoke(invoke); |
| 73 | } |
| 74 | |
| 75 | void BinaryFP(HBinaryOperation* bin) { |
| 76 | HConstant* rhs = bin->InputAt(1)->AsConstant(); |
Nicolas Geoffray | cf8d1bb | 2016-01-25 09:43:30 +0000 | [diff] [blame] | 77 | if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 78 | ReplaceInput(bin, rhs, 1, false); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 83 | if (switch_insn->GetNumEntries() <= |
| 84 | InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) { |
| 85 | return; |
| 86 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 87 | // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to |
| 88 | // address the constant area. |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 89 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 90 | HGraph* graph = GetGraph(); |
| 91 | HBasicBlock* block = switch_insn->GetBlock(); |
| 92 | HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch( |
| 93 | switch_insn->GetStartValue(), |
| 94 | switch_insn->GetNumEntries(), |
| 95 | switch_insn->InputAt(0), |
| 96 | base_, |
| 97 | switch_insn->GetDexPc()); |
| 98 | block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch); |
| 99 | } |
| 100 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 101 | void InitializePCRelativeBasePointer() { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 102 | // Ensure we only initialize the pointer once. |
| 103 | if (base_ != nullptr) { |
| 104 | return; |
| 105 | } |
| 106 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 107 | // Insert the base at the start of the entry block, move it to a better |
| 108 | // position later in MoveBaseIfNeeded(). |
| 109 | base_ = new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress(); |
| 110 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 111 | entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction()); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 112 | DCHECK(base_ != nullptr); |
| 113 | } |
| 114 | |
| 115 | void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 116 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 117 | HX86LoadFromConstantTable* load_constant = |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 118 | new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value, materialize); |
| 119 | insn->GetBlock()->InsertInstructionBefore(load_constant, insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 120 | insn->ReplaceInput(load_constant, input_index); |
| 121 | } |
| 122 | |
| 123 | void HandleInvoke(HInvoke* invoke) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 124 | // If this is an invoke-static/-direct with PC-relative dex cache array |
| 125 | // addressing, we need the PC-relative address base. |
| 126 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
| 127 | if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasPcRelativeDexCache()) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 128 | InitializePCRelativeBasePointer(); |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 129 | // Add the extra parameter base_. |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 130 | DCHECK(!invoke_static_or_direct->HasCurrentMethodInput()); |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 131 | invoke_static_or_direct->AddSpecialInput(base_); |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 132 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 133 | // Ensure that we can load FP arguments from the constant area. |
| 134 | for (size_t i = 0, e = invoke->InputCount(); i < e; i++) { |
| 135 | HConstant* input = invoke->InputAt(i)->AsConstant(); |
| 136 | if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) { |
| 137 | ReplaceInput(invoke, input, i, true); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // The generated HX86ComputeBaseMethodAddress in the entry block needed as an |
| 143 | // input to the HX86LoadFromConstantTable instructions. |
| 144 | HX86ComputeBaseMethodAddress* base_; |
| 145 | }; |
| 146 | |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 147 | void PcRelativeFixups::Run() { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 148 | if (graph_->HasIrreducibleLoops()) { |
| 149 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 150 | // that can be live-in at the irreducible loop header. |
| 151 | return; |
| 152 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 153 | PCRelativeHandlerVisitor visitor(graph_); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 154 | visitor.VisitInsertionOrder(); |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 155 | visitor.MoveBaseIfNeeded(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace x86 |
| 159 | } // namespace art |