| 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" |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 25 | #include "data_type-inl.h" |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 26 | #include "entrypoints/quick/quick_entrypoints.h" |
| Andreas Gampe | 09659c2 | 2017-09-18 18:23:32 -0700 | [diff] [blame] | 27 | #include "heap_poisoning.h" |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 28 | #include "intrinsics.h" |
| Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 29 | #include "intrinsics_utils.h" |
| Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 30 | #include "lock_word.h" |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 31 | #include "mirror/array-inl.h" |
| Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 32 | #include "mirror/object_array-inl.h" |
| Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 33 | #include "mirror/reference.h" |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 34 | #include "mirror/string.h" |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 35 | #include "mirror/var_handle.h" |
| Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 36 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 37 | #include "thread-current-inl.h" |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 38 | #include "utils/x86/assembler_x86.h" |
| 39 | #include "utils/x86/constants_x86.h" |
| 40 | |
| Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 41 | namespace art { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 42 | |
| 43 | namespace x86 { |
| 44 | |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 45 | IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen) |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 46 | : allocator_(codegen->GetGraph()->GetAllocator()), |
| Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 47 | codegen_(codegen) { |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 51 | X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() { |
| Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 52 | return down_cast<X86Assembler*>(codegen_->GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 56 | return codegen_->GetGraph()->GetAllocator(); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) { |
| 60 | Dispatch(invoke); |
| 61 | LocationSummary* res = invoke->GetLocations(); |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 62 | if (res == nullptr) { |
| 63 | return false; |
| 64 | } |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 65 | return res->Intrinsified(); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 68 | using IntrinsicSlowPathX86 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86>; |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 69 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 70 | // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. |
| 71 | #define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT |
| 72 | |
| 73 | // Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers. |
| 74 | class ReadBarrierSystemArrayCopySlowPathX86 : public SlowPathCode { |
| 75 | public: |
| 76 | explicit ReadBarrierSystemArrayCopySlowPathX86(HInstruction* instruction) |
| 77 | : SlowPathCode(instruction) { |
| 78 | DCHECK(kEmitCompilerReadBarrier); |
| 79 | DCHECK(kUseBakerReadBarrier); |
| 80 | } |
| 81 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 82 | void EmitNativeCode(CodeGenerator* codegen) override { |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 83 | CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen); |
| 84 | LocationSummary* locations = instruction_->GetLocations(); |
| 85 | DCHECK(locations->CanCall()); |
| 86 | DCHECK(instruction_->IsInvokeStaticOrDirect()) |
| 87 | << "Unexpected instruction in read barrier arraycopy slow path: " |
| 88 | << instruction_->DebugName(); |
| 89 | DCHECK(instruction_->GetLocations()->Intrinsified()); |
| 90 | DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy); |
| 91 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 92 | int32_t element_size = DataType::Size(DataType::Type::kReference); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 93 | uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value(); |
| 94 | |
| 95 | Register src = locations->InAt(0).AsRegister<Register>(); |
| 96 | Location src_pos = locations->InAt(1); |
| 97 | Register dest = locations->InAt(2).AsRegister<Register>(); |
| 98 | Location dest_pos = locations->InAt(3); |
| 99 | Location length = locations->InAt(4); |
| 100 | Location temp1_loc = locations->GetTemp(0); |
| 101 | Register temp1 = temp1_loc.AsRegister<Register>(); |
| 102 | Register temp2 = locations->GetTemp(1).AsRegister<Register>(); |
| 103 | Register temp3 = locations->GetTemp(2).AsRegister<Register>(); |
| 104 | |
| 105 | __ Bind(GetEntryLabel()); |
| 106 | // In this code path, registers `temp1`, `temp2`, and `temp3` |
| 107 | // (resp.) are not used for the base source address, the base |
| 108 | // destination address, and the end source address (resp.), as in |
| 109 | // other SystemArrayCopy intrinsic code paths. Instead they are |
| 110 | // (resp.) used for: |
| 111 | // - the loop index (`i`); |
| 112 | // - the source index (`src_index`) and the loaded (source) |
| 113 | // reference (`value`); and |
| 114 | // - the destination index (`dest_index`). |
| 115 | |
| 116 | // i = 0 |
| 117 | __ xorl(temp1, temp1); |
| 118 | NearLabel loop; |
| 119 | __ Bind(&loop); |
| 120 | // value = src_array[i + src_pos] |
| 121 | if (src_pos.IsConstant()) { |
| 122 | int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 123 | int32_t adjusted_offset = offset + constant * element_size; |
| 124 | __ movl(temp2, Address(src, temp1, ScaleFactor::TIMES_4, adjusted_offset)); |
| 125 | } else { |
| 126 | __ leal(temp2, Address(src_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0)); |
| 127 | __ movl(temp2, Address(src, temp2, ScaleFactor::TIMES_4, offset)); |
| 128 | } |
| 129 | __ MaybeUnpoisonHeapReference(temp2); |
| 130 | // TODO: Inline the mark bit check before calling the runtime? |
| 131 | // value = ReadBarrier::Mark(value) |
| 132 | // No need to save live registers; it's taken care of by the |
| 133 | // entrypoint. Also, there is no need to update the stack mask, |
| 134 | // as this runtime call will not trigger a garbage collection. |
| 135 | // (See ReadBarrierMarkSlowPathX86::EmitNativeCode for more |
| 136 | // explanations.) |
| 137 | DCHECK_NE(temp2, ESP); |
| 138 | DCHECK(0 <= temp2 && temp2 < kNumberOfCpuRegisters) << temp2; |
| Roland Levillain | 97c4646 | 2017-05-11 14:04:03 +0100 | [diff] [blame] | 139 | int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(temp2); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 140 | // This runtime call does not require a stack map. |
| 141 | x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this); |
| 142 | __ MaybePoisonHeapReference(temp2); |
| 143 | // dest_array[i + dest_pos] = value |
| 144 | if (dest_pos.IsConstant()) { |
| 145 | int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 146 | int32_t adjusted_offset = offset + constant * element_size; |
| 147 | __ movl(Address(dest, temp1, ScaleFactor::TIMES_4, adjusted_offset), temp2); |
| 148 | } else { |
| 149 | __ leal(temp3, Address(dest_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0)); |
| 150 | __ movl(Address(dest, temp3, ScaleFactor::TIMES_4, offset), temp2); |
| 151 | } |
| 152 | // ++i |
| 153 | __ addl(temp1, Immediate(1)); |
| 154 | // if (i != length) goto loop |
| 155 | x86_codegen->GenerateIntCompare(temp1_loc, length); |
| 156 | __ j(kNotEqual, &loop); |
| 157 | __ jmp(GetExitLabel()); |
| 158 | } |
| 159 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 160 | const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathX86"; } |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 161 | |
| 162 | private: |
| 163 | DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86); |
| 164 | }; |
| 165 | |
| 166 | #undef __ |
| 167 | |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 168 | #define __ assembler-> |
| 169 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 170 | static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) { |
| 171 | LocationSummary* locations = |
| 172 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 173 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 174 | locations->SetOut(Location::RequiresRegister()); |
| 175 | if (is64bit) { |
| 176 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 177 | } |
| 178 | } |
| 179 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 180 | static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) { |
| 181 | LocationSummary* locations = |
| 182 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 183 | locations->SetInAt(0, Location::RequiresRegister()); |
| 184 | locations->SetOut(Location::RequiresFpuRegister()); |
| 185 | if (is64bit) { |
| 186 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 187 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) { |
| 192 | Location input = locations->InAt(0); |
| 193 | Location output = locations->Out(); |
| 194 | if (is64bit) { |
| 195 | // Need to use the temporary. |
| 196 | XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 197 | __ movsd(temp, input.AsFpuRegister<XmmRegister>()); |
| 198 | __ movd(output.AsRegisterPairLow<Register>(), temp); |
| 199 | __ psrlq(temp, Immediate(32)); |
| 200 | __ movd(output.AsRegisterPairHigh<Register>(), temp); |
| 201 | } else { |
| 202 | __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>()); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) { |
| 207 | Location input = locations->InAt(0); |
| 208 | Location output = locations->Out(); |
| 209 | if (is64bit) { |
| 210 | // Need to use the temporary. |
| 211 | XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 212 | XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>(); |
| 213 | __ movd(temp1, input.AsRegisterPairLow<Register>()); |
| 214 | __ movd(temp2, input.AsRegisterPairHigh<Register>()); |
| 215 | __ punpckldq(temp1, temp2); |
| 216 | __ movsd(output.AsFpuRegister<XmmRegister>(), temp1); |
| 217 | } else { |
| 218 | __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 223 | CreateFPToIntLocations(allocator_, invoke, /* is64bit= */ true); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 224 | } |
| 225 | void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 226 | CreateIntToFPLocations(allocator_, invoke, /* is64bit= */ true); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 230 | MoveFPToInt(invoke->GetLocations(), /* is64bit= */ true, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 231 | } |
| 232 | void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 233 | MoveIntToFP(invoke->GetLocations(), /* is64bit= */ true, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 237 | CreateFPToIntLocations(allocator_, invoke, /* is64bit= */ false); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 238 | } |
| 239 | void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 240 | CreateIntToFPLocations(allocator_, invoke, /* is64bit= */ false); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 244 | MoveFPToInt(invoke->GetLocations(), /* is64bit= */ false, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 245 | } |
| 246 | void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 247 | MoveIntToFP(invoke->GetLocations(), /* is64bit= */ false, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 250 | static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 251 | LocationSummary* locations = |
| 252 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 253 | locations->SetInAt(0, Location::RequiresRegister()); |
| 254 | locations->SetOut(Location::SameAsFirstInput()); |
| 255 | } |
| 256 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 257 | static void CreateLongToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 258 | LocationSummary* locations = |
| 259 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 260 | locations->SetInAt(0, Location::RequiresRegister()); |
| 261 | locations->SetOut(Location::RequiresRegister()); |
| 262 | } |
| 263 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 264 | static void CreateLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 265 | LocationSummary* locations = |
| 266 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 267 | locations->SetInAt(0, Location::RequiresRegister()); |
| 268 | locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); |
| 269 | } |
| 270 | |
| 271 | static void GenReverseBytes(LocationSummary* locations, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 272 | DataType::Type size, |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 273 | X86Assembler* assembler) { |
| 274 | Register out = locations->Out().AsRegister<Register>(); |
| 275 | |
| 276 | switch (size) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 277 | case DataType::Type::kInt16: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 278 | // TODO: Can be done with an xchg of 8b registers. This is straight from Quick. |
| 279 | __ bswapl(out); |
| 280 | __ sarl(out, Immediate(16)); |
| 281 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 282 | case DataType::Type::kInt32: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 283 | __ bswapl(out); |
| 284 | break; |
| 285 | default: |
| 286 | LOG(FATAL) << "Unexpected size for reverse-bytes: " << size; |
| 287 | UNREACHABLE(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 292 | CreateIntToIntLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 296 | GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 299 | void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 300 | CreateLongToLongLocations(allocator_, invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) { |
| 304 | LocationSummary* locations = invoke->GetLocations(); |
| 305 | Location input = locations->InAt(0); |
| 306 | Register input_lo = input.AsRegisterPairLow<Register>(); |
| 307 | Register input_hi = input.AsRegisterPairHigh<Register>(); |
| 308 | Location output = locations->Out(); |
| 309 | Register output_lo = output.AsRegisterPairLow<Register>(); |
| 310 | Register output_hi = output.AsRegisterPairHigh<Register>(); |
| 311 | |
| 312 | X86Assembler* assembler = GetAssembler(); |
| 313 | // Assign the inputs to the outputs, mixing low/high. |
| 314 | __ movl(output_lo, input_hi); |
| 315 | __ movl(output_hi, input_lo); |
| 316 | __ bswapl(output_lo); |
| 317 | __ bswapl(output_hi); |
| 318 | } |
| 319 | |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 320 | void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 321 | CreateIntToIntLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 325 | GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 328 | static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 329 | LocationSummary* locations = |
| 330 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 331 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 332 | locations->SetOut(Location::RequiresFpuRegister()); |
| 333 | } |
| 334 | |
| 335 | void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 336 | CreateFPToFPLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) { |
| 340 | LocationSummary* locations = invoke->GetLocations(); |
| 341 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 342 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 343 | |
| 344 | GetAssembler()->sqrtsd(out, in); |
| 345 | } |
| 346 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 347 | static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator, |
| 348 | HInvoke* invoke, |
| 349 | CodeGeneratorX86* codegen) { |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 350 | // Do we have instruction support? |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 351 | if (!codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 355 | CreateFPToFPLocations(allocator, invoke); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 358 | static void GenSSE41FPToFPIntrinsic(HInvoke* invoke, X86Assembler* assembler, int round_mode) { |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 359 | LocationSummary* locations = invoke->GetLocations(); |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 360 | DCHECK(!locations->WillCall()); |
| 361 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 362 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 363 | __ roundsd(out, in, Immediate(round_mode)); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 367 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) { |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 371 | GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 2); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 375 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) { |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 379 | GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 1); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 383 | CreateSSE41FPToFPLocations(allocator_, invoke, codegen_); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) { |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 387 | GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 0); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 390 | void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) { |
| 391 | // Do we have instruction support? |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 392 | if (!codegen_->GetInstructionSetFeatures().HasSSE4_1()) { |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 396 | HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect(); |
| 397 | DCHECK(static_or_direct != nullptr); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 398 | LocationSummary* locations = |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 399 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| 400 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 401 | if (static_or_direct->HasSpecialInput() && |
| 402 | invoke->InputAt( |
| 403 | static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) { |
| 404 | locations->SetInAt(1, Location::RequiresRegister()); |
| 405 | } |
| 406 | locations->SetOut(Location::RequiresRegister()); |
| 407 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 408 | locations->AddTemp(Location::RequiresFpuRegister()); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) { |
| 412 | LocationSummary* locations = invoke->GetLocations(); |
| Vladimir Marko | 66704db | 2020-06-08 14:04:27 +0100 | [diff] [blame] | 413 | DCHECK(!locations->WillCall()); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 414 | |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 415 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 416 | XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 417 | XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>(); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 418 | Register out = locations->Out().AsRegister<Register>(); |
| Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 419 | NearLabel skip_incr, done; |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 420 | X86Assembler* assembler = GetAssembler(); |
| 421 | |
| Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 422 | // Since no direct x86 rounding instruction matches the required semantics, |
| 423 | // this intrinsic is implemented as follows: |
| 424 | // result = floor(in); |
| 425 | // if (in - result >= 0.5f) |
| 426 | // result = result + 1.0f; |
| 427 | __ movss(t2, in); |
| 428 | __ roundss(t1, in, Immediate(1)); |
| 429 | __ subss(t2, t1); |
| Aart Bik | 0cf8d9c | 2016-08-10 14:05:54 -0700 | [diff] [blame] | 430 | if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) { |
| 431 | // Direct constant area available. |
| Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 432 | HX86ComputeBaseMethodAddress* method_address = |
| 433 | invoke->InputAt(1)->AsX86ComputeBaseMethodAddress(); |
| Aart Bik | 0cf8d9c | 2016-08-10 14:05:54 -0700 | [diff] [blame] | 434 | Register constant_area = locations->InAt(1).AsRegister<Register>(); |
| Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 435 | __ comiss(t2, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(0.5f), |
| 436 | method_address, |
| 437 | constant_area)); |
| Aart Bik | 0cf8d9c | 2016-08-10 14:05:54 -0700 | [diff] [blame] | 438 | __ j(kBelow, &skip_incr); |
| Nicolas Geoffray | 133719e | 2017-01-22 15:44:39 +0000 | [diff] [blame] | 439 | __ addss(t1, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(1.0f), |
| 440 | method_address, |
| 441 | constant_area)); |
| Aart Bik | 0cf8d9c | 2016-08-10 14:05:54 -0700 | [diff] [blame] | 442 | __ Bind(&skip_incr); |
| 443 | } else { |
| 444 | // No constant area: go through stack. |
| 445 | __ pushl(Immediate(bit_cast<int32_t, float>(0.5f))); |
| 446 | __ pushl(Immediate(bit_cast<int32_t, float>(1.0f))); |
| 447 | __ comiss(t2, Address(ESP, 4)); |
| 448 | __ j(kBelow, &skip_incr); |
| 449 | __ addss(t1, Address(ESP, 0)); |
| 450 | __ Bind(&skip_incr); |
| 451 | __ addl(ESP, Immediate(8)); |
| 452 | } |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 453 | |
| Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 454 | // Final conversion to an integer. Unfortunately this also does not have a |
| 455 | // direct x86 instruction, since NaN should map to 0 and large positive |
| 456 | // values need to be clipped to the extreme value. |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 457 | __ movl(out, Immediate(kPrimIntMax)); |
| Aart Bik | 2c9f495 | 2016-08-01 16:52:27 -0700 | [diff] [blame] | 458 | __ cvtsi2ss(t2, out); |
| 459 | __ comiss(t1, t2); |
| 460 | __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered |
| 461 | __ movl(out, Immediate(0)); // does not change flags |
| 462 | __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out) |
| 463 | __ cvttss2si(out, t1); |
| Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 464 | __ Bind(&done); |
| 465 | } |
| 466 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 467 | static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 468 | LocationSummary* locations = |
| 469 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 470 | InvokeRuntimeCallingConvention calling_convention; |
| 471 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 472 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 473 | } |
| 474 | |
| 475 | static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) { |
| 476 | LocationSummary* locations = invoke->GetLocations(); |
| 477 | DCHECK(locations->WillCall()); |
| 478 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| 479 | X86Assembler* assembler = codegen->GetAssembler(); |
| 480 | |
| 481 | // We need some place to pass the parameters. |
| 482 | __ subl(ESP, Immediate(16)); |
| 483 | __ cfi().AdjustCFAOffset(16); |
| 484 | |
| 485 | // Pass the parameters at the bottom of the stack. |
| 486 | __ movsd(Address(ESP, 0), XMM0); |
| 487 | |
| 488 | // If we have a second parameter, pass it next. |
| 489 | if (invoke->GetNumberOfArguments() == 2) { |
| 490 | __ movsd(Address(ESP, 8), XMM1); |
| 491 | } |
| 492 | |
| 493 | // Now do the actual call. |
| Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 494 | codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc()); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 495 | |
| 496 | // Extract the return value from the FP stack. |
| 497 | __ fstpl(Address(ESP, 0)); |
| 498 | __ movsd(XMM0, Address(ESP, 0)); |
| 499 | |
| 500 | // And clean up the stack. |
| 501 | __ addl(ESP, Immediate(16)); |
| 502 | __ cfi().AdjustCFAOffset(-16); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| Shalini Salomi Bodapati | 8943fa1 | 2018-11-21 15:36:00 +0530 | [diff] [blame] | 505 | static void CreateLowestOneBitLocations(ArenaAllocator* allocator, bool is_long, HInvoke* invoke) { |
| 506 | LocationSummary* locations = |
| 507 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| 508 | if (is_long) { |
| 509 | locations->SetInAt(0, Location::RequiresRegister()); |
| 510 | } else { |
| 511 | locations->SetInAt(0, Location::Any()); |
| 512 | } |
| 513 | locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); |
| 514 | } |
| 515 | |
| 516 | static void GenLowestOneBit(X86Assembler* assembler, |
| 517 | CodeGeneratorX86* codegen, |
| 518 | bool is_long, |
| 519 | HInvoke* invoke) { |
| 520 | LocationSummary* locations = invoke->GetLocations(); |
| 521 | Location src = locations->InAt(0); |
| 522 | Location out_loc = locations->Out(); |
| 523 | |
| 524 | if (invoke->InputAt(0)->IsConstant()) { |
| 525 | // Evaluate this at compile time. |
| 526 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 527 | if (value == 0) { |
| 528 | if (is_long) { |
| 529 | __ xorl(out_loc.AsRegisterPairLow<Register>(), out_loc.AsRegisterPairLow<Register>()); |
| 530 | __ xorl(out_loc.AsRegisterPairHigh<Register>(), out_loc.AsRegisterPairHigh<Register>()); |
| 531 | } else { |
| 532 | __ xorl(out_loc.AsRegister<Register>(), out_loc.AsRegister<Register>()); |
| 533 | } |
| 534 | return; |
| 535 | } |
| 536 | // Nonzero value. |
| 537 | value = is_long ? CTZ(static_cast<uint64_t>(value)) |
| 538 | : CTZ(static_cast<uint32_t>(value)); |
| 539 | if (is_long) { |
| 540 | if (value >= 32) { |
| 541 | int shift = value-32; |
| 542 | codegen->Load32BitValue(out_loc.AsRegisterPairLow<Register>(), 0); |
| 543 | codegen->Load32BitValue(out_loc.AsRegisterPairHigh<Register>(), 1 << shift); |
| 544 | } else { |
| 545 | codegen->Load32BitValue(out_loc.AsRegisterPairLow<Register>(), 1 << value); |
| 546 | codegen->Load32BitValue(out_loc.AsRegisterPairHigh<Register>(), 0); |
| 547 | } |
| 548 | } else { |
| 549 | codegen->Load32BitValue(out_loc.AsRegister<Register>(), 1 << value); |
| 550 | } |
| 551 | return; |
| 552 | } |
| 553 | // Handle non constant case |
| 554 | if (is_long) { |
| 555 | DCHECK(src.IsRegisterPair()); |
| 556 | Register src_lo = src.AsRegisterPairLow<Register>(); |
| 557 | Register src_hi = src.AsRegisterPairHigh<Register>(); |
| 558 | |
| 559 | Register out_lo = out_loc.AsRegisterPairLow<Register>(); |
| 560 | Register out_hi = out_loc.AsRegisterPairHigh<Register>(); |
| 561 | |
| 562 | __ movl(out_lo, src_lo); |
| 563 | __ movl(out_hi, src_hi); |
| 564 | |
| 565 | __ negl(out_lo); |
| 566 | __ adcl(out_hi, Immediate(0)); |
| 567 | __ negl(out_hi); |
| 568 | |
| 569 | __ andl(out_lo, src_lo); |
| 570 | __ andl(out_hi, src_hi); |
| 571 | } else { |
| 572 | if (codegen->GetInstructionSetFeatures().HasAVX2() && src.IsRegister()) { |
| 573 | Register out = out_loc.AsRegister<Register>(); |
| 574 | __ blsi(out, src.AsRegister<Register>()); |
| 575 | } else { |
| 576 | Register out = out_loc.AsRegister<Register>(); |
| 577 | // Do tmp & -tmp |
| 578 | if (src.IsRegister()) { |
| 579 | __ movl(out, src.AsRegister<Register>()); |
| 580 | } else { |
| 581 | DCHECK(src.IsStackSlot()); |
| 582 | __ movl(out, Address(ESP, src.GetStackIndex())); |
| 583 | } |
| 584 | __ negl(out); |
| 585 | |
| 586 | if (src.IsRegister()) { |
| 587 | __ andl(out, src.AsRegister<Register>()); |
| 588 | } else { |
| 589 | __ andl(out, Address(ESP, src.GetStackIndex())); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 595 | void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 596 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) { |
| 600 | GenFPToFPCall(invoke, codegen_, kQuickCos); |
| 601 | } |
| 602 | |
| 603 | void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 604 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) { |
| 608 | GenFPToFPCall(invoke, codegen_, kQuickSin); |
| 609 | } |
| 610 | |
| 611 | void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 612 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) { |
| 616 | GenFPToFPCall(invoke, codegen_, kQuickAcos); |
| 617 | } |
| 618 | |
| 619 | void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 620 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) { |
| 624 | GenFPToFPCall(invoke, codegen_, kQuickAsin); |
| 625 | } |
| 626 | |
| 627 | void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 628 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) { |
| 632 | GenFPToFPCall(invoke, codegen_, kQuickAtan); |
| 633 | } |
| 634 | |
| 635 | void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 636 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) { |
| 640 | GenFPToFPCall(invoke, codegen_, kQuickCbrt); |
| 641 | } |
| 642 | |
| 643 | void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 644 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) { |
| 648 | GenFPToFPCall(invoke, codegen_, kQuickCosh); |
| 649 | } |
| 650 | |
| 651 | void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 652 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) { |
| 656 | GenFPToFPCall(invoke, codegen_, kQuickExp); |
| 657 | } |
| 658 | |
| 659 | void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 660 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) { |
| 664 | GenFPToFPCall(invoke, codegen_, kQuickExpm1); |
| 665 | } |
| 666 | |
| 667 | void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 668 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) { |
| 672 | GenFPToFPCall(invoke, codegen_, kQuickLog); |
| 673 | } |
| 674 | |
| 675 | void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 676 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) { |
| 680 | GenFPToFPCall(invoke, codegen_, kQuickLog10); |
| 681 | } |
| 682 | |
| 683 | void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 684 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) { |
| 688 | GenFPToFPCall(invoke, codegen_, kQuickSinh); |
| 689 | } |
| 690 | |
| 691 | void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 692 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) { |
| 696 | GenFPToFPCall(invoke, codegen_, kQuickTan); |
| 697 | } |
| 698 | |
| 699 | void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 700 | CreateFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) { |
| 704 | GenFPToFPCall(invoke, codegen_, kQuickTanh); |
| 705 | } |
| 706 | |
| Shalini Salomi Bodapati | 8943fa1 | 2018-11-21 15:36:00 +0530 | [diff] [blame] | 707 | void IntrinsicLocationsBuilderX86::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 708 | CreateLowestOneBitLocations(allocator_, /*is_long=*/ false, invoke); |
| 709 | } |
| 710 | void IntrinsicCodeGeneratorX86::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 711 | GenLowestOneBit(GetAssembler(), codegen_, /*is_long=*/ false, invoke); |
| 712 | } |
| 713 | |
| 714 | void IntrinsicLocationsBuilderX86::VisitLongLowestOneBit(HInvoke* invoke) { |
| 715 | CreateLowestOneBitLocations(allocator_, /*is_long=*/ true, invoke); |
| 716 | } |
| 717 | |
| 718 | void IntrinsicCodeGeneratorX86::VisitLongLowestOneBit(HInvoke* invoke) { |
| 719 | GenLowestOneBit(GetAssembler(), codegen_, /*is_long=*/ true, invoke); |
| 720 | } |
| 721 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 722 | static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 723 | LocationSummary* locations = |
| 724 | new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 725 | InvokeRuntimeCallingConvention calling_convention; |
| 726 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 727 | locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1))); |
| 728 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 729 | } |
| 730 | |
| Shalini Salomi Bodapati | 6545ee3 | 2021-11-02 20:01:06 +0530 | [diff] [blame] | 731 | static void CreateFPFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) { |
| 732 | DCHECK_EQ(invoke->GetNumberOfArguments(), 3U); |
| 733 | LocationSummary* locations = |
| 734 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| 735 | InvokeRuntimeCallingConvention calling_convention; |
| 736 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 737 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 738 | locations->SetInAt(2, Location::RequiresFpuRegister()); |
| 739 | locations->SetOut(Location::SameAsFirstInput()); |
| 740 | } |
| 741 | |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 742 | void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 743 | CreateFPFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) { |
| 747 | GenFPToFPCall(invoke, codegen_, kQuickAtan2); |
| 748 | } |
| 749 | |
| Vladimir Marko | 4d17987 | 2018-01-19 14:50:10 +0000 | [diff] [blame] | 750 | void IntrinsicLocationsBuilderX86::VisitMathPow(HInvoke* invoke) { |
| 751 | CreateFPFPToFPCallLocations(allocator_, invoke); |
| 752 | } |
| 753 | |
| 754 | void IntrinsicCodeGeneratorX86::VisitMathPow(HInvoke* invoke) { |
| 755 | GenFPToFPCall(invoke, codegen_, kQuickPow); |
| 756 | } |
| 757 | |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 758 | void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 759 | CreateFPFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) { |
| 763 | GenFPToFPCall(invoke, codegen_, kQuickHypot); |
| 764 | } |
| 765 | |
| 766 | void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 767 | CreateFPFPToFPCallLocations(allocator_, invoke); |
| Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) { |
| 771 | GenFPToFPCall(invoke, codegen_, kQuickNextAfter); |
| 772 | } |
| 773 | |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 774 | void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 775 | // We need at least two of the positions or length to be an integer constant, |
| 776 | // or else we won't have enough free registers. |
| 777 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 778 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 779 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 780 | |
| 781 | int num_constants = |
| 782 | ((src_pos != nullptr) ? 1 : 0) |
| 783 | + ((dest_pos != nullptr) ? 1 : 0) |
| 784 | + ((length != nullptr) ? 1 : 0); |
| 785 | |
| 786 | if (num_constants < 2) { |
| 787 | // Not enough free registers. |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | // As long as we are checking, we might as well check to see if the src and dest |
| 792 | // positions are >= 0. |
| 793 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 794 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 795 | // We will have to fail anyways. |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | // And since we are already checking, check the length too. |
| 800 | if (length != nullptr) { |
| 801 | int32_t len = length->GetValue(); |
| 802 | if (len < 0) { |
| 803 | // Just call as normal. |
| 804 | return; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // Okay, it is safe to generate inline code. |
| 809 | LocationSummary* locations = |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 810 | new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 811 | // arraycopy(Object src, int srcPos, Object dest, int destPos, int length). |
| 812 | locations->SetInAt(0, Location::RequiresRegister()); |
| 813 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 814 | locations->SetInAt(2, Location::RequiresRegister()); |
| 815 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 816 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 817 | |
| 818 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 819 | locations->AddTemp(Location::RegisterLocation(ESI)); |
| 820 | locations->AddTemp(Location::RegisterLocation(EDI)); |
| 821 | locations->AddTemp(Location::RegisterLocation(ECX)); |
| 822 | } |
| 823 | |
| 824 | static void CheckPosition(X86Assembler* assembler, |
| 825 | Location pos, |
| 826 | Register input, |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 827 | Location length, |
| Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 828 | SlowPathCode* slow_path, |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 829 | Register temp, |
| 830 | bool length_is_input_length = false) { |
| 831 | // Where is the length in the Array? |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 832 | const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 833 | |
| 834 | if (pos.IsConstant()) { |
| 835 | int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue(); |
| 836 | if (pos_const == 0) { |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 837 | if (!length_is_input_length) { |
| 838 | // Check that length(input) >= length. |
| 839 | if (length.IsConstant()) { |
| 840 | __ cmpl(Address(input, length_offset), |
| 841 | Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 842 | } else { |
| 843 | __ cmpl(Address(input, length_offset), length.AsRegister<Register>()); |
| 844 | } |
| 845 | __ j(kLess, slow_path->GetEntryLabel()); |
| 846 | } |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 847 | } else { |
| 848 | // Check that length(input) >= pos. |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 849 | __ movl(temp, Address(input, length_offset)); |
| 850 | __ subl(temp, Immediate(pos_const)); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 851 | __ j(kLess, slow_path->GetEntryLabel()); |
| 852 | |
| 853 | // Check that (length(input) - pos) >= length. |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 854 | if (length.IsConstant()) { |
| 855 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 856 | } else { |
| 857 | __ cmpl(temp, length.AsRegister<Register>()); |
| 858 | } |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 859 | __ j(kLess, slow_path->GetEntryLabel()); |
| 860 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 861 | } else if (length_is_input_length) { |
| 862 | // The only way the copy can succeed is if pos is zero. |
| 863 | Register pos_reg = pos.AsRegister<Register>(); |
| 864 | __ testl(pos_reg, pos_reg); |
| 865 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 866 | } else { |
| 867 | // Check that pos >= 0. |
| 868 | Register pos_reg = pos.AsRegister<Register>(); |
| 869 | __ testl(pos_reg, pos_reg); |
| 870 | __ j(kLess, slow_path->GetEntryLabel()); |
| 871 | |
| 872 | // Check that pos <= length(input). |
| 873 | __ cmpl(Address(input, length_offset), pos_reg); |
| 874 | __ j(kLess, slow_path->GetEntryLabel()); |
| 875 | |
| 876 | // Check that (length(input) - pos) >= length. |
| 877 | __ movl(temp, Address(input, length_offset)); |
| 878 | __ subl(temp, pos_reg); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 879 | if (length.IsConstant()) { |
| 880 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 881 | } else { |
| 882 | __ cmpl(temp, length.AsRegister<Register>()); |
| 883 | } |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 884 | __ j(kLess, slow_path->GetEntryLabel()); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 889 | X86Assembler* assembler = GetAssembler(); |
| 890 | LocationSummary* locations = invoke->GetLocations(); |
| 891 | |
| 892 | Register src = locations->InAt(0).AsRegister<Register>(); |
| 893 | Location srcPos = locations->InAt(1); |
| 894 | Register dest = locations->InAt(2).AsRegister<Register>(); |
| 895 | Location destPos = locations->InAt(3); |
| 896 | Location length = locations->InAt(4); |
| 897 | |
| 898 | // Temporaries that we need for MOVSW. |
| 899 | Register src_base = locations->GetTemp(0).AsRegister<Register>(); |
| 900 | DCHECK_EQ(src_base, ESI); |
| 901 | Register dest_base = locations->GetTemp(1).AsRegister<Register>(); |
| 902 | DCHECK_EQ(dest_base, EDI); |
| 903 | Register count = locations->GetTemp(2).AsRegister<Register>(); |
| 904 | DCHECK_EQ(count, ECX); |
| 905 | |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 906 | SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 907 | codegen_->AddSlowPath(slow_path); |
| 908 | |
| 909 | // Bail out if the source and destination are the same (to handle overlap). |
| 910 | __ cmpl(src, dest); |
| 911 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 912 | |
| 913 | // Bail out if the source is null. |
| 914 | __ testl(src, src); |
| 915 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 916 | |
| 917 | // Bail out if the destination is null. |
| 918 | __ testl(dest, dest); |
| 919 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 920 | |
| 921 | // If the length is negative, bail out. |
| 922 | // We have already checked in the LocationsBuilder for the constant case. |
| 923 | if (!length.IsConstant()) { |
| 924 | __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>()); |
| 925 | __ j(kLess, slow_path->GetEntryLabel()); |
| 926 | } |
| 927 | |
| 928 | // We need the count in ECX. |
| 929 | if (length.IsConstant()) { |
| 930 | __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 931 | } else { |
| 932 | __ movl(count, length.AsRegister<Register>()); |
| 933 | } |
| 934 | |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 935 | // Validity checks: source. Use src_base as a temporary register. |
| 936 | CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 937 | |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 938 | // Validity checks: dest. Use src_base as a temporary register. |
| 939 | CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 940 | |
| 941 | // Okay, everything checks out. Finally time to do the copy. |
| 942 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 943 | const size_t char_size = DataType::Size(DataType::Type::kUint16); |
| Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 944 | DCHECK_EQ(char_size, 2u); |
| 945 | |
| 946 | const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value(); |
| 947 | |
| 948 | if (srcPos.IsConstant()) { |
| 949 | int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue(); |
| 950 | __ leal(src_base, Address(src, char_size * srcPos_const + data_offset)); |
| 951 | } else { |
| 952 | __ leal(src_base, Address(src, srcPos.AsRegister<Register>(), |
| 953 | ScaleFactor::TIMES_2, data_offset)); |
| 954 | } |
| 955 | if (destPos.IsConstant()) { |
| 956 | int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue(); |
| 957 | |
| 958 | __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset)); |
| 959 | } else { |
| 960 | __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(), |
| 961 | ScaleFactor::TIMES_2, data_offset)); |
| 962 | } |
| 963 | |
| 964 | // Do the move. |
| 965 | __ rep_movsw(); |
| 966 | |
| 967 | __ Bind(slow_path->GetExitLabel()); |
| 968 | } |
| 969 | |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 970 | void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) { |
| 971 | // The inputs plus one temp. |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 972 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 973 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 974 | InvokeRuntimeCallingConvention calling_convention; |
| 975 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 976 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 977 | locations->SetOut(Location::RegisterLocation(EAX)); |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) { |
| 981 | X86Assembler* assembler = GetAssembler(); |
| 982 | LocationSummary* locations = invoke->GetLocations(); |
| 983 | |
| Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 984 | // Note that the null check must have been done earlier. |
| Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 985 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 986 | |
| 987 | Register argument = locations->InAt(1).AsRegister<Register>(); |
| 988 | __ testl(argument, argument); |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 989 | SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 990 | codegen_->AddSlowPath(slow_path); |
| 991 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 992 | |
| Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 993 | codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path); |
| Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 994 | __ Bind(slow_path->GetExitLabel()); |
| 995 | } |
| 996 | |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 997 | void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 998 | LocationSummary* locations = |
| 999 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1000 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1001 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1002 | |
| 1003 | // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction. |
| 1004 | locations->AddTemp(Location::RegisterLocation(ECX)); |
| 1005 | locations->AddTemp(Location::RegisterLocation(EDI)); |
| 1006 | |
| 1007 | // Set output, ESI needed for repe_cmpsl instruction anyways. |
| 1008 | locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap); |
| 1009 | } |
| 1010 | |
| 1011 | void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) { |
| 1012 | X86Assembler* assembler = GetAssembler(); |
| 1013 | LocationSummary* locations = invoke->GetLocations(); |
| 1014 | |
| 1015 | Register str = locations->InAt(0).AsRegister<Register>(); |
| 1016 | Register arg = locations->InAt(1).AsRegister<Register>(); |
| 1017 | Register ecx = locations->GetTemp(0).AsRegister<Register>(); |
| 1018 | Register edi = locations->GetTemp(1).AsRegister<Register>(); |
| 1019 | Register esi = locations->Out().AsRegister<Register>(); |
| 1020 | |
| Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1021 | NearLabel end, return_true, return_false; |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1022 | |
| 1023 | // Get offsets of count, value, and class fields within a string object. |
| 1024 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1025 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1026 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1027 | |
| 1028 | // Note that the null check must have been done earlier. |
| 1029 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1030 | |
| Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1031 | StringEqualsOptimizations optimizations(invoke); |
| 1032 | if (!optimizations.GetArgumentNotNull()) { |
| 1033 | // Check if input is null, return false if it is. |
| 1034 | __ testl(arg, arg); |
| 1035 | __ j(kEqual, &return_false); |
| 1036 | } |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1037 | |
| Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1038 | if (!optimizations.GetArgumentIsString()) { |
| Vladimir Marko | 53b5200 | 2016-05-24 19:30:45 +0100 | [diff] [blame] | 1039 | // Instanceof check for the argument by comparing class fields. |
| 1040 | // All string objects must have the same type since String cannot be subclassed. |
| 1041 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1042 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| Roland Levillain | 1d775d2 | 2018-09-07 13:56:57 +0100 | [diff] [blame] | 1043 | // |
| 1044 | // As the String class is expected to be non-movable, we can read the class |
| 1045 | // field from String.equals' arguments without read barriers. |
| 1046 | AssertNonMovableStringClass(); |
| 1047 | // Also, because we use the loaded class references only to compare them, we |
| 1048 | // don't need to unpoison them. |
| 1049 | // /* HeapReference<Class> */ ecx = str->klass_ |
| Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1050 | __ movl(ecx, Address(str, class_offset)); |
| Roland Levillain | 1d775d2 | 2018-09-07 13:56:57 +0100 | [diff] [blame] | 1051 | // if (ecx != /* HeapReference<Class> */ arg->klass_) return false |
| Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1052 | __ cmpl(ecx, Address(arg, class_offset)); |
| 1053 | __ j(kNotEqual, &return_false); |
| 1054 | } |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1055 | |
| 1056 | // Reference equality check, return true if same reference. |
| 1057 | __ cmpl(str, arg); |
| 1058 | __ j(kEqual, &return_true); |
| 1059 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1060 | // Load length and compression flag of receiver string. |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1061 | __ movl(ecx, Address(str, count_offset)); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1062 | // Check if lengths and compression flags are equal, return false if they're not. |
| 1063 | // Two identical strings will always have same compression style since |
| 1064 | // compression style is decided on alloc. |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1065 | __ cmpl(ecx, Address(arg, count_offset)); |
| 1066 | __ j(kNotEqual, &return_false); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1067 | // Return true if strings are empty. Even with string compression `count == 0` means empty. |
| 1068 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 1069 | "Expecting 0=compressed, 1=uncompressed"); |
| 1070 | __ jecxz(&return_true); |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1071 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1072 | if (mirror::kUseStringCompression) { |
| 1073 | NearLabel string_uncompressed; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1074 | // Extract length and differentiate between both compressed or both uncompressed. |
| 1075 | // Different compression style is cut above. |
| 1076 | __ shrl(ecx, Immediate(1)); |
| 1077 | __ j(kCarrySet, &string_uncompressed); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1078 | // Divide string length by 2, rounding up, and continue as if uncompressed. |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1079 | __ addl(ecx, Immediate(1)); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1080 | __ shrl(ecx, Immediate(1)); |
| 1081 | __ Bind(&string_uncompressed); |
| 1082 | } |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1083 | // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction. |
| 1084 | __ leal(esi, Address(str, value_offset)); |
| 1085 | __ leal(edi, Address(arg, value_offset)); |
| 1086 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1087 | // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not |
| 1088 | // divisible by 2. |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1089 | __ addl(ecx, Immediate(1)); |
| 1090 | __ shrl(ecx, Immediate(1)); |
| 1091 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1092 | // Assertions that must hold in order to compare strings 2 characters (uncompressed) |
| 1093 | // or 4 characters (compressed) at a time. |
| Agi Csaki | d7138c8 | 2015-08-13 17:46:44 -0700 | [diff] [blame] | 1094 | DCHECK_ALIGNED(value_offset, 4); |
| 1095 | static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded"); |
| 1096 | |
| 1097 | // Loop to compare strings two characters at a time starting at the beginning of the string. |
| 1098 | __ repe_cmpsl(); |
| 1099 | // If strings are not equal, zero flag will be cleared. |
| 1100 | __ j(kNotEqual, &return_false); |
| 1101 | |
| 1102 | // Return true and exit the function. |
| 1103 | // If loop does not result in returning false, we return true. |
| 1104 | __ Bind(&return_true); |
| 1105 | __ movl(esi, Immediate(1)); |
| 1106 | __ jmp(&end); |
| 1107 | |
| 1108 | // Return false and exit the function. |
| 1109 | __ Bind(&return_false); |
| 1110 | __ xorl(esi, esi); |
| 1111 | __ Bind(&end); |
| 1112 | } |
| 1113 | |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1114 | static void CreateStringIndexOfLocations(HInvoke* invoke, |
| 1115 | ArenaAllocator* allocator, |
| 1116 | bool start_at_zero) { |
| 1117 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1118 | LocationSummary::kCallOnSlowPath, |
| 1119 | kIntrinsified); |
| 1120 | // The data needs to be in EDI for scasw. So request that the string is there, anyways. |
| 1121 | locations->SetInAt(0, Location::RegisterLocation(EDI)); |
| 1122 | // If we look for a constant char, we'll still have to copy it into EAX. So just request the |
| 1123 | // allocator to do that, anyways. We can still do the constant check by checking the parameter |
| 1124 | // of the instruction explicitly. |
| 1125 | // Note: This works as we don't clobber EAX anywhere. |
| 1126 | locations->SetInAt(1, Location::RegisterLocation(EAX)); |
| 1127 | if (!start_at_zero) { |
| 1128 | locations->SetInAt(2, Location::RequiresRegister()); // The starting index. |
| 1129 | } |
| 1130 | // As we clobber EDI during execution anyways, also use it as the output. |
| 1131 | locations->SetOut(Location::SameAsFirstInput()); |
| 1132 | |
| 1133 | // repne scasw uses ECX as the counter. |
| 1134 | locations->AddTemp(Location::RegisterLocation(ECX)); |
| 1135 | // Need another temporary to be able to compute the result. |
| 1136 | locations->AddTemp(Location::RequiresRegister()); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1137 | if (mirror::kUseStringCompression) { |
| 1138 | // Need another temporary to be able to save unflagged string length. |
| 1139 | locations->AddTemp(Location::RequiresRegister()); |
| 1140 | } |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 1144 | X86Assembler* assembler, |
| 1145 | CodeGeneratorX86* codegen, |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1146 | bool start_at_zero) { |
| 1147 | LocationSummary* locations = invoke->GetLocations(); |
| 1148 | |
| 1149 | // Note that the null check must have been done earlier. |
| 1150 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1151 | |
| 1152 | Register string_obj = locations->InAt(0).AsRegister<Register>(); |
| 1153 | Register search_value = locations->InAt(1).AsRegister<Register>(); |
| 1154 | Register counter = locations->GetTemp(0).AsRegister<Register>(); |
| 1155 | Register string_length = locations->GetTemp(1).AsRegister<Register>(); |
| 1156 | Register out = locations->Out().AsRegister<Register>(); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1157 | // Only used when string compression feature is on. |
| 1158 | Register string_length_flagged; |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1159 | |
| 1160 | // Check our assumptions for registers. |
| 1161 | DCHECK_EQ(string_obj, EDI); |
| 1162 | DCHECK_EQ(search_value, EAX); |
| 1163 | DCHECK_EQ(counter, ECX); |
| 1164 | DCHECK_EQ(out, EDI); |
| 1165 | |
| 1166 | // 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] | 1167 | // 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] | 1168 | SlowPathCode* slow_path = nullptr; |
| Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 1169 | HInstruction* code_point = invoke->InputAt(1); |
| 1170 | if (code_point->IsIntConstant()) { |
| Vladimir Marko | da05108 | 2016-05-17 16:10:20 +0100 | [diff] [blame] | 1171 | if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1172 | std::numeric_limits<uint16_t>::max()) { |
| 1173 | // Always needs the slow-path. We could directly dispatch to it, but this case should be |
| 1174 | // 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] | 1175 | slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1176 | codegen->AddSlowPath(slow_path); |
| 1177 | __ jmp(slow_path->GetEntryLabel()); |
| 1178 | __ Bind(slow_path->GetExitLabel()); |
| 1179 | return; |
| 1180 | } |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1181 | } else if (code_point->GetType() != DataType::Type::kUint16) { |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1182 | __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max())); |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 1183 | slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1184 | codegen->AddSlowPath(slow_path); |
| 1185 | __ j(kAbove, slow_path->GetEntryLabel()); |
| 1186 | } |
| 1187 | |
| 1188 | // From here down, we know that we are looking for a char that fits in 16 bits. |
| 1189 | // Location of reference to data array within the String object. |
| 1190 | int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1191 | // Location of count within the String object. |
| 1192 | int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1193 | |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1194 | // 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] | 1195 | __ movl(string_length, Address(string_obj, count_offset)); |
| 1196 | |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1197 | // Do a zero-length check. Even with string compression `count == 0` means empty. |
| 1198 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 1199 | "Expecting 0=compressed, 1=uncompressed"); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1200 | // TODO: Support jecxz. |
| Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1201 | NearLabel not_found_label; |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1202 | __ testl(string_length, string_length); |
| 1203 | __ j(kEqual, ¬_found_label); |
| 1204 | |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1205 | if (mirror::kUseStringCompression) { |
| 1206 | string_length_flagged = locations->GetTemp(2).AsRegister<Register>(); |
| 1207 | __ movl(string_length_flagged, string_length); |
| 1208 | // Extract the length and shift out the least significant bit used as compression flag. |
| 1209 | __ shrl(string_length, Immediate(1)); |
| 1210 | } |
| 1211 | |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1212 | if (start_at_zero) { |
| 1213 | // Number of chars to scan is the same as the string length. |
| 1214 | __ movl(counter, string_length); |
| 1215 | |
| 1216 | // Move to the start of the string. |
| 1217 | __ addl(string_obj, Immediate(value_offset)); |
| 1218 | } else { |
| 1219 | Register start_index = locations->InAt(2).AsRegister<Register>(); |
| 1220 | |
| 1221 | // Do a start_index check. |
| 1222 | __ cmpl(start_index, string_length); |
| 1223 | __ j(kGreaterEqual, ¬_found_label); |
| 1224 | |
| 1225 | // Ensure we have a start index >= 0; |
| 1226 | __ xorl(counter, counter); |
| 1227 | __ cmpl(start_index, Immediate(0)); |
| 1228 | __ cmovl(kGreater, counter, start_index); |
| 1229 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1230 | if (mirror::kUseStringCompression) { |
| 1231 | NearLabel modify_counter, offset_uncompressed_label; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1232 | __ testl(string_length_flagged, Immediate(1)); |
| 1233 | __ j(kNotZero, &offset_uncompressed_label); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1234 | // Move to the start of the string: string_obj + value_offset + start_index. |
| 1235 | __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset)); |
| 1236 | __ jmp(&modify_counter); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1237 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1238 | // Move to the start of the string: string_obj + value_offset + 2 * start_index. |
| 1239 | __ Bind(&offset_uncompressed_label); |
| 1240 | __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1241 | |
| 1242 | // Now update ecx (the repne scasw work counter). We have string.length - start_index left to |
| 1243 | // compare. |
| 1244 | __ Bind(&modify_counter); |
| 1245 | } else { |
| 1246 | __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1247 | } |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1248 | __ negl(counter); |
| 1249 | __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0)); |
| 1250 | } |
| 1251 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1252 | if (mirror::kUseStringCompression) { |
| 1253 | NearLabel uncompressed_string_comparison; |
| 1254 | NearLabel comparison_done; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1255 | __ testl(string_length_flagged, Immediate(1)); |
| 1256 | __ j(kNotZero, &uncompressed_string_comparison); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1257 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1258 | // Check if EAX (search_value) is ASCII. |
| 1259 | __ cmpl(search_value, Immediate(127)); |
| 1260 | __ j(kGreater, ¬_found_label); |
| 1261 | // Comparing byte-per-byte. |
| 1262 | __ repne_scasb(); |
| 1263 | __ jmp(&comparison_done); |
| 1264 | |
| 1265 | // Everything is set up for repne scasw: |
| 1266 | // * Comparison address in EDI. |
| 1267 | // * Counter in ECX. |
| 1268 | __ Bind(&uncompressed_string_comparison); |
| 1269 | __ repne_scasw(); |
| 1270 | __ Bind(&comparison_done); |
| 1271 | } else { |
| 1272 | __ repne_scasw(); |
| 1273 | } |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1274 | // Did we find a match? |
| 1275 | __ j(kNotEqual, ¬_found_label); |
| 1276 | |
| 1277 | // Yes, we matched. Compute the index of the result. |
| 1278 | __ subl(string_length, counter); |
| 1279 | __ leal(out, Address(string_length, -1)); |
| 1280 | |
| Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1281 | NearLabel done; |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1282 | __ jmp(&done); |
| 1283 | |
| 1284 | // Failed to match; return -1. |
| 1285 | __ Bind(¬_found_label); |
| 1286 | __ movl(out, Immediate(-1)); |
| 1287 | |
| 1288 | // And join up at the end. |
| 1289 | __ Bind(&done); |
| 1290 | if (slow_path != nullptr) { |
| 1291 | __ Bind(slow_path->GetExitLabel()); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 1296 | CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ true); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 1300 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ true); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 1304 | CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ false); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 1308 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ false); |
| Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1311 | void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1312 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 1313 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1314 | InvokeRuntimeCallingConvention calling_convention; |
| 1315 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1316 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1317 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1318 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 1319 | locations->SetOut(Location::RegisterLocation(EAX)); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1323 | X86Assembler* assembler = GetAssembler(); |
| 1324 | LocationSummary* locations = invoke->GetLocations(); |
| 1325 | |
| 1326 | Register byte_array = locations->InAt(0).AsRegister<Register>(); |
| 1327 | __ testl(byte_array, byte_array); |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 1328 | SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1329 | codegen_->AddSlowPath(slow_path); |
| 1330 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1331 | |
| Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1332 | codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc()); |
| Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1333 | CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>(); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1334 | __ Bind(slow_path->GetExitLabel()); |
| 1335 | } |
| 1336 | |
| 1337 | void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1338 | LocationSummary* locations = |
| 1339 | new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1340 | InvokeRuntimeCallingConvention calling_convention; |
| 1341 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1342 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1343 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1344 | locations->SetOut(Location::RegisterLocation(EAX)); |
| 1345 | } |
| 1346 | |
| 1347 | void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) { |
| Roland Levillain | cc3839c | 2016-02-29 16:23:48 +0000 | [diff] [blame] | 1348 | // No need to emit code checking whether `locations->InAt(2)` is a null |
| 1349 | // pointer, as callers of the native method |
| 1350 | // |
| 1351 | // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data) |
| 1352 | // |
| 1353 | // all include a null check on `data` before calling that method. |
| Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1354 | codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc()); |
| Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1355 | CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>(); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1359 | LocationSummary* locations = new (allocator_) LocationSummary( |
| 1360 | invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1361 | InvokeRuntimeCallingConvention calling_convention; |
| 1362 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1363 | locations->SetOut(Location::RegisterLocation(EAX)); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1367 | X86Assembler* assembler = GetAssembler(); |
| 1368 | LocationSummary* locations = invoke->GetLocations(); |
| 1369 | |
| 1370 | Register string_to_copy = locations->InAt(0).AsRegister<Register>(); |
| 1371 | __ testl(string_to_copy, string_to_copy); |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 1372 | SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1373 | codegen_->AddSlowPath(slow_path); |
| 1374 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1375 | |
| Serban Constantinescu | ba45db0 | 2016-07-12 22:53:02 +0100 | [diff] [blame] | 1376 | codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc()); |
| Roland Levillain | f969a20 | 2016-03-09 16:14:00 +0000 | [diff] [blame] | 1377 | CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>(); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1378 | __ Bind(slow_path->GetExitLabel()); |
| 1379 | } |
| 1380 | |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1381 | void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1382 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1383 | LocationSummary* locations = |
| 1384 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1385 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1386 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1387 | // Place srcEnd in ECX to save a move below. |
| 1388 | locations->SetInAt(2, Location::RegisterLocation(ECX)); |
| 1389 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1390 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1391 | |
| 1392 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 1393 | // We don't have enough registers to also grab ECX, so handle below. |
| 1394 | locations->AddTemp(Location::RegisterLocation(ESI)); |
| 1395 | locations->AddTemp(Location::RegisterLocation(EDI)); |
| 1396 | } |
| 1397 | |
| 1398 | void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1399 | X86Assembler* assembler = GetAssembler(); |
| 1400 | LocationSummary* locations = invoke->GetLocations(); |
| 1401 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1402 | size_t char_component_size = DataType::Size(DataType::Type::kUint16); |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1403 | // Location of data in char array buffer. |
| 1404 | const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value(); |
| 1405 | // Location of char array data in string. |
| 1406 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1407 | |
| 1408 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1409 | Register obj = locations->InAt(0).AsRegister<Register>(); |
| 1410 | Location srcBegin = locations->InAt(1); |
| 1411 | int srcBegin_value = |
| 1412 | srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0; |
| 1413 | Register srcEnd = locations->InAt(2).AsRegister<Register>(); |
| 1414 | Register dst = locations->InAt(3).AsRegister<Register>(); |
| 1415 | Register dstBegin = locations->InAt(4).AsRegister<Register>(); |
| 1416 | |
| 1417 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1418 | const size_t char_size = DataType::Size(DataType::Type::kUint16); |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1419 | DCHECK_EQ(char_size, 2u); |
| 1420 | |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1421 | // Compute the number of chars (words) to move. |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1422 | // 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] | 1423 | __ pushl(ECX); |
| 1424 | int stack_adjust = kX86WordSize; |
| 1425 | __ cfi().AdjustCFAOffset(stack_adjust); |
| 1426 | DCHECK_EQ(srcEnd, ECX); |
| 1427 | if (srcBegin.IsConstant()) { |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1428 | __ subl(ECX, Immediate(srcBegin_value)); |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1429 | } else { |
| 1430 | DCHECK(srcBegin.IsRegister()); |
| 1431 | __ subl(ECX, srcBegin.AsRegister<Register>()); |
| 1432 | } |
| 1433 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1434 | NearLabel done; |
| 1435 | if (mirror::kUseStringCompression) { |
| 1436 | // Location of count in string |
| 1437 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1438 | const size_t c_char_size = DataType::Size(DataType::Type::kInt8); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1439 | DCHECK_EQ(c_char_size, 1u); |
| 1440 | __ pushl(EAX); |
| 1441 | __ cfi().AdjustCFAOffset(stack_adjust); |
| 1442 | |
| 1443 | NearLabel copy_loop, copy_uncompressed; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 1444 | __ testl(Address(obj, count_offset), Immediate(1)); |
| 1445 | static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u, |
| 1446 | "Expecting 0=compressed, 1=uncompressed"); |
| 1447 | __ j(kNotZero, ©_uncompressed); |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1448 | // Compute the address of the source string by adding the number of chars from |
| 1449 | // the source beginning to the value offset of a string. |
| 1450 | __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset)); |
| 1451 | |
| 1452 | // Start the loop to copy String's value to Array of Char. |
| 1453 | __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1454 | __ Bind(©_loop); |
| 1455 | __ jecxz(&done); |
| 1456 | // Use EAX temporary (convert byte from ESI to word). |
| 1457 | // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0. |
| 1458 | __ movzxb(EAX, Address(ESI, 0)); |
| 1459 | __ movw(Address(EDI, 0), EAX); |
| 1460 | __ leal(EDI, Address(EDI, char_size)); |
| 1461 | __ leal(ESI, Address(ESI, c_char_size)); |
| 1462 | // TODO: Add support for LOOP to X86Assembler. |
| 1463 | __ subl(ECX, Immediate(1)); |
| 1464 | __ jmp(©_loop); |
| 1465 | __ Bind(©_uncompressed); |
| 1466 | } |
| 1467 | |
| 1468 | // Do the copy for uncompressed string. |
| 1469 | // Compute the address of the destination buffer. |
| 1470 | __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1471 | __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset)); |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1472 | __ rep_movsw(); |
| 1473 | |
| jessicahandojo | 4877b79 | 2016-09-08 19:49:13 -0700 | [diff] [blame] | 1474 | __ Bind(&done); |
| 1475 | if (mirror::kUseStringCompression) { |
| 1476 | // Restore EAX. |
| 1477 | __ popl(EAX); |
| 1478 | __ cfi().AdjustCFAOffset(-stack_adjust); |
| 1479 | } |
| 1480 | // Restore ECX. |
| Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1481 | __ popl(ECX); |
| 1482 | __ cfi().AdjustCFAOffset(-stack_adjust); |
| 1483 | } |
| 1484 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1485 | static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1486 | Register address = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 1487 | Location out_loc = locations->Out(); |
| 1488 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1489 | // to avoid a SIGBUS. |
| 1490 | switch (size) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1491 | case DataType::Type::kInt8: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1492 | __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0)); |
| 1493 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1494 | case DataType::Type::kInt16: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1495 | __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0)); |
| 1496 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1497 | case DataType::Type::kInt32: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1498 | __ movl(out_loc.AsRegister<Register>(), Address(address, 0)); |
| 1499 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1500 | case DataType::Type::kInt64: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1501 | __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0)); |
| 1502 | __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4)); |
| 1503 | break; |
| 1504 | default: |
| 1505 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 1506 | UNREACHABLE(); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1511 | CreateLongToIntLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1515 | GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1519 | CreateLongToIntLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1523 | GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1527 | CreateLongToLongLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1531 | GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1535 | CreateLongToIntLocations(allocator_, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1539 | GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1542 | static void CreateLongIntToVoidLocations(ArenaAllocator* allocator, |
| 1543 | DataType::Type size, |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1544 | HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1545 | LocationSummary* locations = |
| 1546 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1547 | locations->SetInAt(0, Location::RequiresRegister()); |
| Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1548 | HInstruction* value = invoke->InputAt(1); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1549 | if (size == DataType::Type::kInt8) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1550 | locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value)); |
| 1551 | } else { |
| 1552 | locations->SetInAt(1, Location::RegisterOrConstant(value)); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1556 | static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1557 | Register address = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 1558 | Location value_loc = locations->InAt(1); |
| 1559 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1560 | // to avoid a SIGBUS. |
| 1561 | switch (size) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1562 | case DataType::Type::kInt8: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1563 | if (value_loc.IsConstant()) { |
| 1564 | __ movb(Address(address, 0), |
| 1565 | Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue())); |
| 1566 | } else { |
| 1567 | __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>()); |
| 1568 | } |
| 1569 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1570 | case DataType::Type::kInt16: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1571 | if (value_loc.IsConstant()) { |
| 1572 | __ movw(Address(address, 0), |
| 1573 | Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue())); |
| 1574 | } else { |
| 1575 | __ movw(Address(address, 0), value_loc.AsRegister<Register>()); |
| 1576 | } |
| 1577 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1578 | case DataType::Type::kInt32: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1579 | if (value_loc.IsConstant()) { |
| 1580 | __ movl(Address(address, 0), |
| 1581 | Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue())); |
| 1582 | } else { |
| 1583 | __ movl(Address(address, 0), value_loc.AsRegister<Register>()); |
| 1584 | } |
| 1585 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1586 | case DataType::Type::kInt64: |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1587 | if (value_loc.IsConstant()) { |
| 1588 | int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue(); |
| 1589 | __ movl(Address(address, 0), Immediate(Low32Bits(value))); |
| 1590 | __ movl(Address(address, 4), Immediate(High32Bits(value))); |
| 1591 | } else { |
| 1592 | __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>()); |
| 1593 | __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>()); |
| 1594 | } |
| 1595 | break; |
| 1596 | default: |
| 1597 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 1598 | UNREACHABLE(); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1603 | CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1607 | GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1611 | CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1615 | GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1619 | CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1623 | GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1627 | CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1631 | GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1635 | LocationSummary* locations = |
| 1636 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1637 | locations->SetOut(Location::RequiresRegister()); |
| 1638 | } |
| 1639 | |
| 1640 | void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1641 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1642 | GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>())); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1645 | static void GenUnsafeGet(HInvoke* invoke, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1646 | DataType::Type type, |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1647 | bool is_volatile, |
| 1648 | CodeGeneratorX86* codegen) { |
| 1649 | X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler()); |
| 1650 | LocationSummary* locations = invoke->GetLocations(); |
| 1651 | Location base_loc = locations->InAt(1); |
| 1652 | Register base = base_loc.AsRegister<Register>(); |
| 1653 | Location offset_loc = locations->InAt(2); |
| 1654 | Register offset = offset_loc.AsRegisterPairLow<Register>(); |
| 1655 | Location output_loc = locations->Out(); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1656 | |
| 1657 | switch (type) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1658 | case DataType::Type::kInt32: { |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1659 | Register output = output_loc.AsRegister<Register>(); |
| 1660 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1664 | case DataType::Type::kReference: { |
| Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 1665 | Register output = output_loc.AsRegister<Register>(); |
| 1666 | if (kEmitCompilerReadBarrier) { |
| 1667 | if (kUseBakerReadBarrier) { |
| Sang, Chunlei | 0fcd2b8 | 2016-04-05 17:12:59 +0800 | [diff] [blame] | 1668 | Address src(base, offset, ScaleFactor::TIMES_1, 0); |
| 1669 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 1670 | invoke, output_loc, base, src, /* needs_null_check= */ false); |
| Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 1671 | } else { |
| 1672 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1673 | codegen->GenerateReadBarrierSlow( |
| 1674 | invoke, output_loc, output_loc, base_loc, 0U, offset_loc); |
| 1675 | } |
| 1676 | } else { |
| 1677 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1678 | __ MaybeUnpoisonHeapReference(output); |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1679 | } |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1680 | break; |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1681 | } |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1682 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1683 | case DataType::Type::kInt64: { |
| Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1684 | Register output_lo = output_loc.AsRegisterPairLow<Register>(); |
| 1685 | Register output_hi = output_loc.AsRegisterPairHigh<Register>(); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1686 | if (is_volatile) { |
| 1687 | // Need to use a XMM to read atomically. |
| 1688 | XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 1689 | __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1690 | __ movd(output_lo, temp); |
| 1691 | __ psrlq(temp, Immediate(32)); |
| 1692 | __ movd(output_hi, temp); |
| 1693 | } else { |
| 1694 | __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
| 1695 | __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4)); |
| 1696 | } |
| 1697 | } |
| 1698 | break; |
| 1699 | |
| 1700 | default: |
| 1701 | LOG(FATAL) << "Unsupported op size " << type; |
| 1702 | UNREACHABLE(); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1706 | static bool UnsafeGetIntrinsicOnCallList(Intrinsics intrinsic) { |
| 1707 | switch (intrinsic) { |
| 1708 | case Intrinsics::kUnsafeGetObject: |
| 1709 | case Intrinsics::kUnsafeGetObjectVolatile: |
| 1710 | case Intrinsics::kJdkUnsafeGetObject: |
| 1711 | case Intrinsics::kJdkUnsafeGetObjectVolatile: |
| 1712 | case Intrinsics::kJdkUnsafeGetObjectAcquire: |
| 1713 | return true; |
| 1714 | default: |
| 1715 | break; |
| 1716 | } |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1720 | static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, |
| Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 1721 | HInvoke* invoke, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1722 | DataType::Type type, |
| Roland Levillain | 7c1559a | 2015-12-15 10:55:36 +0000 | [diff] [blame] | 1723 | bool is_volatile) { |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1724 | bool can_call = kEmitCompilerReadBarrier && UnsafeGetIntrinsicOnCallList(invoke->GetIntrinsic()); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1725 | LocationSummary* locations = |
| 1726 | new (allocator) LocationSummary(invoke, |
| 1727 | can_call |
| 1728 | ? LocationSummary::kCallOnSlowPath |
| 1729 | : LocationSummary::kNoCall, |
| 1730 | kIntrinsified); |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 1731 | if (can_call && kUseBakerReadBarrier) { |
| Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 1732 | locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 1733 | } |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1734 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1735 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1736 | locations->SetInAt(2, Location::RequiresRegister()); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1737 | if (type == DataType::Type::kInt64) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1738 | if (is_volatile) { |
| 1739 | // Need to use XMM to read volatile. |
| 1740 | locations->AddTemp(Location::RequiresFpuRegister()); |
| Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 1741 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1742 | } else { |
| 1743 | locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); |
| 1744 | } |
| 1745 | } else { |
| Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 1746 | locations->SetOut(Location::RequiresRegister(), |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 1747 | (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap)); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1752 | VisitJdkUnsafeGet(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1753 | } |
| 1754 | void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1755 | VisitJdkUnsafeGetVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1756 | } |
| 1757 | void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1758 | VisitJdkUnsafeGetLong(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1759 | } |
| 1760 | void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1761 | VisitJdkUnsafeGetLongVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1762 | } |
| 1763 | void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1764 | VisitJdkUnsafeGetObject(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1765 | } |
| 1766 | void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1767 | VisitJdkUnsafeGetObjectVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | |
| 1771 | void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1772 | VisitJdkUnsafeGet(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1773 | } |
| 1774 | void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1775 | VisitJdkUnsafeGetVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1776 | } |
| 1777 | void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1778 | VisitJdkUnsafeGetLong(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1779 | } |
| 1780 | void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1781 | VisitJdkUnsafeGetLongVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1782 | } |
| 1783 | void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1784 | VisitJdkUnsafeGetObject(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1785 | } |
| 1786 | void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1787 | VisitJdkUnsafeGetObjectVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1791 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGet(HInvoke* invoke) { |
| 1792 | CreateIntIntIntToIntLocations( |
| 1793 | allocator_, invoke, DataType::Type::kInt32, /*is_volatile=*/ false); |
| 1794 | } |
| 1795 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetVolatile(HInvoke* invoke) { |
| 1796 | CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /*is_volatile=*/ true); |
| 1797 | } |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 1798 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetAcquire(HInvoke* invoke) { |
| 1799 | CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /*is_volatile=*/ true); |
| 1800 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1801 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetLong(HInvoke* invoke) { |
| 1802 | CreateIntIntIntToIntLocations( |
| 1803 | allocator_, invoke, DataType::Type::kInt64, /*is_volatile=*/ false); |
| 1804 | } |
| 1805 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1806 | CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /*is_volatile=*/ true); |
| 1807 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1808 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetLongAcquire(HInvoke* invoke) { |
| 1809 | CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /*is_volatile=*/ true); |
| 1810 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1811 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetObject(HInvoke* invoke) { |
| 1812 | CreateIntIntIntToIntLocations( |
| 1813 | allocator_, invoke, DataType::Type::kReference, /*is_volatile=*/ false); |
| 1814 | } |
| 1815 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1816 | CreateIntIntIntToIntLocations( |
| 1817 | allocator_, invoke, DataType::Type::kReference, /*is_volatile=*/ true); |
| 1818 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1819 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeGetObjectAcquire(HInvoke* invoke) { |
| 1820 | CreateIntIntIntToIntLocations( |
| 1821 | allocator_, invoke, DataType::Type::kReference, /*is_volatile=*/ true); |
| 1822 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1823 | |
| 1824 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGet(HInvoke* invoke) { |
| 1825 | GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ false, codegen_); |
| 1826 | } |
| 1827 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetVolatile(HInvoke* invoke) { |
| 1828 | GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ true, codegen_); |
| 1829 | } |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 1830 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetAcquire(HInvoke* invoke) { |
| 1831 | GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ true, codegen_); |
| 1832 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1833 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetLong(HInvoke* invoke) { |
| 1834 | GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ false, codegen_); |
| 1835 | } |
| 1836 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1837 | GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ true, codegen_); |
| 1838 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1839 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetLongAcquire(HInvoke* invoke) { |
| 1840 | GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ true, codegen_); |
| 1841 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1842 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetObject(HInvoke* invoke) { |
| 1843 | GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ false, codegen_); |
| 1844 | } |
| 1845 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1846 | GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ true, codegen_); |
| 1847 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1848 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeGetObjectAcquire(HInvoke* invoke) { |
| 1849 | GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ true, codegen_); |
| 1850 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1851 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1852 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1853 | DataType::Type type, |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1854 | HInvoke* invoke, |
| 1855 | bool is_volatile) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1856 | LocationSummary* locations = |
| 1857 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1858 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1859 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1860 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1861 | locations->SetInAt(3, Location::RequiresRegister()); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1862 | if (type == DataType::Type::kReference) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1863 | // Need temp registers for card-marking. |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1864 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1865 | // Ensure the value is in a byte register. |
| 1866 | locations->AddTemp(Location::RegisterLocation(ECX)); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1867 | } else if (type == DataType::Type::kInt64 && is_volatile) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1868 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 1869 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1874 | VisitJdkUnsafePut(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1875 | } |
| 1876 | void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1877 | VisitJdkUnsafePutOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1878 | } |
| 1879 | void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1880 | VisitJdkUnsafePutVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1881 | } |
| 1882 | void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1883 | VisitJdkUnsafePutObject(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1884 | } |
| 1885 | void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1886 | VisitJdkUnsafePutObjectOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1887 | } |
| 1888 | void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1889 | VisitJdkUnsafePutObjectVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1890 | } |
| 1891 | void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1892 | VisitJdkUnsafePutLong(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1893 | } |
| 1894 | void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1895 | VisitJdkUnsafePutLongOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1896 | } |
| 1897 | void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1898 | VisitJdkUnsafePutLongVolatile(invoke); |
| 1899 | } |
| 1900 | |
| 1901 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePut(HInvoke* invoke) { |
| Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1902 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1903 | allocator_, DataType::Type::kInt32, invoke, /*is_volatile=*/ false); |
| 1904 | } |
| 1905 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutOrdered(HInvoke* invoke) { |
| 1906 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1907 | allocator_, DataType::Type::kInt32, invoke, /*is_volatile=*/ false); |
| 1908 | } |
| 1909 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutVolatile(HInvoke* invoke) { |
| 1910 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1911 | allocator_, DataType::Type::kInt32, invoke, /*is_volatile=*/ true); |
| 1912 | } |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 1913 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutRelease(HInvoke* invoke) { |
| 1914 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1915 | allocator_, DataType::Type::kInt32, invoke, /*is_volatile=*/ true); |
| 1916 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1917 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutObject(HInvoke* invoke) { |
| 1918 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1919 | allocator_, DataType::Type::kReference, invoke, /*is_volatile=*/ false); |
| 1920 | } |
| 1921 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1922 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1923 | allocator_, DataType::Type::kReference, invoke, /*is_volatile=*/ false); |
| 1924 | } |
| 1925 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1926 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1927 | allocator_, DataType::Type::kReference, invoke, /*is_volatile=*/ true); |
| 1928 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1929 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutObjectRelease(HInvoke* invoke) { |
| 1930 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1931 | allocator_, DataType::Type::kReference, invoke, /*is_volatile=*/ true); |
| 1932 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1933 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutLong(HInvoke* invoke) { |
| 1934 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1935 | allocator_, DataType::Type::kInt64, invoke, /*is_volatile=*/ false); |
| 1936 | } |
| 1937 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutLongOrdered(HInvoke* invoke) { |
| 1938 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1939 | allocator_, DataType::Type::kInt64, invoke, /*is_volatile=*/ false); |
| 1940 | } |
| 1941 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutLongVolatile(HInvoke* invoke) { |
| 1942 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1943 | allocator_, DataType::Type::kInt64, invoke, /*is_volatile=*/ true); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1944 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 1945 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafePutLongRelease(HInvoke* invoke) { |
| 1946 | CreateIntIntIntIntToVoidPlusTempsLocations( |
| 1947 | allocator_, DataType::Type::kInt64, invoke, /*is_volatile=*/ true); |
| 1948 | } |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1949 | |
| 1950 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 1951 | // memory model. |
| 1952 | static void GenUnsafePut(LocationSummary* locations, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1953 | DataType::Type type, |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1954 | bool is_volatile, |
| 1955 | CodeGeneratorX86* codegen) { |
| Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1956 | X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler()); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1957 | Register base = locations->InAt(1).AsRegister<Register>(); |
| 1958 | Register offset = locations->InAt(2).AsRegisterPairLow<Register>(); |
| 1959 | Location value_loc = locations->InAt(3); |
| 1960 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1961 | if (type == DataType::Type::kInt64) { |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1962 | Register value_lo = value_loc.AsRegisterPairLow<Register>(); |
| 1963 | Register value_hi = value_loc.AsRegisterPairHigh<Register>(); |
| 1964 | if (is_volatile) { |
| 1965 | XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
| 1966 | XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>(); |
| 1967 | __ movd(temp1, value_lo); |
| 1968 | __ movd(temp2, value_hi); |
| 1969 | __ punpckldq(temp1, temp2); |
| 1970 | __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1); |
| 1971 | } else { |
| 1972 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo); |
| 1973 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi); |
| 1974 | } |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1975 | } else if (kPoisonHeapReferences && type == DataType::Type::kReference) { |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1976 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 1977 | __ movl(temp, value_loc.AsRegister<Register>()); |
| 1978 | __ PoisonHeapReference(temp); |
| 1979 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1980 | } else { |
| 1981 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>()); |
| 1982 | } |
| 1983 | |
| 1984 | if (is_volatile) { |
| Mark P Mendell | 17077d8 | 2015-12-16 19:15:59 +0000 | [diff] [blame] | 1985 | codegen->MemoryFence(); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1986 | } |
| 1987 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1988 | if (type == DataType::Type::kReference) { |
| Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1989 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1990 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(), |
| 1991 | locations->GetTemp(1).AsRegister<Register>(), |
| 1992 | base, |
| Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1993 | value_loc.AsRegister<Register>(), |
| 1994 | value_can_be_null); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 1999 | VisitJdkUnsafePut(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2000 | } |
| 2001 | void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2002 | VisitJdkUnsafePutOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2003 | } |
| 2004 | void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2005 | VisitJdkUnsafePutVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2006 | } |
| 2007 | void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2008 | VisitJdkUnsafePutObject(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2009 | } |
| 2010 | void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2011 | VisitJdkUnsafePutObjectOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2012 | } |
| 2013 | void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2014 | VisitJdkUnsafePutObjectVolatile(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2015 | } |
| 2016 | void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2017 | VisitJdkUnsafePutLong(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2018 | } |
| 2019 | void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2020 | VisitJdkUnsafePutLongOrdered(invoke); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2021 | } |
| 2022 | void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2023 | VisitJdkUnsafePutLongVolatile(invoke); |
| 2024 | } |
| 2025 | |
| 2026 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePut(HInvoke* invoke) { |
| 2027 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ false, codegen_); |
| 2028 | } |
| 2029 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutOrdered(HInvoke* invoke) { |
| 2030 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ false, codegen_); |
| 2031 | } |
| 2032 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutVolatile(HInvoke* invoke) { |
| 2033 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ true, codegen_); |
| 2034 | } |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 2035 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutRelease(HInvoke* invoke) { |
| 2036 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ true, codegen_); |
| 2037 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2038 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutObject(HInvoke* invoke) { |
| 2039 | GenUnsafePut( |
| 2040 | invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ false, codegen_); |
| 2041 | } |
| 2042 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutObjectOrdered(HInvoke* invoke) { |
| 2043 | GenUnsafePut( |
| 2044 | invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ false, codegen_); |
| 2045 | } |
| 2046 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutObjectVolatile(HInvoke* invoke) { |
| 2047 | GenUnsafePut( |
| 2048 | invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ true, codegen_); |
| 2049 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 2050 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutObjectRelease(HInvoke* invoke) { |
| 2051 | GenUnsafePut( |
| 2052 | invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ true, codegen_); |
| 2053 | } |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2054 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutLong(HInvoke* invoke) { |
| 2055 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ false, codegen_); |
| 2056 | } |
| 2057 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutLongOrdered(HInvoke* invoke) { |
| 2058 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ false, codegen_); |
| 2059 | } |
| 2060 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutLongVolatile(HInvoke* invoke) { |
| 2061 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ true, codegen_); |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2062 | } |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 2063 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafePutLongRelease(HInvoke* invoke) { |
| 2064 | GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ true, codegen_); |
| 2065 | } |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 2066 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2067 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2068 | DataType::Type type, |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2069 | HInvoke* invoke) { |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2070 | bool can_call = kEmitCompilerReadBarrier && |
| 2071 | kUseBakerReadBarrier && |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2072 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject || |
| 2073 | invoke->GetIntrinsic() == Intrinsics::kJdkUnsafeCASObject); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2074 | LocationSummary* locations = |
| 2075 | new (allocator) LocationSummary(invoke, |
| 2076 | can_call |
| 2077 | ? LocationSummary::kCallOnSlowPath |
| 2078 | : LocationSummary::kNoCall, |
| 2079 | kIntrinsified); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2080 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 2081 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2082 | // Offset is a long, but in 32 bit mode, we only need the low word. |
| 2083 | // Can we update the invoke here to remove a TypeConvert to Long? |
| 2084 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2085 | // Expected value must be in EAX or EDX:EAX. |
| 2086 | // For long, new value must be in ECX:EBX. |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2087 | if (type == DataType::Type::kInt64) { |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2088 | locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX)); |
| 2089 | locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX)); |
| 2090 | } else { |
| 2091 | locations->SetInAt(3, Location::RegisterLocation(EAX)); |
| 2092 | locations->SetInAt(4, Location::RequiresRegister()); |
| 2093 | } |
| 2094 | |
| 2095 | // Force a byte register for the output. |
| 2096 | locations->SetOut(Location::RegisterLocation(EAX)); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2097 | if (type == DataType::Type::kReference) { |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2098 | // Need temporary registers for card-marking, and possibly for |
| 2099 | // (Baker) read barrier. |
| Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2100 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2101 | // Need a byte register for marking. |
| 2102 | locations->AddTemp(Location::RegisterLocation(ECX)); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2107 | VisitJdkUnsafeCASInt(invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2111 | VisitJdkUnsafeCASLong(invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2115 | VisitJdkUnsafeCASObject(invoke); |
| 2116 | } |
| 2117 | |
| 2118 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeCASInt(HInvoke* invoke) { |
| 2119 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke); |
| 2120 | } |
| 2121 | |
| 2122 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeCASLong(HInvoke* invoke) { |
| 2123 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke); |
| 2124 | } |
| 2125 | |
| 2126 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeCASObject(HInvoke* invoke) { |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2127 | // The only read barrier implementation supporting the |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2128 | // JdkUnsafeCASObject intrinsic is the Baker-style read barriers. |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2129 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| Roland Levillain | 391b866 | 2015-12-18 11:43:38 +0000 | [diff] [blame] | 2130 | return; |
| 2131 | } |
| 2132 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2133 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2134 | } |
| 2135 | |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 2136 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeCompareAndSetInt(HInvoke* invoke) { |
| 2137 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke); |
| 2138 | } |
| 2139 | |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 2140 | void IntrinsicLocationsBuilderX86::VisitJdkUnsafeCompareAndSetLong(HInvoke* invoke) { |
| 2141 | CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke); |
| 2142 | } |
| 2143 | |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 2144 | static void GenPrimitiveLockedCmpxchg(DataType::Type type, |
| 2145 | CodeGeneratorX86* codegen, |
| 2146 | Location expected_value, |
| 2147 | Location new_value, |
| 2148 | Register base, |
| 2149 | Register offset, |
| 2150 | // Only necessary for floating point |
| 2151 | Register temp = Register::kNoRegister) { |
| Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2152 | X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler()); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2153 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2154 | if (DataType::Kind(type) == DataType::Type::kInt32) { |
| 2155 | DCHECK_EQ(expected_value.AsRegister<Register>(), EAX); |
| 2156 | } |
| 2157 | |
| 2158 | // The address of the field within the holding object. |
| 2159 | Address field_addr(base, offset, TIMES_1, 0); |
| 2160 | |
| 2161 | switch (type) { |
| 2162 | case DataType::Type::kBool: |
| 2163 | case DataType::Type::kInt8: |
| 2164 | __ LockCmpxchgb(field_addr, new_value.AsRegister<ByteRegister>()); |
| 2165 | break; |
| 2166 | case DataType::Type::kInt16: |
| 2167 | case DataType::Type::kUint16: |
| 2168 | __ LockCmpxchgw(field_addr, new_value.AsRegister<Register>()); |
| 2169 | break; |
| 2170 | case DataType::Type::kInt32: |
| 2171 | __ LockCmpxchgl(field_addr, new_value.AsRegister<Register>()); |
| 2172 | break; |
| 2173 | case DataType::Type::kFloat32: { |
| 2174 | // cmpxchg requires the expected value to be in EAX so the new value must be elsewhere. |
| 2175 | DCHECK_NE(temp, EAX); |
| 2176 | // EAX is both an input and an output for cmpxchg |
| 2177 | codegen->Move32(Location::RegisterLocation(EAX), expected_value); |
| 2178 | codegen->Move32(Location::RegisterLocation(temp), new_value); |
| 2179 | __ LockCmpxchgl(field_addr, temp); |
| 2180 | break; |
| 2181 | } |
| 2182 | case DataType::Type::kInt64: |
| 2183 | // Ensure the expected value is in EAX:EDX and that the new |
| 2184 | // value is in EBX:ECX (required by the CMPXCHG8B instruction). |
| 2185 | DCHECK_EQ(expected_value.AsRegisterPairLow<Register>(), EAX); |
| 2186 | DCHECK_EQ(expected_value.AsRegisterPairHigh<Register>(), EDX); |
| 2187 | DCHECK_EQ(new_value.AsRegisterPairLow<Register>(), EBX); |
| 2188 | DCHECK_EQ(new_value.AsRegisterPairHigh<Register>(), ECX); |
| 2189 | __ LockCmpxchg8b(field_addr); |
| 2190 | break; |
| 2191 | default: |
| 2192 | LOG(FATAL) << "Unexpected CAS type " << type; |
| 2193 | } |
| 2194 | // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we |
| 2195 | // don't need scheduling barriers at this time. |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | static void GenPrimitiveCAS(DataType::Type type, |
| 2199 | CodeGeneratorX86* codegen, |
| 2200 | Location expected_value, |
| 2201 | Location new_value, |
| 2202 | Register base, |
| 2203 | Register offset, |
| 2204 | Location out, |
| 2205 | // Only necessary for floating point |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 2206 | Register temp = Register::kNoRegister, |
| 2207 | bool is_cmpxchg = false) { |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 2208 | X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler()); |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 2209 | |
| 2210 | if (!is_cmpxchg || DataType::Kind(type) == DataType::Type::kInt32) { |
| 2211 | DCHECK_EQ(out.AsRegister<Register>(), EAX); |
| 2212 | } |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 2213 | |
| 2214 | GenPrimitiveLockedCmpxchg(type, codegen, expected_value, new_value, base, offset, temp); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2215 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 2216 | if (is_cmpxchg) { |
| 2217 | // Sign-extend, zero-extend or move the result if necessary |
| 2218 | switch (type) { |
| 2219 | case DataType::Type::kBool: |
| 2220 | __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>()); |
| 2221 | break; |
| 2222 | case DataType::Type::kInt8: |
| 2223 | __ movsxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>()); |
| 2224 | break; |
| 2225 | case DataType::Type::kInt16: |
| 2226 | __ movsxw(out.AsRegister<Register>(), out.AsRegister<Register>()); |
| 2227 | break; |
| 2228 | case DataType::Type::kUint16: |
| 2229 | __ movzxw(out.AsRegister<Register>(), out.AsRegister<Register>()); |
| 2230 | break; |
| 2231 | case DataType::Type::kFloat32: |
| 2232 | __ movd(out.AsFpuRegister<XmmRegister>(), EAX); |
| 2233 | break; |
| 2234 | default: |
| 2235 | // Nothing to do |
| 2236 | break; |
| 2237 | } |
| 2238 | } else { |
| 2239 | // Convert ZF into the Boolean result. |
| 2240 | __ setb(kZero, out.AsRegister<Register>()); |
| 2241 | __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>()); |
| 2242 | } |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | static void GenReferenceCAS(HInvoke* invoke, |
| 2246 | CodeGeneratorX86* codegen, |
| 2247 | Location expected_value, |
| 2248 | Location new_value, |
| 2249 | Register base, |
| 2250 | Register offset, |
| 2251 | Register temp, |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 2252 | Register temp2, |
| 2253 | bool is_cmpxchg = false) { |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2254 | X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler()); |
| 2255 | LocationSummary* locations = invoke->GetLocations(); |
| 2256 | Location out = locations->Out(); |
| 2257 | |
| 2258 | // The address of the field within the holding object. |
| 2259 | Address field_addr(base, offset, TIMES_1, 0); |
| 2260 | |
| Andra Danciu | afad9f9 | 2020-09-15 15:38:32 +0000 | [diff] [blame] | 2261 | Register value = new_value.AsRegister<Register>(); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2262 | Register expected = expected_value.AsRegister<Register>(); |
| 2263 | DCHECK_EQ(expected, EAX); |
| 2264 | DCHECK_NE(temp, temp2); |
| 2265 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2266 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 2267 | // Need to make sure the reference stored in the field is a to-space |
| 2268 | // one before attempting the CAS or the CAS could fail incorrectly. |
| 2269 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
| 2270 | invoke, |
| 2271 | // Unused, used only as a "temporary" within the read barrier. |
| 2272 | Location::RegisterLocation(temp), |
| 2273 | base, |
| 2274 | field_addr, |
| 2275 | /* needs_null_check= */ false, |
| 2276 | /* always_update_field= */ true, |
| 2277 | &temp2); |
| 2278 | } |
| 2279 | bool base_equals_value = (base == value); |
| 2280 | if (kPoisonHeapReferences) { |
| 2281 | if (base_equals_value) { |
| 2282 | // If `base` and `value` are the same register location, move |
| 2283 | // `value` to a temporary register. This way, poisoning |
| 2284 | // `value` won't invalidate `base`. |
| 2285 | value = temp; |
| 2286 | __ movl(value, base); |
| 2287 | } |
| 2288 | |
| 2289 | // Check that the register allocator did not assign the location |
| 2290 | // of `expected` (EAX) to `value` nor to `base`, so that heap |
| 2291 | // poisoning (when enabled) works as intended below. |
| 2292 | // - If `value` were equal to `expected`, both references would |
| 2293 | // be poisoned twice, meaning they would not be poisoned at |
| 2294 | // all, as heap poisoning uses address negation. |
| 2295 | // - If `base` were equal to `expected`, poisoning `expected` |
| 2296 | // would invalidate `base`. |
| 2297 | DCHECK_NE(value, expected); |
| 2298 | DCHECK_NE(base, expected); |
| 2299 | __ PoisonHeapReference(expected); |
| 2300 | __ PoisonHeapReference(value); |
| 2301 | } |
| 2302 | __ LockCmpxchgl(field_addr, value); |
| 2303 | |
| 2304 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
| 2305 | // scheduling barriers at this time. |
| 2306 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 2307 | if (is_cmpxchg) { |
| 2308 | DCHECK_EQ(out.AsRegister<Register>(), EAX); |
| 2309 | __ MaybeUnpoisonHeapReference(out.AsRegister<Register>()); |
| 2310 | } else { |
| 2311 | // Convert ZF into the Boolean result. |
| 2312 | __ setb(kZero, out.AsRegister<Register>()); |
| 2313 | __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>()); |
| 2314 | } |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2315 | |
| Andra Danciu | afad9f9 | 2020-09-15 15:38:32 +0000 | [diff] [blame] | 2316 | // Mark card for object if the new value is stored. |
| 2317 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 2318 | NearLabel skip_mark_gc_card; |
| 2319 | __ j(kNotZero, &skip_mark_gc_card); |
| 2320 | codegen->MarkGCCard(temp, temp2, base, value, value_can_be_null); |
| 2321 | __ Bind(&skip_mark_gc_card); |
| 2322 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2323 | // If heap poisoning is enabled, we need to unpoison the values |
| 2324 | // that were poisoned earlier. |
| 2325 | if (kPoisonHeapReferences) { |
| 2326 | if (base_equals_value) { |
| 2327 | // `value` has been moved to a temporary register, no need to |
| 2328 | // unpoison it. |
| 2329 | } else { |
| 2330 | // Ensure `value` is different from `out`, so that unpoisoning |
| 2331 | // the former does not invalidate the latter. |
| 2332 | DCHECK_NE(value, out.AsRegister<Register>()); |
| 2333 | __ UnpoisonHeapReference(value); |
| 2334 | } |
| 2335 | } |
| 2336 | // Do not unpoison the reference contained in register |
| 2337 | // `expected`, as it is the same as register `out` (EAX). |
| 2338 | } |
| 2339 | |
| 2340 | static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) { |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2341 | LocationSummary* locations = invoke->GetLocations(); |
| 2342 | |
| 2343 | Register base = locations->InAt(1).AsRegister<Register>(); |
| 2344 | Register offset = locations->InAt(2).AsRegisterPairLow<Register>(); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2345 | Location expected_value = locations->InAt(3); |
| 2346 | Location new_value = locations->InAt(4); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2347 | Location out = locations->Out(); |
| 2348 | DCHECK_EQ(out.AsRegister<Register>(), EAX); |
| 2349 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2350 | if (type == DataType::Type::kReference) { |
| Andra Danciu | ff6d5fa | 2020-09-16 07:08:24 +0000 | [diff] [blame] | 2351 | // The only read barrier implementation supporting the |
| 2352 | // UnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2353 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 2354 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2355 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2356 | Register temp2 = locations->GetTemp(1).AsRegister<Register>(); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2357 | GenReferenceCAS(invoke, codegen, expected_value, new_value, base, offset, temp, temp2); |
| Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2358 | } else { |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 2359 | DCHECK(!DataType::IsFloatingPointType(type)); |
| 2360 | GenPrimitiveCAS(type, codegen, expected_value, new_value, base, offset, out); |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2361 | } |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2365 | VisitJdkUnsafeCASInt(invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) { |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2369 | VisitJdkUnsafeCASLong(invoke); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) { |
| Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 2373 | // The only read barrier implementation supporting the |
| 2374 | // UnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2375 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 2376 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2377 | GenCAS(DataType::Type::kReference, invoke, codegen_); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2378 | } |
| 2379 | |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2380 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeCASInt(HInvoke* invoke) { |
| 2381 | GenCAS(DataType::Type::kInt32, invoke, codegen_); |
| 2382 | } |
| 2383 | |
| 2384 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeCASLong(HInvoke* invoke) { |
| 2385 | GenCAS(DataType::Type::kInt64, invoke, codegen_); |
| 2386 | } |
| 2387 | |
| 2388 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeCASObject(HInvoke* invoke) { |
| 2389 | // The only read barrier implementation supporting the |
| 2390 | // JdkUnsafeCASObject intrinsic is the Baker-style read barriers. |
| 2391 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 2392 | |
| 2393 | GenCAS(DataType::Type::kReference, invoke, codegen_); |
| 2394 | } |
| 2395 | |
| Sorin Basca | 0069ad7 | 2021-09-17 17:33:09 +0000 | [diff] [blame] | 2396 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeCompareAndSetInt(HInvoke* invoke) { |
| 2397 | GenCAS(DataType::Type::kInt32, invoke, codegen_); |
| 2398 | } |
| 2399 | |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 2400 | void IntrinsicCodeGeneratorX86::VisitJdkUnsafeCompareAndSetLong(HInvoke* invoke) { |
| 2401 | GenCAS(DataType::Type::kInt64, invoke, codegen_); |
| 2402 | } |
| 2403 | |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 2404 | |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2405 | void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2406 | LocationSummary* locations = |
| 2407 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2408 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2409 | locations->SetOut(Location::SameAsFirstInput()); |
| 2410 | locations->AddTemp(Location::RequiresRegister()); |
| 2411 | } |
| 2412 | |
| 2413 | static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask, |
| 2414 | X86Assembler* assembler) { |
| 2415 | Immediate imm_shift(shift); |
| 2416 | Immediate imm_mask(mask); |
| 2417 | __ movl(temp, reg); |
| 2418 | __ shrl(reg, imm_shift); |
| 2419 | __ andl(temp, imm_mask); |
| 2420 | __ andl(reg, imm_mask); |
| 2421 | __ shll(temp, imm_shift); |
| 2422 | __ orl(reg, temp); |
| 2423 | } |
| 2424 | |
| 2425 | void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) { |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2426 | X86Assembler* assembler = GetAssembler(); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2427 | LocationSummary* locations = invoke->GetLocations(); |
| 2428 | |
| 2429 | Register reg = locations->InAt(0).AsRegister<Register>(); |
| 2430 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 2431 | |
| 2432 | /* |
| 2433 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2434 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 2435 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2436 | * x = bswap x |
| 2437 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 2438 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 2439 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 2440 | */ |
| 2441 | __ bswapl(reg); |
| 2442 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 2443 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 2444 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 2445 | } |
| 2446 | |
| 2447 | void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2448 | LocationSummary* locations = |
| 2449 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2450 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2451 | locations->SetOut(Location::SameAsFirstInput()); |
| 2452 | locations->AddTemp(Location::RequiresRegister()); |
| 2453 | } |
| 2454 | |
| 2455 | void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) { |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2456 | X86Assembler* assembler = GetAssembler(); |
| Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2457 | LocationSummary* locations = invoke->GetLocations(); |
| 2458 | |
| 2459 | Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 2460 | Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 2461 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 2462 | |
| 2463 | // We want to swap high/low, then bswap each one, and then do the same |
| 2464 | // as a 32 bit reverse. |
| 2465 | // Exchange high and low. |
| 2466 | __ movl(temp, reg_low); |
| 2467 | __ movl(reg_low, reg_high); |
| 2468 | __ movl(reg_high, temp); |
| 2469 | |
| 2470 | // bit-reverse low |
| 2471 | __ bswapl(reg_low); |
| 2472 | SwapBits(reg_low, temp, 1, 0x55555555, assembler); |
| 2473 | SwapBits(reg_low, temp, 2, 0x33333333, assembler); |
| 2474 | SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler); |
| 2475 | |
| 2476 | // bit-reverse high |
| 2477 | __ bswapl(reg_high); |
| 2478 | SwapBits(reg_high, temp, 1, 0x55555555, assembler); |
| 2479 | SwapBits(reg_high, temp, 2, 0x33333333, assembler); |
| 2480 | SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler); |
| 2481 | } |
| 2482 | |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2483 | static void CreateBitCountLocations( |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2484 | ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) { |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2485 | if (!codegen->GetInstructionSetFeatures().HasPopCnt()) { |
| 2486 | // Do nothing if there is no popcnt support. This results in generating |
| 2487 | // a call for the intrinsic rather than direct code. |
| 2488 | return; |
| 2489 | } |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2490 | LocationSummary* locations = |
| 2491 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2492 | if (is_long) { |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2493 | locations->AddTemp(Location::RequiresRegister()); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2494 | } |
| Aart Bik | 2a94607 | 2016-01-21 12:49:00 -0800 | [diff] [blame] | 2495 | locations->SetInAt(0, Location::Any()); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2496 | locations->SetOut(Location::RequiresRegister()); |
| 2497 | } |
| 2498 | |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2499 | static void GenBitCount(X86Assembler* assembler, |
| 2500 | CodeGeneratorX86* codegen, |
| 2501 | HInvoke* invoke, bool is_long) { |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2502 | LocationSummary* locations = invoke->GetLocations(); |
| 2503 | Location src = locations->InAt(0); |
| 2504 | Register out = locations->Out().AsRegister<Register>(); |
| 2505 | |
| 2506 | if (invoke->InputAt(0)->IsConstant()) { |
| 2507 | // Evaluate this at compile time. |
| 2508 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2509 | int32_t result = is_long |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2510 | ? POPCOUNT(static_cast<uint64_t>(value)) |
| 2511 | : POPCOUNT(static_cast<uint32_t>(value)); |
| Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 2512 | codegen->Load32BitValue(out, result); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2513 | return; |
| 2514 | } |
| 2515 | |
| 2516 | // Handle the non-constant cases. |
| 2517 | if (!is_long) { |
| 2518 | if (src.IsRegister()) { |
| 2519 | __ popcntl(out, src.AsRegister<Register>()); |
| 2520 | } else { |
| 2521 | DCHECK(src.IsStackSlot()); |
| 2522 | __ popcntl(out, Address(ESP, src.GetStackIndex())); |
| 2523 | } |
| Aart Bik | 2a94607 | 2016-01-21 12:49:00 -0800 | [diff] [blame] | 2524 | } else { |
| 2525 | // The 64-bit case needs to worry about two parts. |
| 2526 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 2527 | if (src.IsRegisterPair()) { |
| 2528 | __ popcntl(temp, src.AsRegisterPairLow<Register>()); |
| 2529 | __ popcntl(out, src.AsRegisterPairHigh<Register>()); |
| 2530 | } else { |
| 2531 | DCHECK(src.IsDoubleStackSlot()); |
| 2532 | __ popcntl(temp, Address(ESP, src.GetStackIndex())); |
| 2533 | __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize))); |
| 2534 | } |
| 2535 | __ addl(out, temp); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2536 | } |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2540 | CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long= */ false); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2544 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ false); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2548 | CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long= */ true); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2552 | GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ true); |
| Aart Bik | c39dac1 | 2016-01-21 08:59:48 -0800 | [diff] [blame] | 2553 | } |
| 2554 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2555 | static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) { |
| 2556 | LocationSummary* locations = |
| 2557 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2558 | if (is_long) { |
| 2559 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2560 | } else { |
| 2561 | locations->SetInAt(0, Location::Any()); |
| 2562 | } |
| 2563 | locations->SetOut(Location::RequiresRegister()); |
| 2564 | } |
| 2565 | |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2566 | static void GenLeadingZeros(X86Assembler* assembler, |
| 2567 | CodeGeneratorX86* codegen, |
| 2568 | HInvoke* invoke, bool is_long) { |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2569 | LocationSummary* locations = invoke->GetLocations(); |
| 2570 | Location src = locations->InAt(0); |
| 2571 | Register out = locations->Out().AsRegister<Register>(); |
| 2572 | |
| 2573 | if (invoke->InputAt(0)->IsConstant()) { |
| 2574 | // Evaluate this at compile time. |
| 2575 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2576 | if (value == 0) { |
| 2577 | value = is_long ? 64 : 32; |
| 2578 | } else { |
| 2579 | value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value)); |
| 2580 | } |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2581 | codegen->Load32BitValue(out, value); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2582 | return; |
| 2583 | } |
| 2584 | |
| 2585 | // Handle the non-constant cases. |
| 2586 | if (!is_long) { |
| 2587 | if (src.IsRegister()) { |
| 2588 | __ bsrl(out, src.AsRegister<Register>()); |
| 2589 | } else { |
| 2590 | DCHECK(src.IsStackSlot()); |
| 2591 | __ bsrl(out, Address(ESP, src.GetStackIndex())); |
| 2592 | } |
| 2593 | |
| 2594 | // 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] | 2595 | NearLabel all_zeroes, done; |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2596 | __ j(kEqual, &all_zeroes); |
| 2597 | |
| 2598 | // Correct the result from BSR to get the final CLZ result. |
| 2599 | __ xorl(out, Immediate(31)); |
| 2600 | __ jmp(&done); |
| 2601 | |
| 2602 | // Fix the zero case with the expected result. |
| 2603 | __ Bind(&all_zeroes); |
| 2604 | __ movl(out, Immediate(32)); |
| 2605 | |
| 2606 | __ Bind(&done); |
| 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | // 64 bit case needs to worry about both parts of the register. |
| 2611 | DCHECK(src.IsRegisterPair()); |
| 2612 | Register src_lo = src.AsRegisterPairLow<Register>(); |
| 2613 | Register src_hi = src.AsRegisterPairHigh<Register>(); |
| Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 2614 | NearLabel handle_low, done, all_zeroes; |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2615 | |
| 2616 | // Is the high word zero? |
| 2617 | __ testl(src_hi, src_hi); |
| 2618 | __ j(kEqual, &handle_low); |
| 2619 | |
| 2620 | // High word is not zero. We know that the BSR result is defined in this case. |
| 2621 | __ bsrl(out, src_hi); |
| 2622 | |
| 2623 | // Correct the result from BSR to get the final CLZ result. |
| 2624 | __ xorl(out, Immediate(31)); |
| 2625 | __ jmp(&done); |
| 2626 | |
| 2627 | // High word was zero. We have to compute the low word count and add 32. |
| 2628 | __ Bind(&handle_low); |
| 2629 | __ bsrl(out, src_lo); |
| 2630 | __ j(kEqual, &all_zeroes); |
| 2631 | |
| 2632 | // We had a valid result. Use an XOR to both correct the result and add 32. |
| 2633 | __ xorl(out, Immediate(63)); |
| 2634 | __ jmp(&done); |
| 2635 | |
| 2636 | // All zero case. |
| 2637 | __ Bind(&all_zeroes); |
| 2638 | __ movl(out, Immediate(64)); |
| 2639 | |
| 2640 | __ Bind(&done); |
| 2641 | } |
| 2642 | |
| 2643 | void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2644 | CreateLeadingZeroLocations(allocator_, invoke, /* is_long= */ false); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2648 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2652 | CreateLeadingZeroLocations(allocator_, invoke, /* is_long= */ true); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2656 | GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true); |
| Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2657 | } |
| 2658 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2659 | static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) { |
| 2660 | LocationSummary* locations = |
| 2661 | new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2662 | if (is_long) { |
| 2663 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2664 | } else { |
| 2665 | locations->SetInAt(0, Location::Any()); |
| 2666 | } |
| 2667 | locations->SetOut(Location::RequiresRegister()); |
| 2668 | } |
| 2669 | |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2670 | static void GenTrailingZeros(X86Assembler* assembler, |
| 2671 | CodeGeneratorX86* codegen, |
| 2672 | HInvoke* invoke, bool is_long) { |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2673 | LocationSummary* locations = invoke->GetLocations(); |
| 2674 | Location src = locations->InAt(0); |
| 2675 | Register out = locations->Out().AsRegister<Register>(); |
| 2676 | |
| 2677 | if (invoke->InputAt(0)->IsConstant()) { |
| 2678 | // Evaluate this at compile time. |
| 2679 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2680 | if (value == 0) { |
| 2681 | value = is_long ? 64 : 32; |
| 2682 | } else { |
| 2683 | value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value)); |
| 2684 | } |
| Aart Bik | a19616e | 2016-02-01 18:57:58 -0800 | [diff] [blame] | 2685 | codegen->Load32BitValue(out, value); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2686 | return; |
| 2687 | } |
| 2688 | |
| 2689 | // Handle the non-constant cases. |
| 2690 | if (!is_long) { |
| 2691 | if (src.IsRegister()) { |
| 2692 | __ bsfl(out, src.AsRegister<Register>()); |
| 2693 | } else { |
| 2694 | DCHECK(src.IsStackSlot()); |
| 2695 | __ bsfl(out, Address(ESP, src.GetStackIndex())); |
| 2696 | } |
| 2697 | |
| 2698 | // BSF sets ZF if the input was zero, and the output is undefined. |
| 2699 | NearLabel done; |
| 2700 | __ j(kNotEqual, &done); |
| 2701 | |
| 2702 | // Fix the zero case with the expected result. |
| 2703 | __ movl(out, Immediate(32)); |
| 2704 | |
| 2705 | __ Bind(&done); |
| 2706 | return; |
| 2707 | } |
| 2708 | |
| 2709 | // 64 bit case needs to worry about both parts of the register. |
| 2710 | DCHECK(src.IsRegisterPair()); |
| 2711 | Register src_lo = src.AsRegisterPairLow<Register>(); |
| 2712 | Register src_hi = src.AsRegisterPairHigh<Register>(); |
| 2713 | NearLabel done, all_zeroes; |
| 2714 | |
| 2715 | // If the low word is zero, then ZF will be set. If not, we have the answer. |
| 2716 | __ bsfl(out, src_lo); |
| 2717 | __ j(kNotEqual, &done); |
| 2718 | |
| 2719 | // Low word was zero. We have to compute the high word count and add 32. |
| 2720 | __ bsfl(out, src_hi); |
| 2721 | __ j(kEqual, &all_zeroes); |
| 2722 | |
| 2723 | // We had a valid result. Add 32 to account for the low word being zero. |
| 2724 | __ addl(out, Immediate(32)); |
| 2725 | __ jmp(&done); |
| 2726 | |
| 2727 | // All zero case. |
| 2728 | __ Bind(&all_zeroes); |
| 2729 | __ movl(out, Immediate(64)); |
| 2730 | |
| 2731 | __ Bind(&done); |
| 2732 | } |
| 2733 | |
| 2734 | void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2735 | CreateTrailingZeroLocations(allocator_, invoke, /* is_long= */ false); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2739 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2743 | CreateTrailingZeroLocations(allocator_, invoke, /* is_long= */ true); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2747 | GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true); |
| Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2748 | } |
| 2749 | |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2750 | static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) { |
| 2751 | return instruction->InputAt(input0) == instruction->InputAt(input1); |
| 2752 | } |
| 2753 | |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2754 | // Compute base address for the System.arraycopy intrinsic in `base`. |
| 2755 | static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2756 | DataType::Type type, |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2757 | const Register& array, |
| 2758 | const Location& pos, |
| 2759 | const Register& base) { |
| 2760 | // This routine is only used by the SystemArrayCopy intrinsic at the |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2761 | // moment. We can allow DataType::Type::kReference as `type` to implement |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2762 | // the SystemArrayCopyChar intrinsic. |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2763 | DCHECK_EQ(type, DataType::Type::kReference); |
| 2764 | const int32_t element_size = DataType::Size(type); |
| 2765 | const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type)); |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2766 | const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value(); |
| 2767 | |
| 2768 | if (pos.IsConstant()) { |
| 2769 | int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue(); |
| 2770 | __ leal(base, Address(array, element_size * constant + data_offset)); |
| 2771 | } else { |
| 2772 | __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset)); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | // Compute end source address for the System.arraycopy intrinsic in `end`. |
| 2777 | static void GenSystemArrayCopyEndAddress(X86Assembler* assembler, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2778 | DataType::Type type, |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2779 | const Location& copy_length, |
| 2780 | const Register& base, |
| 2781 | const Register& end) { |
| 2782 | // This routine is only used by the SystemArrayCopy intrinsic at the |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2783 | // moment. We can allow DataType::Type::kReference as `type` to implement |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2784 | // the SystemArrayCopyChar intrinsic. |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2785 | DCHECK_EQ(type, DataType::Type::kReference); |
| 2786 | const int32_t element_size = DataType::Size(type); |
| 2787 | const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type)); |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (copy_length.IsConstant()) { |
| 2790 | int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue(); |
| 2791 | __ leal(end, Address(base, element_size * constant)); |
| 2792 | } else { |
| 2793 | __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0)); |
| 2794 | } |
| 2795 | } |
| 2796 | |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2797 | void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) { |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2798 | // The only read barrier implementation supporting the |
| 2799 | // SystemArrayCopy intrinsic is the Baker-style read barriers. |
| 2800 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2801 | return; |
| 2802 | } |
| 2803 | |
| 2804 | CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke); |
| 2805 | if (invoke->GetLocations() != nullptr) { |
| 2806 | // Need a byte register for marking. |
| 2807 | invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX)); |
| 2808 | |
| 2809 | static constexpr size_t kSrc = 0; |
| 2810 | static constexpr size_t kSrcPos = 1; |
| 2811 | static constexpr size_t kDest = 2; |
| 2812 | static constexpr size_t kDestPos = 3; |
| 2813 | static constexpr size_t kLength = 4; |
| 2814 | |
| 2815 | if (!invoke->InputAt(kSrcPos)->IsIntConstant() && |
| 2816 | !invoke->InputAt(kDestPos)->IsIntConstant() && |
| 2817 | !invoke->InputAt(kLength)->IsIntConstant()) { |
| 2818 | if (!IsSameInput(invoke, kSrcPos, kDestPos) && |
| 2819 | !IsSameInput(invoke, kSrcPos, kLength) && |
| 2820 | !IsSameInput(invoke, kDestPos, kLength) && |
| 2821 | !IsSameInput(invoke, kSrc, kDest)) { |
| 2822 | // Not enough registers, make the length also take a stack slot. |
| 2823 | invoke->GetLocations()->SetInAt(kLength, Location::Any()); |
| 2824 | } |
| 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) { |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2830 | // The only read barrier implementation supporting the |
| 2831 | // SystemArrayCopy intrinsic is the Baker-style read barriers. |
| 2832 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2833 | |
| 2834 | X86Assembler* assembler = GetAssembler(); |
| 2835 | LocationSummary* locations = invoke->GetLocations(); |
| 2836 | |
| 2837 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 2838 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 2839 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 2840 | uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2841 | uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value(); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2842 | |
| 2843 | Register src = locations->InAt(0).AsRegister<Register>(); |
| 2844 | Location src_pos = locations->InAt(1); |
| 2845 | Register dest = locations->InAt(2).AsRegister<Register>(); |
| 2846 | Location dest_pos = locations->InAt(3); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2847 | Location length_arg = locations->InAt(4); |
| 2848 | Location length = length_arg; |
| 2849 | Location temp1_loc = locations->GetTemp(0); |
| 2850 | Register temp1 = temp1_loc.AsRegister<Register>(); |
| 2851 | Location temp2_loc = locations->GetTemp(1); |
| 2852 | Register temp2 = temp2_loc.AsRegister<Register>(); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2853 | |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 2854 | SlowPathCode* intrinsic_slow_path = |
| 2855 | new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2856 | codegen_->AddSlowPath(intrinsic_slow_path); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2857 | |
| 2858 | NearLabel conditions_on_positions_validated; |
| 2859 | SystemArrayCopyOptimizations optimizations(invoke); |
| 2860 | |
| 2861 | // If source and destination are the same, we go to slow path if we need to do |
| 2862 | // forward copying. |
| 2863 | if (src_pos.IsConstant()) { |
| 2864 | int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 2865 | if (dest_pos.IsConstant()) { |
| 2866 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 2867 | if (optimizations.GetDestinationIsSource()) { |
| 2868 | // Checked when building locations. |
| 2869 | DCHECK_GE(src_pos_constant, dest_pos_constant); |
| 2870 | } else if (src_pos_constant < dest_pos_constant) { |
| 2871 | __ cmpl(src, dest); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2872 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2873 | } |
| 2874 | } else { |
| 2875 | if (!optimizations.GetDestinationIsSource()) { |
| 2876 | __ cmpl(src, dest); |
| 2877 | __ j(kNotEqual, &conditions_on_positions_validated); |
| 2878 | } |
| 2879 | __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant)); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2880 | __ j(kGreater, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2881 | } |
| 2882 | } else { |
| 2883 | if (!optimizations.GetDestinationIsSource()) { |
| 2884 | __ cmpl(src, dest); |
| 2885 | __ j(kNotEqual, &conditions_on_positions_validated); |
| 2886 | } |
| 2887 | if (dest_pos.IsConstant()) { |
| 2888 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 2889 | __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant)); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2890 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2891 | } else { |
| 2892 | __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>()); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2893 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | __ Bind(&conditions_on_positions_validated); |
| 2898 | |
| 2899 | if (!optimizations.GetSourceIsNotNull()) { |
| 2900 | // Bail out if the source is null. |
| 2901 | __ testl(src, src); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2902 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) { |
| 2906 | // Bail out if the destination is null. |
| 2907 | __ testl(dest, dest); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2908 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2911 | Location temp3_loc = locations->GetTemp(2); |
| 2912 | Register temp3 = temp3_loc.AsRegister<Register>(); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2913 | if (length.IsStackSlot()) { |
| 2914 | __ movl(temp3, Address(ESP, length.GetStackIndex())); |
| 2915 | length = Location::RegisterLocation(temp3); |
| 2916 | } |
| 2917 | |
| 2918 | // If the length is negative, bail out. |
| 2919 | // We have already checked in the LocationsBuilder for the constant case. |
| 2920 | if (!length.IsConstant() && |
| 2921 | !optimizations.GetCountIsSourceLength() && |
| 2922 | !optimizations.GetCountIsDestinationLength()) { |
| 2923 | __ testl(length.AsRegister<Register>(), length.AsRegister<Register>()); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2924 | __ j(kLess, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | // Validity checks: source. |
| 2928 | CheckPosition(assembler, |
| 2929 | src_pos, |
| 2930 | src, |
| 2931 | length, |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2932 | intrinsic_slow_path, |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2933 | temp1, |
| 2934 | optimizations.GetCountIsSourceLength()); |
| 2935 | |
| 2936 | // Validity checks: dest. |
| 2937 | CheckPosition(assembler, |
| 2938 | dest_pos, |
| 2939 | dest, |
| 2940 | length, |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2941 | intrinsic_slow_path, |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2942 | temp1, |
| 2943 | optimizations.GetCountIsDestinationLength()); |
| 2944 | |
| 2945 | if (!optimizations.GetDoesNotNeedTypeCheck()) { |
| 2946 | // Check whether all elements of the source array are assignable to the component |
| 2947 | // type of the destination array. We do two checks: the classes are the same, |
| 2948 | // or the destination is Object[]. If none of these checks succeed, we go to the |
| 2949 | // slow path. |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2950 | |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2951 | if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2952 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 2953 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 2954 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2955 | invoke, temp1_loc, src, class_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2956 | // Bail out if the source is not a non primitive array. |
| 2957 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 2958 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2959 | invoke, temp1_loc, temp1, component_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2960 | __ testl(temp1, temp1); |
| 2961 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 2962 | // If heap poisoning is enabled, `temp1` has been unpoisoned |
| 2963 | // by the the previous call to GenerateFieldLoadWithBakerReadBarrier. |
| 2964 | } else { |
| 2965 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 2966 | __ movl(temp1, Address(src, class_offset)); |
| 2967 | __ MaybeUnpoisonHeapReference(temp1); |
| 2968 | // Bail out if the source is not a non primitive array. |
| 2969 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 2970 | __ movl(temp1, Address(temp1, component_offset)); |
| 2971 | __ testl(temp1, temp1); |
| 2972 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 2973 | __ MaybeUnpoisonHeapReference(temp1); |
| 2974 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2975 | __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot)); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2976 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2977 | } |
| 2978 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2979 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 2980 | if (length.Equals(Location::RegisterLocation(temp3))) { |
| 2981 | // When Baker read barriers are enabled, register `temp3`, |
| 2982 | // which in the present case contains the `length` parameter, |
| 2983 | // will be overwritten below. Make the `length` location |
| 2984 | // reference the original stack location; it will be moved |
| 2985 | // back to `temp3` later if necessary. |
| 2986 | DCHECK(length_arg.IsStackSlot()); |
| 2987 | length = length_arg; |
| 2988 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2989 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2990 | // /* HeapReference<Class> */ temp1 = dest->klass_ |
| 2991 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2992 | invoke, temp1_loc, dest, class_offset, /* needs_null_check= */ false); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 2993 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 2994 | if (!optimizations.GetDestinationIsNonPrimitiveArray()) { |
| 2995 | // Bail out if the destination is not a non primitive array. |
| 2996 | // |
| 2997 | // Register `temp1` is not trashed by the read barrier emitted |
| 2998 | // by GenerateFieldLoadWithBakerReadBarrier below, as that |
| 2999 | // method produces a call to a ReadBarrierMarkRegX entry point, |
| 3000 | // which saves all potentially live registers, including |
| 3001 | // temporaries such a `temp1`. |
| 3002 | // /* HeapReference<Class> */ temp2 = temp1->component_type_ |
| 3003 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3004 | invoke, temp2_loc, temp1, component_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3005 | __ testl(temp2, temp2); |
| 3006 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3007 | // If heap poisoning is enabled, `temp2` has been unpoisoned |
| 3008 | // by the the previous call to GenerateFieldLoadWithBakerReadBarrier. |
| 3009 | __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot)); |
| 3010 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3011 | } |
| 3012 | |
| 3013 | // For the same reason given earlier, `temp1` is not trashed by the |
| 3014 | // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below. |
| 3015 | // /* HeapReference<Class> */ temp2 = src->klass_ |
| 3016 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3017 | invoke, temp2_loc, src, class_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3018 | // Note: if heap poisoning is on, we are comparing two unpoisoned references here. |
| 3019 | __ cmpl(temp1, temp2); |
| 3020 | |
| 3021 | if (optimizations.GetDestinationIsTypedObjectArray()) { |
| 3022 | NearLabel do_copy; |
| 3023 | __ j(kEqual, &do_copy); |
| 3024 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 3025 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3026 | invoke, temp1_loc, temp1, component_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3027 | // We do not need to emit a read barrier for the following |
| 3028 | // heap reference load, as `temp1` is only used in a |
| 3029 | // comparison with null below, and this reference is not |
| 3030 | // kept afterwards. |
| 3031 | __ cmpl(Address(temp1, super_offset), Immediate(0)); |
| 3032 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3033 | __ Bind(&do_copy); |
| 3034 | } else { |
| 3035 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3036 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3037 | } else { |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3038 | // Non read barrier code. |
| 3039 | |
| 3040 | // /* HeapReference<Class> */ temp1 = dest->klass_ |
| 3041 | __ movl(temp1, Address(dest, class_offset)); |
| 3042 | if (!optimizations.GetDestinationIsNonPrimitiveArray()) { |
| 3043 | __ MaybeUnpoisonHeapReference(temp1); |
| 3044 | // Bail out if the destination is not a non primitive array. |
| 3045 | // /* HeapReference<Class> */ temp2 = temp1->component_type_ |
| 3046 | __ movl(temp2, Address(temp1, component_offset)); |
| 3047 | __ testl(temp2, temp2); |
| 3048 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3049 | __ MaybeUnpoisonHeapReference(temp2); |
| 3050 | __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot)); |
| 3051 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3052 | // Re-poison the heap reference to make the compare instruction below |
| 3053 | // compare two poisoned references. |
| 3054 | __ PoisonHeapReference(temp1); |
| 3055 | } |
| 3056 | |
| 3057 | // Note: if heap poisoning is on, we are comparing two poisoned references here. |
| 3058 | __ cmpl(temp1, Address(src, class_offset)); |
| 3059 | |
| 3060 | if (optimizations.GetDestinationIsTypedObjectArray()) { |
| 3061 | NearLabel do_copy; |
| 3062 | __ j(kEqual, &do_copy); |
| 3063 | __ MaybeUnpoisonHeapReference(temp1); |
| 3064 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 3065 | __ movl(temp1, Address(temp1, component_offset)); |
| 3066 | __ MaybeUnpoisonHeapReference(temp1); |
| 3067 | __ cmpl(Address(temp1, super_offset), Immediate(0)); |
| 3068 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3069 | __ Bind(&do_copy); |
| 3070 | } else { |
| 3071 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3072 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3073 | } |
| 3074 | } else if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 3075 | DCHECK(optimizations.GetDestinationIsNonPrimitiveArray()); |
| 3076 | // Bail out if the source is not a non primitive array. |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3077 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 3078 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 3079 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3080 | invoke, temp1_loc, src, class_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3081 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 3082 | codegen_->GenerateFieldLoadWithBakerReadBarrier( |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3083 | invoke, temp1_loc, temp1, component_offset, /* needs_null_check= */ false); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3084 | __ testl(temp1, temp1); |
| 3085 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3086 | // If heap poisoning is enabled, `temp1` has been unpoisoned |
| 3087 | // by the the previous call to GenerateFieldLoadWithBakerReadBarrier. |
| 3088 | } else { |
| 3089 | // /* HeapReference<Class> */ temp1 = src->klass_ |
| 3090 | __ movl(temp1, Address(src, class_offset)); |
| 3091 | __ MaybeUnpoisonHeapReference(temp1); |
| 3092 | // /* HeapReference<Class> */ temp1 = temp1->component_type_ |
| 3093 | __ movl(temp1, Address(temp1, component_offset)); |
| 3094 | __ testl(temp1, temp1); |
| 3095 | __ j(kEqual, intrinsic_slow_path->GetEntryLabel()); |
| 3096 | __ MaybeUnpoisonHeapReference(temp1); |
| 3097 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3098 | __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot)); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3099 | __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 3102 | const DataType::Type type = DataType::Type::kReference; |
| 3103 | const int32_t element_size = DataType::Size(type); |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3104 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3105 | // Compute the base source address in `temp1`. |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3106 | GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3107 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3108 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 3109 | // If it is needed (in the case of the fast-path loop), the base |
| 3110 | // destination address is computed later, as `temp2` is used for |
| 3111 | // intermediate computations. |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3112 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3113 | // Compute the end source address in `temp3`. |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3114 | if (length.IsStackSlot()) { |
| 3115 | // Location `length` is again pointing at a stack slot, as |
| 3116 | // register `temp3` (which was containing the length parameter |
| 3117 | // earlier) has been overwritten; restore it now |
| 3118 | DCHECK(length.Equals(length_arg)); |
| 3119 | __ movl(temp3, Address(ESP, length.GetStackIndex())); |
| 3120 | length = Location::RegisterLocation(temp3); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3121 | } |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3122 | GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3123 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3124 | // SystemArrayCopy implementation for Baker read barriers (see |
| 3125 | // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier): |
| 3126 | // |
| 3127 | // if (src_ptr != end_ptr) { |
| 3128 | // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState(); |
| 3129 | // lfence; // Load fence or artificial data dependency to prevent load-load reordering |
| Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 3130 | // bool is_gray = (rb_state == ReadBarrier::GrayState()); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3131 | // if (is_gray) { |
| 3132 | // // Slow-path copy. |
| 3133 | // for (size_t i = 0; i != length; ++i) { |
| 3134 | // dest_array[dest_pos + i] = |
| 3135 | // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i]))); |
| 3136 | // } |
| 3137 | // } else { |
| 3138 | // // Fast-path copy. |
| 3139 | // do { |
| 3140 | // *dest_ptr++ = *src_ptr++; |
| 3141 | // } while (src_ptr != end_ptr) |
| 3142 | // } |
| 3143 | // } |
| 3144 | |
| 3145 | NearLabel loop, done; |
| 3146 | |
| 3147 | // Don't enter copy loop if `length == 0`. |
| 3148 | __ cmpl(temp1, temp3); |
| 3149 | __ j(kEqual, &done); |
| 3150 | |
| Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 3151 | // Given the numeric representation, it's enough to check the low bit of the rb_state. |
| Roland Levillain | 14e5a29 | 2018-06-28 12:00:56 +0100 | [diff] [blame] | 3152 | static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0"); |
| Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 3153 | static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1"); |
| Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 3154 | constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte; |
| 3155 | constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte; |
| 3156 | constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position); |
| 3157 | |
| Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 3158 | // if (rb_state == ReadBarrier::GrayState()) |
| Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 3159 | // goto slow_path; |
| 3160 | // At this point, just do the "if" and make sure that flags are preserved until the branch. |
| 3161 | __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value)); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3162 | |
| 3163 | // Load fence to prevent load-load reordering. |
| 3164 | // Note that this is a no-op, thanks to the x86 memory model. |
| 3165 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 3166 | |
| 3167 | // Slow path used to copy array when `src` is gray. |
| 3168 | SlowPathCode* read_barrier_slow_path = |
| Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 3169 | new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3170 | codegen_->AddSlowPath(read_barrier_slow_path); |
| 3171 | |
| Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 3172 | // We have done the "if" of the gray bit check above, now branch based on the flags. |
| 3173 | __ j(kNotZero, read_barrier_slow_path->GetEntryLabel()); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3174 | |
| 3175 | // Fast-path copy. |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3176 | // Compute the base destination address in `temp2`. |
| 3177 | GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3178 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 3179 | // poison/unpoison. |
| 3180 | __ Bind(&loop); |
| 3181 | __ pushl(Address(temp1, 0)); |
| 3182 | __ cfi().AdjustCFAOffset(4); |
| 3183 | __ popl(Address(temp2, 0)); |
| 3184 | __ cfi().AdjustCFAOffset(-4); |
| 3185 | __ addl(temp1, Immediate(element_size)); |
| 3186 | __ addl(temp2, Immediate(element_size)); |
| 3187 | __ cmpl(temp1, temp3); |
| 3188 | __ j(kNotEqual, &loop); |
| 3189 | |
| 3190 | __ Bind(read_barrier_slow_path->GetExitLabel()); |
| 3191 | __ Bind(&done); |
| 3192 | } else { |
| 3193 | // Non read barrier code. |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3194 | // Compute the base destination address in `temp2`. |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3195 | GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3196 | // Compute the end source address in `temp3`. |
| Roland Levillain | 9cc0ea8 | 2017-03-16 11:25:59 +0000 | [diff] [blame] | 3197 | GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3); |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3198 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 3199 | // poison/unpoison. |
| 3200 | NearLabel loop, done; |
| 3201 | __ cmpl(temp1, temp3); |
| 3202 | __ j(kEqual, &done); |
| 3203 | __ Bind(&loop); |
| 3204 | __ pushl(Address(temp1, 0)); |
| 3205 | __ cfi().AdjustCFAOffset(4); |
| 3206 | __ popl(Address(temp2, 0)); |
| 3207 | __ cfi().AdjustCFAOffset(-4); |
| 3208 | __ addl(temp1, Immediate(element_size)); |
| 3209 | __ addl(temp2, Immediate(element_size)); |
| 3210 | __ cmpl(temp1, temp3); |
| 3211 | __ j(kNotEqual, &loop); |
| 3212 | __ Bind(&done); |
| 3213 | } |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3214 | |
| 3215 | // We only need one card marking on the destination array. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 3216 | codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null= */ false); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3217 | |
| Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 3218 | __ Bind(intrinsic_slow_path->GetExitLabel()); |
| Nicolas Geoffray | fea1abd | 2016-07-06 12:09:12 +0100 | [diff] [blame] | 3219 | } |
| 3220 | |
| Vladimir Marko | 01b6552 | 2020-10-28 15:43:54 +0000 | [diff] [blame] | 3221 | static void RequestBaseMethodAddressInRegister(HInvoke* invoke) { |
| 3222 | LocationSummary* locations = invoke->GetLocations(); |
| 3223 | if (locations != nullptr) { |
| 3224 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
| 3225 | // Note: The base method address is not present yet when this is called from the |
| 3226 | // PCRelativeHandlerVisitor via IsCallFreeIntrinsic() to determine whether to insert it. |
| 3227 | if (invoke_static_or_direct->HasSpecialInput()) { |
| 3228 | DCHECK(invoke_static_or_direct->InputAt(invoke_static_or_direct->GetSpecialInputIndex()) |
| 3229 | ->IsX86ComputeBaseMethodAddress()); |
| 3230 | locations->SetInAt(invoke_static_or_direct->GetSpecialInputIndex(), |
| 3231 | Location::RequiresRegister()); |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3236 | void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) { |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3237 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3238 | InvokeRuntimeCallingConvention calling_convention; |
| 3239 | IntrinsicVisitor::ComputeIntegerValueOfLocations( |
| 3240 | invoke, |
| 3241 | codegen_, |
| 3242 | Location::RegisterLocation(EAX), |
| 3243 | Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| Vladimir Marko | 01b6552 | 2020-10-28 15:43:54 +0000 | [diff] [blame] | 3244 | RequestBaseMethodAddressInRegister(invoke); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) { |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3248 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3249 | IntrinsicVisitor::IntegerValueOfInfo info = |
| 3250 | IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions()); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3251 | LocationSummary* locations = invoke->GetLocations(); |
| 3252 | X86Assembler* assembler = GetAssembler(); |
| 3253 | |
| 3254 | Register out = locations->Out().AsRegister<Register>(); |
| Vladimir Marko | de91ca9 | 2020-10-27 13:41:40 +0000 | [diff] [blame] | 3255 | auto allocate_instance = [&]() { |
| 3256 | DCHECK_EQ(out, InvokeRuntimeCallingConvention().GetRegisterAt(0)); |
| 3257 | codegen_->LoadIntrinsicDeclaringClass(out, invoke->AsInvokeStaticOrDirect()); |
| 3258 | codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc()); |
| 3259 | CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>(); |
| 3260 | }; |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3261 | if (invoke->InputAt(0)->IsConstant()) { |
| 3262 | int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3263 | if (static_cast<uint32_t>(value - info.low) < info.length) { |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3264 | // Just embed the j.l.Integer in the code. |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3265 | DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference); |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3266 | codegen_->LoadBootImageAddress( |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3267 | out, info.value_boot_image_reference, invoke->AsInvokeStaticOrDirect()); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3268 | } else { |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3269 | DCHECK(locations->CanCall()); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3270 | // Allocate and initialize a new j.l.Integer. |
| 3271 | // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the |
| 3272 | // JIT object table. |
| Vladimir Marko | de91ca9 | 2020-10-27 13:41:40 +0000 | [diff] [blame] | 3273 | allocate_instance(); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3274 | __ movl(Address(out, info.value_offset), Immediate(value)); |
| 3275 | } |
| 3276 | } else { |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3277 | DCHECK(locations->CanCall()); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3278 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 3279 | // Check bounds of our cache. |
| 3280 | __ leal(out, Address(in, -info.low)); |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3281 | __ cmpl(out, Immediate(info.length)); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3282 | NearLabel allocate, done; |
| 3283 | __ j(kAboveEqual, &allocate); |
| 3284 | // If the value is within the bounds, load the j.l.Integer directly from the array. |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3285 | constexpr size_t kElementSize = sizeof(mirror::HeapReference<mirror::Object>); |
| Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 3286 | static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>), |
| 3287 | "Check heap reference size."); |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3288 | if (codegen_->GetCompilerOptions().IsBootImage()) { |
| 3289 | DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u); |
| 3290 | size_t method_address_index = invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex(); |
| 3291 | HX86ComputeBaseMethodAddress* method_address = |
| 3292 | invoke->InputAt(method_address_index)->AsX86ComputeBaseMethodAddress(); |
| 3293 | DCHECK(method_address != nullptr); |
| 3294 | Register method_address_reg = |
| 3295 | invoke->GetLocations()->InAt(method_address_index).AsRegister<Register>(); |
| Vladimir Marko | 4ef451a | 2020-07-23 09:54:27 +0000 | [diff] [blame] | 3296 | __ movl(out, |
| 3297 | Address(method_address_reg, out, TIMES_4, CodeGeneratorX86::kPlaceholder32BitOffset)); |
| Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 3298 | codegen_->RecordBootImageIntrinsicPatch(method_address, info.array_data_boot_image_reference); |
| 3299 | } else { |
| 3300 | // Note: We're about to clobber the index in `out`, so we need to use `in` and |
| 3301 | // adjust the offset accordingly. |
| 3302 | uint32_t mid_array_boot_image_offset = |
| 3303 | info.array_data_boot_image_reference - info.low * kElementSize; |
| 3304 | codegen_->LoadBootImageAddress( |
| 3305 | out, mid_array_boot_image_offset, invoke->AsInvokeStaticOrDirect()); |
| 3306 | DCHECK_NE(out, in); |
| 3307 | __ movl(out, Address(out, in, TIMES_4, 0)); |
| 3308 | } |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3309 | __ MaybeUnpoisonHeapReference(out); |
| 3310 | __ jmp(&done); |
| 3311 | __ Bind(&allocate); |
| 3312 | // Otherwise allocate and initialize a new j.l.Integer. |
| Vladimir Marko | de91ca9 | 2020-10-27 13:41:40 +0000 | [diff] [blame] | 3313 | allocate_instance(); |
| Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 3314 | __ movl(Address(out, info.value_offset), in); |
| 3315 | __ Bind(&done); |
| 3316 | } |
| 3317 | } |
| 3318 | |
| Vladimir Marko | 01b6552 | 2020-10-28 15:43:54 +0000 | [diff] [blame] | 3319 | void IntrinsicLocationsBuilderX86::VisitReferenceGetReferent(HInvoke* invoke) { |
| 3320 | IntrinsicVisitor::CreateReferenceGetReferentLocations(invoke, codegen_); |
| 3321 | RequestBaseMethodAddressInRegister(invoke); |
| 3322 | } |
| 3323 | |
| 3324 | void IntrinsicCodeGeneratorX86::VisitReferenceGetReferent(HInvoke* invoke) { |
| 3325 | X86Assembler* assembler = GetAssembler(); |
| 3326 | LocationSummary* locations = invoke->GetLocations(); |
| 3327 | |
| 3328 | Location obj = locations->InAt(0); |
| 3329 | Location out = locations->Out(); |
| 3330 | |
| 3331 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke); |
| 3332 | codegen_->AddSlowPath(slow_path); |
| 3333 | |
| 3334 | if (kEmitCompilerReadBarrier) { |
| 3335 | // Check self->GetWeakRefAccessEnabled(). |
| 3336 | ThreadOffset32 offset = Thread::WeakRefAccessEnabledOffset<kX86PointerSize>(); |
| 3337 | __ fs()->cmpl(Address::Absolute(offset), Immediate(0)); |
| 3338 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3339 | } |
| 3340 | |
| 3341 | // Load the java.lang.ref.Reference class, use the output register as a temporary. |
| 3342 | codegen_->LoadIntrinsicDeclaringClass(out.AsRegister<Register>(), |
| 3343 | invoke->AsInvokeStaticOrDirect()); |
| 3344 | |
| 3345 | // Check static fields java.lang.ref.Reference.{disableIntrinsic,slowPathEnabled} together. |
| 3346 | MemberOffset disable_intrinsic_offset = IntrinsicVisitor::GetReferenceDisableIntrinsicOffset(); |
| 3347 | DCHECK_ALIGNED(disable_intrinsic_offset.Uint32Value(), 2u); |
| 3348 | DCHECK_EQ(disable_intrinsic_offset.Uint32Value() + 1u, |
| 3349 | IntrinsicVisitor::GetReferenceSlowPathEnabledOffset().Uint32Value()); |
| 3350 | __ cmpw(Address(out.AsRegister<Register>(), disable_intrinsic_offset.Uint32Value()), |
| 3351 | Immediate(0)); |
| 3352 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 3353 | |
| 3354 | // Load the value from the field. |
| 3355 | uint32_t referent_offset = mirror::Reference::ReferentOffset().Uint32Value(); |
| 3356 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 3357 | codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke, |
| 3358 | out, |
| 3359 | obj.AsRegister<Register>(), |
| 3360 | referent_offset, |
| 3361 | /*needs_null_check=*/ true); |
| 3362 | // Note that the fence is a no-op, thanks to the x86 memory model. |
| 3363 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile. |
| 3364 | } else { |
| 3365 | __ movl(out.AsRegister<Register>(), Address(obj.AsRegister<Register>(), referent_offset)); |
| 3366 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
| 3367 | // Note that the fence is a no-op, thanks to the x86 memory model. |
| 3368 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile. |
| 3369 | codegen_->MaybeGenerateReadBarrierSlow(invoke, out, out, obj, referent_offset); |
| 3370 | } |
| 3371 | __ Bind(slow_path->GetExitLabel()); |
| 3372 | } |
| 3373 | |
| Vladimir Marko | ac27ac0 | 2021-02-01 09:31:02 +0000 | [diff] [blame] | 3374 | void IntrinsicLocationsBuilderX86::VisitReferenceRefersTo(HInvoke* invoke) { |
| 3375 | IntrinsicVisitor::CreateReferenceRefersToLocations(invoke); |
| 3376 | } |
| 3377 | |
| 3378 | void IntrinsicCodeGeneratorX86::VisitReferenceRefersTo(HInvoke* invoke) { |
| 3379 | X86Assembler* assembler = GetAssembler(); |
| 3380 | LocationSummary* locations = invoke->GetLocations(); |
| 3381 | |
| 3382 | Register obj = locations->InAt(0).AsRegister<Register>(); |
| 3383 | Register other = locations->InAt(1).AsRegister<Register>(); |
| 3384 | Register out = locations->Out().AsRegister<Register>(); |
| 3385 | |
| 3386 | uint32_t referent_offset = mirror::Reference::ReferentOffset().Uint32Value(); |
| 3387 | uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value(); |
| 3388 | |
| 3389 | __ movl(out, Address(obj, referent_offset)); |
| 3390 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
| Vladimir Marko | a0a20cd | 2021-02-05 15:55:47 +0000 | [diff] [blame] | 3391 | __ MaybeUnpoisonHeapReference(out); |
| Vladimir Marko | ac27ac0 | 2021-02-01 09:31:02 +0000 | [diff] [blame] | 3392 | // Note that the fence is a no-op, thanks to the x86 memory model. |
| 3393 | codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile. |
| 3394 | |
| 3395 | NearLabel end, return_true, return_false; |
| 3396 | __ cmpl(out, other); |
| 3397 | |
| 3398 | if (kEmitCompilerReadBarrier) { |
| 3399 | DCHECK(kUseBakerReadBarrier); |
| 3400 | |
| 3401 | __ j(kEqual, &return_true); |
| 3402 | |
| 3403 | // Check if the loaded reference is null. |
| 3404 | __ testl(out, out); |
| 3405 | __ j(kZero, &return_false); |
| 3406 | |
| 3407 | // For correct memory visibility, we need a barrier before loading the lock word |
| 3408 | // but we already have the barrier emitted for volatile load above which is sufficient. |
| 3409 | |
| 3410 | // Load the lockword and check if it is a forwarding address. |
| 3411 | static_assert(LockWord::kStateShift == 30u); |
| 3412 | static_assert(LockWord::kStateForwardingAddress == 3u); |
| 3413 | __ movl(out, Address(out, monitor_offset)); |
| 3414 | __ cmpl(out, Immediate(static_cast<int32_t>(0xc0000000))); |
| 3415 | __ j(kBelow, &return_false); |
| 3416 | |
| 3417 | // Extract the forwarding address and compare with `other`. |
| 3418 | __ shll(out, Immediate(LockWord::kForwardingAddressShift)); |
| 3419 | __ cmpl(out, other); |
| 3420 | } |
| 3421 | |
| 3422 | __ j(kNotEqual, &return_false); |
| 3423 | |
| 3424 | // Return true and exit the function. |
| 3425 | __ Bind(&return_true); |
| 3426 | __ movl(out, Immediate(1)); |
| 3427 | __ jmp(&end); |
| 3428 | |
| 3429 | // Return false and exit the function. |
| 3430 | __ Bind(&return_false); |
| 3431 | __ xorl(out, out); |
| 3432 | __ Bind(&end); |
| 3433 | } |
| 3434 | |
| Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 3435 | void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 3436 | LocationSummary* locations = |
| 3437 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 3438 | locations->SetOut(Location::RequiresRegister()); |
| 3439 | } |
| 3440 | |
| 3441 | void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) { |
| 3442 | X86Assembler* assembler = GetAssembler(); |
| 3443 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 3444 | Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value()); |
| 3445 | NearLabel done; |
| 3446 | __ fs()->movl(out, address); |
| 3447 | __ testl(out, out); |
| 3448 | __ j(kEqual, &done); |
| 3449 | __ fs()->movl(address, Immediate(0)); |
| 3450 | codegen_->MemoryFence(); |
| 3451 | __ Bind(&done); |
| 3452 | } |
| 3453 | |
| Hans Boehm | c7b28de | 2018-03-09 17:05:28 -0800 | [diff] [blame] | 3454 | void IntrinsicLocationsBuilderX86::VisitReachabilityFence(HInvoke* invoke) { |
| 3455 | LocationSummary* locations = |
| 3456 | new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified); |
| 3457 | locations->SetInAt(0, Location::Any()); |
| 3458 | } |
| 3459 | |
| 3460 | void IntrinsicCodeGeneratorX86::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { } |
| Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 3461 | |
| Andra Danciu | dc787f4 | 2020-07-07 14:28:56 +0000 | [diff] [blame] | 3462 | void IntrinsicLocationsBuilderX86::VisitIntegerDivideUnsigned(HInvoke* invoke) { |
| 3463 | LocationSummary* locations = new (allocator_) LocationSummary(invoke, |
| 3464 | LocationSummary::kCallOnSlowPath, |
| 3465 | kIntrinsified); |
| 3466 | locations->SetInAt(0, Location::RegisterLocation(EAX)); |
| 3467 | locations->SetInAt(1, Location::RequiresRegister()); |
| 3468 | locations->SetOut(Location::SameAsFirstInput()); |
| 3469 | // Intel uses edx:eax as the dividend. |
| 3470 | locations->AddTemp(Location::RegisterLocation(EDX)); |
| 3471 | } |
| 3472 | |
| 3473 | void IntrinsicCodeGeneratorX86::VisitIntegerDivideUnsigned(HInvoke* invoke) { |
| 3474 | X86Assembler* assembler = GetAssembler(); |
| 3475 | LocationSummary* locations = invoke->GetLocations(); |
| 3476 | Location out = locations->Out(); |
| 3477 | Location first = locations->InAt(0); |
| 3478 | Location second = locations->InAt(1); |
| 3479 | Register edx = locations->GetTemp(0).AsRegister<Register>(); |
| 3480 | Register second_reg = second.AsRegister<Register>(); |
| 3481 | |
| 3482 | DCHECK_EQ(EAX, first.AsRegister<Register>()); |
| 3483 | DCHECK_EQ(EAX, out.AsRegister<Register>()); |
| 3484 | DCHECK_EQ(EDX, edx); |
| 3485 | |
| 3486 | // Check if divisor is zero, bail to managed implementation to handle. |
| 3487 | __ testl(second_reg, second_reg); |
| 3488 | SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 3489 | codegen_->AddSlowPath(slow_path); |
| 3490 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3491 | |
| 3492 | __ xorl(edx, edx); |
| 3493 | __ divl(second_reg); |
| 3494 | |
| 3495 | __ Bind(slow_path->GetExitLabel()); |
| 3496 | } |
| 3497 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3498 | static bool HasVarHandleIntrinsicImplementation(HInvoke* invoke) { |
| 3499 | VarHandleOptimizations optimizations(invoke); |
| 3500 | if (optimizations.GetDoNotIntrinsify()) { |
| 3501 | return false; |
| 3502 | } |
| 3503 | |
| Vladimir Marko | 4a889b7 | 2021-07-21 16:20:54 +0000 | [diff] [blame] | 3504 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3505 | DCHECK_LE(expected_coordinates_count, 2u); // Filtered by the `DoNotIntrinsify` flag above. |
| Vladimir Marko | 4a889b7 | 2021-07-21 16:20:54 +0000 | [diff] [blame] | 3506 | if (expected_coordinates_count > 1u) { |
| 3507 | // Only static and instance fields VarHandle are supported now. |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3508 | // TODO: add support for arrays and views. |
| Vladimir Marko | b3a7a6a | 2021-07-20 15:02:33 +0100 | [diff] [blame] | 3509 | return false; |
| 3510 | } |
| 3511 | |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 3512 | return true; |
| 3513 | } |
| 3514 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3515 | static void GenerateVarHandleAccessModeCheck(Register varhandle_object, |
| 3516 | mirror::VarHandle::AccessMode access_mode, |
| 3517 | SlowPathCode* slow_path, |
| 3518 | X86Assembler* assembler) { |
| 3519 | const uint32_t access_modes_bitmask_offset = |
| 3520 | mirror::VarHandle::AccessModesBitMaskOffset().Uint32Value(); |
| 3521 | const uint32_t access_mode_bit = 1u << static_cast<uint32_t>(access_mode); |
| 3522 | |
| 3523 | // If the access mode is not supported, bail to runtime implementation to handle |
| 3524 | __ testl(Address(varhandle_object, access_modes_bitmask_offset), Immediate(access_mode_bit)); |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3525 | __ j(kZero, slow_path->GetEntryLabel()); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | static void GenerateVarHandleStaticFieldCheck(Register varhandle_object, |
| 3529 | SlowPathCode* slow_path, |
| 3530 | X86Assembler* assembler) { |
| 3531 | const uint32_t coordtype0_offset = mirror::VarHandle::CoordinateType0Offset().Uint32Value(); |
| 3532 | |
| 3533 | // Check that the VarHandle references a static field by checking that coordinateType0 == null. |
| 3534 | // Do not emit read barrier (or unpoison the reference) for comparing to null. |
| 3535 | __ cmpl(Address(varhandle_object, coordtype0_offset), Immediate(0)); |
| 3536 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 3537 | } |
| 3538 | |
| 3539 | static void GenerateSubTypeObjectCheck(Register object, |
| 3540 | Register temp, |
| 3541 | Address type_address, |
| 3542 | SlowPathCode* slow_path, |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3543 | X86Assembler* assembler, |
| 3544 | bool object_can_be_null = true) { |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3545 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 3546 | const uint32_t super_class_offset = mirror::Class::SuperClassOffset().Uint32Value(); |
| 3547 | NearLabel check_type_compatibility, type_matched; |
| 3548 | |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3549 | // If the object is null, there is no need to check the type |
| 3550 | if (object_can_be_null) { |
| 3551 | __ testl(object, object); |
| 3552 | __ j(kZero, &type_matched); |
| 3553 | } |
| 3554 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3555 | // Do not unpoison for in-memory comparison. |
| 3556 | // We deliberately avoid the read barrier, letting the slow path handle the false negatives. |
| 3557 | __ movl(temp, Address(object, class_offset)); |
| 3558 | __ Bind(&check_type_compatibility); |
| 3559 | __ cmpl(temp, type_address); |
| 3560 | __ j(kEqual, &type_matched); |
| 3561 | // Load the super class. |
| 3562 | __ MaybeUnpoisonHeapReference(temp); |
| 3563 | __ movl(temp, Address(temp, super_class_offset)); |
| 3564 | // If the super class is null, we reached the root of the hierarchy without a match. |
| 3565 | // We let the slow path handle uncovered cases (e.g. interfaces). |
| 3566 | __ testl(temp, temp); |
| 3567 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 3568 | __ jmp(&check_type_compatibility); |
| 3569 | __ Bind(&type_matched); |
| 3570 | } |
| 3571 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3572 | static void GenerateVarHandleInstanceFieldChecks(HInvoke* invoke, |
| 3573 | Register temp, |
| 3574 | SlowPathCode* slow_path, |
| 3575 | X86Assembler* assembler) { |
| 3576 | VarHandleOptimizations optimizations(invoke); |
| 3577 | LocationSummary* locations = invoke->GetLocations(); |
| 3578 | Register varhandle_object = locations->InAt(0).AsRegister<Register>(); |
| 3579 | Register object = locations->InAt(1).AsRegister<Register>(); |
| 3580 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3581 | const uint32_t coordtype0_offset = mirror::VarHandle::CoordinateType0Offset().Uint32Value(); |
| 3582 | const uint32_t coordtype1_offset = mirror::VarHandle::CoordinateType1Offset().Uint32Value(); |
| 3583 | |
| 3584 | // Check that the VarHandle references an instance field by checking that |
| 3585 | // coordinateType1 == null. coordinateType0 should be not null, but this is handled by the |
| 3586 | // type compatibility check with the source object's type, which will fail for null. |
| 3587 | __ cmpl(Address(varhandle_object, coordtype1_offset), Immediate(0)); |
| 3588 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 3589 | |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3590 | // Check if the object is null |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3591 | if (!optimizations.GetSkipObjectNullCheck()) { |
| 3592 | __ testl(object, object); |
| 3593 | __ j(kZero, slow_path->GetEntryLabel()); |
| 3594 | } |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3595 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3596 | // Check the object's class against coordinateType0. |
| 3597 | GenerateSubTypeObjectCheck(object, |
| 3598 | temp, |
| 3599 | Address(varhandle_object, coordtype0_offset), |
| 3600 | slow_path, |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3601 | assembler, |
| 3602 | /* object_can_be_null= */ false); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 3605 | static void GenerateVarTypePrimitiveTypeCheck(Register varhandle_object, |
| 3606 | Register temp, |
| 3607 | DataType::Type type, |
| 3608 | SlowPathCode* slow_path, |
| 3609 | X86Assembler* assembler) { |
| 3610 | const uint32_t var_type_offset = mirror::VarHandle::VarTypeOffset().Uint32Value(); |
| 3611 | const uint32_t primitive_type_offset = mirror::Class::PrimitiveTypeOffset().Uint32Value(); |
| 3612 | const uint32_t primitive_type = static_cast<uint32_t>(DataTypeToPrimitive(type)); |
| 3613 | |
| 3614 | // We do not need a read barrier when loading a reference only for loading a constant field |
| 3615 | // through the reference. |
| 3616 | __ movl(temp, Address(varhandle_object, var_type_offset)); |
| 3617 | __ MaybeUnpoisonHeapReference(temp); |
| 3618 | __ cmpw(Address(temp, primitive_type_offset), Immediate(primitive_type)); |
| 3619 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 3620 | } |
| 3621 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3622 | static void GenerateVarHandleCommonChecks(HInvoke *invoke, |
| 3623 | Register temp, |
| 3624 | SlowPathCode* slow_path, |
| 3625 | X86Assembler* assembler) { |
| 3626 | LocationSummary* locations = invoke->GetLocations(); |
| 3627 | Register vh_object = locations->InAt(0).AsRegister<Register>(); |
| 3628 | mirror::VarHandle::AccessMode access_mode = |
| 3629 | mirror::VarHandle::GetAccessModeByIntrinsic(invoke->GetIntrinsic()); |
| 3630 | |
| 3631 | GenerateVarHandleAccessModeCheck(vh_object, |
| 3632 | access_mode, |
| 3633 | slow_path, |
| 3634 | assembler); |
| 3635 | |
| Vladimir Marko | a41ea27 | 2020-09-07 15:24:36 +0000 | [diff] [blame] | 3636 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3637 | switch (expected_coordinates_count) { |
| 3638 | case 0u: |
| 3639 | GenerateVarHandleStaticFieldCheck(vh_object, slow_path, assembler); |
| 3640 | break; |
| 3641 | case 1u: { |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3642 | GenerateVarHandleInstanceFieldChecks(invoke, temp, slow_path, assembler); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3643 | break; |
| 3644 | } |
| 3645 | default: |
| 3646 | // Unimplemented |
| 3647 | UNREACHABLE(); |
| 3648 | } |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3649 | |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 3650 | // Check the return type and varType parameters. |
| 3651 | mirror::VarHandle::AccessModeTemplate access_mode_template = |
| 3652 | mirror::VarHandle::GetAccessModeTemplate(access_mode); |
| 3653 | DataType::Type type = invoke->GetType(); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3654 | |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 3655 | switch (access_mode_template) { |
| 3656 | case mirror::VarHandle::AccessModeTemplate::kGet: |
| 3657 | // Check the varType.primitiveType against the type we're trying to retrieve. Reference types |
| 3658 | // are also checked later by a HCheckCast node as an additional check. |
| 3659 | GenerateVarTypePrimitiveTypeCheck(vh_object, temp, type, slow_path, assembler); |
| 3660 | break; |
| 3661 | case mirror::VarHandle::AccessModeTemplate::kSet: |
| 3662 | case mirror::VarHandle::AccessModeTemplate::kGetAndUpdate: { |
| 3663 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 3664 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| 3665 | |
| 3666 | // Check the varType.primitiveType against the type of the value we're trying to set. |
| 3667 | GenerateVarTypePrimitiveTypeCheck(vh_object, temp, value_type, slow_path, assembler); |
| 3668 | if (value_type == DataType::Type::kReference) { |
| 3669 | const uint32_t var_type_offset = mirror::VarHandle::VarTypeOffset().Uint32Value(); |
| 3670 | |
| 3671 | // If the value type is a reference, check it against the varType. |
| 3672 | GenerateSubTypeObjectCheck(locations->InAt(value_index).AsRegister<Register>(), |
| 3673 | temp, |
| 3674 | Address(vh_object, var_type_offset), |
| 3675 | slow_path, |
| 3676 | assembler); |
| 3677 | } |
| 3678 | break; |
| 3679 | } |
| 3680 | case mirror::VarHandle::AccessModeTemplate::kCompareAndSet: |
| 3681 | case mirror::VarHandle::AccessModeTemplate::kCompareAndExchange: { |
| 3682 | uint32_t new_value_index = invoke->GetNumberOfArguments() - 1; |
| 3683 | uint32_t expected_value_index = invoke->GetNumberOfArguments() - 2; |
| 3684 | DataType::Type value_type = GetDataTypeFromShorty(invoke, new_value_index); |
| 3685 | DCHECK_EQ(value_type, GetDataTypeFromShorty(invoke, expected_value_index)); |
| 3686 | |
| 3687 | // Check the varType.primitiveType against the type of the expected value. |
| 3688 | GenerateVarTypePrimitiveTypeCheck(vh_object, temp, value_type, slow_path, assembler); |
| 3689 | if (value_type == DataType::Type::kReference) { |
| 3690 | const uint32_t var_type_offset = mirror::VarHandle::VarTypeOffset().Uint32Value(); |
| 3691 | |
| 3692 | // If the value type is a reference, check both the expected and the new value against |
| 3693 | // the varType. |
| 3694 | GenerateSubTypeObjectCheck(locations->InAt(new_value_index).AsRegister<Register>(), |
| 3695 | temp, |
| 3696 | Address(vh_object, var_type_offset), |
| 3697 | slow_path, |
| 3698 | assembler); |
| 3699 | GenerateSubTypeObjectCheck(locations->InAt(expected_value_index).AsRegister<Register>(), |
| 3700 | temp, |
| 3701 | Address(vh_object, var_type_offset), |
| 3702 | slow_path, |
| 3703 | assembler); |
| 3704 | } |
| 3705 | break; |
| 3706 | } |
| 3707 | } |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | // This method loads the field's address referred by a field VarHandle (base + offset). |
| 3711 | // The return value is the register containing object's reference (in case of an instance field) |
| 3712 | // or the declaring class (in case of a static field). The declaring class is stored in temp |
| 3713 | // register. Field's offset is loaded to the `offset` register. |
| 3714 | static Register GenerateVarHandleFieldReference(HInvoke* invoke, |
| 3715 | CodeGeneratorX86* codegen, |
| 3716 | Register temp, |
| 3717 | /*out*/ Register offset) { |
| 3718 | X86Assembler* assembler = codegen->GetAssembler(); |
| 3719 | LocationSummary* locations = invoke->GetLocations(); |
| 3720 | const uint32_t artfield_offset = mirror::FieldVarHandle::ArtFieldOffset().Uint32Value(); |
| 3721 | const uint32_t offset_offset = ArtField::OffsetOffset().Uint32Value(); |
| 3722 | const uint32_t declaring_class_offset = ArtField::DeclaringClassOffset().Uint32Value(); |
| 3723 | Register varhandle_object = locations->InAt(0).AsRegister<Register>(); |
| 3724 | |
| 3725 | // Load the ArtField and the offset |
| 3726 | __ movl(temp, Address(varhandle_object, artfield_offset)); |
| 3727 | __ movl(offset, Address(temp, offset_offset)); |
| Vladimir Marko | a41ea27 | 2020-09-07 15:24:36 +0000 | [diff] [blame] | 3728 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3729 | if (expected_coordinates_count == 0) { |
| 3730 | // For static fields, load the declaring class |
| 3731 | InstructionCodeGeneratorX86* instr_codegen = |
| 3732 | down_cast<InstructionCodeGeneratorX86*>(codegen->GetInstructionVisitor()); |
| 3733 | instr_codegen->GenerateGcRootFieldLoad(invoke, |
| 3734 | Location::RegisterLocation(temp), |
| 3735 | Address(temp, declaring_class_offset), |
| 3736 | /* fixup_label= */ nullptr, |
| 3737 | kCompilerReadBarrierOption); |
| 3738 | return temp; |
| 3739 | } |
| 3740 | |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3741 | // For instance fields, return the register containing the object. |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3742 | DCHECK_EQ(expected_coordinates_count, 1u); |
| Andra Danciu | 63c0c2d | 2020-09-07 15:50:40 +0000 | [diff] [blame] | 3743 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3744 | return locations->InAt(1).AsRegister<Register>(); |
| 3745 | } |
| 3746 | |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3747 | static void CreateVarHandleGetLocations(HInvoke* invoke) { |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3748 | // The only read barrier implementation supporting the |
| 3749 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 3750 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 3751 | return; |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3754 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | 0875b0a | 2020-08-28 11:49:44 +0000 | [diff] [blame] | 3755 | return; |
| 3756 | } |
| 3757 | |
| 3758 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 3759 | LocationSummary* locations = new (allocator) LocationSummary( |
| 3760 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| 3761 | locations->SetInAt(0, Location::RequiresRegister()); |
| Vladimir Marko | a41ea27 | 2020-09-07 15:24:36 +0000 | [diff] [blame] | 3762 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3763 | if (expected_coordinates_count == 1u) { |
| Andra Danciu | 0875b0a | 2020-08-28 11:49:44 +0000 | [diff] [blame] | 3764 | // For instance fields, this is the source object. |
| 3765 | locations->SetInAt(1, Location::RequiresRegister()); |
| 3766 | } |
| 3767 | locations->AddTemp(Location::RequiresRegister()); |
| 3768 | |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 3769 | DataType::Type type = invoke->GetType(); |
| Andra Danciu | 0875b0a | 2020-08-28 11:49:44 +0000 | [diff] [blame] | 3770 | switch (DataType::Kind(type)) { |
| 3771 | case DataType::Type::kInt64: |
| 3772 | locations->AddTemp(Location::RequiresRegister()); |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3773 | if (invoke->GetIntrinsic() != Intrinsics::kVarHandleGet) { |
| 3774 | // We need an XmmRegister for Int64 to ensure an atomic load |
| 3775 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 3776 | } |
| Andra Danciu | 0875b0a | 2020-08-28 11:49:44 +0000 | [diff] [blame] | 3777 | FALLTHROUGH_INTENDED; |
| 3778 | case DataType::Type::kInt32: |
| 3779 | case DataType::Type::kReference: |
| 3780 | locations->SetOut(Location::RequiresRegister()); |
| 3781 | break; |
| 3782 | default: |
| 3783 | DCHECK(DataType::IsFloatingPointType(type)); |
| 3784 | locations->AddTemp(Location::RequiresRegister()); |
| 3785 | locations->SetOut(Location::RequiresFpuRegister()); |
| 3786 | } |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3789 | static void GenerateVarHandleGet(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3790 | // The only read barrier implementation supporting the |
| 3791 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 3792 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| Andra Danciu | e74df4c | 2020-08-10 09:35:51 +0000 | [diff] [blame] | 3793 | |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3794 | X86Assembler* assembler = codegen->GetAssembler(); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3795 | LocationSummary* locations = invoke->GetLocations(); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3796 | DataType::Type type = invoke->GetType(); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3797 | DCHECK_NE(type, DataType::Type::kVoid); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3798 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3799 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 3800 | codegen->AddSlowPath(slow_path); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3801 | |
| 3802 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 3803 | |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3804 | Location out = locations->Out(); |
| 3805 | // Use 'out' as a temporary register if it's a core register |
| 3806 | Register offset = |
| 3807 | out.IsRegister() ? out.AsRegister<Register>() : locations->GetTemp(1).AsRegister<Register>(); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3808 | |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3809 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 3810 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 3811 | // declaring class will be placed in 'temp' register. |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3812 | Register ref = GenerateVarHandleFieldReference(invoke, codegen, temp, offset); |
| 3813 | Address field_addr(ref, offset, TIMES_1, 0); |
| Andra Danciu | 1ca6f32 | 2020-08-12 08:58:07 +0000 | [diff] [blame] | 3814 | |
| 3815 | // Load the value from the field |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3816 | if (type == DataType::Type::kReference && kCompilerReadBarrierOption == kWithReadBarrier) { |
| 3817 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
| 3818 | invoke, out, ref, field_addr, /* needs_null_check= */ false); |
| 3819 | } else if (type == DataType::Type::kInt64 && |
| 3820 | invoke->GetIntrinsic() != Intrinsics::kVarHandleGet) { |
| 3821 | XmmRegister xmm_temp = locations->GetTemp(2).AsFpuRegister<XmmRegister>(); |
| Ulya Trafimovich | 322eced | 2021-06-02 15:39:36 +0100 | [diff] [blame] | 3822 | codegen->LoadFromMemoryNoBarrier( |
| 3823 | type, out, field_addr, /* instr= */ nullptr, xmm_temp, /* is_atomic_load= */ true); |
| Andra Danciu | aa35883 | 2020-08-25 15:09:43 +0000 | [diff] [blame] | 3824 | } else { |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3825 | codegen->LoadFromMemoryNoBarrier(type, out, field_addr); |
| 3826 | } |
| 3827 | |
| 3828 | if (invoke->GetIntrinsic() == Intrinsics::kVarHandleGetVolatile || |
| 3829 | invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAcquire) { |
| 3830 | // Load fence to prevent load-load reordering. |
| 3831 | // Note that this is a no-op, thanks to the x86 memory model. |
| 3832 | codegen->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | __ Bind(slow_path->GetExitLabel()); |
| 3836 | } |
| 3837 | |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 3838 | void IntrinsicLocationsBuilderX86::VisitVarHandleGet(HInvoke* invoke) { |
| 3839 | CreateVarHandleGetLocations(invoke); |
| 3840 | } |
| 3841 | |
| 3842 | void IntrinsicCodeGeneratorX86::VisitVarHandleGet(HInvoke* invoke) { |
| 3843 | GenerateVarHandleGet(invoke, codegen_); |
| 3844 | } |
| 3845 | |
| 3846 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetVolatile(HInvoke* invoke) { |
| 3847 | CreateVarHandleGetLocations(invoke); |
| 3848 | } |
| 3849 | |
| 3850 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetVolatile(HInvoke* invoke) { |
| 3851 | GenerateVarHandleGet(invoke, codegen_); |
| 3852 | } |
| 3853 | |
| 3854 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAcquire(HInvoke* invoke) { |
| 3855 | CreateVarHandleGetLocations(invoke); |
| 3856 | } |
| 3857 | |
| 3858 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAcquire(HInvoke* invoke) { |
| 3859 | GenerateVarHandleGet(invoke, codegen_); |
| 3860 | } |
| 3861 | |
| 3862 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetOpaque(HInvoke* invoke) { |
| 3863 | CreateVarHandleGetLocations(invoke); |
| 3864 | } |
| 3865 | |
| 3866 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetOpaque(HInvoke* invoke) { |
| 3867 | GenerateVarHandleGet(invoke, codegen_); |
| 3868 | } |
| 3869 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3870 | static void CreateVarHandleSetLocations(HInvoke* invoke) { |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3871 | // The only read barrier implementation supporting the |
| 3872 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 3873 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 3874 | return; |
| 3875 | } |
| 3876 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 3877 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3878 | return; |
| 3879 | } |
| 3880 | |
| 3881 | // The last argument should be the value we intend to set. |
| 3882 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 3883 | HInstruction* value = invoke->InputAt(value_index); |
| 3884 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| Andra Danciu | 71b26b2 | 2020-09-20 09:01:38 +0000 | [diff] [blame] | 3885 | bool needs_atomicity = invoke->GetIntrinsic() != Intrinsics::kVarHandleSet; |
| 3886 | if (value_type == DataType::Type::kInt64 && (!value->IsConstant() || needs_atomicity)) { |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3887 | // We avoid the case of a non-constant (or volatile) Int64 value because we would need to |
| 3888 | // place it in a register pair. If the slow path is taken, the ParallelMove might fail to move |
| 3889 | // the pair according to the X86DexCallingConvention in case of an overlap (e.g., move the |
| 3890 | // int64 value from <EAX, EBX> to <EBX, ECX>). (Bug: b/168687887) |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3891 | return; |
| 3892 | } |
| 3893 | |
| 3894 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 3895 | LocationSummary* locations = new (allocator) LocationSummary( |
| 3896 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3897 | locations->SetInAt(0, Location::RequiresRegister()); |
| Vladimir Marko | a41ea27 | 2020-09-07 15:24:36 +0000 | [diff] [blame] | 3898 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3899 | if (expected_coordinates_count == 1u) { |
| 3900 | // For instance fields, this is the source object |
| 3901 | locations->SetInAt(1, Location::RequiresRegister()); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | switch (value_type) { |
| 3905 | case DataType::Type::kBool: |
| 3906 | case DataType::Type::kInt8: |
| 3907 | case DataType::Type::kUint8: |
| 3908 | // Ensure the value is in a byte register |
| 3909 | locations->SetInAt(value_index, Location::ByteRegisterOrConstant(EBX, value)); |
| 3910 | break; |
| 3911 | case DataType::Type::kInt16: |
| 3912 | case DataType::Type::kUint16: |
| 3913 | case DataType::Type::kInt32: |
| 3914 | locations->SetInAt(value_index, Location::RegisterOrConstant(value)); |
| 3915 | break; |
| 3916 | case DataType::Type::kInt64: |
| Andra Danciu | 71b26b2 | 2020-09-20 09:01:38 +0000 | [diff] [blame] | 3917 | // We only handle constant non-atomic int64 values. |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3918 | DCHECK(value->IsConstant()); |
| 3919 | locations->SetInAt(value_index, Location::ConstantLocation(value->AsConstant())); |
| 3920 | break; |
| 3921 | case DataType::Type::kReference: |
| 3922 | locations->SetInAt(value_index, Location::RequiresRegister()); |
| 3923 | break; |
| 3924 | default: |
| 3925 | DCHECK(DataType::IsFloatingPointType(value_type)); |
| Andra Danciu | 71b26b2 | 2020-09-20 09:01:38 +0000 | [diff] [blame] | 3926 | if (needs_atomicity && value_type == DataType::Type::kFloat64) { |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3927 | locations->SetInAt(value_index, Location::RequiresFpuRegister()); |
| 3928 | } else { |
| 3929 | locations->SetInAt(value_index, Location::FpuRegisterOrConstant(value)); |
| 3930 | } |
| 3931 | } |
| 3932 | |
| 3933 | locations->AddTemp(Location::RequiresRegister()); |
| 3934 | // This temporary register is also used for card for MarkGCCard. Make sure it's a byte register |
| 3935 | locations->AddTemp(Location::RegisterLocation(EAX)); |
| 3936 | if (expected_coordinates_count == 0 && value_type == DataType::Type::kReference) { |
| 3937 | // For static reference fields, we need another temporary for the declaring class. We set it |
| 3938 | // last because we want to make sure that the first 2 temps are reserved for HandleFieldSet. |
| 3939 | locations->AddTemp(Location::RequiresRegister()); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
| 3942 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3943 | static void GenerateVarHandleSet(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3944 | // The only read barrier implementation supporting the |
| 3945 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 3946 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 3947 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3948 | X86Assembler* assembler = codegen->GetAssembler(); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3949 | LocationSummary* locations = invoke->GetLocations(); |
| 3950 | // The value we want to set is the last argument |
| 3951 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3952 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3953 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 3954 | Register temp2 = locations->GetTemp(1).AsRegister<Register>(); |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3955 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 3956 | codegen->AddSlowPath(slow_path); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3957 | |
| 3958 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| 3959 | |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 3960 | // For static reference fields, we need another temporary for the declaring class. But since |
| 3961 | // for instance fields the object is in a separate register, it is safe to use the first |
| 3962 | // temporary register for GenerateVarHandleFieldReference. |
| 3963 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 3964 | if (value_type == DataType::Type::kReference && expected_coordinates_count == 0) { |
| 3965 | temp = locations->GetTemp(2).AsRegister<Register>(); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | Register offset = temp2; |
| 3969 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 3970 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 3971 | // declaring class will be placed in 'temp' register. |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3972 | Register reference = GenerateVarHandleFieldReference(invoke, codegen, temp, offset); |
| Andra Danciu | 73c3180 | 2020-09-01 13:17:05 +0000 | [diff] [blame] | 3973 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3974 | bool is_volatile = false; |
| 3975 | switch (invoke->GetIntrinsic()) { |
| 3976 | case Intrinsics::kVarHandleSet: |
| 3977 | case Intrinsics::kVarHandleSetOpaque: |
| 3978 | // The only constraint for setOpaque is to ensure bitwise atomicity (atomically set 64 bit |
| 3979 | // values), but we don't treat Int64 values because we would need to place it in a register |
| 3980 | // pair. If the slow path is taken, the Parallel move might fail to move the register pair |
| 3981 | // in case of an overlap (e.g., move from <EAX, EBX> to <EBX, ECX>). (Bug: b/168687887) |
| 3982 | break; |
| Andra Danciu | 71b26b2 | 2020-09-20 09:01:38 +0000 | [diff] [blame] | 3983 | case Intrinsics::kVarHandleSetRelease: |
| 3984 | // setRelease needs to ensure atomicity too. See the above comment. |
| 3985 | codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyStore); |
| 3986 | break; |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3987 | case Intrinsics::kVarHandleSetVolatile: |
| 3988 | is_volatile = true; |
| 3989 | break; |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3990 | default: |
| 3991 | LOG(FATAL) << "GenerateVarHandleSet received non-set intrinsic " << invoke->GetIntrinsic(); |
| Andra Danciu | aa35883 | 2020-08-25 15:09:43 +0000 | [diff] [blame] | 3992 | } |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 3993 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 3994 | InstructionCodeGeneratorX86* instr_codegen = |
| 3995 | down_cast<InstructionCodeGeneratorX86*>(codegen->GetInstructionVisitor()); |
| 3996 | // Store the value to the field |
| 3997 | instr_codegen->HandleFieldSet(invoke, |
| 3998 | value_index, |
| 3999 | value_type, |
| 4000 | Address(reference, offset, TIMES_1, 0), |
| 4001 | reference, |
| 4002 | is_volatile, |
| 4003 | /* value_can_be_null */ true); |
| 4004 | |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 4005 | __ Bind(slow_path->GetExitLabel()); |
| 4006 | } |
| 4007 | |
| Andra Danciu | cde9819 | 2020-09-13 12:32:09 +0000 | [diff] [blame] | 4008 | void IntrinsicLocationsBuilderX86::VisitVarHandleSet(HInvoke* invoke) { |
| 4009 | CreateVarHandleSetLocations(invoke); |
| 4010 | } |
| 4011 | |
| 4012 | void IntrinsicCodeGeneratorX86::VisitVarHandleSet(HInvoke* invoke) { |
| 4013 | GenerateVarHandleSet(invoke, codegen_); |
| 4014 | } |
| 4015 | |
| 4016 | void IntrinsicLocationsBuilderX86::VisitVarHandleSetVolatile(HInvoke* invoke) { |
| 4017 | CreateVarHandleSetLocations(invoke); |
| 4018 | } |
| 4019 | |
| 4020 | void IntrinsicCodeGeneratorX86::VisitVarHandleSetVolatile(HInvoke* invoke) { |
| 4021 | GenerateVarHandleSet(invoke, codegen_); |
| 4022 | } |
| 4023 | |
| 4024 | void IntrinsicLocationsBuilderX86::VisitVarHandleSetRelease(HInvoke* invoke) { |
| 4025 | CreateVarHandleSetLocations(invoke); |
| 4026 | } |
| 4027 | |
| 4028 | void IntrinsicCodeGeneratorX86::VisitVarHandleSetRelease(HInvoke* invoke) { |
| 4029 | GenerateVarHandleSet(invoke, codegen_); |
| 4030 | } |
| 4031 | |
| 4032 | void IntrinsicLocationsBuilderX86::VisitVarHandleSetOpaque(HInvoke* invoke) { |
| 4033 | CreateVarHandleSetLocations(invoke); |
| 4034 | } |
| 4035 | |
| 4036 | void IntrinsicCodeGeneratorX86::VisitVarHandleSetOpaque(HInvoke* invoke) { |
| 4037 | GenerateVarHandleSet(invoke, codegen_); |
| 4038 | } |
| 4039 | |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 4040 | static void CreateVarHandleGetAndSetLocations(HInvoke* invoke) { |
| 4041 | // The only read barrier implementation supporting the |
| 4042 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4043 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 4044 | return; |
| 4045 | } |
| 4046 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 4047 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | 9dfb1a9 | 2020-09-22 13:27:18 +0000 | [diff] [blame] | 4048 | return; |
| 4049 | } |
| 4050 | |
| 4051 | uint32_t number_of_arguments = invoke->GetNumberOfArguments(); |
| 4052 | uint32_t value_index = number_of_arguments - 1; |
| 4053 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| 4054 | |
| 4055 | if (DataType::Is64BitType(value_type)) { |
| 4056 | // We avoid the case of an Int64/Float64 value because we would need to place it in a register |
| 4057 | // pair. If the slow path is taken, the ParallelMove might fail to move the pair according to |
| 4058 | // the X86DexCallingConvention in case of an overlap (e.g., move the 64 bit value from |
| 4059 | // <EAX, EBX> to <EBX, ECX>). |
| 4060 | return; |
| 4061 | } |
| 4062 | |
| 4063 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 4064 | LocationSummary* locations = new (allocator) LocationSummary( |
| 4065 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| 4066 | locations->AddTemp(Location::RequiresRegister()); |
| 4067 | locations->AddTemp(Location::RequiresRegister()); |
| 4068 | // We use this temporary for the card, so we need a byte register |
| 4069 | locations->AddTemp(Location::RegisterLocation(EBX)); |
| 4070 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4071 | if (GetExpectedVarHandleCoordinatesCount(invoke) == 1u) { |
| 4072 | // For instance fields, this is the source object |
| 4073 | locations->SetInAt(1, Location::RequiresRegister()); |
| 4074 | } else { |
| 4075 | // For static fields, we need another temp because one will be busy with the declaring class. |
| 4076 | locations->AddTemp(Location::RequiresRegister()); |
| 4077 | } |
| 4078 | if (value_type == DataType::Type::kFloat32) { |
| 4079 | locations->AddTemp(Location::RegisterLocation(EAX)); |
| 4080 | locations->SetInAt(value_index, Location::FpuRegisterOrConstant(invoke->InputAt(value_index))); |
| 4081 | locations->SetOut(Location::RequiresFpuRegister()); |
| 4082 | } else { |
| 4083 | locations->SetInAt(value_index, Location::RegisterLocation(EAX)); |
| 4084 | locations->SetOut(Location::RegisterLocation(EAX)); |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | static void GenerateVarHandleGetAndSet(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| 4089 | // The only read barrier implementation supporting the |
| 4090 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4091 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 4092 | |
| 4093 | X86Assembler* assembler = codegen->GetAssembler(); |
| 4094 | LocationSummary* locations = invoke->GetLocations(); |
| 4095 | // The value we want to set is the last argument |
| 4096 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 4097 | Location value = locations->InAt(value_index); |
| 4098 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| 4099 | Register temp = locations->GetTemp(1).AsRegister<Register>(); |
| 4100 | Register temp2 = locations->GetTemp(2).AsRegister<Register>(); |
| 4101 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 4102 | codegen->AddSlowPath(slow_path); |
| 4103 | |
| 4104 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| 4105 | |
| 4106 | Register offset = locations->GetTemp(0).AsRegister<Register>(); |
| 4107 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 4108 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 4109 | // declaring class will be placed in 'temp' register. |
| 4110 | Register reference = GenerateVarHandleFieldReference(invoke, codegen, temp, offset); |
| 4111 | Address field_addr(reference, offset, TIMES_1, 0); |
| 4112 | |
| 4113 | if (invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndSetRelease) { |
| 4114 | codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyStore); |
| 4115 | } |
| 4116 | |
| 4117 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4118 | // For static fields, we need another temporary for the declaring class. But since for instance |
| 4119 | // fields the object is in a separate register, it is safe to use the first temporary register. |
| 4120 | temp = expected_coordinates_count == 1u ? temp : locations->GetTemp(3).AsRegister<Register>(); |
| 4121 | // No need for a lock prefix. `xchg` has an implicit lock when it is used with an address. |
| 4122 | switch (value_type) { |
| 4123 | case DataType::Type::kBool: |
| 4124 | __ xchgb(value.AsRegister<ByteRegister>(), field_addr); |
| 4125 | __ movzxb(locations->Out().AsRegister<Register>(), |
| 4126 | locations->Out().AsRegister<ByteRegister>()); |
| 4127 | break; |
| 4128 | case DataType::Type::kInt8: |
| 4129 | __ xchgb(value.AsRegister<ByteRegister>(), field_addr); |
| 4130 | __ movsxb(locations->Out().AsRegister<Register>(), |
| 4131 | locations->Out().AsRegister<ByteRegister>()); |
| 4132 | break; |
| 4133 | case DataType::Type::kUint16: |
| 4134 | __ xchgw(value.AsRegister<Register>(), field_addr); |
| 4135 | __ movzxw(locations->Out().AsRegister<Register>(), locations->Out().AsRegister<Register>()); |
| 4136 | break; |
| 4137 | case DataType::Type::kInt16: |
| 4138 | __ xchgw(value.AsRegister<Register>(), field_addr); |
| 4139 | __ movsxw(locations->Out().AsRegister<Register>(), locations->Out().AsRegister<Register>()); |
| 4140 | break; |
| 4141 | case DataType::Type::kInt32: |
| 4142 | __ xchgl(value.AsRegister<Register>(), field_addr); |
| 4143 | break; |
| 4144 | case DataType::Type::kFloat32: |
| 4145 | codegen->Move32(Location::RegisterLocation(EAX), value); |
| 4146 | __ xchgl(EAX, field_addr); |
| 4147 | __ movd(locations->Out().AsFpuRegister<XmmRegister>(), EAX); |
| 4148 | break; |
| 4149 | case DataType::Type::kReference: { |
| 4150 | if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { |
| 4151 | // Need to make sure the reference stored in the field is a to-space |
| 4152 | // one before attempting the CAS or the CAS could fail incorrectly. |
| 4153 | codegen->GenerateReferenceLoadWithBakerReadBarrier( |
| 4154 | invoke, |
| 4155 | // Unused, used only as a "temporary" within the read barrier. |
| 4156 | Location::RegisterLocation(temp), |
| 4157 | reference, |
| 4158 | field_addr, |
| 4159 | /* needs_null_check= */ false, |
| 4160 | /* always_update_field= */ true, |
| 4161 | &temp2); |
| 4162 | } |
| 4163 | codegen->MarkGCCard( |
| 4164 | temp, temp2, reference, value.AsRegister<Register>(), /* value_can_be_null= */ false); |
| 4165 | if (kPoisonHeapReferences) { |
| 4166 | __ movl(temp, value.AsRegister<Register>()); |
| 4167 | __ PoisonHeapReference(temp); |
| 4168 | __ xchgl(temp, field_addr); |
| 4169 | __ UnpoisonHeapReference(temp); |
| 4170 | __ movl(locations->Out().AsRegister<Register>(), temp); |
| 4171 | } else { |
| 4172 | __ xchgl(locations->Out().AsRegister<Register>(), field_addr); |
| 4173 | } |
| 4174 | break; |
| 4175 | } |
| 4176 | default: |
| 4177 | UNREACHABLE(); |
| 4178 | } |
| 4179 | |
| 4180 | if (invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndSetAcquire) { |
| 4181 | codegen->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 4182 | } |
| 4183 | |
| 4184 | __ Bind(slow_path->GetExitLabel()); |
| 4185 | } |
| 4186 | |
| 4187 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndSet(HInvoke* invoke) { |
| 4188 | CreateVarHandleGetAndSetLocations(invoke); |
| 4189 | } |
| 4190 | |
| 4191 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndSet(HInvoke* invoke) { |
| 4192 | GenerateVarHandleGetAndSet(invoke, codegen_); |
| 4193 | } |
| 4194 | |
| 4195 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndSetAcquire(HInvoke* invoke) { |
| 4196 | CreateVarHandleGetAndSetLocations(invoke); |
| 4197 | } |
| 4198 | |
| 4199 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndSetAcquire(HInvoke* invoke) { |
| 4200 | GenerateVarHandleGetAndSet(invoke, codegen_); |
| 4201 | } |
| 4202 | |
| 4203 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndSetRelease(HInvoke* invoke) { |
| 4204 | CreateVarHandleGetAndSetLocations(invoke); |
| 4205 | } |
| 4206 | |
| 4207 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndSetRelease(HInvoke* invoke) { |
| 4208 | GenerateVarHandleGetAndSet(invoke, codegen_); |
| 4209 | } |
| 4210 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4211 | static void CreateVarHandleCompareAndSetOrExchangeLocations(HInvoke* invoke) { |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4212 | // The only read barrier implementation supporting the |
| 4213 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4214 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 4215 | return; |
| 4216 | } |
| 4217 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 4218 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4219 | return; |
| 4220 | } |
| 4221 | |
| 4222 | uint32_t number_of_arguments = invoke->GetNumberOfArguments(); |
| 4223 | uint32_t expected_value_index = number_of_arguments - 2; |
| 4224 | uint32_t new_value_index = number_of_arguments - 1; |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4225 | DataType::Type value_type = GetDataTypeFromShorty(invoke, expected_value_index); |
| 4226 | DCHECK_EQ(value_type, GetDataTypeFromShorty(invoke, new_value_index)); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4227 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4228 | if (DataType::Is64BitType(value_type)) { |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4229 | // We avoid the case of an Int64/Float64 value because we would need to place it in a register |
| 4230 | // pair. If the slow path is taken, the ParallelMove might fail to move the pair according to |
| 4231 | // the X86DexCallingConvention in case of an overlap (e.g., move the 64 bit value from |
| 4232 | // <EAX, EBX> to <EBX, ECX>). |
| 4233 | return; |
| 4234 | } |
| 4235 | |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4236 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 4237 | LocationSummary* locations = new (allocator) LocationSummary( |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4238 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4239 | locations->AddTemp(Location::RequiresRegister()); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4240 | locations->AddTemp(Location::RequiresRegister()); |
| 4241 | // We use this temporary for the card, so we need a byte register |
| 4242 | locations->AddTemp(Location::RegisterLocation(EBX)); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4243 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4244 | if (GetExpectedVarHandleCoordinatesCount(invoke) == 1u) { |
| 4245 | // For instance fields, this is the source object |
| 4246 | locations->SetInAt(1, Location::RequiresRegister()); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4247 | } else { |
| 4248 | // For static fields, we need another temp because one will be busy with the declaring class. |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4249 | locations->AddTemp(Location::RequiresRegister()); |
| 4250 | } |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4251 | if (DataType::IsFloatingPointType(value_type)) { |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4252 | // We need EAX for placing the expected value |
| 4253 | locations->AddTemp(Location::RegisterLocation(EAX)); |
| 4254 | locations->SetInAt(new_value_index, |
| 4255 | Location::FpuRegisterOrConstant(invoke->InputAt(new_value_index))); |
| 4256 | locations->SetInAt(expected_value_index, |
| 4257 | Location::FpuRegisterOrConstant(invoke->InputAt(expected_value_index))); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4258 | } else { |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4259 | // Ensure it's in a byte register |
| 4260 | locations->SetInAt(new_value_index, Location::RegisterLocation(ECX)); |
| 4261 | locations->SetInAt(expected_value_index, Location::RegisterLocation(EAX)); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4264 | mirror::VarHandle::AccessModeTemplate access_mode_template = |
| 4265 | mirror::VarHandle::GetAccessModeTemplateByIntrinsic(invoke->GetIntrinsic()); |
| 4266 | |
| 4267 | if (access_mode_template == mirror::VarHandle::AccessModeTemplate::kCompareAndExchange && |
| 4268 | value_type == DataType::Type::kFloat32) { |
| 4269 | locations->SetOut(Location::RequiresFpuRegister()); |
| 4270 | } else { |
| 4271 | locations->SetOut(Location::RegisterLocation(EAX)); |
| 4272 | } |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4275 | static void GenerateVarHandleCompareAndSetOrExchange(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4276 | // The only read barrier implementation supporting the |
| 4277 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4278 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 4279 | |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4280 | X86Assembler* assembler = codegen->GetAssembler(); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4281 | LocationSummary* locations = invoke->GetLocations(); |
| 4282 | uint32_t number_of_arguments = invoke->GetNumberOfArguments(); |
| 4283 | uint32_t expected_value_index = number_of_arguments - 2; |
| 4284 | uint32_t new_value_index = number_of_arguments - 1; |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4285 | DataType::Type type = GetDataTypeFromShorty(invoke, expected_value_index); |
| 4286 | DCHECK_EQ(type, GetDataTypeFromShorty(invoke, new_value_index)); |
| 4287 | Location expected_value = locations->InAt(expected_value_index); |
| 4288 | Location new_value = locations->InAt(new_value_index); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4289 | Register offset = locations->GetTemp(0).AsRegister<Register>(); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4290 | Register temp = locations->GetTemp(1).AsRegister<Register>(); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4291 | Register temp2 = locations->GetTemp(2).AsRegister<Register>(); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4292 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 4293 | codegen->AddSlowPath(slow_path); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4294 | |
| 4295 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4296 | |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4297 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 4298 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 4299 | // declaring class will be placed in 'temp' register. |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4300 | Register reference = GenerateVarHandleFieldReference(invoke, codegen, temp, offset); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4301 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4302 | uint32_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4303 | // For generating the compare and exchange, we need 2 temporaries. In case of a static field, the |
| 4304 | // first temporary contains the declaring class so we need another temporary. In case of an |
| 4305 | // instance field, the object comes in a separate register so it's safe to use the first temp. |
| 4306 | temp = (expected_coordinates_count == 1u) ? temp : locations->GetTemp(3).AsRegister<Register>(); |
| 4307 | DCHECK_NE(temp, reference); |
| 4308 | |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4309 | // We are using `lock cmpxchg` in all cases because there is no CAS equivalent that has weak |
| 4310 | // failure semantics. `lock cmpxchg` has full barrier semantics, and we don't need scheduling |
| 4311 | // barriers at this time. |
| 4312 | |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4313 | mirror::VarHandle::AccessModeTemplate access_mode_template = |
| 4314 | mirror::VarHandle::GetAccessModeTemplateByIntrinsic(invoke->GetIntrinsic()); |
| 4315 | bool is_cmpxchg = |
| 4316 | access_mode_template == mirror::VarHandle::AccessModeTemplate::kCompareAndExchange; |
| 4317 | |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4318 | if (type == DataType::Type::kReference) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4319 | GenReferenceCAS( |
| 4320 | invoke, codegen, expected_value, new_value, reference, offset, temp, temp2, is_cmpxchg); |
| Andra Danciu | 8d8380a | 2020-09-11 09:24:01 +0000 | [diff] [blame] | 4321 | } else { |
| 4322 | Location out = locations->Out(); |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4323 | GenPrimitiveCAS( |
| 4324 | type, codegen, expected_value, new_value, reference, offset, out, temp, is_cmpxchg); |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
| Andra Danciu | 5e13d45 | 2020-09-08 14:35:09 +0000 | [diff] [blame] | 4327 | __ Bind(slow_path->GetExitLabel()); |
| 4328 | } |
| Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 4329 | |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4330 | void IntrinsicLocationsBuilderX86::VisitVarHandleCompareAndSet(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4331 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | void IntrinsicCodeGeneratorX86::VisitVarHandleCompareAndSet(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4335 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | void IntrinsicLocationsBuilderX86::VisitVarHandleWeakCompareAndSet(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4339 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | void IntrinsicCodeGeneratorX86::VisitVarHandleWeakCompareAndSet(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4343 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | void IntrinsicLocationsBuilderX86::VisitVarHandleWeakCompareAndSetPlain(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4347 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | void IntrinsicCodeGeneratorX86::VisitVarHandleWeakCompareAndSetPlain(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4351 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | void IntrinsicLocationsBuilderX86::VisitVarHandleWeakCompareAndSetAcquire(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4355 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | void IntrinsicCodeGeneratorX86::VisitVarHandleWeakCompareAndSetAcquire(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4359 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | void IntrinsicLocationsBuilderX86::VisitVarHandleWeakCompareAndSetRelease(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4363 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | void IntrinsicCodeGeneratorX86::VisitVarHandleWeakCompareAndSetRelease(HInvoke* invoke) { |
| Andra Danciu | 370948e | 2020-09-23 08:07:25 +0000 | [diff] [blame] | 4367 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| 4368 | } |
| 4369 | |
| 4370 | void IntrinsicLocationsBuilderX86::VisitVarHandleCompareAndExchange(HInvoke* invoke) { |
| 4371 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| 4372 | } |
| 4373 | |
| 4374 | void IntrinsicCodeGeneratorX86::VisitVarHandleCompareAndExchange(HInvoke* invoke) { |
| 4375 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| 4376 | } |
| 4377 | |
| 4378 | void IntrinsicLocationsBuilderX86::VisitVarHandleCompareAndExchangeAcquire(HInvoke* invoke) { |
| 4379 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| 4380 | } |
| 4381 | |
| 4382 | void IntrinsicCodeGeneratorX86::VisitVarHandleCompareAndExchangeAcquire(HInvoke* invoke) { |
| 4383 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| 4384 | } |
| 4385 | |
| 4386 | void IntrinsicLocationsBuilderX86::VisitVarHandleCompareAndExchangeRelease(HInvoke* invoke) { |
| 4387 | CreateVarHandleCompareAndSetOrExchangeLocations(invoke); |
| 4388 | } |
| 4389 | |
| 4390 | void IntrinsicCodeGeneratorX86::VisitVarHandleCompareAndExchangeRelease(HInvoke* invoke) { |
| 4391 | GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_); |
| Andra Danciu | 52d2c0c | 2020-09-15 14:27:21 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| Andra Danciu | 49cde24 | 2020-09-22 08:38:50 +0000 | [diff] [blame] | 4394 | static void CreateVarHandleGetAndAddLocations(HInvoke* invoke) { |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4395 | // The only read barrier implementation supporting the |
| 4396 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4397 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 4398 | return; |
| 4399 | } |
| 4400 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 4401 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4402 | return; |
| 4403 | } |
| 4404 | |
| 4405 | // The last argument should be the value we intend to set. |
| 4406 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 4407 | DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index); |
| 4408 | if (DataType::Is64BitType(value_type)) { |
| 4409 | // We avoid the case of an Int64/Float64 value because we would need to place it in a register |
| 4410 | // pair. If the slow path is taken, the ParallelMove might fail to move the pair according to |
| 4411 | // the X86DexCallingConvention in case of an overlap (e.g., move the 64 bit value from |
| 4412 | // <EAX, EBX> to <EBX, ECX>). (Bug: b/168687887) |
| 4413 | return; |
| 4414 | } |
| 4415 | |
| 4416 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 4417 | LocationSummary* locations = new (allocator) LocationSummary( |
| 4418 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| 4419 | locations->AddTemp(Location::RequiresRegister()); |
| 4420 | locations->AddTemp(Location::RequiresRegister()); |
| 4421 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4422 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4423 | if (expected_coordinates_count == 1u) { |
| 4424 | // For instance fields, this is the source object |
| 4425 | locations->SetInAt(1, Location::RequiresRegister()); |
| 4426 | } else { |
| 4427 | // For static fields, we need another temp because one will be busy with the declaring class. |
| 4428 | locations->AddTemp(Location::RequiresRegister()); |
| 4429 | } |
| 4430 | |
| 4431 | if (DataType::IsFloatingPointType(value_type)) { |
| 4432 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 4433 | locations->AddTemp(Location::RegisterLocation(EAX)); |
| 4434 | locations->SetInAt(value_index, Location::RequiresFpuRegister()); |
| 4435 | locations->SetOut(Location::RequiresFpuRegister()); |
| 4436 | } else { |
| 4437 | // xadd updates the register argument with the old value. ByteRegister required for xaddb. |
| 4438 | locations->SetInAt(value_index, Location::RegisterLocation(EAX)); |
| 4439 | locations->SetOut(Location::RegisterLocation(EAX)); |
| 4440 | } |
| 4441 | } |
| 4442 | |
| Andra Danciu | 49cde24 | 2020-09-22 08:38:50 +0000 | [diff] [blame] | 4443 | static void GenerateVarHandleGetAndAdd(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4444 | // The only read barrier implementation supporting the |
| 4445 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4446 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 4447 | |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4448 | X86Assembler* assembler = codegen->GetAssembler(); |
| 4449 | LocationSummary* locations = invoke->GetLocations(); |
| 4450 | uint32_t number_of_arguments = invoke->GetNumberOfArguments(); |
| 4451 | uint32_t value_index = number_of_arguments - 1; |
| 4452 | DataType::Type type = GetDataTypeFromShorty(invoke, value_index); |
| 4453 | DCHECK_EQ(type, invoke->GetType()); |
| 4454 | Location value_loc = locations->InAt(value_index); |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4455 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 4456 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 4457 | codegen->AddSlowPath(slow_path); |
| 4458 | |
| 4459 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| 4460 | |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4461 | Register offset = locations->GetTemp(1).AsRegister<Register>(); |
| 4462 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 4463 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 4464 | // declaring class will be placed in 'temp' register. |
| 4465 | Register reference = GenerateVarHandleFieldReference(invoke, codegen, temp, offset); |
| 4466 | |
| 4467 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4468 | temp = (expected_coordinates_count == 1u) ? temp : locations->GetTemp(2).AsRegister<Register>(); |
| 4469 | DCHECK_NE(temp, reference); |
| 4470 | Address field_addr(reference, offset, TIMES_1, 0); |
| 4471 | |
| 4472 | switch (type) { |
| 4473 | case DataType::Type::kInt8: |
| 4474 | __ LockXaddb(field_addr, value_loc.AsRegister<ByteRegister>()); |
| 4475 | __ movsxb(locations->Out().AsRegister<Register>(), |
| 4476 | locations->Out().AsRegister<ByteRegister>()); |
| 4477 | break; |
| 4478 | case DataType::Type::kInt16: |
| 4479 | __ LockXaddw(field_addr, value_loc.AsRegister<Register>()); |
| 4480 | __ movsxw(locations->Out().AsRegister<Register>(), locations->Out().AsRegister<Register>()); |
| 4481 | break; |
| 4482 | case DataType::Type::kUint16: |
| 4483 | __ LockXaddw(field_addr, value_loc.AsRegister<Register>()); |
| 4484 | __ movzxw(locations->Out().AsRegister<Register>(), locations->Out().AsRegister<Register>()); |
| 4485 | break; |
| 4486 | case DataType::Type::kInt32: |
| 4487 | __ LockXaddl(field_addr, value_loc.AsRegister<Register>()); |
| 4488 | break; |
| 4489 | case DataType::Type::kFloat32: { |
| 4490 | Location temp_float = |
| 4491 | (expected_coordinates_count == 1u) ? locations->GetTemp(2) : locations->GetTemp(3); |
| 4492 | DCHECK(temp_float.IsFpuRegister()); |
| 4493 | Location eax = Location::RegisterLocation(EAX); |
| 4494 | NearLabel try_again; |
| 4495 | __ Bind(&try_again); |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 4496 | __ movss(temp_float.AsFpuRegister<XmmRegister>(), field_addr); |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4497 | __ movd(EAX, temp_float.AsFpuRegister<XmmRegister>()); |
| 4498 | __ addss(temp_float.AsFpuRegister<XmmRegister>(), |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 4499 | value_loc.AsFpuRegister<XmmRegister>()); |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4500 | GenPrimitiveLockedCmpxchg(type, |
| 4501 | codegen, |
| Orion Hodson | d6ea38b | 2020-09-18 13:57:23 +0100 | [diff] [blame] | 4502 | /* expected_value= */ eax, |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4503 | /* new_value= */ temp_float, |
| 4504 | reference, |
| 4505 | offset, |
| 4506 | temp); |
| 4507 | __ j(kNotZero, &try_again); |
| 4508 | |
| 4509 | // The old value is present in EAX. |
| 4510 | codegen->Move32(locations->Out(), eax); |
| 4511 | break; |
| 4512 | } |
| 4513 | default: |
| 4514 | UNREACHABLE(); |
| 4515 | } |
| 4516 | |
| 4517 | __ Bind(slow_path->GetExitLabel()); |
| 4518 | } |
| 4519 | |
| Andra Danciu | 49cde24 | 2020-09-22 08:38:50 +0000 | [diff] [blame] | 4520 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndAdd(HInvoke* invoke) { |
| 4521 | CreateVarHandleGetAndAddLocations(invoke); |
| 4522 | } |
| 4523 | |
| 4524 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndAdd(HInvoke* invoke) { |
| 4525 | GenerateVarHandleGetAndAdd(invoke, codegen_); |
| 4526 | } |
| 4527 | |
| 4528 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndAddAcquire(HInvoke* invoke) { |
| 4529 | CreateVarHandleGetAndAddLocations(invoke); |
| 4530 | } |
| 4531 | |
| 4532 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndAddAcquire(HInvoke* invoke) { |
| 4533 | GenerateVarHandleGetAndAdd(invoke, codegen_); |
| 4534 | } |
| 4535 | |
| 4536 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndAddRelease(HInvoke* invoke) { |
| 4537 | CreateVarHandleGetAndAddLocations(invoke); |
| 4538 | } |
| 4539 | |
| 4540 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndAddRelease(HInvoke* invoke) { |
| 4541 | GenerateVarHandleGetAndAdd(invoke, codegen_); |
| 4542 | } |
| 4543 | |
| Andra Danciu | 6edcc08 | 2020-09-18 09:21:26 +0000 | [diff] [blame] | 4544 | static void CreateVarHandleGetAndBitwiseOpLocations(HInvoke* invoke) { |
| 4545 | // The only read barrier implementation supporting the |
| 4546 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4547 | if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) { |
| 4548 | return; |
| 4549 | } |
| 4550 | |
| Ulyana Trafimovich | 98f01d1 | 2021-07-28 14:33:34 +0000 | [diff] [blame] | 4551 | if (!HasVarHandleIntrinsicImplementation(invoke)) { |
| Andra Danciu | 6edcc08 | 2020-09-18 09:21:26 +0000 | [diff] [blame] | 4552 | return; |
| 4553 | } |
| 4554 | |
| 4555 | // The last argument should be the value we intend to set. |
| 4556 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 4557 | if (DataType::Is64BitType(GetDataTypeFromShorty(invoke, value_index))) { |
| 4558 | // We avoid the case of an Int64 value because we would need to place it in a register pair. |
| 4559 | // If the slow path is taken, the ParallelMove might fail to move the pair according to the |
| 4560 | // X86DexCallingConvention in case of an overlap (e.g., move the 64 bit value from |
| 4561 | // <EAX, EBX> to <EBX, ECX>). (Bug: b/168687887) |
| 4562 | return; |
| 4563 | } |
| 4564 | |
| 4565 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 4566 | LocationSummary* locations = new (allocator) LocationSummary( |
| 4567 | invoke, LocationSummary::kCallOnSlowPath, kIntrinsified); |
| 4568 | // We need a byte register temp to store the result of the bitwise operation |
| 4569 | locations->AddTemp(Location::RegisterLocation(EBX)); |
| 4570 | locations->AddTemp(Location::RequiresRegister()); |
| 4571 | locations->SetInAt(0, Location::RequiresRegister()); |
| 4572 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4573 | if (expected_coordinates_count == 1u) { |
| 4574 | // For instance fields, this is the source object |
| 4575 | locations->SetInAt(1, Location::RequiresRegister()); |
| 4576 | } else { |
| 4577 | // For static fields, we need another temp because one will be busy with the declaring class. |
| 4578 | locations->AddTemp(Location::RequiresRegister()); |
| 4579 | } |
| 4580 | |
| 4581 | locations->SetInAt(value_index, Location::RegisterOrConstant(invoke->InputAt(value_index))); |
| 4582 | locations->SetOut(Location::RegisterLocation(EAX)); |
| 4583 | } |
| 4584 | |
| 4585 | static void GenerateBitwiseOp(HInvoke* invoke, |
| 4586 | CodeGeneratorX86* codegen, |
| 4587 | Register left, |
| 4588 | Register right) { |
| 4589 | X86Assembler* assembler = codegen->GetAssembler(); |
| 4590 | |
| 4591 | switch (invoke->GetIntrinsic()) { |
| 4592 | case Intrinsics::kVarHandleGetAndBitwiseOr: |
| 4593 | case Intrinsics::kVarHandleGetAndBitwiseOrAcquire: |
| 4594 | case Intrinsics::kVarHandleGetAndBitwiseOrRelease: |
| 4595 | __ orl(left, right); |
| 4596 | break; |
| 4597 | case Intrinsics::kVarHandleGetAndBitwiseXor: |
| 4598 | case Intrinsics::kVarHandleGetAndBitwiseXorAcquire: |
| 4599 | case Intrinsics::kVarHandleGetAndBitwiseXorRelease: |
| 4600 | __ xorl(left, right); |
| 4601 | break; |
| 4602 | case Intrinsics::kVarHandleGetAndBitwiseAnd: |
| 4603 | case Intrinsics::kVarHandleGetAndBitwiseAndAcquire: |
| 4604 | case Intrinsics::kVarHandleGetAndBitwiseAndRelease: |
| 4605 | __ andl(left, right); |
| 4606 | break; |
| 4607 | default: |
| 4608 | UNREACHABLE(); |
| 4609 | } |
| 4610 | } |
| 4611 | |
| 4612 | static void GenerateVarHandleGetAndBitwiseOp(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| 4613 | // The only read barrier implementation supporting the |
| 4614 | // VarHandleGet intrinsic is the Baker-style read barriers. |
| 4615 | DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier); |
| 4616 | |
| 4617 | X86Assembler* assembler = codegen->GetAssembler(); |
| 4618 | LocationSummary* locations = invoke->GetLocations(); |
| 4619 | uint32_t value_index = invoke->GetNumberOfArguments() - 1; |
| 4620 | DataType::Type type = GetDataTypeFromShorty(invoke, value_index); |
| 4621 | DCHECK_EQ(type, invoke->GetType()); |
| Andra Danciu | 6edcc08 | 2020-09-18 09:21:26 +0000 | [diff] [blame] | 4622 | Register temp = locations->GetTemp(0).AsRegister<Register>(); |
| 4623 | SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke); |
| 4624 | codegen->AddSlowPath(slow_path); |
| 4625 | |
| 4626 | GenerateVarHandleCommonChecks(invoke, temp, slow_path, assembler); |
| 4627 | |
| Andra Danciu | 6edcc08 | 2020-09-18 09:21:26 +0000 | [diff] [blame] | 4628 | Register offset = locations->GetTemp(1).AsRegister<Register>(); |
| 4629 | size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke); |
| 4630 | // For static field, we need another temporary because the first one contains the declaring class |
| 4631 | Register reference = |
| 4632 | (expected_coordinates_count == 1u) ? temp : locations->GetTemp(2).AsRegister<Register>(); |
| 4633 | // Get the field referred by the VarHandle. The returned register contains the object reference |
| 4634 | // or the declaring class. The field offset will be placed in 'offset'. For static fields, the |
| 4635 | // declaring class will be placed in 'reference' register. |
| 4636 | reference = GenerateVarHandleFieldReference(invoke, codegen, reference, offset); |
| 4637 | DCHECK_NE(temp, reference); |
| 4638 | Address field_addr(reference, offset, TIMES_1, 0); |
| 4639 | |
| 4640 | Register out = locations->Out().AsRegister<Register>(); |
| 4641 | DCHECK_EQ(out, EAX); |
| 4642 | |
| 4643 | if (invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseOrRelease || |
| 4644 | invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseXorRelease || |
| 4645 | invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseAndRelease) { |
| 4646 | codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyStore); |
| 4647 | } |
| 4648 | |
| 4649 | NearLabel try_again; |
| 4650 | __ Bind(&try_again); |
| 4651 | // Place the expected value in EAX for cmpxchg |
| Andra Danciu | d0f71f2 | 2020-09-17 09:00:15 +0000 | [diff] [blame] | 4652 | codegen->LoadFromMemoryNoBarrier(type, locations->Out(), field_addr); |
| Andra Danciu | 6edcc08 | 2020-09-18 09:21:26 +0000 | [diff] [blame] | 4653 | codegen->Move32(locations->GetTemp(0), locations->InAt(value_index)); |
| 4654 | GenerateBitwiseOp(invoke, codegen, temp, out); |
| 4655 | GenPrimitiveLockedCmpxchg(type, |
| 4656 | codegen, |
| 4657 | /* expected_value= */ locations->Out(), |
| 4658 | /* new_value= */ locations->GetTemp(0), |
| 4659 | reference, |
| 4660 | offset); |
| 4661 | // If the cmpxchg failed, another thread changed the value so try again. |
| 4662 | __ j(kNotZero, &try_again); |
| 4663 | |
| 4664 | // The old value is present in EAX. |
| 4665 | |
| 4666 | if (invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseOrAcquire || |
| 4667 | invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseXorAcquire || |
| 4668 | invoke->GetIntrinsic() == Intrinsics::kVarHandleGetAndBitwiseAndAcquire) { |
| 4669 | codegen->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); |
| 4670 | } |
| 4671 | |
| 4672 | __ Bind(slow_path->GetExitLabel()); |
| 4673 | } |
| 4674 | |
| 4675 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseOr(HInvoke* invoke) { |
| 4676 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4677 | } |
| 4678 | |
| 4679 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseOr(HInvoke* invoke) { |
| 4680 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4681 | } |
| 4682 | |
| 4683 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseOrAcquire(HInvoke* invoke) { |
| 4684 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4685 | } |
| 4686 | |
| 4687 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseOrAcquire(HInvoke* invoke) { |
| 4688 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4689 | } |
| 4690 | |
| 4691 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseOrRelease(HInvoke* invoke) { |
| 4692 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4693 | } |
| 4694 | |
| 4695 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseOrRelease(HInvoke* invoke) { |
| 4696 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4697 | } |
| 4698 | |
| 4699 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseXor(HInvoke* invoke) { |
| 4700 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4701 | } |
| 4702 | |
| 4703 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseXor(HInvoke* invoke) { |
| 4704 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4705 | } |
| 4706 | |
| 4707 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseXorAcquire(HInvoke* invoke) { |
| 4708 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4709 | } |
| 4710 | |
| 4711 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseXorAcquire(HInvoke* invoke) { |
| 4712 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4713 | } |
| 4714 | |
| 4715 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseXorRelease(HInvoke* invoke) { |
| 4716 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4717 | } |
| 4718 | |
| 4719 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseXorRelease(HInvoke* invoke) { |
| 4720 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4721 | } |
| 4722 | |
| 4723 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseAnd(HInvoke* invoke) { |
| 4724 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4725 | } |
| 4726 | |
| 4727 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseAnd(HInvoke* invoke) { |
| 4728 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4729 | } |
| 4730 | |
| 4731 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseAndAcquire(HInvoke* invoke) { |
| 4732 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4733 | } |
| 4734 | |
| 4735 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseAndAcquire(HInvoke* invoke) { |
| 4736 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4737 | } |
| 4738 | |
| 4739 | void IntrinsicLocationsBuilderX86::VisitVarHandleGetAndBitwiseAndRelease(HInvoke* invoke) { |
| 4740 | CreateVarHandleGetAndBitwiseOpLocations(invoke); |
| 4741 | } |
| 4742 | |
| 4743 | void IntrinsicCodeGeneratorX86::VisitVarHandleGetAndBitwiseAndRelease(HInvoke* invoke) { |
| 4744 | GenerateVarHandleGetAndBitwiseOp(invoke, codegen_); |
| 4745 | } |
| Andra Danciu | eb2c9dd | 2020-09-14 13:22:40 +0000 | [diff] [blame] | 4746 | |
| Shalini Salomi Bodapati | 6545ee3 | 2021-11-02 20:01:06 +0530 | [diff] [blame] | 4747 | static void GenerateMathFma(HInvoke* invoke, CodeGeneratorX86* codegen) { |
| 4748 | DCHECK(DataType::IsFloatingPointType(invoke->GetType())); |
| 4749 | LocationSummary* locations = invoke->GetLocations(); |
| 4750 | DCHECK(locations->InAt(0).Equals(locations->Out())); |
| 4751 | X86Assembler* assembler = codegen->GetAssembler(); |
| 4752 | XmmRegister left = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 4753 | XmmRegister right = locations->InAt(1).AsFpuRegister<XmmRegister>(); |
| 4754 | XmmRegister accumulator = locations->InAt(2).AsFpuRegister<XmmRegister>(); |
| 4755 | if (invoke->GetType() == DataType::Type::kFloat32) { |
| 4756 | __ vfmadd213ss(left, right, accumulator); |
| 4757 | } else { |
| 4758 | DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64); |
| 4759 | __ vfmadd213sd(left, right, accumulator); |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | void IntrinsicCodeGeneratorX86::VisitMathFmaDouble(HInvoke* invoke) { |
| 4764 | DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2()); |
| 4765 | GenerateMathFma(invoke, codegen_); |
| 4766 | } |
| 4767 | |
| 4768 | void IntrinsicLocationsBuilderX86::VisitMathFmaDouble(HInvoke* invoke) { |
| 4769 | if (codegen_->GetInstructionSetFeatures().HasAVX2()) { |
| 4770 | CreateFPFPFPToFPCallLocations(allocator_, invoke); |
| 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | void IntrinsicCodeGeneratorX86::VisitMathFmaFloat(HInvoke* invoke) { |
| 4775 | DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2()); |
| 4776 | GenerateMathFma(invoke, codegen_); |
| 4777 | } |
| 4778 | |
| 4779 | void IntrinsicLocationsBuilderX86::VisitMathFmaFloat(HInvoke* invoke) { |
| 4780 | if (codegen_->GetInstructionSetFeatures().HasAVX2()) { |
| 4781 | CreateFPFPFPToFPCallLocations(allocator_, invoke); |
| 4782 | } |
| 4783 | } |
| 4784 | |
| Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 4785 | UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble) |
| Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 4786 | UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite) |
| 4787 | UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite) |
| 4788 | UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit) |
| 4789 | UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit) |
| Artem Serov | a3bd4ec | 2020-08-27 16:26:17 +0100 | [diff] [blame] | 4790 | UNIMPLEMENTED_INTRINSIC(X86, LongDivideUnsigned) |
| xueliang.zhong | cb58b07 | 2017-10-13 12:06:56 +0100 | [diff] [blame] | 4791 | UNIMPLEMENTED_INTRINSIC(X86, CRC32Update) |
| Evgeny Astigeevich | 15c5b97 | 2018-11-20 13:41:40 +0000 | [diff] [blame] | 4792 | UNIMPLEMENTED_INTRINSIC(X86, CRC32UpdateBytes) |
| Evgeny Astigeevich | 776a7c2 | 2018-12-17 11:40:34 +0000 | [diff] [blame] | 4793 | UNIMPLEMENTED_INTRINSIC(X86, CRC32UpdateByteBuffer) |
| xueliang.zhong | 9ce340f | 2019-01-22 17:46:09 +0000 | [diff] [blame] | 4794 | UNIMPLEMENTED_INTRINSIC(X86, FP16ToFloat) |
| Vladimir Marko | 7f958e3 | 2019-10-24 09:03:58 +0000 | [diff] [blame] | 4795 | UNIMPLEMENTED_INTRINSIC(X86, FP16ToHalf) |
| Usama Arif | b9f02c2 | 2019-10-25 17:37:33 +0100 | [diff] [blame] | 4796 | UNIMPLEMENTED_INTRINSIC(X86, FP16Floor) |
| Usama Arif | 665aac4 | 2019-10-29 11:13:18 +0000 | [diff] [blame] | 4797 | UNIMPLEMENTED_INTRINSIC(X86, FP16Ceil) |
| Usama Arif | 681692b | 2019-10-30 16:23:26 +0000 | [diff] [blame] | 4798 | UNIMPLEMENTED_INTRINSIC(X86, FP16Rint) |
| Usama Arif | 457e9fa | 2019-11-11 15:29:59 +0000 | [diff] [blame] | 4799 | UNIMPLEMENTED_INTRINSIC(X86, FP16Greater) |
| 4800 | UNIMPLEMENTED_INTRINSIC(X86, FP16GreaterEquals) |
| 4801 | UNIMPLEMENTED_INTRINSIC(X86, FP16Less) |
| 4802 | UNIMPLEMENTED_INTRINSIC(X86, FP16LessEquals) |
| Usama Arif | ecbdc07 | 2019-11-13 13:32:54 +0000 | [diff] [blame] | 4803 | UNIMPLEMENTED_INTRINSIC(X86, FP16Compare) |
| Usama Arif | 39e2979 | 2019-11-15 10:53:29 +0000 | [diff] [blame] | 4804 | UNIMPLEMENTED_INTRINSIC(X86, FP16Min) |
| 4805 | UNIMPLEMENTED_INTRINSIC(X86, FP16Max) |
| Nikita Iashchenko | 745da80 | 2021-01-20 21:52:54 +0000 | [diff] [blame] | 4806 | UNIMPLEMENTED_INTRINSIC(X86, MathMultiplyHigh) |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 4807 | |
| Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 4808 | UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf); |
| 4809 | UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter); |
| Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 4810 | UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend); |
| 4811 | UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength); |
| 4812 | UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString); |
| Vladimir Marko | d456117 | 2017-10-30 17:48:25 +0000 | [diff] [blame] | 4813 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendObject); |
| 4814 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendString); |
| 4815 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendCharSequence); |
| 4816 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendCharArray); |
| 4817 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendBoolean); |
| 4818 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendChar); |
| 4819 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendInt); |
| 4820 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendLong); |
| 4821 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendFloat); |
| 4822 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppendDouble); |
| Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 4823 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength); |
| 4824 | UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString); |
| Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 4825 | |
| Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 4826 | // 1.8. |
| Shalini Salomi Bodapati | 6545ee3 | 2021-11-02 20:01:06 +0530 | [diff] [blame] | 4827 | |
| Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 4828 | UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt) |
| 4829 | UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong) |
| 4830 | UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt) |
| 4831 | UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong) |
| 4832 | UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject) |
| Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 4833 | |
| Andra Danciu | a0130e8 | 2020-07-23 12:34:56 +0000 | [diff] [blame] | 4834 | UNIMPLEMENTED_INTRINSIC(X86, MethodHandleInvokeExact) |
| 4835 | UNIMPLEMENTED_INTRINSIC(X86, MethodHandleInvoke) |
| Andra Danciu | a0130e8 | 2020-07-23 12:34:56 +0000 | [diff] [blame] | 4836 | |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 4837 | // OpenJDK 11 |
| 4838 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeGetAndAddInt) |
| 4839 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeGetAndAddLong) |
| 4840 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeGetAndSetInt) |
| 4841 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeGetAndSetLong) |
| 4842 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeGetAndSetObject) |
| Sorin Basca | 507cf90 | 2021-10-06 12:04:56 +0000 | [diff] [blame] | 4843 | UNIMPLEMENTED_INTRINSIC(X86, JdkUnsafeCompareAndSetObject) |
| Sorin Basca | 2f01e8e | 2021-06-18 06:44:07 +0000 | [diff] [blame] | 4844 | |
| Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 4845 | UNREACHABLE_INTRINSICS(X86) |
| Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 4846 | |
| 4847 | #undef __ |
| 4848 | |
| Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 4849 | } // namespace x86 |
| 4850 | } // namespace art |