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 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 19 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 20 | #include "code_generator_x86_64.h" |
| 21 | #include "entrypoints/quick/quick_entrypoints.h" |
| 22 | #include "intrinsics.h" |
| 23 | #include "mirror/array-inl.h" |
| 24 | #include "mirror/art_method.h" |
| 25 | #include "mirror/string.h" |
| 26 | #include "thread.h" |
| 27 | #include "utils/x86_64/assembler_x86_64.h" |
| 28 | #include "utils/x86_64/constants_x86_64.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace x86_64 { |
| 33 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 34 | IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen) |
| 35 | : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) { |
| 36 | } |
| 37 | |
| 38 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 39 | X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() { |
| 40 | return reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 41 | } |
| 42 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 43 | ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 44 | return codegen_->GetGraph()->GetArena(); |
| 45 | } |
| 46 | |
| 47 | bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) { |
| 48 | Dispatch(invoke); |
| 49 | const LocationSummary* res = invoke->GetLocations(); |
| 50 | return res != nullptr && res->Intrinsified(); |
| 51 | } |
| 52 | |
| 53 | #define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())-> |
| 54 | |
| 55 | // TODO: trg as memory. |
| 56 | static void MoveFromReturnRegister(Location trg, |
| 57 | Primitive::Type type, |
| 58 | CodeGeneratorX86_64* codegen) { |
| 59 | if (!trg.IsValid()) { |
| 60 | DCHECK(type == Primitive::kPrimVoid); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | switch (type) { |
| 65 | case Primitive::kPrimBoolean: |
| 66 | case Primitive::kPrimByte: |
| 67 | case Primitive::kPrimChar: |
| 68 | case Primitive::kPrimShort: |
| 69 | case Primitive::kPrimInt: |
| 70 | case Primitive::kPrimNot: { |
| 71 | CpuRegister trg_reg = trg.AsRegister<CpuRegister>(); |
| 72 | if (trg_reg.AsRegister() != RAX) { |
| 73 | __ movl(trg_reg, CpuRegister(RAX)); |
| 74 | } |
| 75 | break; |
| 76 | } |
| 77 | case Primitive::kPrimLong: { |
| 78 | CpuRegister trg_reg = trg.AsRegister<CpuRegister>(); |
| 79 | if (trg_reg.AsRegister() != RAX) { |
| 80 | __ movq(trg_reg, CpuRegister(RAX)); |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | case Primitive::kPrimVoid: |
| 86 | LOG(FATAL) << "Unexpected void type for valid location " << trg; |
| 87 | UNREACHABLE(); |
| 88 | |
| 89 | case Primitive::kPrimDouble: { |
| 90 | XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>(); |
| 91 | if (trg_reg.AsFloatRegister() != XMM0) { |
| 92 | __ movsd(trg_reg, XmmRegister(XMM0)); |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | case Primitive::kPrimFloat: { |
| 97 | XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>(); |
| 98 | if (trg_reg.AsFloatRegister() != XMM0) { |
| 99 | __ movss(trg_reg, XmmRegister(XMM0)); |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void MoveArguments(HInvoke* invoke, ArenaAllocator* arena, CodeGeneratorX86_64* codegen) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame^] | 107 | if (invoke->GetNumberOfArguments() == 0) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 108 | // No argument to move. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | LocationSummary* locations = invoke->GetLocations(); |
| 113 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
| 114 | |
| 115 | // We're moving potentially two or more locations to locations that could overlap, so we need |
| 116 | // a parallel move resolver. |
| 117 | HParallelMove parallel_move(arena); |
| 118 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame^] | 119 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 120 | HInstruction* input = invoke->InputAt(i); |
| 121 | Location cc_loc = calling_convention_visitor.GetNextLocation(input->GetType()); |
| 122 | Location actual_loc = locations->InAt(i); |
| 123 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 124 | parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 128 | } |
| 129 | |
| 130 | // Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified |
| 131 | // call. This will copy the arguments into the positions for a regular call. |
| 132 | // |
| 133 | // Note: The actual parameters are required to be in the locations given by the invoke's location |
| 134 | // summary. If an intrinsic modifies those locations before a slowpath call, they must be |
| 135 | // restored! |
| 136 | class IntrinsicSlowPathX86_64 : public SlowPathCodeX86_64 { |
| 137 | public: |
| 138 | explicit IntrinsicSlowPathX86_64(HInvoke* invoke) : invoke_(invoke) { } |
| 139 | |
| 140 | void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE { |
| 141 | CodeGeneratorX86_64* codegen = down_cast<CodeGeneratorX86_64*>(codegen_in); |
| 142 | __ Bind(GetEntryLabel()); |
| 143 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 144 | SaveLiveRegisters(codegen, invoke_->GetLocations()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 145 | |
| 146 | MoveArguments(invoke_, codegen->GetGraph()->GetArena(), codegen); |
| 147 | |
| 148 | if (invoke_->IsInvokeStaticOrDirect()) { |
| 149 | codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), CpuRegister(RDI)); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 150 | RecordPcInfo(codegen, invoke_, invoke_->GetDexPc()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 151 | } else { |
| 152 | UNIMPLEMENTED(FATAL) << "Non-direct intrinsic slow-path not yet implemented"; |
| 153 | UNREACHABLE(); |
| 154 | } |
| 155 | |
| 156 | // Copy the result back to the expected output. |
| 157 | Location out = invoke_->GetLocations()->Out(); |
| 158 | if (out.IsValid()) { |
| 159 | DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory. |
| 160 | DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg())); |
| 161 | MoveFromReturnRegister(out, invoke_->GetType(), codegen); |
| 162 | } |
| 163 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 164 | RestoreLiveRegisters(codegen, invoke_->GetLocations()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 165 | __ jmp(GetExitLabel()); |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | // The instruction where this slow path is happening. |
| 170 | HInvoke* const invoke_; |
| 171 | |
| 172 | DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathX86_64); |
| 173 | }; |
| 174 | |
| 175 | #undef __ |
| 176 | #define __ assembler-> |
| 177 | |
| 178 | static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 179 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 180 | LocationSummary::kNoCall, |
| 181 | kIntrinsified); |
| 182 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 183 | locations->SetOut(Location::RequiresRegister()); |
| 184 | } |
| 185 | |
| 186 | static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 187 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 188 | LocationSummary::kNoCall, |
| 189 | kIntrinsified); |
| 190 | locations->SetInAt(0, Location::RequiresRegister()); |
| 191 | locations->SetOut(Location::RequiresFpuRegister()); |
| 192 | } |
| 193 | |
| 194 | static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 195 | Location input = locations->InAt(0); |
| 196 | Location output = locations->Out(); |
| 197 | __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit); |
| 198 | } |
| 199 | |
| 200 | static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 201 | Location input = locations->InAt(0); |
| 202 | Location output = locations->Out(); |
| 203 | __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit); |
| 204 | } |
| 205 | |
| 206 | void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| 207 | CreateFPToIntLocations(arena_, invoke); |
| 208 | } |
| 209 | void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| 210 | CreateIntToFPLocations(arena_, invoke); |
| 211 | } |
| 212 | |
| 213 | void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| 214 | MoveFPToInt(invoke->GetLocations(), true, GetAssembler()); |
| 215 | } |
| 216 | void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| 217 | MoveIntToFP(invoke->GetLocations(), true, GetAssembler()); |
| 218 | } |
| 219 | |
| 220 | void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| 221 | CreateFPToIntLocations(arena_, invoke); |
| 222 | } |
| 223 | void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| 224 | CreateIntToFPLocations(arena_, invoke); |
| 225 | } |
| 226 | |
| 227 | void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| 228 | MoveFPToInt(invoke->GetLocations(), false, GetAssembler()); |
| 229 | } |
| 230 | void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| 231 | MoveIntToFP(invoke->GetLocations(), false, GetAssembler()); |
| 232 | } |
| 233 | |
| 234 | static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 235 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 236 | LocationSummary::kNoCall, |
| 237 | kIntrinsified); |
| 238 | locations->SetInAt(0, Location::RequiresRegister()); |
| 239 | locations->SetOut(Location::SameAsFirstInput()); |
| 240 | } |
| 241 | |
| 242 | static void GenReverseBytes(LocationSummary* locations, |
| 243 | Primitive::Type size, |
| 244 | X86_64Assembler* assembler) { |
| 245 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 246 | |
| 247 | switch (size) { |
| 248 | case Primitive::kPrimShort: |
| 249 | // TODO: Can be done with an xchg of 8b registers. This is straight from Quick. |
| 250 | __ bswapl(out); |
| 251 | __ sarl(out, Immediate(16)); |
| 252 | break; |
| 253 | case Primitive::kPrimInt: |
| 254 | __ bswapl(out); |
| 255 | break; |
| 256 | case Primitive::kPrimLong: |
| 257 | __ bswapq(out); |
| 258 | break; |
| 259 | default: |
| 260 | LOG(FATAL) << "Unexpected size for reverse-bytes: " << size; |
| 261 | UNREACHABLE(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
| 266 | CreateIntToIntLocations(arena_, invoke); |
| 267 | } |
| 268 | |
| 269 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
| 270 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 271 | } |
| 272 | |
| 273 | void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
| 274 | CreateIntToIntLocations(arena_, invoke); |
| 275 | } |
| 276 | |
| 277 | void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
| 278 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 279 | } |
| 280 | |
| 281 | void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
| 282 | CreateIntToIntLocations(arena_, invoke); |
| 283 | } |
| 284 | |
| 285 | void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
| 286 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | // TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we |
| 291 | // need is 64b. |
| 292 | |
| 293 | static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) { |
| 294 | // TODO: Enable memory operations when the assembler supports them. |
| 295 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 296 | LocationSummary::kNoCall, |
| 297 | kIntrinsified); |
| 298 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 299 | // TODO: Allow x86 to work with memory. This requires assembler support, see below. |
| 300 | // locations->SetInAt(0, Location::Any()); // X86 can work on memory directly. |
| 301 | locations->SetOut(Location::SameAsFirstInput()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 302 | locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 305 | static void MathAbsFP(LocationSummary* locations, |
| 306 | bool is64bit, |
| 307 | X86_64Assembler* assembler, |
| 308 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 309 | Location output = locations->Out(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 310 | |
| 311 | if (output.IsFpuRegister()) { |
| 312 | // In-register |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 313 | XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 314 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 315 | // TODO: Can mask directly with constant area using pand if we can guarantee |
| 316 | // that the literal is aligned on a 16 byte boundary. This will avoid a |
| 317 | // temporary. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 318 | if (is64bit) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 319 | __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 320 | __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
| 321 | } else { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 322 | __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 323 | __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
| 324 | } |
| 325 | } else { |
| 326 | // TODO: update when assember support is available. |
| 327 | UNIMPLEMENTED(FATAL) << "Needs assembler support."; |
| 328 | // Once assembler support is available, in-memory operations look like this: |
| 329 | // if (is64bit) { |
| 330 | // DCHECK(output.IsDoubleStackSlot()); |
| 331 | // // No 64b and with literal. |
| 332 | // __ movq(cpu_temp, Immediate(INT64_C(0x7FFFFFFFFFFFFFFF))); |
| 333 | // __ andq(Address(CpuRegister(RSP), output.GetStackIndex()), cpu_temp); |
| 334 | // } else { |
| 335 | // DCHECK(output.IsStackSlot()); |
| 336 | // // Can use and with a literal directly. |
| 337 | // __ andl(Address(CpuRegister(RSP), output.GetStackIndex()), Immediate(INT64_C(0x7FFFFFFF))); |
| 338 | // } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
| 343 | CreateFloatToFloatPlusTemps(arena_, invoke); |
| 344 | } |
| 345 | |
| 346 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 347 | MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
| 351 | CreateFloatToFloatPlusTemps(arena_, invoke); |
| 352 | } |
| 353 | |
| 354 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 355 | MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) { |
| 359 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 360 | LocationSummary::kNoCall, |
| 361 | kIntrinsified); |
| 362 | locations->SetInAt(0, Location::RequiresRegister()); |
| 363 | locations->SetOut(Location::SameAsFirstInput()); |
| 364 | locations->AddTemp(Location::RequiresRegister()); |
| 365 | } |
| 366 | |
| 367 | static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 368 | Location output = locations->Out(); |
| 369 | CpuRegister out = output.AsRegister<CpuRegister>(); |
| 370 | CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 371 | |
| 372 | if (is64bit) { |
| 373 | // Create mask. |
| 374 | __ movq(mask, out); |
| 375 | __ sarq(mask, Immediate(63)); |
| 376 | // Add mask. |
| 377 | __ addq(out, mask); |
| 378 | __ xorq(out, mask); |
| 379 | } else { |
| 380 | // Create mask. |
| 381 | __ movl(mask, out); |
| 382 | __ sarl(mask, Immediate(31)); |
| 383 | // Add mask. |
| 384 | __ addl(out, mask); |
| 385 | __ xorl(out, mask); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) { |
| 390 | CreateIntToIntPlusTemp(arena_, invoke); |
| 391 | } |
| 392 | |
| 393 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) { |
| 394 | GenAbsInteger(invoke->GetLocations(), false, GetAssembler()); |
| 395 | } |
| 396 | |
| 397 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) { |
| 398 | CreateIntToIntPlusTemp(arena_, invoke); |
| 399 | } |
| 400 | |
| 401 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) { |
| 402 | GenAbsInteger(invoke->GetLocations(), true, GetAssembler()); |
| 403 | } |
| 404 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 405 | static void GenMinMaxFP(LocationSummary* locations, |
| 406 | bool is_min, |
| 407 | bool is_double, |
| 408 | X86_64Assembler* assembler, |
| 409 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 410 | Location op1_loc = locations->InAt(0); |
| 411 | Location op2_loc = locations->InAt(1); |
| 412 | Location out_loc = locations->Out(); |
| 413 | XmmRegister out = out_loc.AsFpuRegister<XmmRegister>(); |
| 414 | |
| 415 | // Shortcut for same input locations. |
| 416 | if (op1_loc.Equals(op2_loc)) { |
| 417 | DCHECK(out_loc.Equals(op1_loc)); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // (out := op1) |
| 422 | // out <=? op2 |
| 423 | // if Nan jmp Nan_label |
| 424 | // if out is min jmp done |
| 425 | // if op2 is min jmp op2_label |
| 426 | // handle -0/+0 |
| 427 | // jmp done |
| 428 | // Nan_label: |
| 429 | // out := NaN |
| 430 | // op2_label: |
| 431 | // out := op2 |
| 432 | // done: |
| 433 | // |
| 434 | // This removes one jmp, but needs to copy one input (op1) to out. |
| 435 | // |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 436 | // 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] | 437 | |
| 438 | XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>(); |
| 439 | |
| 440 | Label nan, done, op2_label; |
| 441 | if (is_double) { |
| 442 | __ ucomisd(out, op2); |
| 443 | } else { |
| 444 | __ ucomiss(out, op2); |
| 445 | } |
| 446 | |
| 447 | __ j(Condition::kParityEven, &nan); |
| 448 | |
| 449 | __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label); |
| 450 | __ j(is_min ? Condition::kBelow : Condition::kAbove, &done); |
| 451 | |
| 452 | // Handle 0.0/-0.0. |
| 453 | if (is_min) { |
| 454 | if (is_double) { |
| 455 | __ orpd(out, op2); |
| 456 | } else { |
| 457 | __ orps(out, op2); |
| 458 | } |
| 459 | } else { |
| 460 | if (is_double) { |
| 461 | __ andpd(out, op2); |
| 462 | } else { |
| 463 | __ andps(out, op2); |
| 464 | } |
| 465 | } |
| 466 | __ jmp(&done); |
| 467 | |
| 468 | // NaN handling. |
| 469 | __ Bind(&nan); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 470 | if (is_double) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 471 | __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 472 | } else { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 473 | __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 474 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 475 | __ jmp(&done); |
| 476 | |
| 477 | // out := op2; |
| 478 | __ Bind(&op2_label); |
| 479 | if (is_double) { |
| 480 | __ movsd(out, op2); |
| 481 | } else { |
| 482 | __ movss(out, op2); |
| 483 | } |
| 484 | |
| 485 | // Done. |
| 486 | __ Bind(&done); |
| 487 | } |
| 488 | |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 489 | static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 490 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 491 | LocationSummary::kNoCall, |
| 492 | kIntrinsified); |
| 493 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 494 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 495 | // The following is sub-optimal, but all we can do for now. It would be fine to also accept |
| 496 | // the second input to be the output (we can simply swap inputs). |
| 497 | locations->SetOut(Location::SameAsFirstInput()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 501 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 505 | GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 509 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 513 | GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 517 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 521 | GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 525 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 529 | GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long, |
| 533 | X86_64Assembler* assembler) { |
| 534 | Location op1_loc = locations->InAt(0); |
| 535 | Location op2_loc = locations->InAt(1); |
| 536 | |
| 537 | // Shortcut for same input locations. |
| 538 | if (op1_loc.Equals(op2_loc)) { |
| 539 | // Can return immediately, as op1_loc == out_loc. |
| 540 | // Note: if we ever support separate registers, e.g., output into memory, we need to check for |
| 541 | // a copy here. |
| 542 | DCHECK(locations->Out().Equals(op1_loc)); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 547 | CpuRegister op2 = op2_loc.AsRegister<CpuRegister>(); |
| 548 | |
| 549 | // (out := op1) |
| 550 | // out <=? op2 |
| 551 | // if out is min jmp done |
| 552 | // out := op2 |
| 553 | // done: |
| 554 | |
| 555 | if (is_long) { |
| 556 | __ cmpq(out, op2); |
| 557 | } else { |
| 558 | __ cmpl(out, op2); |
| 559 | } |
| 560 | |
| 561 | __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long); |
| 562 | } |
| 563 | |
| 564 | static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 565 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 566 | LocationSummary::kNoCall, |
| 567 | kIntrinsified); |
| 568 | locations->SetInAt(0, Location::RequiresRegister()); |
| 569 | locations->SetInAt(1, Location::RequiresRegister()); |
| 570 | locations->SetOut(Location::SameAsFirstInput()); |
| 571 | } |
| 572 | |
| 573 | void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
| 574 | CreateIntIntToIntLocations(arena_, invoke); |
| 575 | } |
| 576 | |
| 577 | void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
| 578 | GenMinMax(invoke->GetLocations(), true, false, GetAssembler()); |
| 579 | } |
| 580 | |
| 581 | void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
| 582 | CreateIntIntToIntLocations(arena_, invoke); |
| 583 | } |
| 584 | |
| 585 | void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
| 586 | GenMinMax(invoke->GetLocations(), true, true, GetAssembler()); |
| 587 | } |
| 588 | |
| 589 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
| 590 | CreateIntIntToIntLocations(arena_, invoke); |
| 591 | } |
| 592 | |
| 593 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
| 594 | GenMinMax(invoke->GetLocations(), false, false, GetAssembler()); |
| 595 | } |
| 596 | |
| 597 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
| 598 | CreateIntIntToIntLocations(arena_, invoke); |
| 599 | } |
| 600 | |
| 601 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
| 602 | GenMinMax(invoke->GetLocations(), false, true, GetAssembler()); |
| 603 | } |
| 604 | |
| 605 | static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 606 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 607 | LocationSummary::kNoCall, |
| 608 | kIntrinsified); |
| 609 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 610 | locations->SetOut(Location::RequiresFpuRegister()); |
| 611 | } |
| 612 | |
| 613 | void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) { |
| 614 | CreateFPToFPLocations(arena_, invoke); |
| 615 | } |
| 616 | |
| 617 | void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) { |
| 618 | LocationSummary* locations = invoke->GetLocations(); |
| 619 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 620 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 621 | |
| 622 | GetAssembler()->sqrtsd(out, in); |
| 623 | } |
| 624 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 625 | static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) { |
| 626 | MoveArguments(invoke, codegen->GetGraph()->GetArena(), codegen); |
| 627 | |
| 628 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| 629 | codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(), CpuRegister(RDI)); |
| 630 | codegen->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 631 | |
| 632 | // Copy the result back to the expected output. |
| 633 | Location out = invoke->GetLocations()->Out(); |
| 634 | if (out.IsValid()) { |
| 635 | DCHECK(out.IsRegister()); |
| 636 | MoveFromReturnRegister(out, invoke->GetType(), codegen); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static void CreateSSE41FPToFPLocations(ArenaAllocator* arena, |
| 641 | HInvoke* invoke, |
| 642 | CodeGeneratorX86_64* codegen) { |
| 643 | // Do we have instruction support? |
| 644 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
| 645 | CreateFPToFPLocations(arena, invoke); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | // We have to fall back to a call to the intrinsic. |
| 650 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 651 | LocationSummary::kCall); |
| 652 | InvokeRuntimeCallingConvention calling_convention; |
| 653 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 654 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 655 | // Needs to be RDI for the invoke. |
| 656 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 657 | } |
| 658 | |
| 659 | static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen, |
| 660 | HInvoke* invoke, |
| 661 | X86_64Assembler* assembler, |
| 662 | int round_mode) { |
| 663 | LocationSummary* locations = invoke->GetLocations(); |
| 664 | if (locations->WillCall()) { |
| 665 | InvokeOutOfLineIntrinsic(codegen, invoke); |
| 666 | } else { |
| 667 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 668 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 669 | __ roundsd(out, in, Immediate(round_mode)); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) { |
| 674 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 675 | } |
| 676 | |
| 677 | void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) { |
| 678 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2); |
| 679 | } |
| 680 | |
| 681 | void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) { |
| 682 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 683 | } |
| 684 | |
| 685 | void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) { |
| 686 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1); |
| 687 | } |
| 688 | |
| 689 | void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) { |
| 690 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 691 | } |
| 692 | |
| 693 | void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) { |
| 694 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0); |
| 695 | } |
| 696 | |
| 697 | static void CreateSSE41FPToIntLocations(ArenaAllocator* arena, |
| 698 | HInvoke* invoke, |
| 699 | CodeGeneratorX86_64* codegen) { |
| 700 | // Do we have instruction support? |
| 701 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
| 702 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 703 | LocationSummary::kNoCall, |
| 704 | kIntrinsified); |
| 705 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 706 | locations->SetOut(Location::RequiresFpuRegister()); |
| 707 | locations->AddTemp(Location::RequiresFpuRegister()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 708 | return; |
| 709 | } |
| 710 | |
| 711 | // We have to fall back to a call to the intrinsic. |
| 712 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 713 | LocationSummary::kCall); |
| 714 | InvokeRuntimeCallingConvention calling_convention; |
| 715 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 716 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 717 | // Needs to be RDI for the invoke. |
| 718 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 719 | } |
| 720 | |
| 721 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 722 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 723 | } |
| 724 | |
| 725 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 726 | LocationSummary* locations = invoke->GetLocations(); |
| 727 | if (locations->WillCall()) { |
| 728 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int. |
| 733 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 734 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 735 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 736 | Label done, nan; |
| 737 | X86_64Assembler* assembler = GetAssembler(); |
| 738 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 739 | // Load 0.5 into inPlusPointFive. |
| 740 | __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 741 | |
| 742 | // Add in the input. |
| 743 | __ addss(inPlusPointFive, in); |
| 744 | |
| 745 | // And truncate to an integer. |
| 746 | __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 747 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 748 | // if inPlusPointFive >= maxInt goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 749 | __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 750 | __ j(kAboveEqual, &done); |
| 751 | |
| 752 | // if input == NaN goto nan |
| 753 | __ j(kUnordered, &nan); |
| 754 | |
| 755 | // output = float-to-int-truncate(input) |
| 756 | __ cvttss2si(out, inPlusPointFive); |
| 757 | __ jmp(&done); |
| 758 | __ Bind(&nan); |
| 759 | |
| 760 | // output = 0 |
| 761 | __ xorl(out, out); |
| 762 | __ Bind(&done); |
| 763 | } |
| 764 | |
| 765 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 766 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 767 | } |
| 768 | |
| 769 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 770 | LocationSummary* locations = invoke->GetLocations(); |
| 771 | if (locations->WillCall()) { |
| 772 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | // Implement RoundDouble as t1 = floor(input + 0.5); convert to long. |
| 777 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 778 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 779 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 780 | Label done, nan; |
| 781 | X86_64Assembler* assembler = GetAssembler(); |
| 782 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 783 | // Load 0.5 into inPlusPointFive. |
| 784 | __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 785 | |
| 786 | // Add in the input. |
| 787 | __ addsd(inPlusPointFive, in); |
| 788 | |
| 789 | // And truncate to an integer. |
| 790 | __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 791 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 792 | // if inPlusPointFive >= maxLong goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 793 | __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 794 | __ j(kAboveEqual, &done); |
| 795 | |
| 796 | // if input == NaN goto nan |
| 797 | __ j(kUnordered, &nan); |
| 798 | |
| 799 | // output = double-to-long-truncate(input) |
| 800 | __ cvttsd2si(out, inPlusPointFive, true); |
| 801 | __ jmp(&done); |
| 802 | __ Bind(&nan); |
| 803 | |
| 804 | // output = 0 |
| 805 | __ xorq(out, out); |
| 806 | __ Bind(&done); |
| 807 | } |
| 808 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 809 | void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 810 | // The inputs plus one temp. |
| 811 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 812 | LocationSummary::kCallOnSlowPath, |
| 813 | kIntrinsified); |
| 814 | locations->SetInAt(0, Location::RequiresRegister()); |
| 815 | locations->SetInAt(1, Location::RequiresRegister()); |
| 816 | locations->SetOut(Location::SameAsFirstInput()); |
| 817 | locations->AddTemp(Location::RequiresRegister()); |
| 818 | } |
| 819 | |
| 820 | void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 821 | LocationSummary* locations = invoke->GetLocations(); |
| 822 | |
| 823 | // Location of reference to data array |
| 824 | const int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 825 | // Location of count |
| 826 | const int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 827 | // Starting offset within data array |
| 828 | const int32_t offset_offset = mirror::String::OffsetOffset().Int32Value(); |
| 829 | // Start of char data with array_ |
| 830 | const int32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value(); |
| 831 | |
| 832 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 833 | CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>(); |
| 834 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 835 | Location temp_loc = locations->GetTemp(0); |
| 836 | CpuRegister temp = temp_loc.AsRegister<CpuRegister>(); |
| 837 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 838 | // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth |
| 839 | // the cost. |
| 840 | // TODO: For simplicity, the index parameter is requested in a register, so different from Quick |
| 841 | // we will not optimize the code for constants (which would save a register). |
| 842 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 843 | SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 844 | codegen_->AddSlowPath(slow_path); |
| 845 | |
| 846 | X86_64Assembler* assembler = GetAssembler(); |
| 847 | |
| 848 | __ cmpl(idx, Address(obj, count_offset)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 849 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 850 | __ j(kAboveEqual, slow_path->GetEntryLabel()); |
| 851 | |
| 852 | // Get the actual element. |
| 853 | __ movl(temp, idx); // temp := idx. |
| 854 | __ addl(temp, Address(obj, offset_offset)); // temp := offset + idx. |
| 855 | __ movl(out, Address(obj, value_offset)); // obj := obj.array. |
| 856 | // out = out[2*temp]. |
| 857 | __ movzxw(out, Address(out, temp, ScaleFactor::TIMES_2, data_offset)); |
| 858 | |
| 859 | __ Bind(slow_path->GetExitLabel()); |
| 860 | } |
| 861 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 862 | void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 863 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 864 | LocationSummary::kCall, |
| 865 | kIntrinsified); |
| 866 | InvokeRuntimeCallingConvention calling_convention; |
| 867 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 868 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 869 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 870 | } |
| 871 | |
| 872 | void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 873 | X86_64Assembler* assembler = GetAssembler(); |
| 874 | LocationSummary* locations = invoke->GetLocations(); |
| 875 | |
Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 876 | // Note that the null check must have been done earlier. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 877 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 878 | |
| 879 | CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>(); |
| 880 | __ testl(argument, argument); |
| 881 | SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
| 882 | codegen_->AddSlowPath(slow_path); |
| 883 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 884 | |
| 885 | __ gs()->call(Address::Absolute( |
| 886 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true)); |
| 887 | __ Bind(slow_path->GetExitLabel()); |
| 888 | } |
| 889 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 890 | static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 891 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
| 892 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity. |
| 893 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 894 | // to avoid a SIGBUS. |
| 895 | switch (size) { |
| 896 | case Primitive::kPrimByte: |
| 897 | __ movsxb(out, Address(address, 0)); |
| 898 | break; |
| 899 | case Primitive::kPrimShort: |
| 900 | __ movsxw(out, Address(address, 0)); |
| 901 | break; |
| 902 | case Primitive::kPrimInt: |
| 903 | __ movl(out, Address(address, 0)); |
| 904 | break; |
| 905 | case Primitive::kPrimLong: |
| 906 | __ movq(out, Address(address, 0)); |
| 907 | break; |
| 908 | default: |
| 909 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 910 | UNREACHABLE(); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 915 | CreateIntToIntLocations(arena_, invoke); |
| 916 | } |
| 917 | |
| 918 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 919 | GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 920 | } |
| 921 | |
| 922 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 923 | CreateIntToIntLocations(arena_, invoke); |
| 924 | } |
| 925 | |
| 926 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 927 | GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 928 | } |
| 929 | |
| 930 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 931 | CreateIntToIntLocations(arena_, invoke); |
| 932 | } |
| 933 | |
| 934 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 935 | GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 936 | } |
| 937 | |
| 938 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 939 | CreateIntToIntLocations(arena_, invoke); |
| 940 | } |
| 941 | |
| 942 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 943 | GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 944 | } |
| 945 | |
| 946 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 947 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 948 | LocationSummary::kNoCall, |
| 949 | kIntrinsified); |
| 950 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 951 | locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 955 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 956 | Location value = locations->InAt(1); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 957 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 958 | // to avoid a SIGBUS. |
| 959 | switch (size) { |
| 960 | case Primitive::kPrimByte: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 961 | if (value.IsConstant()) { |
| 962 | __ movb(Address(address, 0), |
| 963 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 964 | } else { |
| 965 | __ movb(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 966 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 967 | break; |
| 968 | case Primitive::kPrimShort: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 969 | if (value.IsConstant()) { |
| 970 | __ movw(Address(address, 0), |
| 971 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 972 | } else { |
| 973 | __ movw(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 974 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 975 | break; |
| 976 | case Primitive::kPrimInt: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 977 | if (value.IsConstant()) { |
| 978 | __ movl(Address(address, 0), |
| 979 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 980 | } else { |
| 981 | __ movl(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 982 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 983 | break; |
| 984 | case Primitive::kPrimLong: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 985 | if (value.IsConstant()) { |
| 986 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
| 987 | DCHECK(IsInt<32>(v)); |
| 988 | int32_t v_32 = v; |
| 989 | __ movq(Address(address, 0), Immediate(v_32)); |
| 990 | } else { |
| 991 | __ movq(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 992 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 993 | break; |
| 994 | default: |
| 995 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 996 | UNREACHABLE(); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1001 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1002 | } |
| 1003 | |
| 1004 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1005 | GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1006 | } |
| 1007 | |
| 1008 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1009 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1010 | } |
| 1011 | |
| 1012 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1013 | GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 1014 | } |
| 1015 | |
| 1016 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1017 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1018 | } |
| 1019 | |
| 1020 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1021 | GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 1022 | } |
| 1023 | |
| 1024 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1025 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1026 | } |
| 1027 | |
| 1028 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1029 | GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 1030 | } |
| 1031 | |
| 1032 | void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1033 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1034 | LocationSummary::kNoCall, |
| 1035 | kIntrinsified); |
| 1036 | locations->SetOut(Location::RequiresRegister()); |
| 1037 | } |
| 1038 | |
| 1039 | void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1040 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
| 1041 | GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true)); |
| 1042 | } |
| 1043 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1044 | static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1045 | bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) { |
| 1046 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1047 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1048 | CpuRegister trg = locations->Out().AsRegister<CpuRegister>(); |
| 1049 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1050 | switch (type) { |
| 1051 | case Primitive::kPrimInt: |
| 1052 | case Primitive::kPrimNot: |
| 1053 | __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1054 | break; |
| 1055 | |
| 1056 | case Primitive::kPrimLong: |
| 1057 | __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1058 | break; |
| 1059 | |
| 1060 | default: |
| 1061 | LOG(FATAL) << "Unsupported op size " << type; |
| 1062 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1067 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1068 | LocationSummary::kNoCall, |
| 1069 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1070 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1071 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1072 | locations->SetInAt(2, Location::RequiresRegister()); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1073 | locations->SetOut(Location::RequiresRegister()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) { |
| 1077 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1078 | } |
| 1079 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| 1080 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1081 | } |
| 1082 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
| 1083 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1084 | } |
| 1085 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1086 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1087 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1088 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1089 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1090 | } |
| 1091 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1092 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1093 | } |
| 1094 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1095 | |
| 1096 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1097 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1098 | } |
| 1099 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1100 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1101 | } |
| 1102 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1103 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1104 | } |
| 1105 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1106 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1107 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1108 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1109 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler()); |
| 1110 | } |
| 1111 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1112 | GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler()); |
| 1113 | } |
| 1114 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1115 | |
| 1116 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena, |
| 1117 | Primitive::Type type, |
| 1118 | HInvoke* invoke) { |
| 1119 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1120 | LocationSummary::kNoCall, |
| 1121 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1122 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1123 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1124 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1125 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1126 | if (type == Primitive::kPrimNot) { |
| 1127 | // Need temp registers for card-marking. |
| 1128 | locations->AddTemp(Location::RequiresRegister()); |
| 1129 | locations->AddTemp(Location::RequiresRegister()); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 1134 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1135 | } |
| 1136 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1137 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1138 | } |
| 1139 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1140 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1141 | } |
| 1142 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 1143 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1144 | } |
| 1145 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1146 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1147 | } |
| 1148 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1149 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1150 | } |
| 1151 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 1152 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1153 | } |
| 1154 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1155 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1156 | } |
| 1157 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1158 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1159 | } |
| 1160 | |
| 1161 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 1162 | // memory model. |
| 1163 | static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile, |
| 1164 | CodeGeneratorX86_64* codegen) { |
| 1165 | X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 1166 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1167 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1168 | CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1169 | |
| 1170 | if (type == Primitive::kPrimLong) { |
| 1171 | __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 1172 | } else { |
| 1173 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 1174 | } |
| 1175 | |
| 1176 | if (is_volatile) { |
| 1177 | __ mfence(); |
| 1178 | } |
| 1179 | |
| 1180 | if (type == Primitive::kPrimNot) { |
| 1181 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 1182 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 1183 | base, |
| 1184 | value); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 1189 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_); |
| 1190 | } |
| 1191 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1192 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_); |
| 1193 | } |
| 1194 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1195 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_); |
| 1196 | } |
| 1197 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 1198 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_); |
| 1199 | } |
| 1200 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1201 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_); |
| 1202 | } |
| 1203 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1204 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_); |
| 1205 | } |
| 1206 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 1207 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_); |
| 1208 | } |
| 1209 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1210 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_); |
| 1211 | } |
| 1212 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1213 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_); |
| 1214 | } |
| 1215 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1216 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type, |
| 1217 | HInvoke* invoke) { |
| 1218 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1219 | LocationSummary::kNoCall, |
| 1220 | kIntrinsified); |
| 1221 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1222 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1223 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1224 | // expected value must be in EAX/RAX. |
| 1225 | locations->SetInAt(3, Location::RegisterLocation(RAX)); |
| 1226 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1227 | |
| 1228 | locations->SetOut(Location::RequiresRegister()); |
| 1229 | if (type == Primitive::kPrimNot) { |
| 1230 | // Need temp registers for card-marking. |
| 1231 | locations->AddTemp(Location::RequiresRegister()); |
| 1232 | locations->AddTemp(Location::RequiresRegister()); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 1237 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke); |
| 1238 | } |
| 1239 | |
| 1240 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 1241 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke); |
| 1242 | } |
| 1243 | |
| 1244 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 1245 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke); |
| 1246 | } |
| 1247 | |
| 1248 | static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
| 1249 | X86_64Assembler* assembler = |
| 1250 | reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 1251 | LocationSummary* locations = invoke->GetLocations(); |
| 1252 | |
| 1253 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1254 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1255 | CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1256 | DCHECK_EQ(expected.AsRegister(), RAX); |
| 1257 | CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1258 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1259 | |
| 1260 | if (type == Primitive::kPrimLong) { |
| 1261 | __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value); |
| 1262 | } else { |
| 1263 | // Integer or object. |
| 1264 | if (type == Primitive::kPrimNot) { |
| 1265 | // Mark card for object assuming new value is stored. |
| 1266 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 1267 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 1268 | base, |
| 1269 | value); |
| 1270 | } |
| 1271 | |
| 1272 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value); |
| 1273 | } |
| 1274 | |
| 1275 | // locked cmpxchg has full barrier semantics, and we don't need scheduling |
| 1276 | // barriers at this time. |
| 1277 | |
| 1278 | // Convert ZF into the boolean result. |
| 1279 | __ setcc(kZero, out); |
| 1280 | __ movzxb(out, out); |
| 1281 | } |
| 1282 | |
| 1283 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 1284 | GenCAS(Primitive::kPrimInt, invoke, codegen_); |
| 1285 | } |
| 1286 | |
| 1287 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 1288 | GenCAS(Primitive::kPrimLong, invoke, codegen_); |
| 1289 | } |
| 1290 | |
| 1291 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 1292 | GenCAS(Primitive::kPrimNot, invoke, codegen_); |
| 1293 | } |
| 1294 | |
| 1295 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) { |
| 1296 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1297 | LocationSummary::kNoCall, |
| 1298 | kIntrinsified); |
| 1299 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1300 | locations->SetOut(Location::SameAsFirstInput()); |
| 1301 | locations->AddTemp(Location::RequiresRegister()); |
| 1302 | } |
| 1303 | |
| 1304 | static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask, |
| 1305 | X86_64Assembler* assembler) { |
| 1306 | Immediate imm_shift(shift); |
| 1307 | Immediate imm_mask(mask); |
| 1308 | __ movl(temp, reg); |
| 1309 | __ shrl(reg, imm_shift); |
| 1310 | __ andl(temp, imm_mask); |
| 1311 | __ andl(reg, imm_mask); |
| 1312 | __ shll(temp, imm_shift); |
| 1313 | __ orl(reg, temp); |
| 1314 | } |
| 1315 | |
| 1316 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) { |
| 1317 | X86_64Assembler* assembler = |
| 1318 | reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 1319 | LocationSummary* locations = invoke->GetLocations(); |
| 1320 | |
| 1321 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1322 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1323 | |
| 1324 | /* |
| 1325 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 1326 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 1327 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 1328 | * x = bswap x |
| 1329 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 1330 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 1331 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 1332 | */ |
| 1333 | __ bswapl(reg); |
| 1334 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 1335 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 1336 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 1337 | } |
| 1338 | |
| 1339 | void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) { |
| 1340 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1341 | LocationSummary::kNoCall, |
| 1342 | kIntrinsified); |
| 1343 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1344 | locations->SetOut(Location::SameAsFirstInput()); |
| 1345 | locations->AddTemp(Location::RequiresRegister()); |
| 1346 | locations->AddTemp(Location::RequiresRegister()); |
| 1347 | } |
| 1348 | |
| 1349 | static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask, |
| 1350 | int32_t shift, int64_t mask, X86_64Assembler* assembler) { |
| 1351 | Immediate imm_shift(shift); |
| 1352 | __ movq(temp_mask, Immediate(mask)); |
| 1353 | __ movq(temp, reg); |
| 1354 | __ shrq(reg, imm_shift); |
| 1355 | __ andq(temp, temp_mask); |
| 1356 | __ andq(reg, temp_mask); |
| 1357 | __ shlq(temp, imm_shift); |
| 1358 | __ orq(reg, temp); |
| 1359 | } |
| 1360 | |
| 1361 | void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) { |
| 1362 | X86_64Assembler* assembler = |
| 1363 | reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 1364 | LocationSummary* locations = invoke->GetLocations(); |
| 1365 | |
| 1366 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1367 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1368 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1369 | |
| 1370 | /* |
| 1371 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 1372 | * swapping bits to reverse bits in a long number x. Using bswap to save instructions |
| 1373 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 1374 | * x = bswap x |
| 1375 | * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555; |
| 1376 | * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333; |
| 1377 | * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F; |
| 1378 | */ |
| 1379 | __ bswapq(reg); |
| 1380 | SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler); |
| 1381 | SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler); |
| 1382 | SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler); |
| 1383 | } |
| 1384 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1385 | // Unimplemented intrinsics. |
| 1386 | |
| 1387 | #define UNIMPLEMENTED_INTRINSIC(Name) \ |
| 1388 | void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 1389 | } \ |
| 1390 | void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 1391 | } |
| 1392 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1393 | UNIMPLEMENTED_INTRINSIC(StringIndexOf) |
| 1394 | UNIMPLEMENTED_INTRINSIC(StringIndexOfAfter) |
| 1395 | UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1396 | UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent) |
| 1397 | |
| 1398 | } // namespace x86_64 |
| 1399 | } // namespace art |