Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "intrinsics_x86_64.h" |
| 18 | |
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_64/instruction_set_features_x86_64.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 23 | #include "base/bit_utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 24 | #include "code_generator_x86_64.h" |
| 25 | #include "entrypoints/quick/quick_entrypoints.h" |
| 26 | #include "intrinsics.h" |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 27 | #include "intrinsics_utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 28 | #include "mirror/array-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 29 | #include "mirror/string.h" |
| 30 | #include "thread.h" |
| 31 | #include "utils/x86_64/assembler_x86_64.h" |
| 32 | #include "utils/x86_64/constants_x86_64.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
| 36 | namespace x86_64 { |
| 37 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 38 | IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen) |
| 39 | : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) { |
| 40 | } |
| 41 | |
| 42 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 43 | X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 44 | return down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 47 | ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 48 | return codegen_->GetGraph()->GetArena(); |
| 49 | } |
| 50 | |
| 51 | bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) { |
| 52 | Dispatch(invoke); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 53 | LocationSummary* res = invoke->GetLocations(); |
| 54 | if (res == nullptr) { |
| 55 | return false; |
| 56 | } |
| 57 | if (kEmitCompilerReadBarrier && res->CanCall()) { |
| 58 | // Generating an intrinsic for this HInvoke may produce an |
| 59 | // IntrinsicSlowPathX86_64 slow path. Currently this approach |
| 60 | // does not work when using read barriers, as the emitted |
| 61 | // calling sequence will make use of another slow path |
| 62 | // (ReadBarrierForRootSlowPathX86_64 for HInvokeStaticOrDirect, |
| 63 | // ReadBarrierSlowPathX86_64 for HInvokeVirtual). So we bail |
| 64 | // out in this case. |
| 65 | // |
| 66 | // TODO: Find a way to have intrinsics work with read barriers. |
| 67 | invoke->SetLocations(nullptr); |
| 68 | return false; |
| 69 | } |
| 70 | return res->Intrinsified(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 73 | static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | 2d27c8e | 2015-04-28 15:48:45 +0100 | [diff] [blame] | 74 | InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor; |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 75 | IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 78 | using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 79 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 80 | #define __ assembler-> |
| 81 | |
| 82 | static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 83 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 84 | LocationSummary::kNoCall, |
| 85 | kIntrinsified); |
| 86 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 87 | locations->SetOut(Location::RequiresRegister()); |
| 88 | } |
| 89 | |
| 90 | static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 91 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 92 | LocationSummary::kNoCall, |
| 93 | kIntrinsified); |
| 94 | locations->SetInAt(0, Location::RequiresRegister()); |
| 95 | locations->SetOut(Location::RequiresFpuRegister()); |
| 96 | } |
| 97 | |
| 98 | static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 99 | Location input = locations->InAt(0); |
| 100 | Location output = locations->Out(); |
| 101 | __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit); |
| 102 | } |
| 103 | |
| 104 | static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 105 | Location input = locations->InAt(0); |
| 106 | Location output = locations->Out(); |
| 107 | __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit); |
| 108 | } |
| 109 | |
| 110 | void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| 111 | CreateFPToIntLocations(arena_, invoke); |
| 112 | } |
| 113 | void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| 114 | CreateIntToFPLocations(arena_, invoke); |
| 115 | } |
| 116 | |
| 117 | void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| 118 | MoveFPToInt(invoke->GetLocations(), true, GetAssembler()); |
| 119 | } |
| 120 | void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| 121 | MoveIntToFP(invoke->GetLocations(), true, GetAssembler()); |
| 122 | } |
| 123 | |
| 124 | void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| 125 | CreateFPToIntLocations(arena_, invoke); |
| 126 | } |
| 127 | void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| 128 | CreateIntToFPLocations(arena_, invoke); |
| 129 | } |
| 130 | |
| 131 | void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| 132 | MoveFPToInt(invoke->GetLocations(), false, GetAssembler()); |
| 133 | } |
| 134 | void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| 135 | MoveIntToFP(invoke->GetLocations(), false, GetAssembler()); |
| 136 | } |
| 137 | |
| 138 | static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 139 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 140 | LocationSummary::kNoCall, |
| 141 | kIntrinsified); |
| 142 | locations->SetInAt(0, Location::RequiresRegister()); |
| 143 | locations->SetOut(Location::SameAsFirstInput()); |
| 144 | } |
| 145 | |
| 146 | static void GenReverseBytes(LocationSummary* locations, |
| 147 | Primitive::Type size, |
| 148 | X86_64Assembler* assembler) { |
| 149 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 150 | |
| 151 | switch (size) { |
| 152 | case Primitive::kPrimShort: |
| 153 | // TODO: Can be done with an xchg of 8b registers. This is straight from Quick. |
| 154 | __ bswapl(out); |
| 155 | __ sarl(out, Immediate(16)); |
| 156 | break; |
| 157 | case Primitive::kPrimInt: |
| 158 | __ bswapl(out); |
| 159 | break; |
| 160 | case Primitive::kPrimLong: |
| 161 | __ bswapq(out); |
| 162 | break; |
| 163 | default: |
| 164 | LOG(FATAL) << "Unexpected size for reverse-bytes: " << size; |
| 165 | UNREACHABLE(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
| 170 | CreateIntToIntLocations(arena_, invoke); |
| 171 | } |
| 172 | |
| 173 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) { |
| 174 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 175 | } |
| 176 | |
| 177 | void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
| 178 | CreateIntToIntLocations(arena_, invoke); |
| 179 | } |
| 180 | |
| 181 | void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) { |
| 182 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 183 | } |
| 184 | |
| 185 | void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
| 186 | CreateIntToIntLocations(arena_, invoke); |
| 187 | } |
| 188 | |
| 189 | void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) { |
| 190 | GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | // TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we |
| 195 | // need is 64b. |
| 196 | |
| 197 | static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) { |
| 198 | // TODO: Enable memory operations when the assembler supports them. |
| 199 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 200 | LocationSummary::kNoCall, |
| 201 | kIntrinsified); |
| 202 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 203 | locations->SetOut(Location::SameAsFirstInput()); |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 204 | locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 207 | static void MathAbsFP(LocationSummary* locations, |
| 208 | bool is64bit, |
| 209 | X86_64Assembler* assembler, |
| 210 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 211 | Location output = locations->Out(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 212 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 213 | DCHECK(output.IsFpuRegister()); |
| 214 | XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 215 | |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 216 | // TODO: Can mask directly with constant area using pand if we can guarantee |
| 217 | // that the literal is aligned on a 16 byte boundary. This will avoid a |
| 218 | // temporary. |
| 219 | if (is64bit) { |
| 220 | __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF))); |
| 221 | __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 222 | } else { |
Mark Mendell | cfa410b | 2015-05-25 16:02:44 -0400 | [diff] [blame] | 223 | __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF))); |
| 224 | __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
| 229 | CreateFloatToFloatPlusTemps(arena_, invoke); |
| 230 | } |
| 231 | |
| 232 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 233 | MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
| 237 | CreateFloatToFloatPlusTemps(arena_, invoke); |
| 238 | } |
| 239 | |
| 240 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 241 | MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) { |
| 245 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 246 | LocationSummary::kNoCall, |
| 247 | kIntrinsified); |
| 248 | locations->SetInAt(0, Location::RequiresRegister()); |
| 249 | locations->SetOut(Location::SameAsFirstInput()); |
| 250 | locations->AddTemp(Location::RequiresRegister()); |
| 251 | } |
| 252 | |
| 253 | static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) { |
| 254 | Location output = locations->Out(); |
| 255 | CpuRegister out = output.AsRegister<CpuRegister>(); |
| 256 | CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 257 | |
| 258 | if (is64bit) { |
| 259 | // Create mask. |
| 260 | __ movq(mask, out); |
| 261 | __ sarq(mask, Immediate(63)); |
| 262 | // Add mask. |
| 263 | __ addq(out, mask); |
| 264 | __ xorq(out, mask); |
| 265 | } else { |
| 266 | // Create mask. |
| 267 | __ movl(mask, out); |
| 268 | __ sarl(mask, Immediate(31)); |
| 269 | // Add mask. |
| 270 | __ addl(out, mask); |
| 271 | __ xorl(out, mask); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) { |
| 276 | CreateIntToIntPlusTemp(arena_, invoke); |
| 277 | } |
| 278 | |
| 279 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) { |
| 280 | GenAbsInteger(invoke->GetLocations(), false, GetAssembler()); |
| 281 | } |
| 282 | |
| 283 | void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) { |
| 284 | CreateIntToIntPlusTemp(arena_, invoke); |
| 285 | } |
| 286 | |
| 287 | void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) { |
| 288 | GenAbsInteger(invoke->GetLocations(), true, GetAssembler()); |
| 289 | } |
| 290 | |
Mark Mendell | 39dcf55 | 2015-04-09 20:42:42 -0400 | [diff] [blame] | 291 | static void GenMinMaxFP(LocationSummary* locations, |
| 292 | bool is_min, |
| 293 | bool is_double, |
| 294 | X86_64Assembler* assembler, |
| 295 | CodeGeneratorX86_64* codegen) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 296 | Location op1_loc = locations->InAt(0); |
| 297 | Location op2_loc = locations->InAt(1); |
| 298 | Location out_loc = locations->Out(); |
| 299 | XmmRegister out = out_loc.AsFpuRegister<XmmRegister>(); |
| 300 | |
| 301 | // Shortcut for same input locations. |
| 302 | if (op1_loc.Equals(op2_loc)) { |
| 303 | DCHECK(out_loc.Equals(op1_loc)); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // (out := op1) |
| 308 | // out <=? op2 |
| 309 | // if Nan jmp Nan_label |
| 310 | // if out is min jmp done |
| 311 | // if op2 is min jmp op2_label |
| 312 | // handle -0/+0 |
| 313 | // jmp done |
| 314 | // Nan_label: |
| 315 | // out := NaN |
| 316 | // op2_label: |
| 317 | // out := op2 |
| 318 | // done: |
| 319 | // |
| 320 | // This removes one jmp, but needs to copy one input (op1) to out. |
| 321 | // |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 322 | // TODO: This is straight from Quick. Make NaN an out-of-line slowpath? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 323 | |
| 324 | XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>(); |
| 325 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 326 | NearLabel nan, done, op2_label; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 327 | if (is_double) { |
| 328 | __ ucomisd(out, op2); |
| 329 | } else { |
| 330 | __ ucomiss(out, op2); |
| 331 | } |
| 332 | |
| 333 | __ j(Condition::kParityEven, &nan); |
| 334 | |
| 335 | __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label); |
| 336 | __ j(is_min ? Condition::kBelow : Condition::kAbove, &done); |
| 337 | |
| 338 | // Handle 0.0/-0.0. |
| 339 | if (is_min) { |
| 340 | if (is_double) { |
| 341 | __ orpd(out, op2); |
| 342 | } else { |
| 343 | __ orps(out, op2); |
| 344 | } |
| 345 | } else { |
| 346 | if (is_double) { |
| 347 | __ andpd(out, op2); |
| 348 | } else { |
| 349 | __ andps(out, op2); |
| 350 | } |
| 351 | } |
| 352 | __ jmp(&done); |
| 353 | |
| 354 | // NaN handling. |
| 355 | __ Bind(&nan); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 356 | if (is_double) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 357 | __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 358 | } else { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 359 | __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 360 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 361 | __ jmp(&done); |
| 362 | |
| 363 | // out := op2; |
| 364 | __ Bind(&op2_label); |
| 365 | if (is_double) { |
| 366 | __ movsd(out, op2); |
| 367 | } else { |
| 368 | __ movss(out, op2); |
| 369 | } |
| 370 | |
| 371 | // Done. |
| 372 | __ Bind(&done); |
| 373 | } |
| 374 | |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 375 | static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 376 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 377 | LocationSummary::kNoCall, |
| 378 | kIntrinsified); |
| 379 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 380 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 381 | // The following is sub-optimal, but all we can do for now. It would be fine to also accept |
| 382 | // the second input to be the output (we can simply swap inputs). |
| 383 | locations->SetOut(Location::SameAsFirstInput()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 387 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 391 | GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 395 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 399 | GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 403 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 407 | GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 411 | CreateFPFPToFP(arena_, invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) { |
Mark Mendell | f55c3e0 | 2015-03-26 21:07:46 -0400 | [diff] [blame] | 415 | GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long, |
| 419 | X86_64Assembler* assembler) { |
| 420 | Location op1_loc = locations->InAt(0); |
| 421 | Location op2_loc = locations->InAt(1); |
| 422 | |
| 423 | // Shortcut for same input locations. |
| 424 | if (op1_loc.Equals(op2_loc)) { |
| 425 | // Can return immediately, as op1_loc == out_loc. |
| 426 | // Note: if we ever support separate registers, e.g., output into memory, we need to check for |
| 427 | // a copy here. |
| 428 | DCHECK(locations->Out().Equals(op1_loc)); |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 433 | CpuRegister op2 = op2_loc.AsRegister<CpuRegister>(); |
| 434 | |
| 435 | // (out := op1) |
| 436 | // out <=? op2 |
| 437 | // if out is min jmp done |
| 438 | // out := op2 |
| 439 | // done: |
| 440 | |
| 441 | if (is_long) { |
| 442 | __ cmpq(out, op2); |
| 443 | } else { |
| 444 | __ cmpl(out, op2); |
| 445 | } |
| 446 | |
| 447 | __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long); |
| 448 | } |
| 449 | |
| 450 | static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 451 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 452 | LocationSummary::kNoCall, |
| 453 | kIntrinsified); |
| 454 | locations->SetInAt(0, Location::RequiresRegister()); |
| 455 | locations->SetInAt(1, Location::RequiresRegister()); |
| 456 | locations->SetOut(Location::SameAsFirstInput()); |
| 457 | } |
| 458 | |
| 459 | void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
| 460 | CreateIntIntToIntLocations(arena_, invoke); |
| 461 | } |
| 462 | |
| 463 | void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) { |
| 464 | GenMinMax(invoke->GetLocations(), true, false, GetAssembler()); |
| 465 | } |
| 466 | |
| 467 | void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
| 468 | CreateIntIntToIntLocations(arena_, invoke); |
| 469 | } |
| 470 | |
| 471 | void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) { |
| 472 | GenMinMax(invoke->GetLocations(), true, true, GetAssembler()); |
| 473 | } |
| 474 | |
| 475 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
| 476 | CreateIntIntToIntLocations(arena_, invoke); |
| 477 | } |
| 478 | |
| 479 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) { |
| 480 | GenMinMax(invoke->GetLocations(), false, false, GetAssembler()); |
| 481 | } |
| 482 | |
| 483 | void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
| 484 | CreateIntIntToIntLocations(arena_, invoke); |
| 485 | } |
| 486 | |
| 487 | void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) { |
| 488 | GenMinMax(invoke->GetLocations(), false, true, GetAssembler()); |
| 489 | } |
| 490 | |
| 491 | static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 492 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 493 | LocationSummary::kNoCall, |
| 494 | kIntrinsified); |
| 495 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 496 | locations->SetOut(Location::RequiresFpuRegister()); |
| 497 | } |
| 498 | |
| 499 | void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) { |
| 500 | CreateFPToFPLocations(arena_, invoke); |
| 501 | } |
| 502 | |
| 503 | void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) { |
| 504 | LocationSummary* locations = invoke->GetLocations(); |
| 505 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 506 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 507 | |
| 508 | GetAssembler()->sqrtsd(out, in); |
| 509 | } |
| 510 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 511 | static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) { |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 512 | MoveArguments(invoke, codegen); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 513 | |
| 514 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 515 | codegen->GenerateStaticOrDirectCall( |
| 516 | invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 517 | codegen->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 518 | |
| 519 | // Copy the result back to the expected output. |
| 520 | Location out = invoke->GetLocations()->Out(); |
| 521 | if (out.IsValid()) { |
| 522 | DCHECK(out.IsRegister()); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 523 | codegen->MoveFromReturnRegister(out, invoke->GetType()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | static void CreateSSE41FPToFPLocations(ArenaAllocator* arena, |
| 528 | HInvoke* invoke, |
| 529 | CodeGeneratorX86_64* codegen) { |
| 530 | // Do we have instruction support? |
| 531 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
| 532 | CreateFPToFPLocations(arena, invoke); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | // We have to fall back to a call to the intrinsic. |
| 537 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 538 | LocationSummary::kCall); |
| 539 | InvokeRuntimeCallingConvention calling_convention; |
| 540 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 541 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 542 | // Needs to be RDI for the invoke. |
| 543 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 544 | } |
| 545 | |
| 546 | static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen, |
| 547 | HInvoke* invoke, |
| 548 | X86_64Assembler* assembler, |
| 549 | int round_mode) { |
| 550 | LocationSummary* locations = invoke->GetLocations(); |
| 551 | if (locations->WillCall()) { |
| 552 | InvokeOutOfLineIntrinsic(codegen, invoke); |
| 553 | } else { |
| 554 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 555 | XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>(); |
| 556 | __ roundsd(out, in, Immediate(round_mode)); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) { |
| 561 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 562 | } |
| 563 | |
| 564 | void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) { |
| 565 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2); |
| 566 | } |
| 567 | |
| 568 | void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) { |
| 569 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 570 | } |
| 571 | |
| 572 | void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) { |
| 573 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1); |
| 574 | } |
| 575 | |
| 576 | void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) { |
| 577 | CreateSSE41FPToFPLocations(arena_, invoke, codegen_); |
| 578 | } |
| 579 | |
| 580 | void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) { |
| 581 | GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0); |
| 582 | } |
| 583 | |
| 584 | static void CreateSSE41FPToIntLocations(ArenaAllocator* arena, |
| 585 | HInvoke* invoke, |
| 586 | CodeGeneratorX86_64* codegen) { |
| 587 | // Do we have instruction support? |
| 588 | if (codegen->GetInstructionSetFeatures().HasSSE4_1()) { |
| 589 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 590 | LocationSummary::kNoCall, |
| 591 | kIntrinsified); |
| 592 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 593 | locations->SetOut(Location::RequiresRegister()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 594 | locations->AddTemp(Location::RequiresFpuRegister()); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
| 598 | // We have to fall back to a call to the intrinsic. |
| 599 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 600 | LocationSummary::kCall); |
| 601 | InvokeRuntimeCallingConvention calling_convention; |
| 602 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 603 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 604 | // Needs to be RDI for the invoke. |
| 605 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 606 | } |
| 607 | |
| 608 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 609 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 610 | } |
| 611 | |
| 612 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 613 | LocationSummary* locations = invoke->GetLocations(); |
| 614 | if (locations->WillCall()) { |
| 615 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int. |
| 620 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 621 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 622 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 623 | NearLabel done, nan; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 624 | X86_64Assembler* assembler = GetAssembler(); |
| 625 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 626 | // Load 0.5 into inPlusPointFive. |
| 627 | __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 628 | |
| 629 | // Add in the input. |
| 630 | __ addss(inPlusPointFive, in); |
| 631 | |
| 632 | // And truncate to an integer. |
| 633 | __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 634 | |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 635 | // Load maxInt into out. |
| 636 | codegen_->Load64BitValue(out, kPrimIntMax); |
| 637 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 638 | // if inPlusPointFive >= maxInt goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 639 | __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 640 | __ j(kAboveEqual, &done); |
| 641 | |
| 642 | // if input == NaN goto nan |
| 643 | __ j(kUnordered, &nan); |
| 644 | |
| 645 | // output = float-to-int-truncate(input) |
| 646 | __ cvttss2si(out, inPlusPointFive); |
| 647 | __ jmp(&done); |
| 648 | __ Bind(&nan); |
| 649 | |
| 650 | // output = 0 |
| 651 | __ xorl(out, out); |
| 652 | __ Bind(&done); |
| 653 | } |
| 654 | |
| 655 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 656 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 657 | } |
| 658 | |
| 659 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 660 | LocationSummary* locations = invoke->GetLocations(); |
| 661 | if (locations->WillCall()) { |
| 662 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | // Implement RoundDouble as t1 = floor(input + 0.5); convert to long. |
| 667 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 668 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 669 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 670 | NearLabel done, nan; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 671 | X86_64Assembler* assembler = GetAssembler(); |
| 672 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 673 | // Load 0.5 into inPlusPointFive. |
| 674 | __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 675 | |
| 676 | // Add in the input. |
| 677 | __ addsd(inPlusPointFive, in); |
| 678 | |
| 679 | // And truncate to an integer. |
| 680 | __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 681 | |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 682 | // Load maxLong into out. |
| 683 | codegen_->Load64BitValue(out, kPrimLongMax); |
| 684 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 685 | // if inPlusPointFive >= maxLong goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 686 | __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 687 | __ j(kAboveEqual, &done); |
| 688 | |
| 689 | // if input == NaN goto nan |
| 690 | __ j(kUnordered, &nan); |
| 691 | |
| 692 | // output = double-to-long-truncate(input) |
| 693 | __ cvttsd2si(out, inPlusPointFive, true); |
| 694 | __ jmp(&done); |
| 695 | __ Bind(&nan); |
| 696 | |
| 697 | // output = 0 |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 698 | __ xorl(out, out); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 699 | __ Bind(&done); |
| 700 | } |
| 701 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 702 | void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 703 | // The inputs plus one temp. |
| 704 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 705 | LocationSummary::kCallOnSlowPath, |
| 706 | kIntrinsified); |
| 707 | locations->SetInAt(0, Location::RequiresRegister()); |
| 708 | locations->SetInAt(1, Location::RequiresRegister()); |
| 709 | locations->SetOut(Location::SameAsFirstInput()); |
| 710 | locations->AddTemp(Location::RequiresRegister()); |
| 711 | } |
| 712 | |
| 713 | void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 714 | LocationSummary* locations = invoke->GetLocations(); |
| 715 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 716 | // Location of reference to data array. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 717 | const int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 718 | // Location of count. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 719 | const int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 720 | |
| 721 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 722 | CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>(); |
| 723 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 724 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 725 | // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth |
| 726 | // the cost. |
| 727 | // TODO: For simplicity, the index parameter is requested in a register, so different from Quick |
| 728 | // we will not optimize the code for constants (which would save a register). |
| 729 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 730 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 731 | codegen_->AddSlowPath(slow_path); |
| 732 | |
| 733 | X86_64Assembler* assembler = GetAssembler(); |
| 734 | |
| 735 | __ cmpl(idx, Address(obj, count_offset)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 736 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 737 | __ j(kAboveEqual, slow_path->GetEntryLabel()); |
| 738 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 739 | // out = out[2*idx]. |
| 740 | __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 741 | |
| 742 | __ Bind(slow_path->GetExitLabel()); |
| 743 | } |
| 744 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 745 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 746 | // Check to see if we have known failures that will cause us to have to bail out |
| 747 | // to the runtime, and just generate the runtime call directly. |
| 748 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 749 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 750 | |
| 751 | // The positions must be non-negative. |
| 752 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 753 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 754 | // We will have to fail anyways. |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | // The length must be > 0. |
| 759 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 760 | if (length != nullptr) { |
| 761 | int32_t len = length->GetValue(); |
| 762 | if (len < 0) { |
| 763 | // Just call as normal. |
| 764 | return; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 769 | LocationSummary::kCallOnSlowPath, |
| 770 | kIntrinsified); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 771 | // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length). |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 772 | locations->SetInAt(0, Location::RequiresRegister()); |
| 773 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 774 | locations->SetInAt(2, Location::RequiresRegister()); |
| 775 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 776 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 777 | |
| 778 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 779 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 780 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 781 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 782 | } |
| 783 | |
| 784 | static void CheckPosition(X86_64Assembler* assembler, |
| 785 | Location pos, |
| 786 | CpuRegister input, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 787 | Location length, |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 788 | SlowPathCode* slow_path, |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 789 | CpuRegister input_len, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 790 | CpuRegister temp, |
| 791 | bool length_is_input_length = false) { |
| 792 | // Where is the length in the Array? |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 793 | const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 794 | |
| 795 | if (pos.IsConstant()) { |
| 796 | int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue(); |
| 797 | if (pos_const == 0) { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 798 | if (!length_is_input_length) { |
| 799 | // Check that length(input) >= length. |
| 800 | if (length.IsConstant()) { |
| 801 | __ cmpl(Address(input, length_offset), |
| 802 | Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 803 | } else { |
| 804 | __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>()); |
| 805 | } |
| 806 | __ j(kLess, slow_path->GetEntryLabel()); |
| 807 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 808 | } else { |
| 809 | // Check that length(input) >= pos. |
| 810 | __ movl(input_len, Address(input, length_offset)); |
| 811 | __ cmpl(input_len, Immediate(pos_const)); |
| 812 | __ j(kLess, slow_path->GetEntryLabel()); |
| 813 | |
| 814 | // Check that (length(input) - pos) >= length. |
| 815 | __ leal(temp, Address(input_len, -pos_const)); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 816 | if (length.IsConstant()) { |
| 817 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 818 | } else { |
| 819 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 820 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 821 | __ j(kLess, slow_path->GetEntryLabel()); |
| 822 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 823 | } else if (length_is_input_length) { |
| 824 | // The only way the copy can succeed is if pos is zero. |
| 825 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 826 | __ testl(pos_reg, pos_reg); |
| 827 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 828 | } else { |
| 829 | // Check that pos >= 0. |
| 830 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 831 | __ testl(pos_reg, pos_reg); |
| 832 | __ j(kLess, slow_path->GetEntryLabel()); |
| 833 | |
| 834 | // Check that pos <= length(input). |
| 835 | __ cmpl(Address(input, length_offset), pos_reg); |
| 836 | __ j(kLess, slow_path->GetEntryLabel()); |
| 837 | |
| 838 | // Check that (length(input) - pos) >= length. |
| 839 | __ movl(temp, Address(input, length_offset)); |
| 840 | __ subl(temp, pos_reg); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 841 | if (length.IsConstant()) { |
| 842 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 843 | } else { |
| 844 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 845 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 846 | __ j(kLess, slow_path->GetEntryLabel()); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 851 | X86_64Assembler* assembler = GetAssembler(); |
| 852 | LocationSummary* locations = invoke->GetLocations(); |
| 853 | |
| 854 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 855 | Location src_pos = locations->InAt(1); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 856 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 857 | Location dest_pos = locations->InAt(3); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 858 | Location length = locations->InAt(4); |
| 859 | |
| 860 | // Temporaries that we need for MOVSW. |
| 861 | CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 862 | DCHECK_EQ(src_base.AsRegister(), RSI); |
| 863 | CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 864 | DCHECK_EQ(dest_base.AsRegister(), RDI); |
| 865 | CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 866 | DCHECK_EQ(count.AsRegister(), RCX); |
| 867 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 868 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 869 | codegen_->AddSlowPath(slow_path); |
| 870 | |
| 871 | // Bail out if the source and destination are the same. |
| 872 | __ cmpl(src, dest); |
| 873 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 874 | |
| 875 | // Bail out if the source is null. |
| 876 | __ testl(src, src); |
| 877 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 878 | |
| 879 | // Bail out if the destination is null. |
| 880 | __ testl(dest, dest); |
| 881 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 882 | |
| 883 | // If the length is negative, bail out. |
| 884 | // We have already checked in the LocationsBuilder for the constant case. |
| 885 | if (!length.IsConstant()) { |
| 886 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
| 887 | __ j(kLess, slow_path->GetEntryLabel()); |
| 888 | } |
| 889 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 890 | // Validity checks: source. |
| 891 | CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base); |
| 892 | |
| 893 | // Validity checks: dest. |
| 894 | CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base); |
| 895 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 896 | // We need the count in RCX. |
| 897 | if (length.IsConstant()) { |
| 898 | __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 899 | } else { |
| 900 | __ movl(count, length.AsRegister<CpuRegister>()); |
| 901 | } |
| 902 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 903 | // Okay, everything checks out. Finally time to do the copy. |
| 904 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 905 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 906 | DCHECK_EQ(char_size, 2u); |
| 907 | |
| 908 | const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value(); |
| 909 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 910 | if (src_pos.IsConstant()) { |
| 911 | int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 912 | __ leal(src_base, Address(src, char_size * src_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 913 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 914 | __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 915 | ScaleFactor::TIMES_2, data_offset)); |
| 916 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 917 | if (dest_pos.IsConstant()) { |
| 918 | int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 919 | __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 920 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 921 | __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 922 | ScaleFactor::TIMES_2, data_offset)); |
| 923 | } |
| 924 | |
| 925 | // Do the move. |
| 926 | __ rep_movsw(); |
| 927 | |
| 928 | __ Bind(slow_path->GetExitLabel()); |
| 929 | } |
| 930 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 931 | |
| 932 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 933 | CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 936 | // TODO: Implement read barriers in the SystemArrayCopy intrinsic. |
| 937 | // Note that this code path is not used (yet) because we do not |
| 938 | // intrinsify methods that can go into the IntrinsicSlowPathX86_64 |
| 939 | // slow path. |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 940 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
| 941 | X86_64Assembler* assembler = GetAssembler(); |
| 942 | LocationSummary* locations = invoke->GetLocations(); |
| 943 | |
| 944 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 945 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 946 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 947 | uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
| 948 | |
| 949 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
| 950 | Location src_pos = locations->InAt(1); |
| 951 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
| 952 | Location dest_pos = locations->InAt(3); |
| 953 | Location length = locations->InAt(4); |
| 954 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 955 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 956 | CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 957 | |
| 958 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
| 959 | codegen_->AddSlowPath(slow_path); |
| 960 | |
| 961 | NearLabel ok; |
| 962 | SystemArrayCopyOptimizations optimizations(invoke); |
| 963 | |
| 964 | if (!optimizations.GetDestinationIsSource()) { |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 965 | if (!src_pos.IsConstant() || !dest_pos.IsConstant()) { |
| 966 | __ cmpl(src, dest); |
| 967 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | // If source and destination are the same, we go to slow path if we need to do |
| 971 | // forward copying. |
| 972 | if (src_pos.IsConstant()) { |
| 973 | int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 974 | if (dest_pos.IsConstant()) { |
| 975 | // Checked when building locations. |
| 976 | DCHECK(!optimizations.GetDestinationIsSource() |
| 977 | || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue())); |
| 978 | } else { |
| 979 | if (!optimizations.GetDestinationIsSource()) { |
| 980 | __ j(kNotEqual, &ok); |
| 981 | } |
| 982 | __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant)); |
| 983 | __ j(kGreater, slow_path->GetEntryLabel()); |
| 984 | } |
| 985 | } else { |
| 986 | if (!optimizations.GetDestinationIsSource()) { |
| 987 | __ j(kNotEqual, &ok); |
| 988 | } |
| 989 | if (dest_pos.IsConstant()) { |
| 990 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 991 | __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant)); |
| 992 | __ j(kLess, slow_path->GetEntryLabel()); |
| 993 | } else { |
| 994 | __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>()); |
| 995 | __ j(kLess, slow_path->GetEntryLabel()); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | __ Bind(&ok); |
| 1000 | |
| 1001 | if (!optimizations.GetSourceIsNotNull()) { |
| 1002 | // Bail out if the source is null. |
| 1003 | __ testl(src, src); |
| 1004 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1005 | } |
| 1006 | |
| 1007 | if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) { |
| 1008 | // Bail out if the destination is null. |
| 1009 | __ testl(dest, dest); |
| 1010 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1011 | } |
| 1012 | |
| 1013 | // If the length is negative, bail out. |
| 1014 | // We have already checked in the LocationsBuilder for the constant case. |
| 1015 | if (!length.IsConstant() && |
| 1016 | !optimizations.GetCountIsSourceLength() && |
| 1017 | !optimizations.GetCountIsDestinationLength()) { |
| 1018 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
| 1019 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1020 | } |
| 1021 | |
| 1022 | // Validity checks: source. |
| 1023 | CheckPosition(assembler, |
| 1024 | src_pos, |
| 1025 | src, |
| 1026 | length, |
| 1027 | slow_path, |
| 1028 | temp1, |
| 1029 | temp2, |
| 1030 | optimizations.GetCountIsSourceLength()); |
| 1031 | |
| 1032 | // Validity checks: dest. |
| 1033 | CheckPosition(assembler, |
| 1034 | dest_pos, |
| 1035 | dest, |
| 1036 | length, |
| 1037 | slow_path, |
| 1038 | temp1, |
| 1039 | temp2, |
| 1040 | optimizations.GetCountIsDestinationLength()); |
| 1041 | |
| 1042 | if (!optimizations.GetDoesNotNeedTypeCheck()) { |
| 1043 | // Check whether all elements of the source array are assignable to the component |
| 1044 | // type of the destination array. We do two checks: the classes are the same, |
| 1045 | // or the destination is Object[]. If none of these checks succeed, we go to the |
| 1046 | // slow path. |
| 1047 | __ movl(temp1, Address(dest, class_offset)); |
| 1048 | __ movl(temp2, Address(src, class_offset)); |
| 1049 | bool did_unpoison = false; |
| 1050 | if (!optimizations.GetDestinationIsNonPrimitiveArray() || |
| 1051 | !optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1052 | // One or two of the references need to be unpoisoned. Unpoisoned them |
| 1053 | // both to make the identity check valid. |
| 1054 | __ MaybeUnpoisonHeapReference(temp1); |
| 1055 | __ MaybeUnpoisonHeapReference(temp2); |
| 1056 | did_unpoison = true; |
| 1057 | } |
| 1058 | |
| 1059 | if (!optimizations.GetDestinationIsNonPrimitiveArray()) { |
| 1060 | // Bail out if the destination is not a non primitive array. |
| 1061 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1062 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1063 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1064 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1065 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1066 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1067 | } |
| 1068 | |
| 1069 | if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1070 | // Bail out if the source is not a non primitive array. |
| 1071 | __ movl(CpuRegister(TMP), Address(temp2, component_offset)); |
| 1072 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1073 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1074 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1075 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1076 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1077 | } |
| 1078 | |
| 1079 | __ cmpl(temp1, temp2); |
| 1080 | |
| 1081 | if (optimizations.GetDestinationIsTypedObjectArray()) { |
| 1082 | NearLabel do_copy; |
| 1083 | __ j(kEqual, &do_copy); |
| 1084 | if (!did_unpoison) { |
| 1085 | __ MaybeUnpoisonHeapReference(temp1); |
| 1086 | } |
| 1087 | __ movl(temp1, Address(temp1, component_offset)); |
| 1088 | __ MaybeUnpoisonHeapReference(temp1); |
| 1089 | __ movl(temp1, Address(temp1, super_offset)); |
| 1090 | // No need to unpoison the result, we're comparing against null. |
| 1091 | __ testl(temp1, temp1); |
| 1092 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1093 | __ Bind(&do_copy); |
| 1094 | } else { |
| 1095 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1096 | } |
| 1097 | } else if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1098 | DCHECK(optimizations.GetDestinationIsNonPrimitiveArray()); |
| 1099 | // Bail out if the source is not a non primitive array. |
| 1100 | __ movl(temp1, Address(src, class_offset)); |
| 1101 | __ MaybeUnpoisonHeapReference(temp1); |
| 1102 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1103 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1104 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1105 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1106 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1107 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1108 | } |
| 1109 | |
| 1110 | // Compute base source address, base destination address, and end source address. |
| 1111 | |
| 1112 | uint32_t element_size = sizeof(int32_t); |
| 1113 | uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value(); |
| 1114 | if (src_pos.IsConstant()) { |
| 1115 | int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1116 | __ leal(temp1, Address(src, element_size * constant + offset)); |
| 1117 | } else { |
| 1118 | __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset)); |
| 1119 | } |
| 1120 | |
| 1121 | if (dest_pos.IsConstant()) { |
| 1122 | int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1123 | __ leal(temp2, Address(dest, element_size * constant + offset)); |
| 1124 | } else { |
| 1125 | __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset)); |
| 1126 | } |
| 1127 | |
| 1128 | if (length.IsConstant()) { |
| 1129 | int32_t constant = length.GetConstant()->AsIntConstant()->GetValue(); |
| 1130 | __ leal(temp3, Address(temp1, element_size * constant)); |
| 1131 | } else { |
| 1132 | __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0)); |
| 1133 | } |
| 1134 | |
| 1135 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1136 | // poison/unpoison, nor do any read barrier as the next uses of the destination |
| 1137 | // array will do it. |
| 1138 | NearLabel loop, done; |
| 1139 | __ cmpl(temp1, temp3); |
| 1140 | __ j(kEqual, &done); |
| 1141 | __ Bind(&loop); |
| 1142 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1143 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1144 | __ addl(temp1, Immediate(element_size)); |
| 1145 | __ addl(temp2, Immediate(element_size)); |
| 1146 | __ cmpl(temp1, temp3); |
| 1147 | __ j(kNotEqual, &loop); |
| 1148 | __ Bind(&done); |
| 1149 | |
| 1150 | // We only need one card marking on the destination array. |
| 1151 | codegen_->MarkGCCard(temp1, |
| 1152 | temp2, |
| 1153 | dest, |
| 1154 | CpuRegister(kNoRegister), |
| 1155 | false); |
| 1156 | |
| 1157 | __ Bind(slow_path->GetExitLabel()); |
| 1158 | } |
| 1159 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1160 | void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1161 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1162 | LocationSummary::kCall, |
| 1163 | kIntrinsified); |
| 1164 | InvokeRuntimeCallingConvention calling_convention; |
| 1165 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1166 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1167 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1168 | } |
| 1169 | |
| 1170 | void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1171 | X86_64Assembler* assembler = GetAssembler(); |
| 1172 | LocationSummary* locations = invoke->GetLocations(); |
| 1173 | |
Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 1174 | // Note that the null check must have been done earlier. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1175 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1176 | |
| 1177 | CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1178 | __ testl(argument, argument); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1179 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1180 | codegen_->AddSlowPath(slow_path); |
| 1181 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1182 | |
| 1183 | __ gs()->call(Address::Absolute( |
| 1184 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true)); |
| 1185 | __ Bind(slow_path->GetExitLabel()); |
| 1186 | } |
| 1187 | |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1188 | void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1189 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1190 | LocationSummary::kNoCall, |
| 1191 | kIntrinsified); |
| 1192 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1193 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1194 | |
| 1195 | // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction. |
| 1196 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1197 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1198 | |
| 1199 | // Set output, RSI needed for repe_cmpsq instruction anyways. |
| 1200 | locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap); |
| 1201 | } |
| 1202 | |
| 1203 | void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1204 | X86_64Assembler* assembler = GetAssembler(); |
| 1205 | LocationSummary* locations = invoke->GetLocations(); |
| 1206 | |
| 1207 | CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1208 | CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1209 | CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1210 | CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1211 | CpuRegister rsi = locations->Out().AsRegister<CpuRegister>(); |
| 1212 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1213 | NearLabel end, return_true, return_false; |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1214 | |
| 1215 | // Get offsets of count, value, and class fields within a string object. |
| 1216 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1217 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1218 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1219 | |
| 1220 | // Note that the null check must have been done earlier. |
| 1221 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1222 | |
| 1223 | // Check if input is null, return false if it is. |
| 1224 | __ testl(arg, arg); |
| 1225 | __ j(kEqual, &return_false); |
| 1226 | |
| 1227 | // Instanceof check for the argument by comparing class fields. |
| 1228 | // All string objects must have the same type since String cannot be subclassed. |
| 1229 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1230 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 1231 | __ movl(rcx, Address(str, class_offset)); |
| 1232 | __ cmpl(rcx, Address(arg, class_offset)); |
| 1233 | __ j(kNotEqual, &return_false); |
| 1234 | |
| 1235 | // Reference equality check, return true if same reference. |
| 1236 | __ cmpl(str, arg); |
| 1237 | __ j(kEqual, &return_true); |
| 1238 | |
| 1239 | // Load length of receiver string. |
| 1240 | __ movl(rcx, Address(str, count_offset)); |
| 1241 | // Check if lengths are equal, return false if they're not. |
| 1242 | __ cmpl(rcx, Address(arg, count_offset)); |
| 1243 | __ j(kNotEqual, &return_false); |
| 1244 | // Return true if both strings are empty. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1245 | __ jrcxz(&return_true); |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1246 | |
| 1247 | // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction. |
| 1248 | __ leal(rsi, Address(str, value_offset)); |
| 1249 | __ leal(rdi, Address(arg, value_offset)); |
| 1250 | |
| 1251 | // Divide string length by 4 and adjust for lengths not divisible by 4. |
| 1252 | __ addl(rcx, Immediate(3)); |
| 1253 | __ shrl(rcx, Immediate(2)); |
| 1254 | |
| 1255 | // Assertions that must hold in order to compare strings 4 characters at a time. |
| 1256 | DCHECK_ALIGNED(value_offset, 8); |
| 1257 | static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded"); |
| 1258 | |
| 1259 | // Loop to compare strings four characters at a time starting at the beginning of the string. |
| 1260 | __ repe_cmpsq(); |
| 1261 | // If strings are not equal, zero flag will be cleared. |
| 1262 | __ j(kNotEqual, &return_false); |
| 1263 | |
| 1264 | // Return true and exit the function. |
| 1265 | // If loop does not result in returning false, we return true. |
| 1266 | __ Bind(&return_true); |
| 1267 | __ movl(rsi, Immediate(1)); |
| 1268 | __ jmp(&end); |
| 1269 | |
| 1270 | // Return false and exit the function. |
| 1271 | __ Bind(&return_false); |
| 1272 | __ xorl(rsi, rsi); |
| 1273 | __ Bind(&end); |
| 1274 | } |
| 1275 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1276 | static void CreateStringIndexOfLocations(HInvoke* invoke, |
| 1277 | ArenaAllocator* allocator, |
| 1278 | bool start_at_zero) { |
| 1279 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1280 | LocationSummary::kCallOnSlowPath, |
| 1281 | kIntrinsified); |
| 1282 | // The data needs to be in RDI for scasw. So request that the string is there, anyways. |
| 1283 | locations->SetInAt(0, Location::RegisterLocation(RDI)); |
| 1284 | // If we look for a constant char, we'll still have to copy it into RAX. So just request the |
| 1285 | // allocator to do that, anyways. We can still do the constant check by checking the parameter |
| 1286 | // of the instruction explicitly. |
| 1287 | // Note: This works as we don't clobber RAX anywhere. |
| 1288 | locations->SetInAt(1, Location::RegisterLocation(RAX)); |
| 1289 | if (!start_at_zero) { |
| 1290 | locations->SetInAt(2, Location::RequiresRegister()); // The starting index. |
| 1291 | } |
| 1292 | // As we clobber RDI during execution anyways, also use it as the output. |
| 1293 | locations->SetOut(Location::SameAsFirstInput()); |
| 1294 | |
| 1295 | // repne scasw uses RCX as the counter. |
| 1296 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1297 | // Need another temporary to be able to compute the result. |
| 1298 | locations->AddTemp(Location::RequiresRegister()); |
| 1299 | } |
| 1300 | |
| 1301 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 1302 | X86_64Assembler* assembler, |
| 1303 | CodeGeneratorX86_64* codegen, |
| 1304 | ArenaAllocator* allocator, |
| 1305 | bool start_at_zero) { |
| 1306 | LocationSummary* locations = invoke->GetLocations(); |
| 1307 | |
| 1308 | // Note that the null check must have been done earlier. |
| 1309 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1310 | |
| 1311 | CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1312 | CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1313 | CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1314 | CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1315 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1316 | |
| 1317 | // Check our assumptions for registers. |
| 1318 | DCHECK_EQ(string_obj.AsRegister(), RDI); |
| 1319 | DCHECK_EQ(search_value.AsRegister(), RAX); |
| 1320 | DCHECK_EQ(counter.AsRegister(), RCX); |
| 1321 | DCHECK_EQ(out.AsRegister(), RDI); |
| 1322 | |
| 1323 | // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically, |
| 1324 | // or directly dispatch if we have a constant. |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1325 | SlowPathCode* slow_path = nullptr; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1326 | if (invoke->InputAt(1)->IsIntConstant()) { |
| 1327 | if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) > |
| 1328 | std::numeric_limits<uint16_t>::max()) { |
| 1329 | // Always needs the slow-path. We could directly dispatch to it, but this case should be |
| 1330 | // rare, so for simplicity just put the full slow-path down and branch unconditionally. |
| 1331 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1332 | codegen->AddSlowPath(slow_path); |
| 1333 | __ jmp(slow_path->GetEntryLabel()); |
| 1334 | __ Bind(slow_path->GetExitLabel()); |
| 1335 | return; |
| 1336 | } |
| 1337 | } else { |
| 1338 | __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max())); |
| 1339 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1340 | codegen->AddSlowPath(slow_path); |
| 1341 | __ j(kAbove, slow_path->GetEntryLabel()); |
| 1342 | } |
| 1343 | |
| 1344 | // From here down, we know that we are looking for a char that fits in 16 bits. |
| 1345 | // Location of reference to data array within the String object. |
| 1346 | int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1347 | // Location of count within the String object. |
| 1348 | int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1349 | |
| 1350 | // Load string length, i.e., the count field of the string. |
| 1351 | __ movl(string_length, Address(string_obj, count_offset)); |
| 1352 | |
| 1353 | // Do a length check. |
| 1354 | // TODO: Support jecxz. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1355 | NearLabel not_found_label; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1356 | __ testl(string_length, string_length); |
| 1357 | __ j(kEqual, ¬_found_label); |
| 1358 | |
| 1359 | if (start_at_zero) { |
| 1360 | // Number of chars to scan is the same as the string length. |
| 1361 | __ movl(counter, string_length); |
| 1362 | |
| 1363 | // Move to the start of the string. |
| 1364 | __ addq(string_obj, Immediate(value_offset)); |
| 1365 | } else { |
| 1366 | CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1367 | |
| 1368 | // Do a start_index check. |
| 1369 | __ cmpl(start_index, string_length); |
| 1370 | __ j(kGreaterEqual, ¬_found_label); |
| 1371 | |
| 1372 | // Ensure we have a start index >= 0; |
| 1373 | __ xorl(counter, counter); |
| 1374 | __ cmpl(start_index, Immediate(0)); |
| 1375 | __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough. |
| 1376 | |
| 1377 | // Move to the start of the string: string_obj + value_offset + 2 * start_index. |
| 1378 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1379 | |
| 1380 | // Now update ecx, the work counter: it's gonna be string.length - start_index. |
| 1381 | __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit. |
| 1382 | __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0)); |
| 1383 | } |
| 1384 | |
| 1385 | // Everything is set up for repne scasw: |
| 1386 | // * Comparison address in RDI. |
| 1387 | // * Counter in ECX. |
| 1388 | __ repne_scasw(); |
| 1389 | |
| 1390 | // Did we find a match? |
| 1391 | __ j(kNotEqual, ¬_found_label); |
| 1392 | |
| 1393 | // Yes, we matched. Compute the index of the result. |
| 1394 | __ subl(string_length, counter); |
| 1395 | __ leal(out, Address(string_length, -1)); |
| 1396 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1397 | NearLabel done; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1398 | __ jmp(&done); |
| 1399 | |
| 1400 | // Failed to match; return -1. |
| 1401 | __ Bind(¬_found_label); |
| 1402 | __ movl(out, Immediate(-1)); |
| 1403 | |
| 1404 | // And join up at the end. |
| 1405 | __ Bind(&done); |
| 1406 | if (slow_path != nullptr) { |
| 1407 | __ Bind(slow_path->GetExitLabel()); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) { |
| 1412 | CreateStringIndexOfLocations(invoke, arena_, true); |
| 1413 | } |
| 1414 | |
| 1415 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) { |
| 1416 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true); |
| 1417 | } |
| 1418 | |
| 1419 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
| 1420 | CreateStringIndexOfLocations(invoke, arena_, false); |
| 1421 | } |
| 1422 | |
| 1423 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
| 1424 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false); |
| 1425 | } |
| 1426 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1427 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1428 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1429 | LocationSummary::kCall, |
| 1430 | kIntrinsified); |
| 1431 | InvokeRuntimeCallingConvention calling_convention; |
| 1432 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1433 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1434 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1435 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 1436 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1437 | } |
| 1438 | |
| 1439 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1440 | X86_64Assembler* assembler = GetAssembler(); |
| 1441 | LocationSummary* locations = invoke->GetLocations(); |
| 1442 | |
| 1443 | CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1444 | __ testl(byte_array, byte_array); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1445 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1446 | codegen_->AddSlowPath(slow_path); |
| 1447 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1448 | |
| 1449 | __ gs()->call(Address::Absolute( |
| 1450 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true)); |
| 1451 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1452 | __ Bind(slow_path->GetExitLabel()); |
| 1453 | } |
| 1454 | |
| 1455 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 1456 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1457 | LocationSummary::kCall, |
| 1458 | kIntrinsified); |
| 1459 | InvokeRuntimeCallingConvention calling_convention; |
| 1460 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1461 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1462 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1463 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1464 | } |
| 1465 | |
| 1466 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 1467 | X86_64Assembler* assembler = GetAssembler(); |
| 1468 | |
| 1469 | __ gs()->call(Address::Absolute( |
| 1470 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true)); |
| 1471 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1472 | } |
| 1473 | |
| 1474 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1475 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1476 | LocationSummary::kCall, |
| 1477 | kIntrinsified); |
| 1478 | InvokeRuntimeCallingConvention calling_convention; |
| 1479 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1480 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1481 | } |
| 1482 | |
| 1483 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1484 | X86_64Assembler* assembler = GetAssembler(); |
| 1485 | LocationSummary* locations = invoke->GetLocations(); |
| 1486 | |
| 1487 | CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1488 | __ testl(string_to_copy, string_to_copy); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1489 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1490 | codegen_->AddSlowPath(slow_path); |
| 1491 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1492 | |
| 1493 | __ gs()->call(Address::Absolute( |
| 1494 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true)); |
| 1495 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1496 | __ Bind(slow_path->GetExitLabel()); |
| 1497 | } |
| 1498 | |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1499 | void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1500 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1501 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1502 | LocationSummary::kNoCall, |
| 1503 | kIntrinsified); |
| 1504 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1505 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1506 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1507 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1508 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1509 | |
| 1510 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 1511 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 1512 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1513 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1514 | } |
| 1515 | |
| 1516 | void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1517 | X86_64Assembler* assembler = GetAssembler(); |
| 1518 | LocationSummary* locations = invoke->GetLocations(); |
| 1519 | |
| 1520 | size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1521 | // Location of data in char array buffer. |
| 1522 | const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value(); |
| 1523 | // Location of char array data in string. |
| 1524 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1525 | |
| 1526 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1527 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1528 | Location srcBegin = locations->InAt(1); |
| 1529 | int srcBegin_value = |
| 1530 | srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0; |
| 1531 | CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1532 | CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1533 | CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1534 | |
| 1535 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 1536 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1537 | DCHECK_EQ(char_size, 2u); |
| 1538 | |
| 1539 | // Compute the address of the destination buffer. |
| 1540 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1541 | |
| 1542 | // Compute the address of the source string. |
| 1543 | if (srcBegin.IsConstant()) { |
| 1544 | // Compute the address of the source string by adding the number of chars from |
| 1545 | // the source beginning to the value offset of a string. |
| 1546 | __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset)); |
| 1547 | } else { |
| 1548 | __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(), |
| 1549 | ScaleFactor::TIMES_2, value_offset)); |
| 1550 | } |
| 1551 | |
| 1552 | // Compute the number of chars (words) to move. |
| 1553 | __ movl(CpuRegister(RCX), srcEnd); |
| 1554 | if (srcBegin.IsConstant()) { |
| 1555 | if (srcBegin_value != 0) { |
| 1556 | __ subl(CpuRegister(RCX), Immediate(srcBegin_value)); |
| 1557 | } |
| 1558 | } else { |
| 1559 | DCHECK(srcBegin.IsRegister()); |
| 1560 | __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>()); |
| 1561 | } |
| 1562 | |
| 1563 | // Do the move. |
| 1564 | __ rep_movsw(); |
| 1565 | } |
| 1566 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1567 | static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 1568 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1569 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity. |
| 1570 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1571 | // to avoid a SIGBUS. |
| 1572 | switch (size) { |
| 1573 | case Primitive::kPrimByte: |
| 1574 | __ movsxb(out, Address(address, 0)); |
| 1575 | break; |
| 1576 | case Primitive::kPrimShort: |
| 1577 | __ movsxw(out, Address(address, 0)); |
| 1578 | break; |
| 1579 | case Primitive::kPrimInt: |
| 1580 | __ movl(out, Address(address, 0)); |
| 1581 | break; |
| 1582 | case Primitive::kPrimLong: |
| 1583 | __ movq(out, Address(address, 0)); |
| 1584 | break; |
| 1585 | default: |
| 1586 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 1587 | UNREACHABLE(); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1592 | CreateIntToIntLocations(arena_, invoke); |
| 1593 | } |
| 1594 | |
| 1595 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1596 | GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1597 | } |
| 1598 | |
| 1599 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1600 | CreateIntToIntLocations(arena_, invoke); |
| 1601 | } |
| 1602 | |
| 1603 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1604 | GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 1605 | } |
| 1606 | |
| 1607 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1608 | CreateIntToIntLocations(arena_, invoke); |
| 1609 | } |
| 1610 | |
| 1611 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1612 | GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 1613 | } |
| 1614 | |
| 1615 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1616 | CreateIntToIntLocations(arena_, invoke); |
| 1617 | } |
| 1618 | |
| 1619 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1620 | GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 1621 | } |
| 1622 | |
| 1623 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1624 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1625 | LocationSummary::kNoCall, |
| 1626 | kIntrinsified); |
| 1627 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 1628 | locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 1632 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1633 | Location value = locations->InAt(1); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1634 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1635 | // to avoid a SIGBUS. |
| 1636 | switch (size) { |
| 1637 | case Primitive::kPrimByte: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1638 | if (value.IsConstant()) { |
| 1639 | __ movb(Address(address, 0), |
| 1640 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1641 | } else { |
| 1642 | __ movb(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1643 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1644 | break; |
| 1645 | case Primitive::kPrimShort: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1646 | if (value.IsConstant()) { |
| 1647 | __ movw(Address(address, 0), |
| 1648 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1649 | } else { |
| 1650 | __ movw(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1651 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1652 | break; |
| 1653 | case Primitive::kPrimInt: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1654 | if (value.IsConstant()) { |
| 1655 | __ movl(Address(address, 0), |
| 1656 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1657 | } else { |
| 1658 | __ movl(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1659 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1660 | break; |
| 1661 | case Primitive::kPrimLong: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1662 | if (value.IsConstant()) { |
| 1663 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
| 1664 | DCHECK(IsInt<32>(v)); |
| 1665 | int32_t v_32 = v; |
| 1666 | __ movq(Address(address, 0), Immediate(v_32)); |
| 1667 | } else { |
| 1668 | __ movq(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1669 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1670 | break; |
| 1671 | default: |
| 1672 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 1673 | UNREACHABLE(); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1678 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1679 | } |
| 1680 | |
| 1681 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1682 | GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1683 | } |
| 1684 | |
| 1685 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1686 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1687 | } |
| 1688 | |
| 1689 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1690 | GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 1691 | } |
| 1692 | |
| 1693 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1694 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1695 | } |
| 1696 | |
| 1697 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1698 | GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 1699 | } |
| 1700 | |
| 1701 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1702 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1703 | } |
| 1704 | |
| 1705 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1706 | GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 1707 | } |
| 1708 | |
| 1709 | void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1710 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1711 | LocationSummary::kNoCall, |
| 1712 | kIntrinsified); |
| 1713 | locations->SetOut(Location::RequiresRegister()); |
| 1714 | } |
| 1715 | |
| 1716 | void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1717 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
| 1718 | GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true)); |
| 1719 | } |
| 1720 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1721 | static void GenUnsafeGet(HInvoke* invoke, |
| 1722 | Primitive::Type type, |
| 1723 | bool is_volatile ATTRIBUTE_UNUSED, |
| 1724 | CodeGeneratorX86_64* codegen) { |
| 1725 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 1726 | LocationSummary* locations = invoke->GetLocations(); |
| 1727 | Location base_loc = locations->InAt(1); |
| 1728 | CpuRegister base = base_loc.AsRegister<CpuRegister>(); |
| 1729 | Location offset_loc = locations->InAt(2); |
| 1730 | CpuRegister offset = offset_loc.AsRegister<CpuRegister>(); |
| 1731 | Location output_loc = locations->Out(); |
| 1732 | CpuRegister output = locations->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1733 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1734 | switch (type) { |
| 1735 | case Primitive::kPrimInt: |
| 1736 | case Primitive::kPrimNot: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1737 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1738 | if (type == Primitive::kPrimNot) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1739 | codegen->MaybeGenerateReadBarrier(invoke, output_loc, output_loc, base_loc, 0U, offset_loc); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1740 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1741 | break; |
| 1742 | |
| 1743 | case Primitive::kPrimLong: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1744 | __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1745 | break; |
| 1746 | |
| 1747 | default: |
| 1748 | LOG(FATAL) << "Unsupported op size " << type; |
| 1749 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1754 | bool can_call = kEmitCompilerReadBarrier && |
| 1755 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject || |
| 1756 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1757 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1758 | can_call ? |
| 1759 | LocationSummary::kCallOnSlowPath : |
| 1760 | LocationSummary::kNoCall, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1761 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1762 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1763 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1764 | locations->SetInAt(2, Location::RequiresRegister()); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1765 | locations->SetOut(Location::RequiresRegister()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) { |
| 1769 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1770 | } |
| 1771 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| 1772 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1773 | } |
| 1774 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
| 1775 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1776 | } |
| 1777 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1778 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1779 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1780 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1781 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1782 | } |
| 1783 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1784 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1785 | } |
| 1786 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1787 | |
| 1788 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1789 | GenUnsafeGet(invoke, Primitive::kPrimInt, false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1790 | } |
| 1791 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1792 | GenUnsafeGet(invoke, Primitive::kPrimInt, true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1793 | } |
| 1794 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1795 | GenUnsafeGet(invoke, Primitive::kPrimLong, false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1796 | } |
| 1797 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1798 | GenUnsafeGet(invoke, Primitive::kPrimLong, true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1799 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1800 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1801 | GenUnsafeGet(invoke, Primitive::kPrimNot, false, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1802 | } |
| 1803 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1804 | GenUnsafeGet(invoke, Primitive::kPrimNot, true, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1807 | |
| 1808 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena, |
| 1809 | Primitive::Type type, |
| 1810 | HInvoke* invoke) { |
| 1811 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1812 | LocationSummary::kNoCall, |
| 1813 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1814 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1815 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1816 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1817 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1818 | if (type == Primitive::kPrimNot) { |
| 1819 | // Need temp registers for card-marking. |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1820 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1821 | locations->AddTemp(Location::RequiresRegister()); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 1826 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1827 | } |
| 1828 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1829 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1830 | } |
| 1831 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1832 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 1833 | } |
| 1834 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 1835 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1836 | } |
| 1837 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1838 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1839 | } |
| 1840 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1841 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 1842 | } |
| 1843 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 1844 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1845 | } |
| 1846 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1847 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1848 | } |
| 1849 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1850 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 1851 | } |
| 1852 | |
| 1853 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 1854 | // memory model. |
| 1855 | static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile, |
| 1856 | CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1857 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1858 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1859 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1860 | CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1861 | |
| 1862 | if (type == Primitive::kPrimLong) { |
| 1863 | __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1864 | } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) { |
| 1865 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1866 | __ movl(temp, value); |
| 1867 | __ PoisonHeapReference(temp); |
| 1868 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1869 | } else { |
| 1870 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 1871 | } |
| 1872 | |
| 1873 | if (is_volatile) { |
| 1874 | __ mfence(); |
| 1875 | } |
| 1876 | |
| 1877 | if (type == Primitive::kPrimNot) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1878 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1879 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 1880 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 1881 | base, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1882 | value, |
| 1883 | value_can_be_null); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 1888 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_); |
| 1889 | } |
| 1890 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1891 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_); |
| 1892 | } |
| 1893 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1894 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_); |
| 1895 | } |
| 1896 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 1897 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_); |
| 1898 | } |
| 1899 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1900 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_); |
| 1901 | } |
| 1902 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1903 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_); |
| 1904 | } |
| 1905 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 1906 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_); |
| 1907 | } |
| 1908 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1909 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_); |
| 1910 | } |
| 1911 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1912 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_); |
| 1913 | } |
| 1914 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1915 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type, |
| 1916 | HInvoke* invoke) { |
| 1917 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1918 | LocationSummary::kNoCall, |
| 1919 | kIntrinsified); |
| 1920 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1921 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1922 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1923 | // expected value must be in EAX/RAX. |
| 1924 | locations->SetInAt(3, Location::RegisterLocation(RAX)); |
| 1925 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1926 | |
| 1927 | locations->SetOut(Location::RequiresRegister()); |
| 1928 | if (type == Primitive::kPrimNot) { |
| 1929 | // Need temp registers for card-marking. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1930 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1931 | locations->AddTemp(Location::RequiresRegister()); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 1936 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke); |
| 1937 | } |
| 1938 | |
| 1939 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 1940 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke); |
| 1941 | } |
| 1942 | |
| 1943 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 1944 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke); |
| 1945 | } |
| 1946 | |
| 1947 | static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1948 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1949 | LocationSummary* locations = invoke->GetLocations(); |
| 1950 | |
| 1951 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1952 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1953 | CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>(); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1954 | // Ensure `expected` is in RAX (required by the CMPXCHG instruction). |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1955 | DCHECK_EQ(expected.AsRegister(), RAX); |
| 1956 | CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1957 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1958 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1959 | if (type == Primitive::kPrimNot) { |
| 1960 | // Mark card for object assuming new value is stored. |
| 1961 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 1962 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 1963 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 1964 | base, |
| 1965 | value, |
| 1966 | value_can_be_null); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1967 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1968 | bool base_equals_value = (base.AsRegister() == value.AsRegister()); |
| 1969 | Register value_reg = value.AsRegister(); |
| 1970 | if (kPoisonHeapReferences) { |
| 1971 | if (base_equals_value) { |
| 1972 | // If `base` and `value` are the same register location, move |
| 1973 | // `value_reg` to a temporary register. This way, poisoning |
| 1974 | // `value_reg` won't invalidate `base`. |
| 1975 | value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister(); |
| 1976 | __ movl(CpuRegister(value_reg), base); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1977 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1978 | |
| 1979 | // Check that the register allocator did not assign the location |
| 1980 | // of `expected` (RAX) to `value` nor to `base`, so that heap |
| 1981 | // poisoning (when enabled) works as intended below. |
| 1982 | // - If `value` were equal to `expected`, both references would |
| 1983 | // be poisoned twice, meaning they would not be poisoned at |
| 1984 | // all, as heap poisoning uses address negation. |
| 1985 | // - If `base` were equal to `expected`, poisoning `expected` |
| 1986 | // would invalidate `base`. |
| 1987 | DCHECK_NE(value_reg, expected.AsRegister()); |
| 1988 | DCHECK_NE(base.AsRegister(), expected.AsRegister()); |
| 1989 | |
| 1990 | __ PoisonHeapReference(expected); |
| 1991 | __ PoisonHeapReference(CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1994 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1995 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1996 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1997 | // scheduling barriers at this time. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 1998 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 1999 | // Convert ZF into the boolean result. |
| 2000 | __ setcc(kZero, out); |
| 2001 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2002 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 2003 | // In the case of the `UnsafeCASObject` intrinsic, accessing an |
| 2004 | // object in the heap with LOCK CMPXCHG does not require a read |
| 2005 | // barrier, as we do not keep a reference to this heap location. |
| 2006 | // However, if heap poisoning is enabled, we need to unpoison the |
| 2007 | // values that were poisoned earlier. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2008 | if (kPoisonHeapReferences) { |
| 2009 | if (base_equals_value) { |
| 2010 | // `value_reg` has been moved to a temporary register, no need |
| 2011 | // to unpoison it. |
| 2012 | } else { |
| 2013 | // Ensure `value` is different from `out`, so that unpoisoning |
| 2014 | // the former does not invalidate the latter. |
| 2015 | DCHECK_NE(value_reg, out.AsRegister()); |
| 2016 | __ UnpoisonHeapReference(CpuRegister(value_reg)); |
| 2017 | } |
| 2018 | // Ensure `expected` is different from `out`, so that unpoisoning |
| 2019 | // the former does not invalidate the latter. |
| 2020 | DCHECK_NE(expected.AsRegister(), out.AsRegister()); |
| 2021 | __ UnpoisonHeapReference(expected); |
| 2022 | } |
| 2023 | } else { |
| 2024 | if (type == Primitive::kPrimInt) { |
| 2025 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value); |
| 2026 | } else if (type == Primitive::kPrimLong) { |
| 2027 | __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value); |
| 2028 | } else { |
| 2029 | LOG(FATAL) << "Unexpected CAS type " << type; |
| 2030 | } |
| 2031 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 2032 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2033 | // scheduling barriers at this time. |
| 2034 | |
| 2035 | // Convert ZF into the boolean result. |
| 2036 | __ setcc(kZero, out); |
| 2037 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2038 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 2042 | GenCAS(Primitive::kPrimInt, invoke, codegen_); |
| 2043 | } |
| 2044 | |
| 2045 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 2046 | GenCAS(Primitive::kPrimLong, invoke, codegen_); |
| 2047 | } |
| 2048 | |
| 2049 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 2050 | GenCAS(Primitive::kPrimNot, invoke, codegen_); |
| 2051 | } |
| 2052 | |
| 2053 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) { |
| 2054 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2055 | LocationSummary::kNoCall, |
| 2056 | kIntrinsified); |
| 2057 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2058 | locations->SetOut(Location::SameAsFirstInput()); |
| 2059 | locations->AddTemp(Location::RequiresRegister()); |
| 2060 | } |
| 2061 | |
| 2062 | static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask, |
| 2063 | X86_64Assembler* assembler) { |
| 2064 | Immediate imm_shift(shift); |
| 2065 | Immediate imm_mask(mask); |
| 2066 | __ movl(temp, reg); |
| 2067 | __ shrl(reg, imm_shift); |
| 2068 | __ andl(temp, imm_mask); |
| 2069 | __ andl(reg, imm_mask); |
| 2070 | __ shll(temp, imm_shift); |
| 2071 | __ orl(reg, temp); |
| 2072 | } |
| 2073 | |
| 2074 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2075 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2076 | LocationSummary* locations = invoke->GetLocations(); |
| 2077 | |
| 2078 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2079 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2080 | |
| 2081 | /* |
| 2082 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2083 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 2084 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2085 | * x = bswap x |
| 2086 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 2087 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 2088 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 2089 | */ |
| 2090 | __ bswapl(reg); |
| 2091 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 2092 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 2093 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 2094 | } |
| 2095 | |
| 2096 | void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) { |
| 2097 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2098 | LocationSummary::kNoCall, |
| 2099 | kIntrinsified); |
| 2100 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2101 | locations->SetOut(Location::SameAsFirstInput()); |
| 2102 | locations->AddTemp(Location::RequiresRegister()); |
| 2103 | locations->AddTemp(Location::RequiresRegister()); |
| 2104 | } |
| 2105 | |
| 2106 | static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask, |
| 2107 | int32_t shift, int64_t mask, X86_64Assembler* assembler) { |
| 2108 | Immediate imm_shift(shift); |
| 2109 | __ movq(temp_mask, Immediate(mask)); |
| 2110 | __ movq(temp, reg); |
| 2111 | __ shrq(reg, imm_shift); |
| 2112 | __ andq(temp, temp_mask); |
| 2113 | __ andq(reg, temp_mask); |
| 2114 | __ shlq(temp, imm_shift); |
| 2115 | __ orq(reg, temp); |
| 2116 | } |
| 2117 | |
| 2118 | void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2119 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2120 | LocationSummary* locations = invoke->GetLocations(); |
| 2121 | |
| 2122 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2123 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2124 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 2125 | |
| 2126 | /* |
| 2127 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2128 | * swapping bits to reverse bits in a long number x. Using bswap to save instructions |
| 2129 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2130 | * x = bswap x |
| 2131 | * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555; |
| 2132 | * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333; |
| 2133 | * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F; |
| 2134 | */ |
| 2135 | __ bswapq(reg); |
| 2136 | SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler); |
| 2137 | SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler); |
| 2138 | SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler); |
| 2139 | } |
| 2140 | |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2141 | static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2142 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2143 | LocationSummary::kNoCall, |
| 2144 | kIntrinsified); |
| 2145 | locations->SetInAt(0, Location::Any()); |
| 2146 | locations->SetOut(Location::RequiresRegister()); |
| 2147 | } |
| 2148 | |
| 2149 | static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) { |
| 2150 | LocationSummary* locations = invoke->GetLocations(); |
| 2151 | Location src = locations->InAt(0); |
| 2152 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2153 | |
| 2154 | int zero_value_result = is_long ? 64 : 32; |
| 2155 | if (invoke->InputAt(0)->IsConstant()) { |
| 2156 | // Evaluate this at compile time. |
| 2157 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2158 | if (value == 0) { |
| 2159 | value = zero_value_result; |
| 2160 | } else { |
| 2161 | value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value)); |
| 2162 | } |
| 2163 | if (value == 0) { |
| 2164 | __ xorl(out, out); |
| 2165 | } else { |
| 2166 | __ movl(out, Immediate(value)); |
| 2167 | } |
| 2168 | return; |
| 2169 | } |
| 2170 | |
| 2171 | // Handle the non-constant cases. |
| 2172 | if (src.IsRegister()) { |
| 2173 | if (is_long) { |
| 2174 | __ bsrq(out, src.AsRegister<CpuRegister>()); |
| 2175 | } else { |
| 2176 | __ bsrl(out, src.AsRegister<CpuRegister>()); |
| 2177 | } |
| 2178 | } else if (is_long) { |
| 2179 | DCHECK(src.IsDoubleStackSlot()); |
| 2180 | __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2181 | } else { |
| 2182 | DCHECK(src.IsStackSlot()); |
| 2183 | __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2184 | } |
| 2185 | |
| 2186 | // 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] | 2187 | NearLabel is_zero, done; |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2188 | __ j(kEqual, &is_zero); |
| 2189 | |
| 2190 | // Correct the result from BSR to get the CLZ result. |
| 2191 | __ xorl(out, Immediate(zero_value_result - 1)); |
| 2192 | __ jmp(&done); |
| 2193 | |
| 2194 | // Fix the zero case with the expected result. |
| 2195 | __ Bind(&is_zero); |
| 2196 | __ movl(out, Immediate(zero_value_result)); |
| 2197 | |
| 2198 | __ Bind(&done); |
| 2199 | } |
| 2200 | |
| 2201 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 2202 | CreateLeadingZeroLocations(arena_, invoke); |
| 2203 | } |
| 2204 | |
| 2205 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 2206 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2207 | GenLeadingZeros(assembler, invoke, /* is_long */ false); |
| 2208 | } |
| 2209 | |
| 2210 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 2211 | CreateLeadingZeroLocations(arena_, invoke); |
| 2212 | } |
| 2213 | |
| 2214 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 2215 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2216 | GenLeadingZeros(assembler, invoke, /* is_long */ true); |
| 2217 | } |
| 2218 | |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2219 | static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2220 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2221 | LocationSummary::kNoCall, |
| 2222 | kIntrinsified); |
| 2223 | locations->SetInAt(0, Location::Any()); |
| 2224 | locations->SetOut(Location::RequiresRegister()); |
| 2225 | } |
| 2226 | |
| 2227 | static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) { |
| 2228 | LocationSummary* locations = invoke->GetLocations(); |
| 2229 | Location src = locations->InAt(0); |
| 2230 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2231 | |
| 2232 | int zero_value_result = is_long ? 64 : 32; |
| 2233 | if (invoke->InputAt(0)->IsConstant()) { |
| 2234 | // Evaluate this at compile time. |
| 2235 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2236 | if (value == 0) { |
| 2237 | value = zero_value_result; |
| 2238 | } else { |
| 2239 | value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value)); |
| 2240 | } |
| 2241 | if (value == 0) { |
| 2242 | __ xorl(out, out); |
| 2243 | } else { |
| 2244 | __ movl(out, Immediate(value)); |
| 2245 | } |
| 2246 | return; |
| 2247 | } |
| 2248 | |
| 2249 | // Handle the non-constant cases. |
| 2250 | if (src.IsRegister()) { |
| 2251 | if (is_long) { |
| 2252 | __ bsfq(out, src.AsRegister<CpuRegister>()); |
| 2253 | } else { |
| 2254 | __ bsfl(out, src.AsRegister<CpuRegister>()); |
| 2255 | } |
| 2256 | } else if (is_long) { |
| 2257 | DCHECK(src.IsDoubleStackSlot()); |
| 2258 | __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2259 | } else { |
| 2260 | DCHECK(src.IsStackSlot()); |
| 2261 | __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2262 | } |
| 2263 | |
| 2264 | // BSF sets ZF if the input was zero, and the output is undefined. |
| 2265 | NearLabel done; |
| 2266 | __ j(kNotEqual, &done); |
| 2267 | |
| 2268 | // Fix the zero case with the expected result. |
| 2269 | __ movl(out, Immediate(zero_value_result)); |
| 2270 | |
| 2271 | __ Bind(&done); |
| 2272 | } |
| 2273 | |
| 2274 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 2275 | CreateTrailingZeroLocations(arena_, invoke); |
| 2276 | } |
| 2277 | |
| 2278 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 2279 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2280 | GenTrailingZeros(assembler, invoke, /* is_long */ false); |
| 2281 | } |
| 2282 | |
| 2283 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 2284 | CreateTrailingZeroLocations(arena_, invoke); |
| 2285 | } |
| 2286 | |
| 2287 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 2288 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2289 | GenTrailingZeros(assembler, invoke, /* is_long */ true); |
| 2290 | } |
| 2291 | |
| 2292 | static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2293 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2294 | LocationSummary::kNoCall, |
| 2295 | kIntrinsified); |
| 2296 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2297 | // The shift count needs to be in CL or a constant. |
| 2298 | locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1))); |
| 2299 | locations->SetOut(Location::SameAsFirstInput()); |
| 2300 | } |
| 2301 | |
| 2302 | static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) { |
| 2303 | LocationSummary* locations = invoke->GetLocations(); |
| 2304 | CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2305 | Location second = locations->InAt(1); |
| 2306 | |
| 2307 | if (is_long) { |
| 2308 | if (second.IsRegister()) { |
| 2309 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 2310 | if (is_left) { |
| 2311 | __ rolq(first_reg, second_reg); |
| 2312 | } else { |
| 2313 | __ rorq(first_reg, second_reg); |
| 2314 | } |
| 2315 | } else { |
| 2316 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue); |
| 2317 | if (is_left) { |
| 2318 | __ rolq(first_reg, imm); |
| 2319 | } else { |
| 2320 | __ rorq(first_reg, imm); |
| 2321 | } |
| 2322 | } |
| 2323 | } else { |
| 2324 | if (second.IsRegister()) { |
| 2325 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 2326 | if (is_left) { |
| 2327 | __ roll(first_reg, second_reg); |
| 2328 | } else { |
| 2329 | __ rorl(first_reg, second_reg); |
| 2330 | } |
| 2331 | } else { |
| 2332 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue); |
| 2333 | if (is_left) { |
| 2334 | __ roll(first_reg, imm); |
| 2335 | } else { |
| 2336 | __ rorl(first_reg, imm); |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) { |
| 2343 | CreateRotateLocations(arena_, invoke); |
| 2344 | } |
| 2345 | |
| 2346 | void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) { |
| 2347 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2348 | GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true); |
| 2349 | } |
| 2350 | |
| 2351 | void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) { |
| 2352 | CreateRotateLocations(arena_, invoke); |
| 2353 | } |
| 2354 | |
| 2355 | void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) { |
| 2356 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2357 | GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false); |
| 2358 | } |
| 2359 | |
| 2360 | void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) { |
| 2361 | CreateRotateLocations(arena_, invoke); |
| 2362 | } |
| 2363 | |
| 2364 | void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) { |
| 2365 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2366 | GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true); |
| 2367 | } |
| 2368 | |
| 2369 | void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) { |
| 2370 | CreateRotateLocations(arena_, invoke); |
| 2371 | } |
| 2372 | |
| 2373 | void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) { |
| 2374 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2375 | GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false); |
| 2376 | } |
| 2377 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2378 | // Unimplemented intrinsics. |
| 2379 | |
| 2380 | #define UNIMPLEMENTED_INTRINSIC(Name) \ |
| 2381 | void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 2382 | } \ |
| 2383 | void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 2384 | } |
| 2385 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2386 | UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent) |
| 2387 | |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2388 | #undef UNIMPLEMENTED_INTRINSIC |
| 2389 | |
| 2390 | #undef __ |
| 2391 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2392 | } // namespace x86_64 |
| 2393 | } // namespace art |