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 |
| 1402 | // bool is_gray = (rb_state == ReadBarrier::gray_ptr_); |
| 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. |
| 1423 | static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0"); |
| 1424 | static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1"); |
| 1425 | static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2"); |
| 1426 | constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte; |
| 1427 | constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte; |
| 1428 | constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position); |
| 1429 | |
| 1430 | // if (rb_state == ReadBarrier::gray_ptr_) |
| 1431 | // goto slow_path; |
| 1432 | // At this point, just do the "if" and make sure that flags are preserved until the branch. |
| 1433 | __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value)); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1434 | |
| 1435 | // Load fence to prevent load-load reordering. |
| 1436 | // Note that this is a no-op, thanks to the x86-64 memory model. |
| 1437 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 1438 | |
| 1439 | // Slow path used to copy array when `src` is gray. |
| 1440 | SlowPathCode* read_barrier_slow_path = |
| 1441 | new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke); |
| 1442 | codegen_->AddSlowPath(read_barrier_slow_path); |
| 1443 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 1444 | // We have done the "if" of the gray bit check above, now branch based on the flags. |
| 1445 | __ j(kNotZero, read_barrier_slow_path->GetEntryLabel()); |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1446 | |
| 1447 | // Fast-path copy. |
| 1448 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1449 | // poison/unpoison. |
| 1450 | __ Bind(&loop); |
| 1451 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1452 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1453 | __ addl(temp1, Immediate(element_size)); |
| 1454 | __ addl(temp2, Immediate(element_size)); |
| 1455 | __ cmpl(temp1, temp3); |
| 1456 | __ j(kNotEqual, &loop); |
| 1457 | |
| 1458 | __ Bind(read_barrier_slow_path->GetExitLabel()); |
| 1459 | __ Bind(&done); |
| 1460 | } else { |
| 1461 | // Non read barrier code. |
| 1462 | |
| 1463 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1464 | // poison/unpoison. |
| 1465 | NearLabel loop, done; |
| 1466 | __ cmpl(temp1, temp3); |
| 1467 | __ j(kEqual, &done); |
| 1468 | __ Bind(&loop); |
| 1469 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1470 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1471 | __ addl(temp1, Immediate(element_size)); |
| 1472 | __ addl(temp2, Immediate(element_size)); |
| 1473 | __ cmpl(temp1, temp3); |
| 1474 | __ j(kNotEqual, &loop); |
| 1475 | __ Bind(&done); |
| 1476 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1477 | |
| 1478 | // We only need one card marking on the destination array. |
| 1479 | codegen_->MarkGCCard(temp1, |
| 1480 | temp2, |
| 1481 | dest, |
| 1482 | CpuRegister(kNoRegister), |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1483 | /* value_can_be_null */ false); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1484 | |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 1485 | __ Bind(intrinsic_slow_path->GetExitLabel()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1486 | } |
| 1487 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1488 | void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1489 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 1490 | LocationSummary::kCallOnMainAndSlowPath, |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1491 | kIntrinsified); |
| 1492 | InvokeRuntimeCallingConvention calling_convention; |
| 1493 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1494 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1495 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1496 | } |
| 1497 | |
| 1498 | void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1499 | X86_64Assembler* assembler = GetAssembler(); |
| 1500 | LocationSummary* locations = invoke->GetLocations(); |
| 1501 | |
Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 1502 | // Note that the null check must have been done earlier. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1503 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1504 | |
| 1505 | CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1506 | __ testl(argument, argument); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1507 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1508 | codegen_->AddSlowPath(slow_path); |
| 1509 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1510 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1511 | codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1512 | __ Bind(slow_path->GetExitLabel()); |
| 1513 | } |
| 1514 | |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1515 | void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1516 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1517 | LocationSummary::kNoCall, |
| 1518 | kIntrinsified); |
| 1519 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1520 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1521 | |
| 1522 | // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction. |
| 1523 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1524 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1525 | |
| 1526 | // Set output, RSI needed for repe_cmpsq instruction anyways. |
| 1527 | locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap); |
| 1528 | } |
| 1529 | |
| 1530 | void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1531 | X86_64Assembler* assembler = GetAssembler(); |
| 1532 | LocationSummary* locations = invoke->GetLocations(); |
| 1533 | |
| 1534 | CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1535 | CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1536 | CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1537 | CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1538 | CpuRegister rsi = locations->Out().AsRegister<CpuRegister>(); |
| 1539 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1540 | NearLabel end, return_true, return_false; |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1541 | |
| 1542 | // Get offsets of count, value, and class fields within a string object. |
| 1543 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1544 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1545 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1546 | |
| 1547 | // Note that the null check must have been done earlier. |
| 1548 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1549 | |
Vladimir Marko | 53b5200 | 2016-05-24 19:30:45 +0100 | [diff] [blame] | 1550 | StringEqualsOptimizations optimizations(invoke); |
| 1551 | if (!optimizations.GetArgumentNotNull()) { |
| 1552 | // Check if input is null, return false if it is. |
| 1553 | __ testl(arg, arg); |
| 1554 | __ j(kEqual, &return_false); |
| 1555 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1556 | |
Vladimir Marko | 53b5200 | 2016-05-24 19:30:45 +0100 | [diff] [blame] | 1557 | if (!optimizations.GetArgumentIsString()) { |
| 1558 | // Instanceof check for the argument by comparing class fields. |
| 1559 | // All string objects must have the same type since String cannot be subclassed. |
| 1560 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1561 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 1562 | __ movl(rcx, Address(str, class_offset)); |
| 1563 | __ cmpl(rcx, Address(arg, class_offset)); |
| 1564 | __ j(kNotEqual, &return_false); |
| 1565 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1566 | |
| 1567 | // Reference equality check, return true if same reference. |
| 1568 | __ cmpl(str, arg); |
| 1569 | __ j(kEqual, &return_true); |
| 1570 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1571 | // Load length and compression flag of receiver string. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1572 | __ movl(rcx, Address(str, count_offset)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1573 | // Check if lengths and compressiond flags are equal, return false if they're not. |
| 1574 | // Two identical strings will always have same compression style since |
| 1575 | // compression style is decided on alloc. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1576 | __ cmpl(rcx, Address(arg, count_offset)); |
| 1577 | __ j(kNotEqual, &return_false); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (mirror::kUseStringCompression) { |
| 1580 | NearLabel string_uncompressed; |
| 1581 | // Both string are compressed. |
| 1582 | __ cmpl(rcx, Immediate(0)); |
| 1583 | __ j(kGreaterEqual, &string_uncompressed); |
| 1584 | // Divide string length by 2, rounding up, and continue as if uncompressed. |
| 1585 | // Merge clearing the compression flag with +1 for rounding. |
| 1586 | __ addl(rcx, Immediate(static_cast<int32_t>(0x80000001))); |
| 1587 | __ shrl(rcx, Immediate(1)); |
| 1588 | __ Bind(&string_uncompressed); |
| 1589 | } |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1590 | // Return true if both strings are empty. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1591 | __ jrcxz(&return_true); |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1592 | // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction. |
| 1593 | __ leal(rsi, Address(str, value_offset)); |
| 1594 | __ leal(rdi, Address(arg, value_offset)); |
| 1595 | |
| 1596 | // Divide string length by 4 and adjust for lengths not divisible by 4. |
| 1597 | __ addl(rcx, Immediate(3)); |
| 1598 | __ shrl(rcx, Immediate(2)); |
| 1599 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1600 | // Assertions that must hold in order to compare strings 4 characters (uncompressed) |
| 1601 | // or 8 characters (compressed) at a time. |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1602 | DCHECK_ALIGNED(value_offset, 8); |
| 1603 | static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded"); |
| 1604 | |
| 1605 | // Loop to compare strings four characters at a time starting at the beginning of the string. |
| 1606 | __ repe_cmpsq(); |
| 1607 | // If strings are not equal, zero flag will be cleared. |
| 1608 | __ j(kNotEqual, &return_false); |
| 1609 | |
| 1610 | // Return true and exit the function. |
| 1611 | // If loop does not result in returning false, we return true. |
| 1612 | __ Bind(&return_true); |
| 1613 | __ movl(rsi, Immediate(1)); |
| 1614 | __ jmp(&end); |
| 1615 | |
| 1616 | // Return false and exit the function. |
| 1617 | __ Bind(&return_false); |
| 1618 | __ xorl(rsi, rsi); |
| 1619 | __ Bind(&end); |
| 1620 | } |
| 1621 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1622 | static void CreateStringIndexOfLocations(HInvoke* invoke, |
| 1623 | ArenaAllocator* allocator, |
| 1624 | bool start_at_zero) { |
| 1625 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1626 | LocationSummary::kCallOnSlowPath, |
| 1627 | kIntrinsified); |
| 1628 | // The data needs to be in RDI for scasw. So request that the string is there, anyways. |
| 1629 | locations->SetInAt(0, Location::RegisterLocation(RDI)); |
| 1630 | // If we look for a constant char, we'll still have to copy it into RAX. So just request the |
| 1631 | // allocator to do that, anyways. We can still do the constant check by checking the parameter |
| 1632 | // of the instruction explicitly. |
| 1633 | // Note: This works as we don't clobber RAX anywhere. |
| 1634 | locations->SetInAt(1, Location::RegisterLocation(RAX)); |
| 1635 | if (!start_at_zero) { |
| 1636 | locations->SetInAt(2, Location::RequiresRegister()); // The starting index. |
| 1637 | } |
| 1638 | // As we clobber RDI during execution anyways, also use it as the output. |
| 1639 | locations->SetOut(Location::SameAsFirstInput()); |
| 1640 | |
| 1641 | // repne scasw uses RCX as the counter. |
| 1642 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1643 | // Need another temporary to be able to compute the result. |
| 1644 | locations->AddTemp(Location::RequiresRegister()); |
| 1645 | } |
| 1646 | |
| 1647 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 1648 | X86_64Assembler* assembler, |
| 1649 | CodeGeneratorX86_64* codegen, |
| 1650 | ArenaAllocator* allocator, |
| 1651 | bool start_at_zero) { |
| 1652 | LocationSummary* locations = invoke->GetLocations(); |
| 1653 | |
| 1654 | // Note that the null check must have been done earlier. |
| 1655 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1656 | |
| 1657 | CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1658 | CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1659 | CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1660 | CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1661 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1662 | |
| 1663 | // Check our assumptions for registers. |
| 1664 | DCHECK_EQ(string_obj.AsRegister(), RDI); |
| 1665 | DCHECK_EQ(search_value.AsRegister(), RAX); |
| 1666 | DCHECK_EQ(counter.AsRegister(), RCX); |
| 1667 | DCHECK_EQ(out.AsRegister(), RDI); |
| 1668 | |
| 1669 | // 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] | 1670 | // 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] | 1671 | SlowPathCode* slow_path = nullptr; |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 1672 | HInstruction* code_point = invoke->InputAt(1); |
| 1673 | if (code_point->IsIntConstant()) { |
Vladimir Marko | da05108 | 2016-05-17 16:10:20 +0100 | [diff] [blame] | 1674 | if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1675 | std::numeric_limits<uint16_t>::max()) { |
| 1676 | // Always needs the slow-path. We could directly dispatch to it, but this case should be |
| 1677 | // rare, so for simplicity just put the full slow-path down and branch unconditionally. |
| 1678 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1679 | codegen->AddSlowPath(slow_path); |
| 1680 | __ jmp(slow_path->GetEntryLabel()); |
| 1681 | __ Bind(slow_path->GetExitLabel()); |
| 1682 | return; |
| 1683 | } |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 1684 | } else if (code_point->GetType() != Primitive::kPrimChar) { |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1685 | __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max())); |
| 1686 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1687 | codegen->AddSlowPath(slow_path); |
| 1688 | __ j(kAbove, slow_path->GetEntryLabel()); |
| 1689 | } |
| 1690 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1691 | // From here down, we know that we are looking for a char that fits in |
| 1692 | // 16 bits (uncompressed) or 8 bits (compressed). |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1693 | // Location of reference to data array within the String object. |
| 1694 | int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1695 | // Location of count within the String object. |
| 1696 | int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1697 | |
| 1698 | // Load string length, i.e., the count field of the string. |
| 1699 | __ movl(string_length, Address(string_obj, count_offset)); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1700 | if (mirror::kUseStringCompression) { |
| 1701 | // Use TMP to keep string_length_flagged. |
| 1702 | __ movl(CpuRegister(TMP), string_length); |
| 1703 | // Mask out first bit used as compression flag. |
| 1704 | __ andl(string_length, Immediate(INT32_MAX)); |
| 1705 | } |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1706 | |
| 1707 | // Do a length check. |
| 1708 | // TODO: Support jecxz. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1709 | NearLabel not_found_label; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1710 | __ testl(string_length, string_length); |
| 1711 | __ j(kEqual, ¬_found_label); |
| 1712 | |
| 1713 | if (start_at_zero) { |
| 1714 | // Number of chars to scan is the same as the string length. |
| 1715 | __ movl(counter, string_length); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1716 | // Move to the start of the string. |
| 1717 | __ addq(string_obj, Immediate(value_offset)); |
| 1718 | } else { |
| 1719 | CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1720 | |
| 1721 | // Do a start_index check. |
| 1722 | __ cmpl(start_index, string_length); |
| 1723 | __ j(kGreaterEqual, ¬_found_label); |
| 1724 | |
| 1725 | // Ensure we have a start index >= 0; |
| 1726 | __ xorl(counter, counter); |
| 1727 | __ cmpl(start_index, Immediate(0)); |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1728 | __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough. |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1729 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1730 | if (mirror::kUseStringCompression) { |
| 1731 | NearLabel modify_counter, offset_uncompressed_label; |
| 1732 | __ cmpl(CpuRegister(TMP), Immediate(0)); |
| 1733 | __ j(kGreaterEqual, &offset_uncompressed_label); |
| 1734 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset)); |
| 1735 | __ jmp(&modify_counter); |
| 1736 | // Move to the start of the string: string_obj + value_offset + 2 * start_index. |
| 1737 | __ Bind(&offset_uncompressed_label); |
| 1738 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1739 | __ Bind(&modify_counter); |
| 1740 | } else { |
| 1741 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1742 | } |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1743 | // Now update ecx, the work counter: it's gonna be string.length - start_index. |
| 1744 | __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit. |
| 1745 | __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0)); |
| 1746 | } |
| 1747 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1748 | if (mirror::kUseStringCompression) { |
| 1749 | NearLabel uncompressed_string_comparison; |
| 1750 | NearLabel comparison_done; |
| 1751 | __ cmpl(CpuRegister(TMP), Immediate(0)); |
| 1752 | __ j(kGreater, &uncompressed_string_comparison); |
| 1753 | // Check if RAX (search_value) is ASCII. |
| 1754 | __ cmpl(search_value, Immediate(127)); |
| 1755 | __ j(kGreater, ¬_found_label); |
| 1756 | // Comparing byte-per-byte. |
| 1757 | __ repne_scasb(); |
| 1758 | __ jmp(&comparison_done); |
| 1759 | // Everything is set up for repne scasw: |
| 1760 | // * Comparison address in RDI. |
| 1761 | // * Counter in ECX. |
| 1762 | __ Bind(&uncompressed_string_comparison); |
| 1763 | __ repne_scasw(); |
| 1764 | __ Bind(&comparison_done); |
| 1765 | } else { |
| 1766 | __ repne_scasw(); |
| 1767 | } |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1768 | // Did we find a match? |
| 1769 | __ j(kNotEqual, ¬_found_label); |
| 1770 | |
| 1771 | // Yes, we matched. Compute the index of the result. |
| 1772 | __ subl(string_length, counter); |
| 1773 | __ leal(out, Address(string_length, -1)); |
| 1774 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1775 | NearLabel done; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1776 | __ jmp(&done); |
| 1777 | |
| 1778 | // Failed to match; return -1. |
| 1779 | __ Bind(¬_found_label); |
| 1780 | __ movl(out, Immediate(-1)); |
| 1781 | |
| 1782 | // And join up at the end. |
| 1783 | __ Bind(&done); |
| 1784 | if (slow_path != nullptr) { |
| 1785 | __ Bind(slow_path->GetExitLabel()); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1790 | CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1794 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1798 | CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1802 | GenerateStringIndexOf( |
| 1803 | invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1806 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1807 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 1808 | LocationSummary::kCallOnMainAndSlowPath, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1809 | kIntrinsified); |
| 1810 | InvokeRuntimeCallingConvention calling_convention; |
| 1811 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1812 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1813 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1814 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 1815 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1816 | } |
| 1817 | |
| 1818 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1819 | X86_64Assembler* assembler = GetAssembler(); |
| 1820 | LocationSummary* locations = invoke->GetLocations(); |
| 1821 | |
| 1822 | CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1823 | __ testl(byte_array, byte_array); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1824 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1825 | codegen_->AddSlowPath(slow_path); |
| 1826 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1827 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1828 | codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1829 | CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1830 | __ Bind(slow_path->GetExitLabel()); |
| 1831 | } |
| 1832 | |
| 1833 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 1834 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 1835 | LocationSummary::kCallOnMainOnly, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1836 | kIntrinsified); |
| 1837 | InvokeRuntimeCallingConvention calling_convention; |
| 1838 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1839 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1840 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1841 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1842 | } |
| 1843 | |
| 1844 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 1845 | // No need to emit code checking whether `locations->InAt(2)` is a null |
| 1846 | // pointer, as callers of the native method |
| 1847 | // |
| 1848 | // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data) |
| 1849 | // |
| 1850 | // all include a null check on `data` before calling that method. |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1851 | codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1852 | CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1856 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 1857 | LocationSummary::kCallOnMainAndSlowPath, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1858 | kIntrinsified); |
| 1859 | InvokeRuntimeCallingConvention calling_convention; |
| 1860 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1861 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1862 | } |
| 1863 | |
| 1864 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1865 | X86_64Assembler* assembler = GetAssembler(); |
| 1866 | LocationSummary* locations = invoke->GetLocations(); |
| 1867 | |
| 1868 | CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1869 | __ testl(string_to_copy, string_to_copy); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1870 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1871 | codegen_->AddSlowPath(slow_path); |
| 1872 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1873 | |
Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1874 | codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc()); |
Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1875 | CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1876 | __ Bind(slow_path->GetExitLabel()); |
| 1877 | } |
| 1878 | |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1879 | void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1880 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1881 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1882 | LocationSummary::kNoCall, |
| 1883 | kIntrinsified); |
| 1884 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1885 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1886 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1887 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1888 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1889 | |
| 1890 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 1891 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 1892 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1893 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1894 | } |
| 1895 | |
| 1896 | void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1897 | X86_64Assembler* assembler = GetAssembler(); |
| 1898 | LocationSummary* locations = invoke->GetLocations(); |
| 1899 | |
| 1900 | size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1901 | // Location of data in char array buffer. |
| 1902 | const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value(); |
| 1903 | // Location of char array data in string. |
| 1904 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1905 | |
| 1906 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1907 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1908 | Location srcBegin = locations->InAt(1); |
| 1909 | int srcBegin_value = |
| 1910 | srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0; |
| 1911 | CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1912 | CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1913 | CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1914 | |
| 1915 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 1916 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1917 | DCHECK_EQ(char_size, 2u); |
| 1918 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1919 | NearLabel done; |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1920 | // Compute the number of chars (words) to move. |
| 1921 | __ movl(CpuRegister(RCX), srcEnd); |
| 1922 | if (srcBegin.IsConstant()) { |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1923 | __ subl(CpuRegister(RCX), Immediate(srcBegin_value)); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1924 | } else { |
| 1925 | DCHECK(srcBegin.IsRegister()); |
| 1926 | __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>()); |
| 1927 | } |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1928 | if (mirror::kUseStringCompression) { |
| 1929 | NearLabel copy_uncompressed, copy_loop; |
| 1930 | const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte); |
| 1931 | DCHECK_EQ(c_char_size, 1u); |
| 1932 | // Location of count in string. |
| 1933 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1934 | |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1935 | __ cmpl(Address(obj, count_offset), Immediate(0)); |
| 1936 | __ j(kGreaterEqual, ©_uncompressed); |
| 1937 | // Compute the address of the source string by adding the number of chars from |
| 1938 | // the source beginning to the value offset of a string. |
| 1939 | __ leaq(CpuRegister(RSI), |
| 1940 | CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset)); |
| 1941 | // Start the loop to copy String's value to Array of Char. |
| 1942 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1943 | |
| 1944 | __ Bind(©_loop); |
| 1945 | __ jrcxz(&done); |
| 1946 | // Use TMP as temporary (convert byte from RSI to word). |
| 1947 | // TODO: Selecting RAX as the temporary and using LODSB/STOSW. |
| 1948 | __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0)); |
| 1949 | __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP)); |
| 1950 | __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size)); |
| 1951 | __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size)); |
| 1952 | // TODO: Add support for LOOP to X86_64Assembler. |
| 1953 | __ subl(CpuRegister(RCX), Immediate(1)); |
| 1954 | __ jmp(©_loop); |
| 1955 | |
| 1956 | __ Bind(©_uncompressed); |
| 1957 | } |
| 1958 | |
| 1959 | __ leaq(CpuRegister(RSI), |
| 1960 | CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset)); |
| 1961 | // Compute the address of the destination buffer. |
| 1962 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1963 | // Do the move. |
| 1964 | __ rep_movsw(); |
jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1965 | |
| 1966 | __ Bind(&done); |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1967 | } |
| 1968 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1969 | static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 1970 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1971 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity. |
| 1972 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1973 | // to avoid a SIGBUS. |
| 1974 | switch (size) { |
| 1975 | case Primitive::kPrimByte: |
| 1976 | __ movsxb(out, Address(address, 0)); |
| 1977 | break; |
| 1978 | case Primitive::kPrimShort: |
| 1979 | __ movsxw(out, Address(address, 0)); |
| 1980 | break; |
| 1981 | case Primitive::kPrimInt: |
| 1982 | __ movl(out, Address(address, 0)); |
| 1983 | break; |
| 1984 | case Primitive::kPrimLong: |
| 1985 | __ movq(out, Address(address, 0)); |
| 1986 | break; |
| 1987 | default: |
| 1988 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 1989 | UNREACHABLE(); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1994 | CreateIntToIntLocations(arena_, invoke); |
| 1995 | } |
| 1996 | |
| 1997 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1998 | GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1999 | } |
| 2000 | |
| 2001 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 2002 | CreateIntToIntLocations(arena_, invoke); |
| 2003 | } |
| 2004 | |
| 2005 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 2006 | GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 2007 | } |
| 2008 | |
| 2009 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 2010 | CreateIntToIntLocations(arena_, invoke); |
| 2011 | } |
| 2012 | |
| 2013 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 2014 | GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 2015 | } |
| 2016 | |
| 2017 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 2018 | CreateIntToIntLocations(arena_, invoke); |
| 2019 | } |
| 2020 | |
| 2021 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 2022 | GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 2023 | } |
| 2024 | |
| 2025 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2026 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2027 | LocationSummary::kNoCall, |
| 2028 | kIntrinsified); |
| 2029 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 2030 | locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 2034 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2035 | Location value = locations->InAt(1); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2036 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 2037 | // to avoid a SIGBUS. |
| 2038 | switch (size) { |
| 2039 | case Primitive::kPrimByte: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2040 | if (value.IsConstant()) { |
| 2041 | __ movb(Address(address, 0), |
| 2042 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2043 | } else { |
| 2044 | __ movb(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2045 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2046 | break; |
| 2047 | case Primitive::kPrimShort: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2048 | if (value.IsConstant()) { |
| 2049 | __ movw(Address(address, 0), |
| 2050 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2051 | } else { |
| 2052 | __ movw(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2053 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2054 | break; |
| 2055 | case Primitive::kPrimInt: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2056 | if (value.IsConstant()) { |
| 2057 | __ movl(Address(address, 0), |
| 2058 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 2059 | } else { |
| 2060 | __ movl(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2061 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2062 | break; |
| 2063 | case Primitive::kPrimLong: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 2064 | if (value.IsConstant()) { |
| 2065 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
| 2066 | DCHECK(IsInt<32>(v)); |
| 2067 | int32_t v_32 = v; |
| 2068 | __ movq(Address(address, 0), Immediate(v_32)); |
| 2069 | } else { |
| 2070 | __ movq(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 2071 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2072 | break; |
| 2073 | default: |
| 2074 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 2075 | UNREACHABLE(); |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 2080 | CreateIntIntToVoidLocations(arena_, invoke); |
| 2081 | } |
| 2082 | |
| 2083 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 2084 | GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 2085 | } |
| 2086 | |
| 2087 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 2088 | CreateIntIntToVoidLocations(arena_, invoke); |
| 2089 | } |
| 2090 | |
| 2091 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 2092 | GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 2093 | } |
| 2094 | |
| 2095 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 2096 | CreateIntIntToVoidLocations(arena_, invoke); |
| 2097 | } |
| 2098 | |
| 2099 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 2100 | GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 2101 | } |
| 2102 | |
| 2103 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 2104 | CreateIntIntToVoidLocations(arena_, invoke); |
| 2105 | } |
| 2106 | |
| 2107 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 2108 | GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 2109 | } |
| 2110 | |
| 2111 | void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 2112 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2113 | LocationSummary::kNoCall, |
| 2114 | kIntrinsified); |
| 2115 | locations->SetOut(Location::RequiresRegister()); |
| 2116 | } |
| 2117 | |
| 2118 | void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 2119 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2120 | GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(), |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2121 | /* no_rip */ true)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2124 | static void GenUnsafeGet(HInvoke* invoke, |
| 2125 | Primitive::Type type, |
| 2126 | bool is_volatile ATTRIBUTE_UNUSED, |
| 2127 | CodeGeneratorX86_64* codegen) { |
| 2128 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 2129 | LocationSummary* locations = invoke->GetLocations(); |
| 2130 | Location base_loc = locations->InAt(1); |
| 2131 | CpuRegister base = base_loc.AsRegister<CpuRegister>(); |
| 2132 | Location offset_loc = locations->InAt(2); |
| 2133 | CpuRegister offset = offset_loc.AsRegister<CpuRegister>(); |
| 2134 | Location output_loc = locations->Out(); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2135 | CpuRegister output = output_loc.AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2136 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2137 | switch (type) { |
| 2138 | case Primitive::kPrimInt: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2139 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | |
| 2142 | case Primitive::kPrimNot: { |
| 2143 | if (kEmitCompilerReadBarrier) { |
| 2144 | if (kUseBakerReadBarrier) { |
Sang, Chunlei | 0fcd2b8 | 2016-04-05 17:12:59 +0800 | [diff] [blame] | 2145 | Address src(base, offset, ScaleFactor::TIMES_1, 0); |
| 2146 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2147 | invoke, output_loc, base, src, /* needs_null_check */ false); |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2148 | } else { |
| 2149 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 2150 | codegen->GenerateReadBarrierSlow( |
| 2151 | invoke, output_loc, output_loc, base_loc, 0U, offset_loc); |
| 2152 | } |
| 2153 | } else { |
| 2154 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 2155 | __ MaybeUnpoisonHeapReference(output); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2156 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2157 | break; |
Roland Levillain | 1e7f8db | 2015-12-15 10:54:19 +0000 | [diff] [blame] | 2158 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2159 | |
| 2160 | case Primitive::kPrimLong: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2161 | __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2162 | break; |
| 2163 | |
| 2164 | default: |
| 2165 | LOG(FATAL) << "Unsupported op size " << type; |
| 2166 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2170 | static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2171 | bool can_call = kEmitCompilerReadBarrier && |
| 2172 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject || |
| 2173 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2174 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2175 | can_call ? |
| 2176 | LocationSummary::kCallOnSlowPath : |
| 2177 | LocationSummary::kNoCall, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2178 | kIntrinsified); |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 2179 | if (can_call && kUseBakerReadBarrier) { |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 2180 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 2181 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2182 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2183 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2184 | locations->SetInAt(2, Location::RequiresRegister()); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2185 | locations->SetOut(Location::RequiresRegister(), |
| 2186 | can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2190 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2191 | } |
| 2192 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2193 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2194 | } |
| 2195 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2196 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2197 | } |
| 2198 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2199 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2200 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2201 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2202 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2203 | } |
| 2204 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 2205 | CreateIntIntIntToIntLocations(arena_, invoke); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2206 | } |
| 2207 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2208 | |
| 2209 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2210 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2211 | } |
| 2212 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2213 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2214 | } |
| 2215 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2216 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2217 | } |
| 2218 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2219 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2220 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2221 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2222 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2223 | } |
| 2224 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2225 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2228 | |
| 2229 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena, |
| 2230 | Primitive::Type type, |
| 2231 | HInvoke* invoke) { |
| 2232 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2233 | LocationSummary::kNoCall, |
| 2234 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2235 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2236 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2237 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2238 | locations->SetInAt(3, Location::RequiresRegister()); |
| 2239 | if (type == Primitive::kPrimNot) { |
| 2240 | // Need temp registers for card-marking. |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2241 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2242 | locations->AddTemp(Location::RequiresRegister()); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 2247 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2248 | } |
| 2249 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 2250 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2251 | } |
| 2252 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 2253 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2254 | } |
| 2255 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 2256 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2257 | } |
| 2258 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 2259 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2260 | } |
| 2261 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 2262 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2263 | } |
| 2264 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 2265 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2266 | } |
| 2267 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 2268 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2269 | } |
| 2270 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 2271 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2272 | } |
| 2273 | |
| 2274 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 2275 | // memory model. |
| 2276 | static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile, |
| 2277 | CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2278 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2279 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2280 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2281 | CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>(); |
| 2282 | |
| 2283 | if (type == Primitive::kPrimLong) { |
| 2284 | __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2285 | } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) { |
| 2286 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2287 | __ movl(temp, value); |
| 2288 | __ PoisonHeapReference(temp); |
| 2289 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2290 | } else { |
| 2291 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 2292 | } |
| 2293 | |
| 2294 | if (is_volatile) { |
Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 2295 | codegen->MemoryFence(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | if (type == Primitive::kPrimNot) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2299 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2300 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 2301 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 2302 | base, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2303 | value, |
| 2304 | value_can_be_null); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2309 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2310 | } |
| 2311 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2312 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2313 | } |
| 2314 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2315 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2316 | } |
| 2317 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2318 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2319 | } |
| 2320 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2321 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2322 | } |
| 2323 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2324 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2325 | } |
| 2326 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2327 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2328 | } |
| 2329 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2330 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2331 | } |
| 2332 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2333 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2336 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type, |
| 2337 | HInvoke* invoke) { |
| 2338 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2339 | LocationSummary::kNoCall, |
| 2340 | kIntrinsified); |
| 2341 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 2342 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2343 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2344 | // expected value must be in EAX/RAX. |
| 2345 | locations->SetInAt(3, Location::RegisterLocation(RAX)); |
| 2346 | locations->SetInAt(4, Location::RequiresRegister()); |
| 2347 | |
| 2348 | locations->SetOut(Location::RequiresRegister()); |
| 2349 | if (type == Primitive::kPrimNot) { |
| 2350 | // Need temp registers for card-marking. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2351 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2352 | locations->AddTemp(Location::RequiresRegister()); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 2357 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke); |
| 2358 | } |
| 2359 | |
| 2360 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 2361 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke); |
| 2362 | } |
| 2363 | |
| 2364 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2365 | // The UnsafeCASObject intrinsic is missing a read barrier, and |
| 2366 | // therefore sometimes does not work as expected (b/25883050). |
| 2367 | // Turn it off temporarily as a quick fix, until the read barrier is |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2368 | // implemented (see TODO in GenCAS). |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2369 | // |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2370 | // TODO(rpl): Implement read barrier support in GenCAS and re-enable |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2371 | // this intrinsic. |
| 2372 | if (kEmitCompilerReadBarrier) { |
| 2373 | return; |
| 2374 | } |
| 2375 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2376 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke); |
| 2377 | } |
| 2378 | |
| 2379 | static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2380 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2381 | LocationSummary* locations = invoke->GetLocations(); |
| 2382 | |
| 2383 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2384 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2385 | CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>(); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2386 | // Ensure `expected` is in RAX (required by the CMPXCHG instruction). |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2387 | DCHECK_EQ(expected.AsRegister(), RAX); |
| 2388 | CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>(); |
| 2389 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2390 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2391 | if (type == Primitive::kPrimNot) { |
| 2392 | // Mark card for object assuming new value is stored. |
| 2393 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 2394 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 2395 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 2396 | base, |
| 2397 | value, |
| 2398 | value_can_be_null); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2399 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2400 | bool base_equals_value = (base.AsRegister() == value.AsRegister()); |
| 2401 | Register value_reg = value.AsRegister(); |
| 2402 | if (kPoisonHeapReferences) { |
| 2403 | if (base_equals_value) { |
| 2404 | // If `base` and `value` are the same register location, move |
| 2405 | // `value_reg` to a temporary register. This way, poisoning |
| 2406 | // `value_reg` won't invalidate `base`. |
| 2407 | value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister(); |
| 2408 | __ movl(CpuRegister(value_reg), base); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2409 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2410 | |
| 2411 | // Check that the register allocator did not assign the location |
| 2412 | // of `expected` (RAX) to `value` nor to `base`, so that heap |
| 2413 | // poisoning (when enabled) works as intended below. |
| 2414 | // - If `value` were equal to `expected`, both references would |
| 2415 | // be poisoned twice, meaning they would not be poisoned at |
| 2416 | // all, as heap poisoning uses address negation. |
| 2417 | // - If `base` were equal to `expected`, poisoning `expected` |
| 2418 | // would invalidate `base`. |
| 2419 | DCHECK_NE(value_reg, expected.AsRegister()); |
| 2420 | DCHECK_NE(base.AsRegister(), expected.AsRegister()); |
| 2421 | |
| 2422 | __ PoisonHeapReference(expected); |
| 2423 | __ PoisonHeapReference(CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2424 | } |
| 2425 | |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2426 | // TODO: Add a read barrier for the reference stored in the object |
| 2427 | // before attempting the CAS, similar to the one in the |
| 2428 | // art::Unsafe_compareAndSwapObject JNI implementation. |
| 2429 | // |
| 2430 | // Note that this code is not (yet) used when read barriers are |
| 2431 | // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject). |
| 2432 | DCHECK(!kEmitCompilerReadBarrier); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2433 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2434 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2435 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2436 | // scheduling barriers at this time. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2437 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2438 | // Convert ZF into the boolean result. |
| 2439 | __ setcc(kZero, out); |
| 2440 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2441 | |
Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2442 | // If heap poisoning is enabled, we need to unpoison the values |
| 2443 | // that were poisoned earlier. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2444 | if (kPoisonHeapReferences) { |
| 2445 | if (base_equals_value) { |
| 2446 | // `value_reg` has been moved to a temporary register, no need |
| 2447 | // to unpoison it. |
| 2448 | } else { |
| 2449 | // Ensure `value` is different from `out`, so that unpoisoning |
| 2450 | // the former does not invalidate the latter. |
| 2451 | DCHECK_NE(value_reg, out.AsRegister()); |
| 2452 | __ UnpoisonHeapReference(CpuRegister(value_reg)); |
| 2453 | } |
| 2454 | // Ensure `expected` is different from `out`, so that unpoisoning |
| 2455 | // the former does not invalidate the latter. |
| 2456 | DCHECK_NE(expected.AsRegister(), out.AsRegister()); |
| 2457 | __ UnpoisonHeapReference(expected); |
| 2458 | } |
| 2459 | } else { |
| 2460 | if (type == Primitive::kPrimInt) { |
| 2461 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value); |
| 2462 | } else if (type == Primitive::kPrimLong) { |
| 2463 | __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value); |
| 2464 | } else { |
| 2465 | LOG(FATAL) << "Unexpected CAS type " << type; |
| 2466 | } |
| 2467 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2468 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2469 | // scheduling barriers at this time. |
| 2470 | |
| 2471 | // Convert ZF into the boolean result. |
| 2472 | __ setcc(kZero, out); |
| 2473 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2474 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 2478 | GenCAS(Primitive::kPrimInt, invoke, codegen_); |
| 2479 | } |
| 2480 | |
| 2481 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 2482 | GenCAS(Primitive::kPrimLong, invoke, codegen_); |
| 2483 | } |
| 2484 | |
| 2485 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2486 | // The UnsafeCASObject intrinsic is missing a read barrier, and |
| 2487 | // therefore sometimes does not work as expected (b/25883050). |
| 2488 | // Turn it off temporarily as a quick fix, until the read barrier is |
| 2489 | // implemented (see TODO in GenCAS). |
| 2490 | // |
| 2491 | // TODO(rpl): Implement read barrier support in GenCAS and re-enable |
| 2492 | // this intrinsic. |
| 2493 | DCHECK(!kEmitCompilerReadBarrier); |
| 2494 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2495 | GenCAS(Primitive::kPrimNot, invoke, codegen_); |
| 2496 | } |
| 2497 | |
| 2498 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) { |
| 2499 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2500 | LocationSummary::kNoCall, |
| 2501 | kIntrinsified); |
| 2502 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2503 | locations->SetOut(Location::SameAsFirstInput()); |
| 2504 | locations->AddTemp(Location::RequiresRegister()); |
| 2505 | } |
| 2506 | |
| 2507 | static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask, |
| 2508 | X86_64Assembler* assembler) { |
| 2509 | Immediate imm_shift(shift); |
| 2510 | Immediate imm_mask(mask); |
| 2511 | __ movl(temp, reg); |
| 2512 | __ shrl(reg, imm_shift); |
| 2513 | __ andl(temp, imm_mask); |
| 2514 | __ andl(reg, imm_mask); |
| 2515 | __ shll(temp, imm_shift); |
| 2516 | __ orl(reg, temp); |
| 2517 | } |
| 2518 | |
| 2519 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2520 | X86_64Assembler* assembler = GetAssembler(); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2521 | LocationSummary* locations = invoke->GetLocations(); |
| 2522 | |
| 2523 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2524 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2525 | |
| 2526 | /* |
| 2527 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2528 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 2529 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2530 | * x = bswap x |
| 2531 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 2532 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 2533 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 2534 | */ |
| 2535 | __ bswapl(reg); |
| 2536 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 2537 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 2538 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 2539 | } |
| 2540 | |
| 2541 | void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) { |
| 2542 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2543 | LocationSummary::kNoCall, |
| 2544 | kIntrinsified); |
| 2545 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2546 | locations->SetOut(Location::SameAsFirstInput()); |
| 2547 | locations->AddTemp(Location::RequiresRegister()); |
| 2548 | locations->AddTemp(Location::RequiresRegister()); |
| 2549 | } |
| 2550 | |
| 2551 | static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask, |
| 2552 | int32_t shift, int64_t mask, X86_64Assembler* assembler) { |
| 2553 | Immediate imm_shift(shift); |
| 2554 | __ movq(temp_mask, Immediate(mask)); |
| 2555 | __ movq(temp, reg); |
| 2556 | __ shrq(reg, imm_shift); |
| 2557 | __ andq(temp, temp_mask); |
| 2558 | __ andq(reg, temp_mask); |
| 2559 | __ shlq(temp, imm_shift); |
| 2560 | __ orq(reg, temp); |
| 2561 | } |
| 2562 | |
| 2563 | void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2564 | X86_64Assembler* assembler = GetAssembler(); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2565 | LocationSummary* locations = invoke->GetLocations(); |
| 2566 | |
| 2567 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2568 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2569 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 2570 | |
| 2571 | /* |
| 2572 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2573 | * swapping bits to reverse bits in a long number x. Using bswap to save instructions |
| 2574 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2575 | * x = bswap x |
| 2576 | * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555; |
| 2577 | * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333; |
| 2578 | * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F; |
| 2579 | */ |
| 2580 | __ bswapq(reg); |
| 2581 | SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler); |
| 2582 | SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler); |
| 2583 | SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler); |
| 2584 | } |
| 2585 | |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2586 | static void CreateBitCountLocations( |
| 2587 | ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) { |
| 2588 | if (!codegen->GetInstructionSetFeatures().HasPopCnt()) { |
| 2589 | // Do nothing if there is no popcnt support. This results in generating |
| 2590 | // a call for the intrinsic rather than direct code. |
| 2591 | return; |
| 2592 | } |
| 2593 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2594 | LocationSummary::kNoCall, |
| 2595 | kIntrinsified); |
| 2596 | locations->SetInAt(0, Location::Any()); |
| 2597 | locations->SetOut(Location::RequiresRegister()); |
| 2598 | } |
| 2599 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2600 | static void GenBitCount(X86_64Assembler* assembler, |
| 2601 | CodeGeneratorX86_64* codegen, |
| 2602 | HInvoke* invoke, |
| 2603 | bool is_long) { |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2604 | LocationSummary* locations = invoke->GetLocations(); |
| 2605 | Location src = locations->InAt(0); |
| 2606 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2607 | |
| 2608 | if (invoke->InputAt(0)->IsConstant()) { |
| 2609 | // Evaluate this at compile time. |
| 2610 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2611 | int32_t result = is_long |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2612 | ? POPCOUNT(static_cast<uint64_t>(value)) |
| 2613 | : POPCOUNT(static_cast<uint32_t>(value)); |
Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2614 | codegen->Load32BitValue(out, result); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2615 | return; |
| 2616 | } |
| 2617 | |
| 2618 | if (src.IsRegister()) { |
| 2619 | if (is_long) { |
| 2620 | __ popcntq(out, src.AsRegister<CpuRegister>()); |
| 2621 | } else { |
| 2622 | __ popcntl(out, src.AsRegister<CpuRegister>()); |
| 2623 | } |
| 2624 | } else if (is_long) { |
| 2625 | DCHECK(src.IsDoubleStackSlot()); |
| 2626 | __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2627 | } else { |
| 2628 | DCHECK(src.IsStackSlot()); |
| 2629 | __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) { |
| 2634 | CreateBitCountLocations(arena_, codegen_, invoke); |
| 2635 | } |
| 2636 | |
| 2637 | void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2638 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) { |
| 2642 | CreateBitCountLocations(arena_, codegen_, invoke); |
| 2643 | } |
| 2644 | |
| 2645 | void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2646 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true); |
| 2647 | } |
| 2648 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2649 | static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) { |
| 2650 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2651 | LocationSummary::kNoCall, |
| 2652 | kIntrinsified); |
| 2653 | locations->SetInAt(0, Location::Any()); |
| 2654 | locations->SetOut(Location::RequiresRegister()); |
| 2655 | locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL |
| 2656 | : Location::RequiresRegister()); // any will do |
| 2657 | } |
| 2658 | |
| 2659 | static void GenOneBit(X86_64Assembler* assembler, |
| 2660 | CodeGeneratorX86_64* codegen, |
| 2661 | HInvoke* invoke, |
| 2662 | bool is_high, bool is_long) { |
| 2663 | LocationSummary* locations = invoke->GetLocations(); |
| 2664 | Location src = locations->InAt(0); |
| 2665 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2666 | |
| 2667 | if (invoke->InputAt(0)->IsConstant()) { |
| 2668 | // Evaluate this at compile time. |
| 2669 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2670 | if (value == 0) { |
| 2671 | __ xorl(out, out); // Clears upper bits too. |
| 2672 | return; |
| 2673 | } |
| 2674 | // Nonzero value. |
| 2675 | if (is_high) { |
| 2676 | value = is_long ? 63 - CLZ(static_cast<uint64_t>(value)) |
| 2677 | : 31 - CLZ(static_cast<uint32_t>(value)); |
| 2678 | } else { |
| 2679 | value = is_long ? CTZ(static_cast<uint64_t>(value)) |
| 2680 | : CTZ(static_cast<uint32_t>(value)); |
| 2681 | } |
| 2682 | if (is_long) { |
Pavel Vyssotski | 7f7f6da | 2016-06-22 12:36:10 +0600 | [diff] [blame] | 2683 | codegen->Load64BitValue(out, 1ULL << value); |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2684 | } else { |
| 2685 | codegen->Load32BitValue(out, 1 << value); |
| 2686 | } |
| 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | // Handle the non-constant cases. |
| 2691 | CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2692 | if (is_high) { |
| 2693 | // Use architectural support: basically 1 << bsr. |
| 2694 | if (src.IsRegister()) { |
| 2695 | if (is_long) { |
| 2696 | __ bsrq(tmp, src.AsRegister<CpuRegister>()); |
| 2697 | } else { |
| 2698 | __ bsrl(tmp, src.AsRegister<CpuRegister>()); |
| 2699 | } |
| 2700 | } else if (is_long) { |
| 2701 | DCHECK(src.IsDoubleStackSlot()); |
| 2702 | __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2703 | } else { |
| 2704 | DCHECK(src.IsStackSlot()); |
| 2705 | __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2706 | } |
| 2707 | // BSR sets ZF if the input was zero. |
| 2708 | NearLabel is_zero, done; |
| 2709 | __ j(kEqual, &is_zero); |
| 2710 | __ movl(out, Immediate(1)); // Clears upper bits too. |
| 2711 | if (is_long) { |
| 2712 | __ shlq(out, tmp); |
| 2713 | } else { |
| 2714 | __ shll(out, tmp); |
| 2715 | } |
| 2716 | __ jmp(&done); |
| 2717 | __ Bind(&is_zero); |
| 2718 | __ xorl(out, out); // Clears upper bits too. |
| 2719 | __ Bind(&done); |
| 2720 | } else { |
| 2721 | // Copy input into temporary. |
| 2722 | if (src.IsRegister()) { |
| 2723 | if (is_long) { |
| 2724 | __ movq(tmp, src.AsRegister<CpuRegister>()); |
| 2725 | } else { |
| 2726 | __ movl(tmp, src.AsRegister<CpuRegister>()); |
| 2727 | } |
| 2728 | } else if (is_long) { |
| 2729 | DCHECK(src.IsDoubleStackSlot()); |
| 2730 | __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2731 | } else { |
| 2732 | DCHECK(src.IsStackSlot()); |
| 2733 | __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2734 | } |
| 2735 | // Do the bit twiddling: basically tmp & -tmp; |
| 2736 | if (is_long) { |
| 2737 | __ movq(out, tmp); |
| 2738 | __ negq(tmp); |
| 2739 | __ andq(out, tmp); |
| 2740 | } else { |
| 2741 | __ movl(out, tmp); |
| 2742 | __ negl(tmp); |
| 2743 | __ andl(out, tmp); |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) { |
| 2749 | CreateOneBitLocations(arena_, invoke, /* is_high */ true); |
| 2750 | } |
| 2751 | |
| 2752 | void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) { |
| 2753 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false); |
| 2754 | } |
| 2755 | |
| 2756 | void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) { |
| 2757 | CreateOneBitLocations(arena_, invoke, /* is_high */ true); |
| 2758 | } |
| 2759 | |
| 2760 | void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) { |
| 2761 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true); |
| 2762 | } |
| 2763 | |
| 2764 | void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 2765 | CreateOneBitLocations(arena_, invoke, /* is_high */ false); |
| 2766 | } |
| 2767 | |
| 2768 | void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 2769 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false); |
| 2770 | } |
| 2771 | |
| 2772 | void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) { |
| 2773 | CreateOneBitLocations(arena_, invoke, /* is_high */ false); |
| 2774 | } |
| 2775 | |
| 2776 | void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) { |
| 2777 | GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true); |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2778 | } |
| 2779 | |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2780 | static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2781 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2782 | LocationSummary::kNoCall, |
| 2783 | kIntrinsified); |
| 2784 | locations->SetInAt(0, Location::Any()); |
| 2785 | locations->SetOut(Location::RequiresRegister()); |
| 2786 | } |
| 2787 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2788 | static void GenLeadingZeros(X86_64Assembler* assembler, |
| 2789 | CodeGeneratorX86_64* codegen, |
| 2790 | HInvoke* invoke, bool is_long) { |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2791 | LocationSummary* locations = invoke->GetLocations(); |
| 2792 | Location src = locations->InAt(0); |
| 2793 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2794 | |
| 2795 | int zero_value_result = is_long ? 64 : 32; |
| 2796 | if (invoke->InputAt(0)->IsConstant()) { |
| 2797 | // Evaluate this at compile time. |
| 2798 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2799 | if (value == 0) { |
| 2800 | value = zero_value_result; |
| 2801 | } else { |
| 2802 | value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value)); |
| 2803 | } |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2804 | codegen->Load32BitValue(out, value); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2805 | return; |
| 2806 | } |
| 2807 | |
| 2808 | // Handle the non-constant cases. |
| 2809 | if (src.IsRegister()) { |
| 2810 | if (is_long) { |
| 2811 | __ bsrq(out, src.AsRegister<CpuRegister>()); |
| 2812 | } else { |
| 2813 | __ bsrl(out, src.AsRegister<CpuRegister>()); |
| 2814 | } |
| 2815 | } else if (is_long) { |
| 2816 | DCHECK(src.IsDoubleStackSlot()); |
| 2817 | __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2818 | } else { |
| 2819 | DCHECK(src.IsStackSlot()); |
| 2820 | __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2821 | } |
| 2822 | |
| 2823 | // 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] | 2824 | NearLabel is_zero, done; |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2825 | __ j(kEqual, &is_zero); |
| 2826 | |
| 2827 | // Correct the result from BSR to get the CLZ result. |
| 2828 | __ xorl(out, Immediate(zero_value_result - 1)); |
| 2829 | __ jmp(&done); |
| 2830 | |
| 2831 | // Fix the zero case with the expected result. |
| 2832 | __ Bind(&is_zero); |
| 2833 | __ movl(out, Immediate(zero_value_result)); |
| 2834 | |
| 2835 | __ Bind(&done); |
| 2836 | } |
| 2837 | |
| 2838 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 2839 | CreateLeadingZeroLocations(arena_, invoke); |
| 2840 | } |
| 2841 | |
| 2842 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2843 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 2847 | CreateLeadingZeroLocations(arena_, invoke); |
| 2848 | } |
| 2849 | |
| 2850 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2851 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true); |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2852 | } |
| 2853 | |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2854 | static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2855 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2856 | LocationSummary::kNoCall, |
| 2857 | kIntrinsified); |
| 2858 | locations->SetInAt(0, Location::Any()); |
| 2859 | locations->SetOut(Location::RequiresRegister()); |
| 2860 | } |
| 2861 | |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2862 | static void GenTrailingZeros(X86_64Assembler* assembler, |
| 2863 | CodeGeneratorX86_64* codegen, |
| 2864 | HInvoke* invoke, bool is_long) { |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2865 | LocationSummary* locations = invoke->GetLocations(); |
| 2866 | Location src = locations->InAt(0); |
| 2867 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2868 | |
| 2869 | int zero_value_result = is_long ? 64 : 32; |
| 2870 | if (invoke->InputAt(0)->IsConstant()) { |
| 2871 | // Evaluate this at compile time. |
| 2872 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2873 | if (value == 0) { |
| 2874 | value = zero_value_result; |
| 2875 | } else { |
| 2876 | value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value)); |
| 2877 | } |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2878 | codegen->Load32BitValue(out, value); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2879 | return; |
| 2880 | } |
| 2881 | |
| 2882 | // Handle the non-constant cases. |
| 2883 | if (src.IsRegister()) { |
| 2884 | if (is_long) { |
| 2885 | __ bsfq(out, src.AsRegister<CpuRegister>()); |
| 2886 | } else { |
| 2887 | __ bsfl(out, src.AsRegister<CpuRegister>()); |
| 2888 | } |
| 2889 | } else if (is_long) { |
| 2890 | DCHECK(src.IsDoubleStackSlot()); |
| 2891 | __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2892 | } else { |
| 2893 | DCHECK(src.IsStackSlot()); |
| 2894 | __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2895 | } |
| 2896 | |
| 2897 | // BSF sets ZF if the input was zero, and the output is undefined. |
| 2898 | NearLabel done; |
| 2899 | __ j(kNotEqual, &done); |
| 2900 | |
| 2901 | // Fix the zero case with the expected result. |
| 2902 | __ movl(out, Immediate(zero_value_result)); |
| 2903 | |
| 2904 | __ Bind(&done); |
| 2905 | } |
| 2906 | |
| 2907 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 2908 | CreateTrailingZeroLocations(arena_, invoke); |
| 2909 | } |
| 2910 | |
| 2911 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2912 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false); |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 2916 | CreateTrailingZeroLocations(arena_, invoke); |
| 2917 | } |
| 2918 | |
| 2919 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
Aart Bik | c5d4754 | 2016-01-27 17:00:35 -0800 | [diff] [blame] | 2920 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true); |
| 2921 | } |
| 2922 | |
Serguei Katkov | 288c7a8 | 2016-05-16 11:53:15 +0600 | [diff] [blame] | 2923 | void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) { |
| 2924 | if (kEmitCompilerReadBarrier) { |
| 2925 | // Do not intrinsify this call with the read barrier configuration. |
| 2926 | return; |
| 2927 | } |
| 2928 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2929 | LocationSummary::kCallOnSlowPath, |
| 2930 | kIntrinsified); |
| 2931 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2932 | locations->SetOut(Location::SameAsFirstInput()); |
| 2933 | locations->AddTemp(Location::RequiresRegister()); |
| 2934 | } |
| 2935 | |
| 2936 | void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) { |
| 2937 | DCHECK(!kEmitCompilerReadBarrier); |
| 2938 | LocationSummary* locations = invoke->GetLocations(); |
| 2939 | X86_64Assembler* assembler = GetAssembler(); |
| 2940 | |
| 2941 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2942 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2943 | |
| 2944 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
| 2945 | codegen_->AddSlowPath(slow_path); |
| 2946 | |
| 2947 | // Load ArtMethod first. |
| 2948 | HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect(); |
| 2949 | DCHECK(invoke_direct != nullptr); |
| 2950 | Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall( |
| 2951 | invoke_direct, locations->GetTemp(0)); |
| 2952 | DCHECK(temp_loc.Equals(locations->GetTemp(0))); |
| 2953 | CpuRegister temp = temp_loc.AsRegister<CpuRegister>(); |
| 2954 | |
| 2955 | // Now get declaring class. |
| 2956 | __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value())); |
| 2957 | |
| 2958 | uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset(); |
| 2959 | uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset(); |
| 2960 | DCHECK_NE(slow_path_flag_offset, 0u); |
| 2961 | DCHECK_NE(disable_flag_offset, 0u); |
| 2962 | DCHECK_NE(slow_path_flag_offset, disable_flag_offset); |
| 2963 | |
| 2964 | // Check static flags preventing us for using intrinsic. |
| 2965 | if (slow_path_flag_offset == disable_flag_offset + 1) { |
| 2966 | __ cmpw(Address(temp, disable_flag_offset), Immediate(0)); |
| 2967 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 2968 | } else { |
| 2969 | __ cmpb(Address(temp, disable_flag_offset), Immediate(0)); |
| 2970 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 2971 | __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0)); |
| 2972 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 2973 | } |
| 2974 | |
| 2975 | // Fast path. |
| 2976 | __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value())); |
| 2977 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
| 2978 | __ MaybeUnpoisonHeapReference(out); |
| 2979 | __ Bind(slow_path->GetExitLabel()); |
| 2980 | } |
| 2981 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2982 | UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite) |
| 2983 | UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2984 | |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 2985 | // 1.8. |
| 2986 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt) |
| 2987 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong) |
| 2988 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt) |
| 2989 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong) |
| 2990 | UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject) |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 2991 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2992 | UNREACHABLE_INTRINSICS(X86_64) |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2993 | |
| 2994 | #undef __ |
| 2995 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2996 | } // namespace x86_64 |
| 2997 | } // namespace art |