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