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