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