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