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" |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 20 | #include "runtime.h" |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | namespace x86 { |
| 24 | |
| 25 | /** |
| 26 | * Finds instructions that need the constant area base as an input. |
| 27 | */ |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 28 | class PCRelativeHandlerVisitor : public HGraphVisitor { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 29 | public: |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 30 | PCRelativeHandlerVisitor(HGraph* graph, CodeGenerator* codegen) |
| 31 | : HGraphVisitor(graph), |
| 32 | codegen_(down_cast<CodeGeneratorX86*>(codegen)), |
| 33 | base_(nullptr) {} |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 34 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 35 | void MoveBaseIfNeeded() { |
| 36 | if (base_ != nullptr) { |
| 37 | // Bring the base closer to the first use (previously, it was in the |
| 38 | // entry block) and relieve some pressure on the register allocator |
| 39 | // while avoiding recalculation of the base in a loop. |
| 40 | base_->MoveBeforeFirstUserAndOutOfLoops(); |
| 41 | } |
| 42 | } |
| 43 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 44 | private: |
| 45 | void VisitAdd(HAdd* add) OVERRIDE { |
| 46 | BinaryFP(add); |
| 47 | } |
| 48 | |
| 49 | void VisitSub(HSub* sub) OVERRIDE { |
| 50 | BinaryFP(sub); |
| 51 | } |
| 52 | |
| 53 | void VisitMul(HMul* mul) OVERRIDE { |
| 54 | BinaryFP(mul); |
| 55 | } |
| 56 | |
| 57 | void VisitDiv(HDiv* div) OVERRIDE { |
| 58 | BinaryFP(div); |
| 59 | } |
| 60 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 61 | void VisitCompare(HCompare* compare) OVERRIDE { |
| 62 | BinaryFP(compare); |
| 63 | } |
| 64 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 65 | void VisitReturn(HReturn* ret) OVERRIDE { |
| 66 | HConstant* value = ret->InputAt(0)->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 67 | if ((value != nullptr && DataType::IsFloatingPointType(value->GetType()))) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 68 | ReplaceInput(ret, value, 0, true); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 73 | HandleInvoke(invoke); |
| 74 | } |
| 75 | |
| 76 | void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE { |
| 77 | HandleInvoke(invoke); |
| 78 | } |
| 79 | |
| 80 | void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE { |
| 81 | HandleInvoke(invoke); |
| 82 | } |
| 83 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 84 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
Vladimir Marko | e47f60c | 2018-02-21 13:43:28 +0000 | [diff] [blame] | 85 | if (load_class->HasPcRelativeLoadKind()) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 86 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_class); |
| 87 | load_class->AddSpecialInput(method_address); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 91 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
Vladimir Marko | e47f60c | 2018-02-21 13:43:28 +0000 | [diff] [blame] | 92 | if (load_string->HasPcRelativeLoadKind()) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 93 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_string); |
| 94 | load_string->AddSpecialInput(method_address); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 98 | void BinaryFP(HBinaryOperation* bin) { |
| 99 | HConstant* rhs = bin->InputAt(1)->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 100 | if (rhs != nullptr && DataType::IsFloatingPointType(rhs->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 101 | ReplaceInput(bin, rhs, 1, false); |
| 102 | } |
| 103 | } |
| 104 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 105 | void VisitEqual(HEqual* cond) OVERRIDE { |
| 106 | BinaryFP(cond); |
| 107 | } |
| 108 | |
| 109 | void VisitNotEqual(HNotEqual* cond) OVERRIDE { |
| 110 | BinaryFP(cond); |
| 111 | } |
| 112 | |
| 113 | void VisitLessThan(HLessThan* cond) OVERRIDE { |
| 114 | BinaryFP(cond); |
| 115 | } |
| 116 | |
| 117 | void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE { |
| 118 | BinaryFP(cond); |
| 119 | } |
| 120 | |
| 121 | void VisitGreaterThan(HGreaterThan* cond) OVERRIDE { |
| 122 | BinaryFP(cond); |
| 123 | } |
| 124 | |
| 125 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE { |
| 126 | BinaryFP(cond); |
| 127 | } |
| 128 | |
| 129 | void VisitNeg(HNeg* neg) OVERRIDE { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 130 | if (DataType::IsFloatingPointType(neg->GetType())) { |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 131 | // 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] | 132 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(neg); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 133 | HGraph* graph = GetGraph(); |
| 134 | HBasicBlock* block = neg->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 135 | HX86FPNeg* x86_fp_neg = new (graph->GetAllocator()) HX86FPNeg( |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 136 | neg->GetType(), |
| 137 | neg->InputAt(0), |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 138 | method_address, |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 139 | neg->GetDexPc()); |
| 140 | block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg); |
| 141 | } |
| 142 | } |
| 143 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 144 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 145 | if (switch_insn->GetNumEntries() <= |
| 146 | InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) { |
| 147 | return; |
| 148 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 149 | // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to |
| 150 | // address the constant area. |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 151 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(switch_insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 152 | HGraph* graph = GetGraph(); |
| 153 | HBasicBlock* block = switch_insn->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 154 | HX86PackedSwitch* x86_switch = new (graph->GetAllocator()) HX86PackedSwitch( |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 155 | switch_insn->GetStartValue(), |
| 156 | switch_insn->GetNumEntries(), |
| 157 | switch_insn->InputAt(0), |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 158 | method_address, |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 159 | switch_insn->GetDexPc()); |
| 160 | block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch); |
| 161 | } |
| 162 | |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 163 | HX86ComputeBaseMethodAddress* GetPCRelativeBasePointer(HInstruction* cursor) { |
| 164 | bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops(); |
| 165 | if (!has_irreducible_loops) { |
| 166 | // Ensure we only initialize the pointer once. |
| 167 | if (base_ != nullptr) { |
| 168 | return base_; |
| 169 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 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(). |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 173 | HX86ComputeBaseMethodAddress* method_address = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 174 | new (GetGraph()->GetAllocator()) HX86ComputeBaseMethodAddress(); |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 175 | if (has_irreducible_loops) { |
| 176 | cursor->GetBlock()->InsertInstructionBefore(method_address, cursor); |
| 177 | } else { |
| 178 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 179 | entry_block->InsertInstructionBefore(method_address, entry_block->GetFirstInstruction()); |
| 180 | base_ = method_address; |
| 181 | } |
| 182 | return method_address; |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 186 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 187 | HX86LoadFromConstantTable* load_constant = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 188 | new (GetGraph()->GetAllocator()) HX86LoadFromConstantTable(method_address, value); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 189 | if (!materialize) { |
| 190 | load_constant->MarkEmittedAtUseSite(); |
| 191 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 192 | insn->GetBlock()->InsertInstructionBefore(load_constant, insn); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 193 | insn->ReplaceInput(load_constant, input_index); |
| 194 | } |
| 195 | |
| 196 | void HandleInvoke(HInvoke* invoke) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 197 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 198 | |
| 199 | // We can't add the method address if we already have a current method pointer. |
| 200 | // This may arise when sharpening doesn't remove the current method pointer from the invoke. |
| 201 | if (invoke_static_or_direct != nullptr && invoke_static_or_direct->HasCurrentMethodInput()) { |
| 202 | // Note: This happens only for recursive calls (including compiling an intrinsic |
| 203 | // by faking a call to itself; we use kRuntimeCall for this case). |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 204 | DCHECK(!invoke_static_or_direct->HasPcRelativeMethodLoadKind()); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 208 | // If this is an invoke-static/-direct with PC-relative addressing (within boot image |
| 209 | // or using .bss or .data.bimg.rel.ro), we need the PC-relative address base. |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 210 | bool base_added = false; |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 211 | if (invoke_static_or_direct != nullptr && |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 212 | invoke_static_or_direct->HasPcRelativeMethodLoadKind() && |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 213 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) { |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 214 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke); |
| 215 | // Add the extra parameter. |
| 216 | invoke_static_or_direct->AddSpecialInput(method_address); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 217 | base_added = true; |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 218 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 219 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 220 | // Ensure that we can load FP arguments from the constant area. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 221 | HInputsRef inputs = invoke->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 222 | for (size_t i = 0; i < inputs.size(); i++) { |
| 223 | HConstant* input = inputs[i]->AsConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 224 | if (input != nullptr && DataType::IsFloatingPointType(input->GetType())) { |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 225 | ReplaceInput(invoke, input, i, true); |
| 226 | } |
| 227 | } |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 228 | |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 229 | switch (invoke->GetIntrinsic()) { |
| 230 | case Intrinsics::kMathAbsDouble: |
| 231 | case Intrinsics::kMathAbsFloat: |
| 232 | case Intrinsics::kMathMaxDoubleDouble: |
| 233 | case Intrinsics::kMathMaxFloatFloat: |
| 234 | case Intrinsics::kMathMinDoubleDouble: |
| 235 | case Intrinsics::kMathMinFloatFloat: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 236 | LOG(FATAL) << "Unreachable min/max/abs: intrinsics should have been lowered " |
| 237 | "to IR nodes by instruction simplifier"; |
| 238 | UNREACHABLE(); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 239 | case Intrinsics::kIntegerValueOf: |
| 240 | // This intrinsic can be call free if it loads the address of the boot image object. |
| 241 | // If we're compiling PIC, we need the address base for loading from .data.bimg.rel.ro. |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 242 | if (Runtime::Current()->UseJitCompilation()) { |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | FALLTHROUGH_INTENDED; |
Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 246 | case Intrinsics::kMathRoundFloat: |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 247 | // This intrinsic needs the constant area. |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 248 | if (!base_added) { |
| 249 | DCHECK(invoke_static_or_direct != nullptr); |
| 250 | DCHECK(!invoke_static_or_direct->HasCurrentMethodInput()); |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 251 | HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke); |
| 252 | invoke_static_or_direct->AddSpecialInput(method_address); |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 253 | } |
| 254 | break; |
| 255 | default: |
| 256 | break; |
| 257 | } |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 260 | CodeGeneratorX86* codegen_; |
| 261 | |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 262 | // The generated HX86ComputeBaseMethodAddress in the entry block needed as an |
Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 263 | // input to the HX86LoadFromConstantTable instructions. Only set for |
| 264 | // graphs with reducible loops. |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 265 | HX86ComputeBaseMethodAddress* base_; |
| 266 | }; |
| 267 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 268 | bool PcRelativeFixups::Run() { |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 269 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 270 | visitor.VisitInsertionOrder(); |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 271 | visitor.MoveBaseIfNeeded(); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 272 | return true; |
Mark Mendell | 9499107 | 2015-10-06 14:58:32 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | } // namespace x86 |
| 276 | } // namespace art |