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