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