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