Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "intrinsics_x86_64.h" |
| 18 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 19 | #include <limits> |
| 20 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 21 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 22 | #include "art_method.h" |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 23 | #include "base/bit_utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 24 | #include "code_generator_x86_64.h" |
| 25 | #include "entrypoints/quick/quick_entrypoints.h" |
Andreas Gampe | 09659c2 | 2017-09-18 18:23:32 -0700 | [diff] [blame] | 26 | #include "heap_poisoning.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 27 | #include "intrinsics.h" |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 28 | #include "intrinsics_utils.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 29 | #include "lock_word.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 30 | #include "mirror/array-inl.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 31 | #include "mirror/object_array-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 32 | #include "mirror/reference.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 33 | #include "mirror/string.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 34 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 35 | #include "thread-current-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 36 | #include "utils/x86_64/assembler_x86_64.h" |
| 37 | #include "utils/x86_64/constants_x86_64.h" |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | namespace x86_64 { |
| 42 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 43 | IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen) |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 44 | : allocator_(codegen->GetGraph()->GetAllocator()), codegen_(codegen) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 47 | X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 48 | return down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 51 | ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 52 | return codegen_->GetGraph()->GetAllocator(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) { |
| 56 | Dispatch(invoke); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 57 | LocationSummary* res = invoke->GetLocations(); |
| 58 | if (res == nullptr) { |
| 59 | return false; |
| 60 | } |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 61 | return res->Intrinsified(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 64 | static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 65 | InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor; |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 66 | IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 69 | using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 70 | |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 71 | // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. |
| 72 | #define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT |
| 73 | |
| 74 | // Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers. |
| 75 | class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode { |
| 76 | public: |
| 77 | explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction) |
| 78 | : SlowPathCode(instruction) { |
| 79 | DCHECK(kEmitCompilerReadBarrier); |
| 80 | DCHECK(kUseBakerReadBarrier); |
| 81 | } |
| 82 | |
| 83 | void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 84 | CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen); |
| 85 | LocationSummary* locations = instruction_->GetLocations(); |
| 86 | DCHECK(locations->CanCall()); |
| 87 | DCHECK(instruction_->IsInvokeStaticOrDirect()) |
| 88 | << "Unexpected instruction in read barrier arraycopy slow path: " |
| 89 | << instruction_->DebugName(); |
| 90 | DCHECK(instruction_->GetLocations()->Intrinsified()); |
| 91 | DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy); |
| 92 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 93 | int32_t element_size = DataType::Size(DataType::Type::kReference); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 94 | |
| 95 | CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 96 | CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 97 | CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 98 | |
| 99 | __ Bind(GetEntryLabel()); |
| 100 | NearLabel loop; |
| 101 | __ Bind(&loop); |
| 102 | __ movl(CpuRegister(TMP), Address(src_curr_addr, 0)); |
| 103 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 104 | // TODO: Inline the mark bit check before calling the runtime? |
| 105 | // TMP = ReadBarrier::Mark(TMP); |
| 106 | // No need to save live registers; it's taken care of by the |
| 107 | // entrypoint. Also, there is no need to update the stack mask, |
| 108 | // as this runtime call will not trigger a garbage collection. |
Roland Levillain | 97c4646 | 2017-05-11 14:04:03 +0100 | [diff] [blame] | 109 | int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 110 | // This runtime call does not require a stack map. |
| 111 | x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this); |
| 112 | __ MaybePoisonHeapReference(CpuRegister(TMP)); |
| 113 | __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP)); |
| 114 | __ addl(src_curr_addr, Immediate(element_size)); |
| 115 | __ addl(dst_curr_addr, Immediate(element_size)); |
| 116 | __ cmpl(src_curr_addr, src_stop_addr); |
| 117 | __ j(kNotEqual, &loop); |
| 118 | __ jmp(GetExitLabel()); |
| 119 | } |
| 120 | |
| 121 | const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; } |
| 122 | |
| 123 | private: |
| 124 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64); |
| 125 | }; |
| 126 | |
| 127 | #undef __ |
| 128 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 129 | #define __ assembler-> |
| 130 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 131 | static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 132 | LocationSummary* locations = |
| 133 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 134 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 135 | locations->SetOut(Location::RequiresRegister()); |
| 136 | } |
| 137 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 138 | static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 139 | LocationSummary* locations = |
| 140 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 141 | locations->SetInAt(0, Location::RequiresRegister()); |
| 142 | locations->SetOut(Location::RequiresFpuRegister()); |
| 143 | } |
| 144 | |
| 145 | static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 146 | Location input = locations->InAt(0); |
| 147 | Location output = locations->Out(); |
| 148 | __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit); |
| 149 | } |
| 150 | |
| 151 | static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 152 | Location input = locations->InAt(0); |
| 153 | Location output = locations->Out(); |
| 154 | __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit); |
| 155 | } |
| 156 | |
| 157 | void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 158 | CreateFPToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 159 | } |
| 160 | void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 161 | CreateIntToFPLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 165 | MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 166 | } |
| 167 | void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 168 | MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 172 | CreateFPToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 173 | } |
| 174 | void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 175 | CreateIntToFPLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 179 | MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 180 | } |
| 181 | void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 182 | MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 186 | LocationSummary* locations = |
| 187 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 188 | locations->SetInAt(0, Location::RequiresRegister()); |
| 189 | locations->SetOut(Location::SameAsFirstInput()); |
| 190 | } |
| 191 | |
| 192 | static void GenReverseBytes(LocationSummary* locations, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 193 | DataType::Type size, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 194 | X86_64Assembler* assembler) { |
| 195 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 196 | |
| 197 | switch (size) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 198 | case DataType::Type::kInt16: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 199 | // TODO: Can be done with an xchg of 8b registers. This is straight from Quick. |
| 200 | __ bswapl(out); |
| 201 | __ sarl(out, Immediate(16)); |
| 202 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 203 | case DataType::Type::kInt32: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 204 | __ bswapl(out); |
| 205 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 206 | case DataType::Type::kInt64: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 207 | __ bswapq(out); |
| 208 | break; |
| 209 | default: |
| 210 | LOG(FATAL) << "Unexpected size for reverse-bytes: " << size; |
| 211 | UNREACHABLE(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 216 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 220 | GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 224 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 228 | GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 232 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 236 | GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | // TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we |
| 241 | // need is 64b. |
| 242 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 243 | static void CreateFloatToFloatPlusTemps(ArenaAllocator* allocator, HInvoke* invoke) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 244 | // TODO: Enable memory operations when the assembler supports them. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 245 | LocationSummary* locations = |
| 246 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 247 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 248 | locations->SetOut(Location::SameAsFirstInput()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 249 | locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 252 | static void MathAbsFP(LocationSummary* locations, |
| 253 | bool is64bit, |
| 254 | X86_64Assembler* assembler, |
| 255 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 256 | Location output = locations->Out(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 257 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 258 | DCHECK(output.IsFpuRegister()); |
| 259 | XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 260 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 261 | // TODO: Can mask directly with constant area using pand if we can guarantee |
| 262 | // that the literal is aligned on a 16 byte boundary. This will avoid a |
| 263 | // temporary. |
| 264 | if (is64bit) { |
| 265 | __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF))); |
| 266 | __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 267 | } else { |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 268 | __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF))); |
| 269 | __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 274 | CreateFloatToFloatPlusTemps(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 278 | MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 282 | CreateFloatToFloatPlusTemps(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 286 | MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 289 | static void CreateIntToIntPlusTemp(ArenaAllocator* allocator, HInvoke* invoke) { |
| 290 | LocationSummary* locations = |
| 291 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 292 | locations->SetInAt(0, Location::RequiresRegister()); |
| 293 | locations->SetOut(Location::SameAsFirstInput()); |
| 294 | locations->AddTemp(Location::RequiresRegister()); |
| 295 | } |
| 296 | |
| 297 | static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 298 | Location output = locations->Out(); |
| 299 | CpuRegister out = output.AsRegister<CpuRegister>(); |
| 300 | CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 301 | |
| 302 | if (is64bit) { |
| 303 | // Create mask. |
| 304 | __ movq(mask, out); |
| 305 | __ sarq(mask, Immediate(63)); |
| 306 | // Add mask. |
| 307 | __ addq(out, mask); |
| 308 | __ xorq(out, mask); |
| 309 | } else { |
| 310 | // Create mask. |
| 311 | __ movl(mask, out); |
| 312 | __ sarl(mask, Immediate(31)); |
| 313 | // Add mask. |
| 314 | __ addl(out, mask); |
| 315 | __ xorl(out, mask); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 320 | CreateIntToIntPlusTemp(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 324 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 328 | CreateIntToIntPlusTemp(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 332 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 335 | static void GenMinMaxFP(LocationSummary* locations, |
| 336 | bool is_min, |
| 337 | bool is_double, |
| 338 | X86_64Assembler* assembler, |
| 339 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 340 | Location op1_loc = locations->InAt(0); |
| 341 | Location op2_loc = locations->InAt(1); |
| 342 | Location out_loc = locations->Out(); |
| 343 | XmmRegister out = out_loc.AsFpuRegister<XmmRegister>(); |
| 344 | |
| 345 | // Shortcut for same input locations. |
| 346 | if (op1_loc.Equals(op2_loc)) { |
| 347 | DCHECK(out_loc.Equals(op1_loc)); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | // (out := op1) |
| 352 | // out <=? op2 |
| 353 | // if Nan jmp Nan_label |
| 354 | // if out is min jmp done |
| 355 | // if op2 is min jmp op2_label |
| 356 | // handle -0/+0 |
| 357 | // jmp done |
| 358 | // Nan_label: |
| 359 | // out := NaN |
| 360 | // op2_label: |
| 361 | // out := op2 |
| 362 | // done: |
| 363 | // |
| 364 | // This removes one jmp, but needs to copy one input (op1) to out. |
| 365 | // |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 366 | // TODO: This is straight from Quick. Make NaN an out-of-line slowpath? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 367 | |
| 368 | XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>(); |
| 369 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 370 | NearLabel nan, done, op2_label; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 371 | if (is_double) { |
| 372 | __ ucomisd(out, op2); |
| 373 | } else { |
| 374 | __ ucomiss(out, op2); |
| 375 | } |
| 376 | |
| 377 | __ j(Condition::kParityEven, &nan); |
| 378 | |
| 379 | __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label); |
| 380 | __ j(is_min ? Condition::kBelow : Condition::kAbove, &done); |
| 381 | |
| 382 | // Handle 0.0/-0.0. |
| 383 | if (is_min) { |
| 384 | if (is_double) { |
| 385 | __ orpd(out, op2); |
| 386 | } else { |
| 387 | __ orps(out, op2); |
| 388 | } |
| 389 | } else { |
| 390 | if (is_double) { |
| 391 | __ andpd(out, op2); |
| 392 | } else { |
| 393 | __ andps(out, op2); |
| 394 | } |
| 395 | } |
| 396 | __ jmp(&done); |
| 397 | |
| 398 | // NaN handling. |
| 399 | __ Bind(&nan); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 400 | if (is_double) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 401 | __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 402 | } else { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 403 | __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 404 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 405 | __ jmp(&done); |
| 406 | |
| 407 | // out := op2; |
| 408 | __ Bind(&op2_label); |
| 409 | if (is_double) { |
| 410 | __ movsd(out, op2); |
| 411 | } else { |
| 412 | __ movss(out, op2); |
| 413 | } |
| 414 | |
| 415 | // Done. |
| 416 | __ Bind(&done); |
| 417 | } |
| 418 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 419 | static void CreateFPFPToFP(ArenaAllocator* allocator, HInvoke* invoke) { |
| 420 | LocationSummary* locations = |
| 421 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 422 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 423 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 424 | // The following is sub-optimal, but all we can do for now. It would be fine to also accept |
| 425 | // the second input to be the output (we can simply swap inputs). |
| 426 | locations->SetOut(Location::SameAsFirstInput()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 430 | CreateFPFPToFP(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 434 | GenMinMaxFP( |
| 435 | invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 439 | CreateFPFPToFP(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 443 | GenMinMaxFP( |
| 444 | invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 448 | CreateFPFPToFP(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 452 | GenMinMaxFP( |
| 453 | invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 457 | CreateFPFPToFP(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 461 | GenMinMaxFP( |
| 462 | invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long, |
| 466 | X86_64Assembler* assembler) { |
| 467 | Location op1_loc = locations->InAt(0); |
| 468 | Location op2_loc = locations->InAt(1); |
| 469 | |
| 470 | // Shortcut for same input locations. |
| 471 | if (op1_loc.Equals(op2_loc)) { |
| 472 | // Can return immediately, as op1_loc == out_loc. |
| 473 | // Note: if we ever support separate registers, e.g., output into memory, we need to check for |
| 474 | // a copy here. |
| 475 | DCHECK(locations->Out().Equals(op1_loc)); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 480 | CpuRegister op2 = op2_loc.AsRegister<CpuRegister>(); |
| 481 | |
| 482 | // (out := op1) |
| 483 | // out <=? op2 |
| 484 | // if out is min jmp done |
| 485 | // out := op2 |
| 486 | // done: |
| 487 | |
| 488 | if (is_long) { |
| 489 | __ cmpq(out, op2); |
| 490 | } else { |
| 491 | __ cmpl(out, op2); |
| 492 | } |
| 493 | |
| 494 | __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long); |
| 495 | } |
| 496 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 497 | static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 498 | LocationSummary* locations = |
| 499 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 500 | locations->SetInAt(0, Location::RequiresRegister()); |
| 501 | locations->SetInAt(1, Location::RequiresRegister()); |
| 502 | locations->SetOut(Location::SameAsFirstInput()); |
| 503 | } |
| 504 | |
| 505 | void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 506 | CreateIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 510 | GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 514 | CreateIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 518 | GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 522 | CreateIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 526 | GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 530 | CreateIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 534 | GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 537 | static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 538 | LocationSummary* locations = |
| 539 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 540 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 541 | locations->SetOut(Location::RequiresFpuRegister()); |
| 542 | } |
| 543 | |
| 544 | void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 545 | CreateFPToFPLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) { |
| 549 | LocationSummary* locations = invoke->GetLocations(); |
| 550 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 551 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 552 | |
| 553 | GetAssembler()->sqrtsd(out, in); |
| 554 | } |
| 555 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 556 | static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) { |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 557 | MoveArguments(invoke, codegen); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 558 | |
| 559 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 560 | codegen->GenerateStaticOrDirectCall( |
| 561 | invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 562 | |
| 563 | // Copy the result back to the expected output. |
| 564 | Location out = invoke->GetLocations()->Out(); |
| 565 | if (out.IsValid()) { |
| 566 | DCHECK(out.IsRegister()); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 567 | codegen->MoveFromReturnRegister(out, invoke->GetType()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 571 | static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator, |
| 572 | HInvoke* invoke, |
| 573 | CodeGeneratorX86_64* codegen) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 574 | // Do we have instruction support? |
| 575 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 576 | CreateFPToFPLocations(allocator, invoke); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | |
| 580 | // We have to fall back to a call to the intrinsic. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 581 | LocationSummary* locations = |
| 582 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 583 | InvokeRuntimeCallingConvention calling_convention; |
| 584 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 585 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 586 | // Needs to be RDI for the invoke. |
| 587 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 588 | } |
| 589 | |
| 590 | static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen, |
| 591 | HInvoke* invoke, |
| 592 | X86_64Assembler* assembler, |
| 593 | int round_mode) { |
| 594 | LocationSummary* locations = invoke->GetLocations(); |
| 595 | if (locations->WillCall()) { |
| 596 | InvokeOutOfLineIntrinsic(codegen, invoke); |
| 597 | } else { |
| 598 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 599 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 600 | __ roundsd(out, in, Immediate(round_mode)); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 605 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) { |
| 609 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2); |
| 610 | } |
| 611 | |
| 612 | void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 613 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) { |
| 617 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1); |
| 618 | } |
| 619 | |
| 620 | void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 621 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) { |
| 625 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0); |
| 626 | } |
| 627 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 628 | static void CreateSSE41FPToIntLocations(ArenaAllocator* allocator, |
| 629 | HInvoke* invoke, |
| 630 | CodeGeneratorX86_64* codegen) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 631 | // Do we have instruction support? |
| 632 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 633 | LocationSummary* locations = |
| 634 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 635 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 636 | locations->SetOut(Location::RequiresRegister()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 637 | locations->AddTemp(Location::RequiresFpuRegister()); |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 638 | locations->AddTemp(Location::RequiresFpuRegister()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | // We have to fall back to a call to the intrinsic. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 643 | LocationSummary* locations = |
| 644 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 645 | InvokeRuntimeCallingConvention calling_convention; |
| 646 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 647 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 648 | // Needs to be RDI for the invoke. |
| 649 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 650 | } |
| 651 | |
| 652 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 653 | CreateSSE41FPToIntLocations(allocator_, invoke, codegen_); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 657 | LocationSummary* locations = invoke->GetLocations(); |
| 658 | if (locations->WillCall()) { |
| 659 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 660 | return; |
| 661 | } |
| 662 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 663 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 664 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 665 | XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 666 | XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>(); |
| 667 | NearLabel skip_incr, done; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 668 | X86_64Assembler* assembler = GetAssembler(); |
| 669 | |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 670 | // Since no direct x86 rounding instruction matches the required semantics, |
| 671 | // this intrinsic is implemented as follows: |
| 672 | // result = floor(in); |
| 673 | // if (in - result >= 0.5f) |
| 674 | // result = result + 1.0f; |
| 675 | __ movss(t2, in); |
| 676 | __ roundss(t1, in, Immediate(1)); |
| 677 | __ subss(t2, t1); |
| 678 | __ comiss(t2, codegen_->LiteralFloatAddress(0.5f)); |
| 679 | __ j(kBelow, &skip_incr); |
| 680 | __ addss(t1, codegen_->LiteralFloatAddress(1.0f)); |
| 681 | __ Bind(&skip_incr); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 682 | |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 683 | // Final conversion to an integer. Unfortunately this also does not have a |
| 684 | // direct x86 instruction, since NaN should map to 0 and large positive |
| 685 | // values need to be clipped to the extreme value. |
| 686 | codegen_->Load32BitValue(out, kPrimIntMax); |
| 687 | __ cvtsi2ss(t2, out); |
| 688 | __ comiss(t1, t2); |
| 689 | __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered |
| 690 | __ movl(out, Immediate(0)); // does not change flags |
| 691 | __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out) |
| 692 | __ cvttss2si(out, t1); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 693 | __ Bind(&done); |
| 694 | } |
| 695 | |
| 696 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 697 | CreateSSE41FPToIntLocations(allocator_, invoke, codegen_); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 701 | LocationSummary* locations = invoke->GetLocations(); |
| 702 | if (locations->WillCall()) { |
| 703 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 704 | return; |
| 705 | } |
| 706 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 707 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 708 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 709 | XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 710 | XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>(); |
| 711 | NearLabel skip_incr, done; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 712 | X86_64Assembler* assembler = GetAssembler(); |
| 713 | |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 714 | // Since no direct x86 rounding instruction matches the required semantics, |
| 715 | // this intrinsic is implemented as follows: |
| 716 | // result = floor(in); |
| 717 | // if (in - result >= 0.5) |
| 718 | // result = result + 1.0f; |
| 719 | __ movsd(t2, in); |
| 720 | __ roundsd(t1, in, Immediate(1)); |
| 721 | __ subsd(t2, t1); |
| 722 | __ comisd(t2, codegen_->LiteralDoubleAddress(0.5)); |
| 723 | __ j(kBelow, &skip_incr); |
| 724 | __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f)); |
| 725 | __ Bind(&skip_incr); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 726 | |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 727 | // Final conversion to an integer. Unfortunately this also does not have a |
| 728 | // direct x86 instruction, since NaN should map to 0 and large positive |
| 729 | // values need to be clipped to the extreme value. |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 730 | codegen_->Load64BitValue(out, kPrimLongMax); |
Aart Bik | 349f388 | 2016-08-02 15:40:56 -0700 | [diff] [blame] | 731 | __ cvtsi2sd(t2, out, /* is64bit */ true); |
| 732 | __ comisd(t1, t2); |
| 733 | __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered |
| 734 | __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit |
| 735 | __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out) |
| 736 | __ cvttsd2si(out, t1, /* is64bit */ true); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 737 | __ Bind(&done); |
| 738 | } |
| 739 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 740 | static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 741 | LocationSummary* locations = |
| 742 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 743 | InvokeRuntimeCallingConvention calling_convention; |
| 744 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 745 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 746 | |
| 747 | // We have to ensure that the native code doesn't clobber the XMM registers which are |
| 748 | // non-volatile for ART, but volatile for Native calls. This will ensure that they are |
| 749 | // saved in the prologue and properly restored. |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 750 | for (FloatRegister fp_reg : non_volatile_xmm_regs) { |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 751 | locations->AddTemp(Location::FpuRegisterLocation(fp_reg)); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen, |
| 756 | QuickEntrypointEnum entry) { |
| 757 | LocationSummary* locations = invoke->GetLocations(); |
| 758 | DCHECK(locations->WillCall()); |
| 759 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 760 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 761 | codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc()); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 765 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) { |
| 769 | GenFPToFPCall(invoke, codegen_, kQuickCos); |
| 770 | } |
| 771 | |
| 772 | void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 773 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) { |
| 777 | GenFPToFPCall(invoke, codegen_, kQuickSin); |
| 778 | } |
| 779 | |
| 780 | void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 781 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) { |
| 785 | GenFPToFPCall(invoke, codegen_, kQuickAcos); |
| 786 | } |
| 787 | |
| 788 | void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 789 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) { |
| 793 | GenFPToFPCall(invoke, codegen_, kQuickAsin); |
| 794 | } |
| 795 | |
| 796 | void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 797 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) { |
| 801 | GenFPToFPCall(invoke, codegen_, kQuickAtan); |
| 802 | } |
| 803 | |
| 804 | void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 805 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) { |
| 809 | GenFPToFPCall(invoke, codegen_, kQuickCbrt); |
| 810 | } |
| 811 | |
| 812 | void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 813 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) { |
| 817 | GenFPToFPCall(invoke, codegen_, kQuickCosh); |
| 818 | } |
| 819 | |
| 820 | void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 821 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) { |
| 825 | GenFPToFPCall(invoke, codegen_, kQuickExp); |
| 826 | } |
| 827 | |
| 828 | void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 829 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) { |
| 833 | GenFPToFPCall(invoke, codegen_, kQuickExpm1); |
| 834 | } |
| 835 | |
| 836 | void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 837 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) { |
| 841 | GenFPToFPCall(invoke, codegen_, kQuickLog); |
| 842 | } |
| 843 | |
| 844 | void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 845 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) { |
| 849 | GenFPToFPCall(invoke, codegen_, kQuickLog10); |
| 850 | } |
| 851 | |
| 852 | void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 853 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) { |
| 857 | GenFPToFPCall(invoke, codegen_, kQuickSinh); |
| 858 | } |
| 859 | |
| 860 | void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 861 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) { |
| 865 | GenFPToFPCall(invoke, codegen_, kQuickTan); |
| 866 | } |
| 867 | |
| 868 | void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 869 | CreateFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) { |
| 873 | GenFPToFPCall(invoke, codegen_, kQuickTanh); |
| 874 | } |
| 875 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 876 | static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 877 | LocationSummary* locations = |
| 878 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 879 | InvokeRuntimeCallingConvention calling_convention; |
| 880 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 881 | locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1))); |
| 882 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 883 | |
| 884 | // We have to ensure that the native code doesn't clobber the XMM registers which are |
| 885 | // non-volatile for ART, but volatile for Native calls. This will ensure that they are |
| 886 | // saved in the prologue and properly restored. |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 887 | for (FloatRegister fp_reg : non_volatile_xmm_regs) { |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 888 | locations->AddTemp(Location::FpuRegisterLocation(fp_reg)); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 893 | CreateFPFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) { |
| 897 | GenFPToFPCall(invoke, codegen_, kQuickAtan2); |
| 898 | } |
| 899 | |
| 900 | void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 901 | CreateFPFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) { |
| 905 | GenFPToFPCall(invoke, codegen_, kQuickHypot); |
| 906 | } |
| 907 | |
| 908 | void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 909 | CreateFPFPToFPCallLocations(allocator_, invoke); |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) { |
| 913 | GenFPToFPCall(invoke, codegen_, kQuickNextAfter); |
| 914 | } |
| 915 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 916 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 917 | // Check to see if we have known failures that will cause us to have to bail out |
| 918 | // to the runtime, and just generate the runtime call directly. |
| 919 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 920 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 921 | |
| 922 | // The positions must be non-negative. |
| 923 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 924 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 925 | // We will have to fail anyways. |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | // The length must be > 0. |
| 930 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 931 | if (length != nullptr) { |
| 932 | int32_t len = length->GetValue(); |
| 933 | if (len < 0) { |
| 934 | // Just call as normal. |
| 935 | return; |
| 936 | } |
| 937 | } |
| 938 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 939 | LocationSummary* locations = |
| 940 | new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 941 | // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length). |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 942 | locations->SetInAt(0, Location::RequiresRegister()); |
| 943 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 944 | locations->SetInAt(2, Location::RequiresRegister()); |
| 945 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 946 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 947 | |
| 948 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 949 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 950 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 951 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 952 | } |
| 953 | |
| 954 | static void CheckPosition(X86_64Assembler* assembler, |
| 955 | Location pos, |
| 956 | CpuRegister input, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 957 | Location length, |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 958 | SlowPathCode* slow_path, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 959 | CpuRegister temp, |
| 960 | bool length_is_input_length = false) { |
| 961 | // Where is the length in the Array? |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 962 | const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 963 | |
| 964 | if (pos.IsConstant()) { |
| 965 | int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue(); |
| 966 | if (pos_const == 0) { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 967 | if (!length_is_input_length) { |
| 968 | // Check that length(input) >= length. |
| 969 | if (length.IsConstant()) { |
| 970 | __ cmpl(Address(input, length_offset), |
| 971 | Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 972 | } else { |
| 973 | __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>()); |
| 974 | } |
| 975 | __ j(kLess, slow_path->GetEntryLabel()); |
| 976 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 977 | } else { |
| 978 | // Check that length(input) >= pos. |
Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 979 | __ movl(temp, Address(input, length_offset)); |
| 980 | __ subl(temp, Immediate(pos_const)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 981 | __ j(kLess, slow_path->GetEntryLabel()); |
| 982 | |
| 983 | // Check that (length(input) - pos) >= length. |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 984 | if (length.IsConstant()) { |
| 985 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 986 | } else { |
| 987 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 988 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 989 | __ j(kLess, slow_path->GetEntryLabel()); |
| 990 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 991 | } else if (length_is_input_length) { |
| 992 | // The only way the copy can succeed is if pos is zero. |
| 993 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 994 | __ testl(pos_reg, pos_reg); |
| 995 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 996 | } else { |
| 997 | // Check that pos >= 0. |
| 998 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 999 | __ testl(pos_reg, pos_reg); |
| 1000 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1001 | |
| 1002 | // Check that pos <= length(input). |
| 1003 | __ cmpl(Address(input, length_offset), pos_reg); |
| 1004 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1005 | |
| 1006 | // Check that (length(input) - pos) >= length. |
| 1007 | __ movl(temp, Address(input, length_offset)); |
| 1008 | __ subl(temp, pos_reg); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1009 | if (length.IsConstant()) { |
| 1010 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 1011 | } else { |
| 1012 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 1013 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1014 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 1019 | X86_64Assembler* assembler = GetAssembler(); |
| 1020 | LocationSummary* locations = invoke->GetLocations(); |
| 1021 | |
| 1022 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1023 | Location src_pos = locations->InAt(1); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1024 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1025 | Location dest_pos = locations->InAt(3); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1026 | Location length = locations->InAt(4); |
| 1027 | |
| 1028 | // Temporaries that we need for MOVSW. |
| 1029 | CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1030 | DCHECK_EQ(src_base.AsRegister(), RSI); |
| 1031 | CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1032 | DCHECK_EQ(dest_base.AsRegister(), RDI); |
| 1033 | CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 1034 | DCHECK_EQ(count.AsRegister(), RCX); |
| 1035 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1036 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1037 | codegen_->AddSlowPath(slow_path); |
| 1038 | |
| 1039 | // Bail out if the source and destination are the same. |
| 1040 | __ cmpl(src, dest); |
| 1041 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1042 | |
| 1043 | // Bail out if the source is null. |
| 1044 | __ testl(src, src); |
| 1045 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1046 | |
| 1047 | // Bail out if the destination is null. |
| 1048 | __ testl(dest, dest); |
| 1049 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1050 | |
| 1051 | // If the length is negative, bail out. |
| 1052 | // We have already checked in the LocationsBuilder for the constant case. |
| 1053 | if (!length.IsConstant()) { |
| 1054 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
| 1055 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1056 | } |
| 1057 | |
Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 1058 | // Validity checks: source. Use src_base as a temporary register. |
| 1059 | CheckPosition(assembler, src_pos, src, length, slow_path, src_base); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1060 | |
Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 1061 | // Validity checks: dest. Use src_base as a temporary register. |
| 1062 | CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1063 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1064 | // We need the count in RCX. |
| 1065 | if (length.IsConstant()) { |
| 1066 | __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 1067 | } else { |
| 1068 | __ movl(count, length.AsRegister<CpuRegister>()); |
| 1069 | } |
| 1070 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1071 | // Okay, everything checks out. Finally time to do the copy. |
| 1072 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1073 | const size_t char_size = DataType::Size(DataType::Type::kUint16); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1074 | DCHECK_EQ(char_size, 2u); |
| 1075 | |
| 1076 | const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value(); |
| 1077 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1078 | if (src_pos.IsConstant()) { |
| 1079 | int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1080 | __ leal(src_base, Address(src, char_size * src_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1081 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1082 | __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1083 | ScaleFactor::TIMES_2, data_offset)); |
| 1084 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1085 | if (dest_pos.IsConstant()) { |
| 1086 | int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1087 | __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1088 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1089 | __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1090 | ScaleFactor::TIMES_2, data_offset)); |
| 1091 | } |
| 1092 | |
| 1093 | // Do the move. |
| 1094 | __ rep_movsw(); |
| 1095 | |
| 1096 | __ Bind(slow_path->GetExitLabel()); |
| 1097 | } |
| 1098 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1099 | |
| 1100 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1101 | // The only read barrier implementation supporting the |
| 1102 | // SystemArrayCopy intrinsic is the Baker-style read barriers. |
| 1103 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 1107 | CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 1110 | // Compute base source address, base destination address, and end |
| 1111 | // source address for the System.arraycopy intrinsic in `src_base`, |
| 1112 | // `dst_base` and `src_end` respectively. |
| 1113 | static void GenSystemArrayCopyAddresses(X86_64Assembler* assembler, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1114 | DataType::Type type, |
Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 1115 | const CpuRegister& src, |
| 1116 | const Location& src_pos, |
| 1117 | const CpuRegister& dst, |
| 1118 | const Location& dst_pos, |
| 1119 | const Location& copy_length, |
| 1120 | const CpuRegister& src_base, |
| 1121 | const CpuRegister& dst_base, |
| 1122 | const CpuRegister& src_end) { |
| 1123 | // This routine is only used by the SystemArrayCopy intrinsic. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1124 | DCHECK_EQ(type, DataType::Type::kReference); |
| 1125 | const int32_t element_size = DataType::Size(type); |
| 1126 | const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type)); |
Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 1127 | const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value(); |
| 1128 | |
| 1129 | if (src_pos.IsConstant()) { |
| 1130 | int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1131 | __ leal(src_base, Address(src, element_size * constant + data_offset)); |
| 1132 | } else { |
| 1133 | __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset)); |
| 1134 | } |
| 1135 | |
| 1136 | if (dst_pos.IsConstant()) { |
| 1137 | int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1138 | __ leal(dst_base, Address(dst, element_size * constant + data_offset)); |
| 1139 | } else { |
| 1140 | __ leal(dst_base, Address(dst, dst_pos.AsRegister<CpuRegister>(), scale_factor, data_offset)); |
| 1141 | } |
| 1142 | |
| 1143 | if (copy_length.IsConstant()) { |
| 1144 | int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue(); |
| 1145 | __ leal(src_end, Address(src_base, element_size * constant)); |
| 1146 | } else { |
| 1147 | __ leal(src_end, Address(src_base, copy_length.AsRegister<CpuRegister>(), scale_factor, 0)); |
| 1148 | } |
| 1149 | } |
| 1150 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1151 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1152 | // The only read barrier implementation supporting the |
| 1153 | // SystemArrayCopy intrinsic is the Baker-style read barriers. |
| 1154 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 1155 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1156 | X86_64Assembler* assembler = GetAssembler(); |
| 1157 | LocationSummary* locations = invoke->GetLocations(); |
| 1158 | |
| 1159 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 1160 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 1161 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 1162 | uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1163 | uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1164 | |
| 1165 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1166 | Location src_pos = locations->InAt(1); |
| 1167 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1168 | Location dest_pos = locations->InAt(3); |
| 1169 | Location length = locations->InAt(4); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1170 | Location temp1_loc = locations->GetTemp(0); |
| 1171 | CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>(); |
| 1172 | Location temp2_loc = locations->GetTemp(1); |
| 1173 | CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>(); |
| 1174 | Location temp3_loc = locations->GetTemp(2); |
| 1175 | CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>(); |
| 1176 | Location TMP_loc = Location::RegisterLocation(TMP); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1177 | |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1178 | SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
| 1179 | codegen_->AddSlowPath(intrinsic_slow_path); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1180 | |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 1181 | NearLabel conditions_on_positions_validated; |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1182 | SystemArrayCopyOptimizations optimizations(invoke); |
| 1183 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1184 | // If source and destination are the same, we go to slow path if we need to do |
| 1185 | // forward copying. |
| 1186 | if (src_pos.IsConstant()) { |
| 1187 | int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1188 | if (dest_pos.IsConstant()) { |
Nicolas Geoffray | 9f65db8 | 2016-07-07 12:07:42 +0100 | [diff] [blame] | 1189 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1190 | if (optimizations.GetDestinationIsSource()) { |
| 1191 | // Checked when building locations. |
| 1192 | DCHECK_GE(src_pos_constant, dest_pos_constant); |
| 1193 | } else if (src_pos_constant < dest_pos_constant) { |
| 1194 | __ cmpl(src, dest); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1195 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | 9f65db8 | 2016-07-07 12:07:42 +0100 | [diff] [blame] | 1196 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1197 | } else { |
| 1198 | if (!optimizations.GetDestinationIsSource()) { |
Nicolas Geoffray | 9f65db8 | 2016-07-07 12:07:42 +0100 | [diff] [blame] | 1199 | __ cmpl(src, dest); |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 1200 | __ j(kNotEqual, &conditions_on_positions_validated); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1201 | } |
| 1202 | __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1203 | __ j(kGreater, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1204 | } |
| 1205 | } else { |
| 1206 | if (!optimizations.GetDestinationIsSource()) { |
Nicolas Geoffray | 9f65db8 | 2016-07-07 12:07:42 +0100 | [diff] [blame] | 1207 | __ cmpl(src, dest); |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 1208 | __ j(kNotEqual, &conditions_on_positions_validated); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1209 | } |
| 1210 | if (dest_pos.IsConstant()) { |
| 1211 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1212 | __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1213 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1214 | } else { |
| 1215 | __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>()); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1216 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 1220 | __ Bind(&conditions_on_positions_validated); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1221 | |
| 1222 | if (!optimizations.GetSourceIsNotNull()) { |
| 1223 | // Bail out if the source is null. |
| 1224 | __ testl(src, src); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1225 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) { |
| 1229 | // Bail out if the destination is null. |
| 1230 | __ testl(dest, dest); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1231 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | // If the length is negative, bail out. |
| 1235 | // We have already checked in the LocationsBuilder for the constant case. |
| 1236 | if (!length.IsConstant() && |
| 1237 | !optimizations.GetCountIsSourceLength() && |
| 1238 | !optimizations.GetCountIsDestinationLength()) { |
| 1239 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1240 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | // Validity checks: source. |
| 1244 | CheckPosition(assembler, |
| 1245 | src_pos, |
| 1246 | src, |
| 1247 | length, |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1248 | intrinsic_slow_path, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1249 | temp1, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1250 | optimizations.GetCountIsSourceLength()); |
| 1251 | |
| 1252 | // Validity checks: dest. |
| 1253 | CheckPosition(assembler, |
| 1254 | dest_pos, |
| 1255 | dest, |
| 1256 | length, |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1257 | intrinsic_slow_path, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1258 | temp1, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1259 | optimizations.GetCountIsDestinationLength()); |
| 1260 | |
| 1261 | if (!optimizations.GetDoesNotNeedTypeCheck()) { |
| 1262 | // Check whether all elements of the source array are assignable to the component |
| 1263 | // type of the destination array. We do two checks: the classes are the same, |
| 1264 | // or the destination is Object[]. If none of these checks succeed, we go to the |
| 1265 | // slow path. |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1266 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1267 | bool did_unpoison = false; |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1268 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1269 | // /* HeapReference<Class> */ temp1 = dest->klass_ |
| 1270 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1271 | invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1272 | // Register `temp1` is not trashed by the read barrier emitted |
| 1273 | // by GenerateFieldLoadWithBakerReadBarrier below, as that |
| 1274 | // method produces a call to a ReadBarrierMarkRegX entry point, |
| 1275 | // which saves all potentially live registers, including |
| 1276 | // temporaries such a `temp1`. |
| 1277 | // /* HeapReference<Class> */ temp2 = src->klass_ |
| 1278 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1279 | invoke, temp2_loc, src, class_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1280 | // If heap poisoning is enabled, `temp1` and `temp2` have been |
| 1281 | // unpoisoned by the the previous calls to |
| 1282 | // GenerateFieldLoadWithBakerReadBarrier. |
| 1283 | } else { |
| 1284 | // /* HeapReference<Class> */ temp1 = dest->klass_ |
| 1285 | __ movl(temp1, Address(dest, class_offset)); |
| 1286 | // /* HeapReference<Class> */ temp2 = src->klass_ |
| 1287 | __ movl(temp2, Address(src, class_offset)); |
| 1288 | if (!optimizations.GetDestinationIsNonPrimitiveArray() || |
| 1289 | !optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1290 | // One or two of the references need to be unpoisoned. Unpoison them |
| 1291 | // both to make the identity check valid. |
| 1292 | __ MaybeUnpoisonHeapReference(temp1); |
| 1293 | __ MaybeUnpoisonHeapReference(temp2); |
| 1294 | did_unpoison = true; |
| 1295 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | if (!optimizations.GetDestinationIsNonPrimitiveArray()) { |
| 1299 | // Bail out if the destination is not a non primitive array. |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1300 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1301 | // /* HeapReference<Class> */ TMP = temp1->component_type_ |
| 1302 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1303 | invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1304 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1305 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1306 | // If heap poisoning is enabled, `TMP` has been unpoisoned by |
| 1307 | // the the previous call to GenerateFieldLoadWithBakerReadBarrier. |
| 1308 | } else { |
| 1309 | // /* HeapReference<Class> */ TMP = temp1->component_type_ |
| 1310 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1311 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1312 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1313 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1314 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1315 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1316 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1320 | // Bail out if the source is not a non primitive array. |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1321 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1322 | // For the same reason given earlier, `temp1` is not trashed by the |
| 1323 | // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below. |
| 1324 | // /* HeapReference<Class> */ TMP = temp2->component_type_ |
| 1325 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1326 | invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1327 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1328 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1329 | // If heap poisoning is enabled, `TMP` has been unpoisoned by |
| 1330 | // the the previous call to GenerateFieldLoadWithBakerReadBarrier. |
| 1331 | } else { |
| 1332 | // /* HeapReference<Class> */ TMP = temp2->component_type_ |
| 1333 | __ movl(CpuRegister(TMP), Address(temp2, component_offset)); |
| 1334 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1335 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1336 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1337 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1338 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1339 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | __ cmpl(temp1, temp2); |
| 1343 | |
| 1344 | if (optimizations.GetDestinationIsTypedObjectArray()) { |
| 1345 | NearLabel do_copy; |
| 1346 | __ j(kEqual, &do_copy); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1347 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1348 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 1349 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1350 | invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1351 | // We do not need to emit a read barrier for the following |
| 1352 | // heap reference load, as `temp1` is only used in a |
| 1353 | // comparison with null below, and this reference is not |
| 1354 | // kept afterwards. |
| 1355 | __ cmpl(Address(temp1, super_offset), Immediate(0)); |
| 1356 | } else { |
| 1357 | if (!did_unpoison) { |
| 1358 | __ MaybeUnpoisonHeapReference(temp1); |
| 1359 | } |
| 1360 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 1361 | __ movl(temp1, Address(temp1, component_offset)); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1362 | __ MaybeUnpoisonHeapReference(temp1); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1363 | // No need to unpoison the following heap reference load, as |
| 1364 | // we're comparing against null. |
| 1365 | __ cmpl(Address(temp1, super_offset), Immediate(0)); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1366 | } |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1367 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1368 | __ Bind(&do_copy); |
| 1369 | } else { |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1370 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1371 | } |
| 1372 | } else if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1373 | DCHECK(optimizations.GetDestinationIsNonPrimitiveArray()); |
| 1374 | // Bail out if the source is not a non primitive array. |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1375 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1376 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 1377 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1378 | invoke, temp1_loc, src, class_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1379 | // /* HeapReference<Class> */ TMP = temp1->component_type_ |
| 1380 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1381 | invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1382 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1383 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1384 | } else { |
| 1385 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 1386 | __ movl(temp1, Address(src, class_offset)); |
| 1387 | __ MaybeUnpoisonHeapReference(temp1); |
| 1388 | // /* HeapReference<Class> */ TMP = temp1->component_type_ |
| 1389 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1390 | // No need to unpoison `TMP` now, as we're comparing against null. |
| 1391 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1392 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 1393 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1394 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1395 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1396 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1397 | } |
| 1398 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1399 | const DataType::Type type = DataType::Type::kReference; |
| 1400 | const int32_t element_size = DataType::Size(type); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1401 | |
Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 1402 | // Compute base source address, base destination address, and end |
| 1403 | // source address in `temp1`, `temp2` and `temp3` respectively. |
| 1404 | GenSystemArrayCopyAddresses( |
| 1405 | GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1406 | |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1407 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 1408 | // SystemArrayCopy implementation for Baker read barriers (see |
| 1409 | // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier): |
| 1410 | // |
| 1411 | // if (src_ptr != end_ptr) { |
| 1412 | // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState(); |
| 1413 | // lfence; // Load fence or artificial data dependency to prevent load-load reordering |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1414 | // bool is_gray = (rb_state == ReadBarrier::GrayState()); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1415 | // if (is_gray) { |
| 1416 | // // Slow-path copy. |
| 1417 | // do { |
| 1418 | // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++))); |
| 1419 | // } while (src_ptr != end_ptr) |
| 1420 | // } else { |
| 1421 | // // Fast-path copy. |
| 1422 | // do { |
| 1423 | // *dest_ptr++ = *src_ptr++; |
| 1424 | // } while (src_ptr != end_ptr) |
| 1425 | // } |
| 1426 | // } |
| 1427 | |
| 1428 | NearLabel loop, done; |
| 1429 | |
| 1430 | // Don't enter copy loop if `length == 0`. |
| 1431 | __ cmpl(temp1, temp3); |
| 1432 | __ j(kEqual, &done); |
| 1433 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1434 | // 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] | 1435 | static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0"); |
| 1436 | static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1"); |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1437 | constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte; |
| 1438 | constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte; |
| 1439 | constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position); |
| 1440 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1441 | // if (rb_state == ReadBarrier::GrayState()) |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1442 | // goto slow_path; |
| 1443 | // At this point, just do the "if" and make sure that flags are preserved until the branch. |
| 1444 | __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1445 | |
| 1446 | // Load fence to prevent load-load reordering. |
| 1447 | // Note that this is a no-op, thanks to the x86-64 memory model. |
| 1448 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 1449 | |
| 1450 | // Slow path used to copy array when `src` is gray. |
| 1451 | SlowPathCode* read_barrier_slow_path = |
| 1452 | new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke); |
| 1453 | codegen_->AddSlowPath(read_barrier_slow_path); |
| 1454 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1455 | // We have done the "if" of the gray bit check above, now branch based on the flags. |
| 1456 | __ j(kNotZero, read_barrier_slow_path->GetEntryLabel()); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1457 | |
| 1458 | // Fast-path copy. |
| 1459 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1460 | // poison/unpoison. |
| 1461 | __ Bind(&loop); |
| 1462 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1463 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1464 | __ addl(temp1, Immediate(element_size)); |
| 1465 | __ addl(temp2, Immediate(element_size)); |
| 1466 | __ cmpl(temp1, temp3); |
| 1467 | __ j(kNotEqual, &loop); |
| 1468 | |
| 1469 | __ Bind(read_barrier_slow_path->GetExitLabel()); |
| 1470 | __ Bind(&done); |
| 1471 | } else { |
| 1472 | // Non read barrier code. |
| 1473 | |
| 1474 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1475 | // poison/unpoison. |
| 1476 | NearLabel loop, done; |
| 1477 | __ cmpl(temp1, temp3); |
| 1478 | __ j(kEqual, &done); |
| 1479 | __ Bind(&loop); |
| 1480 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1481 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1482 | __ addl(temp1, Immediate(element_size)); |
| 1483 | __ addl(temp2, Immediate(element_size)); |
| 1484 | __ cmpl(temp1, temp3); |
| 1485 | __ j(kNotEqual, &loop); |
| 1486 | __ Bind(&done); |
| 1487 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1488 | |
| 1489 | // We only need one card marking on the destination array. |
Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 1490 | codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* value_can_be_null */ false); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1491 | |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1492 | __ Bind(intrinsic_slow_path->GetExitLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1495 | void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1496 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 1497 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1498 | InvokeRuntimeCallingConvention calling_convention; |
| 1499 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1500 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1501 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1502 | } |
| 1503 | |
| 1504 | void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1505 | X86_64Assembler* assembler = GetAssembler(); |
| 1506 | LocationSummary* locations = invoke->GetLocations(); |
| 1507 | |
Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 1508 | // Note that the null check must have been done earlier. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1509 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1510 | |
| 1511 | CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1512 | __ testl(argument, argument); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1513 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1514 | codegen_->AddSlowPath(slow_path); |
| 1515 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1516 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1517 | codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1518 | __ Bind(slow_path->GetExitLabel()); |
| 1519 | } |
| 1520 | |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1521 | void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1522 | LocationSummary* locations = |
| 1523 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1524 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1525 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1526 | |
| 1527 | // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction. |
| 1528 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1529 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1530 | |
| 1531 | // Set output, RSI needed for repe_cmpsq instruction anyways. |
| 1532 | locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap); |
| 1533 | } |
| 1534 | |
| 1535 | void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1536 | X86_64Assembler* assembler = GetAssembler(); |
| 1537 | LocationSummary* locations = invoke->GetLocations(); |
| 1538 | |
| 1539 | CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1540 | CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1541 | CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1542 | CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1543 | CpuRegister rsi = locations->Out().AsRegister<CpuRegister>(); |
| 1544 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1545 | NearLabel end, return_true, return_false; |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1546 | |
| 1547 | // Get offsets of count, value, and class fields within a string object. |
| 1548 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1549 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1550 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1551 | |
| 1552 | // Note that the null check must have been done earlier. |
| 1553 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1554 | |
Vladimir Marko | 53b5200 | 2016-05-24 19:30:45 +0100 | [diff] [blame] | 1555 | StringEqualsOptimizations optimizations(invoke); |
| 1556 | if (!optimizations.GetArgumentNotNull()) { |
| 1557 | // Check if input is null, return false if it is. |
| 1558 | __ testl(arg, arg); |
| 1559 | __ j(kEqual, &return_false); |
| 1560 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1561 | |
Vladimir Marko | 53b5200 | 2016-05-24 19:30:45 +0100 | [diff] [blame] | 1562 | if (!optimizations.GetArgumentIsString()) { |
| 1563 | // Instanceof check for the argument by comparing class fields. |
| 1564 | // All string objects must have the same type since String cannot be subclassed. |
| 1565 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1566 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 1567 | __ movl(rcx, Address(str, class_offset)); |
| 1568 | __ cmpl(rcx, Address(arg, class_offset)); |
| 1569 | __ j(kNotEqual, &return_false); |
| 1570 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1571 | |
| 1572 | // Reference equality check, return true if same reference. |
| 1573 | __ cmpl(str, arg); |
| 1574 | __ j(kEqual, &return_true); |
| 1575 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1576 | // Load length and compression flag of receiver string. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1577 | __ movl(rcx, Address(str, count_offset)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1578 | // Check if lengths and compressiond flags are equal, return false if they're not. |
| 1579 | // Two identical strings will always have same compression style since |
| 1580 | // compression style is decided on alloc. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1581 | __ cmpl(rcx, Address(arg, count_offset)); |
| 1582 | __ j(kNotEqual, &return_false); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1583 | // Return true if both strings are empty. Even with string compression `count == 0` means empty. |
| 1584 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 1585 | "Expecting 0=compressed, 1=uncompressed"); |
| 1586 | __ jrcxz(&return_true); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1587 | |
| 1588 | if (mirror::kUseStringCompression) { |
| 1589 | NearLabel string_uncompressed; |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1590 | // Extract length and differentiate between both compressed or both uncompressed. |
| 1591 | // Different compression style is cut above. |
| 1592 | __ shrl(rcx, Immediate(1)); |
| 1593 | __ j(kCarrySet, &string_uncompressed); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1594 | // Divide string length by 2, rounding up, and continue as if uncompressed. |
| 1595 | // Merge clearing the compression flag with +1 for rounding. |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1596 | __ addl(rcx, Immediate(1)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1597 | __ shrl(rcx, Immediate(1)); |
| 1598 | __ Bind(&string_uncompressed); |
| 1599 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1600 | // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction. |
| 1601 | __ leal(rsi, Address(str, value_offset)); |
| 1602 | __ leal(rdi, Address(arg, value_offset)); |
| 1603 | |
| 1604 | // Divide string length by 4 and adjust for lengths not divisible by 4. |
| 1605 | __ addl(rcx, Immediate(3)); |
| 1606 | __ shrl(rcx, Immediate(2)); |
| 1607 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1608 | // Assertions that must hold in order to compare strings 4 characters (uncompressed) |
| 1609 | // or 8 characters (compressed) at a time. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1610 | DCHECK_ALIGNED(value_offset, 8); |
| 1611 | static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded"); |
| 1612 | |
| 1613 | // Loop to compare strings four characters at a time starting at the beginning of the string. |
| 1614 | __ repe_cmpsq(); |
| 1615 | // If strings are not equal, zero flag will be cleared. |
| 1616 | __ j(kNotEqual, &return_false); |
| 1617 | |
| 1618 | // Return true and exit the function. |
| 1619 | // If loop does not result in returning false, we return true. |
| 1620 | __ Bind(&return_true); |
| 1621 | __ movl(rsi, Immediate(1)); |
| 1622 | __ jmp(&end); |
| 1623 | |
| 1624 | // Return false and exit the function. |
| 1625 | __ Bind(&return_false); |
| 1626 | __ xorl(rsi, rsi); |
| 1627 | __ Bind(&end); |
| 1628 | } |
| 1629 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1630 | static void CreateStringIndexOfLocations(HInvoke* invoke, |
| 1631 | ArenaAllocator* allocator, |
| 1632 | bool start_at_zero) { |
| 1633 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1634 | LocationSummary::kCallOnSlowPath, |
| 1635 | kIntrinsified); |
| 1636 | // The data needs to be in RDI for scasw. So request that the string is there, anyways. |
| 1637 | locations->SetInAt(0, Location::RegisterLocation(RDI)); |
| 1638 | // If we look for a constant char, we'll still have to copy it into RAX. So just request the |
| 1639 | // allocator to do that, anyways. We can still do the constant check by checking the parameter |
| 1640 | // of the instruction explicitly. |
| 1641 | // Note: This works as we don't clobber RAX anywhere. |
| 1642 | locations->SetInAt(1, Location::RegisterLocation(RAX)); |
| 1643 | if (!start_at_zero) { |
| 1644 | locations->SetInAt(2, Location::RequiresRegister()); // The starting index. |
| 1645 | } |
| 1646 | // As we clobber RDI during execution anyways, also use it as the output. |
| 1647 | locations->SetOut(Location::SameAsFirstInput()); |
| 1648 | |
| 1649 | // repne scasw uses RCX as the counter. |
| 1650 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1651 | // Need another temporary to be able to compute the result. |
| 1652 | locations->AddTemp(Location::RequiresRegister()); |
| 1653 | } |
| 1654 | |
| 1655 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 1656 | X86_64Assembler* assembler, |
| 1657 | CodeGeneratorX86_64* codegen, |
| 1658 | ArenaAllocator* allocator, |
| 1659 | bool start_at_zero) { |
| 1660 | LocationSummary* locations = invoke->GetLocations(); |
| 1661 | |
| 1662 | // Note that the null check must have been done earlier. |
| 1663 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1664 | |
| 1665 | CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1666 | CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1667 | CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1668 | CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1669 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1670 | |
| 1671 | // Check our assumptions for registers. |
| 1672 | DCHECK_EQ(string_obj.AsRegister(), RDI); |
| 1673 | DCHECK_EQ(search_value.AsRegister(), RAX); |
| 1674 | DCHECK_EQ(counter.AsRegister(), RCX); |
| 1675 | DCHECK_EQ(out.AsRegister(), RDI); |
| 1676 | |
| 1677 | // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically, |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 1678 | // or directly dispatch for a large constant, or omit slow-path for a small constant or a char. |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1679 | SlowPathCode* slow_path = nullptr; |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 1680 | HInstruction* code_point = invoke->InputAt(1); |
| 1681 | if (code_point->IsIntConstant()) { |
Vladimir Marko | da05108 | 2016-05-17 16:10:20 +0100 | [diff] [blame] | 1682 | if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1683 | std::numeric_limits<uint16_t>::max()) { |
| 1684 | // Always needs the slow-path. We could directly dispatch to it, but this case should be |
| 1685 | // rare, so for simplicity just put the full slow-path down and branch unconditionally. |
| 1686 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1687 | codegen->AddSlowPath(slow_path); |
| 1688 | __ jmp(slow_path->GetEntryLabel()); |
| 1689 | __ Bind(slow_path->GetExitLabel()); |
| 1690 | return; |
| 1691 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1692 | } else if (code_point->GetType() != DataType::Type::kUint16) { |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1693 | __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max())); |
| 1694 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1695 | codegen->AddSlowPath(slow_path); |
| 1696 | __ j(kAbove, slow_path->GetEntryLabel()); |
| 1697 | } |
| 1698 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1699 | // From here down, we know that we are looking for a char that fits in |
| 1700 | // 16 bits (uncompressed) or 8 bits (compressed). |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1701 | // Location of reference to data array within the String object. |
| 1702 | int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1703 | // Location of count within the String object. |
| 1704 | int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1705 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1706 | // Load the count field of the string containing the length and compression flag. |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1707 | __ movl(string_length, Address(string_obj, count_offset)); |
| 1708 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1709 | // Do a zero-length check. Even with string compression `count == 0` means empty. |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1710 | // TODO: Support jecxz. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1711 | NearLabel not_found_label; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1712 | __ testl(string_length, string_length); |
| 1713 | __ j(kEqual, ¬_found_label); |
| 1714 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1715 | if (mirror::kUseStringCompression) { |
| 1716 | // Use TMP to keep string_length_flagged. |
| 1717 | __ movl(CpuRegister(TMP), string_length); |
| 1718 | // Mask out first bit used as compression flag. |
| 1719 | __ shrl(string_length, Immediate(1)); |
| 1720 | } |
| 1721 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1722 | if (start_at_zero) { |
| 1723 | // Number of chars to scan is the same as the string length. |
| 1724 | __ movl(counter, string_length); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1725 | // Move to the start of the string. |
| 1726 | __ addq(string_obj, Immediate(value_offset)); |
| 1727 | } else { |
| 1728 | CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1729 | |
| 1730 | // Do a start_index check. |
| 1731 | __ cmpl(start_index, string_length); |
| 1732 | __ j(kGreaterEqual, ¬_found_label); |
| 1733 | |
| 1734 | // Ensure we have a start index >= 0; |
| 1735 | __ xorl(counter, counter); |
| 1736 | __ cmpl(start_index, Immediate(0)); |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1737 | __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough. |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1738 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1739 | if (mirror::kUseStringCompression) { |
| 1740 | NearLabel modify_counter, offset_uncompressed_label; |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1741 | __ testl(CpuRegister(TMP), Immediate(1)); |
| 1742 | __ j(kNotZero, &offset_uncompressed_label); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1743 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset)); |
| 1744 | __ jmp(&modify_counter); |
| 1745 | // Move to the start of the string: string_obj + value_offset + 2 * start_index. |
| 1746 | __ Bind(&offset_uncompressed_label); |
| 1747 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1748 | __ Bind(&modify_counter); |
| 1749 | } else { |
| 1750 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1751 | } |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1752 | // Now update ecx, the work counter: it's gonna be string.length - start_index. |
| 1753 | __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit. |
| 1754 | __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0)); |
| 1755 | } |
| 1756 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1757 | if (mirror::kUseStringCompression) { |
| 1758 | NearLabel uncompressed_string_comparison; |
| 1759 | NearLabel comparison_done; |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1760 | __ testl(CpuRegister(TMP), Immediate(1)); |
| 1761 | __ j(kNotZero, &uncompressed_string_comparison); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1762 | // Check if RAX (search_value) is ASCII. |
| 1763 | __ cmpl(search_value, Immediate(127)); |
| 1764 | __ j(kGreater, ¬_found_label); |
| 1765 | // Comparing byte-per-byte. |
| 1766 | __ repne_scasb(); |
| 1767 | __ jmp(&comparison_done); |
| 1768 | // Everything is set up for repne scasw: |
| 1769 | // * Comparison address in RDI. |
| 1770 | // * Counter in ECX. |
| 1771 | __ Bind(&uncompressed_string_comparison); |
| 1772 | __ repne_scasw(); |
| 1773 | __ Bind(&comparison_done); |
| 1774 | } else { |
| 1775 | __ repne_scasw(); |
| 1776 | } |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1777 | // Did we find a match? |
| 1778 | __ j(kNotEqual, ¬_found_label); |
| 1779 | |
| 1780 | // Yes, we matched. Compute the index of the result. |
| 1781 | __ subl(string_length, counter); |
| 1782 | __ leal(out, Address(string_length, -1)); |
| 1783 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1784 | NearLabel done; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1785 | __ jmp(&done); |
| 1786 | |
| 1787 | // Failed to match; return -1. |
| 1788 | __ Bind(¬_found_label); |
| 1789 | __ movl(out, Immediate(-1)); |
| 1790 | |
| 1791 | // And join up at the end. |
| 1792 | __ Bind(&done); |
| 1793 | if (slow_path != nullptr) { |
| 1794 | __ Bind(slow_path->GetExitLabel()); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1799 | CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1803 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1807 | CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1811 | GenerateStringIndexOf( |
| 1812 | invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1815 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1816 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 1817 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1818 | InvokeRuntimeCallingConvention calling_convention; |
| 1819 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1820 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1821 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1822 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 1823 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1824 | } |
| 1825 | |
| 1826 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1827 | X86_64Assembler* assembler = GetAssembler(); |
| 1828 | LocationSummary* locations = invoke->GetLocations(); |
| 1829 | |
| 1830 | CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1831 | __ testl(byte_array, byte_array); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1832 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1833 | codegen_->AddSlowPath(slow_path); |
| 1834 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1835 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1836 | codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1837 | CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1838 | __ Bind(slow_path->GetExitLabel()); |
| 1839 | } |
| 1840 | |
| 1841 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1842 | LocationSummary* locations = |
| 1843 | new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1844 | InvokeRuntimeCallingConvention calling_convention; |
| 1845 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1846 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1847 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1848 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1849 | } |
| 1850 | |
| 1851 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 1852 | // No need to emit code checking whether `locations->InAt(2)` is a null |
| 1853 | // pointer, as callers of the native method |
| 1854 | // |
| 1855 | // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data) |
| 1856 | // |
| 1857 | // all include a null check on `data` before calling that method. |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1858 | codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1859 | CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1863 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 1864 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1865 | InvokeRuntimeCallingConvention calling_convention; |
| 1866 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1867 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1868 | } |
| 1869 | |
| 1870 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1871 | X86_64Assembler* assembler = GetAssembler(); |
| 1872 | LocationSummary* locations = invoke->GetLocations(); |
| 1873 | |
| 1874 | CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1875 | __ testl(string_to_copy, string_to_copy); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1876 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1877 | codegen_->AddSlowPath(slow_path); |
| 1878 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1879 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1880 | codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1881 | CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1882 | __ Bind(slow_path->GetExitLabel()); |
| 1883 | } |
| 1884 | |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1885 | void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1886 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1887 | LocationSummary* locations = |
| 1888 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1889 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1890 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1891 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1892 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1893 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1894 | |
| 1895 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 1896 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 1897 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1898 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1899 | } |
| 1900 | |
| 1901 | void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1902 | X86_64Assembler* assembler = GetAssembler(); |
| 1903 | LocationSummary* locations = invoke->GetLocations(); |
| 1904 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1905 | size_t char_component_size = DataType::Size(DataType::Type::kUint16); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1906 | // Location of data in char array buffer. |
| 1907 | const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value(); |
| 1908 | // Location of char array data in string. |
| 1909 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1910 | |
| 1911 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1912 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1913 | Location srcBegin = locations->InAt(1); |
| 1914 | int srcBegin_value = |
| 1915 | srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0; |
| 1916 | CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1917 | CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1918 | CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1919 | |
| 1920 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1921 | const size_t char_size = DataType::Size(DataType::Type::kUint16); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1922 | DCHECK_EQ(char_size, 2u); |
| 1923 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1924 | NearLabel done; |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1925 | // Compute the number of chars (words) to move. |
| 1926 | __ movl(CpuRegister(RCX), srcEnd); |
| 1927 | if (srcBegin.IsConstant()) { |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1928 | __ subl(CpuRegister(RCX), Immediate(srcBegin_value)); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1929 | } else { |
| 1930 | DCHECK(srcBegin.IsRegister()); |
| 1931 | __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>()); |
| 1932 | } |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1933 | if (mirror::kUseStringCompression) { |
| 1934 | NearLabel copy_uncompressed, copy_loop; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1935 | const size_t c_char_size = DataType::Size(DataType::Type::kInt8); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1936 | DCHECK_EQ(c_char_size, 1u); |
| 1937 | // Location of count in string. |
| 1938 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1939 | |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1940 | __ testl(Address(obj, count_offset), Immediate(1)); |
| 1941 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 1942 | "Expecting 0=compressed, 1=uncompressed"); |
| 1943 | __ j(kNotZero, ©_uncompressed); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1944 | // Compute the address of the source string by adding the number of chars from |
| 1945 | // the source beginning to the value offset of a string. |
| 1946 | __ leaq(CpuRegister(RSI), |
| 1947 | CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset)); |
| 1948 | // Start the loop to copy String's value to Array of Char. |
| 1949 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1950 | |
| 1951 | __ Bind(©_loop); |
| 1952 | __ jrcxz(&done); |
| 1953 | // Use TMP as temporary (convert byte from RSI to word). |
| 1954 | // TODO: Selecting RAX as the temporary and using LODSB/STOSW. |
| 1955 | __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0)); |
| 1956 | __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP)); |
| 1957 | __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size)); |
| 1958 | __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size)); |
| 1959 | // TODO: Add support for LOOP to X86_64Assembler. |
| 1960 | __ subl(CpuRegister(RCX), Immediate(1)); |
| 1961 | __ jmp(©_loop); |
| 1962 | |
| 1963 | __ Bind(©_uncompressed); |
| 1964 | } |
| 1965 | |
| 1966 | __ leaq(CpuRegister(RSI), |
| 1967 | CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset)); |
| 1968 | // Compute the address of the destination buffer. |
| 1969 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1970 | // Do the move. |
| 1971 | __ rep_movsw(); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1972 | |
| 1973 | __ Bind(&done); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1974 | } |
| 1975 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1976 | static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1977 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1978 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity. |
| 1979 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1980 | // to avoid a SIGBUS. |
| 1981 | switch (size) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1982 | case DataType::Type::kInt8: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1983 | __ movsxb(out, Address(address, 0)); |
| 1984 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1985 | case DataType::Type::kInt16: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1986 | __ movsxw(out, Address(address, 0)); |
| 1987 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1988 | case DataType::Type::kInt32: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1989 | __ movl(out, Address(address, 0)); |
| 1990 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1991 | case DataType::Type::kInt64: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1992 | __ movq(out, Address(address, 0)); |
| 1993 | break; |
| 1994 | default: |
| 1995 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 1996 | UNREACHABLE(); |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2001 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2005 | GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2009 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2013 | GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2017 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2021 | GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2025 | CreateIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2029 | GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2032 | static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 2033 | LocationSummary* locations = |
| 2034 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2035 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 2036 | locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2039 | static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2040 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2041 | Location value = locations->InAt(1); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2042 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 2043 | // to avoid a SIGBUS. |
| 2044 | switch (size) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2045 | case DataType::Type::kInt8: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2046 | if (value.IsConstant()) { |
| 2047 | __ movb(Address(address, 0), |
| 2048 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2049 | } else { |
| 2050 | __ movb(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2051 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2052 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2053 | case DataType::Type::kInt16: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2054 | if (value.IsConstant()) { |
| 2055 | __ movw(Address(address, 0), |
| 2056 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2057 | } else { |
| 2058 | __ movw(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2059 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2060 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2061 | case DataType::Type::kInt32: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2062 | if (value.IsConstant()) { |
| 2063 | __ movl(Address(address, 0), |
| 2064 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2065 | } else { |
| 2066 | __ movl(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2067 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2068 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2069 | case DataType::Type::kInt64: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2070 | if (value.IsConstant()) { |
| 2071 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
| 2072 | DCHECK(IsInt<32>(v)); |
| 2073 | int32_t v_32 = v; |
| 2074 | __ movq(Address(address, 0), Immediate(v_32)); |
| 2075 | } else { |
| 2076 | __ movq(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2077 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2078 | break; |
| 2079 | default: |
| 2080 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 2081 | UNREACHABLE(); |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2086 | CreateIntIntToVoidLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2090 | GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2094 | CreateIntIntToVoidLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2098 | GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2102 | CreateIntIntToVoidLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2106 | GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2110 | CreateIntIntToVoidLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2114 | GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2118 | LocationSummary* locations = |
| 2119 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2120 | locations->SetOut(Location::RequiresRegister()); |
| 2121 | } |
| 2122 | |
| 2123 | void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 2124 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2125 | GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(), |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2126 | /* no_rip */ true)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2129 | static void GenUnsafeGet(HInvoke* invoke, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2130 | DataType::Type type, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2131 | bool is_volatile ATTRIBUTE_UNUSED, |
| 2132 | CodeGeneratorX86_64* codegen) { |
| 2133 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 2134 | LocationSummary* locations = invoke->GetLocations(); |
| 2135 | Location base_loc = locations->InAt(1); |
| 2136 | CpuRegister base = base_loc.AsRegister<CpuRegister>(); |
| 2137 | Location offset_loc = locations->InAt(2); |
| 2138 | CpuRegister offset = offset_loc.AsRegister<CpuRegister>(); |
| 2139 | Location output_loc = locations->Out(); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2140 | CpuRegister output = output_loc.AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2141 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2142 | switch (type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2143 | case DataType::Type::kInt32: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2144 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2145 | break; |
| 2146 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2147 | case DataType::Type::kReference: { |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2148 | if (kEmitCompilerReadBarrier) { |
| 2149 | if (kUseBakerReadBarrier) { |
Sang, Chunlei | 0fcd2b8 | 2016-04-05 17:12:59 +0800 | [diff] [blame] | 2150 | Address src(base, offset, ScaleFactor::TIMES_1, 0); |
| 2151 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2152 | invoke, output_loc, base, src, /* needs_null_check */ false); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2153 | } else { |
| 2154 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 2155 | codegen->GenerateReadBarrierSlow( |
| 2156 | invoke, output_loc, output_loc, base_loc, 0U, offset_loc); |
| 2157 | } |
| 2158 | } else { |
| 2159 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 2160 | __ MaybeUnpoisonHeapReference(output); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2161 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2162 | break; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2163 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2164 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2165 | case DataType::Type::kInt64: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2166 | __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2167 | break; |
| 2168 | |
| 2169 | default: |
| 2170 | LOG(FATAL) << "Unsupported op size " << type; |
| 2171 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2175 | static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2176 | bool can_call = kEmitCompilerReadBarrier && |
| 2177 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject || |
| 2178 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2179 | LocationSummary* locations = |
| 2180 | new (allocator) LocationSummary(invoke, |
| 2181 | can_call |
| 2182 | ? LocationSummary::kCallOnSlowPath |
| 2183 | : LocationSummary::kNoCall, |
| 2184 | kIntrinsified); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 2185 | if (can_call && kUseBakerReadBarrier) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 2186 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 2187 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2188 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2189 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2190 | locations->SetInAt(2, Location::RequiresRegister()); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2191 | locations->SetOut(Location::RequiresRegister(), |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2192 | (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2196 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2197 | } |
| 2198 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2199 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2200 | } |
| 2201 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2202 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2203 | } |
| 2204 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2205 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2206 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2207 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2208 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2209 | } |
| 2210 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2211 | CreateIntIntIntToIntLocations(allocator_, invoke); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2214 | |
| 2215 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2216 | GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2217 | } |
| 2218 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2219 | GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2220 | } |
| 2221 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2222 | GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2223 | } |
| 2224 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2225 | GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2226 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2227 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2228 | GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2229 | } |
| 2230 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2231 | GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2232 | } |
| 2233 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2234 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2235 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2236 | DataType::Type type, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2237 | HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2238 | LocationSummary* locations = |
| 2239 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2240 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2241 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2242 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2243 | locations->SetInAt(3, Location::RequiresRegister()); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2244 | if (type == DataType::Type::kReference) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2245 | // Need temp registers for card-marking. |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2246 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2247 | locations->AddTemp(Location::RequiresRegister()); |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2252 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2253 | } |
| 2254 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2255 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2256 | } |
| 2257 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2258 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2259 | } |
| 2260 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2261 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2262 | } |
| 2263 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2264 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2265 | } |
| 2266 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2267 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2268 | } |
| 2269 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2270 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2271 | } |
| 2272 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2273 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2274 | } |
| 2275 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2276 | CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 2280 | // memory model. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2281 | static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2282 | CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2283 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2284 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2285 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2286 | CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>(); |
| 2287 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2288 | if (type == DataType::Type::kInt64) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2289 | __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2290 | } else if (kPoisonHeapReferences && type == DataType::Type::kReference) { |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2291 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2292 | __ movl(temp, value); |
| 2293 | __ PoisonHeapReference(temp); |
| 2294 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2295 | } else { |
| 2296 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 2297 | } |
| 2298 | |
| 2299 | if (is_volatile) { |
Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 2300 | codegen->MemoryFence(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2303 | if (type == DataType::Type::kReference) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2304 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2305 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 2306 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 2307 | base, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2308 | value, |
| 2309 | value_can_be_null); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2314 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2315 | } |
| 2316 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2317 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2318 | } |
| 2319 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2320 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2321 | } |
| 2322 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2323 | GenUnsafePut( |
| 2324 | invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2325 | } |
| 2326 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2327 | GenUnsafePut( |
| 2328 | invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2329 | } |
| 2330 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2331 | GenUnsafePut( |
| 2332 | invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2333 | } |
| 2334 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2335 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2336 | } |
| 2337 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2338 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2339 | } |
| 2340 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2341 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2342 | } |
| 2343 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2344 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2345 | DataType::Type type, |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2346 | HInvoke* invoke) { |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2347 | bool can_call = kEmitCompilerReadBarrier && |
| 2348 | kUseBakerReadBarrier && |
| 2349 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2350 | LocationSummary* locations = |
| 2351 | new (allocator) LocationSummary(invoke, |
| 2352 | can_call |
| 2353 | ? LocationSummary::kCallOnSlowPath |
| 2354 | : LocationSummary::kNoCall, |
| 2355 | kIntrinsified); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2356 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 2357 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2358 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2359 | // expected value must be in EAX/RAX. |
| 2360 | locations->SetInAt(3, Location::RegisterLocation(RAX)); |
| 2361 | locations->SetInAt(4, Location::RequiresRegister()); |
| 2362 | |
| 2363 | locations->SetOut(Location::RequiresRegister()); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2364 | if (type == DataType::Type::kReference) { |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2365 | // Need temporary registers for card-marking, and possibly for |
| 2366 | // (Baker) read barrier. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2367 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2368 | locations->AddTemp(Location::RequiresRegister()); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2373 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2377 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2381 | // The only read barrier implementation supporting the |
| 2382 | // UnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2383 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2384 | return; |
| 2385 | } |
| 2386 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2387 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2390 | static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2391 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2392 | LocationSummary* locations = invoke->GetLocations(); |
| 2393 | |
| 2394 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2395 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2396 | CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>(); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2397 | // Ensure `expected` is in RAX (required by the CMPXCHG instruction). |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2398 | DCHECK_EQ(expected.AsRegister(), RAX); |
| 2399 | CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>(); |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2400 | Location out_loc = locations->Out(); |
| 2401 | CpuRegister out = out_loc.AsRegister<CpuRegister>(); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2402 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2403 | if (type == DataType::Type::kReference) { |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2404 | // The only read barrier implementation supporting the |
| 2405 | // UnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2406 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 2407 | |
| 2408 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2409 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 2410 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2411 | // Mark card for object assuming new value is stored. |
| 2412 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2413 | codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null); |
| 2414 | |
| 2415 | // The address of the field within the holding object. |
| 2416 | Address field_addr(base, offset, ScaleFactor::TIMES_1, 0); |
| 2417 | |
| 2418 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 2419 | // Need to make sure the reference stored in the field is a to-space |
| 2420 | // one before attempting the CAS or the CAS could fail incorrectly. |
| 2421 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
| 2422 | invoke, |
| 2423 | out_loc, // Unused, used only as a "temporary" within the read barrier. |
| 2424 | base, |
| 2425 | field_addr, |
| 2426 | /* needs_null_check */ false, |
| 2427 | /* always_update_field */ true, |
| 2428 | &temp1, |
| 2429 | &temp2); |
| 2430 | } |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2431 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2432 | bool base_equals_value = (base.AsRegister() == value.AsRegister()); |
| 2433 | Register value_reg = value.AsRegister(); |
| 2434 | if (kPoisonHeapReferences) { |
| 2435 | if (base_equals_value) { |
| 2436 | // If `base` and `value` are the same register location, move |
| 2437 | // `value_reg` to a temporary register. This way, poisoning |
| 2438 | // `value_reg` won't invalidate `base`. |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2439 | value_reg = temp1.AsRegister(); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2440 | __ movl(CpuRegister(value_reg), base); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2441 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2442 | |
| 2443 | // Check that the register allocator did not assign the location |
| 2444 | // of `expected` (RAX) to `value` nor to `base`, so that heap |
| 2445 | // poisoning (when enabled) works as intended below. |
| 2446 | // - If `value` were equal to `expected`, both references would |
| 2447 | // be poisoned twice, meaning they would not be poisoned at |
| 2448 | // all, as heap poisoning uses address negation. |
| 2449 | // - If `base` were equal to `expected`, poisoning `expected` |
| 2450 | // would invalidate `base`. |
| 2451 | DCHECK_NE(value_reg, expected.AsRegister()); |
| 2452 | DCHECK_NE(base.AsRegister(), expected.AsRegister()); |
| 2453 | |
| 2454 | __ PoisonHeapReference(expected); |
| 2455 | __ PoisonHeapReference(CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2456 | } |
| 2457 | |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2458 | __ LockCmpxchgl(field_addr, CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2459 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2460 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2461 | // scheduling barriers at this time. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2462 | |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2463 | // Convert ZF into the Boolean result. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2464 | __ setcc(kZero, out); |
| 2465 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2466 | |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2467 | // If heap poisoning is enabled, we need to unpoison the values |
| 2468 | // that were poisoned earlier. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2469 | if (kPoisonHeapReferences) { |
| 2470 | if (base_equals_value) { |
| 2471 | // `value_reg` has been moved to a temporary register, no need |
| 2472 | // to unpoison it. |
| 2473 | } else { |
| 2474 | // Ensure `value` is different from `out`, so that unpoisoning |
| 2475 | // the former does not invalidate the latter. |
| 2476 | DCHECK_NE(value_reg, out.AsRegister()); |
| 2477 | __ UnpoisonHeapReference(CpuRegister(value_reg)); |
| 2478 | } |
| 2479 | // Ensure `expected` is different from `out`, so that unpoisoning |
| 2480 | // the former does not invalidate the latter. |
| 2481 | DCHECK_NE(expected.AsRegister(), out.AsRegister()); |
| 2482 | __ UnpoisonHeapReference(expected); |
| 2483 | } |
| 2484 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2485 | if (type == DataType::Type::kInt32) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2486 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2487 | } else if (type == DataType::Type::kInt64) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2488 | __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value); |
| 2489 | } else { |
| 2490 | LOG(FATAL) << "Unexpected CAS type " << type; |
| 2491 | } |
| 2492 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2493 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2494 | // scheduling barriers at this time. |
| 2495 | |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2496 | // Convert ZF into the Boolean result. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2497 | __ setcc(kZero, out); |
| 2498 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2499 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2503 | GenCAS(DataType::Type::kInt32, invoke, codegen_); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2507 | GenCAS(DataType::Type::kInt64, invoke, codegen_); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2511 | // The only read barrier implementation supporting the |
| 2512 | // UnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2513 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2514 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2515 | GenCAS(DataType::Type::kReference, invoke, codegen_); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2519 | LocationSummary* locations = |
| 2520 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2521 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2522 | locations->SetOut(Location::SameAsFirstInput()); |
| 2523 | locations->AddTemp(Location::RequiresRegister()); |
| 2524 | } |
| 2525 | |
| 2526 | static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask, |
| 2527 | X86_64Assembler* assembler) { |
| 2528 | Immediate imm_shift(shift); |
| 2529 | Immediate imm_mask(mask); |
| 2530 | __ movl(temp, reg); |
| 2531 | __ shrl(reg, imm_shift); |
| 2532 | __ andl(temp, imm_mask); |
| 2533 | __ andl(reg, imm_mask); |
| 2534 | __ shll(temp, imm_shift); |
| 2535 | __ orl(reg, temp); |
| 2536 | } |
| 2537 | |
| 2538 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2539 | X86_64Assembler* assembler = GetAssembler(); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2540 | LocationSummary* locations = invoke->GetLocations(); |
| 2541 | |
| 2542 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2543 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2544 | |
| 2545 | /* |
| 2546 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2547 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 2548 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2549 | * x = bswap x |
| 2550 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 2551 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 2552 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 2553 | */ |
| 2554 | __ bswapl(reg); |
| 2555 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 2556 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 2557 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 2558 | } |
| 2559 | |
| 2560 | void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2561 | LocationSummary* locations = |
| 2562 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2563 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2564 | locations->SetOut(Location::SameAsFirstInput()); |
| 2565 | locations->AddTemp(Location::RequiresRegister()); |
| 2566 | locations->AddTemp(Location::RequiresRegister()); |
| 2567 | } |
| 2568 | |
| 2569 | static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask, |
| 2570 | int32_t shift, int64_t mask, X86_64Assembler* assembler) { |
| 2571 | Immediate imm_shift(shift); |
| 2572 | __ movq(temp_mask, Immediate(mask)); |
| 2573 | __ movq(temp, reg); |
| 2574 | __ shrq(reg, imm_shift); |
| 2575 | __ andq(temp, temp_mask); |
| 2576 | __ andq(reg, temp_mask); |
| 2577 | __ shlq(temp, imm_shift); |
| 2578 | __ orq(reg, temp); |
| 2579 | } |
| 2580 | |
| 2581 | void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2582 | X86_64Assembler* assembler = GetAssembler(); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2583 | LocationSummary* locations = invoke->GetLocations(); |
| 2584 | |
| 2585 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2586 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2587 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 2588 | |
| 2589 | /* |
| 2590 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2591 | * swapping bits to reverse bits in a long number x. Using bswap to save instructions |
| 2592 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2593 | * x = bswap x |
| 2594 | * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555; |
| 2595 | * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333; |
| 2596 | * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F; |
| 2597 | */ |
| 2598 | __ bswapq(reg); |
| 2599 | SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler); |
| 2600 | SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler); |
| 2601 | SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler); |
| 2602 | } |
| 2603 | |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2604 | static void CreateBitCountLocations( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2605 | ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) { |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2606 | if (!codegen->GetInstructionSetFeatures().HasPopCnt()) { |
| 2607 | // Do nothing if there is no popcnt support. This results in generating |
| 2608 | // a call for the intrinsic rather than direct code. |
| 2609 | return; |
| 2610 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2611 | LocationSummary* locations = |
| 2612 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2613 | locations->SetInAt(0, Location::Any()); |
| 2614 | locations->SetOut(Location::RequiresRegister()); |
| 2615 | } |
| 2616 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2617 | static void GenBitCount(X86_64Assembler* assembler, |
| 2618 | CodeGeneratorX86_64* codegen, |
| 2619 | HInvoke* invoke, |
| 2620 | bool is_long) { |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2621 | LocationSummary* locations = invoke->GetLocations(); |
| 2622 | Location src = locations->InAt(0); |
| 2623 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2624 | |
| 2625 | if (invoke->InputAt(0)->IsConstant()) { |
| 2626 | // Evaluate this at compile time. |
| 2627 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2628 | int32_t result = is_long |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2629 | ? POPCOUNT(static_cast<uint64_t>(value)) |
| 2630 | : POPCOUNT(static_cast<uint32_t>(value)); |
Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2631 | codegen->Load32BitValue(out, result); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2632 | return; |
| 2633 | } |
| 2634 | |
| 2635 | if (src.IsRegister()) { |
| 2636 | if (is_long) { |
| 2637 | __ popcntq(out, src.AsRegister<CpuRegister>()); |
| 2638 | } else { |
| 2639 | __ popcntl(out, src.AsRegister<CpuRegister>()); |
| 2640 | } |
| 2641 | } else if (is_long) { |
| 2642 | DCHECK(src.IsDoubleStackSlot()); |
| 2643 | __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2644 | } else { |
| 2645 | DCHECK(src.IsStackSlot()); |
| 2646 | __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2651 | CreateBitCountLocations(allocator_, codegen_, invoke); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2655 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2659 | CreateBitCountLocations(allocator_, codegen_, invoke); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2663 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true); |
| 2664 | } |
| 2665 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2666 | static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) { |
| 2667 | LocationSummary* locations = |
| 2668 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2669 | locations->SetInAt(0, Location::Any()); |
| 2670 | locations->SetOut(Location::RequiresRegister()); |
| 2671 | locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL |
| 2672 | : Location::RequiresRegister()); // any will do |
| 2673 | } |
| 2674 | |
| 2675 | static void GenOneBit(X86_64Assembler* assembler, |
| 2676 | CodeGeneratorX86_64* codegen, |
| 2677 | HInvoke* invoke, |
| 2678 | bool is_high, bool is_long) { |
| 2679 | LocationSummary* locations = invoke->GetLocations(); |
| 2680 | Location src = locations->InAt(0); |
| 2681 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2682 | |
| 2683 | if (invoke->InputAt(0)->IsConstant()) { |
| 2684 | // Evaluate this at compile time. |
| 2685 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2686 | if (value == 0) { |
| 2687 | __ xorl(out, out); // Clears upper bits too. |
| 2688 | return; |
| 2689 | } |
| 2690 | // Nonzero value. |
| 2691 | if (is_high) { |
| 2692 | value = is_long ? 63 - CLZ(static_cast<uint64_t>(value)) |
| 2693 | : 31 - CLZ(static_cast<uint32_t>(value)); |
| 2694 | } else { |
| 2695 | value = is_long ? CTZ(static_cast<uint64_t>(value)) |
| 2696 | : CTZ(static_cast<uint32_t>(value)); |
| 2697 | } |
| 2698 | if (is_long) { |
Pavel Vyssotski | 7f7f6da | 2016-06-22 12:36:10 +0600 | [diff] [blame] | 2699 | codegen->Load64BitValue(out, 1ULL << value); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2700 | } else { |
| 2701 | codegen->Load32BitValue(out, 1 << value); |
| 2702 | } |
| 2703 | return; |
| 2704 | } |
| 2705 | |
| 2706 | // Handle the non-constant cases. |
| 2707 | CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2708 | if (is_high) { |
| 2709 | // Use architectural support: basically 1 << bsr. |
| 2710 | if (src.IsRegister()) { |
| 2711 | if (is_long) { |
| 2712 | __ bsrq(tmp, src.AsRegister<CpuRegister>()); |
| 2713 | } else { |
| 2714 | __ bsrl(tmp, src.AsRegister<CpuRegister>()); |
| 2715 | } |
| 2716 | } else if (is_long) { |
| 2717 | DCHECK(src.IsDoubleStackSlot()); |
| 2718 | __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2719 | } else { |
| 2720 | DCHECK(src.IsStackSlot()); |
| 2721 | __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2722 | } |
| 2723 | // BSR sets ZF if the input was zero. |
| 2724 | NearLabel is_zero, done; |
| 2725 | __ j(kEqual, &is_zero); |
| 2726 | __ movl(out, Immediate(1)); // Clears upper bits too. |
| 2727 | if (is_long) { |
| 2728 | __ shlq(out, tmp); |
| 2729 | } else { |
| 2730 | __ shll(out, tmp); |
| 2731 | } |
| 2732 | __ jmp(&done); |
| 2733 | __ Bind(&is_zero); |
| 2734 | __ xorl(out, out); // Clears upper bits too. |
| 2735 | __ Bind(&done); |
| 2736 | } else { |
| 2737 | // Copy input into temporary. |
| 2738 | if (src.IsRegister()) { |
| 2739 | if (is_long) { |
| 2740 | __ movq(tmp, src.AsRegister<CpuRegister>()); |
| 2741 | } else { |
| 2742 | __ movl(tmp, src.AsRegister<CpuRegister>()); |
| 2743 | } |
| 2744 | } else if (is_long) { |
| 2745 | DCHECK(src.IsDoubleStackSlot()); |
| 2746 | __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2747 | } else { |
| 2748 | DCHECK(src.IsStackSlot()); |
| 2749 | __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2750 | } |
| 2751 | // Do the bit twiddling: basically tmp & -tmp; |
| 2752 | if (is_long) { |
| 2753 | __ movq(out, tmp); |
| 2754 | __ negq(tmp); |
| 2755 | __ andq(out, tmp); |
| 2756 | } else { |
| 2757 | __ movl(out, tmp); |
| 2758 | __ negl(tmp); |
| 2759 | __ andl(out, tmp); |
| 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2765 | CreateOneBitLocations(allocator_, invoke, /* is_high */ true); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) { |
| 2769 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false); |
| 2770 | } |
| 2771 | |
| 2772 | void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2773 | CreateOneBitLocations(allocator_, invoke, /* is_high */ true); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) { |
| 2777 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true); |
| 2778 | } |
| 2779 | |
| 2780 | void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2781 | CreateOneBitLocations(allocator_, invoke, /* is_high */ false); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 2785 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false); |
| 2786 | } |
| 2787 | |
| 2788 | void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2789 | CreateOneBitLocations(allocator_, invoke, /* is_high */ false); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) { |
| 2793 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2794 | } |
| 2795 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2796 | static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 2797 | LocationSummary* locations = |
| 2798 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2799 | locations->SetInAt(0, Location::Any()); |
| 2800 | locations->SetOut(Location::RequiresRegister()); |
| 2801 | } |
| 2802 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2803 | static void GenLeadingZeros(X86_64Assembler* assembler, |
| 2804 | CodeGeneratorX86_64* codegen, |
| 2805 | HInvoke* invoke, bool is_long) { |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2806 | LocationSummary* locations = invoke->GetLocations(); |
| 2807 | Location src = locations->InAt(0); |
| 2808 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2809 | |
| 2810 | int zero_value_result = is_long ? 64 : 32; |
| 2811 | if (invoke->InputAt(0)->IsConstant()) { |
| 2812 | // Evaluate this at compile time. |
| 2813 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2814 | if (value == 0) { |
| 2815 | value = zero_value_result; |
| 2816 | } else { |
| 2817 | value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value)); |
| 2818 | } |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2819 | codegen->Load32BitValue(out, value); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2820 | return; |
| 2821 | } |
| 2822 | |
| 2823 | // Handle the non-constant cases. |
| 2824 | if (src.IsRegister()) { |
| 2825 | if (is_long) { |
| 2826 | __ bsrq(out, src.AsRegister<CpuRegister>()); |
| 2827 | } else { |
| 2828 | __ bsrl(out, src.AsRegister<CpuRegister>()); |
| 2829 | } |
| 2830 | } else if (is_long) { |
| 2831 | DCHECK(src.IsDoubleStackSlot()); |
| 2832 | __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2833 | } else { |
| 2834 | DCHECK(src.IsStackSlot()); |
| 2835 | __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2836 | } |
| 2837 | |
| 2838 | // BSR sets ZF if the input was zero, and the output is undefined. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2839 | NearLabel is_zero, done; |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2840 | __ j(kEqual, &is_zero); |
| 2841 | |
| 2842 | // Correct the result from BSR to get the CLZ result. |
| 2843 | __ xorl(out, Immediate(zero_value_result - 1)); |
| 2844 | __ jmp(&done); |
| 2845 | |
| 2846 | // Fix the zero case with the expected result. |
| 2847 | __ Bind(&is_zero); |
| 2848 | __ movl(out, Immediate(zero_value_result)); |
| 2849 | |
| 2850 | __ Bind(&done); |
| 2851 | } |
| 2852 | |
| 2853 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2854 | CreateLeadingZeroLocations(allocator_, invoke); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2858 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2862 | CreateLeadingZeroLocations(allocator_, invoke); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2866 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2867 | } |
| 2868 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2869 | static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 2870 | LocationSummary* locations = |
| 2871 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2872 | locations->SetInAt(0, Location::Any()); |
| 2873 | locations->SetOut(Location::RequiresRegister()); |
| 2874 | } |
| 2875 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2876 | static void GenTrailingZeros(X86_64Assembler* assembler, |
| 2877 | CodeGeneratorX86_64* codegen, |
| 2878 | HInvoke* invoke, bool is_long) { |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2879 | LocationSummary* locations = invoke->GetLocations(); |
| 2880 | Location src = locations->InAt(0); |
| 2881 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2882 | |
| 2883 | int zero_value_result = is_long ? 64 : 32; |
| 2884 | if (invoke->InputAt(0)->IsConstant()) { |
| 2885 | // Evaluate this at compile time. |
| 2886 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2887 | if (value == 0) { |
| 2888 | value = zero_value_result; |
| 2889 | } else { |
| 2890 | value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value)); |
| 2891 | } |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2892 | codegen->Load32BitValue(out, value); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | // Handle the non-constant cases. |
| 2897 | if (src.IsRegister()) { |
| 2898 | if (is_long) { |
| 2899 | __ bsfq(out, src.AsRegister<CpuRegister>()); |
| 2900 | } else { |
| 2901 | __ bsfl(out, src.AsRegister<CpuRegister>()); |
| 2902 | } |
| 2903 | } else if (is_long) { |
| 2904 | DCHECK(src.IsDoubleStackSlot()); |
| 2905 | __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2906 | } else { |
| 2907 | DCHECK(src.IsStackSlot()); |
| 2908 | __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2909 | } |
| 2910 | |
| 2911 | // BSF sets ZF if the input was zero, and the output is undefined. |
| 2912 | NearLabel done; |
| 2913 | __ j(kNotEqual, &done); |
| 2914 | |
| 2915 | // Fix the zero case with the expected result. |
| 2916 | __ movl(out, Immediate(zero_value_result)); |
| 2917 | |
| 2918 | __ Bind(&done); |
| 2919 | } |
| 2920 | |
| 2921 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2922 | CreateTrailingZeroLocations(allocator_, invoke); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2926 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2930 | CreateTrailingZeroLocations(allocator_, invoke); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2934 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true); |
| 2935 | } |
| 2936 | |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2937 | void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) { |
| 2938 | InvokeRuntimeCallingConvention calling_convention; |
| 2939 | IntrinsicVisitor::ComputeIntegerValueOfLocations( |
| 2940 | invoke, |
| 2941 | codegen_, |
| 2942 | Location::RegisterLocation(RAX), |
| 2943 | Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2944 | } |
| 2945 | |
| 2946 | void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) { |
| 2947 | IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo(); |
| 2948 | LocationSummary* locations = invoke->GetLocations(); |
| 2949 | X86_64Assembler* assembler = GetAssembler(); |
| 2950 | |
| 2951 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2952 | InvokeRuntimeCallingConvention calling_convention; |
| 2953 | if (invoke->InputAt(0)->IsConstant()) { |
| 2954 | int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| 2955 | if (value >= info.low && value <= info.high) { |
| 2956 | // Just embed the j.l.Integer in the code. |
| 2957 | ScopedObjectAccess soa(Thread::Current()); |
| 2958 | mirror::Object* boxed = info.cache->Get(value + (-info.low)); |
| 2959 | DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed)); |
| 2960 | uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed)); |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2961 | __ movl(out, Immediate(static_cast<int32_t>(address))); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2962 | } else { |
| 2963 | // Allocate and initialize a new j.l.Integer. |
| 2964 | // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the |
| 2965 | // JIT object table. |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2966 | CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0)); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2967 | uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer)); |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2968 | __ movl(argument, Immediate(static_cast<int32_t>(address))); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2969 | codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc()); |
| 2970 | CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>(); |
| 2971 | __ movl(Address(out, info.value_offset), Immediate(value)); |
| 2972 | } |
| 2973 | } else { |
| 2974 | CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2975 | // Check bounds of our cache. |
| 2976 | __ leal(out, Address(in, -info.low)); |
| 2977 | __ cmpl(out, Immediate(info.high - info.low + 1)); |
| 2978 | NearLabel allocate, done; |
| 2979 | __ j(kAboveEqual, &allocate); |
| 2980 | // If the value is within the bounds, load the j.l.Integer directly from the array. |
| 2981 | uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value(); |
| 2982 | uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache)); |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2983 | if (data_offset + address <= std::numeric_limits<int32_t>::max()) { |
| 2984 | __ movl(out, Address(out, TIMES_4, data_offset + address)); |
| 2985 | } else { |
| 2986 | CpuRegister temp = CpuRegister(calling_convention.GetRegisterAt(0)); |
| 2987 | __ movl(temp, Immediate(static_cast<int32_t>(data_offset + address))); |
| 2988 | __ movl(out, Address(temp, out, TIMES_4, 0)); |
| 2989 | } |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2990 | __ MaybeUnpoisonHeapReference(out); |
| 2991 | __ jmp(&done); |
| 2992 | __ Bind(&allocate); |
| 2993 | // Otherwise allocate and initialize a new j.l.Integer. |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2994 | CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0)); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2995 | address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer)); |
Colin Cross | 0bd9717 | 2017-03-15 16:33:27 -0700 | [diff] [blame] | 2996 | __ movl(argument, Immediate(static_cast<int32_t>(address))); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 2997 | codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc()); |
| 2998 | CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>(); |
| 2999 | __ movl(Address(out, info.value_offset), in); |
| 3000 | __ Bind(&done); |
| 3001 | } |
| 3002 | } |
| 3003 | |
Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 3004 | void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 3005 | LocationSummary* locations = |
| 3006 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 3007 | locations->SetOut(Location::RequiresRegister()); |
| 3008 | } |
| 3009 | |
| 3010 | void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) { |
| 3011 | X86_64Assembler* assembler = GetAssembler(); |
| 3012 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
| 3013 | Address address = Address::Absolute |
| 3014 | (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip */ true); |
| 3015 | NearLabel done; |
| 3016 | __ gs()->movl(out, address); |
| 3017 | __ testl(out, out); |
| 3018 | __ j(kEqual, &done); |
| 3019 | __ gs()->movl(address, Immediate(0)); |
| 3020 | codegen_->MemoryFence(); |
| 3021 | __ Bind(&done); |
| 3022 | } |
| 3023 | |
Vladimir Marko | 4ee8e29 | 2017-06-02 15:39:30 +0000 | [diff] [blame] | 3024 | UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent) |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 3025 | UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite) |
| 3026 | UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3027 | |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 3028 | UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf); |
| 3029 | UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter); |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 3030 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend); |
| 3031 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength); |
| 3032 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString); |
| 3033 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend); |
| 3034 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength); |
| 3035 | UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString); |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 3036 | |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 3037 | // 1.8. |
| 3038 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt) |
| 3039 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong) |
| 3040 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt) |
| 3041 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong) |
| 3042 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject) |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 3043 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 3044 | UNREACHABLE_INTRINSICS(X86_64) |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 3045 | |
| 3046 | #undef __ |
| 3047 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3048 | } // namespace x86_64 |
| 3049 | } // namespace art |