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 | |
| 83 | void BinaryFP(HBinaryOperation* bin) { |
| 84 | HConstant* rhs = bin->InputAt(1)->AsConstant(); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 85 | if (rhs != nullptr && Primitive::IsFloatingPointType(rhs->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 86 | ReplaceInput(bin, rhs, 1, false); |
| 87 | } |
| 88 | } |
| 89 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 90 | 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 Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 129 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 130 | if (switch_insn->GetNumEntries() <= |
| 131 | InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) { |
| 132 | return; |
| 133 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 134 | // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to |
| 135 | // address the constant area. |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 136 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 137 | 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 Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 148 | void InitializePCRelativeBasePointer() { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 149 | // Ensure we only initialize the pointer once. |
| 150 | if (base_ != nullptr) { |
| 151 | return; |
| 152 | } |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 153 | // 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 Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 158 | DCHECK(base_ != nullptr); |
| 159 | } |
| 160 | |
| 161 | void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 162 | InitializePCRelativeBasePointer(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 163 | HX86LoadFromConstantTable* load_constant = |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 164 | new (GetGraph()->GetArena()) HX86LoadFromConstantTable(base_, value); |
| 165 | if (!materialize) { |
| 166 | load_constant->MarkEmittedAtUseSite(); |
| 167 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 168 | insn->GetBlock()->InsertInstructionBefore(load_constant, insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 169 | insn->ReplaceInput(load_constant, input_index); |
| 170 | } |
| 171 | |
| 172 | void HandleInvoke(HInvoke* invoke) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 173 | // 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 Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 176 | // 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 Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame^] | 186 | if (invoke_static_or_direct != nullptr && |
| 187 | invoke_static_or_direct->HasPcRelativeDexCache() && |
| 188 | !WillHaveCallFreeIntrinsicsCodeGen(invoke)) { |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 189 | InitializePCRelativeBasePointer(); |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 190 | // Add the extra parameter base_. |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 191 | invoke_static_or_direct->AddSpecialInput(base_); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 192 | base_added = true; |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 193 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 194 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 195 | // 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 Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 202 | |
| 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 Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame^] | 223 | 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 Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 241 | // The generated HX86ComputeBaseMethodAddress in the entry block needed as an |
| 242 | // input to the HX86LoadFromConstantTable instructions. |
| 243 | HX86ComputeBaseMethodAddress* base_; |
| 244 | }; |
| 245 | |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 246 | void PcRelativeFixups::Run() { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 247 | 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 Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame^] | 252 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 253 | visitor.VisitInsertionOrder(); |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 254 | visitor.MoveBaseIfNeeded(); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | } // namespace x86 |
| 258 | } // namespace art |