Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | #include "code_generator_x86_64.h" |
| 18 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method.h" |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 20 | #include "code_generator_utils.h" |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 21 | #include "compiled_method.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 22 | #include "entrypoints/quick/quick_entrypoints.h" |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 23 | #include "gc/accounting/card_table.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 24 | #include "intrinsics.h" |
| 25 | #include "intrinsics_x86_64.h" |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 26 | #include "mirror/array-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 27 | #include "mirror/class-inl.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 28 | #include "mirror/object_reference.h" |
| 29 | #include "thread.h" |
| 30 | #include "utils/assembler.h" |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 31 | #include "utils/stack_checks.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 32 | #include "utils/x86_64/assembler_x86_64.h" |
| 33 | #include "utils/x86_64/managed_register_x86_64.h" |
| 34 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 35 | namespace art { |
| 36 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 37 | template<class MirrorType> |
| 38 | class GcRoot; |
| 39 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 40 | namespace x86_64 { |
| 41 | |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 42 | static constexpr int kCurrentMethodStackOffset = 0; |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 43 | static constexpr Register kMethodRegisterArgument = RDI; |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 44 | // The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump |
| 45 | // table version generates 7 instructions and num_entries literals. Compare/jump sequence will |
| 46 | // generates less code/data with a small num_entries. |
| 47 | static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5; |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 48 | |
Nicolas Geoffray | 4597b5b | 2015-01-23 21:51:55 +0000 | [diff] [blame] | 49 | static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 }; |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 50 | static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 }; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 51 | |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 52 | static constexpr int kC2ConditionMask = 0x400; |
| 53 | |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 54 | // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. |
| 55 | #define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 56 | #define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value() |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 57 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 58 | class NullCheckSlowPathX86_64 : public SlowPathCode { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 59 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 60 | explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 61 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 62 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 63 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 64 | __ Bind(GetEntryLabel()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 65 | if (instruction_->CanThrowIntoCatchBlock()) { |
| 66 | // Live registers will be restored in the catch block if caught. |
| 67 | SaveLiveRegisters(codegen, instruction_->GetLocations()); |
| 68 | } |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 69 | x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 70 | instruction_, |
| 71 | instruction_->GetDexPc(), |
| 72 | this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 73 | CheckEntrypointTypes<kQuickThrowNullPointer, void, void>(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 76 | bool IsFatal() const OVERRIDE { return true; } |
| 77 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 78 | const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; } |
| 79 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 80 | private: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64); |
| 82 | }; |
| 83 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 84 | class DivZeroCheckSlowPathX86_64 : public SlowPathCode { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 85 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 86 | explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {} |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 87 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 88 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 89 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 90 | __ Bind(GetEntryLabel()); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 91 | x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 92 | CheckEntrypointTypes<kQuickThrowDivZero, void, void>(); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 95 | bool IsFatal() const OVERRIDE { return true; } |
| 96 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 97 | const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; } |
| 98 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 99 | private: |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 100 | DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64); |
| 101 | }; |
| 102 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 103 | class DivRemMinusOneSlowPathX86_64 : public SlowPathCode { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 104 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 105 | DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div) |
| 106 | : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {} |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 107 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 108 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 109 | __ Bind(GetEntryLabel()); |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 110 | if (type_ == Primitive::kPrimInt) { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 111 | if (is_div_) { |
| 112 | __ negl(cpu_reg_); |
| 113 | } else { |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 114 | __ xorl(cpu_reg_, cpu_reg_); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 117 | } else { |
| 118 | DCHECK_EQ(Primitive::kPrimLong, type_); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 119 | if (is_div_) { |
| 120 | __ negq(cpu_reg_); |
| 121 | } else { |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 122 | __ xorl(cpu_reg_, cpu_reg_); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 123 | } |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 124 | } |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 125 | __ jmp(GetExitLabel()); |
| 126 | } |
| 127 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 128 | const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; } |
| 129 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 130 | private: |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 131 | const CpuRegister cpu_reg_; |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 132 | const Primitive::Type type_; |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 133 | const bool is_div_; |
| 134 | DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 137 | class SuspendCheckSlowPathX86_64 : public SlowPathCode { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 138 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 139 | SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 140 | : SlowPathCode(instruction), successor_(successor) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 141 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 142 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 143 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 144 | __ Bind(GetEntryLabel()); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 145 | x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 146 | CheckEntrypointTypes<kQuickTestSuspend, void, void>(); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 147 | if (successor_ == nullptr) { |
| 148 | __ jmp(GetReturnLabel()); |
| 149 | } else { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 150 | __ jmp(x86_64_codegen->GetLabelOf(successor_)); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 151 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 154 | Label* GetReturnLabel() { |
| 155 | DCHECK(successor_ == nullptr); |
| 156 | return &return_label_; |
| 157 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 158 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 159 | HBasicBlock* GetSuccessor() const { |
| 160 | return successor_; |
| 161 | } |
| 162 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 163 | const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; } |
| 164 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 165 | private: |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 166 | HBasicBlock* const successor_; |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 167 | Label return_label_; |
| 168 | |
| 169 | DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64); |
| 170 | }; |
| 171 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 172 | class BoundsCheckSlowPathX86_64 : public SlowPathCode { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 173 | public: |
Serban Constantinescu | 5a6cc49 | 2015-08-13 15:20:25 +0100 | [diff] [blame] | 174 | explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 175 | : SlowPathCode(instruction) {} |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 176 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 177 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Serban Constantinescu | 5a6cc49 | 2015-08-13 15:20:25 +0100 | [diff] [blame] | 178 | LocationSummary* locations = instruction_->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 179 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 180 | __ Bind(GetEntryLabel()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 181 | if (instruction_->CanThrowIntoCatchBlock()) { |
| 182 | // Live registers will be restored in the catch block if caught. |
| 183 | SaveLiveRegisters(codegen, instruction_->GetLocations()); |
| 184 | } |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 185 | // Are we using an array length from memory? |
| 186 | HInstruction* array_length = instruction_->InputAt(1); |
| 187 | Location length_loc = locations->InAt(1); |
| 188 | InvokeRuntimeCallingConvention calling_convention; |
| 189 | if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) { |
| 190 | // Load the array length into our temporary. |
| 191 | uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength()); |
| 192 | Location array_loc = array_length->GetLocations()->InAt(0); |
| 193 | Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset); |
| 194 | length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1)); |
| 195 | // Check for conflicts with index. |
| 196 | if (length_loc.Equals(locations->InAt(0))) { |
| 197 | // We know we aren't using parameter 2. |
| 198 | length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2)); |
| 199 | } |
| 200 | __ movl(length_loc.AsRegister<CpuRegister>(), array_len); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 201 | if (mirror::kUseStringCompression) { |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 202 | __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 203 | } |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 206 | // We're moving two locations to locations that could overlap, so we need a parallel |
| 207 | // move resolver. |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 208 | codegen->EmitParallelMoves( |
Serban Constantinescu | 5a6cc49 | 2015-08-13 15:20:25 +0100 | [diff] [blame] | 209 | locations->InAt(0), |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 210 | Location::RegisterLocation(calling_convention.GetRegisterAt(0)), |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 211 | Primitive::kPrimInt, |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 212 | length_loc, |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 213 | Location::RegisterLocation(calling_convention.GetRegisterAt(1)), |
| 214 | Primitive::kPrimInt); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 215 | QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt() |
| 216 | ? kQuickThrowStringBounds |
| 217 | : kQuickThrowArrayBounds; |
| 218 | x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 219 | CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>(); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 220 | CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 223 | bool IsFatal() const OVERRIDE { return true; } |
| 224 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 225 | const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; } |
| 226 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 227 | private: |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 228 | DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64); |
| 229 | }; |
| 230 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 231 | class LoadClassSlowPathX86_64 : public SlowPathCode { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 232 | public: |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 233 | LoadClassSlowPathX86_64(HLoadClass* cls, |
| 234 | HInstruction* at, |
| 235 | uint32_t dex_pc, |
| 236 | bool do_clinit) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 237 | : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 238 | DCHECK(at->IsLoadClass() || at->IsClinitCheck()); |
| 239 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 240 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 241 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 242 | LocationSummary* locations = at_->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 243 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 244 | __ Bind(GetEntryLabel()); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 245 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 246 | SaveLiveRegisters(codegen, locations); |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 247 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 248 | InvokeRuntimeCallingConvention calling_convention; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 249 | __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex())); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 250 | x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 251 | at_, |
| 252 | dex_pc_, |
| 253 | this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 254 | if (do_clinit_) { |
| 255 | CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>(); |
| 256 | } else { |
| 257 | CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>(); |
| 258 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 259 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 260 | Location out = locations->Out(); |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 261 | // Move the class to the desired location. |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 262 | if (out.IsValid()) { |
| 263 | DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg())); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 264 | x86_64_codegen->Move(out, Location::RegisterLocation(RAX)); |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 267 | RestoreLiveRegisters(codegen, locations); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 268 | __ jmp(GetExitLabel()); |
| 269 | } |
| 270 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 271 | const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; } |
| 272 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 273 | private: |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 274 | // The class this slow path will load. |
| 275 | HLoadClass* const cls_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 276 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 277 | // The instruction where this slow path is happening. |
| 278 | // (Might be the load class or an initialization check). |
| 279 | HInstruction* const at_; |
| 280 | |
| 281 | // The dex PC of `at_`. |
| 282 | const uint32_t dex_pc_; |
| 283 | |
| 284 | // Whether to initialize the class. |
| 285 | const bool do_clinit_; |
| 286 | |
| 287 | DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 288 | }; |
| 289 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 290 | class LoadStringSlowPathX86_64 : public SlowPathCode { |
| 291 | public: |
| 292 | explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {} |
| 293 | |
| 294 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 295 | LocationSummary* locations = instruction_->GetLocations(); |
| 296 | DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); |
| 297 | |
| 298 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
| 299 | __ Bind(GetEntryLabel()); |
| 300 | SaveLiveRegisters(codegen, locations); |
| 301 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 302 | const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex(); |
Vladimir Marko | 94ce9c2 | 2016-09-30 14:50:51 +0100 | [diff] [blame] | 303 | // Custom calling convention: RAX serves as both input and output. |
| 304 | __ movl(CpuRegister(RAX), Immediate(string_index)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 305 | x86_64_codegen->InvokeRuntime(kQuickResolveString, |
| 306 | instruction_, |
| 307 | instruction_->GetDexPc(), |
| 308 | this); |
| 309 | CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>(); |
| 310 | x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX)); |
| 311 | RestoreLiveRegisters(codegen, locations); |
| 312 | |
| 313 | // Store the resolved String to the BSS entry. |
| 314 | __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false), |
| 315 | locations->Out().AsRegister<CpuRegister>()); |
| 316 | Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString()); |
| 317 | __ Bind(fixup_label); |
| 318 | |
| 319 | __ jmp(GetExitLabel()); |
| 320 | } |
| 321 | |
| 322 | const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; } |
| 323 | |
| 324 | private: |
| 325 | DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64); |
| 326 | }; |
| 327 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 328 | class TypeCheckSlowPathX86_64 : public SlowPathCode { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 329 | public: |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 330 | TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 331 | : SlowPathCode(instruction), is_fatal_(is_fatal) {} |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 332 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 333 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 334 | LocationSummary* locations = instruction_->GetLocations(); |
Serban Constantinescu | 5a6cc49 | 2015-08-13 15:20:25 +0100 | [diff] [blame] | 335 | uint32_t dex_pc = instruction_->GetDexPc(); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 336 | DCHECK(instruction_->IsCheckCast() |
| 337 | || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 338 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 339 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 340 | __ Bind(GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 341 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 342 | if (!is_fatal_) { |
| 343 | SaveLiveRegisters(codegen, locations); |
| 344 | } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 345 | |
| 346 | // We're moving two locations to locations that could overlap, so we need a parallel |
| 347 | // move resolver. |
| 348 | InvokeRuntimeCallingConvention calling_convention; |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 349 | codegen->EmitParallelMoves(locations->InAt(0), |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 350 | Location::RegisterLocation(calling_convention.GetRegisterAt(0)), |
| 351 | Primitive::kPrimNot, |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 352 | locations->InAt(1), |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 353 | Location::RegisterLocation(calling_convention.GetRegisterAt(1)), |
| 354 | Primitive::kPrimNot); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 355 | if (instruction_->IsInstanceOf()) { |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 356 | x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this); |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 357 | CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>(); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 358 | } else { |
| 359 | DCHECK(instruction_->IsCheckCast()); |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 360 | x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this); |
| 361 | CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>(); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 362 | } |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 363 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 364 | if (!is_fatal_) { |
| 365 | if (instruction_->IsInstanceOf()) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 366 | x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX)); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 367 | } |
Nicolas Geoffray | 7537437 | 2015-09-17 17:12:19 +0000 | [diff] [blame] | 368 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 369 | RestoreLiveRegisters(codegen, locations); |
| 370 | __ jmp(GetExitLabel()); |
| 371 | } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 374 | const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; } |
| 375 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 376 | bool IsFatal() const OVERRIDE { return is_fatal_; } |
| 377 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 378 | private: |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 379 | const bool is_fatal_; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 380 | |
| 381 | DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64); |
| 382 | }; |
| 383 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 384 | class DeoptimizationSlowPathX86_64 : public SlowPathCode { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 385 | public: |
Aart Bik | 42249c3 | 2016-01-07 15:33:50 -0800 | [diff] [blame] | 386 | explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 387 | : SlowPathCode(instruction) {} |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 388 | |
| 389 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 390 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 391 | __ Bind(GetEntryLabel()); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 392 | x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 393 | CheckEntrypointTypes<kQuickDeoptimize, void, void>(); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Alexandre Rames | 9931f31 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 396 | const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; } |
| 397 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 398 | private: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 399 | DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64); |
| 400 | }; |
| 401 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 402 | class ArraySetSlowPathX86_64 : public SlowPathCode { |
| 403 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 404 | explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {} |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 405 | |
| 406 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 407 | LocationSummary* locations = instruction_->GetLocations(); |
| 408 | __ Bind(GetEntryLabel()); |
| 409 | SaveLiveRegisters(codegen, locations); |
| 410 | |
| 411 | InvokeRuntimeCallingConvention calling_convention; |
| 412 | HParallelMove parallel_move(codegen->GetGraph()->GetArena()); |
| 413 | parallel_move.AddMove( |
| 414 | locations->InAt(0), |
| 415 | Location::RegisterLocation(calling_convention.GetRegisterAt(0)), |
| 416 | Primitive::kPrimNot, |
| 417 | nullptr); |
| 418 | parallel_move.AddMove( |
| 419 | locations->InAt(1), |
| 420 | Location::RegisterLocation(calling_convention.GetRegisterAt(1)), |
| 421 | Primitive::kPrimInt, |
| 422 | nullptr); |
| 423 | parallel_move.AddMove( |
| 424 | locations->InAt(2), |
| 425 | Location::RegisterLocation(calling_convention.GetRegisterAt(2)), |
| 426 | Primitive::kPrimNot, |
| 427 | nullptr); |
| 428 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 429 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 430 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 431 | x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 432 | CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 433 | RestoreLiveRegisters(codegen, locations); |
| 434 | __ jmp(GetExitLabel()); |
| 435 | } |
| 436 | |
| 437 | const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; } |
| 438 | |
| 439 | private: |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 440 | DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64); |
| 441 | }; |
| 442 | |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 443 | // Slow path marking an object reference `ref` during a read |
| 444 | // barrier. The field `obj.field` in the object `obj` holding this |
| 445 | // reference does not get updated by this slow path after marking (see |
| 446 | // ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that). |
| 447 | // |
| 448 | // This means that after the execution of this slow path, `ref` will |
| 449 | // always be up-to-date, but `obj.field` may not; i.e., after the |
| 450 | // flip, `ref` will be a to-space reference, but `obj.field` will |
| 451 | // probably still be a from-space reference (unless it gets updated by |
| 452 | // another thread, or if another thread installed another object |
| 453 | // reference (different from `ref`) in `obj.field`). |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 454 | class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode { |
| 455 | public: |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 456 | ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, |
| 457 | Location ref, |
| 458 | bool unpoison_ref_before_marking) |
| 459 | : SlowPathCode(instruction), |
| 460 | ref_(ref), |
| 461 | unpoison_ref_before_marking_(unpoison_ref_before_marking) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 462 | DCHECK(kEmitCompilerReadBarrier); |
| 463 | } |
| 464 | |
| 465 | const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; } |
| 466 | |
| 467 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 468 | LocationSummary* locations = instruction_->GetLocations(); |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 469 | CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>(); |
| 470 | Register ref_reg = ref_cpu_reg.AsRegister(); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 471 | DCHECK(locations->CanCall()); |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 472 | DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 473 | DCHECK(instruction_->IsInstanceFieldGet() || |
| 474 | instruction_->IsStaticFieldGet() || |
| 475 | instruction_->IsArrayGet() || |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 476 | instruction_->IsArraySet() || |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 477 | instruction_->IsLoadClass() || |
| 478 | instruction_->IsLoadString() || |
| 479 | instruction_->IsInstanceOf() || |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 480 | instruction_->IsCheckCast() || |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 481 | (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) || |
| 482 | (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified())) |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 483 | << "Unexpected instruction in read barrier marking slow path: " |
| 484 | << instruction_->DebugName(); |
| 485 | |
| 486 | __ Bind(GetEntryLabel()); |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 487 | if (unpoison_ref_before_marking_) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 488 | // Object* ref = ref_addr->AsMirrorPtr() |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 489 | __ MaybeUnpoisonHeapReference(ref_cpu_reg); |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 490 | } |
Roland Levillain | 4359e61 | 2016-07-20 11:32:19 +0100 | [diff] [blame] | 491 | // No need to save live registers; it's taken care of by the |
| 492 | // entrypoint. Also, there is no need to update the stack mask, |
| 493 | // as this runtime call will not trigger a garbage collection. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 494 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 495 | DCHECK_NE(ref_reg, RSP); |
| 496 | DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg; |
Roland Levillain | 02b7580 | 2016-07-13 11:54:35 +0100 | [diff] [blame] | 497 | // "Compact" slow path, saving two moves. |
| 498 | // |
| 499 | // Instead of using the standard runtime calling convention (input |
| 500 | // and output in R0): |
| 501 | // |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 502 | // RDI <- ref |
Roland Levillain | 02b7580 | 2016-07-13 11:54:35 +0100 | [diff] [blame] | 503 | // RAX <- ReadBarrierMark(RDI) |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 504 | // ref <- RAX |
Roland Levillain | 02b7580 | 2016-07-13 11:54:35 +0100 | [diff] [blame] | 505 | // |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 506 | // we just use rX (the register containing `ref`) as input and output |
Roland Levillain | 02b7580 | 2016-07-13 11:54:35 +0100 | [diff] [blame] | 507 | // of a dedicated entrypoint: |
| 508 | // |
| 509 | // rX <- ReadBarrierMarkRegX(rX) |
| 510 | // |
| 511 | int32_t entry_point_offset = |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 512 | CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg); |
Roland Levillain | dec8f63 | 2016-07-22 17:10:06 +0100 | [diff] [blame] | 513 | // This runtime call does not require a stack map. |
| 514 | x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 515 | __ jmp(GetExitLabel()); |
| 516 | } |
| 517 | |
| 518 | private: |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 519 | // The location (register) of the marked object reference. |
| 520 | const Location ref_; |
| 521 | // Should the reference in `ref_` be unpoisoned prior to marking it? |
| 522 | const bool unpoison_ref_before_marking_; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 523 | |
| 524 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64); |
| 525 | }; |
| 526 | |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 527 | // Slow path marking an object reference `ref` during a read barrier, |
| 528 | // and if needed, atomically updating the field `obj.field` in the |
| 529 | // object `obj` holding this reference after marking (contrary to |
| 530 | // ReadBarrierMarkSlowPathX86_64 above, which never tries to update |
| 531 | // `obj.field`). |
| 532 | // |
| 533 | // This means that after the execution of this slow path, both `ref` |
| 534 | // and `obj.field` will be up-to-date; i.e., after the flip, both will |
| 535 | // hold the same to-space reference (unless another thread installed |
| 536 | // another object reference (different from `ref`) in `obj.field`). |
| 537 | class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode { |
| 538 | public: |
| 539 | ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction, |
| 540 | Location ref, |
| 541 | CpuRegister obj, |
| 542 | const Address& field_addr, |
| 543 | bool unpoison_ref_before_marking, |
| 544 | CpuRegister temp1, |
| 545 | CpuRegister temp2) |
| 546 | : SlowPathCode(instruction), |
| 547 | ref_(ref), |
| 548 | obj_(obj), |
| 549 | field_addr_(field_addr), |
| 550 | unpoison_ref_before_marking_(unpoison_ref_before_marking), |
| 551 | temp1_(temp1), |
| 552 | temp2_(temp2) { |
| 553 | DCHECK(kEmitCompilerReadBarrier); |
| 554 | } |
| 555 | |
| 556 | const char* GetDescription() const OVERRIDE { |
| 557 | return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64"; |
| 558 | } |
| 559 | |
| 560 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 561 | LocationSummary* locations = instruction_->GetLocations(); |
| 562 | CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>(); |
| 563 | Register ref_reg = ref_cpu_reg.AsRegister(); |
| 564 | DCHECK(locations->CanCall()); |
| 565 | DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg; |
| 566 | // This slow path is only used by the UnsafeCASObject intrinsic. |
| 567 | DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified())) |
| 568 | << "Unexpected instruction in read barrier marking and field updating slow path: " |
| 569 | << instruction_->DebugName(); |
| 570 | DCHECK(instruction_->GetLocations()->Intrinsified()); |
| 571 | DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject); |
| 572 | |
| 573 | __ Bind(GetEntryLabel()); |
| 574 | if (unpoison_ref_before_marking_) { |
| 575 | // Object* ref = ref_addr->AsMirrorPtr() |
| 576 | __ MaybeUnpoisonHeapReference(ref_cpu_reg); |
| 577 | } |
| 578 | |
| 579 | // Save the old (unpoisoned) reference. |
| 580 | __ movl(temp1_, ref_cpu_reg); |
| 581 | |
| 582 | // No need to save live registers; it's taken care of by the |
| 583 | // entrypoint. Also, there is no need to update the stack mask, |
| 584 | // as this runtime call will not trigger a garbage collection. |
| 585 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
| 586 | DCHECK_NE(ref_reg, RSP); |
| 587 | DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg; |
| 588 | // "Compact" slow path, saving two moves. |
| 589 | // |
| 590 | // Instead of using the standard runtime calling convention (input |
| 591 | // and output in R0): |
| 592 | // |
| 593 | // RDI <- ref |
| 594 | // RAX <- ReadBarrierMark(RDI) |
| 595 | // ref <- RAX |
| 596 | // |
| 597 | // we just use rX (the register containing `ref`) as input and output |
| 598 | // of a dedicated entrypoint: |
| 599 | // |
| 600 | // rX <- ReadBarrierMarkRegX(rX) |
| 601 | // |
| 602 | int32_t entry_point_offset = |
| 603 | CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg); |
| 604 | // This runtime call does not require a stack map. |
| 605 | x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this); |
| 606 | |
| 607 | // If the new reference is different from the old reference, |
| 608 | // update the field in the holder (`*field_addr`). |
| 609 | // |
| 610 | // Note that this field could also hold a different object, if |
| 611 | // another thread had concurrently changed it. In that case, the |
| 612 | // LOCK CMPXCHGL instruction in the compare-and-set (CAS) |
| 613 | // operation below would abort the CAS, leaving the field as-is. |
| 614 | NearLabel done; |
| 615 | __ cmpl(temp1_, ref_cpu_reg); |
| 616 | __ j(kEqual, &done); |
| 617 | |
| 618 | // Update the the holder's field atomically. This may fail if |
| 619 | // mutator updates before us, but it's OK. This is achived |
| 620 | // using a strong compare-and-set (CAS) operation with relaxed |
| 621 | // memory synchronization ordering, where the expected value is |
| 622 | // the old reference and the desired value is the new reference. |
| 623 | // This operation is implemented with a 32-bit LOCK CMPXLCHG |
| 624 | // instruction, which requires the expected value (the old |
| 625 | // reference) to be in EAX. Save RAX beforehand, and move the |
| 626 | // expected value (stored in `temp1_`) into EAX. |
| 627 | __ movq(temp2_, CpuRegister(RAX)); |
| 628 | __ movl(CpuRegister(RAX), temp1_); |
| 629 | |
| 630 | // Convenience aliases. |
| 631 | CpuRegister base = obj_; |
| 632 | CpuRegister expected = CpuRegister(RAX); |
| 633 | CpuRegister value = ref_cpu_reg; |
| 634 | |
| 635 | bool base_equals_value = (base.AsRegister() == value.AsRegister()); |
| 636 | Register value_reg = ref_reg; |
| 637 | if (kPoisonHeapReferences) { |
| 638 | if (base_equals_value) { |
| 639 | // If `base` and `value` are the same register location, move |
| 640 | // `value_reg` to a temporary register. This way, poisoning |
| 641 | // `value_reg` won't invalidate `base`. |
| 642 | value_reg = temp1_.AsRegister(); |
| 643 | __ movl(CpuRegister(value_reg), base); |
| 644 | } |
| 645 | |
| 646 | // Check that the register allocator did not assign the location |
| 647 | // of `expected` (RAX) to `value` nor to `base`, so that heap |
| 648 | // poisoning (when enabled) works as intended below. |
| 649 | // - If `value` were equal to `expected`, both references would |
| 650 | // be poisoned twice, meaning they would not be poisoned at |
| 651 | // all, as heap poisoning uses address negation. |
| 652 | // - If `base` were equal to `expected`, poisoning `expected` |
| 653 | // would invalidate `base`. |
| 654 | DCHECK_NE(value_reg, expected.AsRegister()); |
| 655 | DCHECK_NE(base.AsRegister(), expected.AsRegister()); |
| 656 | |
| 657 | __ PoisonHeapReference(expected); |
| 658 | __ PoisonHeapReference(CpuRegister(value_reg)); |
| 659 | } |
| 660 | |
| 661 | __ LockCmpxchgl(field_addr_, CpuRegister(value_reg)); |
| 662 | |
| 663 | // If heap poisoning is enabled, we need to unpoison the values |
| 664 | // that were poisoned earlier. |
| 665 | if (kPoisonHeapReferences) { |
| 666 | if (base_equals_value) { |
| 667 | // `value_reg` has been moved to a temporary register, no need |
| 668 | // to unpoison it. |
| 669 | } else { |
| 670 | __ UnpoisonHeapReference(CpuRegister(value_reg)); |
| 671 | } |
| 672 | // No need to unpoison `expected` (RAX), as it is be overwritten below. |
| 673 | } |
| 674 | |
| 675 | // Restore RAX. |
| 676 | __ movq(CpuRegister(RAX), temp2_); |
| 677 | |
| 678 | __ Bind(&done); |
| 679 | __ jmp(GetExitLabel()); |
| 680 | } |
| 681 | |
| 682 | private: |
| 683 | // The location (register) of the marked object reference. |
| 684 | const Location ref_; |
| 685 | // The register containing the object holding the marked object reference field. |
| 686 | const CpuRegister obj_; |
| 687 | // The address of the marked reference field. The base of this address must be `obj_`. |
| 688 | const Address field_addr_; |
| 689 | |
| 690 | // Should the reference in `ref_` be unpoisoned prior to marking it? |
| 691 | const bool unpoison_ref_before_marking_; |
| 692 | |
| 693 | const CpuRegister temp1_; |
| 694 | const CpuRegister temp2_; |
| 695 | |
| 696 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64); |
| 697 | }; |
| 698 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 699 | // Slow path generating a read barrier for a heap reference. |
| 700 | class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode { |
| 701 | public: |
| 702 | ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction, |
| 703 | Location out, |
| 704 | Location ref, |
| 705 | Location obj, |
| 706 | uint32_t offset, |
| 707 | Location index) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 708 | : SlowPathCode(instruction), |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 709 | out_(out), |
| 710 | ref_(ref), |
| 711 | obj_(obj), |
| 712 | offset_(offset), |
| 713 | index_(index) { |
| 714 | DCHECK(kEmitCompilerReadBarrier); |
| 715 | // If `obj` is equal to `out` or `ref`, it means the initial |
| 716 | // object has been overwritten by (or after) the heap object |
| 717 | // reference load to be instrumented, e.g.: |
| 718 | // |
| 719 | // __ movl(out, Address(out, offset)); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 720 | // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 721 | // |
| 722 | // In that case, we have lost the information about the original |
| 723 | // object, and the emitted read barrier cannot work properly. |
| 724 | DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out; |
| 725 | DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref; |
| 726 | } |
| 727 | |
| 728 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 729 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
| 730 | LocationSummary* locations = instruction_->GetLocations(); |
| 731 | CpuRegister reg_out = out_.AsRegister<CpuRegister>(); |
| 732 | DCHECK(locations->CanCall()); |
| 733 | DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_; |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 734 | DCHECK(instruction_->IsInstanceFieldGet() || |
| 735 | instruction_->IsStaticFieldGet() || |
| 736 | instruction_->IsArrayGet() || |
| 737 | instruction_->IsInstanceOf() || |
| 738 | instruction_->IsCheckCast() || |
Roland Levillain | dec8f63 | 2016-07-22 17:10:06 +0100 | [diff] [blame] | 739 | (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified()) |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 740 | << "Unexpected instruction in read barrier for heap reference slow path: " |
| 741 | << instruction_->DebugName(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 742 | |
| 743 | __ Bind(GetEntryLabel()); |
| 744 | SaveLiveRegisters(codegen, locations); |
| 745 | |
| 746 | // We may have to change the index's value, but as `index_` is a |
| 747 | // constant member (like other "inputs" of this slow path), |
| 748 | // introduce a copy of it, `index`. |
| 749 | Location index = index_; |
| 750 | if (index_.IsValid()) { |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 751 | // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 752 | if (instruction_->IsArrayGet()) { |
| 753 | // Compute real offset and store it in index_. |
| 754 | Register index_reg = index_.AsRegister<CpuRegister>().AsRegister(); |
| 755 | DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg)); |
| 756 | if (codegen->IsCoreCalleeSaveRegister(index_reg)) { |
| 757 | // We are about to change the value of `index_reg` (see the |
| 758 | // calls to art::x86_64::X86_64Assembler::shll and |
| 759 | // art::x86_64::X86_64Assembler::AddImmediate below), but it |
| 760 | // has not been saved by the previous call to |
| 761 | // art::SlowPathCode::SaveLiveRegisters, as it is a |
| 762 | // callee-save register -- |
| 763 | // art::SlowPathCode::SaveLiveRegisters does not consider |
| 764 | // callee-save registers, as it has been designed with the |
| 765 | // assumption that callee-save registers are supposed to be |
| 766 | // handled by the called function. So, as a callee-save |
| 767 | // register, `index_reg` _would_ eventually be saved onto |
| 768 | // the stack, but it would be too late: we would have |
| 769 | // changed its value earlier. Therefore, we manually save |
| 770 | // it here into another freely available register, |
| 771 | // `free_reg`, chosen of course among the caller-save |
| 772 | // registers (as a callee-save `free_reg` register would |
| 773 | // exhibit the same problem). |
| 774 | // |
| 775 | // Note we could have requested a temporary register from |
| 776 | // the register allocator instead; but we prefer not to, as |
| 777 | // this is a slow path, and we know we can find a |
| 778 | // caller-save register that is available. |
| 779 | Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister(); |
| 780 | __ movl(CpuRegister(free_reg), CpuRegister(index_reg)); |
| 781 | index_reg = free_reg; |
| 782 | index = Location::RegisterLocation(index_reg); |
| 783 | } else { |
| 784 | // The initial register stored in `index_` has already been |
| 785 | // saved in the call to art::SlowPathCode::SaveLiveRegisters |
| 786 | // (as it is not a callee-save register), so we can freely |
| 787 | // use it. |
| 788 | } |
| 789 | // Shifting the index value contained in `index_reg` by the |
| 790 | // scale factor (2) cannot overflow in practice, as the |
| 791 | // runtime is unable to allocate object arrays with a size |
| 792 | // larger than 2^26 - 1 (that is, 2^28 - 4 bytes). |
| 793 | __ shll(CpuRegister(index_reg), Immediate(TIMES_4)); |
| 794 | static_assert( |
| 795 | sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t), |
| 796 | "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes."); |
| 797 | __ AddImmediate(CpuRegister(index_reg), Immediate(offset_)); |
| 798 | } else { |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 799 | // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile |
| 800 | // intrinsics, `index_` is not shifted by a scale factor of 2 |
| 801 | // (as in the case of ArrayGet), as it is actually an offset |
| 802 | // to an object field within an object. |
| 803 | DCHECK(instruction_->IsInvoke()) << instruction_->DebugName(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 804 | DCHECK(instruction_->GetLocations()->Intrinsified()); |
| 805 | DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) || |
| 806 | (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)) |
| 807 | << instruction_->AsInvoke()->GetIntrinsic(); |
| 808 | DCHECK_EQ(offset_, 0U); |
| 809 | DCHECK(index_.IsRegister()); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | // We're moving two or three locations to locations that could |
| 814 | // overlap, so we need a parallel move resolver. |
| 815 | InvokeRuntimeCallingConvention calling_convention; |
| 816 | HParallelMove parallel_move(codegen->GetGraph()->GetArena()); |
| 817 | parallel_move.AddMove(ref_, |
| 818 | Location::RegisterLocation(calling_convention.GetRegisterAt(0)), |
| 819 | Primitive::kPrimNot, |
| 820 | nullptr); |
| 821 | parallel_move.AddMove(obj_, |
| 822 | Location::RegisterLocation(calling_convention.GetRegisterAt(1)), |
| 823 | Primitive::kPrimNot, |
| 824 | nullptr); |
| 825 | if (index.IsValid()) { |
| 826 | parallel_move.AddMove(index, |
| 827 | Location::RegisterLocation(calling_convention.GetRegisterAt(2)), |
| 828 | Primitive::kPrimInt, |
| 829 | nullptr); |
| 830 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 831 | } else { |
| 832 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 833 | __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_)); |
| 834 | } |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 835 | x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 836 | instruction_, |
| 837 | instruction_->GetDexPc(), |
| 838 | this); |
| 839 | CheckEntrypointTypes< |
| 840 | kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>(); |
| 841 | x86_64_codegen->Move(out_, Location::RegisterLocation(RAX)); |
| 842 | |
| 843 | RestoreLiveRegisters(codegen, locations); |
| 844 | __ jmp(GetExitLabel()); |
| 845 | } |
| 846 | |
| 847 | const char* GetDescription() const OVERRIDE { |
| 848 | return "ReadBarrierForHeapReferenceSlowPathX86_64"; |
| 849 | } |
| 850 | |
| 851 | private: |
| 852 | CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) { |
| 853 | size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister()); |
| 854 | size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister()); |
| 855 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 856 | if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) { |
| 857 | return static_cast<CpuRegister>(i); |
| 858 | } |
| 859 | } |
| 860 | // We shall never fail to find a free caller-save register, as |
| 861 | // there are more than two core caller-save registers on x86-64 |
| 862 | // (meaning it is possible to find one which is different from |
| 863 | // `ref` and `obj`). |
| 864 | DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u); |
| 865 | LOG(FATAL) << "Could not find a free caller-save register"; |
| 866 | UNREACHABLE(); |
| 867 | } |
| 868 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 869 | const Location out_; |
| 870 | const Location ref_; |
| 871 | const Location obj_; |
| 872 | const uint32_t offset_; |
| 873 | // An additional location containing an index to an array. |
| 874 | // Only used for HArrayGet and the UnsafeGetObject & |
| 875 | // UnsafeGetObjectVolatile intrinsics. |
| 876 | const Location index_; |
| 877 | |
| 878 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64); |
| 879 | }; |
| 880 | |
| 881 | // Slow path generating a read barrier for a GC root. |
| 882 | class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode { |
| 883 | public: |
| 884 | ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root) |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 885 | : SlowPathCode(instruction), out_(out), root_(root) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 886 | DCHECK(kEmitCompilerReadBarrier); |
| 887 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 888 | |
| 889 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 890 | LocationSummary* locations = instruction_->GetLocations(); |
| 891 | DCHECK(locations->CanCall()); |
| 892 | DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg())); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 893 | DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString()) |
| 894 | << "Unexpected instruction in read barrier for GC root slow path: " |
| 895 | << instruction_->DebugName(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 896 | |
| 897 | __ Bind(GetEntryLabel()); |
| 898 | SaveLiveRegisters(codegen, locations); |
| 899 | |
| 900 | InvokeRuntimeCallingConvention calling_convention; |
| 901 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
| 902 | x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 903 | x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 904 | instruction_, |
| 905 | instruction_->GetDexPc(), |
| 906 | this); |
| 907 | CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>(); |
| 908 | x86_64_codegen->Move(out_, Location::RegisterLocation(RAX)); |
| 909 | |
| 910 | RestoreLiveRegisters(codegen, locations); |
| 911 | __ jmp(GetExitLabel()); |
| 912 | } |
| 913 | |
| 914 | const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; } |
| 915 | |
| 916 | private: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 917 | const Location out_; |
| 918 | const Location root_; |
| 919 | |
| 920 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64); |
| 921 | }; |
| 922 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 923 | #undef __ |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 924 | // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. |
| 925 | #define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 926 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 927 | inline Condition X86_64IntegerCondition(IfCondition cond) { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 928 | switch (cond) { |
| 929 | case kCondEQ: return kEqual; |
| 930 | case kCondNE: return kNotEqual; |
| 931 | case kCondLT: return kLess; |
| 932 | case kCondLE: return kLessEqual; |
| 933 | case kCondGT: return kGreater; |
| 934 | case kCondGE: return kGreaterEqual; |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 935 | case kCondB: return kBelow; |
| 936 | case kCondBE: return kBelowEqual; |
| 937 | case kCondA: return kAbove; |
| 938 | case kCondAE: return kAboveEqual; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 939 | } |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 940 | LOG(FATAL) << "Unreachable"; |
| 941 | UNREACHABLE(); |
| 942 | } |
| 943 | |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 944 | // Maps FP condition to x86_64 name. |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 945 | inline Condition X86_64FPCondition(IfCondition cond) { |
| 946 | switch (cond) { |
| 947 | case kCondEQ: return kEqual; |
| 948 | case kCondNE: return kNotEqual; |
| 949 | case kCondLT: return kBelow; |
| 950 | case kCondLE: return kBelowEqual; |
| 951 | case kCondGT: return kAbove; |
| 952 | case kCondGE: return kAboveEqual; |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 953 | default: break; // should not happen |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 954 | }; |
| 955 | LOG(FATAL) << "Unreachable"; |
| 956 | UNREACHABLE(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 959 | HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch( |
| 960 | const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info, |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 961 | HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) { |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 962 | switch (desired_dispatch_info.code_ptr_location) { |
| 963 | case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup: |
| 964 | case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect: |
| 965 | // For direct code, we actually prefer to call via the code pointer from ArtMethod*. |
| 966 | return HInvokeStaticOrDirect::DispatchInfo { |
| 967 | desired_dispatch_info.method_load_kind, |
| 968 | HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod, |
| 969 | desired_dispatch_info.method_load_data, |
| 970 | 0u |
| 971 | }; |
| 972 | default: |
| 973 | return desired_dispatch_info; |
| 974 | } |
| 975 | } |
| 976 | |
Serguei Katkov | 288c7a8 | 2016-05-16 11:53:15 +0600 | [diff] [blame] | 977 | Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke, |
| 978 | Location temp) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 979 | // All registers are assumed to be correctly set up. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 980 | Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp. |
| 981 | switch (invoke->GetMethodLoadKind()) { |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 982 | case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 983 | // temp = thread->string_init_entrypoint |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 984 | uint32_t offset = |
| 985 | GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value(); |
| 986 | __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true)); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 987 | break; |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 988 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 989 | case HInvokeStaticOrDirect::MethodLoadKind::kRecursive: |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 990 | callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 991 | break; |
| 992 | case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress: |
| 993 | __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress())); |
| 994 | break; |
| 995 | case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup: |
| 996 | __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder. |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 997 | method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, |
| 998 | invoke->GetTargetMethod().dex_method_index); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 999 | __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn. |
| 1000 | break; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1001 | case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1002 | __ movq(temp.AsRegister<CpuRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 1003 | Address::Absolute(kDummy32BitOffset, /* no_rip */ false)); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1004 | // Bind a new fixup label at the end of the "movl" insn. |
| 1005 | uint32_t offset = invoke->GetDexCacheArrayOffset(); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1006 | __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset)); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1007 | break; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1008 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1009 | case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: { |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 1010 | Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1011 | Register method_reg; |
| 1012 | CpuRegister reg = temp.AsRegister<CpuRegister>(); |
| 1013 | if (current_method.IsRegister()) { |
| 1014 | method_reg = current_method.AsRegister<Register>(); |
| 1015 | } else { |
| 1016 | DCHECK(invoke->GetLocations()->Intrinsified()); |
| 1017 | DCHECK(!current_method.IsValid()); |
| 1018 | method_reg = reg.AsRegister(); |
| 1019 | __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset)); |
| 1020 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1021 | // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1022 | __ movq(reg, |
| 1023 | Address(CpuRegister(method_reg), |
| 1024 | ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue())); |
Vladimir Marko | 40ecb12 | 2016-04-06 17:33:41 +0100 | [diff] [blame] | 1025 | // temp = temp[index_in_cache]; |
| 1026 | // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file. |
| 1027 | uint32_t index_in_cache = invoke->GetDexMethodIndex(); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1028 | __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache))); |
| 1029 | break; |
Vladimir Marko | 9b688a0 | 2015-05-06 14:12:42 +0100 | [diff] [blame] | 1030 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1031 | } |
Serguei Katkov | 288c7a8 | 2016-05-16 11:53:15 +0600 | [diff] [blame] | 1032 | return callee_method; |
| 1033 | } |
| 1034 | |
| 1035 | void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, |
| 1036 | Location temp) { |
| 1037 | // All registers are assumed to be correctly set up. |
| 1038 | Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1039 | |
| 1040 | switch (invoke->GetCodePtrLocation()) { |
| 1041 | case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf: |
| 1042 | __ call(&frame_entry_label_); |
| 1043 | break; |
| 1044 | case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1045 | relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, |
| 1046 | invoke->GetTargetMethod().dex_method_index); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1047 | Label* label = &relative_call_patches_.back().label; |
| 1048 | __ call(label); // Bind to the patch label, override at link time. |
| 1049 | __ Bind(label); // Bind the label at the end of the "call" insn. |
| 1050 | break; |
| 1051 | } |
| 1052 | case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup: |
| 1053 | case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect: |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1054 | // Filtered out by GetSupportedInvokeStaticOrDirectDispatch(). |
| 1055 | LOG(FATAL) << "Unsupported"; |
| 1056 | UNREACHABLE(); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1057 | case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod: |
| 1058 | // (callee_method + offset_of_quick_compiled_code)() |
| 1059 | __ call(Address(callee_method.AsRegister<CpuRegister>(), |
| 1060 | ArtMethod::EntryPointFromQuickCompiledCodeOffset( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1061 | kX86_64PointerSize).SizeValue())); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1062 | break; |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 1063 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1064 | |
| 1065 | DCHECK(!IsLeafMethod()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 1068 | void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) { |
| 1069 | CpuRegister temp = temp_in.AsRegister<CpuRegister>(); |
| 1070 | size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset( |
| 1071 | invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue(); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Use the calling convention instead of the location of the receiver, as |
| 1074 | // intrinsics may have put the receiver in a different register. In the intrinsics |
| 1075 | // slow path, the arguments have been moved to the right place, so here we are |
| 1076 | // guaranteed that the receiver is the first register of the calling convention. |
| 1077 | InvokeDexCallingConvention calling_convention; |
| 1078 | Register receiver = calling_convention.GetRegisterAt(0); |
| 1079 | |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 1080 | size_t class_offset = mirror::Object::ClassOffset().SizeValue(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1081 | // /* HeapReference<Class> */ temp = receiver->klass_ |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1082 | __ movl(temp, Address(CpuRegister(receiver), class_offset)); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 1083 | MaybeRecordImplicitNullCheck(invoke); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1084 | // Instead of simply (possibly) unpoisoning `temp` here, we should |
| 1085 | // emit a read barrier for the previous class reference load. |
| 1086 | // However this is not required in practice, as this is an |
| 1087 | // intermediate/temporary reference and because the current |
| 1088 | // concurrent copying collector keeps the from-space memory |
| 1089 | // intact/accessible until the end of the marking phase (the |
| 1090 | // concurrent copying collector may not in the future). |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 1091 | __ MaybeUnpoisonHeapReference(temp); |
| 1092 | // temp = temp->GetMethodAt(method_offset); |
| 1093 | __ movq(temp, Address(temp, method_offset)); |
| 1094 | // call temp->GetEntryPoint(); |
| 1095 | __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1096 | kX86_64PointerSize).SizeValue())); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1099 | void CodeGeneratorX86_64::RecordSimplePatch() { |
| 1100 | if (GetCompilerOptions().GetIncludePatchInformation()) { |
| 1101 | simple_patches_.emplace_back(); |
| 1102 | __ Bind(&simple_patches_.back()); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1106 | void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) { |
| 1107 | DCHECK(GetCompilerOptions().IsBootImage()); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1108 | string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex()); |
| 1109 | __ Bind(&string_patches_.back().label); |
| 1110 | } |
| 1111 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 1112 | void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) { |
| 1113 | type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex()); |
| 1114 | __ Bind(&type_patches_.back().label); |
| 1115 | } |
| 1116 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1117 | Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) { |
| 1118 | DCHECK(!GetCompilerOptions().IsBootImage()); |
| 1119 | string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex()); |
| 1120 | return &string_patches_.back().label; |
| 1121 | } |
| 1122 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1123 | Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file, |
| 1124 | uint32_t element_offset) { |
| 1125 | // Add a patch entry and return the label. |
| 1126 | pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset); |
| 1127 | return &pc_relative_dex_cache_patches_.back().label; |
| 1128 | } |
| 1129 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1130 | // The label points to the end of the "movl" or another instruction but the literal offset |
| 1131 | // for method patch needs to point to the embedded constant which occupies the last 4 bytes. |
| 1132 | constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u; |
| 1133 | |
| 1134 | template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)> |
| 1135 | inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches( |
| 1136 | const ArenaDeque<PatchInfo<Label>>& infos, |
| 1137 | ArenaVector<LinkerPatch>* linker_patches) { |
| 1138 | for (const PatchInfo<Label>& info : infos) { |
| 1139 | uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment; |
| 1140 | linker_patches->push_back( |
| 1141 | Factory(literal_offset, &info.dex_file, info.label.Position(), info.index)); |
| 1142 | } |
| 1143 | } |
| 1144 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1145 | void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) { |
| 1146 | DCHECK(linker_patches->empty()); |
| 1147 | size_t size = |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 1148 | method_patches_.size() + |
| 1149 | relative_call_patches_.size() + |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1150 | pc_relative_dex_cache_patches_.size() + |
| 1151 | simple_patches_.size() + |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 1152 | string_patches_.size() + |
| 1153 | type_patches_.size(); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1154 | linker_patches->reserve(size); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1155 | for (const PatchInfo<Label>& info : method_patches_) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 1156 | uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1157 | linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index)); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1158 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1159 | for (const PatchInfo<Label>& info : relative_call_patches_) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 1160 | uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1161 | linker_patches->push_back( |
| 1162 | LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index)); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1163 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1164 | EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_, |
| 1165 | linker_patches); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1166 | for (const Label& label : simple_patches_) { |
| 1167 | uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment; |
| 1168 | linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset)); |
| 1169 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1170 | if (!GetCompilerOptions().IsBootImage()) { |
| 1171 | EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches); |
| 1172 | } else { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1173 | // These are always PC-relative, see GetSupportedLoadStringKind(). |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1174 | EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1175 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1176 | // These are always PC-relative, see GetSupportedLoadClassKind(). |
| 1177 | EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1180 | void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 1181 | stream << Register(reg); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 1185 | stream << FloatRegister(reg); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1188 | size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) { |
| 1189 | __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id)); |
| 1190 | return kX86_64WordSize; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1193 | size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) { |
| 1194 | __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index)); |
| 1195 | return kX86_64WordSize; |
| 1196 | } |
| 1197 | |
| 1198 | size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) { |
| 1199 | __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id)); |
| 1200 | return kX86_64WordSize; |
| 1201 | } |
| 1202 | |
| 1203 | size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) { |
| 1204 | __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index)); |
| 1205 | return kX86_64WordSize; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 1208 | void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint, |
| 1209 | HInstruction* instruction, |
| 1210 | uint32_t dex_pc, |
| 1211 | SlowPathCode* slow_path) { |
Alexandre Rames | 91a6516 | 2016-09-19 13:54:30 +0100 | [diff] [blame] | 1212 | ValidateInvokeRuntime(entrypoint, instruction, slow_path); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1213 | GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value()); |
| 1214 | if (EntrypointRequiresStackMap(entrypoint)) { |
| 1215 | RecordPcInfo(instruction, dex_pc, slow_path); |
| 1216 | } |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 1217 | } |
| 1218 | |
Roland Levillain | dec8f63 | 2016-07-22 17:10:06 +0100 | [diff] [blame] | 1219 | void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset, |
| 1220 | HInstruction* instruction, |
| 1221 | SlowPathCode* slow_path) { |
| 1222 | ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1223 | GenerateInvokeRuntime(entry_point_offset); |
| 1224 | } |
| 1225 | |
| 1226 | void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) { |
Roland Levillain | dec8f63 | 2016-07-22 17:10:06 +0100 | [diff] [blame] | 1227 | __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true)); |
| 1228 | } |
| 1229 | |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1230 | static constexpr int kNumberOfCpuRegisterPairs = 0; |
Nicolas Geoffray | 4597b5b | 2015-01-23 21:51:55 +0000 | [diff] [blame] | 1231 | // Use a fake return address register to mimic Quick. |
| 1232 | static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 1233 | CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1234 | const X86_64InstructionSetFeatures& isa_features, |
| 1235 | const CompilerOptions& compiler_options, |
| 1236 | OptimizingCompilerStats* stats) |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 1237 | : CodeGenerator(graph, |
| 1238 | kNumberOfCpuRegisters, |
| 1239 | kNumberOfFloatRegisters, |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1240 | kNumberOfCpuRegisterPairs, |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 1241 | ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves), |
| 1242 | arraysize(kCoreCalleeSaves)) |
Nicolas Geoffray | 4597b5b | 2015-01-23 21:51:55 +0000 | [diff] [blame] | 1243 | | (1 << kFakeReturnRegister), |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 1244 | ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves), |
| 1245 | arraysize(kFpuCalleeSaves)), |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 1246 | compiler_options, |
| 1247 | stats), |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 1248 | block_labels_(nullptr), |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1249 | location_builder_(graph, this), |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 1250 | instruction_visitor_(graph, this), |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 1251 | move_resolver_(graph->GetArena(), this), |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 1252 | assembler_(graph->GetArena()), |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 1253 | isa_features_(isa_features), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 1254 | constant_area_start_(0), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 1255 | method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
| 1256 | relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 1257 | pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1258 | simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
| 1259 | string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 1260 | type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), |
Nicolas Geoffray | 3395fbc | 2016-11-14 12:40:52 +0000 | [diff] [blame] | 1261 | fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) { |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1262 | AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister)); |
| 1263 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1264 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 1265 | InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph, |
| 1266 | CodeGeneratorX86_64* codegen) |
Aart Bik | 42249c3 | 2016-01-07 15:33:50 -0800 | [diff] [blame] | 1267 | : InstructionCodeGenerator(graph, codegen), |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1268 | assembler_(codegen->GetAssembler()), |
| 1269 | codegen_(codegen) {} |
| 1270 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 1271 | void CodeGeneratorX86_64::SetupBlockedRegisters() const { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1272 | // Stack register is always reserved. |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 1273 | blocked_core_registers_[RSP] = true; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1274 | |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 1275 | // Block the register used as TMP. |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 1276 | blocked_core_registers_[TMP] = true; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1277 | } |
| 1278 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1279 | static dwarf::Reg DWARFReg(Register reg) { |
David Srbecky | 9d8606d | 2015-04-12 09:35:32 +0100 | [diff] [blame] | 1280 | return dwarf::Reg::X86_64Core(static_cast<int>(reg)); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1281 | } |
David Srbecky | 9d8606d | 2015-04-12 09:35:32 +0100 | [diff] [blame] | 1282 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1283 | static dwarf::Reg DWARFReg(FloatRegister reg) { |
David Srbecky | 9d8606d | 2015-04-12 09:35:32 +0100 | [diff] [blame] | 1284 | return dwarf::Reg::X86_64Fp(static_cast<int>(reg)); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1287 | void CodeGeneratorX86_64::GenerateFrameEntry() { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1288 | __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 1289 | __ Bind(&frame_entry_label_); |
Nicolas Geoffray | f6e206c | 2014-08-07 20:25:41 +0100 | [diff] [blame] | 1290 | bool skip_overflow_check = IsLeafMethod() |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 1291 | && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64); |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1292 | DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks()); |
Nicolas Geoffray | f6e206c | 2014-08-07 20:25:41 +0100 | [diff] [blame] | 1293 | |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1294 | if (!skip_overflow_check) { |
Nicolas Geoffray | f6e206c | 2014-08-07 20:25:41 +0100 | [diff] [blame] | 1295 | __ testq(CpuRegister(RAX), Address( |
| 1296 | CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64)))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1297 | RecordPcInfo(nullptr, 0); |
Nicolas Geoffray | f6e206c | 2014-08-07 20:25:41 +0100 | [diff] [blame] | 1298 | } |
Nicolas Geoffray | a26369a | 2015-01-22 08:46:05 +0000 | [diff] [blame] | 1299 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1300 | if (HasEmptyFrame()) { |
| 1301 | return; |
| 1302 | } |
| 1303 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 1304 | for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) { |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1305 | Register reg = kCoreCalleeSaves[i]; |
Nicolas Geoffray | 4597b5b | 2015-01-23 21:51:55 +0000 | [diff] [blame] | 1306 | if (allocated_registers_.ContainsCoreRegister(reg)) { |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1307 | __ pushq(CpuRegister(reg)); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1308 | __ cfi().AdjustCFAOffset(kX86_64WordSize); |
| 1309 | __ cfi().RelOffset(DWARFReg(reg), 0); |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } |
Nicolas Geoffray | f6e206c | 2014-08-07 20:25:41 +0100 | [diff] [blame] | 1312 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1313 | int adjust = GetFrameSize() - GetCoreSpillSize(); |
| 1314 | __ subq(CpuRegister(RSP), Immediate(adjust)); |
| 1315 | __ cfi().AdjustCFAOffset(adjust); |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1316 | uint32_t xmm_spill_location = GetFpuSpillStart(); |
| 1317 | size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize(); |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 1318 | |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1319 | for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) { |
| 1320 | if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 1321 | int offset = xmm_spill_location + (xmm_spill_slot_size * i); |
| 1322 | __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i])); |
| 1323 | __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset); |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1324 | } |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Nicolas Geoffray | 96eeb4e | 2016-10-12 22:03:31 +0100 | [diff] [blame] | 1327 | // Save the current method if we need it. Note that we do not |
| 1328 | // do this in HCurrentMethod, as the instruction might have been removed |
| 1329 | // in the SSA graph. |
| 1330 | if (RequiresCurrentMethod()) { |
| 1331 | __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset), |
| 1332 | CpuRegister(kMethodRegisterArgument)); |
| 1333 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | void CodeGeneratorX86_64::GenerateFrameExit() { |
David Srbecky | c34dc93 | 2015-04-12 09:27:43 +0100 | [diff] [blame] | 1337 | __ cfi().RememberState(); |
| 1338 | if (!HasEmptyFrame()) { |
| 1339 | uint32_t xmm_spill_location = GetFpuSpillStart(); |
| 1340 | size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize(); |
| 1341 | for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) { |
| 1342 | if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) { |
| 1343 | int offset = xmm_spill_location + (xmm_spill_slot_size * i); |
| 1344 | __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset)); |
| 1345 | __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i])); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | int adjust = GetFrameSize() - GetCoreSpillSize(); |
| 1350 | __ addq(CpuRegister(RSP), Immediate(adjust)); |
| 1351 | __ cfi().AdjustCFAOffset(-adjust); |
| 1352 | |
| 1353 | for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) { |
| 1354 | Register reg = kCoreCalleeSaves[i]; |
| 1355 | if (allocated_registers_.ContainsCoreRegister(reg)) { |
| 1356 | __ popq(CpuRegister(reg)); |
| 1357 | __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize)); |
| 1358 | __ cfi().Restore(DWARFReg(reg)); |
| 1359 | } |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
David Srbecky | c34dc93 | 2015-04-12 09:27:43 +0100 | [diff] [blame] | 1362 | __ ret(); |
| 1363 | __ cfi().RestoreState(); |
| 1364 | __ cfi().DefCFAOffset(GetFrameSize()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 1367 | void CodeGeneratorX86_64::Bind(HBasicBlock* block) { |
| 1368 | __ Bind(GetLabelOf(block)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1371 | void CodeGeneratorX86_64::Move(Location destination, Location source) { |
| 1372 | if (source.Equals(destination)) { |
| 1373 | return; |
| 1374 | } |
| 1375 | if (destination.IsRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1376 | CpuRegister dest = destination.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1377 | if (source.IsRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1378 | __ movq(dest, source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1379 | } else if (source.IsFpuRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1380 | __ movd(dest, source.AsFpuRegister<XmmRegister>()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1381 | } else if (source.IsStackSlot()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1382 | __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex())); |
| 1383 | } else if (source.IsConstant()) { |
| 1384 | HConstant* constant = source.GetConstant(); |
| 1385 | if (constant->IsLongConstant()) { |
| 1386 | Load64BitValue(dest, constant->AsLongConstant()->GetValue()); |
| 1387 | } else { |
| 1388 | Load32BitValue(dest, GetInt32ValueOf(constant)); |
| 1389 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1390 | } else { |
| 1391 | DCHECK(source.IsDoubleStackSlot()); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1392 | __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1393 | } |
| 1394 | } else if (destination.IsFpuRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1395 | XmmRegister dest = destination.AsFpuRegister<XmmRegister>(); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1396 | if (source.IsRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1397 | __ movd(dest, source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1398 | } else if (source.IsFpuRegister()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1399 | __ movaps(dest, source.AsFpuRegister<XmmRegister>()); |
| 1400 | } else if (source.IsConstant()) { |
| 1401 | HConstant* constant = source.GetConstant(); |
| 1402 | int64_t value = CodeGenerator::GetInt64ValueOf(constant); |
| 1403 | if (constant->IsFloatConstant()) { |
| 1404 | Load32BitValue(dest, static_cast<int32_t>(value)); |
| 1405 | } else { |
| 1406 | Load64BitValue(dest, value); |
| 1407 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1408 | } else if (source.IsStackSlot()) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1409 | __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1410 | } else { |
| 1411 | DCHECK(source.IsDoubleStackSlot()); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1412 | __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1413 | } |
| 1414 | } else if (destination.IsStackSlot()) { |
| 1415 | if (source.IsRegister()) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1416 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 1417 | source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1418 | } else if (source.IsFpuRegister()) { |
| 1419 | __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 1420 | source.AsFpuRegister<XmmRegister>()); |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 1421 | } else if (source.IsConstant()) { |
| 1422 | HConstant* constant = source.GetConstant(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1423 | int32_t value = GetInt32ValueOf(constant); |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 1424 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1425 | } else { |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 1426 | DCHECK(source.IsStackSlot()) << source; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 1427 | __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 1428 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1429 | } |
| 1430 | } else { |
| 1431 | DCHECK(destination.IsDoubleStackSlot()); |
| 1432 | if (source.IsRegister()) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1433 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 1434 | source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1435 | } else if (source.IsFpuRegister()) { |
| 1436 | __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 1437 | source.AsFpuRegister<XmmRegister>()); |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 1438 | } else if (source.IsConstant()) { |
| 1439 | HConstant* constant = source.GetConstant(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1440 | DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant()); |
| 1441 | int64_t value = GetInt64ValueOf(constant); |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 1442 | Store64BitValueToStack(destination, value); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1443 | } else { |
| 1444 | DCHECK(source.IsDoubleStackSlot()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 1445 | __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 1446 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 1451 | void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) { |
| 1452 | DCHECK(location.IsRegister()); |
| 1453 | Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value)); |
| 1454 | } |
| 1455 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 1456 | void CodeGeneratorX86_64::MoveLocation( |
| 1457 | Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) { |
| 1458 | Move(dst, src); |
| 1459 | } |
| 1460 | |
| 1461 | void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) { |
| 1462 | if (location.IsRegister()) { |
| 1463 | locations->AddTemp(location); |
| 1464 | } else { |
| 1465 | UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location; |
| 1466 | } |
| 1467 | } |
| 1468 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1469 | void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1470 | DCHECK(!successor->IsExitBlock()); |
| 1471 | |
| 1472 | HBasicBlock* block = got->GetBlock(); |
| 1473 | HInstruction* previous = got->GetPrevious(); |
| 1474 | |
| 1475 | HLoopInformation* info = block->GetLoopInformation(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1476 | if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1477 | GenerateSuspendCheck(info->GetSuspendCheck(), successor); |
| 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) { |
| 1482 | GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr); |
| 1483 | } |
| 1484 | if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1485 | __ jmp(codegen_->GetLabelOf(successor)); |
| 1486 | } |
| 1487 | } |
| 1488 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1489 | void LocationsBuilderX86_64::VisitGoto(HGoto* got) { |
| 1490 | got->SetLocations(nullptr); |
| 1491 | } |
| 1492 | |
| 1493 | void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) { |
| 1494 | HandleGoto(got, got->GetSuccessor()); |
| 1495 | } |
| 1496 | |
| 1497 | void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) { |
| 1498 | try_boundary->SetLocations(nullptr); |
| 1499 | } |
| 1500 | |
| 1501 | void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) { |
| 1502 | HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor(); |
| 1503 | if (!successor->IsExitBlock()) { |
| 1504 | HandleGoto(try_boundary, successor); |
| 1505 | } |
| 1506 | } |
| 1507 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1508 | void LocationsBuilderX86_64::VisitExit(HExit* exit) { |
| 1509 | exit->SetLocations(nullptr); |
| 1510 | } |
| 1511 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1512 | void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame] | 1515 | template<class LabelType> |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1516 | void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond, |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame] | 1517 | LabelType* true_label, |
| 1518 | LabelType* false_label) { |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1519 | if (cond->IsFPConditionTrueIfNaN()) { |
| 1520 | __ j(kUnordered, true_label); |
| 1521 | } else if (cond->IsFPConditionFalseIfNaN()) { |
| 1522 | __ j(kUnordered, false_label); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1523 | } |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1524 | __ j(X86_64FPCondition(cond->GetCondition()), true_label); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1527 | void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1528 | LocationSummary* locations = condition->GetLocations(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1529 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1530 | Location left = locations->InAt(0); |
| 1531 | Location right = locations->InAt(1); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1532 | Primitive::Type type = condition->InputAt(0)->GetType(); |
| 1533 | switch (type) { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1534 | case Primitive::kPrimBoolean: |
| 1535 | case Primitive::kPrimByte: |
| 1536 | case Primitive::kPrimChar: |
| 1537 | case Primitive::kPrimShort: |
| 1538 | case Primitive::kPrimInt: |
| 1539 | case Primitive::kPrimNot: { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1540 | codegen_->GenerateIntCompare(left, right); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1541 | break; |
| 1542 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1543 | case Primitive::kPrimLong: { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1544 | codegen_->GenerateLongCompare(left, right); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1545 | break; |
| 1546 | } |
| 1547 | case Primitive::kPrimFloat: { |
| 1548 | if (right.IsFpuRegister()) { |
| 1549 | __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>()); |
| 1550 | } else if (right.IsConstant()) { |
| 1551 | __ ucomiss(left.AsFpuRegister<XmmRegister>(), |
| 1552 | codegen_->LiteralFloatAddress( |
| 1553 | right.GetConstant()->AsFloatConstant()->GetValue())); |
| 1554 | } else { |
| 1555 | DCHECK(right.IsStackSlot()); |
| 1556 | __ ucomiss(left.AsFpuRegister<XmmRegister>(), |
| 1557 | Address(CpuRegister(RSP), right.GetStackIndex())); |
| 1558 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1559 | break; |
| 1560 | } |
| 1561 | case Primitive::kPrimDouble: { |
| 1562 | if (right.IsFpuRegister()) { |
| 1563 | __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>()); |
| 1564 | } else if (right.IsConstant()) { |
| 1565 | __ ucomisd(left.AsFpuRegister<XmmRegister>(), |
| 1566 | codegen_->LiteralDoubleAddress( |
| 1567 | right.GetConstant()->AsDoubleConstant()->GetValue())); |
| 1568 | } else { |
| 1569 | DCHECK(right.IsDoubleStackSlot()); |
| 1570 | __ ucomisd(left.AsFpuRegister<XmmRegister>(), |
| 1571 | Address(CpuRegister(RSP), right.GetStackIndex())); |
| 1572 | } |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1573 | break; |
| 1574 | } |
| 1575 | default: |
| 1576 | LOG(FATAL) << "Unexpected condition type " << type; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | template<class LabelType> |
| 1581 | void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition, |
| 1582 | LabelType* true_target_in, |
| 1583 | LabelType* false_target_in) { |
| 1584 | // Generated branching requires both targets to be explicit. If either of the |
| 1585 | // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead. |
| 1586 | LabelType fallthrough_target; |
| 1587 | LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in; |
| 1588 | LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in; |
| 1589 | |
| 1590 | // Generate the comparison to set the CC. |
| 1591 | GenerateCompareTest(condition); |
| 1592 | |
| 1593 | // Now generate the correct jump(s). |
| 1594 | Primitive::Type type = condition->InputAt(0)->GetType(); |
| 1595 | switch (type) { |
| 1596 | case Primitive::kPrimLong: { |
| 1597 | __ j(X86_64IntegerCondition(condition->GetCondition()), true_target); |
| 1598 | break; |
| 1599 | } |
| 1600 | case Primitive::kPrimFloat: { |
| 1601 | GenerateFPJumps(condition, true_target, false_target); |
| 1602 | break; |
| 1603 | } |
| 1604 | case Primitive::kPrimDouble: { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1605 | GenerateFPJumps(condition, true_target, false_target); |
| 1606 | break; |
| 1607 | } |
| 1608 | default: |
| 1609 | LOG(FATAL) << "Unexpected condition type " << type; |
| 1610 | } |
| 1611 | |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1612 | if (false_target != &fallthrough_target) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1613 | __ jmp(false_target); |
| 1614 | } |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (fallthrough_target.IsLinked()) { |
| 1617 | __ Bind(&fallthrough_target); |
| 1618 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1619 | } |
| 1620 | |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1621 | static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) { |
| 1622 | // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS |
| 1623 | // are set only strictly before `branch`. We can't use the eflags on long |
| 1624 | // conditions if they are materialized due to the complex branching. |
| 1625 | return cond->IsCondition() && |
| 1626 | cond->GetNext() == branch && |
| 1627 | !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType()); |
| 1628 | } |
| 1629 | |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame] | 1630 | template<class LabelType> |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1631 | void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction, |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1632 | size_t condition_input_index, |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame] | 1633 | LabelType* true_target, |
| 1634 | LabelType* false_target) { |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1635 | HInstruction* cond = instruction->InputAt(condition_input_index); |
| 1636 | |
| 1637 | if (true_target == nullptr && false_target == nullptr) { |
| 1638 | // Nothing to do. The code always falls through. |
| 1639 | return; |
| 1640 | } else if (cond->IsIntConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1641 | // Constant condition, statically compared against "true" (integer value 1). |
| 1642 | if (cond->AsIntConstant()->IsTrue()) { |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1643 | if (true_target != nullptr) { |
| 1644 | __ jmp(true_target); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1645 | } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1646 | } else { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1647 | DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue(); |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1648 | if (false_target != nullptr) { |
| 1649 | __ jmp(false_target); |
| 1650 | } |
| 1651 | } |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | // The following code generates these patterns: |
| 1656 | // (1) true_target == nullptr && false_target != nullptr |
| 1657 | // - opposite condition true => branch to false_target |
| 1658 | // (2) true_target != nullptr && false_target == nullptr |
| 1659 | // - condition true => branch to true_target |
| 1660 | // (3) true_target != nullptr && false_target != nullptr |
| 1661 | // - condition true => branch to true_target |
| 1662 | // - branch to false_target |
| 1663 | if (IsBooleanValueOrMaterializedCondition(cond)) { |
| 1664 | if (AreEflagsSetFrom(cond, instruction)) { |
| 1665 | if (true_target == nullptr) { |
| 1666 | __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target); |
| 1667 | } else { |
| 1668 | __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target); |
| 1669 | } |
| 1670 | } else { |
| 1671 | // Materialized condition, compare against 0. |
| 1672 | Location lhs = instruction->GetLocations()->InAt(condition_input_index); |
| 1673 | if (lhs.IsRegister()) { |
| 1674 | __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>()); |
| 1675 | } else { |
| 1676 | __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0)); |
| 1677 | } |
| 1678 | if (true_target == nullptr) { |
| 1679 | __ j(kEqual, false_target); |
| 1680 | } else { |
| 1681 | __ j(kNotEqual, true_target); |
| 1682 | } |
Roland Levillain | 3a3fd0f | 2014-10-10 13:56:31 +0100 | [diff] [blame] | 1683 | } |
| 1684 | } else { |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1685 | // Condition has not been materialized, use its inputs as the |
| 1686 | // comparison and its condition as the branch condition. |
Mark Mendell | b8b9769 | 2015-05-22 16:58:19 -0400 | [diff] [blame] | 1687 | HCondition* condition = cond->AsCondition(); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1688 | |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1689 | // If this is a long or FP comparison that has been folded into |
| 1690 | // the HCondition, generate the comparison directly. |
| 1691 | Primitive::Type type = condition->InputAt(0)->GetType(); |
| 1692 | if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) { |
| 1693 | GenerateCompareTestAndBranch(condition, true_target, false_target); |
| 1694 | return; |
| 1695 | } |
| 1696 | |
| 1697 | Location lhs = condition->GetLocations()->InAt(0); |
| 1698 | Location rhs = condition->GetLocations()->InAt(1); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1699 | codegen_->GenerateIntCompare(lhs, rhs); |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1700 | if (true_target == nullptr) { |
| 1701 | __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target); |
| 1702 | } else { |
Mark Mendell | b8b9769 | 2015-05-22 16:58:19 -0400 | [diff] [blame] | 1703 | __ j(X86_64IntegerCondition(condition->GetCondition()), true_target); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1704 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1705 | } |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1706 | |
| 1707 | // If neither branch falls through (case 3), the conditional branch to `true_target` |
| 1708 | // was already emitted (case 2) and we need to emit a jump to `false_target`. |
| 1709 | if (true_target != nullptr && false_target != nullptr) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1710 | __ jmp(false_target); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1714 | void LocationsBuilderX86_64::VisitIf(HIf* if_instr) { |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1715 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
| 1716 | if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1717 | locations->SetInAt(0, Location::Any()); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) { |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1722 | HBasicBlock* true_successor = if_instr->IfTrueSuccessor(); |
| 1723 | HBasicBlock* false_successor = if_instr->IfFalseSuccessor(); |
| 1724 | Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ? |
| 1725 | nullptr : codegen_->GetLabelOf(true_successor); |
| 1726 | Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ? |
| 1727 | nullptr : codegen_->GetLabelOf(false_successor); |
| 1728 | GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) { |
| 1732 | LocationSummary* locations = new (GetGraph()->GetArena()) |
| 1733 | LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath); |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 1734 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 1735 | if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1736 | locations->SetInAt(0, Location::Any()); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) { |
Aart Bik | 42249c3 | 2016-01-07 15:33:50 -0800 | [diff] [blame] | 1741 | SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1742 | GenerateTestAndBranch<Label>(deoptimize, |
| 1743 | /* condition_input_index */ 0, |
| 1744 | slow_path->GetEntryLabel(), |
| 1745 | /* false_target */ nullptr); |
| 1746 | } |
| 1747 | |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1748 | static bool SelectCanUseCMOV(HSelect* select) { |
| 1749 | // There are no conditional move instructions for XMMs. |
| 1750 | if (Primitive::IsFloatingPointType(select->GetType())) { |
| 1751 | return false; |
| 1752 | } |
| 1753 | |
| 1754 | // A FP condition doesn't generate the single CC that we need. |
| 1755 | HInstruction* condition = select->GetCondition(); |
| 1756 | if (condition->IsCondition() && |
| 1757 | Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) { |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
| 1761 | // We can generate a CMOV for this Select. |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1765 | void LocationsBuilderX86_64::VisitSelect(HSelect* select) { |
| 1766 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select); |
| 1767 | if (Primitive::IsFloatingPointType(select->GetType())) { |
| 1768 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1769 | locations->SetInAt(1, Location::Any()); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1770 | } else { |
| 1771 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1772 | if (SelectCanUseCMOV(select)) { |
Mark Mendell | dee1b9a | 2016-02-12 14:36:51 -0500 | [diff] [blame] | 1773 | if (select->InputAt(1)->IsConstant()) { |
| 1774 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1775 | } else { |
| 1776 | locations->SetInAt(1, Location::Any()); |
| 1777 | } |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1778 | } else { |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1779 | locations->SetInAt(1, Location::Any()); |
| 1780 | } |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1781 | } |
| 1782 | if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) { |
| 1783 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1784 | } |
| 1785 | locations->SetOut(Location::SameAsFirstInput()); |
| 1786 | } |
| 1787 | |
| 1788 | void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) { |
| 1789 | LocationSummary* locations = select->GetLocations(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1790 | if (SelectCanUseCMOV(select)) { |
| 1791 | // If both the condition and the source types are integer, we can generate |
| 1792 | // a CMOV to implement Select. |
| 1793 | CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | dee1b9a | 2016-02-12 14:36:51 -0500 | [diff] [blame] | 1794 | Location value_true_loc = locations->InAt(1); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1795 | DCHECK(locations->InAt(0).Equals(locations->Out())); |
| 1796 | |
| 1797 | HInstruction* select_condition = select->GetCondition(); |
| 1798 | Condition cond = kNotEqual; |
| 1799 | |
| 1800 | // Figure out how to test the 'condition'. |
| 1801 | if (select_condition->IsCondition()) { |
| 1802 | HCondition* condition = select_condition->AsCondition(); |
| 1803 | if (!condition->IsEmittedAtUseSite()) { |
| 1804 | // This was a previously materialized condition. |
| 1805 | // Can we use the existing condition code? |
| 1806 | if (AreEflagsSetFrom(condition, select)) { |
| 1807 | // Materialization was the previous instruction. Condition codes are right. |
| 1808 | cond = X86_64IntegerCondition(condition->GetCondition()); |
| 1809 | } else { |
| 1810 | // No, we have to recreate the condition code. |
| 1811 | CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1812 | __ testl(cond_reg, cond_reg); |
| 1813 | } |
| 1814 | } else { |
| 1815 | GenerateCompareTest(condition); |
| 1816 | cond = X86_64IntegerCondition(condition->GetCondition()); |
| 1817 | } |
| 1818 | } else { |
| 1819 | // Must be a boolean condition, which needs to be compared to 0. |
| 1820 | CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1821 | __ testl(cond_reg, cond_reg); |
| 1822 | } |
| 1823 | |
| 1824 | // If the condition is true, overwrite the output, which already contains false. |
| 1825 | // Generate the correct sized CMOV. |
Mark Mendell | dee1b9a | 2016-02-12 14:36:51 -0500 | [diff] [blame] | 1826 | bool is_64_bit = Primitive::Is64BitType(select->GetType()); |
| 1827 | if (value_true_loc.IsRegister()) { |
| 1828 | __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit); |
| 1829 | } else { |
| 1830 | __ cmov(cond, |
| 1831 | value_false, |
| 1832 | Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit); |
| 1833 | } |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 1834 | } else { |
| 1835 | NearLabel false_target; |
| 1836 | GenerateTestAndBranch<NearLabel>(select, |
| 1837 | /* condition_input_index */ 2, |
| 1838 | /* true_target */ nullptr, |
| 1839 | &false_target); |
| 1840 | codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType()); |
| 1841 | __ Bind(&false_target); |
| 1842 | } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 1845 | void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) { |
| 1846 | new (GetGraph()->GetArena()) LocationSummary(info); |
| 1847 | } |
| 1848 | |
David Srbecky | d28f4a0 | 2016-03-14 17:14:24 +0000 | [diff] [blame] | 1849 | void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) { |
| 1850 | // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile. |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | void CodeGeneratorX86_64::GenerateNop() { |
| 1854 | __ nop(); |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1857 | void LocationsBuilderX86_64::HandleCondition(HCondition* cond) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1858 | LocationSummary* locations = |
Roland Levillain | 0d37cd0 | 2015-05-27 16:39:19 +0100 | [diff] [blame] | 1859 | new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1860 | // Handle the long/FP comparisons made in instruction simplification. |
| 1861 | switch (cond->InputAt(0)->GetType()) { |
| 1862 | case Primitive::kPrimLong: |
| 1863 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1864 | locations->SetInAt(1, Location::Any()); |
| 1865 | break; |
| 1866 | case Primitive::kPrimFloat: |
| 1867 | case Primitive::kPrimDouble: |
| 1868 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 1869 | locations->SetInAt(1, Location::Any()); |
| 1870 | break; |
| 1871 | default: |
| 1872 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1873 | locations->SetInAt(1, Location::Any()); |
| 1874 | break; |
| 1875 | } |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 1876 | if (!cond->IsEmittedAtUseSite()) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1877 | locations->SetOut(Location::RequiresRegister()); |
| 1878 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1881 | void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) { |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 1882 | if (cond->IsEmittedAtUseSite()) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1883 | return; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1884 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1885 | |
| 1886 | LocationSummary* locations = cond->GetLocations(); |
| 1887 | Location lhs = locations->InAt(0); |
| 1888 | Location rhs = locations->InAt(1); |
| 1889 | CpuRegister reg = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 152408f | 2015-12-31 12:28:50 -0500 | [diff] [blame] | 1890 | NearLabel true_label, false_label; |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1891 | |
| 1892 | switch (cond->InputAt(0)->GetType()) { |
| 1893 | default: |
| 1894 | // Integer case. |
| 1895 | |
| 1896 | // Clear output register: setcc only sets the low byte. |
| 1897 | __ xorl(reg, reg); |
| 1898 | |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1899 | codegen_->GenerateIntCompare(lhs, rhs); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1900 | __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1901 | return; |
| 1902 | case Primitive::kPrimLong: |
| 1903 | // Clear output register: setcc only sets the low byte. |
| 1904 | __ xorl(reg, reg); |
| 1905 | |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 1906 | codegen_->GenerateLongCompare(lhs, rhs); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1907 | __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1908 | return; |
| 1909 | case Primitive::kPrimFloat: { |
| 1910 | XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>(); |
| 1911 | if (rhs.IsConstant()) { |
| 1912 | float value = rhs.GetConstant()->AsFloatConstant()->GetValue(); |
| 1913 | __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value)); |
| 1914 | } else if (rhs.IsStackSlot()) { |
| 1915 | __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex())); |
| 1916 | } else { |
| 1917 | __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>()); |
| 1918 | } |
| 1919 | GenerateFPJumps(cond, &true_label, &false_label); |
| 1920 | break; |
| 1921 | } |
| 1922 | case Primitive::kPrimDouble: { |
| 1923 | XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>(); |
| 1924 | if (rhs.IsConstant()) { |
| 1925 | double value = rhs.GetConstant()->AsDoubleConstant()->GetValue(); |
| 1926 | __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value)); |
| 1927 | } else if (rhs.IsDoubleStackSlot()) { |
| 1928 | __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex())); |
| 1929 | } else { |
| 1930 | __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>()); |
| 1931 | } |
| 1932 | GenerateFPJumps(cond, &true_label, &false_label); |
| 1933 | break; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | // Convert the jumps into the result. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1938 | NearLabel done_label; |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1939 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1940 | // False case: result = 0. |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1941 | __ Bind(&false_label); |
| 1942 | __ xorl(reg, reg); |
| 1943 | __ jmp(&done_label); |
| 1944 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 1945 | // True case: result = 1. |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1946 | __ Bind(&true_label); |
| 1947 | __ movl(reg, Immediate(1)); |
| 1948 | __ Bind(&done_label); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | void LocationsBuilderX86_64::VisitEqual(HEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1952 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1956 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1960 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1964 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1968 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1972 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1976 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1980 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1984 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1988 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1992 | HandleCondition(comp); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1996 | HandleCondition(comp); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 1999 | void LocationsBuilderX86_64::VisitBelow(HBelow* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2000 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2004 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2008 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2012 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | void LocationsBuilderX86_64::VisitAbove(HAbove* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2016 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2020 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2024 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) { |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2028 | HandleCondition(comp); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2031 | void LocationsBuilderX86_64::VisitCompare(HCompare* compare) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2032 | LocationSummary* locations = |
| 2033 | new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2034 | switch (compare->InputAt(0)->GetType()) { |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 2035 | case Primitive::kPrimBoolean: |
| 2036 | case Primitive::kPrimByte: |
| 2037 | case Primitive::kPrimShort: |
| 2038 | case Primitive::kPrimChar: |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2039 | case Primitive::kPrimInt: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2040 | case Primitive::kPrimLong: { |
| 2041 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2042 | locations->SetInAt(1, Location::Any()); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2043 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 2044 | break; |
| 2045 | } |
| 2046 | case Primitive::kPrimFloat: |
| 2047 | case Primitive::kPrimDouble: { |
| 2048 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2049 | locations->SetInAt(1, Location::Any()); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2050 | locations->SetOut(Location::RequiresRegister()); |
| 2051 | break; |
| 2052 | } |
| 2053 | default: |
| 2054 | LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType(); |
| 2055 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2059 | LocationSummary* locations = compare->GetLocations(); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2060 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2061 | Location left = locations->InAt(0); |
| 2062 | Location right = locations->InAt(1); |
| 2063 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2064 | NearLabel less, greater, done; |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2065 | Primitive::Type type = compare->InputAt(0)->GetType(); |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2066 | Condition less_cond = kLess; |
| 2067 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2068 | switch (type) { |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 2069 | case Primitive::kPrimBoolean: |
| 2070 | case Primitive::kPrimByte: |
| 2071 | case Primitive::kPrimShort: |
| 2072 | case Primitive::kPrimChar: |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2073 | case Primitive::kPrimInt: { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 2074 | codegen_->GenerateIntCompare(left, right); |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2075 | break; |
| 2076 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2077 | case Primitive::kPrimLong: { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 2078 | codegen_->GenerateLongCompare(left, right); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2079 | break; |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2080 | } |
| 2081 | case Primitive::kPrimFloat: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2082 | XmmRegister left_reg = left.AsFpuRegister<XmmRegister>(); |
| 2083 | if (right.IsConstant()) { |
| 2084 | float value = right.GetConstant()->AsFloatConstant()->GetValue(); |
| 2085 | __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value)); |
| 2086 | } else if (right.IsStackSlot()) { |
| 2087 | __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex())); |
| 2088 | } else { |
| 2089 | __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>()); |
| 2090 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2091 | __ j(kUnordered, compare->IsGtBias() ? &greater : &less); |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2092 | less_cond = kBelow; // ucomis{s,d} sets CF |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2093 | break; |
| 2094 | } |
| 2095 | case Primitive::kPrimDouble: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2096 | XmmRegister left_reg = left.AsFpuRegister<XmmRegister>(); |
| 2097 | if (right.IsConstant()) { |
| 2098 | double value = right.GetConstant()->AsDoubleConstant()->GetValue(); |
| 2099 | __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value)); |
| 2100 | } else if (right.IsDoubleStackSlot()) { |
| 2101 | __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex())); |
| 2102 | } else { |
| 2103 | __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>()); |
| 2104 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2105 | __ j(kUnordered, compare->IsGtBias() ? &greater : &less); |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2106 | less_cond = kBelow; // ucomis{s,d} sets CF |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2107 | break; |
| 2108 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2109 | default: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2110 | LOG(FATAL) << "Unexpected compare type " << type; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2111 | } |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2112 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2113 | __ movl(out, Immediate(0)); |
Calin Juravle | 91debbc | 2014-11-26 19:01:09 +0000 | [diff] [blame] | 2114 | __ j(kEqual, &done); |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2115 | __ j(less_cond, &less); |
Calin Juravle | fd86124 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2116 | |
Calin Juravle | 91debbc | 2014-11-26 19:01:09 +0000 | [diff] [blame] | 2117 | __ Bind(&greater); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2118 | __ movl(out, Immediate(1)); |
| 2119 | __ jmp(&done); |
| 2120 | |
| 2121 | __ Bind(&less); |
| 2122 | __ movl(out, Immediate(-1)); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2123 | |
| 2124 | __ Bind(&done); |
| 2125 | } |
| 2126 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2127 | void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2128 | LocationSummary* locations = |
| 2129 | new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2130 | locations->SetOut(Location::ConstantLocation(constant)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2133 | void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) { |
Roland Levillain | 3a3fd0f | 2014-10-10 13:56:31 +0100 | [diff] [blame] | 2134 | // Will be generated at use site. |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2135 | } |
| 2136 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2137 | void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) { |
| 2138 | LocationSummary* locations = |
| 2139 | new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); |
| 2140 | locations->SetOut(Location::ConstantLocation(constant)); |
| 2141 | } |
| 2142 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2143 | void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2144 | // Will be generated at use site. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2147 | void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2148 | LocationSummary* locations = |
| 2149 | new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2150 | locations->SetOut(Location::ConstantLocation(constant)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2153 | void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) { |
Roland Levillain | 3a3fd0f | 2014-10-10 13:56:31 +0100 | [diff] [blame] | 2154 | // Will be generated at use site. |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2157 | void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) { |
| 2158 | LocationSummary* locations = |
| 2159 | new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); |
| 2160 | locations->SetOut(Location::ConstantLocation(constant)); |
| 2161 | } |
| 2162 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2163 | void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2164 | // Will be generated at use site. |
| 2165 | } |
| 2166 | |
| 2167 | void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) { |
| 2168 | LocationSummary* locations = |
| 2169 | new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); |
| 2170 | locations->SetOut(Location::ConstantLocation(constant)); |
| 2171 | } |
| 2172 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2173 | void InstructionCodeGeneratorX86_64::VisitDoubleConstant( |
| 2174 | HDoubleConstant* constant ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2175 | // Will be generated at use site. |
| 2176 | } |
| 2177 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 2178 | void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) { |
| 2179 | memory_barrier->SetLocations(nullptr); |
| 2180 | } |
| 2181 | |
| 2182 | void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2183 | codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind()); |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2186 | void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) { |
| 2187 | ret->SetLocations(nullptr); |
| 2188 | } |
| 2189 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2190 | void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2191 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | void LocationsBuilderX86_64::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2195 | LocationSummary* locations = |
| 2196 | new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2197 | switch (ret->InputAt(0)->GetType()) { |
| 2198 | case Primitive::kPrimBoolean: |
| 2199 | case Primitive::kPrimByte: |
| 2200 | case Primitive::kPrimChar: |
| 2201 | case Primitive::kPrimShort: |
| 2202 | case Primitive::kPrimInt: |
| 2203 | case Primitive::kPrimNot: |
| 2204 | case Primitive::kPrimLong: |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 2205 | locations->SetInAt(0, Location::RegisterLocation(RAX)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2206 | break; |
| 2207 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2208 | case Primitive::kPrimFloat: |
| 2209 | case Primitive::kPrimDouble: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2210 | locations->SetInAt(0, Location::FpuRegisterLocation(XMM0)); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2211 | break; |
| 2212 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2213 | default: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2214 | LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2215 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) { |
| 2219 | if (kIsDebugBuild) { |
| 2220 | switch (ret->InputAt(0)->GetType()) { |
| 2221 | case Primitive::kPrimBoolean: |
| 2222 | case Primitive::kPrimByte: |
| 2223 | case Primitive::kPrimChar: |
| 2224 | case Primitive::kPrimShort: |
| 2225 | case Primitive::kPrimInt: |
| 2226 | case Primitive::kPrimNot: |
| 2227 | case Primitive::kPrimLong: |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2228 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2229 | break; |
| 2230 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2231 | case Primitive::kPrimFloat: |
| 2232 | case Primitive::kPrimDouble: |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2233 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(), |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2234 | XMM0); |
| 2235 | break; |
| 2236 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2237 | default: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2238 | LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2242 | } |
| 2243 | |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 2244 | Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const { |
| 2245 | switch (type) { |
| 2246 | case Primitive::kPrimBoolean: |
| 2247 | case Primitive::kPrimByte: |
| 2248 | case Primitive::kPrimChar: |
| 2249 | case Primitive::kPrimShort: |
| 2250 | case Primitive::kPrimInt: |
| 2251 | case Primitive::kPrimNot: |
| 2252 | case Primitive::kPrimLong: |
| 2253 | return Location::RegisterLocation(RAX); |
| 2254 | |
| 2255 | case Primitive::kPrimVoid: |
| 2256 | return Location::NoLocation(); |
| 2257 | |
| 2258 | case Primitive::kPrimDouble: |
| 2259 | case Primitive::kPrimFloat: |
| 2260 | return Location::FpuRegisterLocation(XMM0); |
| 2261 | } |
Nicolas Geoffray | 0d1652e | 2015-06-03 12:12:19 +0100 | [diff] [blame] | 2262 | |
| 2263 | UNREACHABLE(); |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const { |
| 2267 | return Location::RegisterLocation(kMethodRegisterArgument); |
| 2268 | } |
| 2269 | |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 2270 | Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2271 | switch (type) { |
| 2272 | case Primitive::kPrimBoolean: |
| 2273 | case Primitive::kPrimByte: |
| 2274 | case Primitive::kPrimChar: |
| 2275 | case Primitive::kPrimShort: |
| 2276 | case Primitive::kPrimInt: |
| 2277 | case Primitive::kPrimNot: { |
| 2278 | uint32_t index = gp_index_++; |
| 2279 | stack_index_++; |
| 2280 | if (index < calling_convention.GetNumberOfRegisters()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 2281 | return Location::RegisterLocation(calling_convention.GetRegisterAt(index)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2282 | } else { |
| 2283 | return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1)); |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | case Primitive::kPrimLong: { |
| 2288 | uint32_t index = gp_index_; |
| 2289 | stack_index_ += 2; |
| 2290 | if (index < calling_convention.GetNumberOfRegisters()) { |
| 2291 | gp_index_ += 1; |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 2292 | return Location::RegisterLocation(calling_convention.GetRegisterAt(index)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2293 | } else { |
| 2294 | gp_index_ += 2; |
| 2295 | return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2)); |
| 2296 | } |
| 2297 | } |
| 2298 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2299 | case Primitive::kPrimFloat: { |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 2300 | uint32_t index = float_index_++; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2301 | stack_index_++; |
| 2302 | if (index < calling_convention.GetNumberOfFpuRegisters()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 2303 | return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index)); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2304 | } else { |
| 2305 | return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1)); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | case Primitive::kPrimDouble: { |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 2310 | uint32_t index = float_index_++; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2311 | stack_index_ += 2; |
| 2312 | if (index < calling_convention.GetNumberOfFpuRegisters()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 2313 | return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index)); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 2314 | } else { |
| 2315 | return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2)); |
| 2316 | } |
| 2317 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2318 | |
| 2319 | case Primitive::kPrimVoid: |
| 2320 | LOG(FATAL) << "Unexpected parameter type " << type; |
| 2321 | break; |
| 2322 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2323 | return Location::NoLocation(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 2326 | void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) { |
| 2327 | // The trampoline uses the same calling convention as dex calling conventions, |
| 2328 | // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain |
| 2329 | // the method_idx. |
| 2330 | HandleInvoke(invoke); |
| 2331 | } |
| 2332 | |
| 2333 | void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) { |
| 2334 | codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke); |
| 2335 | } |
| 2336 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2337 | void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 2338 | // Explicit clinit checks triggered by static invokes must have been pruned by |
| 2339 | // art::PrepareForRegisterAllocation. |
| 2340 | DCHECK(!invoke->IsStaticWithExplicitClinitCheck()); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2341 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 2342 | IntrinsicLocationsBuilderX86_64 intrinsic(codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2343 | if (intrinsic.TryDispatch(invoke)) { |
| 2344 | return; |
| 2345 | } |
| 2346 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2347 | HandleInvoke(invoke); |
| 2348 | } |
| 2349 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2350 | static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
| 2351 | if (invoke->GetLocations()->Intrinsified()) { |
| 2352 | IntrinsicCodeGeneratorX86_64 intrinsic(codegen); |
| 2353 | intrinsic.Dispatch(invoke); |
| 2354 | return true; |
| 2355 | } |
| 2356 | return false; |
| 2357 | } |
| 2358 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2359 | void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 2360 | // Explicit clinit checks triggered by static invokes must have been pruned by |
| 2361 | // art::PrepareForRegisterAllocation. |
| 2362 | DCHECK(!invoke->IsStaticWithExplicitClinitCheck()); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2363 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2364 | if (TryGenerateIntrinsicCode(invoke, codegen_)) { |
| 2365 | return; |
| 2366 | } |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2367 | |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 2368 | LocationSummary* locations = invoke->GetLocations(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2369 | codegen_->GenerateStaticOrDirectCall( |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 2370 | invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation()); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 2371 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2374 | void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) { |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 2375 | InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor; |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 2376 | CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2379 | void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 2380 | IntrinsicLocationsBuilderX86_64 intrinsic(codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2381 | if (intrinsic.TryDispatch(invoke)) { |
| 2382 | return; |
| 2383 | } |
| 2384 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2385 | HandleInvoke(invoke); |
| 2386 | } |
| 2387 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2388 | void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2389 | if (TryGenerateIntrinsicCode(invoke, codegen_)) { |
| 2390 | return; |
| 2391 | } |
| 2392 | |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 2393 | codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0)); |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 2394 | DCHECK(!codegen_->IsLeafMethod()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2395 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2398 | void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) { |
| 2399 | HandleInvoke(invoke); |
| 2400 | // Add the hidden argument. |
| 2401 | invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX)); |
| 2402 | } |
| 2403 | |
| 2404 | void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) { |
| 2405 | // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2406 | LocationSummary* locations = invoke->GetLocations(); |
| 2407 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2408 | CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>(); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2409 | Location receiver = locations->InAt(0); |
| 2410 | size_t class_offset = mirror::Object::ClassOffset().SizeValue(); |
| 2411 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2412 | // Set the hidden argument. This is safe to do this here, as RAX |
| 2413 | // won't be modified thereafter, before the `call` instruction. |
| 2414 | DCHECK_EQ(RAX, hidden_reg.AsRegister()); |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 2415 | codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex()); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2416 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2417 | if (receiver.IsStackSlot()) { |
| 2418 | __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex())); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2419 | // /* HeapReference<Class> */ temp = temp->klass_ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2420 | __ movl(temp, Address(temp, class_offset)); |
| 2421 | } else { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2422 | // /* HeapReference<Class> */ temp = receiver->klass_ |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2423 | __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset)); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2424 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2425 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2426 | // Instead of simply (possibly) unpoisoning `temp` here, we should |
| 2427 | // emit a read barrier for the previous class reference load. |
| 2428 | // However this is not required in practice, as this is an |
| 2429 | // intermediate/temporary reference and because the current |
| 2430 | // concurrent copying collector keeps the from-space memory |
| 2431 | // intact/accessible until the end of the marking phase (the |
| 2432 | // concurrent copying collector may not in the future). |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2433 | __ MaybeUnpoisonHeapReference(temp); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2434 | // temp = temp->GetAddressOfIMT() |
| 2435 | __ movq(temp, |
| 2436 | Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value())); |
| 2437 | // temp = temp->GetImtEntryAt(method_offset); |
| 2438 | uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement( |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 2439 | invoke->GetImtIndex(), kX86_64PointerSize)); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2440 | // temp = temp->GetImtEntryAt(method_offset); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2441 | __ movq(temp, Address(temp, method_offset)); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2442 | // call temp->GetEntryPoint(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2443 | __ call(Address( |
| 2444 | temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue())); |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2445 | |
| 2446 | DCHECK(!codegen_->IsLeafMethod()); |
| 2447 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 2448 | } |
| 2449 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2450 | void LocationsBuilderX86_64::VisitNeg(HNeg* neg) { |
| 2451 | LocationSummary* locations = |
| 2452 | new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall); |
| 2453 | switch (neg->GetResultType()) { |
| 2454 | case Primitive::kPrimInt: |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 2455 | case Primitive::kPrimLong: |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2456 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2457 | locations->SetOut(Location::SameAsFirstInput()); |
| 2458 | break; |
| 2459 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2460 | case Primitive::kPrimFloat: |
| 2461 | case Primitive::kPrimDouble: |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2462 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2463 | locations->SetOut(Location::SameAsFirstInput()); |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2464 | locations->AddTemp(Location::RequiresFpuRegister()); |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2465 | break; |
| 2466 | |
| 2467 | default: |
| 2468 | LOG(FATAL) << "Unexpected neg type " << neg->GetResultType(); |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) { |
| 2473 | LocationSummary* locations = neg->GetLocations(); |
| 2474 | Location out = locations->Out(); |
| 2475 | Location in = locations->InAt(0); |
| 2476 | switch (neg->GetResultType()) { |
| 2477 | case Primitive::kPrimInt: |
| 2478 | DCHECK(in.IsRegister()); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2479 | DCHECK(in.Equals(out)); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2480 | __ negl(out.AsRegister<CpuRegister>()); |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2481 | break; |
| 2482 | |
| 2483 | case Primitive::kPrimLong: |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 2484 | DCHECK(in.IsRegister()); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2485 | DCHECK(in.Equals(out)); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2486 | __ negq(out.AsRegister<CpuRegister>()); |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 2487 | break; |
| 2488 | |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2489 | case Primitive::kPrimFloat: { |
| 2490 | DCHECK(in.Equals(out)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2491 | XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2492 | // Implement float negation with an exclusive or with value |
| 2493 | // 0x80000000 (mask for bit 31, representing the sign of a |
| 2494 | // single-precision floating-point number). |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2495 | __ movss(mask, codegen_->LiteralInt32Address(0x80000000)); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2496 | __ xorps(out.AsFpuRegister<XmmRegister>(), mask); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2497 | break; |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2498 | } |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2499 | |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2500 | case Primitive::kPrimDouble: { |
| 2501 | DCHECK(in.Equals(out)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2502 | XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2503 | // Implement double negation with an exclusive or with value |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 2504 | // 0x8000000000000000 (mask for bit 63, representing the sign of |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2505 | // a double-precision floating-point number). |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2506 | __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000))); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2507 | __ xorpd(out.AsFpuRegister<XmmRegister>(), mask); |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2508 | break; |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 2509 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2510 | |
| 2511 | default: |
| 2512 | LOG(FATAL) << "Unexpected neg type " << neg->GetResultType(); |
| 2513 | } |
| 2514 | } |
| 2515 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2516 | void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) { |
| 2517 | LocationSummary* locations = |
| 2518 | new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall); |
| 2519 | Primitive::Type result_type = conversion->GetResultType(); |
| 2520 | Primitive::Type input_type = conversion->GetInputType(); |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 2521 | DCHECK_NE(result_type, input_type); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2522 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 2523 | // The Java language does not allow treating boolean as an integral type but |
| 2524 | // our bit representation makes it safe. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2525 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2526 | switch (result_type) { |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2527 | case Primitive::kPrimByte: |
| 2528 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2529 | case Primitive::kPrimLong: |
| 2530 | // Type conversion from long to byte is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2531 | case Primitive::kPrimBoolean: |
| 2532 | // Boolean input is a result of code transformations. |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2533 | case Primitive::kPrimShort: |
| 2534 | case Primitive::kPrimInt: |
| 2535 | case Primitive::kPrimChar: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2536 | // Processing a Dex `int-to-byte' instruction. |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2537 | locations->SetInAt(0, Location::Any()); |
| 2538 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 2539 | break; |
| 2540 | |
| 2541 | default: |
| 2542 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2543 | << " to " << result_type; |
| 2544 | } |
| 2545 | break; |
| 2546 | |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2547 | case Primitive::kPrimShort: |
| 2548 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2549 | case Primitive::kPrimLong: |
| 2550 | // Type conversion from long to short is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2551 | case Primitive::kPrimBoolean: |
| 2552 | // Boolean input is a result of code transformations. |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2553 | case Primitive::kPrimByte: |
| 2554 | case Primitive::kPrimInt: |
| 2555 | case Primitive::kPrimChar: |
| 2556 | // Processing a Dex `int-to-short' instruction. |
| 2557 | locations->SetInAt(0, Location::Any()); |
| 2558 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 2559 | break; |
| 2560 | |
| 2561 | default: |
| 2562 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2563 | << " to " << result_type; |
| 2564 | } |
| 2565 | break; |
| 2566 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2567 | case Primitive::kPrimInt: |
| 2568 | switch (input_type) { |
| 2569 | case Primitive::kPrimLong: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2570 | // Processing a Dex `long-to-int' instruction. |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2571 | locations->SetInAt(0, Location::Any()); |
| 2572 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 2573 | break; |
| 2574 | |
| 2575 | case Primitive::kPrimFloat: |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2576 | // Processing a Dex `float-to-int' instruction. |
| 2577 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 2578 | locations->SetOut(Location::RequiresRegister()); |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2579 | break; |
| 2580 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2581 | case Primitive::kPrimDouble: |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2582 | // Processing a Dex `double-to-int' instruction. |
| 2583 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 2584 | locations->SetOut(Location::RequiresRegister()); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2585 | break; |
| 2586 | |
| 2587 | default: |
| 2588 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2589 | << " to " << result_type; |
| 2590 | } |
| 2591 | break; |
| 2592 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2593 | case Primitive::kPrimLong: |
| 2594 | switch (input_type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2595 | case Primitive::kPrimBoolean: |
| 2596 | // Boolean input is a result of code transformations. |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2597 | case Primitive::kPrimByte: |
| 2598 | case Primitive::kPrimShort: |
| 2599 | case Primitive::kPrimInt: |
Roland Levillain | 666c732 | 2014-11-10 13:39:43 +0000 | [diff] [blame] | 2600 | case Primitive::kPrimChar: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2601 | // Processing a Dex `int-to-long' instruction. |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2602 | // TODO: We would benefit from a (to-be-implemented) |
| 2603 | // Location::RegisterOrStackSlot requirement for this input. |
| 2604 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2605 | locations->SetOut(Location::RequiresRegister()); |
| 2606 | break; |
| 2607 | |
| 2608 | case Primitive::kPrimFloat: |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2609 | // Processing a Dex `float-to-long' instruction. |
| 2610 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 2611 | locations->SetOut(Location::RequiresRegister()); |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2612 | break; |
| 2613 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2614 | case Primitive::kPrimDouble: |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2615 | // Processing a Dex `double-to-long' instruction. |
| 2616 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 2617 | locations->SetOut(Location::RequiresRegister()); |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | |
| 2620 | default: |
| 2621 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2622 | << " to " << result_type; |
| 2623 | } |
| 2624 | break; |
| 2625 | |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2626 | case Primitive::kPrimChar: |
| 2627 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2628 | case Primitive::kPrimLong: |
| 2629 | // Type conversion from long to char is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2630 | case Primitive::kPrimBoolean: |
| 2631 | // Boolean input is a result of code transformations. |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2632 | case Primitive::kPrimByte: |
| 2633 | case Primitive::kPrimShort: |
| 2634 | case Primitive::kPrimInt: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2635 | // Processing a Dex `int-to-char' instruction. |
| 2636 | locations->SetInAt(0, Location::Any()); |
| 2637 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 2638 | break; |
| 2639 | |
| 2640 | default: |
| 2641 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2642 | << " to " << result_type; |
| 2643 | } |
| 2644 | break; |
| 2645 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2646 | case Primitive::kPrimFloat: |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2647 | switch (input_type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2648 | case Primitive::kPrimBoolean: |
| 2649 | // Boolean input is a result of code transformations. |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2650 | case Primitive::kPrimByte: |
| 2651 | case Primitive::kPrimShort: |
| 2652 | case Primitive::kPrimInt: |
| 2653 | case Primitive::kPrimChar: |
| 2654 | // Processing a Dex `int-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2655 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2656 | locations->SetOut(Location::RequiresFpuRegister()); |
| 2657 | break; |
| 2658 | |
| 2659 | case Primitive::kPrimLong: |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2660 | // Processing a Dex `long-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2661 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2662 | locations->SetOut(Location::RequiresFpuRegister()); |
| 2663 | break; |
| 2664 | |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2665 | case Primitive::kPrimDouble: |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 2666 | // Processing a Dex `double-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2667 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 2668 | locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2669 | break; |
| 2670 | |
| 2671 | default: |
| 2672 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2673 | << " to " << result_type; |
| 2674 | }; |
| 2675 | break; |
| 2676 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2677 | case Primitive::kPrimDouble: |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2678 | switch (input_type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2679 | case Primitive::kPrimBoolean: |
| 2680 | // Boolean input is a result of code transformations. |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2681 | case Primitive::kPrimByte: |
| 2682 | case Primitive::kPrimShort: |
| 2683 | case Primitive::kPrimInt: |
| 2684 | case Primitive::kPrimChar: |
| 2685 | // Processing a Dex `int-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2686 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2687 | locations->SetOut(Location::RequiresFpuRegister()); |
| 2688 | break; |
| 2689 | |
| 2690 | case Primitive::kPrimLong: |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 2691 | // Processing a Dex `long-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2692 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 2693 | locations->SetOut(Location::RequiresFpuRegister()); |
| 2694 | break; |
| 2695 | |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2696 | case Primitive::kPrimFloat: |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 2697 | // Processing a Dex `float-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2698 | locations->SetInAt(0, Location::Any()); |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 2699 | locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2700 | break; |
| 2701 | |
| 2702 | default: |
| 2703 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2704 | << " to " << result_type; |
| 2705 | } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2706 | break; |
| 2707 | |
| 2708 | default: |
| 2709 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2710 | << " to " << result_type; |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) { |
| 2715 | LocationSummary* locations = conversion->GetLocations(); |
| 2716 | Location out = locations->Out(); |
| 2717 | Location in = locations->InAt(0); |
| 2718 | Primitive::Type result_type = conversion->GetResultType(); |
| 2719 | Primitive::Type input_type = conversion->GetInputType(); |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 2720 | DCHECK_NE(result_type, input_type); |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2721 | switch (result_type) { |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2722 | case Primitive::kPrimByte: |
| 2723 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2724 | case Primitive::kPrimLong: |
| 2725 | // Type conversion from long to byte is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2726 | case Primitive::kPrimBoolean: |
| 2727 | // Boolean input is a result of code transformations. |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2728 | case Primitive::kPrimShort: |
| 2729 | case Primitive::kPrimInt: |
| 2730 | case Primitive::kPrimChar: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2731 | // Processing a Dex `int-to-byte' instruction. |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2732 | if (in.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2733 | __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>()); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2734 | } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2735 | __ movsxb(out.AsRegister<CpuRegister>(), |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2736 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 2737 | } else { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2738 | __ movl(out.AsRegister<CpuRegister>(), |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2739 | Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant())))); |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 2740 | } |
| 2741 | break; |
| 2742 | |
| 2743 | default: |
| 2744 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2745 | << " to " << result_type; |
| 2746 | } |
| 2747 | break; |
| 2748 | |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2749 | case Primitive::kPrimShort: |
| 2750 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2751 | case Primitive::kPrimLong: |
| 2752 | // Type conversion from long to short is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2753 | case Primitive::kPrimBoolean: |
| 2754 | // Boolean input is a result of code transformations. |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2755 | case Primitive::kPrimByte: |
| 2756 | case Primitive::kPrimInt: |
| 2757 | case Primitive::kPrimChar: |
| 2758 | // Processing a Dex `int-to-short' instruction. |
| 2759 | if (in.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2760 | __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>()); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2761 | } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2762 | __ movsxw(out.AsRegister<CpuRegister>(), |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2763 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 2764 | } else { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2765 | __ movl(out.AsRegister<CpuRegister>(), |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2766 | Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant())))); |
Roland Levillain | 01a8d71 | 2014-11-14 16:27:39 +0000 | [diff] [blame] | 2767 | } |
| 2768 | break; |
| 2769 | |
| 2770 | default: |
| 2771 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2772 | << " to " << result_type; |
| 2773 | } |
| 2774 | break; |
| 2775 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2776 | case Primitive::kPrimInt: |
| 2777 | switch (input_type) { |
| 2778 | case Primitive::kPrimLong: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2779 | // Processing a Dex `long-to-int' instruction. |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2780 | if (in.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2781 | __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>()); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2782 | } else if (in.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2783 | __ movl(out.AsRegister<CpuRegister>(), |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2784 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 2785 | } else { |
| 2786 | DCHECK(in.IsConstant()); |
| 2787 | DCHECK(in.GetConstant()->IsLongConstant()); |
| 2788 | int64_t value = in.GetConstant()->AsLongConstant()->GetValue(); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2789 | __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value))); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2790 | } |
| 2791 | break; |
| 2792 | |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2793 | case Primitive::kPrimFloat: { |
| 2794 | // Processing a Dex `float-to-int' instruction. |
| 2795 | XmmRegister input = in.AsFpuRegister<XmmRegister>(); |
| 2796 | CpuRegister output = out.AsRegister<CpuRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2797 | NearLabel done, nan; |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2798 | |
| 2799 | __ movl(output, Immediate(kPrimIntMax)); |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 2800 | // if input >= (float)INT_MAX goto done |
| 2801 | __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax)); |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2802 | __ j(kAboveEqual, &done); |
| 2803 | // if input == NaN goto nan |
| 2804 | __ j(kUnordered, &nan); |
| 2805 | // output = float-to-int-truncate(input) |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2806 | __ cvttss2si(output, input, false); |
Roland Levillain | 3f8f936 | 2014-12-02 17:45:01 +0000 | [diff] [blame] | 2807 | __ jmp(&done); |
| 2808 | __ Bind(&nan); |
| 2809 | // output = 0 |
| 2810 | __ xorl(output, output); |
| 2811 | __ Bind(&done); |
| 2812 | break; |
| 2813 | } |
| 2814 | |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2815 | case Primitive::kPrimDouble: { |
| 2816 | // Processing a Dex `double-to-int' instruction. |
| 2817 | XmmRegister input = in.AsFpuRegister<XmmRegister>(); |
| 2818 | CpuRegister output = out.AsRegister<CpuRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2819 | NearLabel done, nan; |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2820 | |
| 2821 | __ movl(output, Immediate(kPrimIntMax)); |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 2822 | // if input >= (double)INT_MAX goto done |
| 2823 | __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax)); |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2824 | __ j(kAboveEqual, &done); |
| 2825 | // if input == NaN goto nan |
| 2826 | __ j(kUnordered, &nan); |
| 2827 | // output = double-to-int-truncate(input) |
| 2828 | __ cvttsd2si(output, input); |
| 2829 | __ jmp(&done); |
| 2830 | __ Bind(&nan); |
| 2831 | // output = 0 |
| 2832 | __ xorl(output, output); |
| 2833 | __ Bind(&done); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2834 | break; |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2835 | } |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 2836 | |
| 2837 | default: |
| 2838 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2839 | << " to " << result_type; |
| 2840 | } |
| 2841 | break; |
| 2842 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2843 | case Primitive::kPrimLong: |
| 2844 | switch (input_type) { |
| 2845 | DCHECK(out.IsRegister()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2846 | case Primitive::kPrimBoolean: |
| 2847 | // Boolean input is a result of code transformations. |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2848 | case Primitive::kPrimByte: |
| 2849 | case Primitive::kPrimShort: |
| 2850 | case Primitive::kPrimInt: |
Roland Levillain | 666c732 | 2014-11-10 13:39:43 +0000 | [diff] [blame] | 2851 | case Primitive::kPrimChar: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2852 | // Processing a Dex `int-to-long' instruction. |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2853 | DCHECK(in.IsRegister()); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2854 | __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>()); |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2857 | case Primitive::kPrimFloat: { |
| 2858 | // Processing a Dex `float-to-long' instruction. |
| 2859 | XmmRegister input = in.AsFpuRegister<XmmRegister>(); |
| 2860 | CpuRegister output = out.AsRegister<CpuRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2861 | NearLabel done, nan; |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2862 | |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 2863 | codegen_->Load64BitValue(output, kPrimLongMax); |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 2864 | // if input >= (float)LONG_MAX goto done |
| 2865 | __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax)); |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2866 | __ j(kAboveEqual, &done); |
| 2867 | // if input == NaN goto nan |
| 2868 | __ j(kUnordered, &nan); |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2869 | // output = float-to-long-truncate(input) |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2870 | __ cvttss2si(output, input, true); |
| 2871 | __ jmp(&done); |
| 2872 | __ Bind(&nan); |
| 2873 | // output = 0 |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 2874 | __ xorl(output, output); |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2875 | __ Bind(&done); |
| 2876 | break; |
| 2877 | } |
| 2878 | |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2879 | case Primitive::kPrimDouble: { |
| 2880 | // Processing a Dex `double-to-long' instruction. |
| 2881 | XmmRegister input = in.AsFpuRegister<XmmRegister>(); |
| 2882 | CpuRegister output = out.AsRegister<CpuRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2883 | NearLabel done, nan; |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2884 | |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 2885 | codegen_->Load64BitValue(output, kPrimLongMax); |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 2886 | // if input >= (double)LONG_MAX goto done |
| 2887 | __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax)); |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2888 | __ j(kAboveEqual, &done); |
| 2889 | // if input == NaN goto nan |
| 2890 | __ j(kUnordered, &nan); |
| 2891 | // output = double-to-long-truncate(input) |
| 2892 | __ cvttsd2si(output, input, true); |
| 2893 | __ jmp(&done); |
| 2894 | __ Bind(&nan); |
| 2895 | // output = 0 |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 2896 | __ xorl(output, output); |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2897 | __ Bind(&done); |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2898 | break; |
Roland Levillain | 4c0b61f | 2014-12-05 12:06:01 +0000 | [diff] [blame] | 2899 | } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2900 | |
| 2901 | default: |
| 2902 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2903 | << " to " << result_type; |
| 2904 | } |
| 2905 | break; |
| 2906 | |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2907 | case Primitive::kPrimChar: |
| 2908 | switch (input_type) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2909 | case Primitive::kPrimLong: |
| 2910 | // Type conversion from long to char is a result of code transformations. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2911 | case Primitive::kPrimBoolean: |
| 2912 | // Boolean input is a result of code transformations. |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2913 | case Primitive::kPrimByte: |
| 2914 | case Primitive::kPrimShort: |
| 2915 | case Primitive::kPrimInt: |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2916 | // Processing a Dex `int-to-char' instruction. |
| 2917 | if (in.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2918 | __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>()); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2919 | } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2920 | __ movzxw(out.AsRegister<CpuRegister>(), |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2921 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 2922 | } else { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 2923 | __ movl(out.AsRegister<CpuRegister>(), |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 2924 | Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant())))); |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 2925 | } |
| 2926 | break; |
| 2927 | |
| 2928 | default: |
| 2929 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2930 | << " to " << result_type; |
| 2931 | } |
| 2932 | break; |
| 2933 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2934 | case Primitive::kPrimFloat: |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2935 | switch (input_type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2936 | case Primitive::kPrimBoolean: |
| 2937 | // Boolean input is a result of code transformations. |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2938 | case Primitive::kPrimByte: |
| 2939 | case Primitive::kPrimShort: |
| 2940 | case Primitive::kPrimInt: |
| 2941 | case Primitive::kPrimChar: |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2942 | // Processing a Dex `int-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2943 | if (in.IsRegister()) { |
| 2944 | __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false); |
| 2945 | } else if (in.IsConstant()) { |
| 2946 | int32_t v = in.GetConstant()->AsIntConstant()->GetValue(); |
| 2947 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 2948 | codegen_->Load32BitValue(dest, static_cast<float>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2949 | } else { |
| 2950 | __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), |
| 2951 | Address(CpuRegister(RSP), in.GetStackIndex()), false); |
| 2952 | } |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2953 | break; |
| 2954 | |
| 2955 | case Primitive::kPrimLong: |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2956 | // Processing a Dex `long-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2957 | if (in.IsRegister()) { |
| 2958 | __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true); |
| 2959 | } else if (in.IsConstant()) { |
| 2960 | int64_t v = in.GetConstant()->AsLongConstant()->GetValue(); |
| 2961 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Pavel Vyssotski | 4c858cd | 2016-03-16 13:59:53 +0600 | [diff] [blame] | 2962 | codegen_->Load32BitValue(dest, static_cast<float>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2963 | } else { |
| 2964 | __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), |
| 2965 | Address(CpuRegister(RSP), in.GetStackIndex()), true); |
| 2966 | } |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2967 | break; |
| 2968 | |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2969 | case Primitive::kPrimDouble: |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 2970 | // Processing a Dex `double-to-float' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2971 | if (in.IsFpuRegister()) { |
| 2972 | __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>()); |
| 2973 | } else if (in.IsConstant()) { |
| 2974 | double v = in.GetConstant()->AsDoubleConstant()->GetValue(); |
| 2975 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 2976 | codegen_->Load32BitValue(dest, static_cast<float>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2977 | } else { |
| 2978 | __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), |
| 2979 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 2980 | } |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2981 | break; |
| 2982 | |
| 2983 | default: |
| 2984 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 2985 | << " to " << result_type; |
| 2986 | }; |
| 2987 | break; |
| 2988 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2989 | case Primitive::kPrimDouble: |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2990 | switch (input_type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 2991 | case Primitive::kPrimBoolean: |
| 2992 | // Boolean input is a result of code transformations. |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 2993 | case Primitive::kPrimByte: |
| 2994 | case Primitive::kPrimShort: |
| 2995 | case Primitive::kPrimInt: |
| 2996 | case Primitive::kPrimChar: |
Roland Levillain | 6d0e483 | 2014-11-27 18:31:21 +0000 | [diff] [blame] | 2997 | // Processing a Dex `int-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2998 | if (in.IsRegister()) { |
| 2999 | __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false); |
| 3000 | } else if (in.IsConstant()) { |
| 3001 | int32_t v = in.GetConstant()->AsIntConstant()->GetValue(); |
| 3002 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 3003 | codegen_->Load64BitValue(dest, static_cast<double>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 3004 | } else { |
| 3005 | __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), |
| 3006 | Address(CpuRegister(RSP), in.GetStackIndex()), false); |
| 3007 | } |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 3008 | break; |
| 3009 | |
| 3010 | case Primitive::kPrimLong: |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 3011 | // Processing a Dex `long-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 3012 | if (in.IsRegister()) { |
| 3013 | __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true); |
| 3014 | } else if (in.IsConstant()) { |
| 3015 | int64_t v = in.GetConstant()->AsLongConstant()->GetValue(); |
| 3016 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 3017 | codegen_->Load64BitValue(dest, static_cast<double>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 3018 | } else { |
| 3019 | __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), |
| 3020 | Address(CpuRegister(RSP), in.GetStackIndex()), true); |
| 3021 | } |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 3022 | break; |
| 3023 | |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 3024 | case Primitive::kPrimFloat: |
Roland Levillain | 8964e2b | 2014-12-04 12:10:50 +0000 | [diff] [blame] | 3025 | // Processing a Dex `float-to-double' instruction. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 3026 | if (in.IsFpuRegister()) { |
| 3027 | __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>()); |
| 3028 | } else if (in.IsConstant()) { |
| 3029 | float v = in.GetConstant()->AsFloatConstant()->GetValue(); |
| 3030 | XmmRegister dest = out.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 3031 | codegen_->Load64BitValue(dest, static_cast<double>(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 3032 | } else { |
| 3033 | __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), |
| 3034 | Address(CpuRegister(RSP), in.GetStackIndex())); |
| 3035 | } |
Roland Levillain | cff1374 | 2014-11-17 14:32:17 +0000 | [diff] [blame] | 3036 | break; |
| 3037 | |
| 3038 | default: |
| 3039 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 3040 | << " to " << result_type; |
| 3041 | }; |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3042 | break; |
| 3043 | |
| 3044 | default: |
| 3045 | LOG(FATAL) << "Unexpected type conversion from " << input_type |
| 3046 | << " to " << result_type; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3050 | void LocationsBuilderX86_64::VisitAdd(HAdd* add) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3051 | LocationSummary* locations = |
| 3052 | new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3053 | switch (add->GetResultType()) { |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3054 | case Primitive::kPrimInt: { |
| 3055 | locations->SetInAt(0, Location::RequiresRegister()); |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3056 | locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1))); |
| 3057 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3058 | break; |
| 3059 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3060 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3061 | case Primitive::kPrimLong: { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3062 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 3063 | // We can use a leaq or addq if the constant can fit in an immediate. |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 3064 | locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1))); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 3065 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3066 | break; |
| 3067 | } |
| 3068 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3069 | case Primitive::kPrimDouble: |
| 3070 | case Primitive::kPrimFloat: { |
| 3071 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3072 | locations->SetInAt(1, Location::Any()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3073 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3074 | break; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3075 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3076 | |
| 3077 | default: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3078 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3079 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) { |
| 3083 | LocationSummary* locations = add->GetLocations(); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3084 | Location first = locations->InAt(0); |
| 3085 | Location second = locations->InAt(1); |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3086 | Location out = locations->Out(); |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3087 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3088 | switch (add->GetResultType()) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3089 | case Primitive::kPrimInt: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3090 | if (second.IsRegister()) { |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3091 | if (out.AsRegister<Register>() == first.AsRegister<Register>()) { |
| 3092 | __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Mark Mendell | 33bf245 | 2015-05-27 10:08:24 -0400 | [diff] [blame] | 3093 | } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) { |
| 3094 | __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3095 | } else { |
| 3096 | __ leal(out.AsRegister<CpuRegister>(), Address( |
| 3097 | first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0)); |
| 3098 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3099 | } else if (second.IsConstant()) { |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3100 | if (out.AsRegister<Register>() == first.AsRegister<Register>()) { |
| 3101 | __ addl(out.AsRegister<CpuRegister>(), |
| 3102 | Immediate(second.GetConstant()->AsIntConstant()->GetValue())); |
| 3103 | } else { |
| 3104 | __ leal(out.AsRegister<CpuRegister>(), Address( |
| 3105 | first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue())); |
| 3106 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3107 | } else { |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 3108 | DCHECK(first.Equals(locations->Out())); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3109 | __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex())); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3110 | } |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3111 | break; |
| 3112 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3113 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3114 | case Primitive::kPrimLong: { |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 3115 | if (second.IsRegister()) { |
| 3116 | if (out.AsRegister<Register>() == first.AsRegister<Register>()) { |
| 3117 | __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Mark Mendell | 33bf245 | 2015-05-27 10:08:24 -0400 | [diff] [blame] | 3118 | } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) { |
| 3119 | __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>()); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 3120 | } else { |
| 3121 | __ leaq(out.AsRegister<CpuRegister>(), Address( |
| 3122 | first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0)); |
| 3123 | } |
| 3124 | } else { |
| 3125 | DCHECK(second.IsConstant()); |
| 3126 | int64_t value = second.GetConstant()->AsLongConstant()->GetValue(); |
| 3127 | int32_t int32_value = Low32Bits(value); |
| 3128 | DCHECK_EQ(int32_value, value); |
| 3129 | if (out.AsRegister<Register>() == first.AsRegister<Register>()) { |
| 3130 | __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value)); |
| 3131 | } else { |
| 3132 | __ leaq(out.AsRegister<CpuRegister>(), Address( |
| 3133 | first.AsRegister<CpuRegister>(), int32_value)); |
| 3134 | } |
| 3135 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3136 | break; |
| 3137 | } |
| 3138 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3139 | case Primitive::kPrimFloat: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3140 | if (second.IsFpuRegister()) { |
| 3141 | __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3142 | } else if (second.IsConstant()) { |
| 3143 | __ addss(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3144 | codegen_->LiteralFloatAddress( |
| 3145 | second.GetConstant()->AsFloatConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3146 | } else { |
| 3147 | DCHECK(second.IsStackSlot()); |
| 3148 | __ addss(first.AsFpuRegister<XmmRegister>(), |
| 3149 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3150 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3151 | break; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | case Primitive::kPrimDouble: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3155 | if (second.IsFpuRegister()) { |
| 3156 | __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3157 | } else if (second.IsConstant()) { |
| 3158 | __ addsd(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3159 | codegen_->LiteralDoubleAddress( |
| 3160 | second.GetConstant()->AsDoubleConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3161 | } else { |
| 3162 | DCHECK(second.IsDoubleStackSlot()); |
| 3163 | __ addsd(first.AsFpuRegister<XmmRegister>(), |
| 3164 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3165 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3166 | break; |
| 3167 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3168 | |
| 3169 | default: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 3170 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | void LocationsBuilderX86_64::VisitSub(HSub* sub) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3175 | LocationSummary* locations = |
| 3176 | new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3177 | switch (sub->GetResultType()) { |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3178 | case Primitive::kPrimInt: { |
| 3179 | locations->SetInAt(0, Location::RequiresRegister()); |
| 3180 | locations->SetInAt(1, Location::Any()); |
| 3181 | locations->SetOut(Location::SameAsFirstInput()); |
| 3182 | break; |
| 3183 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3184 | case Primitive::kPrimLong: { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3185 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 3186 | locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1))); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3187 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3188 | break; |
| 3189 | } |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3190 | case Primitive::kPrimFloat: |
| 3191 | case Primitive::kPrimDouble: { |
| 3192 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3193 | locations->SetInAt(1, Location::Any()); |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3194 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3195 | break; |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3196 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3197 | default: |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3198 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3199 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) { |
| 3203 | LocationSummary* locations = sub->GetLocations(); |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3204 | Location first = locations->InAt(0); |
| 3205 | Location second = locations->InAt(1); |
| 3206 | DCHECK(first.Equals(locations->Out())); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3207 | switch (sub->GetResultType()) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3208 | case Primitive::kPrimInt: { |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3209 | if (second.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3210 | __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3211 | } else if (second.IsConstant()) { |
| 3212 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue()); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3213 | __ subl(first.AsRegister<CpuRegister>(), imm); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3214 | } else { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3215 | __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex())); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3216 | } |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 3217 | break; |
| 3218 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3219 | case Primitive::kPrimLong: { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 3220 | if (second.IsConstant()) { |
| 3221 | int64_t value = second.GetConstant()->AsLongConstant()->GetValue(); |
| 3222 | DCHECK(IsInt<32>(value)); |
| 3223 | __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value))); |
| 3224 | } else { |
| 3225 | __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
| 3226 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3227 | break; |
| 3228 | } |
| 3229 | |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3230 | case Primitive::kPrimFloat: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3231 | if (second.IsFpuRegister()) { |
| 3232 | __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3233 | } else if (second.IsConstant()) { |
| 3234 | __ subss(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3235 | codegen_->LiteralFloatAddress( |
| 3236 | second.GetConstant()->AsFloatConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3237 | } else { |
| 3238 | DCHECK(second.IsStackSlot()); |
| 3239 | __ subss(first.AsFpuRegister<XmmRegister>(), |
| 3240 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3241 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3242 | break; |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | case Primitive::kPrimDouble: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3246 | if (second.IsFpuRegister()) { |
| 3247 | __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3248 | } else if (second.IsConstant()) { |
| 3249 | __ subsd(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3250 | codegen_->LiteralDoubleAddress( |
| 3251 | second.GetConstant()->AsDoubleConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3252 | } else { |
| 3253 | DCHECK(second.IsDoubleStackSlot()); |
| 3254 | __ subsd(first.AsFpuRegister<XmmRegister>(), |
| 3255 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3256 | } |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3257 | break; |
| 3258 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3259 | |
| 3260 | default: |
Calin Juravle | 1135168 | 2014-10-23 15:38:15 +0100 | [diff] [blame] | 3261 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3265 | void LocationsBuilderX86_64::VisitMul(HMul* mul) { |
| 3266 | LocationSummary* locations = |
| 3267 | new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall); |
| 3268 | switch (mul->GetResultType()) { |
| 3269 | case Primitive::kPrimInt: { |
| 3270 | locations->SetInAt(0, Location::RequiresRegister()); |
| 3271 | locations->SetInAt(1, Location::Any()); |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3272 | if (mul->InputAt(1)->IsIntConstant()) { |
| 3273 | // Can use 3 operand multiply. |
| 3274 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 3275 | } else { |
| 3276 | locations->SetOut(Location::SameAsFirstInput()); |
| 3277 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3278 | break; |
| 3279 | } |
| 3280 | case Primitive::kPrimLong: { |
| 3281 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3282 | locations->SetInAt(1, Location::Any()); |
| 3283 | if (mul->InputAt(1)->IsLongConstant() && |
| 3284 | IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 3285 | // Can use 3 operand multiply. |
| 3286 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 3287 | } else { |
| 3288 | locations->SetOut(Location::SameAsFirstInput()); |
| 3289 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3290 | break; |
| 3291 | } |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3292 | case Primitive::kPrimFloat: |
| 3293 | case Primitive::kPrimDouble: { |
| 3294 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3295 | locations->SetInAt(1, Location::Any()); |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3296 | locations->SetOut(Location::SameAsFirstInput()); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3297 | break; |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3298 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3299 | |
| 3300 | default: |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3301 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) { |
| 3306 | LocationSummary* locations = mul->GetLocations(); |
| 3307 | Location first = locations->InAt(0); |
| 3308 | Location second = locations->InAt(1); |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3309 | Location out = locations->Out(); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3310 | switch (mul->GetResultType()) { |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3311 | case Primitive::kPrimInt: |
| 3312 | // The constant may have ended up in a register, so test explicitly to avoid |
| 3313 | // problems where the output may not be the same as the first operand. |
| 3314 | if (mul->InputAt(1)->IsIntConstant()) { |
| 3315 | Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue()); |
| 3316 | __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm); |
| 3317 | } else if (second.IsRegister()) { |
| 3318 | DCHECK(first.Equals(out)); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3319 | __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3320 | } else { |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3321 | DCHECK(first.Equals(out)); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3322 | DCHECK(second.IsStackSlot()); |
Roland Levillain | 199f336 | 2014-11-27 17:15:16 +0000 | [diff] [blame] | 3323 | __ imull(first.AsRegister<CpuRegister>(), |
| 3324 | Address(CpuRegister(RSP), second.GetStackIndex())); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3325 | } |
| 3326 | break; |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3327 | case Primitive::kPrimLong: { |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3328 | // The constant may have ended up in a register, so test explicitly to avoid |
| 3329 | // problems where the output may not be the same as the first operand. |
| 3330 | if (mul->InputAt(1)->IsLongConstant()) { |
| 3331 | int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue(); |
| 3332 | if (IsInt<32>(value)) { |
| 3333 | __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), |
| 3334 | Immediate(static_cast<int32_t>(value))); |
| 3335 | } else { |
| 3336 | // Have to use the constant area. |
| 3337 | DCHECK(first.Equals(out)); |
| 3338 | __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value)); |
| 3339 | } |
| 3340 | } else if (second.IsRegister()) { |
| 3341 | DCHECK(first.Equals(out)); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 3342 | __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3343 | } else { |
| 3344 | DCHECK(second.IsDoubleStackSlot()); |
| 3345 | DCHECK(first.Equals(out)); |
| 3346 | __ imulq(first.AsRegister<CpuRegister>(), |
| 3347 | Address(CpuRegister(RSP), second.GetStackIndex())); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 3348 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3349 | break; |
| 3350 | } |
| 3351 | |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3352 | case Primitive::kPrimFloat: { |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3353 | DCHECK(first.Equals(out)); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3354 | if (second.IsFpuRegister()) { |
| 3355 | __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3356 | } else if (second.IsConstant()) { |
| 3357 | __ mulss(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3358 | codegen_->LiteralFloatAddress( |
| 3359 | second.GetConstant()->AsFloatConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3360 | } else { |
| 3361 | DCHECK(second.IsStackSlot()); |
| 3362 | __ mulss(first.AsFpuRegister<XmmRegister>(), |
| 3363 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3364 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3365 | break; |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | case Primitive::kPrimDouble: { |
Mark Mendell | 4a2aa4a | 2015-07-27 16:13:10 -0400 | [diff] [blame] | 3369 | DCHECK(first.Equals(out)); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3370 | if (second.IsFpuRegister()) { |
| 3371 | __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3372 | } else if (second.IsConstant()) { |
| 3373 | __ mulsd(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3374 | codegen_->LiteralDoubleAddress( |
| 3375 | second.GetConstant()->AsDoubleConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3376 | } else { |
| 3377 | DCHECK(second.IsDoubleStackSlot()); |
| 3378 | __ mulsd(first.AsFpuRegister<XmmRegister>(), |
| 3379 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3380 | } |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3381 | break; |
| 3382 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3383 | |
| 3384 | default: |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 3385 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3389 | void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset, |
| 3390 | uint32_t stack_adjustment, bool is_float) { |
| 3391 | if (source.IsStackSlot()) { |
| 3392 | DCHECK(is_float); |
| 3393 | __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment)); |
| 3394 | } else if (source.IsDoubleStackSlot()) { |
| 3395 | DCHECK(!is_float); |
| 3396 | __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment)); |
| 3397 | } else { |
| 3398 | // Write the value to the temporary location on the stack and load to FP stack. |
| 3399 | if (is_float) { |
| 3400 | Location stack_temp = Location::StackSlot(temp_offset); |
| 3401 | codegen_->Move(stack_temp, source); |
| 3402 | __ flds(Address(CpuRegister(RSP), temp_offset)); |
| 3403 | } else { |
| 3404 | Location stack_temp = Location::DoubleStackSlot(temp_offset); |
| 3405 | codegen_->Move(stack_temp, source); |
| 3406 | __ fldl(Address(CpuRegister(RSP), temp_offset)); |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) { |
| 3412 | Primitive::Type type = rem->GetResultType(); |
| 3413 | bool is_float = type == Primitive::kPrimFloat; |
| 3414 | size_t elem_size = Primitive::ComponentSize(type); |
| 3415 | LocationSummary* locations = rem->GetLocations(); |
| 3416 | Location first = locations->InAt(0); |
| 3417 | Location second = locations->InAt(1); |
| 3418 | Location out = locations->Out(); |
| 3419 | |
| 3420 | // Create stack space for 2 elements. |
| 3421 | // TODO: enhance register allocator to ask for stack temporaries. |
| 3422 | __ subq(CpuRegister(RSP), Immediate(2 * elem_size)); |
| 3423 | |
| 3424 | // Load the values to the FP stack in reverse order, using temporaries if needed. |
| 3425 | PushOntoFPStack(second, elem_size, 2 * elem_size, is_float); |
| 3426 | PushOntoFPStack(first, 0, 2 * elem_size, is_float); |
| 3427 | |
| 3428 | // Loop doing FPREM until we stabilize. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 3429 | NearLabel retry; |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3430 | __ Bind(&retry); |
| 3431 | __ fprem(); |
| 3432 | |
| 3433 | // Move FP status to AX. |
| 3434 | __ fstsw(); |
| 3435 | |
| 3436 | // And see if the argument reduction is complete. This is signaled by the |
| 3437 | // C2 FPU flag bit set to 0. |
| 3438 | __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask)); |
| 3439 | __ j(kNotEqual, &retry); |
| 3440 | |
| 3441 | // We have settled on the final value. Retrieve it into an XMM register. |
| 3442 | // Store FP top of stack to real stack. |
| 3443 | if (is_float) { |
| 3444 | __ fsts(Address(CpuRegister(RSP), 0)); |
| 3445 | } else { |
| 3446 | __ fstl(Address(CpuRegister(RSP), 0)); |
| 3447 | } |
| 3448 | |
| 3449 | // Pop the 2 items from the FP stack. |
| 3450 | __ fucompp(); |
| 3451 | |
| 3452 | // Load the value from the stack into an XMM register. |
| 3453 | DCHECK(out.IsFpuRegister()) << out; |
| 3454 | if (is_float) { |
| 3455 | __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0)); |
| 3456 | } else { |
| 3457 | __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0)); |
| 3458 | } |
| 3459 | |
| 3460 | // And remove the temporary stack space we allocated. |
| 3461 | __ addq(CpuRegister(RSP), Immediate(2 * elem_size)); |
| 3462 | } |
| 3463 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3464 | void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) { |
| 3465 | DCHECK(instruction->IsDiv() || instruction->IsRem()); |
| 3466 | |
| 3467 | LocationSummary* locations = instruction->GetLocations(); |
| 3468 | Location second = locations->InAt(1); |
| 3469 | DCHECK(second.IsConstant()); |
| 3470 | |
| 3471 | CpuRegister output_register = locations->Out().AsRegister<CpuRegister>(); |
| 3472 | CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>(); |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3473 | int64_t imm = Int64FromConstant(second.GetConstant()); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3474 | |
| 3475 | DCHECK(imm == 1 || imm == -1); |
| 3476 | |
| 3477 | switch (instruction->GetResultType()) { |
| 3478 | case Primitive::kPrimInt: { |
| 3479 | if (instruction->IsRem()) { |
| 3480 | __ xorl(output_register, output_register); |
| 3481 | } else { |
| 3482 | __ movl(output_register, input_register); |
| 3483 | if (imm == -1) { |
| 3484 | __ negl(output_register); |
| 3485 | } |
| 3486 | } |
| 3487 | break; |
| 3488 | } |
| 3489 | |
| 3490 | case Primitive::kPrimLong: { |
| 3491 | if (instruction->IsRem()) { |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 3492 | __ xorl(output_register, output_register); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3493 | } else { |
| 3494 | __ movq(output_register, input_register); |
| 3495 | if (imm == -1) { |
| 3496 | __ negq(output_register); |
| 3497 | } |
| 3498 | } |
| 3499 | break; |
| 3500 | } |
| 3501 | |
| 3502 | default: |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3503 | LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType(); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3504 | } |
| 3505 | } |
| 3506 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3507 | void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) { |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3508 | LocationSummary* locations = instruction->GetLocations(); |
| 3509 | Location second = locations->InAt(1); |
| 3510 | |
| 3511 | CpuRegister output_register = locations->Out().AsRegister<CpuRegister>(); |
| 3512 | CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>(); |
| 3513 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3514 | int64_t imm = Int64FromConstant(second.GetConstant()); |
Nicolas Geoffray | 68f6289 | 2016-01-04 08:39:49 +0000 | [diff] [blame] | 3515 | DCHECK(IsPowerOfTwo(AbsOrMin(imm))); |
| 3516 | uint64_t abs_imm = AbsOrMin(imm); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3517 | |
| 3518 | CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 3519 | |
| 3520 | if (instruction->GetResultType() == Primitive::kPrimInt) { |
Nicolas Geoffray | 68f6289 | 2016-01-04 08:39:49 +0000 | [diff] [blame] | 3521 | __ leal(tmp, Address(numerator, abs_imm - 1)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3522 | __ testl(numerator, numerator); |
| 3523 | __ cmov(kGreaterEqual, tmp, numerator); |
| 3524 | int shift = CTZ(imm); |
| 3525 | __ sarl(tmp, Immediate(shift)); |
| 3526 | |
| 3527 | if (imm < 0) { |
| 3528 | __ negl(tmp); |
| 3529 | } |
| 3530 | |
| 3531 | __ movl(output_register, tmp); |
| 3532 | } else { |
| 3533 | DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong); |
| 3534 | CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 3535 | |
Nicolas Geoffray | 68f6289 | 2016-01-04 08:39:49 +0000 | [diff] [blame] | 3536 | codegen_->Load64BitValue(rdx, abs_imm - 1); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3537 | __ addq(rdx, numerator); |
| 3538 | __ testq(numerator, numerator); |
| 3539 | __ cmov(kGreaterEqual, rdx, numerator); |
| 3540 | int shift = CTZ(imm); |
| 3541 | __ sarq(rdx, Immediate(shift)); |
| 3542 | |
| 3543 | if (imm < 0) { |
| 3544 | __ negq(rdx); |
| 3545 | } |
| 3546 | |
| 3547 | __ movq(output_register, rdx); |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) { |
| 3552 | DCHECK(instruction->IsDiv() || instruction->IsRem()); |
| 3553 | |
| 3554 | LocationSummary* locations = instruction->GetLocations(); |
| 3555 | Location second = locations->InAt(1); |
| 3556 | |
| 3557 | CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>() |
| 3558 | : locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 3559 | CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>(); |
| 3560 | CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>() |
| 3561 | : locations->Out().AsRegister<CpuRegister>(); |
| 3562 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 3563 | |
| 3564 | DCHECK_EQ(RAX, eax.AsRegister()); |
| 3565 | DCHECK_EQ(RDX, edx.AsRegister()); |
| 3566 | if (instruction->IsDiv()) { |
| 3567 | DCHECK_EQ(RAX, out.AsRegister()); |
| 3568 | } else { |
| 3569 | DCHECK_EQ(RDX, out.AsRegister()); |
| 3570 | } |
| 3571 | |
| 3572 | int64_t magic; |
| 3573 | int shift; |
| 3574 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3575 | // TODO: can these branches be written as one? |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3576 | if (instruction->GetResultType() == Primitive::kPrimInt) { |
| 3577 | int imm = second.GetConstant()->AsIntConstant()->GetValue(); |
| 3578 | |
| 3579 | CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift); |
| 3580 | |
| 3581 | __ movl(numerator, eax); |
| 3582 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3583 | __ movl(eax, Immediate(magic)); |
| 3584 | __ imull(numerator); |
| 3585 | |
| 3586 | if (imm > 0 && magic < 0) { |
| 3587 | __ addl(edx, numerator); |
| 3588 | } else if (imm < 0 && magic > 0) { |
| 3589 | __ subl(edx, numerator); |
| 3590 | } |
| 3591 | |
| 3592 | if (shift != 0) { |
| 3593 | __ sarl(edx, Immediate(shift)); |
| 3594 | } |
| 3595 | |
| 3596 | __ movl(eax, edx); |
| 3597 | __ shrl(edx, Immediate(31)); |
| 3598 | __ addl(edx, eax); |
| 3599 | |
| 3600 | if (instruction->IsRem()) { |
| 3601 | __ movl(eax, numerator); |
| 3602 | __ imull(edx, Immediate(imm)); |
| 3603 | __ subl(eax, edx); |
| 3604 | __ movl(edx, eax); |
| 3605 | } else { |
| 3606 | __ movl(eax, edx); |
| 3607 | } |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3608 | } else { |
| 3609 | int64_t imm = second.GetConstant()->AsLongConstant()->GetValue(); |
| 3610 | |
| 3611 | DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong); |
| 3612 | |
| 3613 | CpuRegister rax = eax; |
| 3614 | CpuRegister rdx = edx; |
| 3615 | |
| 3616 | CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift); |
| 3617 | |
| 3618 | // Save the numerator. |
| 3619 | __ movq(numerator, rax); |
| 3620 | |
| 3621 | // RAX = magic |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 3622 | codegen_->Load64BitValue(rax, magic); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3623 | |
| 3624 | // RDX:RAX = magic * numerator |
| 3625 | __ imulq(numerator); |
| 3626 | |
| 3627 | if (imm > 0 && magic < 0) { |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3628 | // RDX += numerator |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3629 | __ addq(rdx, numerator); |
| 3630 | } else if (imm < 0 && magic > 0) { |
| 3631 | // RDX -= numerator |
| 3632 | __ subq(rdx, numerator); |
| 3633 | } |
| 3634 | |
| 3635 | // Shift if needed. |
| 3636 | if (shift != 0) { |
| 3637 | __ sarq(rdx, Immediate(shift)); |
| 3638 | } |
| 3639 | |
| 3640 | // RDX += 1 if RDX < 0 |
| 3641 | __ movq(rax, rdx); |
| 3642 | __ shrq(rdx, Immediate(63)); |
| 3643 | __ addq(rdx, rax); |
| 3644 | |
| 3645 | if (instruction->IsRem()) { |
| 3646 | __ movq(rax, numerator); |
| 3647 | |
| 3648 | if (IsInt<32>(imm)) { |
| 3649 | __ imulq(rdx, Immediate(static_cast<int32_t>(imm))); |
| 3650 | } else { |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 3651 | __ imulq(rdx, codegen_->LiteralInt64Address(imm)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | __ subq(rax, rdx); |
| 3655 | __ movq(rdx, rax); |
| 3656 | } else { |
| 3657 | __ movq(rax, rdx); |
| 3658 | } |
| 3659 | } |
| 3660 | } |
| 3661 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3662 | void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) { |
| 3663 | DCHECK(instruction->IsDiv() || instruction->IsRem()); |
| 3664 | Primitive::Type type = instruction->GetResultType(); |
| 3665 | DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong); |
| 3666 | |
| 3667 | bool is_div = instruction->IsDiv(); |
| 3668 | LocationSummary* locations = instruction->GetLocations(); |
| 3669 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3670 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 3671 | Location second = locations->InAt(1); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3672 | |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3673 | DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister()); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3674 | DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3675 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3676 | if (second.IsConstant()) { |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3677 | int64_t imm = Int64FromConstant(second.GetConstant()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3678 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3679 | if (imm == 0) { |
| 3680 | // Do not generate anything. DivZeroCheck would prevent any code to be executed. |
| 3681 | } else if (imm == 1 || imm == -1) { |
| 3682 | DivRemOneOrMinusOne(instruction); |
Nicolas Geoffray | 68f6289 | 2016-01-04 08:39:49 +0000 | [diff] [blame] | 3683 | } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) { |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 3684 | DivByPowerOfTwo(instruction->AsDiv()); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3685 | } else { |
| 3686 | DCHECK(imm <= -2 || imm >= 2); |
| 3687 | GenerateDivRemWithAnyConstant(instruction); |
| 3688 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3689 | } else { |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 3690 | SlowPathCode* slow_path = |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3691 | new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64( |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 3692 | instruction, out.AsRegister(), type, is_div); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3693 | codegen_->AddSlowPath(slow_path); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3694 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3695 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 3696 | // 0x80000000(00000000)/-1 triggers an arithmetic exception! |
| 3697 | // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000) |
| 3698 | // so it's safe to just use negl instead of more complex comparisons. |
| 3699 | if (type == Primitive::kPrimInt) { |
| 3700 | __ cmpl(second_reg, Immediate(-1)); |
| 3701 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3702 | // edx:eax <- sign-extended of eax |
| 3703 | __ cdq(); |
| 3704 | // eax = quotient, edx = remainder |
| 3705 | __ idivl(second_reg); |
| 3706 | } else { |
| 3707 | __ cmpq(second_reg, Immediate(-1)); |
| 3708 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3709 | // rdx:rax <- sign-extended of rax |
| 3710 | __ cqo(); |
| 3711 | // rax = quotient, rdx = remainder |
| 3712 | __ idivq(second_reg); |
| 3713 | } |
| 3714 | __ Bind(slow_path->GetExitLabel()); |
| 3715 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3718 | void LocationsBuilderX86_64::VisitDiv(HDiv* div) { |
| 3719 | LocationSummary* locations = |
| 3720 | new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall); |
| 3721 | switch (div->GetResultType()) { |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3722 | case Primitive::kPrimInt: |
| 3723 | case Primitive::kPrimLong: { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3724 | locations->SetInAt(0, Location::RegisterLocation(RAX)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3725 | locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1))); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3726 | locations->SetOut(Location::SameAsFirstInput()); |
| 3727 | // Intel uses edx:eax as the dividend. |
| 3728 | locations->AddTemp(Location::RegisterLocation(RDX)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3729 | // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way |
| 3730 | // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as |
| 3731 | // output and request another temp. |
| 3732 | if (div->InputAt(1)->IsConstant()) { |
| 3733 | locations->AddTemp(Location::RequiresRegister()); |
| 3734 | } |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3735 | break; |
| 3736 | } |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3737 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3738 | case Primitive::kPrimFloat: |
| 3739 | case Primitive::kPrimDouble: { |
| 3740 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3741 | locations->SetInAt(1, Location::Any()); |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3742 | locations->SetOut(Location::SameAsFirstInput()); |
| 3743 | break; |
| 3744 | } |
| 3745 | |
| 3746 | default: |
| 3747 | LOG(FATAL) << "Unexpected div type " << div->GetResultType(); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) { |
| 3752 | LocationSummary* locations = div->GetLocations(); |
| 3753 | Location first = locations->InAt(0); |
| 3754 | Location second = locations->InAt(1); |
| 3755 | DCHECK(first.Equals(locations->Out())); |
| 3756 | |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3757 | Primitive::Type type = div->GetResultType(); |
| 3758 | switch (type) { |
| 3759 | case Primitive::kPrimInt: |
| 3760 | case Primitive::kPrimLong: { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3761 | GenerateDivRemIntegral(div); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3762 | break; |
| 3763 | } |
| 3764 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3765 | case Primitive::kPrimFloat: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3766 | if (second.IsFpuRegister()) { |
| 3767 | __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3768 | } else if (second.IsConstant()) { |
| 3769 | __ divss(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3770 | codegen_->LiteralFloatAddress( |
| 3771 | second.GetConstant()->AsFloatConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3772 | } else { |
| 3773 | DCHECK(second.IsStackSlot()); |
| 3774 | __ divss(first.AsFpuRegister<XmmRegister>(), |
| 3775 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3776 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3777 | break; |
| 3778 | } |
| 3779 | |
| 3780 | case Primitive::kPrimDouble: { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3781 | if (second.IsFpuRegister()) { |
| 3782 | __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>()); |
| 3783 | } else if (second.IsConstant()) { |
| 3784 | __ divsd(first.AsFpuRegister<XmmRegister>(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 3785 | codegen_->LiteralDoubleAddress( |
| 3786 | second.GetConstant()->AsDoubleConstant()->GetValue())); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 3787 | } else { |
| 3788 | DCHECK(second.IsDoubleStackSlot()); |
| 3789 | __ divsd(first.AsFpuRegister<XmmRegister>(), |
| 3790 | Address(CpuRegister(RSP), second.GetStackIndex())); |
| 3791 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 3792 | break; |
| 3793 | } |
| 3794 | |
| 3795 | default: |
| 3796 | LOG(FATAL) << "Unexpected div type " << div->GetResultType(); |
| 3797 | } |
| 3798 | } |
| 3799 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3800 | void LocationsBuilderX86_64::VisitRem(HRem* rem) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 3801 | Primitive::Type type = rem->GetResultType(); |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3802 | LocationSummary* locations = |
| 3803 | new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall); |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 3804 | |
| 3805 | switch (type) { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3806 | case Primitive::kPrimInt: |
| 3807 | case Primitive::kPrimLong: { |
| 3808 | locations->SetInAt(0, Location::RegisterLocation(RAX)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3809 | locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1))); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3810 | // Intel uses rdx:rax as the dividend and puts the remainder in rdx |
| 3811 | locations->SetOut(Location::RegisterLocation(RDX)); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 3812 | // We need to save the numerator while we tweak eax and edx. As we are using imul in a way |
| 3813 | // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as |
| 3814 | // output and request another temp. |
| 3815 | if (rem->InputAt(1)->IsConstant()) { |
| 3816 | locations->AddTemp(Location::RequiresRegister()); |
| 3817 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3818 | break; |
| 3819 | } |
| 3820 | |
| 3821 | case Primitive::kPrimFloat: |
| 3822 | case Primitive::kPrimDouble: { |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3823 | locations->SetInAt(0, Location::Any()); |
| 3824 | locations->SetInAt(1, Location::Any()); |
| 3825 | locations->SetOut(Location::RequiresFpuRegister()); |
| 3826 | locations->AddTemp(Location::RegisterLocation(RAX)); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3827 | break; |
| 3828 | } |
| 3829 | |
| 3830 | default: |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 3831 | LOG(FATAL) << "Unexpected rem type " << type; |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) { |
| 3836 | Primitive::Type type = rem->GetResultType(); |
| 3837 | switch (type) { |
| 3838 | case Primitive::kPrimInt: |
| 3839 | case Primitive::kPrimLong: { |
| 3840 | GenerateDivRemIntegral(rem); |
| 3841 | break; |
| 3842 | } |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3843 | case Primitive::kPrimFloat: |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 3844 | case Primitive::kPrimDouble: { |
Mark Mendell | 24f2dfa | 2015-01-14 19:51:45 -0500 | [diff] [blame] | 3845 | GenerateRemFP(rem); |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 3846 | break; |
| 3847 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 3848 | default: |
| 3849 | LOG(FATAL) << "Unexpected rem type " << rem->GetResultType(); |
| 3850 | } |
| 3851 | } |
| 3852 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3853 | void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 3854 | LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3855 | locations->SetInAt(0, Location::Any()); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) { |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 3859 | SlowPathCode* slow_path = |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3860 | new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction); |
| 3861 | codegen_->AddSlowPath(slow_path); |
| 3862 | |
| 3863 | LocationSummary* locations = instruction->GetLocations(); |
| 3864 | Location value = locations->InAt(0); |
| 3865 | |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3866 | switch (instruction->GetType()) { |
Nicolas Geoffray | e567161 | 2016-03-16 11:03:54 +0000 | [diff] [blame] | 3867 | case Primitive::kPrimBoolean: |
Serguei Katkov | 8c0676c | 2015-08-03 13:55:33 +0600 | [diff] [blame] | 3868 | case Primitive::kPrimByte: |
| 3869 | case Primitive::kPrimChar: |
| 3870 | case Primitive::kPrimShort: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3871 | case Primitive::kPrimInt: { |
| 3872 | if (value.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3873 | __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>()); |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3874 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3875 | } else if (value.IsStackSlot()) { |
| 3876 | __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0)); |
| 3877 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3878 | } else { |
| 3879 | DCHECK(value.IsConstant()) << value; |
| 3880 | if (value.GetConstant()->AsIntConstant()->GetValue() == 0) { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 3881 | __ jmp(slow_path->GetEntryLabel()); |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | break; |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3885 | } |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3886 | case Primitive::kPrimLong: { |
| 3887 | if (value.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3888 | __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>()); |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3889 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3890 | } else if (value.IsDoubleStackSlot()) { |
| 3891 | __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0)); |
| 3892 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3893 | } else { |
| 3894 | DCHECK(value.IsConstant()) << value; |
| 3895 | if (value.GetConstant()->AsLongConstant()->GetValue() == 0) { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 3896 | __ jmp(slow_path->GetEntryLabel()); |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | break; |
| 3900 | } |
| 3901 | default: |
| 3902 | LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType(); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3903 | } |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3906 | void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) { |
| 3907 | DCHECK(op->IsShl() || op->IsShr() || op->IsUShr()); |
| 3908 | |
| 3909 | LocationSummary* locations = |
| 3910 | new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall); |
| 3911 | |
| 3912 | switch (op->GetResultType()) { |
| 3913 | case Primitive::kPrimInt: |
| 3914 | case Primitive::kPrimLong: { |
| 3915 | locations->SetInAt(0, Location::RequiresRegister()); |
| 3916 | // The shift count needs to be in CL. |
| 3917 | locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1))); |
| 3918 | locations->SetOut(Location::SameAsFirstInput()); |
| 3919 | break; |
| 3920 | } |
| 3921 | default: |
| 3922 | LOG(FATAL) << "Unexpected operation type " << op->GetResultType(); |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) { |
| 3927 | DCHECK(op->IsShl() || op->IsShr() || op->IsUShr()); |
| 3928 | |
| 3929 | LocationSummary* locations = op->GetLocations(); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3930 | CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>(); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3931 | Location second = locations->InAt(1); |
| 3932 | |
| 3933 | switch (op->GetResultType()) { |
| 3934 | case Primitive::kPrimInt: { |
| 3935 | if (second.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3936 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3937 | if (op->IsShl()) { |
| 3938 | __ shll(first_reg, second_reg); |
| 3939 | } else if (op->IsShr()) { |
| 3940 | __ sarl(first_reg, second_reg); |
| 3941 | } else { |
| 3942 | __ shrl(first_reg, second_reg); |
| 3943 | } |
| 3944 | } else { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 3945 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3946 | if (op->IsShl()) { |
| 3947 | __ shll(first_reg, imm); |
| 3948 | } else if (op->IsShr()) { |
| 3949 | __ sarl(first_reg, imm); |
| 3950 | } else { |
| 3951 | __ shrl(first_reg, imm); |
| 3952 | } |
| 3953 | } |
| 3954 | break; |
| 3955 | } |
| 3956 | case Primitive::kPrimLong: { |
| 3957 | if (second.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 3958 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3959 | if (op->IsShl()) { |
| 3960 | __ shlq(first_reg, second_reg); |
| 3961 | } else if (op->IsShr()) { |
| 3962 | __ sarq(first_reg, second_reg); |
| 3963 | } else { |
| 3964 | __ shrq(first_reg, second_reg); |
| 3965 | } |
| 3966 | } else { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 3967 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3968 | if (op->IsShl()) { |
| 3969 | __ shlq(first_reg, imm); |
| 3970 | } else if (op->IsShr()) { |
| 3971 | __ sarq(first_reg, imm); |
| 3972 | } else { |
| 3973 | __ shrq(first_reg, imm); |
| 3974 | } |
| 3975 | } |
| 3976 | break; |
| 3977 | } |
| 3978 | default: |
| 3979 | LOG(FATAL) << "Unexpected operation type " << op->GetResultType(); |
Vladimir Marko | 351dddf | 2015-12-11 16:34:46 +0000 | [diff] [blame] | 3980 | UNREACHABLE(); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 3984 | void LocationsBuilderX86_64::VisitRor(HRor* ror) { |
| 3985 | LocationSummary* locations = |
| 3986 | new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall); |
| 3987 | |
| 3988 | switch (ror->GetResultType()) { |
| 3989 | case Primitive::kPrimInt: |
| 3990 | case Primitive::kPrimLong: { |
| 3991 | locations->SetInAt(0, Location::RequiresRegister()); |
| 3992 | // The shift count needs to be in CL (unless it is a constant). |
| 3993 | locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1))); |
| 3994 | locations->SetOut(Location::SameAsFirstInput()); |
| 3995 | break; |
| 3996 | } |
| 3997 | default: |
| 3998 | LOG(FATAL) << "Unexpected operation type " << ror->GetResultType(); |
| 3999 | UNREACHABLE(); |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) { |
| 4004 | LocationSummary* locations = ror->GetLocations(); |
| 4005 | CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 4006 | Location second = locations->InAt(1); |
| 4007 | |
| 4008 | switch (ror->GetResultType()) { |
| 4009 | case Primitive::kPrimInt: |
| 4010 | if (second.IsRegister()) { |
| 4011 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 4012 | __ rorl(first_reg, second_reg); |
| 4013 | } else { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 4014 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 4015 | __ rorl(first_reg, imm); |
| 4016 | } |
| 4017 | break; |
| 4018 | case Primitive::kPrimLong: |
| 4019 | if (second.IsRegister()) { |
| 4020 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 4021 | __ rorq(first_reg, second_reg); |
| 4022 | } else { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 4023 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 4024 | __ rorq(first_reg, imm); |
| 4025 | } |
| 4026 | break; |
| 4027 | default: |
| 4028 | LOG(FATAL) << "Unexpected operation type " << ror->GetResultType(); |
| 4029 | UNREACHABLE(); |
| 4030 | } |
| 4031 | } |
| 4032 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4033 | void LocationsBuilderX86_64::VisitShl(HShl* shl) { |
| 4034 | HandleShift(shl); |
| 4035 | } |
| 4036 | |
| 4037 | void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) { |
| 4038 | HandleShift(shl); |
| 4039 | } |
| 4040 | |
| 4041 | void LocationsBuilderX86_64::VisitShr(HShr* shr) { |
| 4042 | HandleShift(shr); |
| 4043 | } |
| 4044 | |
| 4045 | void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) { |
| 4046 | HandleShift(shr); |
| 4047 | } |
| 4048 | |
| 4049 | void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) { |
| 4050 | HandleShift(ushr); |
| 4051 | } |
| 4052 | |
| 4053 | void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) { |
| 4054 | HandleShift(ushr); |
| 4055 | } |
| 4056 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4057 | void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4058 | LocationSummary* locations = |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 4059 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly); |
Nicolas Geoffray | 9ae0daa | 2014-09-30 22:40:23 +0100 | [diff] [blame] | 4060 | InvokeRuntimeCallingConvention calling_convention; |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4061 | if (instruction->IsStringAlloc()) { |
| 4062 | locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument)); |
| 4063 | } else { |
| 4064 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 4065 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 4066 | } |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 4067 | locations->SetOut(Location::RegisterLocation(RAX)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) { |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4071 | // Note: if heap poisoning is enabled, the entry point takes cares |
| 4072 | // of poisoning the reference. |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4073 | if (instruction->IsStringAlloc()) { |
| 4074 | // String is allocated through StringFactory. Call NewEmptyString entry point. |
| 4075 | CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 4076 | MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize); |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4077 | __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true)); |
| 4078 | __ call(Address(temp, code_offset.SizeValue())); |
| 4079 | codegen_->RecordPcInfo(instruction, instruction->GetDexPc()); |
| 4080 | } else { |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 4081 | codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc()); |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4082 | CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>(); |
| 4083 | DCHECK(!codegen_->IsLeafMethod()); |
| 4084 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4085 | } |
| 4086 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4087 | void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) { |
| 4088 | LocationSummary* locations = |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 4089 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4090 | InvokeRuntimeCallingConvention calling_convention; |
| 4091 | locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4092 | locations->SetOut(Location::RegisterLocation(RAX)); |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 4093 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 4094 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) { |
| 4098 | InvokeRuntimeCallingConvention calling_convention; |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 4099 | codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)), |
| 4100 | instruction->GetTypeIndex()); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4101 | // Note: if heap poisoning is enabled, the entry point takes cares |
| 4102 | // of poisoning the reference. |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 4103 | codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc()); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 4104 | CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>(); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4105 | |
| 4106 | DCHECK(!codegen_->IsLeafMethod()); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4107 | } |
| 4108 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4109 | void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4110 | LocationSummary* locations = |
| 4111 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4112 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 4113 | if (location.IsStackSlot()) { |
| 4114 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 4115 | } else if (location.IsDoubleStackSlot()) { |
| 4116 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 4117 | } |
| 4118 | locations->SetOut(location); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4119 | } |
| 4120 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4121 | void InstructionCodeGeneratorX86_64::VisitParameterValue( |
| 4122 | HParameterValue* instruction ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4123 | // Nothing to do, the parameter is already at its location. |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) { |
| 4127 | LocationSummary* locations = |
| 4128 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 4129 | locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument)); |
| 4130 | } |
| 4131 | |
| 4132 | void InstructionCodeGeneratorX86_64::VisitCurrentMethod( |
| 4133 | HCurrentMethod* instruction ATTRIBUTE_UNUSED) { |
| 4134 | // Nothing to do, the method is already at its location. |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4135 | } |
| 4136 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 4137 | void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) { |
| 4138 | LocationSummary* locations = |
| 4139 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 4140 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4141 | locations->SetOut(Location::RequiresRegister()); |
| 4142 | } |
| 4143 | |
| 4144 | void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) { |
| 4145 | LocationSummary* locations = instruction->GetLocations(); |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4146 | if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) { |
Nicolas Geoffray | ff484b9 | 2016-07-13 14:13:48 +0100 | [diff] [blame] | 4147 | uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset( |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 4148 | instruction->GetIndex(), kX86_64PointerSize).SizeValue(); |
Nicolas Geoffray | ff484b9 | 2016-07-13 14:13:48 +0100 | [diff] [blame] | 4149 | __ movq(locations->Out().AsRegister<CpuRegister>(), |
| 4150 | Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset)); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 4151 | } else { |
Nicolas Geoffray | ff484b9 | 2016-07-13 14:13:48 +0100 | [diff] [blame] | 4152 | uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement( |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 4153 | instruction->GetIndex(), kX86_64PointerSize)); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 4154 | __ movq(locations->Out().AsRegister<CpuRegister>(), |
| 4155 | Address(locations->InAt(0).AsRegister<CpuRegister>(), |
| 4156 | mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value())); |
Nicolas Geoffray | ff484b9 | 2016-07-13 14:13:48 +0100 | [diff] [blame] | 4157 | __ movq(locations->Out().AsRegister<CpuRegister>(), |
| 4158 | Address(locations->Out().AsRegister<CpuRegister>(), method_offset)); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 4159 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4162 | void LocationsBuilderX86_64::VisitNot(HNot* not_) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4163 | LocationSummary* locations = |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4164 | new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 4165 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4166 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4169 | void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) { |
| 4170 | LocationSummary* locations = not_->GetLocations(); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 4171 | DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(), |
| 4172 | locations->Out().AsRegister<CpuRegister>().AsRegister()); |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4173 | Location out = locations->Out(); |
Nicolas Geoffray | d8ef2e9 | 2015-02-24 16:02:06 +0000 | [diff] [blame] | 4174 | switch (not_->GetResultType()) { |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4175 | case Primitive::kPrimInt: |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 4176 | __ notl(out.AsRegister<CpuRegister>()); |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4177 | break; |
| 4178 | |
| 4179 | case Primitive::kPrimLong: |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 4180 | __ notq(out.AsRegister<CpuRegister>()); |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4181 | break; |
| 4182 | |
| 4183 | default: |
| 4184 | LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType(); |
| 4185 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4188 | void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) { |
| 4189 | LocationSummary* locations = |
| 4190 | new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall); |
| 4191 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4192 | locations->SetOut(Location::SameAsFirstInput()); |
| 4193 | } |
| 4194 | |
| 4195 | void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) { |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4196 | LocationSummary* locations = bool_not->GetLocations(); |
| 4197 | DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(), |
| 4198 | locations->Out().AsRegister<CpuRegister>().AsRegister()); |
| 4199 | Location out = locations->Out(); |
| 4200 | __ xorl(out.AsRegister<CpuRegister>(), Immediate(1)); |
| 4201 | } |
| 4202 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4203 | void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4204 | LocationSummary* locations = |
| 4205 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 4206 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4207 | locations->SetInAt(i, Location::Any()); |
| 4208 | } |
| 4209 | locations->SetOut(Location::Any()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4210 | } |
| 4211 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 4212 | void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 4213 | LOG(FATAL) << "Unimplemented"; |
| 4214 | } |
| 4215 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4216 | void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4217 | /* |
| 4218 | * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4219 | * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model. |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4220 | * For those cases, all we need to ensure is that there is a scheduling barrier in place. |
| 4221 | */ |
| 4222 | switch (kind) { |
| 4223 | case MemBarrierKind::kAnyAny: { |
Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 4224 | MemoryFence(); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4225 | break; |
| 4226 | } |
| 4227 | case MemBarrierKind::kAnyStore: |
| 4228 | case MemBarrierKind::kLoadAny: |
| 4229 | case MemBarrierKind::kStoreStore: { |
| 4230 | // nop |
| 4231 | break; |
| 4232 | } |
Mark Mendell | 7aa04a1 | 2016-01-27 22:39:07 -0500 | [diff] [blame] | 4233 | case MemBarrierKind::kNTStoreStore: |
| 4234 | // Non-Temporal Store/Store needs an explicit fence. |
| 4235 | MemoryFence(/* non-temporal */ true); |
| 4236 | break; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | |
| 4240 | void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) { |
| 4241 | DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet()); |
| 4242 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4243 | bool object_field_get_with_read_barrier = |
| 4244 | kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4245 | LocationSummary* locations = |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4246 | new (GetGraph()->GetArena()) LocationSummary(instruction, |
| 4247 | object_field_get_with_read_barrier ? |
| 4248 | LocationSummary::kCallOnSlowPath : |
| 4249 | LocationSummary::kNoCall); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 4250 | if (object_field_get_with_read_barrier && kUseBakerReadBarrier) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 4251 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 4252 | } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4253 | locations->SetInAt(0, Location::RequiresRegister()); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4254 | if (Primitive::IsFloatingPointType(instruction->GetType())) { |
| 4255 | locations->SetOut(Location::RequiresFpuRegister()); |
| 4256 | } else { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4257 | // The output overlaps for an object field get when read barriers |
| 4258 | // are enabled: we do not want the move to overwrite the object's |
| 4259 | // location, as we need it to emit the read barrier. |
| 4260 | locations->SetOut( |
| 4261 | Location::RequiresRegister(), |
| 4262 | object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4263 | } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction, |
| 4267 | const FieldInfo& field_info) { |
| 4268 | DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet()); |
| 4269 | |
| 4270 | LocationSummary* locations = instruction->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4271 | Location base_loc = locations->InAt(0); |
| 4272 | CpuRegister base = base_loc.AsRegister<CpuRegister>(); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4273 | Location out = locations->Out(); |
| 4274 | bool is_volatile = field_info.IsVolatile(); |
| 4275 | Primitive::Type field_type = field_info.GetFieldType(); |
| 4276 | uint32_t offset = field_info.GetFieldOffset().Uint32Value(); |
| 4277 | |
| 4278 | switch (field_type) { |
| 4279 | case Primitive::kPrimBoolean: { |
| 4280 | __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4281 | break; |
| 4282 | } |
| 4283 | |
| 4284 | case Primitive::kPrimByte: { |
| 4285 | __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4286 | break; |
| 4287 | } |
| 4288 | |
| 4289 | case Primitive::kPrimShort: { |
| 4290 | __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4291 | break; |
| 4292 | } |
| 4293 | |
| 4294 | case Primitive::kPrimChar: { |
| 4295 | __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4296 | break; |
| 4297 | } |
| 4298 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4299 | case Primitive::kPrimInt: { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4300 | __ movl(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4301 | break; |
| 4302 | } |
| 4303 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4304 | case Primitive::kPrimNot: { |
| 4305 | // /* HeapReference<Object> */ out = *(base + offset) |
| 4306 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4307 | // Note that a potential implicit null check is handled in this |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 4308 | // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4309 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 4310 | instruction, out, base, offset, /* needs_null_check */ true); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4311 | if (is_volatile) { |
| 4312 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 4313 | } |
| 4314 | } else { |
| 4315 | __ movl(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4316 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4317 | if (is_volatile) { |
| 4318 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 4319 | } |
| 4320 | // If read barriers are enabled, emit read barriers other than |
| 4321 | // Baker's using a slow path (and also unpoison the loaded |
| 4322 | // reference, if heap poisoning is enabled). |
| 4323 | codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset); |
| 4324 | } |
| 4325 | break; |
| 4326 | } |
| 4327 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4328 | case Primitive::kPrimLong: { |
| 4329 | __ movq(out.AsRegister<CpuRegister>(), Address(base, offset)); |
| 4330 | break; |
| 4331 | } |
| 4332 | |
| 4333 | case Primitive::kPrimFloat: { |
| 4334 | __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset)); |
| 4335 | break; |
| 4336 | } |
| 4337 | |
| 4338 | case Primitive::kPrimDouble: { |
| 4339 | __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset)); |
| 4340 | break; |
| 4341 | } |
| 4342 | |
| 4343 | case Primitive::kPrimVoid: |
| 4344 | LOG(FATAL) << "Unreachable type " << field_type; |
| 4345 | UNREACHABLE(); |
| 4346 | } |
| 4347 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4348 | if (field_type == Primitive::kPrimNot) { |
| 4349 | // Potential implicit null checks, in the case of reference |
| 4350 | // fields, are handled in the previous switch statement. |
| 4351 | } else { |
| 4352 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4353 | } |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4354 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4355 | if (is_volatile) { |
| 4356 | if (field_type == Primitive::kPrimNot) { |
| 4357 | // Memory barriers, in the case of references, are also handled |
| 4358 | // in the previous switch statement. |
| 4359 | } else { |
| 4360 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 4361 | } |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4362 | } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction, |
| 4366 | const FieldInfo& field_info) { |
| 4367 | DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet()); |
| 4368 | |
| 4369 | LocationSummary* locations = |
| 4370 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4371 | Primitive::Type field_type = field_info.GetFieldType(); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4372 | bool is_volatile = field_info.IsVolatile(); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4373 | bool needs_write_barrier = |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4374 | CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1)); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4375 | |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 4376 | locations->SetInAt(0, Location::RequiresRegister()); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4377 | if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4378 | if (is_volatile) { |
| 4379 | // In order to satisfy the semantics of volatile, this must be a single instruction store. |
| 4380 | locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1))); |
| 4381 | } else { |
| 4382 | locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1))); |
| 4383 | } |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4384 | } else { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4385 | if (is_volatile) { |
| 4386 | // In order to satisfy the semantics of volatile, this must be a single instruction store. |
| 4387 | locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1))); |
| 4388 | } else { |
| 4389 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 4390 | } |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4391 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4392 | if (needs_write_barrier) { |
Nicolas Geoffray | 9ae0daa | 2014-09-30 22:40:23 +0100 | [diff] [blame] | 4393 | // Temporary registers for the write barrier. |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4394 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 4395 | locations->AddTemp(Location::RequiresRegister()); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4396 | } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) { |
| 4397 | // Temporary register for the reference poisoning. |
Nicolas Geoffray | 1a43dd7 | 2014-07-17 15:15:34 +0100 | [diff] [blame] | 4398 | locations->AddTemp(Location::RequiresRegister()); |
| 4399 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4400 | } |
| 4401 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4402 | void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4403 | const FieldInfo& field_info, |
| 4404 | bool value_can_be_null) { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4405 | DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet()); |
| 4406 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4407 | LocationSummary* locations = instruction->GetLocations(); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4408 | CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>(); |
| 4409 | Location value = locations->InAt(1); |
| 4410 | bool is_volatile = field_info.IsVolatile(); |
| 4411 | Primitive::Type field_type = field_info.GetFieldType(); |
| 4412 | uint32_t offset = field_info.GetFieldOffset().Uint32Value(); |
| 4413 | |
| 4414 | if (is_volatile) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4415 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4416 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4417 | |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4418 | bool maybe_record_implicit_null_check_done = false; |
| 4419 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4420 | switch (field_type) { |
| 4421 | case Primitive::kPrimBoolean: |
| 4422 | case Primitive::kPrimByte: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4423 | if (value.IsConstant()) { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4424 | int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4425 | __ movb(Address(base, offset), Immediate(v)); |
| 4426 | } else { |
| 4427 | __ movb(Address(base, offset), value.AsRegister<CpuRegister>()); |
| 4428 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4429 | break; |
| 4430 | } |
| 4431 | |
| 4432 | case Primitive::kPrimShort: |
| 4433 | case Primitive::kPrimChar: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4434 | if (value.IsConstant()) { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4435 | int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4436 | __ movw(Address(base, offset), Immediate(v)); |
| 4437 | } else { |
| 4438 | __ movw(Address(base, offset), value.AsRegister<CpuRegister>()); |
| 4439 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4440 | break; |
| 4441 | } |
| 4442 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4443 | case Primitive::kPrimInt: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4444 | case Primitive::kPrimNot: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4445 | if (value.IsConstant()) { |
| 4446 | int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant()); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4447 | // `field_type == Primitive::kPrimNot` implies `v == 0`. |
| 4448 | DCHECK((field_type != Primitive::kPrimNot) || (v == 0)); |
| 4449 | // Note: if heap poisoning is enabled, no need to poison |
| 4450 | // (negate) `v` if it is a reference, as it would be null. |
Roland Levillain | 06b66d0 | 2015-07-01 12:47:25 +0100 | [diff] [blame] | 4451 | __ movl(Address(base, offset), Immediate(v)); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4452 | } else { |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4453 | if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) { |
| 4454 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 4455 | __ movl(temp, value.AsRegister<CpuRegister>()); |
| 4456 | __ PoisonHeapReference(temp); |
| 4457 | __ movl(Address(base, offset), temp); |
| 4458 | } else { |
| 4459 | __ movl(Address(base, offset), value.AsRegister<CpuRegister>()); |
| 4460 | } |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4461 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4462 | break; |
| 4463 | } |
| 4464 | |
| 4465 | case Primitive::kPrimLong: { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4466 | if (value.IsConstant()) { |
| 4467 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4468 | codegen_->MoveInt64ToAddress(Address(base, offset), |
| 4469 | Address(base, offset + sizeof(int32_t)), |
| 4470 | v, |
| 4471 | instruction); |
| 4472 | maybe_record_implicit_null_check_done = true; |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4473 | } else { |
| 4474 | __ movq(Address(base, offset), value.AsRegister<CpuRegister>()); |
| 4475 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4476 | break; |
| 4477 | } |
| 4478 | |
Nicolas Geoffray | 52e832b | 2014-11-06 15:15:31 +0000 | [diff] [blame] | 4479 | case Primitive::kPrimFloat: { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4480 | if (value.IsConstant()) { |
| 4481 | int32_t v = |
| 4482 | bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue()); |
| 4483 | __ movl(Address(base, offset), Immediate(v)); |
| 4484 | } else { |
| 4485 | __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>()); |
| 4486 | } |
Nicolas Geoffray | 52e832b | 2014-11-06 15:15:31 +0000 | [diff] [blame] | 4487 | break; |
| 4488 | } |
| 4489 | |
| 4490 | case Primitive::kPrimDouble: { |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4491 | if (value.IsConstant()) { |
| 4492 | int64_t v = |
| 4493 | bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue()); |
| 4494 | codegen_->MoveInt64ToAddress(Address(base, offset), |
| 4495 | Address(base, offset + sizeof(int32_t)), |
| 4496 | v, |
| 4497 | instruction); |
| 4498 | maybe_record_implicit_null_check_done = true; |
| 4499 | } else { |
| 4500 | __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>()); |
| 4501 | } |
Nicolas Geoffray | 52e832b | 2014-11-06 15:15:31 +0000 | [diff] [blame] | 4502 | break; |
| 4503 | } |
| 4504 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4505 | case Primitive::kPrimVoid: |
| 4506 | LOG(FATAL) << "Unreachable type " << field_type; |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 4507 | UNREACHABLE(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4508 | } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4509 | |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4510 | if (!maybe_record_implicit_null_check_done) { |
| 4511 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4512 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4513 | |
| 4514 | if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) { |
| 4515 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 4516 | CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>(); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4517 | codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4520 | if (is_volatile) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4521 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny); |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 4526 | HandleFieldSet(instruction, instruction->GetFieldInfo()); |
| 4527 | } |
| 4528 | |
| 4529 | void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4530 | HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull()); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4534 | HandleFieldGet(instruction); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4538 | HandleFieldGet(instruction, instruction->GetFieldInfo()); |
| 4539 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4540 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4541 | void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) { |
| 4542 | HandleFieldGet(instruction); |
| 4543 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4544 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4545 | void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) { |
| 4546 | HandleFieldGet(instruction, instruction->GetFieldInfo()); |
| 4547 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4548 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4549 | void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
| 4550 | HandleFieldSet(instruction, instruction->GetFieldInfo()); |
| 4551 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4552 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 4553 | void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 4554 | HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull()); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4555 | } |
| 4556 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 4557 | void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet( |
| 4558 | HUnresolvedInstanceFieldGet* instruction) { |
| 4559 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4560 | codegen_->CreateUnresolvedFieldLocationSummary( |
| 4561 | instruction, instruction->GetFieldType(), calling_convention); |
| 4562 | } |
| 4563 | |
| 4564 | void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet( |
| 4565 | HUnresolvedInstanceFieldGet* instruction) { |
| 4566 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4567 | codegen_->GenerateUnresolvedFieldAccess(instruction, |
| 4568 | instruction->GetFieldType(), |
| 4569 | instruction->GetFieldIndex(), |
| 4570 | instruction->GetDexPc(), |
| 4571 | calling_convention); |
| 4572 | } |
| 4573 | |
| 4574 | void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet( |
| 4575 | HUnresolvedInstanceFieldSet* instruction) { |
| 4576 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4577 | codegen_->CreateUnresolvedFieldLocationSummary( |
| 4578 | instruction, instruction->GetFieldType(), calling_convention); |
| 4579 | } |
| 4580 | |
| 4581 | void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet( |
| 4582 | HUnresolvedInstanceFieldSet* instruction) { |
| 4583 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4584 | codegen_->GenerateUnresolvedFieldAccess(instruction, |
| 4585 | instruction->GetFieldType(), |
| 4586 | instruction->GetFieldIndex(), |
| 4587 | instruction->GetDexPc(), |
| 4588 | calling_convention); |
| 4589 | } |
| 4590 | |
| 4591 | void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet( |
| 4592 | HUnresolvedStaticFieldGet* instruction) { |
| 4593 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4594 | codegen_->CreateUnresolvedFieldLocationSummary( |
| 4595 | instruction, instruction->GetFieldType(), calling_convention); |
| 4596 | } |
| 4597 | |
| 4598 | void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet( |
| 4599 | HUnresolvedStaticFieldGet* instruction) { |
| 4600 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4601 | codegen_->GenerateUnresolvedFieldAccess(instruction, |
| 4602 | instruction->GetFieldType(), |
| 4603 | instruction->GetFieldIndex(), |
| 4604 | instruction->GetDexPc(), |
| 4605 | calling_convention); |
| 4606 | } |
| 4607 | |
| 4608 | void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet( |
| 4609 | HUnresolvedStaticFieldSet* instruction) { |
| 4610 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4611 | codegen_->CreateUnresolvedFieldLocationSummary( |
| 4612 | instruction, instruction->GetFieldType(), calling_convention); |
| 4613 | } |
| 4614 | |
| 4615 | void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet( |
| 4616 | HUnresolvedStaticFieldSet* instruction) { |
| 4617 | FieldAccessCallingConventionX86_64 calling_convention; |
| 4618 | codegen_->GenerateUnresolvedFieldAccess(instruction, |
| 4619 | instruction->GetFieldType(), |
| 4620 | instruction->GetFieldIndex(), |
| 4621 | instruction->GetDexPc(), |
| 4622 | calling_convention); |
| 4623 | } |
| 4624 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4625 | void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 4626 | LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction); |
| 4627 | Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks() |
| 4628 | ? Location::RequiresRegister() |
| 4629 | : Location::Any(); |
| 4630 | locations->SetInAt(0, loc); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4631 | } |
| 4632 | |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 4633 | void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) { |
| 4634 | if (CanMoveNullCheckToUser(instruction)) { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4635 | return; |
| 4636 | } |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 4637 | LocationSummary* locations = instruction->GetLocations(); |
| 4638 | Location obj = locations->InAt(0); |
| 4639 | |
| 4640 | __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0)); |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 4641 | RecordPcInfo(instruction, instruction->GetDexPc()); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 4644 | void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) { |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 4645 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction); |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 4646 | AddSlowPath(slow_path); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4647 | |
| 4648 | LocationSummary* locations = instruction->GetLocations(); |
| 4649 | Location obj = locations->InAt(0); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4650 | |
| 4651 | if (obj.IsRegister()) { |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 4652 | __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 4653 | } else if (obj.IsStackSlot()) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4654 | __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0)); |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 4655 | } else { |
| 4656 | DCHECK(obj.IsConstant()) << obj; |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 4657 | DCHECK(obj.GetConstant()->IsNullConstant()); |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 4658 | __ jmp(slow_path->GetEntryLabel()); |
| 4659 | return; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 4660 | } |
| 4661 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 4662 | } |
| 4663 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 4664 | void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) { |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 4665 | codegen_->GenerateNullCheck(instruction); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4668 | void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4669 | bool object_array_get_with_read_barrier = |
| 4670 | kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4671 | LocationSummary* locations = |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4672 | new (GetGraph()->GetArena()) LocationSummary(instruction, |
| 4673 | object_array_get_with_read_barrier ? |
| 4674 | LocationSummary::kCallOnSlowPath : |
| 4675 | LocationSummary::kNoCall); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 4676 | if (object_array_get_with_read_barrier && kUseBakerReadBarrier) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 4677 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 4678 | } |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 4679 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 4680 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4681 | if (Primitive::IsFloatingPointType(instruction->GetType())) { |
| 4682 | locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); |
| 4683 | } else { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4684 | // The output overlaps for an object array get when read barriers |
| 4685 | // are enabled: we do not want the move to overwrite the array's |
| 4686 | // location, as we need it to emit the read barrier. |
| 4687 | locations->SetOut( |
| 4688 | Location::RequiresRegister(), |
| 4689 | object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 4690 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) { |
| 4694 | LocationSummary* locations = instruction->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4695 | Location obj_loc = locations->InAt(0); |
| 4696 | CpuRegister obj = obj_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4697 | Location index = locations->InAt(1); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4698 | Location out_loc = locations->Out(); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 4699 | uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4700 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4701 | Primitive::Type type = instruction->GetType(); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4702 | switch (type) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4703 | case Primitive::kPrimBoolean: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4704 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4705 | __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4706 | break; |
| 4707 | } |
| 4708 | |
| 4709 | case Primitive::kPrimByte: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4710 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4711 | __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4712 | break; |
| 4713 | } |
| 4714 | |
| 4715 | case Primitive::kPrimShort: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4716 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4717 | __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4718 | break; |
| 4719 | } |
| 4720 | |
| 4721 | case Primitive::kPrimChar: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4722 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 4723 | if (mirror::kUseStringCompression && instruction->IsStringCharAt()) { |
| 4724 | // Branch cases into compressed and uncompressed for each index's type. |
| 4725 | uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 4726 | NearLabel done, not_compressed; |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 4727 | __ testl(Address(obj, count_offset), Immediate(1)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 4728 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 4729 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 4730 | "Expecting 0=compressed, 1=uncompressed"); |
| 4731 | __ j(kNotZero, ¬_compressed); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 4732 | __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset)); |
| 4733 | __ jmp(&done); |
| 4734 | __ Bind(¬_compressed); |
| 4735 | __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset)); |
| 4736 | __ Bind(&done); |
| 4737 | } else { |
| 4738 | __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset)); |
| 4739 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4740 | break; |
| 4741 | } |
| 4742 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4743 | case Primitive::kPrimInt: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4744 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4745 | __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4746 | break; |
| 4747 | } |
| 4748 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4749 | case Primitive::kPrimNot: { |
| 4750 | static_assert( |
| 4751 | sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t), |
| 4752 | "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes."); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4753 | // /* HeapReference<Object> */ out = |
| 4754 | // *(obj + data_offset + index * sizeof(HeapReference<Object>)) |
| 4755 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4756 | // Note that a potential implicit null check is handled in this |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 4757 | // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4758 | codegen_->GenerateArrayLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 4759 | instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4760 | } else { |
| 4761 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4762 | __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset)); |
| 4763 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4764 | // If read barriers are enabled, emit read barriers other than |
| 4765 | // Baker's using a slow path (and also unpoison the loaded |
| 4766 | // reference, if heap poisoning is enabled). |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4767 | if (index.IsConstant()) { |
| 4768 | uint32_t offset = |
| 4769 | (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4770 | codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset); |
| 4771 | } else { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4772 | codegen_->MaybeGenerateReadBarrierSlow( |
| 4773 | instruction, out_loc, out_loc, obj_loc, data_offset, index); |
| 4774 | } |
| 4775 | } |
| 4776 | break; |
| 4777 | } |
| 4778 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4779 | case Primitive::kPrimLong: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4780 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4781 | __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4782 | break; |
| 4783 | } |
| 4784 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4785 | case Primitive::kPrimFloat: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4786 | XmmRegister out = out_loc.AsFpuRegister<XmmRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4787 | __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset)); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4788 | break; |
| 4789 | } |
| 4790 | |
| 4791 | case Primitive::kPrimDouble: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4792 | XmmRegister out = out_loc.AsFpuRegister<XmmRegister>(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4793 | __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset)); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 4794 | break; |
| 4795 | } |
| 4796 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4797 | case Primitive::kPrimVoid: |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4798 | LOG(FATAL) << "Unreachable type " << type; |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 4799 | UNREACHABLE(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4800 | } |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4801 | |
| 4802 | if (type == Primitive::kPrimNot) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 4803 | // Potential implicit null checks, in the case of reference |
| 4804 | // arrays, are handled in the previous switch statement. |
| 4805 | } else { |
| 4806 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4807 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4811 | Primitive::Type value_type = instruction->GetComponentType(); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4812 | |
| 4813 | bool needs_write_barrier = |
| 4814 | CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 4815 | bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck(); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4816 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4817 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary( |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4818 | instruction, |
Vladimir Marko | 8d49fd7 | 2016-08-25 15:20:47 +0100 | [diff] [blame] | 4819 | may_need_runtime_call_for_type_check ? |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4820 | LocationSummary::kCallOnSlowPath : |
| 4821 | LocationSummary::kNoCall); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4822 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4823 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4824 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 4825 | if (Primitive::IsFloatingPointType(value_type)) { |
| 4826 | locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2))); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4827 | } else { |
| 4828 | locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2))); |
| 4829 | } |
| 4830 | |
| 4831 | if (needs_write_barrier) { |
| 4832 | // Temporary registers for the write barrier. |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4833 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too. |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4834 | locations->AddTemp(Location::RequiresRegister()); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4835 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) { |
| 4839 | LocationSummary* locations = instruction->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4840 | Location array_loc = locations->InAt(0); |
| 4841 | CpuRegister array = array_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4842 | Location index = locations->InAt(1); |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 4843 | Location value = locations->InAt(2); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 4844 | Primitive::Type value_type = instruction->GetComponentType(); |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 4845 | bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck(); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4846 | bool needs_write_barrier = |
| 4847 | CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4848 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 4849 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 4850 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4851 | |
| 4852 | switch (value_type) { |
| 4853 | case Primitive::kPrimBoolean: |
| 4854 | case Primitive::kPrimByte: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4855 | uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4856 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4857 | if (value.IsRegister()) { |
| 4858 | __ movb(address, value.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4859 | } else { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4860 | __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4861 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4862 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4863 | break; |
| 4864 | } |
| 4865 | |
| 4866 | case Primitive::kPrimShort: |
| 4867 | case Primitive::kPrimChar: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4868 | uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4869 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4870 | if (value.IsRegister()) { |
| 4871 | __ movw(address, value.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4872 | } else { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4873 | DCHECK(value.IsConstant()) << value; |
| 4874 | __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4875 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4876 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4877 | break; |
| 4878 | } |
| 4879 | |
| 4880 | case Primitive::kPrimNot: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4881 | uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4882 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4883 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4884 | if (!value.IsRegister()) { |
| 4885 | // Just setting null. |
| 4886 | DCHECK(instruction->InputAt(2)->IsNullConstant()); |
| 4887 | DCHECK(value.IsConstant()) << value; |
| 4888 | __ movl(address, Immediate(0)); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4889 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4890 | DCHECK(!needs_write_barrier); |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 4891 | DCHECK(!may_need_runtime_call_for_type_check); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4892 | break; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 4893 | } |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4894 | |
| 4895 | DCHECK(needs_write_barrier); |
| 4896 | CpuRegister register_value = value.AsRegister<CpuRegister>(); |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4897 | // We cannot use a NearLabel for `done`, as its range may be too |
| 4898 | // short when Baker read barriers are enabled. |
| 4899 | Label done; |
| 4900 | NearLabel not_null, do_put; |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4901 | SlowPathCode* slow_path = nullptr; |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4902 | Location temp_loc = locations->GetTemp(0); |
| 4903 | CpuRegister temp = temp_loc.AsRegister<CpuRegister>(); |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 4904 | if (may_need_runtime_call_for_type_check) { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4905 | slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction); |
| 4906 | codegen_->AddSlowPath(slow_path); |
| 4907 | if (instruction->GetValueCanBeNull()) { |
| 4908 | __ testl(register_value, register_value); |
| 4909 | __ j(kNotEqual, ¬_null); |
| 4910 | __ movl(address, Immediate(0)); |
| 4911 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4912 | __ jmp(&done); |
| 4913 | __ Bind(¬_null); |
| 4914 | } |
| 4915 | |
Roland Levillain | 9d6e1f8 | 2016-09-05 15:57:33 +0100 | [diff] [blame] | 4916 | // Note that when Baker read barriers are enabled, the type |
| 4917 | // checks are performed without read barriers. This is fine, |
| 4918 | // even in the case where a class object is in the from-space |
| 4919 | // after the flip, as a comparison involving such a type would |
| 4920 | // not produce a false positive; it may of course produce a |
| 4921 | // false negative, in which case we would take the ArraySet |
| 4922 | // slow path. |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4923 | |
Roland Levillain | 9d6e1f8 | 2016-09-05 15:57:33 +0100 | [diff] [blame] | 4924 | // /* HeapReference<Class> */ temp = array->klass_ |
| 4925 | __ movl(temp, Address(array, class_offset)); |
| 4926 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4927 | __ MaybeUnpoisonHeapReference(temp); |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4928 | |
Roland Levillain | 9d6e1f8 | 2016-09-05 15:57:33 +0100 | [diff] [blame] | 4929 | // /* HeapReference<Class> */ temp = temp->component_type_ |
| 4930 | __ movl(temp, Address(temp, component_offset)); |
| 4931 | // If heap poisoning is enabled, no need to unpoison `temp` |
| 4932 | // nor the object reference in `register_value->klass`, as |
| 4933 | // we are comparing two poisoned references. |
| 4934 | __ cmpl(temp, Address(register_value, class_offset)); |
Roland Levillain | 16d9f94 | 2016-08-25 17:27:56 +0100 | [diff] [blame] | 4935 | |
Roland Levillain | 9d6e1f8 | 2016-09-05 15:57:33 +0100 | [diff] [blame] | 4936 | if (instruction->StaticTypeOfArrayIsObjectArray()) { |
| 4937 | __ j(kEqual, &do_put); |
| 4938 | // If heap poisoning is enabled, the `temp` reference has |
| 4939 | // not been unpoisoned yet; unpoison it now. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4940 | __ MaybeUnpoisonHeapReference(temp); |
| 4941 | |
Roland Levillain | 9d6e1f8 | 2016-09-05 15:57:33 +0100 | [diff] [blame] | 4942 | // If heap poisoning is enabled, no need to unpoison the |
| 4943 | // heap reference loaded below, as it is only used for a |
| 4944 | // comparison with null. |
| 4945 | __ cmpl(Address(temp, super_offset), Immediate(0)); |
| 4946 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 4947 | __ Bind(&do_put); |
| 4948 | } else { |
| 4949 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4950 | } |
| 4951 | } |
| 4952 | |
| 4953 | if (kPoisonHeapReferences) { |
| 4954 | __ movl(temp, register_value); |
| 4955 | __ PoisonHeapReference(temp); |
| 4956 | __ movl(address, temp); |
| 4957 | } else { |
| 4958 | __ movl(address, register_value); |
| 4959 | } |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 4960 | if (!may_need_runtime_call_for_type_check) { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4961 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 4962 | } |
| 4963 | |
| 4964 | CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 4965 | codegen_->MarkGCCard( |
| 4966 | temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull()); |
| 4967 | __ Bind(&done); |
| 4968 | |
| 4969 | if (slow_path != nullptr) { |
| 4970 | __ Bind(slow_path->GetExitLabel()); |
| 4971 | } |
| 4972 | |
| 4973 | break; |
| 4974 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 4975 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4976 | case Primitive::kPrimInt: { |
| 4977 | uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4978 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4979 | if (value.IsRegister()) { |
| 4980 | __ movl(address, value.AsRegister<CpuRegister>()); |
| 4981 | } else { |
| 4982 | DCHECK(value.IsConstant()) << value; |
| 4983 | int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant()); |
| 4984 | __ movl(address, Immediate(v)); |
| 4985 | } |
| 4986 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4987 | break; |
| 4988 | } |
| 4989 | |
| 4990 | case Primitive::kPrimLong: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4991 | uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4992 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4993 | if (value.IsRegister()) { |
| 4994 | __ movq(address, value.AsRegister<CpuRegister>()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 4995 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 4996 | } else { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 4997 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 4998 | Address address_high = |
| 4999 | CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t)); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 5000 | codegen_->MoveInt64ToAddress(address, address_high, v, instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5001 | } |
| 5002 | break; |
| 5003 | } |
| 5004 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5005 | case Primitive::kPrimFloat: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 5006 | uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 5007 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 5008 | if (value.IsFpuRegister()) { |
| 5009 | __ movss(address, value.AsFpuRegister<XmmRegister>()); |
| 5010 | } else { |
| 5011 | DCHECK(value.IsConstant()); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 5012 | int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 5013 | __ movl(address, Immediate(v)); |
| 5014 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5015 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5016 | break; |
| 5017 | } |
| 5018 | |
| 5019 | case Primitive::kPrimDouble: { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 5020 | uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value(); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 5021 | Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 5022 | if (value.IsFpuRegister()) { |
| 5023 | __ movsd(address, value.AsFpuRegister<XmmRegister>()); |
| 5024 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
| 5025 | } else { |
| 5026 | int64_t v = |
| 5027 | bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue()); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 5028 | Address address_high = |
| 5029 | CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t)); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 5030 | codegen_->MoveInt64ToAddress(address, address_high, v, instruction); |
| 5031 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5032 | break; |
| 5033 | } |
| 5034 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5035 | case Primitive::kPrimVoid: |
| 5036 | LOG(FATAL) << "Unreachable type " << instruction->GetType(); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 5037 | UNREACHABLE(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5042 | LocationSummary* locations = |
| 5043 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 5044 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5045 | if (!instruction->IsEmittedAtUseSite()) { |
| 5046 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 5047 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) { |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5051 | if (instruction->IsEmittedAtUseSite()) { |
| 5052 | return; |
| 5053 | } |
| 5054 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5055 | LocationSummary* locations = instruction->GetLocations(); |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 5056 | uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5057 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 5058 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5059 | __ movl(out, Address(obj, offset)); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5060 | codegen_->MaybeRecordImplicitNullCheck(instruction); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5061 | // Mask out most significant bit in case the array is String's array of char. |
| 5062 | if (mirror::kUseStringCompression && instruction->IsStringLength()) { |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 5063 | __ shrl(out, Immediate(1)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5064 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 5068 | RegisterSet caller_saves = RegisterSet::Empty(); |
| 5069 | InvokeRuntimeCallingConvention calling_convention; |
| 5070 | caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 5071 | caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 5072 | LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves); |
Mark Mendell | f60c90b | 2015-03-04 15:12:59 -0500 | [diff] [blame] | 5073 | locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0))); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5074 | HInstruction* length = instruction->InputAt(1); |
| 5075 | if (!length->IsEmittedAtUseSite()) { |
| 5076 | locations->SetInAt(1, Location::RegisterOrConstant(length)); |
| 5077 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) { |
| 5081 | LocationSummary* locations = instruction->GetLocations(); |
Mark Mendell | f60c90b | 2015-03-04 15:12:59 -0500 | [diff] [blame] | 5082 | Location index_loc = locations->InAt(0); |
| 5083 | Location length_loc = locations->InAt(1); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5084 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5085 | |
Mark Mendell | 99dbd68 | 2015-04-22 16:18:52 -0400 | [diff] [blame] | 5086 | if (length_loc.IsConstant()) { |
| 5087 | int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant()); |
| 5088 | if (index_loc.IsConstant()) { |
| 5089 | // BCE will remove the bounds check if we are guarenteed to pass. |
| 5090 | int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant()); |
| 5091 | if (index < 0 || index >= length) { |
| 5092 | codegen_->AddSlowPath(slow_path); |
| 5093 | __ jmp(slow_path->GetEntryLabel()); |
| 5094 | } else { |
| 5095 | // Some optimization after BCE may have generated this, and we should not |
| 5096 | // generate a bounds check if it is a valid range. |
| 5097 | } |
| 5098 | return; |
| 5099 | } |
| 5100 | |
| 5101 | // We have to reverse the jump condition because the length is the constant. |
| 5102 | CpuRegister index_reg = index_loc.AsRegister<CpuRegister>(); |
| 5103 | __ cmpl(index_reg, Immediate(length)); |
| 5104 | codegen_->AddSlowPath(slow_path); |
| 5105 | __ j(kAboveEqual, slow_path->GetEntryLabel()); |
Mark Mendell | f60c90b | 2015-03-04 15:12:59 -0500 | [diff] [blame] | 5106 | } else { |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5107 | HInstruction* array_length = instruction->InputAt(1); |
| 5108 | if (array_length->IsEmittedAtUseSite()) { |
| 5109 | // Address the length field in the array. |
| 5110 | DCHECK(array_length->IsArrayLength()); |
| 5111 | uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength()); |
| 5112 | Location array_loc = array_length->GetLocations()->InAt(0); |
| 5113 | Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5114 | if (mirror::kUseStringCompression && instruction->IsStringCharAt()) { |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 5115 | // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for |
| 5116 | // the string compression flag) with the in-memory length and avoid the temporary. |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5117 | CpuRegister length_reg = CpuRegister(TMP); |
| 5118 | __ movl(length_reg, array_len); |
| 5119 | codegen_->MaybeRecordImplicitNullCheck(array_length); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 5120 | __ shrl(length_reg, Immediate(1)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5121 | codegen_->GenerateIntCompare(length_reg, index_loc); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5122 | } else { |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 5123 | // Checking the bound for general case: |
| 5124 | // Array of char or String's array when the compression feature off. |
| 5125 | if (index_loc.IsConstant()) { |
| 5126 | int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant()); |
| 5127 | __ cmpl(array_len, Immediate(value)); |
| 5128 | } else { |
| 5129 | __ cmpl(array_len, index_loc.AsRegister<CpuRegister>()); |
| 5130 | } |
| 5131 | codegen_->MaybeRecordImplicitNullCheck(array_length); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 5132 | } |
Mark Mendell | 99dbd68 | 2015-04-22 16:18:52 -0400 | [diff] [blame] | 5133 | } else { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 5134 | codegen_->GenerateIntCompare(length_loc, index_loc); |
Mark Mendell | 99dbd68 | 2015-04-22 16:18:52 -0400 | [diff] [blame] | 5135 | } |
| 5136 | codegen_->AddSlowPath(slow_path); |
| 5137 | __ j(kBelowEqual, slow_path->GetEntryLabel()); |
Mark Mendell | f60c90b | 2015-03-04 15:12:59 -0500 | [diff] [blame] | 5138 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp, |
| 5142 | CpuRegister card, |
| 5143 | CpuRegister object, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 5144 | CpuRegister value, |
| 5145 | bool value_can_be_null) { |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 5146 | NearLabel is_null; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 5147 | if (value_can_be_null) { |
| 5148 | __ testl(value, value); |
| 5149 | __ j(kEqual, &is_null); |
| 5150 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5151 | __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5152 | /* no_rip */ true)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5153 | __ movq(temp, object); |
| 5154 | __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift)); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 5155 | __ movb(Address(temp, card, TIMES_1, 0), card); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 5156 | if (value_can_be_null) { |
| 5157 | __ Bind(&is_null); |
| 5158 | } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5159 | } |
| 5160 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5161 | void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 5162 | LOG(FATAL) << "Unimplemented"; |
| 5163 | } |
| 5164 | |
| 5165 | void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5166 | codegen_->GetMoveResolver()->EmitNativeCode(instruction); |
| 5167 | } |
| 5168 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5169 | void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) { |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5170 | LocationSummary* locations = |
| 5171 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath); |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 5172 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 5176 | HBasicBlock* block = instruction->GetBlock(); |
| 5177 | if (block->GetLoopInformation() != nullptr) { |
| 5178 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction); |
| 5179 | // The back edge will generate the suspend check. |
| 5180 | return; |
| 5181 | } |
| 5182 | if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) { |
| 5183 | // The goto will generate the suspend check. |
| 5184 | return; |
| 5185 | } |
| 5186 | GenerateSuspendCheck(instruction, nullptr); |
| 5187 | } |
| 5188 | |
| 5189 | void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction, |
| 5190 | HBasicBlock* successor) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5191 | SuspendCheckSlowPathX86_64* slow_path = |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 5192 | down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath()); |
| 5193 | if (slow_path == nullptr) { |
| 5194 | slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor); |
| 5195 | instruction->SetSlowPath(slow_path); |
| 5196 | codegen_->AddSlowPath(slow_path); |
| 5197 | if (successor != nullptr) { |
| 5198 | DCHECK(successor->IsLoopHeader()); |
| 5199 | codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction); |
| 5200 | } |
| 5201 | } else { |
| 5202 | DCHECK_EQ(slow_path->GetSuccessor(), successor); |
| 5203 | } |
| 5204 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5205 | __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5206 | /* no_rip */ true), |
| 5207 | Immediate(0)); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 5208 | if (successor == nullptr) { |
| 5209 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 5210 | __ Bind(slow_path->GetReturnLabel()); |
| 5211 | } else { |
| 5212 | __ j(kEqual, codegen_->GetLabelOf(successor)); |
| 5213 | __ jmp(slow_path->GetEntryLabel()); |
| 5214 | } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5217 | X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const { |
| 5218 | return codegen_->GetAssembler(); |
| 5219 | } |
| 5220 | |
| 5221 | void ParallelMoveResolverX86_64::EmitMove(size_t index) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 5222 | MoveOperands* move = moves_[index]; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5223 | Location source = move->GetSource(); |
| 5224 | Location destination = move->GetDestination(); |
| 5225 | |
| 5226 | if (source.IsRegister()) { |
| 5227 | if (destination.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5228 | __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5229 | } else if (destination.IsStackSlot()) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5230 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5231 | source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5232 | } else { |
| 5233 | DCHECK(destination.IsDoubleStackSlot()); |
| 5234 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5235 | source.AsRegister<CpuRegister>()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5236 | } |
| 5237 | } else if (source.IsStackSlot()) { |
| 5238 | if (destination.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5239 | __ movl(destination.AsRegister<CpuRegister>(), |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5240 | Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5241 | } else if (destination.IsFpuRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5242 | __ movss(destination.AsFpuRegister<XmmRegister>(), |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5243 | Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5244 | } else { |
| 5245 | DCHECK(destination.IsStackSlot()); |
| 5246 | __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 5247 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP)); |
| 5248 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5249 | } else if (source.IsDoubleStackSlot()) { |
| 5250 | if (destination.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5251 | __ movq(destination.AsRegister<CpuRegister>(), |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5252 | Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5253 | } else if (destination.IsFpuRegister()) { |
Roland Levillain | 199f336 | 2014-11-27 17:15:16 +0000 | [diff] [blame] | 5254 | __ movsd(destination.AsFpuRegister<XmmRegister>(), |
| 5255 | Address(CpuRegister(RSP), source.GetStackIndex())); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5256 | } else { |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 5257 | DCHECK(destination.IsDoubleStackSlot()) << destination; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5258 | __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 5259 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP)); |
| 5260 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5261 | } else if (source.IsConstant()) { |
| 5262 | HConstant* constant = source.GetConstant(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 5263 | if (constant->IsIntConstant() || constant->IsNullConstant()) { |
| 5264 | int32_t value = CodeGenerator::GetInt32ValueOf(constant); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5265 | if (destination.IsRegister()) { |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 5266 | if (value == 0) { |
| 5267 | __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>()); |
| 5268 | } else { |
| 5269 | __ movl(destination.AsRegister<CpuRegister>(), Immediate(value)); |
| 5270 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5271 | } else { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5272 | DCHECK(destination.IsStackSlot()) << destination; |
Nicolas Geoffray | 748f140 | 2015-01-27 08:17:54 +0000 | [diff] [blame] | 5273 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value)); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5274 | } |
| 5275 | } else if (constant->IsLongConstant()) { |
| 5276 | int64_t value = constant->AsLongConstant()->GetValue(); |
| 5277 | if (destination.IsRegister()) { |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 5278 | codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5279 | } else { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5280 | DCHECK(destination.IsDoubleStackSlot()) << destination; |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 5281 | codegen_->Store64BitValueToStack(destination, value); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5282 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5283 | } else if (constant->IsFloatConstant()) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 5284 | float fp_value = constant->AsFloatConstant()->GetValue(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5285 | if (destination.IsFpuRegister()) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 5286 | XmmRegister dest = destination.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 5287 | codegen_->Load32BitValue(dest, fp_value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5288 | } else { |
| 5289 | DCHECK(destination.IsStackSlot()) << destination; |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 5290 | Immediate imm(bit_cast<int32_t, float>(fp_value)); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5291 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm); |
| 5292 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5293 | } else { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5294 | DCHECK(constant->IsDoubleConstant()) << constant->DebugName(); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 5295 | double fp_value = constant->AsDoubleConstant()->GetValue(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 5296 | int64_t value = bit_cast<int64_t, double>(fp_value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5297 | if (destination.IsFpuRegister()) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 5298 | XmmRegister dest = destination.AsFpuRegister<XmmRegister>(); |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 5299 | codegen_->Load64BitValue(dest, fp_value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5300 | } else { |
| 5301 | DCHECK(destination.IsDoubleStackSlot()) << destination; |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 5302 | codegen_->Store64BitValueToStack(destination, value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5303 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 5304 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5305 | } else if (source.IsFpuRegister()) { |
| 5306 | if (destination.IsFpuRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5307 | __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5308 | } else if (destination.IsStackSlot()) { |
| 5309 | __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5310 | source.AsFpuRegister<XmmRegister>()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5311 | } else { |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 5312 | DCHECK(destination.IsDoubleStackSlot()) << destination; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5313 | __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()), |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5314 | source.AsFpuRegister<XmmRegister>()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5315 | } |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5319 | void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5320 | __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem)); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5321 | __ movl(Address(CpuRegister(RSP), mem), reg); |
| 5322 | __ movl(reg, CpuRegister(TMP)); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5325 | void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) { |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5326 | ScratchRegisterScope ensure_scratch( |
| 5327 | this, TMP, RAX, codegen_->GetNumberOfCoreRegisters()); |
| 5328 | |
| 5329 | int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0; |
| 5330 | __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset)); |
| 5331 | __ movl(CpuRegister(ensure_scratch.GetRegister()), |
| 5332 | Address(CpuRegister(RSP), mem2 + stack_offset)); |
| 5333 | __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP)); |
| 5334 | __ movl(Address(CpuRegister(RSP), mem1 + stack_offset), |
| 5335 | CpuRegister(ensure_scratch.GetRegister())); |
| 5336 | } |
| 5337 | |
Mark Mendell | 8a1c728 | 2015-06-29 15:41:28 -0400 | [diff] [blame] | 5338 | void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) { |
| 5339 | __ movq(CpuRegister(TMP), reg1); |
| 5340 | __ movq(reg1, reg2); |
| 5341 | __ movq(reg2, CpuRegister(TMP)); |
| 5342 | } |
| 5343 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5344 | void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) { |
| 5345 | __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem)); |
| 5346 | __ movq(Address(CpuRegister(RSP), mem), reg); |
| 5347 | __ movq(reg, CpuRegister(TMP)); |
| 5348 | } |
| 5349 | |
| 5350 | void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) { |
| 5351 | ScratchRegisterScope ensure_scratch( |
Guillaume Sanchez | e14590b | 2015-04-15 18:57:27 +0000 | [diff] [blame] | 5352 | this, TMP, RAX, codegen_->GetNumberOfCoreRegisters()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5353 | |
Guillaume Sanchez | e14590b | 2015-04-15 18:57:27 +0000 | [diff] [blame] | 5354 | int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0; |
| 5355 | __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset)); |
| 5356 | __ movq(CpuRegister(ensure_scratch.GetRegister()), |
| 5357 | Address(CpuRegister(RSP), mem2 + stack_offset)); |
| 5358 | __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP)); |
| 5359 | __ movq(Address(CpuRegister(RSP), mem1 + stack_offset), |
| 5360 | CpuRegister(ensure_scratch.GetRegister())); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5361 | } |
| 5362 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5363 | void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) { |
| 5364 | __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem)); |
| 5365 | __ movss(Address(CpuRegister(RSP), mem), reg); |
| 5366 | __ movd(reg, CpuRegister(TMP)); |
| 5367 | } |
| 5368 | |
| 5369 | void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) { |
| 5370 | __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem)); |
| 5371 | __ movsd(Address(CpuRegister(RSP), mem), reg); |
| 5372 | __ movd(reg, CpuRegister(TMP)); |
| 5373 | } |
| 5374 | |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5375 | void ParallelMoveResolverX86_64::EmitSwap(size_t index) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 5376 | MoveOperands* move = moves_[index]; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5377 | Location source = move->GetSource(); |
| 5378 | Location destination = move->GetDestination(); |
| 5379 | |
| 5380 | if (source.IsRegister() && destination.IsRegister()) { |
Mark Mendell | 8a1c728 | 2015-06-29 15:41:28 -0400 | [diff] [blame] | 5381 | Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5382 | } else if (source.IsRegister() && destination.IsStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5383 | Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5384 | } else if (source.IsStackSlot() && destination.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5385 | Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5386 | } else if (source.IsStackSlot() && destination.IsStackSlot()) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5387 | Exchange32(destination.GetStackIndex(), source.GetStackIndex()); |
| 5388 | } else if (source.IsRegister() && destination.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5389 | Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5390 | } else if (source.IsDoubleStackSlot() && destination.IsRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5391 | Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 5392 | } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) { |
| 5393 | Exchange64(destination.GetStackIndex(), source.GetStackIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5394 | } else if (source.IsFpuRegister() && destination.IsFpuRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5395 | __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>()); |
| 5396 | __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>()); |
| 5397 | __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP)); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5398 | } else if (source.IsFpuRegister() && destination.IsStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5399 | Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5400 | } else if (source.IsStackSlot() && destination.IsFpuRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5401 | Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5402 | } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5403 | Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5404 | } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 5405 | Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex()); |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5406 | } else { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5407 | LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination; |
Nicolas Geoffray | ecb2f9b | 2014-06-13 08:59:59 +0000 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | |
| 5411 | |
| 5412 | void ParallelMoveResolverX86_64::SpillScratch(int reg) { |
| 5413 | __ pushq(CpuRegister(reg)); |
| 5414 | } |
| 5415 | |
| 5416 | |
| 5417 | void ParallelMoveResolverX86_64::RestoreScratch(int reg) { |
| 5418 | __ popq(CpuRegister(reg)); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 5419 | } |
| 5420 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5421 | void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck( |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 5422 | SlowPathCode* slow_path, CpuRegister class_reg) { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5423 | __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()), |
| 5424 | Immediate(mirror::Class::kStatusInitialized)); |
| 5425 | __ j(kLess, slow_path->GetEntryLabel()); |
| 5426 | __ Bind(slow_path->GetExitLabel()); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5427 | // No need for memory fence, thanks to the x86-64 memory model. |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5430 | HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind( |
| 5431 | HLoadClass::LoadKind desired_class_load_kind) { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5432 | switch (desired_class_load_kind) { |
| 5433 | case HLoadClass::LoadKind::kReferrersClass: |
| 5434 | break; |
| 5435 | case HLoadClass::LoadKind::kBootImageLinkTimeAddress: |
| 5436 | DCHECK(!GetCompilerOptions().GetCompilePic()); |
| 5437 | // We prefer the always-available RIP-relative address for the x86-64 boot image. |
| 5438 | return HLoadClass::LoadKind::kBootImageLinkTimePcRelative; |
| 5439 | case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: |
| 5440 | DCHECK(GetCompilerOptions().GetCompilePic()); |
| 5441 | break; |
| 5442 | case HLoadClass::LoadKind::kBootImageAddress: |
| 5443 | break; |
| 5444 | case HLoadClass::LoadKind::kDexCacheAddress: |
| 5445 | DCHECK(Runtime::Current()->UseJitCompilation()); |
| 5446 | break; |
| 5447 | case HLoadClass::LoadKind::kDexCachePcRelative: |
| 5448 | DCHECK(!Runtime::Current()->UseJitCompilation()); |
| 5449 | break; |
| 5450 | case HLoadClass::LoadKind::kDexCacheViaMethod: |
| 5451 | break; |
| 5452 | } |
| 5453 | return desired_class_load_kind; |
| 5454 | } |
| 5455 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5456 | void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5457 | if (cls->NeedsAccessCheck()) { |
| 5458 | InvokeRuntimeCallingConvention calling_convention; |
| 5459 | CodeGenerator::CreateLoadClassLocationSummary( |
| 5460 | cls, |
| 5461 | Location::RegisterLocation(calling_convention.GetRegisterAt(0)), |
| 5462 | Location::RegisterLocation(RAX), |
| 5463 | /* code_generator_supports_read_barrier */ true); |
| 5464 | return; |
| 5465 | } |
| 5466 | |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5467 | const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage(); |
| 5468 | LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier) |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5469 | ? LocationSummary::kCallOnSlowPath |
| 5470 | : LocationSummary::kNoCall; |
| 5471 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind); |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5472 | if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 5473 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5474 | } |
| 5475 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5476 | HLoadClass::LoadKind load_kind = cls->GetLoadKind(); |
| 5477 | if (load_kind == HLoadClass::LoadKind::kReferrersClass || |
| 5478 | load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) { |
| 5479 | locations->SetInAt(0, Location::RequiresRegister()); |
| 5480 | } |
| 5481 | locations->SetOut(Location::RequiresRegister()); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) { |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 5485 | LocationSummary* locations = cls->GetLocations(); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 5486 | if (cls->NeedsAccessCheck()) { |
| 5487 | codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex()); |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 5488 | codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc()); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 5489 | CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>(); |
Calin Juravle | 580b609 | 2015-10-06 17:35:58 +0100 | [diff] [blame] | 5490 | return; |
| 5491 | } |
| 5492 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5493 | Location out_loc = locations->Out(); |
| 5494 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5495 | |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5496 | const ReadBarrierOption read_barrier_option = cls->IsInBootImage() |
| 5497 | ? kWithoutReadBarrier |
| 5498 | : kCompilerReadBarrierOption; |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5499 | bool generate_null_check = false; |
| 5500 | switch (cls->GetLoadKind()) { |
| 5501 | case HLoadClass::LoadKind::kReferrersClass: { |
| 5502 | DCHECK(!cls->CanCallRuntime()); |
| 5503 | DCHECK(!cls->MustGenerateClinitCheck()); |
| 5504 | // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_ |
| 5505 | CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>(); |
| 5506 | GenerateGcRootFieldLoad( |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5507 | cls, |
| 5508 | out_loc, |
| 5509 | Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()), |
Roland Levillain | 00468f3 | 2016-10-27 18:02:48 +0100 | [diff] [blame] | 5510 | /* fixup_label */ nullptr, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5511 | read_barrier_option); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5512 | break; |
| 5513 | } |
| 5514 | case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5515 | DCHECK_EQ(read_barrier_option, kWithoutReadBarrier); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5516 | __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false)); |
| 5517 | codegen_->RecordTypePatch(cls); |
| 5518 | break; |
| 5519 | case HLoadClass::LoadKind::kBootImageAddress: { |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5520 | DCHECK_EQ(read_barrier_option, kWithoutReadBarrier); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5521 | DCHECK_NE(cls->GetAddress(), 0u); |
| 5522 | uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress()); |
| 5523 | __ movl(out, Immediate(address)); // Zero-extended. |
| 5524 | codegen_->RecordSimplePatch(); |
| 5525 | break; |
| 5526 | } |
| 5527 | case HLoadClass::LoadKind::kDexCacheAddress: { |
| 5528 | DCHECK_NE(cls->GetAddress(), 0u); |
| 5529 | // /* GcRoot<mirror::Class> */ out = *address |
| 5530 | if (IsUint<32>(cls->GetAddress())) { |
| 5531 | Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true); |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5532 | GenerateGcRootFieldLoad(cls, |
| 5533 | out_loc, |
| 5534 | address, |
Roland Levillain | 00468f3 | 2016-10-27 18:02:48 +0100 | [diff] [blame] | 5535 | /* fixup_label */ nullptr, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5536 | read_barrier_option); |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5537 | } else { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5538 | // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address). |
| 5539 | __ movq(out, Immediate(cls->GetAddress())); |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5540 | GenerateGcRootFieldLoad(cls, |
| 5541 | out_loc, |
| 5542 | Address(out, 0), |
Roland Levillain | 00468f3 | 2016-10-27 18:02:48 +0100 | [diff] [blame] | 5543 | /* fixup_label */ nullptr, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5544 | read_barrier_option); |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5545 | } |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5546 | generate_null_check = !cls->IsInDexCache(); |
| 5547 | break; |
| 5548 | } |
| 5549 | case HLoadClass::LoadKind::kDexCachePcRelative: { |
| 5550 | uint32_t offset = cls->GetDexCacheElementOffset(); |
| 5551 | Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset); |
| 5552 | Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, |
| 5553 | /* no_rip */ false); |
| 5554 | // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */ |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5555 | GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5556 | generate_null_check = !cls->IsInDexCache(); |
| 5557 | break; |
| 5558 | } |
| 5559 | case HLoadClass::LoadKind::kDexCacheViaMethod: { |
| 5560 | // /* GcRoot<mirror::Class>[] */ out = |
| 5561 | // current_method.ptr_sized_fields_->dex_cache_resolved_types_ |
| 5562 | CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>(); |
| 5563 | __ movq(out, |
| 5564 | Address(current_method, |
| 5565 | ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value())); |
| 5566 | // /* GcRoot<mirror::Class> */ out = out[type_index] |
| 5567 | GenerateGcRootFieldLoad( |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 5568 | cls, |
| 5569 | out_loc, |
| 5570 | Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())), |
Roland Levillain | 00468f3 | 2016-10-27 18:02:48 +0100 | [diff] [blame] | 5571 | /* fixup_label */ nullptr, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5572 | read_barrier_option); |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 5573 | generate_null_check = !cls->IsInDexCache(); |
| 5574 | break; |
| 5575 | } |
| 5576 | default: |
| 5577 | LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind(); |
| 5578 | UNREACHABLE(); |
| 5579 | } |
| 5580 | |
| 5581 | if (generate_null_check || cls->MustGenerateClinitCheck()) { |
| 5582 | DCHECK(cls->CanCallRuntime()); |
| 5583 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64( |
| 5584 | cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck()); |
| 5585 | codegen_->AddSlowPath(slow_path); |
| 5586 | if (generate_null_check) { |
| 5587 | __ testl(out, out); |
| 5588 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 5589 | } |
| 5590 | if (cls->MustGenerateClinitCheck()) { |
| 5591 | GenerateClassInitializationCheck(slow_path, out); |
| 5592 | } else { |
| 5593 | __ Bind(slow_path->GetExitLabel()); |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5594 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5595 | } |
| 5596 | } |
| 5597 | |
| 5598 | void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) { |
| 5599 | LocationSummary* locations = |
| 5600 | new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath); |
| 5601 | locations->SetInAt(0, Location::RequiresRegister()); |
| 5602 | if (check->HasUses()) { |
| 5603 | locations->SetOut(Location::SameAsFirstInput()); |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5608 | // We assume the class to not be null. |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 5609 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64( |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5610 | check->GetLoadClass(), check, check->GetDexPc(), true); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5611 | codegen_->AddSlowPath(slow_path); |
Roland Levillain | 199f336 | 2014-11-27 17:15:16 +0000 | [diff] [blame] | 5612 | GenerateClassInitializationCheck(slow_path, |
| 5613 | check->GetLocations()->InAt(0).AsRegister<CpuRegister>()); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5614 | } |
| 5615 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5616 | HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind( |
| 5617 | HLoadString::LoadKind desired_string_load_kind) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5618 | switch (desired_string_load_kind) { |
| 5619 | case HLoadString::LoadKind::kBootImageLinkTimeAddress: |
| 5620 | DCHECK(!GetCompilerOptions().GetCompilePic()); |
| 5621 | // We prefer the always-available RIP-relative address for the x86-64 boot image. |
| 5622 | return HLoadString::LoadKind::kBootImageLinkTimePcRelative; |
| 5623 | case HLoadString::LoadKind::kBootImageLinkTimePcRelative: |
| 5624 | DCHECK(GetCompilerOptions().GetCompilePic()); |
| 5625 | break; |
| 5626 | case HLoadString::LoadKind::kBootImageAddress: |
| 5627 | break; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 5628 | case HLoadString::LoadKind::kBssEntry: |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 5629 | DCHECK(!Runtime::Current()->UseJitCompilation()); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5630 | break; |
| 5631 | case HLoadString::LoadKind::kDexCacheViaMethod: |
| 5632 | break; |
| 5633 | } |
| 5634 | return desired_string_load_kind; |
| 5635 | } |
| 5636 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5637 | void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) { |
Nicolas Geoffray | 3395fbc | 2016-11-14 12:40:52 +0000 | [diff] [blame] | 5638 | LocationSummary::CallKind call_kind = load->NeedsEnvironment() |
| 5639 | ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) |
| 5640 | ? LocationSummary::kCallOnMainOnly |
| 5641 | : LocationSummary::kCallOnSlowPath) |
| 5642 | : LocationSummary::kNoCall; |
Nicolas Geoffray | 917d016 | 2015-11-24 18:25:35 +0000 | [diff] [blame] | 5643 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5644 | if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) { |
Christina Wadsworth | abb341b | 2016-08-31 16:29:44 -0700 | [diff] [blame] | 5645 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 5646 | } else { |
| 5647 | locations->SetOut(Location::RequiresRegister()); |
Vladimir Marko | 94ce9c2 | 2016-09-30 14:50:51 +0100 | [diff] [blame] | 5648 | if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) { |
| 5649 | if (!kUseReadBarrier || kUseBakerReadBarrier) { |
| 5650 | // Rely on the pResolveString and/or marking to save everything. |
| 5651 | // Custom calling convention: RAX serves as both input and output. |
| 5652 | RegisterSet caller_saves = RegisterSet::Empty(); |
| 5653 | caller_saves.Add(Location::RegisterLocation(RAX)); |
| 5654 | locations->SetCustomSlowPathCallerSaves(caller_saves); |
| 5655 | } else { |
| 5656 | // For non-Baker read barrier we have a temp-clobbering call. |
| 5657 | } |
| 5658 | } |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5659 | } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) { |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 5663 | LocationSummary* locations = load->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5664 | Location out_loc = locations->Out(); |
| 5665 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5666 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5667 | switch (load->GetLoadKind()) { |
| 5668 | case HLoadString::LoadKind::kBootImageLinkTimePcRelative: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5669 | __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 5670 | codegen_->RecordBootStringPatch(load); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5671 | return; // No dex cache slow path. |
| 5672 | } |
| 5673 | case HLoadString::LoadKind::kBootImageAddress: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5674 | DCHECK_NE(load->GetAddress(), 0u); |
| 5675 | uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress()); |
| 5676 | __ movl(out, Immediate(address)); // Zero-extended. |
| 5677 | codegen_->RecordSimplePatch(); |
| 5678 | return; // No dex cache slow path. |
| 5679 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 5680 | case HLoadString::LoadKind::kBssEntry: { |
| 5681 | Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, |
| 5682 | /* no_rip */ false); |
| 5683 | Label* fixup_label = codegen_->NewStringBssEntryPatch(load); |
| 5684 | // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */ |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5685 | GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 5686 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load); |
| 5687 | codegen_->AddSlowPath(slow_path); |
| 5688 | __ testl(out, out); |
| 5689 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 5690 | __ Bind(slow_path->GetExitLabel()); |
| 5691 | return; |
| 5692 | } |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5693 | default: |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 5694 | break; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 5695 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5696 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 5697 | // TODO: Re-add the compiler code to do string dex cache lookup again. |
Vladimir Marko | 94ce9c2 | 2016-09-30 14:50:51 +0100 | [diff] [blame] | 5698 | // Custom calling convention: RAX serves as both input and output. |
| 5699 | __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex())); |
Christina Wadsworth | abb341b | 2016-08-31 16:29:44 -0700 | [diff] [blame] | 5700 | codegen_->InvokeRuntime(kQuickResolveString, |
| 5701 | load, |
| 5702 | load->GetDexPc()); |
| 5703 | CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>(); |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 5706 | static Address GetExceptionTlsAddress() { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5707 | return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5708 | /* no_rip */ true); |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 5709 | } |
| 5710 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5711 | void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) { |
| 5712 | LocationSummary* locations = |
| 5713 | new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall); |
| 5714 | locations->SetOut(Location::RequiresRegister()); |
| 5715 | } |
| 5716 | |
| 5717 | void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) { |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 5718 | __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress()); |
| 5719 | } |
| 5720 | |
| 5721 | void LocationsBuilderX86_64::VisitClearException(HClearException* clear) { |
| 5722 | new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall); |
| 5723 | } |
| 5724 | |
| 5725 | void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) { |
| 5726 | __ gs()->movl(GetExceptionTlsAddress(), Immediate(0)); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) { |
| 5730 | LocationSummary* locations = |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 5731 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5732 | InvokeRuntimeCallingConvention calling_convention; |
| 5733 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 5734 | } |
| 5735 | |
| 5736 | void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) { |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 5737 | codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc()); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 5738 | CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>(); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 5741 | static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) { |
| 5742 | if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) { |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 5743 | // We need a temporary for holding the iftable length. |
| 5744 | return true; |
| 5745 | } |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5746 | return kEmitCompilerReadBarrier && |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 5747 | !kUseBakerReadBarrier && |
| 5748 | (type_check_kind == TypeCheckKind::kAbstractClassCheck || |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5749 | type_check_kind == TypeCheckKind::kClassHierarchyCheck || |
| 5750 | type_check_kind == TypeCheckKind::kArrayObjectCheck); |
| 5751 | } |
| 5752 | |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 5753 | static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) { |
| 5754 | return kEmitCompilerReadBarrier && |
| 5755 | !kUseBakerReadBarrier && |
| 5756 | (type_check_kind == TypeCheckKind::kAbstractClassCheck || |
| 5757 | type_check_kind == TypeCheckKind::kClassHierarchyCheck || |
| 5758 | type_check_kind == TypeCheckKind::kArrayObjectCheck); |
| 5759 | } |
| 5760 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5761 | void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5762 | LocationSummary::CallKind call_kind = LocationSummary::kNoCall; |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5763 | TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5764 | bool baker_read_barrier_slow_path = false; |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5765 | switch (type_check_kind) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5766 | case TypeCheckKind::kExactCheck: |
| 5767 | case TypeCheckKind::kAbstractClassCheck: |
| 5768 | case TypeCheckKind::kClassHierarchyCheck: |
| 5769 | case TypeCheckKind::kArrayObjectCheck: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5770 | call_kind = |
| 5771 | kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall; |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5772 | baker_read_barrier_slow_path = kUseBakerReadBarrier; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5773 | break; |
| 5774 | case TypeCheckKind::kArrayCheck: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5775 | case TypeCheckKind::kUnresolvedCheck: |
| 5776 | case TypeCheckKind::kInterfaceCheck: |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5777 | call_kind = LocationSummary::kCallOnSlowPath; |
| 5778 | break; |
| 5779 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5780 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5781 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5782 | if (baker_read_barrier_slow_path) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 5783 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 5784 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5785 | locations->SetInAt(0, Location::RequiresRegister()); |
| 5786 | locations->SetInAt(1, Location::Any()); |
| 5787 | // Note that TypeCheckSlowPathX86_64 uses this "out" register too. |
| 5788 | locations->SetOut(Location::RequiresRegister()); |
| 5789 | // When read barriers are enabled, we need a temporary register for |
| 5790 | // some cases. |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 5791 | if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5792 | locations->AddTemp(Location::RequiresRegister()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5793 | } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5796 | void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5797 | TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5798 | LocationSummary* locations = instruction->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5799 | Location obj_loc = locations->InAt(0); |
| 5800 | CpuRegister obj = obj_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5801 | Location cls = locations->InAt(1); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5802 | Location out_loc = locations->Out(); |
| 5803 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 5804 | Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ? |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5805 | locations->GetTemp(0) : |
| 5806 | Location::NoLocation(); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5807 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5808 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 5809 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 5810 | uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 5811 | SlowPathCode* slow_path = nullptr; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5812 | NearLabel done, zero; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5813 | |
| 5814 | // Return 0 if `obj` is null. |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 5815 | // Avoid null check if we know obj is not null. |
| 5816 | if (instruction->MustDoNullCheck()) { |
| 5817 | __ testl(obj, obj); |
| 5818 | __ j(kEqual, &zero); |
| 5819 | } |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5820 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5821 | switch (type_check_kind) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5822 | case TypeCheckKind::kExactCheck: { |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 5823 | // /* HeapReference<Class> */ out = obj->klass_ |
| 5824 | GenerateReferenceLoadTwoRegisters(instruction, |
| 5825 | out_loc, |
| 5826 | obj_loc, |
| 5827 | class_offset, |
| 5828 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5829 | if (cls.IsRegister()) { |
| 5830 | __ cmpl(out, cls.AsRegister<CpuRegister>()); |
| 5831 | } else { |
| 5832 | DCHECK(cls.IsStackSlot()) << cls; |
| 5833 | __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 5834 | } |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 5835 | if (zero.IsLinked()) { |
| 5836 | // Classes must be equal for the instanceof to succeed. |
| 5837 | __ j(kNotEqual, &zero); |
| 5838 | __ movl(out, Immediate(1)); |
| 5839 | __ jmp(&done); |
| 5840 | } else { |
| 5841 | __ setcc(kEqual, out); |
| 5842 | // setcc only sets the low byte. |
| 5843 | __ andl(out, Immediate(1)); |
| 5844 | } |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5845 | break; |
| 5846 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5847 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5848 | case TypeCheckKind::kAbstractClassCheck: { |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 5849 | // /* HeapReference<Class> */ out = obj->klass_ |
| 5850 | GenerateReferenceLoadTwoRegisters(instruction, |
| 5851 | out_loc, |
| 5852 | obj_loc, |
| 5853 | class_offset, |
| 5854 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5855 | // If the class is abstract, we eagerly fetch the super class of the |
| 5856 | // object to avoid doing a comparison we know will fail. |
| 5857 | NearLabel loop, success; |
| 5858 | __ Bind(&loop); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5859 | // /* HeapReference<Class> */ out = out->super_class_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 5860 | GenerateReferenceLoadOneRegister(instruction, |
| 5861 | out_loc, |
| 5862 | super_offset, |
| 5863 | maybe_temp_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5864 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5865 | __ testl(out, out); |
| 5866 | // If `out` is null, we use it for the result, and jump to `done`. |
| 5867 | __ j(kEqual, &done); |
| 5868 | if (cls.IsRegister()) { |
| 5869 | __ cmpl(out, cls.AsRegister<CpuRegister>()); |
| 5870 | } else { |
| 5871 | DCHECK(cls.IsStackSlot()) << cls; |
| 5872 | __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 5873 | } |
| 5874 | __ j(kNotEqual, &loop); |
| 5875 | __ movl(out, Immediate(1)); |
| 5876 | if (zero.IsLinked()) { |
| 5877 | __ jmp(&done); |
| 5878 | } |
| 5879 | break; |
| 5880 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5881 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5882 | case TypeCheckKind::kClassHierarchyCheck: { |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 5883 | // /* HeapReference<Class> */ out = obj->klass_ |
| 5884 | GenerateReferenceLoadTwoRegisters(instruction, |
| 5885 | out_loc, |
| 5886 | obj_loc, |
| 5887 | class_offset, |
| 5888 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5889 | // Walk over the class hierarchy to find a match. |
| 5890 | NearLabel loop, success; |
| 5891 | __ Bind(&loop); |
| 5892 | if (cls.IsRegister()) { |
| 5893 | __ cmpl(out, cls.AsRegister<CpuRegister>()); |
| 5894 | } else { |
| 5895 | DCHECK(cls.IsStackSlot()) << cls; |
| 5896 | __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 5897 | } |
| 5898 | __ j(kEqual, &success); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5899 | // /* HeapReference<Class> */ out = out->super_class_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 5900 | GenerateReferenceLoadOneRegister(instruction, |
| 5901 | out_loc, |
| 5902 | super_offset, |
| 5903 | maybe_temp_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5904 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5905 | __ testl(out, out); |
| 5906 | __ j(kNotEqual, &loop); |
| 5907 | // If `out` is null, we use it for the result, and jump to `done`. |
| 5908 | __ jmp(&done); |
| 5909 | __ Bind(&success); |
| 5910 | __ movl(out, Immediate(1)); |
| 5911 | if (zero.IsLinked()) { |
| 5912 | __ jmp(&done); |
| 5913 | } |
| 5914 | break; |
| 5915 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5916 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5917 | case TypeCheckKind::kArrayObjectCheck: { |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 5918 | // /* HeapReference<Class> */ out = obj->klass_ |
| 5919 | GenerateReferenceLoadTwoRegisters(instruction, |
| 5920 | out_loc, |
| 5921 | obj_loc, |
| 5922 | class_offset, |
| 5923 | kCompilerReadBarrierOption); |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 5924 | // Do an exact check. |
| 5925 | NearLabel exact_check; |
| 5926 | if (cls.IsRegister()) { |
| 5927 | __ cmpl(out, cls.AsRegister<CpuRegister>()); |
| 5928 | } else { |
| 5929 | DCHECK(cls.IsStackSlot()) << cls; |
| 5930 | __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 5931 | } |
| 5932 | __ j(kEqual, &exact_check); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5933 | // Otherwise, we need to check that the object's class is a non-primitive array. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5934 | // /* HeapReference<Class> */ out = out->component_type_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 5935 | GenerateReferenceLoadOneRegister(instruction, |
| 5936 | out_loc, |
| 5937 | component_offset, |
| 5938 | maybe_temp_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 5939 | kCompilerReadBarrierOption); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5940 | __ testl(out, out); |
| 5941 | // If `out` is null, we use it for the result, and jump to `done`. |
| 5942 | __ j(kEqual, &done); |
| 5943 | __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot)); |
| 5944 | __ j(kNotEqual, &zero); |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 5945 | __ Bind(&exact_check); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5946 | __ movl(out, Immediate(1)); |
| 5947 | __ jmp(&done); |
| 5948 | break; |
| 5949 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5950 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5951 | case TypeCheckKind::kArrayCheck: { |
Mathieu Chartier | 9fd8c60 | 2016-11-14 14:38:53 -0800 | [diff] [blame] | 5952 | // No read barrier since the slow path will retry upon failure. |
| 5953 | // /* HeapReference<Class> */ out = obj->klass_ |
| 5954 | GenerateReferenceLoadTwoRegisters(instruction, |
| 5955 | out_loc, |
| 5956 | obj_loc, |
| 5957 | class_offset, |
| 5958 | kWithoutReadBarrier); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5959 | if (cls.IsRegister()) { |
| 5960 | __ cmpl(out, cls.AsRegister<CpuRegister>()); |
| 5961 | } else { |
| 5962 | DCHECK(cls.IsStackSlot()) << cls; |
| 5963 | __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 5964 | } |
| 5965 | DCHECK(locations->OnlyCallsOnSlowPath()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5966 | slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction, |
| 5967 | /* is_fatal */ false); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5968 | codegen_->AddSlowPath(slow_path); |
| 5969 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 5970 | __ movl(out, Immediate(1)); |
| 5971 | if (zero.IsLinked()) { |
| 5972 | __ jmp(&done); |
| 5973 | } |
| 5974 | break; |
| 5975 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5976 | |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 5977 | case TypeCheckKind::kUnresolvedCheck: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5978 | case TypeCheckKind::kInterfaceCheck: { |
| 5979 | // Note that we indeed only call on slow path, but we always go |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 5980 | // into the slow path for the unresolved and interface check |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5981 | // cases. |
| 5982 | // |
| 5983 | // We cannot directly call the InstanceofNonTrivial runtime |
| 5984 | // entry point without resorting to a type checking slow path |
| 5985 | // here (i.e. by calling InvokeRuntime directly), as it would |
| 5986 | // require to assign fixed registers for the inputs of this |
| 5987 | // HInstanceOf instruction (following the runtime calling |
| 5988 | // convention), which might be cluttered by the potential first |
| 5989 | // read barrier emission at the beginning of this method. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 5990 | // |
| 5991 | // TODO: Introduce a new runtime entry point taking the object |
| 5992 | // to test (instead of its class) as argument, and let it deal |
| 5993 | // with the read barrier issues. This will let us refactor this |
| 5994 | // case of the `switch` code as it was previously (with a direct |
| 5995 | // call to the runtime not using a type checking slow path). |
| 5996 | // This should also be beneficial for the other cases above. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 5997 | DCHECK(locations->OnlyCallsOnSlowPath()); |
| 5998 | slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction, |
| 5999 | /* is_fatal */ false); |
| 6000 | codegen_->AddSlowPath(slow_path); |
| 6001 | __ jmp(slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6002 | if (zero.IsLinked()) { |
| 6003 | __ jmp(&done); |
| 6004 | } |
| 6005 | break; |
| 6006 | } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 6007 | } |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 6008 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6009 | if (zero.IsLinked()) { |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 6010 | __ Bind(&zero); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6011 | __ xorl(out, out); |
| 6012 | } |
| 6013 | |
| 6014 | if (done.IsLinked()) { |
| 6015 | __ Bind(&done); |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 6016 | } |
| 6017 | |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 6018 | if (slow_path != nullptr) { |
| 6019 | __ Bind(slow_path->GetExitLabel()); |
| 6020 | } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6023 | static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6024 | switch (type_check_kind) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6025 | case TypeCheckKind::kExactCheck: |
| 6026 | case TypeCheckKind::kAbstractClassCheck: |
| 6027 | case TypeCheckKind::kClassHierarchyCheck: |
| 6028 | case TypeCheckKind::kArrayObjectCheck: |
Andreas Gampe | b5f3d81 | 2016-11-04 19:25:20 -0700 | [diff] [blame] | 6029 | return !throws_into_catch && !kEmitCompilerReadBarrier; |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 6030 | case TypeCheckKind::kInterfaceCheck: |
| 6031 | return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6032 | case TypeCheckKind::kArrayCheck: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6033 | case TypeCheckKind::kUnresolvedCheck: |
Andreas Gampe | b5f3d81 | 2016-11-04 19:25:20 -0700 | [diff] [blame] | 6034 | return false; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6035 | } |
Andreas Gampe | b5f3d81 | 2016-11-04 19:25:20 -0700 | [diff] [blame] | 6036 | LOG(FATAL) << "Unreachable"; |
| 6037 | UNREACHABLE(); |
| 6038 | } |
| 6039 | |
| 6040 | void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) { |
| 6041 | bool throws_into_catch = instruction->CanThrowIntoCatchBlock(); |
| 6042 | TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); |
| 6043 | bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch); |
| 6044 | LocationSummary::CallKind call_kind = is_fatal_slow_path |
| 6045 | ? LocationSummary::kNoCall |
| 6046 | : LocationSummary::kCallOnSlowPath; |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6047 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); |
| 6048 | locations->SetInAt(0, Location::RequiresRegister()); |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6049 | if (type_check_kind == TypeCheckKind::kInterfaceCheck) { |
| 6050 | // Require a register for the interface check since there is a loop that compares the class to |
| 6051 | // a memory address. |
| 6052 | locations->SetInAt(1, Location::RequiresRegister()); |
| 6053 | } else { |
| 6054 | locations->SetInAt(1, Location::Any()); |
| 6055 | } |
| 6056 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6057 | // Note that TypeCheckSlowPathX86_64 uses this "temp" register too. |
| 6058 | locations->AddTemp(Location::RequiresRegister()); |
| 6059 | // When read barriers are enabled, we need an additional temporary |
| 6060 | // register for some cases. |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 6061 | if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6062 | locations->AddTemp(Location::RequiresRegister()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6063 | } |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6067 | TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6068 | LocationSummary* locations = instruction->GetLocations(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6069 | Location obj_loc = locations->InAt(0); |
| 6070 | CpuRegister obj = obj_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6071 | Location cls = locations->InAt(1); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6072 | Location temp_loc = locations->GetTemp(0); |
| 6073 | CpuRegister temp = temp_loc.AsRegister<CpuRegister>(); |
Nicolas Geoffray | 53b07bd | 2016-11-05 15:09:19 +0000 | [diff] [blame] | 6074 | Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ? |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6075 | locations->GetTemp(1) : |
| 6076 | Location::NoLocation(); |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6077 | const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 6078 | const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 6079 | const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 6080 | const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
| 6081 | const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value(); |
| 6082 | const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value(); |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6083 | const uint32_t object_array_data_offset = |
| 6084 | mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value(); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6085 | |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6086 | // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases |
| 6087 | // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding |
| 6088 | // read barriers is done for performance and code size reasons. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6089 | bool is_type_check_slow_path_fatal = |
Andreas Gampe | b5f3d81 | 2016-11-04 19:25:20 -0700 | [diff] [blame] | 6090 | IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6091 | SlowPathCode* type_check_slow_path = |
| 6092 | new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction, |
| 6093 | is_type_check_slow_path_fatal); |
| 6094 | codegen_->AddSlowPath(type_check_slow_path); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6095 | |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6096 | |
| 6097 | NearLabel done; |
| 6098 | // Avoid null check if we know obj is not null. |
| 6099 | if (instruction->MustDoNullCheck()) { |
| 6100 | __ testl(obj, obj); |
| 6101 | __ j(kEqual, &done); |
| 6102 | } |
| 6103 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6104 | switch (type_check_kind) { |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6105 | case TypeCheckKind::kExactCheck: |
| 6106 | case TypeCheckKind::kArrayCheck: { |
Roland Levillain | 8650378 | 2016-02-11 19:07:30 +0000 | [diff] [blame] | 6107 | // /* HeapReference<Class> */ temp = obj->klass_ |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6108 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6109 | temp_loc, |
| 6110 | obj_loc, |
| 6111 | class_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6112 | kWithoutReadBarrier); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6113 | if (cls.IsRegister()) { |
| 6114 | __ cmpl(temp, cls.AsRegister<CpuRegister>()); |
| 6115 | } else { |
| 6116 | DCHECK(cls.IsStackSlot()) << cls; |
| 6117 | __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 6118 | } |
| 6119 | // Jump to slow path for throwing the exception or doing a |
| 6120 | // more involved array check. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6121 | __ j(kNotEqual, type_check_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6122 | break; |
| 6123 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6124 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6125 | case TypeCheckKind::kAbstractClassCheck: { |
Roland Levillain | 8650378 | 2016-02-11 19:07:30 +0000 | [diff] [blame] | 6126 | // /* HeapReference<Class> */ temp = obj->klass_ |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6127 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6128 | temp_loc, |
| 6129 | obj_loc, |
| 6130 | class_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6131 | kWithoutReadBarrier); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6132 | // If the class is abstract, we eagerly fetch the super class of the |
| 6133 | // object to avoid doing a comparison we know will fail. |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6134 | NearLabel loop; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6135 | __ Bind(&loop); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6136 | // /* HeapReference<Class> */ temp = temp->super_class_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6137 | GenerateReferenceLoadOneRegister(instruction, |
| 6138 | temp_loc, |
| 6139 | super_offset, |
| 6140 | maybe_temp2_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6141 | kWithoutReadBarrier); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6142 | |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6143 | // If the class reference currently in `temp` is null, jump to the slow path to throw the |
| 6144 | // exception. |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6145 | __ testl(temp, temp); |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6146 | // Otherwise, compare the classes. |
| 6147 | __ j(kZero, type_check_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6148 | if (cls.IsRegister()) { |
| 6149 | __ cmpl(temp, cls.AsRegister<CpuRegister>()); |
| 6150 | } else { |
| 6151 | DCHECK(cls.IsStackSlot()) << cls; |
| 6152 | __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 6153 | } |
| 6154 | __ j(kNotEqual, &loop); |
| 6155 | break; |
| 6156 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6157 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6158 | case TypeCheckKind::kClassHierarchyCheck: { |
Roland Levillain | 8650378 | 2016-02-11 19:07:30 +0000 | [diff] [blame] | 6159 | // /* HeapReference<Class> */ temp = obj->klass_ |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6160 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6161 | temp_loc, |
| 6162 | obj_loc, |
| 6163 | class_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6164 | kWithoutReadBarrier); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6165 | // Walk over the class hierarchy to find a match. |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 6166 | NearLabel loop; |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6167 | __ Bind(&loop); |
| 6168 | if (cls.IsRegister()) { |
| 6169 | __ cmpl(temp, cls.AsRegister<CpuRegister>()); |
| 6170 | } else { |
| 6171 | DCHECK(cls.IsStackSlot()) << cls; |
| 6172 | __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 6173 | } |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 6174 | __ j(kEqual, &done); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6175 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6176 | // /* HeapReference<Class> */ temp = temp->super_class_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6177 | GenerateReferenceLoadOneRegister(instruction, |
| 6178 | temp_loc, |
| 6179 | super_offset, |
| 6180 | maybe_temp2_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6181 | kWithoutReadBarrier); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6182 | |
| 6183 | // If the class reference currently in `temp` is not null, jump |
| 6184 | // back at the beginning of the loop. |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6185 | __ testl(temp, temp); |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6186 | __ j(kNotZero, &loop); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6187 | // Otherwise, jump to the slow path to throw the exception. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6188 | __ jmp(type_check_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6189 | break; |
| 6190 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6191 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6192 | case TypeCheckKind::kArrayObjectCheck: { |
Roland Levillain | 8650378 | 2016-02-11 19:07:30 +0000 | [diff] [blame] | 6193 | // /* HeapReference<Class> */ temp = obj->klass_ |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6194 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6195 | temp_loc, |
| 6196 | obj_loc, |
| 6197 | class_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6198 | kWithoutReadBarrier); |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 6199 | // Do an exact check. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6200 | NearLabel check_non_primitive_component_type; |
Nicolas Geoffray | abfcf18 | 2015-09-21 18:41:21 +0100 | [diff] [blame] | 6201 | if (cls.IsRegister()) { |
| 6202 | __ cmpl(temp, cls.AsRegister<CpuRegister>()); |
| 6203 | } else { |
| 6204 | DCHECK(cls.IsStackSlot()) << cls; |
| 6205 | __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex())); |
| 6206 | } |
| 6207 | __ j(kEqual, &done); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6208 | |
| 6209 | // Otherwise, we need to check that the object's class is a non-primitive array. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6210 | // /* HeapReference<Class> */ temp = temp->component_type_ |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6211 | GenerateReferenceLoadOneRegister(instruction, |
| 6212 | temp_loc, |
| 6213 | component_offset, |
| 6214 | maybe_temp2_loc, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6215 | kWithoutReadBarrier); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6216 | |
| 6217 | // If the component type is not null (i.e. the object is indeed |
| 6218 | // an array), jump to label `check_non_primitive_component_type` |
| 6219 | // to further check that this component type is not a primitive |
| 6220 | // type. |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6221 | __ testl(temp, temp); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6222 | // Otherwise, jump to the slow path to throw the exception. |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6223 | __ j(kZero, type_check_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6224 | __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot)); |
Mathieu Chartier | b99f4d6 | 2016-11-07 16:17:26 -0800 | [diff] [blame] | 6225 | __ j(kNotEqual, type_check_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6226 | break; |
| 6227 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6228 | |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6229 | case TypeCheckKind::kUnresolvedCheck: { |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6230 | // We always go into the type check slow path for the unresolved case. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6231 | // |
| 6232 | // We cannot directly call the CheckCast runtime entry point |
| 6233 | // without resorting to a type checking slow path here (i.e. by |
| 6234 | // calling InvokeRuntime directly), as it would require to |
| 6235 | // assign fixed registers for the inputs of this HInstanceOf |
| 6236 | // instruction (following the runtime calling convention), which |
| 6237 | // might be cluttered by the potential first read barrier |
| 6238 | // emission at the beginning of this method. |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6239 | __ jmp(type_check_slow_path->GetEntryLabel()); |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6240 | break; |
| 6241 | } |
| 6242 | |
| 6243 | case TypeCheckKind::kInterfaceCheck: |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6244 | // Fast path for the interface check. We always go slow path for heap poisoning since |
| 6245 | // unpoisoning cls would require an extra temp. |
| 6246 | if (!kPoisonHeapReferences) { |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6247 | // Try to avoid read barriers to improve the fast path. We can not get false positives by |
| 6248 | // doing this. |
| 6249 | // /* HeapReference<Class> */ temp = obj->klass_ |
| 6250 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6251 | temp_loc, |
| 6252 | obj_loc, |
| 6253 | class_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6254 | kWithoutReadBarrier); |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6255 | |
| 6256 | // /* HeapReference<Class> */ temp = temp->iftable_ |
| 6257 | GenerateReferenceLoadTwoRegisters(instruction, |
| 6258 | temp_loc, |
| 6259 | temp_loc, |
| 6260 | iftable_offset, |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6261 | kWithoutReadBarrier); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6262 | // Iftable is never null. |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6263 | __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset)); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6264 | // Loop through the iftable and check if any class matches. |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6265 | NearLabel start_loop; |
| 6266 | __ Bind(&start_loop); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6267 | // Need to subtract first to handle the empty array case. |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 6268 | __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2)); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6269 | __ j(kNegative, type_check_slow_path->GetEntryLabel()); |
| 6270 | // Go to next interface if the classes do not match. |
| 6271 | __ cmpl(cls.AsRegister<CpuRegister>(), |
| 6272 | CodeGeneratorX86_64::ArrayAddress(temp, |
| 6273 | maybe_temp2_loc, |
| 6274 | TIMES_4, |
| 6275 | object_array_data_offset)); |
| 6276 | __ j(kNotEqual, &start_loop); // Return if same class. |
| 6277 | } else { |
| 6278 | __ jmp(type_check_slow_path->GetEntryLabel()); |
Mathieu Chartier | cdba73b | 2016-11-03 19:23:06 -0700 | [diff] [blame] | 6279 | } |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6280 | break; |
| 6281 | } |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 6282 | |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6283 | if (done.IsLinked()) { |
| 6284 | __ Bind(&done); |
| 6285 | } |
| 6286 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6287 | __ Bind(type_check_slow_path->GetExitLabel()); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6288 | } |
| 6289 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6290 | void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) { |
| 6291 | LocationSummary* locations = |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 6292 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly); |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6293 | InvokeRuntimeCallingConvention calling_convention; |
| 6294 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 6295 | } |
| 6296 | |
| 6297 | void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) { |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 6298 | codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject, |
Alexandre Rames | 8158f28 | 2015-08-07 10:26:17 +0100 | [diff] [blame] | 6299 | instruction, |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 6300 | instruction->GetDexPc()); |
Roland Levillain | 888d067 | 2015-11-23 18:53:50 +0000 | [diff] [blame] | 6301 | if (instruction->IsEnter()) { |
| 6302 | CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>(); |
| 6303 | } else { |
| 6304 | CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>(); |
| 6305 | } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6308 | void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); } |
| 6309 | void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); } |
| 6310 | void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); } |
| 6311 | |
| 6312 | void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) { |
| 6313 | LocationSummary* locations = |
| 6314 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 6315 | DCHECK(instruction->GetResultType() == Primitive::kPrimInt |
| 6316 | || instruction->GetResultType() == Primitive::kPrimLong); |
| 6317 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 6318 | locations->SetInAt(1, Location::Any()); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6319 | locations->SetOut(Location::SameAsFirstInput()); |
| 6320 | } |
| 6321 | |
| 6322 | void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) { |
| 6323 | HandleBitwiseOperation(instruction); |
| 6324 | } |
| 6325 | |
| 6326 | void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) { |
| 6327 | HandleBitwiseOperation(instruction); |
| 6328 | } |
| 6329 | |
| 6330 | void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) { |
| 6331 | HandleBitwiseOperation(instruction); |
| 6332 | } |
| 6333 | |
| 6334 | void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) { |
| 6335 | LocationSummary* locations = instruction->GetLocations(); |
| 6336 | Location first = locations->InAt(0); |
| 6337 | Location second = locations->InAt(1); |
| 6338 | DCHECK(first.Equals(locations->Out())); |
| 6339 | |
| 6340 | if (instruction->GetResultType() == Primitive::kPrimInt) { |
| 6341 | if (second.IsRegister()) { |
| 6342 | if (instruction->IsAnd()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6343 | __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6344 | } else if (instruction->IsOr()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6345 | __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6346 | } else { |
| 6347 | DCHECK(instruction->IsXor()); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6348 | __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>()); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6349 | } |
| 6350 | } else if (second.IsConstant()) { |
| 6351 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue()); |
| 6352 | if (instruction->IsAnd()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6353 | __ andl(first.AsRegister<CpuRegister>(), imm); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6354 | } else if (instruction->IsOr()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6355 | __ orl(first.AsRegister<CpuRegister>(), imm); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6356 | } else { |
| 6357 | DCHECK(instruction->IsXor()); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6358 | __ xorl(first.AsRegister<CpuRegister>(), imm); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6359 | } |
| 6360 | } else { |
| 6361 | Address address(CpuRegister(RSP), second.GetStackIndex()); |
| 6362 | if (instruction->IsAnd()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6363 | __ andl(first.AsRegister<CpuRegister>(), address); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6364 | } else if (instruction->IsOr()) { |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6365 | __ orl(first.AsRegister<CpuRegister>(), address); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6366 | } else { |
| 6367 | DCHECK(instruction->IsXor()); |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 6368 | __ xorl(first.AsRegister<CpuRegister>(), address); |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6369 | } |
| 6370 | } |
| 6371 | } else { |
| 6372 | DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6373 | CpuRegister first_reg = first.AsRegister<CpuRegister>(); |
| 6374 | bool second_is_constant = false; |
| 6375 | int64_t value = 0; |
| 6376 | if (second.IsConstant()) { |
| 6377 | second_is_constant = true; |
| 6378 | value = second.GetConstant()->AsLongConstant()->GetValue(); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6379 | } |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 6380 | bool is_int32_value = IsInt<32>(value); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6381 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6382 | if (instruction->IsAnd()) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6383 | if (second_is_constant) { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 6384 | if (is_int32_value) { |
| 6385 | __ andq(first_reg, Immediate(static_cast<int32_t>(value))); |
| 6386 | } else { |
| 6387 | __ andq(first_reg, codegen_->LiteralInt64Address(value)); |
| 6388 | } |
| 6389 | } else if (second.IsDoubleStackSlot()) { |
| 6390 | __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex())); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6391 | } else { |
| 6392 | __ andq(first_reg, second.AsRegister<CpuRegister>()); |
| 6393 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6394 | } else if (instruction->IsOr()) { |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6395 | if (second_is_constant) { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 6396 | if (is_int32_value) { |
| 6397 | __ orq(first_reg, Immediate(static_cast<int32_t>(value))); |
| 6398 | } else { |
| 6399 | __ orq(first_reg, codegen_->LiteralInt64Address(value)); |
| 6400 | } |
| 6401 | } else if (second.IsDoubleStackSlot()) { |
| 6402 | __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex())); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6403 | } else { |
| 6404 | __ orq(first_reg, second.AsRegister<CpuRegister>()); |
| 6405 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6406 | } else { |
| 6407 | DCHECK(instruction->IsXor()); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6408 | if (second_is_constant) { |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 6409 | if (is_int32_value) { |
| 6410 | __ xorq(first_reg, Immediate(static_cast<int32_t>(value))); |
| 6411 | } else { |
| 6412 | __ xorq(first_reg, codegen_->LiteralInt64Address(value)); |
| 6413 | } |
| 6414 | } else if (second.IsDoubleStackSlot()) { |
| 6415 | __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex())); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 6416 | } else { |
| 6417 | __ xorq(first_reg, second.AsRegister<CpuRegister>()); |
| 6418 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 6419 | } |
| 6420 | } |
| 6421 | } |
| 6422 | |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6423 | void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister( |
| 6424 | HInstruction* instruction, |
| 6425 | Location out, |
| 6426 | uint32_t offset, |
| 6427 | Location maybe_temp, |
| 6428 | ReadBarrierOption read_barrier_option) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6429 | CpuRegister out_reg = out.AsRegister<CpuRegister>(); |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6430 | if (read_barrier_option == kWithReadBarrier) { |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6431 | CHECK(kEmitCompilerReadBarrier); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6432 | if (kUseBakerReadBarrier) { |
| 6433 | // Load with fast path based Baker's read barrier. |
| 6434 | // /* HeapReference<Object> */ out = *(out + offset) |
| 6435 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6436 | instruction, out, out_reg, offset, /* needs_null_check */ false); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6437 | } else { |
| 6438 | // Load with slow path based read barrier. |
Roland Levillain | 95e7ffc | 2016-01-22 11:57:25 +0000 | [diff] [blame] | 6439 | // Save the value of `out` into `maybe_temp` before overwriting it |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6440 | // in the following move operation, as we will need it for the |
| 6441 | // read barrier below. |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6442 | DCHECK(maybe_temp.IsRegister()) << maybe_temp; |
Roland Levillain | 95e7ffc | 2016-01-22 11:57:25 +0000 | [diff] [blame] | 6443 | __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6444 | // /* HeapReference<Object> */ out = *(out + offset) |
| 6445 | __ movl(out_reg, Address(out_reg, offset)); |
Roland Levillain | 95e7ffc | 2016-01-22 11:57:25 +0000 | [diff] [blame] | 6446 | codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6447 | } |
| 6448 | } else { |
| 6449 | // Plain load with no read barrier. |
| 6450 | // /* HeapReference<Object> */ out = *(out + offset) |
| 6451 | __ movl(out_reg, Address(out_reg, offset)); |
| 6452 | __ MaybeUnpoisonHeapReference(out_reg); |
| 6453 | } |
| 6454 | } |
| 6455 | |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6456 | void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters( |
| 6457 | HInstruction* instruction, |
| 6458 | Location out, |
| 6459 | Location obj, |
| 6460 | uint32_t offset, |
| 6461 | ReadBarrierOption read_barrier_option) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6462 | CpuRegister out_reg = out.AsRegister<CpuRegister>(); |
| 6463 | CpuRegister obj_reg = obj.AsRegister<CpuRegister>(); |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6464 | if (read_barrier_option == kWithReadBarrier) { |
Mathieu Chartier | aa474eb | 2016-11-09 15:18:27 -0800 | [diff] [blame] | 6465 | CHECK(kEmitCompilerReadBarrier); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6466 | if (kUseBakerReadBarrier) { |
| 6467 | // Load with fast path based Baker's read barrier. |
| 6468 | // /* HeapReference<Object> */ out = *(obj + offset) |
| 6469 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6470 | instruction, out, obj_reg, offset, /* needs_null_check */ false); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6471 | } else { |
| 6472 | // Load with slow path based read barrier. |
| 6473 | // /* HeapReference<Object> */ out = *(obj + offset) |
| 6474 | __ movl(out_reg, Address(obj_reg, offset)); |
| 6475 | codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset); |
| 6476 | } |
| 6477 | } else { |
| 6478 | // Plain load with no read barrier. |
| 6479 | // /* HeapReference<Object> */ out = *(obj + offset) |
| 6480 | __ movl(out_reg, Address(obj_reg, offset)); |
| 6481 | __ MaybeUnpoisonHeapReference(out_reg); |
| 6482 | } |
| 6483 | } |
| 6484 | |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6485 | void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad( |
| 6486 | HInstruction* instruction, |
| 6487 | Location root, |
| 6488 | const Address& address, |
| 6489 | Label* fixup_label, |
| 6490 | ReadBarrierOption read_barrier_option) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6491 | CpuRegister root_reg = root.AsRegister<CpuRegister>(); |
Mathieu Chartier | 3af00dc | 2016-11-10 11:25:57 -0800 | [diff] [blame] | 6492 | if (read_barrier_option == kWithReadBarrier) { |
Mathieu Chartier | 31b12e3 | 2016-09-02 17:11:57 -0700 | [diff] [blame] | 6493 | DCHECK(kEmitCompilerReadBarrier); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6494 | if (kUseBakerReadBarrier) { |
| 6495 | // Fast path implementation of art::ReadBarrier::BarrierForRoot when |
| 6496 | // Baker's read barrier are used: |
| 6497 | // |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 6498 | // root = *address; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6499 | // if (Thread::Current()->GetIsGcMarking()) { |
| 6500 | // root = ReadBarrier::Mark(root) |
| 6501 | // } |
| 6502 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 6503 | // /* GcRoot<mirror::Object> */ root = *address |
| 6504 | __ movl(root_reg, address); |
| 6505 | if (fixup_label != nullptr) { |
| 6506 | __ Bind(fixup_label); |
| 6507 | } |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6508 | static_assert( |
| 6509 | sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>), |
| 6510 | "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> " |
| 6511 | "have different sizes."); |
| 6512 | static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t), |
| 6513 | "art::mirror::CompressedReference<mirror::Object> and int32_t " |
| 6514 | "have different sizes."); |
| 6515 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6516 | // Slow path marking the GC root `root`. |
| 6517 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64( |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 6518 | instruction, root, /* unpoison_ref_before_marking */ false); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6519 | codegen_->AddSlowPath(slow_path); |
| 6520 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6521 | __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(), |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6522 | /* no_rip */ true), |
| 6523 | Immediate(0)); |
| 6524 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 6525 | __ Bind(slow_path->GetExitLabel()); |
| 6526 | } else { |
| 6527 | // GC root loaded through a slow path for read barriers other |
| 6528 | // than Baker's. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 6529 | // /* GcRoot<mirror::Object>* */ root = address |
| 6530 | __ leaq(root_reg, address); |
| 6531 | if (fixup_label != nullptr) { |
| 6532 | __ Bind(fixup_label); |
| 6533 | } |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6534 | // /* mirror::Object* */ root = root->Read() |
| 6535 | codegen_->GenerateReadBarrierForRootSlow(instruction, root, root); |
| 6536 | } |
| 6537 | } else { |
| 6538 | // Plain GC root load with no read barrier. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 6539 | // /* GcRoot<mirror::Object> */ root = *address |
| 6540 | __ movl(root_reg, address); |
| 6541 | if (fixup_label != nullptr) { |
| 6542 | __ Bind(fixup_label); |
| 6543 | } |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 6544 | // Note that GC roots are not affected by heap poisoning, thus we |
| 6545 | // do not have to unpoison `root_reg` here. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction, |
| 6550 | Location ref, |
| 6551 | CpuRegister obj, |
| 6552 | uint32_t offset, |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6553 | bool needs_null_check) { |
| 6554 | DCHECK(kEmitCompilerReadBarrier); |
| 6555 | DCHECK(kUseBakerReadBarrier); |
| 6556 | |
| 6557 | // /* HeapReference<Object> */ ref = *(obj + offset) |
| 6558 | Address src(obj, offset); |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6559 | GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction, |
| 6563 | Location ref, |
| 6564 | CpuRegister obj, |
| 6565 | uint32_t data_offset, |
| 6566 | Location index, |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6567 | bool needs_null_check) { |
| 6568 | DCHECK(kEmitCompilerReadBarrier); |
| 6569 | DCHECK(kUseBakerReadBarrier); |
| 6570 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 6571 | static_assert( |
| 6572 | sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t), |
| 6573 | "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes."); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6574 | // /* HeapReference<Object> */ ref = |
| 6575 | // *(obj + data_offset + index * sizeof(HeapReference<Object>)) |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6576 | Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset); |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6577 | GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
| 6580 | void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction, |
| 6581 | Location ref, |
| 6582 | CpuRegister obj, |
| 6583 | const Address& src, |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 6584 | bool needs_null_check, |
| 6585 | bool always_update_field, |
| 6586 | CpuRegister* temp1, |
| 6587 | CpuRegister* temp2) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6588 | DCHECK(kEmitCompilerReadBarrier); |
| 6589 | DCHECK(kUseBakerReadBarrier); |
| 6590 | |
| 6591 | // In slow path based read barriers, the read barrier call is |
| 6592 | // inserted after the original load. However, in fast path based |
| 6593 | // Baker's read barriers, we need to perform the load of |
| 6594 | // mirror::Object::monitor_ *before* the original reference load. |
| 6595 | // This load-load ordering is required by the read barrier. |
| 6596 | // The fast path/slow path (for Baker's algorithm) should look like: |
| 6597 | // |
| 6598 | // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState(); |
| 6599 | // lfence; // Load fence or artificial data dependency to prevent load-load reordering |
| 6600 | // HeapReference<Object> ref = *src; // Original reference load. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 6601 | // bool is_gray = (rb_state == ReadBarrier::GrayState()); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6602 | // if (is_gray) { |
| 6603 | // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path. |
| 6604 | // } |
| 6605 | // |
| 6606 | // Note: the original implementation in ReadBarrier::Barrier is |
| 6607 | // slightly more complex as: |
| 6608 | // - it implements the load-load fence using a data dependency on |
Roland Levillain | e3f43ac | 2016-01-19 15:07:47 +0000 | [diff] [blame] | 6609 | // the high-bits of rb_state, which are expected to be all zeroes |
| 6610 | // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead |
| 6611 | // here, which is a no-op thanks to the x86-64 memory model); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6612 | // - it performs additional checks that we do not do here for |
| 6613 | // performance reasons. |
| 6614 | |
| 6615 | CpuRegister ref_reg = ref.AsRegister<CpuRegister>(); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6616 | uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value(); |
| 6617 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6618 | // Given the numeric representation, it's enough to check the low bit of the rb_state. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 6619 | static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0"); |
| 6620 | static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1"); |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6621 | constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte; |
| 6622 | constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte; |
| 6623 | constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position); |
| 6624 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 6625 | // if (rb_state == ReadBarrier::GrayState()) |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6626 | // ref = ReadBarrier::Mark(ref); |
| 6627 | // At this point, just do the "if" and make sure that flags are preserved until the branch. |
| 6628 | __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value)); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6629 | if (needs_null_check) { |
| 6630 | MaybeRecordImplicitNullCheck(instruction); |
| 6631 | } |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6632 | |
| 6633 | // Load fence to prevent load-load reordering. |
| 6634 | // Note that this is a no-op, thanks to the x86-64 memory model. |
| 6635 | GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 6636 | |
| 6637 | // The actual reference load. |
| 6638 | // /* HeapReference<Object> */ ref = *src |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6639 | __ movl(ref_reg, src); // Flags are unaffected. |
| 6640 | |
| 6641 | // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch. |
| 6642 | // Slow path marking the object `ref` when it is gray. |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 6643 | SlowPathCode* slow_path; |
| 6644 | if (always_update_field) { |
| 6645 | DCHECK(temp1 != nullptr); |
| 6646 | DCHECK(temp2 != nullptr); |
| 6647 | slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64( |
| 6648 | instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2); |
| 6649 | } else { |
| 6650 | slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64( |
| 6651 | instruction, ref, /* unpoison_ref_before_marking */ true); |
| 6652 | } |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 6653 | AddSlowPath(slow_path); |
| 6654 | |
| 6655 | // We have done the "if" of the gray bit check above, now branch based on the flags. |
| 6656 | __ j(kNotZero, slow_path->GetEntryLabel()); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6657 | |
| 6658 | // Object* ref = ref_addr->AsMirrorPtr() |
| 6659 | __ MaybeUnpoisonHeapReference(ref_reg); |
| 6660 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6661 | __ Bind(slow_path->GetExitLabel()); |
| 6662 | } |
| 6663 | |
| 6664 | void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction, |
| 6665 | Location out, |
| 6666 | Location ref, |
| 6667 | Location obj, |
| 6668 | uint32_t offset, |
| 6669 | Location index) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6670 | DCHECK(kEmitCompilerReadBarrier); |
| 6671 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6672 | // Insert a slow path based read barrier *after* the reference load. |
| 6673 | // |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6674 | // If heap poisoning is enabled, the unpoisoning of the loaded |
| 6675 | // reference will be carried out by the runtime within the slow |
| 6676 | // path. |
| 6677 | // |
| 6678 | // Note that `ref` currently does not get unpoisoned (when heap |
| 6679 | // poisoning is enabled), which is alright as the `ref` argument is |
| 6680 | // not used by the artReadBarrierSlow entry point. |
| 6681 | // |
| 6682 | // TODO: Unpoison `ref` when it is used by artReadBarrierSlow. |
| 6683 | SlowPathCode* slow_path = new (GetGraph()->GetArena()) |
| 6684 | ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index); |
| 6685 | AddSlowPath(slow_path); |
| 6686 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6687 | __ jmp(slow_path->GetEntryLabel()); |
| 6688 | __ Bind(slow_path->GetExitLabel()); |
| 6689 | } |
| 6690 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6691 | void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction, |
| 6692 | Location out, |
| 6693 | Location ref, |
| 6694 | Location obj, |
| 6695 | uint32_t offset, |
| 6696 | Location index) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6697 | if (kEmitCompilerReadBarrier) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6698 | // Baker's read barriers shall be handled by the fast path |
| 6699 | // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier). |
| 6700 | DCHECK(!kUseBakerReadBarrier); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6701 | // If heap poisoning is enabled, unpoisoning will be taken care of |
| 6702 | // by the runtime within the slow path. |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6703 | GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6704 | } else if (kPoisonHeapReferences) { |
| 6705 | __ UnpoisonHeapReference(out.AsRegister<CpuRegister>()); |
| 6706 | } |
| 6707 | } |
| 6708 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6709 | void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction, |
| 6710 | Location out, |
| 6711 | Location root) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6712 | DCHECK(kEmitCompilerReadBarrier); |
| 6713 | |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 6714 | // Insert a slow path based read barrier *after* the GC root load. |
| 6715 | // |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6716 | // Note that GC roots are not affected by heap poisoning, so we do |
| 6717 | // not need to do anything special for this here. |
| 6718 | SlowPathCode* slow_path = |
| 6719 | new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root); |
| 6720 | AddSlowPath(slow_path); |
| 6721 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 6722 | __ jmp(slow_path->GetEntryLabel()); |
| 6723 | __ Bind(slow_path->GetExitLabel()); |
| 6724 | } |
| 6725 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 6726 | void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 6727 | // Nothing to do, this should be removed during prepare for register allocator. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 6728 | LOG(FATAL) << "Unreachable"; |
| 6729 | } |
| 6730 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 6731 | void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 6732 | // Nothing to do, this should be removed during prepare for register allocator. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 6733 | LOG(FATAL) << "Unreachable"; |
| 6734 | } |
| 6735 | |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6736 | // Simple implementation of packed switch - generate cascaded compare/jumps. |
| 6737 | void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) { |
| 6738 | LocationSummary* locations = |
| 6739 | new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall); |
| 6740 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6741 | locations->AddTemp(Location::RequiresRegister()); |
| 6742 | locations->AddTemp(Location::RequiresRegister()); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6743 | } |
| 6744 | |
| 6745 | void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) { |
| 6746 | int32_t lower_bound = switch_instr->GetStartValue(); |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 6747 | uint32_t num_entries = switch_instr->GetNumEntries(); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6748 | LocationSummary* locations = switch_instr->GetLocations(); |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6749 | CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>(); |
| 6750 | CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 6751 | CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>(); |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 6752 | HBasicBlock* default_block = switch_instr->GetDefaultBlock(); |
| 6753 | |
| 6754 | // Should we generate smaller inline compare/jumps? |
| 6755 | if (num_entries <= kPackedSwitchJumpTableThreshold) { |
| 6756 | // Figure out the correct compare values and jump conditions. |
| 6757 | // Handle the first compare/branch as a special case because it might |
| 6758 | // jump to the default case. |
| 6759 | DCHECK_GT(num_entries, 2u); |
| 6760 | Condition first_condition; |
| 6761 | uint32_t index; |
| 6762 | const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors(); |
| 6763 | if (lower_bound != 0) { |
| 6764 | first_condition = kLess; |
| 6765 | __ cmpl(value_reg_in, Immediate(lower_bound)); |
| 6766 | __ j(first_condition, codegen_->GetLabelOf(default_block)); |
| 6767 | __ j(kEqual, codegen_->GetLabelOf(successors[0])); |
| 6768 | |
| 6769 | index = 1; |
| 6770 | } else { |
| 6771 | // Handle all the compare/jumps below. |
| 6772 | first_condition = kBelow; |
| 6773 | index = 0; |
| 6774 | } |
| 6775 | |
| 6776 | // Handle the rest of the compare/jumps. |
| 6777 | for (; index + 1 < num_entries; index += 2) { |
| 6778 | int32_t compare_to_value = lower_bound + index + 1; |
| 6779 | __ cmpl(value_reg_in, Immediate(compare_to_value)); |
| 6780 | // Jump to successors[index] if value < case_value[index]. |
| 6781 | __ j(first_condition, codegen_->GetLabelOf(successors[index])); |
| 6782 | // Jump to successors[index + 1] if value == case_value[index + 1]. |
| 6783 | __ j(kEqual, codegen_->GetLabelOf(successors[index + 1])); |
| 6784 | } |
| 6785 | |
| 6786 | if (index != num_entries) { |
| 6787 | // There are an odd number of entries. Handle the last one. |
| 6788 | DCHECK_EQ(index + 1, num_entries); |
Nicolas Geoffray | 6ce0173 | 2015-12-30 14:10:13 +0000 | [diff] [blame] | 6789 | __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index))); |
Vladimir Marko | f3e0ee2 | 2015-12-17 15:23:13 +0000 | [diff] [blame] | 6790 | __ j(kEqual, codegen_->GetLabelOf(successors[index])); |
| 6791 | } |
| 6792 | |
| 6793 | // And the default for any other value. |
| 6794 | if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) { |
| 6795 | __ jmp(codegen_->GetLabelOf(default_block)); |
| 6796 | } |
| 6797 | return; |
| 6798 | } |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6799 | |
| 6800 | // Remove the bias, if needed. |
| 6801 | Register value_reg_out = value_reg_in.AsRegister(); |
| 6802 | if (lower_bound != 0) { |
| 6803 | __ leal(temp_reg, Address(value_reg_in, -lower_bound)); |
| 6804 | value_reg_out = temp_reg.AsRegister(); |
| 6805 | } |
| 6806 | CpuRegister value_reg(value_reg_out); |
| 6807 | |
| 6808 | // Is the value in range? |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6809 | __ cmpl(value_reg, Immediate(num_entries - 1)); |
| 6810 | __ j(kAbove, codegen_->GetLabelOf(default_block)); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6811 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6812 | // We are in the range of the table. |
| 6813 | // Load the address of the jump table in the constant area. |
| 6814 | __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr)); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6815 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6816 | // Load the (signed) offset from the jump table. |
| 6817 | __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0)); |
| 6818 | |
| 6819 | // Add the offset to the address of the table base. |
| 6820 | __ addq(temp_reg, base_reg); |
| 6821 | |
| 6822 | // And jump. |
| 6823 | __ jmp(temp_reg); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 6824 | } |
| 6825 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 6826 | void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) { |
| 6827 | if (value == 0) { |
| 6828 | __ xorl(dest, dest); |
| 6829 | } else { |
| 6830 | __ movl(dest, Immediate(value)); |
| 6831 | } |
| 6832 | } |
| 6833 | |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 6834 | void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) { |
| 6835 | if (value == 0) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 6836 | // Clears upper bits too. |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 6837 | __ xorl(dest, dest); |
Vladimir Marko | ed00978 | 2016-02-22 16:54:39 +0000 | [diff] [blame] | 6838 | } else if (IsUint<32>(value)) { |
| 6839 | // We can use a 32 bit move, as it will zero-extend and is shorter. |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 6840 | __ movl(dest, Immediate(static_cast<int32_t>(value))); |
| 6841 | } else { |
| 6842 | __ movq(dest, Immediate(value)); |
| 6843 | } |
| 6844 | } |
| 6845 | |
Mark Mendell | 7c0b44f | 2016-02-01 10:08:35 -0500 | [diff] [blame] | 6846 | void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) { |
| 6847 | if (value == 0) { |
| 6848 | __ xorps(dest, dest); |
| 6849 | } else { |
| 6850 | __ movss(dest, LiteralInt32Address(value)); |
| 6851 | } |
| 6852 | } |
| 6853 | |
| 6854 | void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) { |
| 6855 | if (value == 0) { |
| 6856 | __ xorpd(dest, dest); |
| 6857 | } else { |
| 6858 | __ movsd(dest, LiteralInt64Address(value)); |
| 6859 | } |
| 6860 | } |
| 6861 | |
| 6862 | void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) { |
| 6863 | Load32BitValue(dest, bit_cast<int32_t, float>(value)); |
| 6864 | } |
| 6865 | |
| 6866 | void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) { |
| 6867 | Load64BitValue(dest, bit_cast<int64_t, double>(value)); |
| 6868 | } |
| 6869 | |
Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 6870 | void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) { |
| 6871 | if (value == 0) { |
| 6872 | __ testl(dest, dest); |
| 6873 | } else { |
| 6874 | __ cmpl(dest, Immediate(value)); |
| 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) { |
| 6879 | if (IsInt<32>(value)) { |
| 6880 | if (value == 0) { |
| 6881 | __ testq(dest, dest); |
| 6882 | } else { |
| 6883 | __ cmpq(dest, Immediate(static_cast<int32_t>(value))); |
| 6884 | } |
| 6885 | } else { |
| 6886 | // Value won't fit in an int. |
| 6887 | __ cmpq(dest, LiteralInt64Address(value)); |
| 6888 | } |
| 6889 | } |
| 6890 | |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6891 | void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) { |
| 6892 | CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>(); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 6893 | GenerateIntCompare(lhs_reg, rhs); |
| 6894 | } |
| 6895 | |
| 6896 | void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) { |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6897 | if (rhs.IsConstant()) { |
| 6898 | int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 6899 | Compare32BitValue(lhs, value); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6900 | } else if (rhs.IsStackSlot()) { |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 6901 | __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex())); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6902 | } else { |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 6903 | __ cmpl(lhs, rhs.AsRegister<CpuRegister>()); |
Vladimir Marko | 56f4bdd | 2016-09-16 11:32:36 +0100 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | |
| 6907 | void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) { |
| 6908 | CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>(); |
| 6909 | if (rhs.IsConstant()) { |
| 6910 | int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue(); |
| 6911 | Compare64BitValue(lhs_reg, value); |
| 6912 | } else if (rhs.IsDoubleStackSlot()) { |
| 6913 | __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex())); |
| 6914 | } else { |
| 6915 | __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>()); |
| 6916 | } |
| 6917 | } |
| 6918 | |
| 6919 | Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj, |
| 6920 | Location index, |
| 6921 | ScaleFactor scale, |
| 6922 | uint32_t data_offset) { |
| 6923 | return index.IsConstant() ? |
| 6924 | Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) : |
| 6925 | Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset); |
| 6926 | } |
| 6927 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 6928 | void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) { |
| 6929 | DCHECK(dest.IsDoubleStackSlot()); |
| 6930 | if (IsInt<32>(value)) { |
| 6931 | // Can move directly as an int32 constant. |
| 6932 | __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), |
| 6933 | Immediate(static_cast<int32_t>(value))); |
| 6934 | } else { |
| 6935 | Load64BitValue(CpuRegister(TMP), value); |
| 6936 | __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP)); |
| 6937 | } |
| 6938 | } |
| 6939 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 6940 | /** |
| 6941 | * Class to handle late fixup of offsets into constant area. |
| 6942 | */ |
| 6943 | class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> { |
| 6944 | public: |
| 6945 | RIPFixup(CodeGeneratorX86_64& codegen, size_t offset) |
| 6946 | : codegen_(&codegen), offset_into_constant_area_(offset) {} |
| 6947 | |
| 6948 | protected: |
| 6949 | void SetOffset(size_t offset) { offset_into_constant_area_ = offset; } |
| 6950 | |
| 6951 | CodeGeneratorX86_64* codegen_; |
| 6952 | |
| 6953 | private: |
| 6954 | void Process(const MemoryRegion& region, int pos) OVERRIDE { |
| 6955 | // Patch the correct offset for the instruction. We use the address of the |
| 6956 | // 'next' instruction, which is 'pos' (patch the 4 bytes before). |
| 6957 | int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_; |
| 6958 | int32_t relative_position = constant_offset - pos; |
| 6959 | |
| 6960 | // Patch in the right value. |
| 6961 | region.StoreUnaligned<int32_t>(pos - 4, relative_position); |
| 6962 | } |
| 6963 | |
| 6964 | // Location in constant area that the fixup refers to. |
| 6965 | size_t offset_into_constant_area_; |
| 6966 | }; |
| 6967 | |
| 6968 | /** |
| 6969 | t * Class to handle late fixup of offsets to a jump table that will be created in the |
| 6970 | * constant area. |
| 6971 | */ |
| 6972 | class JumpTableRIPFixup : public RIPFixup { |
| 6973 | public: |
| 6974 | JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr) |
| 6975 | : RIPFixup(codegen, -1), switch_instr_(switch_instr) {} |
| 6976 | |
| 6977 | void CreateJumpTable() { |
| 6978 | X86_64Assembler* assembler = codegen_->GetAssembler(); |
| 6979 | |
| 6980 | // Ensure that the reference to the jump table has the correct offset. |
| 6981 | const int32_t offset_in_constant_table = assembler->ConstantAreaSize(); |
| 6982 | SetOffset(offset_in_constant_table); |
| 6983 | |
| 6984 | // Compute the offset from the start of the function to this jump table. |
| 6985 | const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table; |
| 6986 | |
| 6987 | // Populate the jump table with the correct values for the jump table. |
| 6988 | int32_t num_entries = switch_instr_->GetNumEntries(); |
| 6989 | HBasicBlock* block = switch_instr_->GetBlock(); |
| 6990 | const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors(); |
| 6991 | // The value that we want is the target offset - the position of the table. |
| 6992 | for (int32_t i = 0; i < num_entries; i++) { |
| 6993 | HBasicBlock* b = successors[i]; |
| 6994 | Label* l = codegen_->GetLabelOf(b); |
| 6995 | DCHECK(l->IsBound()); |
| 6996 | int32_t offset_to_block = l->Position() - current_table_offset; |
| 6997 | assembler->AppendInt32(offset_to_block); |
| 6998 | } |
| 6999 | } |
| 7000 | |
| 7001 | private: |
| 7002 | const HPackedSwitch* switch_instr_; |
| 7003 | }; |
| 7004 | |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 7005 | void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) { |
| 7006 | // Generate the constant area if needed. |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 7007 | X86_64Assembler* assembler = GetAssembler(); |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 7008 | if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) { |
| 7009 | // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8 byte values. |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 7010 | assembler->Align(4, 0); |
| 7011 | constant_area_start_ = assembler->CodeSize(); |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 7012 | |
| 7013 | // Populate any jump tables. |
| 7014 | for (auto jump_table : fixups_to_jump_tables_) { |
| 7015 | jump_table->CreateJumpTable(); |
| 7016 | } |
| 7017 | |
| 7018 | // And now add the constant area to the generated code. |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 7019 | assembler->AddConstantArea(); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | // And finish up. |
| 7023 | CodeGenerator::Finalize(allocator); |
| 7024 | } |
| 7025 | |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 7026 | Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) { |
| 7027 | AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v)); |
| 7028 | return Address::RIP(fixup); |
| 7029 | } |
| 7030 | |
| 7031 | Address CodeGeneratorX86_64::LiteralFloatAddress(float v) { |
| 7032 | AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v)); |
| 7033 | return Address::RIP(fixup); |
| 7034 | } |
| 7035 | |
| 7036 | Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) { |
| 7037 | AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v)); |
| 7038 | return Address::RIP(fixup); |
| 7039 | } |
| 7040 | |
| 7041 | Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) { |
| 7042 | AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v)); |
| 7043 | return Address::RIP(fixup); |
| 7044 | } |
| 7045 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 7046 | // TODO: trg as memory. |
| 7047 | void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) { |
| 7048 | if (!trg.IsValid()) { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 7049 | DCHECK_EQ(type, Primitive::kPrimVoid); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 7050 | return; |
| 7051 | } |
| 7052 | |
| 7053 | DCHECK_NE(type, Primitive::kPrimVoid); |
| 7054 | |
| 7055 | Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type); |
| 7056 | if (trg.Equals(return_loc)) { |
| 7057 | return; |
| 7058 | } |
| 7059 | |
| 7060 | // Let the parallel move resolver take care of all of this. |
| 7061 | HParallelMove parallel_move(GetGraph()->GetArena()); |
| 7062 | parallel_move.AddMove(return_loc, trg, type, nullptr); |
| 7063 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 7064 | } |
| 7065 | |
Mark Mendell | 9c86b48 | 2015-09-18 13:36:07 -0400 | [diff] [blame] | 7066 | Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) { |
| 7067 | // Create a fixup to be used to create and address the jump table. |
| 7068 | JumpTableRIPFixup* table_fixup = |
| 7069 | new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr); |
| 7070 | |
| 7071 | // We have to populate the jump tables. |
| 7072 | fixups_to_jump_tables_.push_back(table_fixup); |
| 7073 | return Address::RIP(table_fixup); |
| 7074 | } |
| 7075 | |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 7076 | void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low, |
| 7077 | const Address& addr_high, |
| 7078 | int64_t v, |
| 7079 | HInstruction* instruction) { |
| 7080 | if (IsInt<32>(v)) { |
| 7081 | int32_t v_32 = v; |
| 7082 | __ movq(addr_low, Immediate(v_32)); |
| 7083 | MaybeRecordImplicitNullCheck(instruction); |
| 7084 | } else { |
| 7085 | // Didn't fit in a register. Do it in pieces. |
| 7086 | int32_t low_v = Low32Bits(v); |
| 7087 | int32_t high_v = High32Bits(v); |
| 7088 | __ movl(addr_low, Immediate(low_v)); |
| 7089 | MaybeRecordImplicitNullCheck(instruction); |
| 7090 | __ movl(addr_high, Immediate(high_v)); |
| 7091 | } |
| 7092 | } |
| 7093 | |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 7094 | #undef __ |
| 7095 | |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 7096 | } // namespace x86_64 |
| 7097 | } // namespace art |