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) { |
| 613 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 614 | } |
| 615 | |
| 616 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) { |
| 617 | LocationSummary* locations = invoke->GetLocations(); |
| 618 | if (locations->WillCall()) { |
| 619 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int. |
| 624 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 625 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 626 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 627 | NearLabel done, nan; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 628 | X86_64Assembler* assembler = GetAssembler(); |
| 629 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 630 | // Load 0.5 into inPlusPointFive. |
| 631 | __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 632 | |
| 633 | // Add in the input. |
| 634 | __ addss(inPlusPointFive, in); |
| 635 | |
| 636 | // And truncate to an integer. |
| 637 | __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 638 | |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 639 | // Load maxInt into out. |
| 640 | codegen_->Load64BitValue(out, kPrimIntMax); |
| 641 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 642 | // if inPlusPointFive >= maxInt goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 643 | __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 644 | __ j(kAboveEqual, &done); |
| 645 | |
| 646 | // if input == NaN goto nan |
| 647 | __ j(kUnordered, &nan); |
| 648 | |
| 649 | // output = float-to-int-truncate(input) |
| 650 | __ cvttss2si(out, inPlusPointFive); |
| 651 | __ jmp(&done); |
| 652 | __ Bind(&nan); |
| 653 | |
| 654 | // output = 0 |
| 655 | __ xorl(out, out); |
| 656 | __ Bind(&done); |
| 657 | } |
| 658 | |
| 659 | void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 660 | CreateSSE41FPToIntLocations(arena_, invoke, codegen_); |
| 661 | } |
| 662 | |
| 663 | void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) { |
| 664 | LocationSummary* locations = invoke->GetLocations(); |
| 665 | if (locations->WillCall()) { |
| 666 | InvokeOutOfLineIntrinsic(codegen_, invoke); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | // Implement RoundDouble as t1 = floor(input + 0.5); convert to long. |
| 671 | XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>(); |
| 672 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 673 | XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>(); |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 674 | NearLabel done, nan; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 675 | X86_64Assembler* assembler = GetAssembler(); |
| 676 | |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 677 | // Load 0.5 into inPlusPointFive. |
| 678 | __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5)); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 679 | |
| 680 | // Add in the input. |
| 681 | __ addsd(inPlusPointFive, in); |
| 682 | |
| 683 | // And truncate to an integer. |
| 684 | __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1)); |
| 685 | |
Pavel Vyssotski | 9ca2571 | 2015-07-31 13:03:17 +0600 | [diff] [blame] | 686 | // Load maxLong into out. |
| 687 | codegen_->Load64BitValue(out, kPrimLongMax); |
| 688 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 689 | // if inPlusPointFive >= maxLong goto done |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 690 | __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax))); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 691 | __ j(kAboveEqual, &done); |
| 692 | |
| 693 | // if input == NaN goto nan |
| 694 | __ j(kUnordered, &nan); |
| 695 | |
| 696 | // output = double-to-long-truncate(input) |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 697 | __ cvttsd2si(out, inPlusPointFive, /* is64bit */ true); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 698 | __ jmp(&done); |
| 699 | __ Bind(&nan); |
| 700 | |
| 701 | // output = 0 |
Mark Mendell | 92e83bf | 2015-05-07 11:25:03 -0400 | [diff] [blame] | 702 | __ xorl(out, out); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 703 | __ Bind(&done); |
| 704 | } |
| 705 | |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame^] | 706 | static void CreateFPToFPCallLocations(ArenaAllocator* arena, |
| 707 | HInvoke* invoke) { |
| 708 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 709 | LocationSummary::kCall, |
| 710 | kIntrinsified); |
| 711 | InvokeRuntimeCallingConvention calling_convention; |
| 712 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 713 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 714 | |
| 715 | // We have to ensure that the native code doesn't clobber the XMM registers which are |
| 716 | // non-volatile for ART, but volatile for Native calls. This will ensure that they are |
| 717 | // saved in the prologue and properly restored. |
| 718 | for (auto fp_reg : non_volatile_xmm_regs) { |
| 719 | locations->AddTemp(Location::FpuRegisterLocation(fp_reg)); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen, |
| 724 | QuickEntrypointEnum entry) { |
| 725 | LocationSummary* locations = invoke->GetLocations(); |
| 726 | DCHECK(locations->WillCall()); |
| 727 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| 728 | X86_64Assembler* assembler = codegen->GetAssembler(); |
| 729 | |
| 730 | __ gs()->call(Address::Absolute(GetThreadOffset<kX86_64WordSize>(entry), true)); |
| 731 | codegen->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 732 | } |
| 733 | |
| 734 | void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) { |
| 735 | CreateFPToFPCallLocations(arena_, invoke); |
| 736 | } |
| 737 | |
| 738 | void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) { |
| 739 | GenFPToFPCall(invoke, codegen_, kQuickCos); |
| 740 | } |
| 741 | |
| 742 | void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) { |
| 743 | CreateFPToFPCallLocations(arena_, invoke); |
| 744 | } |
| 745 | |
| 746 | void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) { |
| 747 | GenFPToFPCall(invoke, codegen_, kQuickSin); |
| 748 | } |
| 749 | |
| 750 | void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) { |
| 751 | CreateFPToFPCallLocations(arena_, invoke); |
| 752 | } |
| 753 | |
| 754 | void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) { |
| 755 | GenFPToFPCall(invoke, codegen_, kQuickAcos); |
| 756 | } |
| 757 | |
| 758 | void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) { |
| 759 | CreateFPToFPCallLocations(arena_, invoke); |
| 760 | } |
| 761 | |
| 762 | void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) { |
| 763 | GenFPToFPCall(invoke, codegen_, kQuickAsin); |
| 764 | } |
| 765 | |
| 766 | void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) { |
| 767 | CreateFPToFPCallLocations(arena_, invoke); |
| 768 | } |
| 769 | |
| 770 | void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) { |
| 771 | GenFPToFPCall(invoke, codegen_, kQuickAtan); |
| 772 | } |
| 773 | |
| 774 | void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) { |
| 775 | CreateFPToFPCallLocations(arena_, invoke); |
| 776 | } |
| 777 | |
| 778 | void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) { |
| 779 | GenFPToFPCall(invoke, codegen_, kQuickCbrt); |
| 780 | } |
| 781 | |
| 782 | void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) { |
| 783 | CreateFPToFPCallLocations(arena_, invoke); |
| 784 | } |
| 785 | |
| 786 | void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) { |
| 787 | GenFPToFPCall(invoke, codegen_, kQuickCosh); |
| 788 | } |
| 789 | |
| 790 | void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) { |
| 791 | CreateFPToFPCallLocations(arena_, invoke); |
| 792 | } |
| 793 | |
| 794 | void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) { |
| 795 | GenFPToFPCall(invoke, codegen_, kQuickExp); |
| 796 | } |
| 797 | |
| 798 | void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) { |
| 799 | CreateFPToFPCallLocations(arena_, invoke); |
| 800 | } |
| 801 | |
| 802 | void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) { |
| 803 | GenFPToFPCall(invoke, codegen_, kQuickExpm1); |
| 804 | } |
| 805 | |
| 806 | void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) { |
| 807 | CreateFPToFPCallLocations(arena_, invoke); |
| 808 | } |
| 809 | |
| 810 | void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) { |
| 811 | GenFPToFPCall(invoke, codegen_, kQuickLog); |
| 812 | } |
| 813 | |
| 814 | void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) { |
| 815 | CreateFPToFPCallLocations(arena_, invoke); |
| 816 | } |
| 817 | |
| 818 | void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) { |
| 819 | GenFPToFPCall(invoke, codegen_, kQuickLog10); |
| 820 | } |
| 821 | |
| 822 | void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) { |
| 823 | CreateFPToFPCallLocations(arena_, invoke); |
| 824 | } |
| 825 | |
| 826 | void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) { |
| 827 | GenFPToFPCall(invoke, codegen_, kQuickSinh); |
| 828 | } |
| 829 | |
| 830 | void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) { |
| 831 | CreateFPToFPCallLocations(arena_, invoke); |
| 832 | } |
| 833 | |
| 834 | void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) { |
| 835 | GenFPToFPCall(invoke, codegen_, kQuickTan); |
| 836 | } |
| 837 | |
| 838 | void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) { |
| 839 | CreateFPToFPCallLocations(arena_, invoke); |
| 840 | } |
| 841 | |
| 842 | void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) { |
| 843 | GenFPToFPCall(invoke, codegen_, kQuickTanh); |
| 844 | } |
| 845 | |
| 846 | static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, |
| 847 | HInvoke* invoke) { |
| 848 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 849 | LocationSummary::kCall, |
| 850 | kIntrinsified); |
| 851 | InvokeRuntimeCallingConvention calling_convention; |
| 852 | locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); |
| 853 | locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1))); |
| 854 | locations->SetOut(Location::FpuRegisterLocation(XMM0)); |
| 855 | |
| 856 | // We have to ensure that the native code doesn't clobber the XMM registers which are |
| 857 | // non-volatile for ART, but volatile for Native calls. This will ensure that they are |
| 858 | // saved in the prologue and properly restored. |
| 859 | for (auto fp_reg : non_volatile_xmm_regs) { |
| 860 | locations->AddTemp(Location::FpuRegisterLocation(fp_reg)); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) { |
| 865 | CreateFPFPToFPCallLocations(arena_, invoke); |
| 866 | } |
| 867 | |
| 868 | void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) { |
| 869 | GenFPToFPCall(invoke, codegen_, kQuickAtan2); |
| 870 | } |
| 871 | |
| 872 | void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) { |
| 873 | CreateFPFPToFPCallLocations(arena_, invoke); |
| 874 | } |
| 875 | |
| 876 | void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) { |
| 877 | GenFPToFPCall(invoke, codegen_, kQuickHypot); |
| 878 | } |
| 879 | |
| 880 | void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) { |
| 881 | CreateFPFPToFPCallLocations(arena_, invoke); |
| 882 | } |
| 883 | |
| 884 | void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) { |
| 885 | GenFPToFPCall(invoke, codegen_, kQuickNextAfter); |
| 886 | } |
| 887 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 888 | void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 889 | // The inputs plus one temp. |
| 890 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 891 | LocationSummary::kCallOnSlowPath, |
| 892 | kIntrinsified); |
| 893 | locations->SetInAt(0, Location::RequiresRegister()); |
| 894 | locations->SetInAt(1, Location::RequiresRegister()); |
| 895 | locations->SetOut(Location::SameAsFirstInput()); |
| 896 | locations->AddTemp(Location::RequiresRegister()); |
| 897 | } |
| 898 | |
| 899 | void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) { |
| 900 | LocationSummary* locations = invoke->GetLocations(); |
| 901 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 902 | // Location of reference to data array. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 903 | const int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 904 | // Location of count. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 905 | const int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 906 | |
| 907 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 908 | CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>(); |
| 909 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 910 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 911 | // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth |
| 912 | // the cost. |
| 913 | // TODO: For simplicity, the index parameter is requested in a register, so different from Quick |
| 914 | // we will not optimize the code for constants (which would save a register). |
| 915 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 916 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 917 | codegen_->AddSlowPath(slow_path); |
| 918 | |
| 919 | X86_64Assembler* assembler = GetAssembler(); |
| 920 | |
| 921 | __ cmpl(idx, Address(obj, count_offset)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 922 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 923 | __ j(kAboveEqual, slow_path->GetEntryLabel()); |
| 924 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 925 | // out = out[2*idx]. |
| 926 | __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 927 | |
| 928 | __ Bind(slow_path->GetExitLabel()); |
| 929 | } |
| 930 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 931 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 932 | // Check to see if we have known failures that will cause us to have to bail out |
| 933 | // to the runtime, and just generate the runtime call directly. |
| 934 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 935 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 936 | |
| 937 | // The positions must be non-negative. |
| 938 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 939 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 940 | // We will have to fail anyways. |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | // The length must be > 0. |
| 945 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 946 | if (length != nullptr) { |
| 947 | int32_t len = length->GetValue(); |
| 948 | if (len < 0) { |
| 949 | // Just call as normal. |
| 950 | return; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 955 | LocationSummary::kCallOnSlowPath, |
| 956 | kIntrinsified); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 957 | // 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] | 958 | locations->SetInAt(0, Location::RequiresRegister()); |
| 959 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 960 | locations->SetInAt(2, Location::RequiresRegister()); |
| 961 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 962 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 963 | |
| 964 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 965 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 966 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 967 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 968 | } |
| 969 | |
| 970 | static void CheckPosition(X86_64Assembler* assembler, |
| 971 | Location pos, |
| 972 | CpuRegister input, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 973 | Location length, |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 974 | SlowPathCode* slow_path, |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 975 | CpuRegister input_len, |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 976 | CpuRegister temp, |
| 977 | bool length_is_input_length = false) { |
| 978 | // Where is the length in the Array? |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 979 | const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 980 | |
| 981 | if (pos.IsConstant()) { |
| 982 | int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue(); |
| 983 | if (pos_const == 0) { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 984 | if (!length_is_input_length) { |
| 985 | // Check that length(input) >= length. |
| 986 | if (length.IsConstant()) { |
| 987 | __ cmpl(Address(input, length_offset), |
| 988 | Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 989 | } else { |
| 990 | __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>()); |
| 991 | } |
| 992 | __ j(kLess, slow_path->GetEntryLabel()); |
| 993 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 994 | } else { |
| 995 | // Check that length(input) >= pos. |
| 996 | __ movl(input_len, Address(input, length_offset)); |
| 997 | __ cmpl(input_len, Immediate(pos_const)); |
| 998 | __ j(kLess, slow_path->GetEntryLabel()); |
| 999 | |
| 1000 | // Check that (length(input) - pos) >= length. |
| 1001 | __ leal(temp, Address(input_len, -pos_const)); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1002 | if (length.IsConstant()) { |
| 1003 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 1004 | } else { |
| 1005 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 1006 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1007 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1008 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1009 | } else if (length_is_input_length) { |
| 1010 | // The only way the copy can succeed is if pos is zero. |
| 1011 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 1012 | __ testl(pos_reg, pos_reg); |
| 1013 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1014 | } else { |
| 1015 | // Check that pos >= 0. |
| 1016 | CpuRegister pos_reg = pos.AsRegister<CpuRegister>(); |
| 1017 | __ testl(pos_reg, pos_reg); |
| 1018 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1019 | |
| 1020 | // Check that pos <= length(input). |
| 1021 | __ cmpl(Address(input, length_offset), pos_reg); |
| 1022 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1023 | |
| 1024 | // Check that (length(input) - pos) >= length. |
| 1025 | __ movl(temp, Address(input, length_offset)); |
| 1026 | __ subl(temp, pos_reg); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1027 | if (length.IsConstant()) { |
| 1028 | __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 1029 | } else { |
| 1030 | __ cmpl(temp, length.AsRegister<CpuRegister>()); |
| 1031 | } |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1032 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) { |
| 1037 | X86_64Assembler* assembler = GetAssembler(); |
| 1038 | LocationSummary* locations = invoke->GetLocations(); |
| 1039 | |
| 1040 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1041 | Location src_pos = locations->InAt(1); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1042 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1043 | Location dest_pos = locations->InAt(3); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1044 | Location length = locations->InAt(4); |
| 1045 | |
| 1046 | // Temporaries that we need for MOVSW. |
| 1047 | CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1048 | DCHECK_EQ(src_base.AsRegister(), RSI); |
| 1049 | CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1050 | DCHECK_EQ(dest_base.AsRegister(), RDI); |
| 1051 | CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 1052 | DCHECK_EQ(count.AsRegister(), RCX); |
| 1053 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1054 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1055 | codegen_->AddSlowPath(slow_path); |
| 1056 | |
| 1057 | // Bail out if the source and destination are the same. |
| 1058 | __ cmpl(src, dest); |
| 1059 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1060 | |
| 1061 | // Bail out if the source is null. |
| 1062 | __ testl(src, src); |
| 1063 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1064 | |
| 1065 | // Bail out if the destination is null. |
| 1066 | __ testl(dest, dest); |
| 1067 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1068 | |
| 1069 | // If the length is negative, bail out. |
| 1070 | // We have already checked in the LocationsBuilder for the constant case. |
| 1071 | if (!length.IsConstant()) { |
| 1072 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
| 1073 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1074 | } |
| 1075 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1076 | // Validity checks: source. |
| 1077 | CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base); |
| 1078 | |
| 1079 | // Validity checks: dest. |
| 1080 | CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base); |
| 1081 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1082 | // We need the count in RCX. |
| 1083 | if (length.IsConstant()) { |
| 1084 | __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue())); |
| 1085 | } else { |
| 1086 | __ movl(count, length.AsRegister<CpuRegister>()); |
| 1087 | } |
| 1088 | |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1089 | // Okay, everything checks out. Finally time to do the copy. |
| 1090 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 1091 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1092 | DCHECK_EQ(char_size, 2u); |
| 1093 | |
| 1094 | const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value(); |
| 1095 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1096 | if (src_pos.IsConstant()) { |
| 1097 | int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1098 | __ leal(src_base, Address(src, char_size * src_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1099 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1100 | __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1101 | ScaleFactor::TIMES_2, data_offset)); |
| 1102 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1103 | if (dest_pos.IsConstant()) { |
| 1104 | int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1105 | __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset)); |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1106 | } else { |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1107 | __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(), |
Mark Mendell | 6bc53a9 | 2015-07-01 14:26:52 -0400 | [diff] [blame] | 1108 | ScaleFactor::TIMES_2, data_offset)); |
| 1109 | } |
| 1110 | |
| 1111 | // Do the move. |
| 1112 | __ rep_movsw(); |
| 1113 | |
| 1114 | __ Bind(slow_path->GetExitLabel()); |
| 1115 | } |
| 1116 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1117 | |
| 1118 | void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 1119 | CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1122 | // TODO: Implement read barriers in the SystemArrayCopy intrinsic. |
| 1123 | // Note that this code path is not used (yet) because we do not |
| 1124 | // intrinsify methods that can go into the IntrinsicSlowPathX86_64 |
| 1125 | // slow path. |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1126 | void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) { |
| 1127 | X86_64Assembler* assembler = GetAssembler(); |
| 1128 | LocationSummary* locations = invoke->GetLocations(); |
| 1129 | |
| 1130 | uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); |
| 1131 | uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); |
| 1132 | uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); |
| 1133 | uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); |
| 1134 | |
| 1135 | CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1136 | Location src_pos = locations->InAt(1); |
| 1137 | CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1138 | Location dest_pos = locations->InAt(3); |
| 1139 | Location length = locations->InAt(4); |
| 1140 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1141 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1142 | CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>(); |
| 1143 | |
| 1144 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
| 1145 | codegen_->AddSlowPath(slow_path); |
| 1146 | |
| 1147 | NearLabel ok; |
| 1148 | SystemArrayCopyOptimizations optimizations(invoke); |
| 1149 | |
| 1150 | if (!optimizations.GetDestinationIsSource()) { |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 1151 | if (!src_pos.IsConstant() || !dest_pos.IsConstant()) { |
| 1152 | __ cmpl(src, dest); |
| 1153 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | // If source and destination are the same, we go to slow path if we need to do |
| 1157 | // forward copying. |
| 1158 | if (src_pos.IsConstant()) { |
| 1159 | int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1160 | if (dest_pos.IsConstant()) { |
| 1161 | // Checked when building locations. |
| 1162 | DCHECK(!optimizations.GetDestinationIsSource() |
| 1163 | || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue())); |
| 1164 | } else { |
| 1165 | if (!optimizations.GetDestinationIsSource()) { |
| 1166 | __ j(kNotEqual, &ok); |
| 1167 | } |
| 1168 | __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant)); |
| 1169 | __ j(kGreater, slow_path->GetEntryLabel()); |
| 1170 | } |
| 1171 | } else { |
| 1172 | if (!optimizations.GetDestinationIsSource()) { |
| 1173 | __ j(kNotEqual, &ok); |
| 1174 | } |
| 1175 | if (dest_pos.IsConstant()) { |
| 1176 | int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1177 | __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant)); |
| 1178 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1179 | } else { |
| 1180 | __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>()); |
| 1181 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | __ Bind(&ok); |
| 1186 | |
| 1187 | if (!optimizations.GetSourceIsNotNull()) { |
| 1188 | // Bail out if the source is null. |
| 1189 | __ testl(src, src); |
| 1190 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1191 | } |
| 1192 | |
| 1193 | if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) { |
| 1194 | // Bail out if the destination is null. |
| 1195 | __ testl(dest, dest); |
| 1196 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1197 | } |
| 1198 | |
| 1199 | // If the length is negative, bail out. |
| 1200 | // We have already checked in the LocationsBuilder for the constant case. |
| 1201 | if (!length.IsConstant() && |
| 1202 | !optimizations.GetCountIsSourceLength() && |
| 1203 | !optimizations.GetCountIsDestinationLength()) { |
| 1204 | __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>()); |
| 1205 | __ j(kLess, slow_path->GetEntryLabel()); |
| 1206 | } |
| 1207 | |
| 1208 | // Validity checks: source. |
| 1209 | CheckPosition(assembler, |
| 1210 | src_pos, |
| 1211 | src, |
| 1212 | length, |
| 1213 | slow_path, |
| 1214 | temp1, |
| 1215 | temp2, |
| 1216 | optimizations.GetCountIsSourceLength()); |
| 1217 | |
| 1218 | // Validity checks: dest. |
| 1219 | CheckPosition(assembler, |
| 1220 | dest_pos, |
| 1221 | dest, |
| 1222 | length, |
| 1223 | slow_path, |
| 1224 | temp1, |
| 1225 | temp2, |
| 1226 | optimizations.GetCountIsDestinationLength()); |
| 1227 | |
| 1228 | if (!optimizations.GetDoesNotNeedTypeCheck()) { |
| 1229 | // Check whether all elements of the source array are assignable to the component |
| 1230 | // type of the destination array. We do two checks: the classes are the same, |
| 1231 | // or the destination is Object[]. If none of these checks succeed, we go to the |
| 1232 | // slow path. |
| 1233 | __ movl(temp1, Address(dest, class_offset)); |
| 1234 | __ movl(temp2, Address(src, class_offset)); |
| 1235 | bool did_unpoison = false; |
| 1236 | if (!optimizations.GetDestinationIsNonPrimitiveArray() || |
| 1237 | !optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1238 | // One or two of the references need to be unpoisoned. Unpoisoned them |
| 1239 | // both to make the identity check valid. |
| 1240 | __ MaybeUnpoisonHeapReference(temp1); |
| 1241 | __ MaybeUnpoisonHeapReference(temp2); |
| 1242 | did_unpoison = true; |
| 1243 | } |
| 1244 | |
| 1245 | if (!optimizations.GetDestinationIsNonPrimitiveArray()) { |
| 1246 | // Bail out if the destination is not a non primitive array. |
| 1247 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1248 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1249 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1250 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1251 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1252 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1253 | } |
| 1254 | |
| 1255 | if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1256 | // Bail out if the source is not a non primitive array. |
| 1257 | __ movl(CpuRegister(TMP), Address(temp2, component_offset)); |
| 1258 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1259 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1260 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1261 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1262 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1263 | } |
| 1264 | |
| 1265 | __ cmpl(temp1, temp2); |
| 1266 | |
| 1267 | if (optimizations.GetDestinationIsTypedObjectArray()) { |
| 1268 | NearLabel do_copy; |
| 1269 | __ j(kEqual, &do_copy); |
| 1270 | if (!did_unpoison) { |
| 1271 | __ MaybeUnpoisonHeapReference(temp1); |
| 1272 | } |
| 1273 | __ movl(temp1, Address(temp1, component_offset)); |
| 1274 | __ MaybeUnpoisonHeapReference(temp1); |
| 1275 | __ movl(temp1, Address(temp1, super_offset)); |
| 1276 | // No need to unpoison the result, we're comparing against null. |
| 1277 | __ testl(temp1, temp1); |
| 1278 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1279 | __ Bind(&do_copy); |
| 1280 | } else { |
| 1281 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1282 | } |
| 1283 | } else if (!optimizations.GetSourceIsNonPrimitiveArray()) { |
| 1284 | DCHECK(optimizations.GetDestinationIsNonPrimitiveArray()); |
| 1285 | // Bail out if the source is not a non primitive array. |
| 1286 | __ movl(temp1, Address(src, class_offset)); |
| 1287 | __ MaybeUnpoisonHeapReference(temp1); |
| 1288 | __ movl(CpuRegister(TMP), Address(temp1, component_offset)); |
| 1289 | __ testl(CpuRegister(TMP), CpuRegister(TMP)); |
| 1290 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1291 | __ MaybeUnpoisonHeapReference(CpuRegister(TMP)); |
| 1292 | __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot)); |
| 1293 | __ j(kNotEqual, slow_path->GetEntryLabel()); |
| 1294 | } |
| 1295 | |
| 1296 | // Compute base source address, base destination address, and end source address. |
| 1297 | |
| 1298 | uint32_t element_size = sizeof(int32_t); |
| 1299 | uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value(); |
| 1300 | if (src_pos.IsConstant()) { |
| 1301 | int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1302 | __ leal(temp1, Address(src, element_size * constant + offset)); |
| 1303 | } else { |
| 1304 | __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset)); |
| 1305 | } |
| 1306 | |
| 1307 | if (dest_pos.IsConstant()) { |
| 1308 | int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue(); |
| 1309 | __ leal(temp2, Address(dest, element_size * constant + offset)); |
| 1310 | } else { |
| 1311 | __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset)); |
| 1312 | } |
| 1313 | |
| 1314 | if (length.IsConstant()) { |
| 1315 | int32_t constant = length.GetConstant()->AsIntConstant()->GetValue(); |
| 1316 | __ leal(temp3, Address(temp1, element_size * constant)); |
| 1317 | } else { |
| 1318 | __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0)); |
| 1319 | } |
| 1320 | |
| 1321 | // Iterate over the arrays and do a raw copy of the objects. We don't need to |
| 1322 | // poison/unpoison, nor do any read barrier as the next uses of the destination |
| 1323 | // array will do it. |
| 1324 | NearLabel loop, done; |
| 1325 | __ cmpl(temp1, temp3); |
| 1326 | __ j(kEqual, &done); |
| 1327 | __ Bind(&loop); |
| 1328 | __ movl(CpuRegister(TMP), Address(temp1, 0)); |
| 1329 | __ movl(Address(temp2, 0), CpuRegister(TMP)); |
| 1330 | __ addl(temp1, Immediate(element_size)); |
| 1331 | __ addl(temp2, Immediate(element_size)); |
| 1332 | __ cmpl(temp1, temp3); |
| 1333 | __ j(kNotEqual, &loop); |
| 1334 | __ Bind(&done); |
| 1335 | |
| 1336 | // We only need one card marking on the destination array. |
| 1337 | codegen_->MarkGCCard(temp1, |
| 1338 | temp2, |
| 1339 | dest, |
| 1340 | CpuRegister(kNoRegister), |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1341 | /* value_can_be_null */ false); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1342 | |
| 1343 | __ Bind(slow_path->GetExitLabel()); |
| 1344 | } |
| 1345 | |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1346 | void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1347 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1348 | LocationSummary::kCall, |
| 1349 | kIntrinsified); |
| 1350 | InvokeRuntimeCallingConvention calling_convention; |
| 1351 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1352 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1353 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1354 | } |
| 1355 | |
| 1356 | void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) { |
| 1357 | X86_64Assembler* assembler = GetAssembler(); |
| 1358 | LocationSummary* locations = invoke->GetLocations(); |
| 1359 | |
Nicolas Geoffray | 512e04d | 2015-03-27 17:21:24 +0000 | [diff] [blame] | 1360 | // Note that the null check must have been done earlier. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1361 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1362 | |
| 1363 | CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1364 | __ testl(argument, argument); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1365 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1366 | codegen_->AddSlowPath(slow_path); |
| 1367 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1368 | |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1369 | __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), |
| 1370 | /* no_rip */ true)); |
Nicolas Geoffray | d75948a | 2015-03-27 09:53:16 +0000 | [diff] [blame] | 1371 | __ Bind(slow_path->GetExitLabel()); |
| 1372 | } |
| 1373 | |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1374 | void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1375 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1376 | LocationSummary::kNoCall, |
| 1377 | kIntrinsified); |
| 1378 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1379 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1380 | |
| 1381 | // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction. |
| 1382 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1383 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1384 | |
| 1385 | // Set output, RSI needed for repe_cmpsq instruction anyways. |
| 1386 | locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap); |
| 1387 | } |
| 1388 | |
| 1389 | void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) { |
| 1390 | X86_64Assembler* assembler = GetAssembler(); |
| 1391 | LocationSummary* locations = invoke->GetLocations(); |
| 1392 | |
| 1393 | CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1394 | CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1395 | CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1396 | CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1397 | CpuRegister rsi = locations->Out().AsRegister<CpuRegister>(); |
| 1398 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1399 | NearLabel end, return_true, return_false; |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1400 | |
| 1401 | // Get offsets of count, value, and class fields within a string object. |
| 1402 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1403 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1404 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1405 | |
| 1406 | // Note that the null check must have been done earlier. |
| 1407 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1408 | |
| 1409 | // Check if input is null, return false if it is. |
| 1410 | __ testl(arg, arg); |
| 1411 | __ j(kEqual, &return_false); |
| 1412 | |
| 1413 | // Instanceof check for the argument by comparing class fields. |
| 1414 | // All string objects must have the same type since String cannot be subclassed. |
| 1415 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1416 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 1417 | __ movl(rcx, Address(str, class_offset)); |
| 1418 | __ cmpl(rcx, Address(arg, class_offset)); |
| 1419 | __ j(kNotEqual, &return_false); |
| 1420 | |
| 1421 | // Reference equality check, return true if same reference. |
| 1422 | __ cmpl(str, arg); |
| 1423 | __ j(kEqual, &return_true); |
| 1424 | |
| 1425 | // Load length of receiver string. |
| 1426 | __ movl(rcx, Address(str, count_offset)); |
| 1427 | // Check if lengths are equal, return false if they're not. |
| 1428 | __ cmpl(rcx, Address(arg, count_offset)); |
| 1429 | __ j(kNotEqual, &return_false); |
| 1430 | // Return true if both strings are empty. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1431 | __ jrcxz(&return_true); |
Agi Csaki | f8cfb20 | 2015-08-13 17:54:54 -0700 | [diff] [blame] | 1432 | |
| 1433 | // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction. |
| 1434 | __ leal(rsi, Address(str, value_offset)); |
| 1435 | __ leal(rdi, Address(arg, value_offset)); |
| 1436 | |
| 1437 | // Divide string length by 4 and adjust for lengths not divisible by 4. |
| 1438 | __ addl(rcx, Immediate(3)); |
| 1439 | __ shrl(rcx, Immediate(2)); |
| 1440 | |
| 1441 | // Assertions that must hold in order to compare strings 4 characters at a time. |
| 1442 | DCHECK_ALIGNED(value_offset, 8); |
| 1443 | static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded"); |
| 1444 | |
| 1445 | // Loop to compare strings four characters at a time starting at the beginning of the string. |
| 1446 | __ repe_cmpsq(); |
| 1447 | // If strings are not equal, zero flag will be cleared. |
| 1448 | __ j(kNotEqual, &return_false); |
| 1449 | |
| 1450 | // Return true and exit the function. |
| 1451 | // If loop does not result in returning false, we return true. |
| 1452 | __ Bind(&return_true); |
| 1453 | __ movl(rsi, Immediate(1)); |
| 1454 | __ jmp(&end); |
| 1455 | |
| 1456 | // Return false and exit the function. |
| 1457 | __ Bind(&return_false); |
| 1458 | __ xorl(rsi, rsi); |
| 1459 | __ Bind(&end); |
| 1460 | } |
| 1461 | |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1462 | static void CreateStringIndexOfLocations(HInvoke* invoke, |
| 1463 | ArenaAllocator* allocator, |
| 1464 | bool start_at_zero) { |
| 1465 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1466 | LocationSummary::kCallOnSlowPath, |
| 1467 | kIntrinsified); |
| 1468 | // The data needs to be in RDI for scasw. So request that the string is there, anyways. |
| 1469 | locations->SetInAt(0, Location::RegisterLocation(RDI)); |
| 1470 | // If we look for a constant char, we'll still have to copy it into RAX. So just request the |
| 1471 | // allocator to do that, anyways. We can still do the constant check by checking the parameter |
| 1472 | // of the instruction explicitly. |
| 1473 | // Note: This works as we don't clobber RAX anywhere. |
| 1474 | locations->SetInAt(1, Location::RegisterLocation(RAX)); |
| 1475 | if (!start_at_zero) { |
| 1476 | locations->SetInAt(2, Location::RequiresRegister()); // The starting index. |
| 1477 | } |
| 1478 | // As we clobber RDI during execution anyways, also use it as the output. |
| 1479 | locations->SetOut(Location::SameAsFirstInput()); |
| 1480 | |
| 1481 | // repne scasw uses RCX as the counter. |
| 1482 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1483 | // Need another temporary to be able to compute the result. |
| 1484 | locations->AddTemp(Location::RequiresRegister()); |
| 1485 | } |
| 1486 | |
| 1487 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 1488 | X86_64Assembler* assembler, |
| 1489 | CodeGeneratorX86_64* codegen, |
| 1490 | ArenaAllocator* allocator, |
| 1491 | bool start_at_zero) { |
| 1492 | LocationSummary* locations = invoke->GetLocations(); |
| 1493 | |
| 1494 | // Note that the null check must have been done earlier. |
| 1495 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1496 | |
| 1497 | CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1498 | CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>(); |
| 1499 | CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 1500 | CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 1501 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 1502 | |
| 1503 | // Check our assumptions for registers. |
| 1504 | DCHECK_EQ(string_obj.AsRegister(), RDI); |
| 1505 | DCHECK_EQ(search_value.AsRegister(), RAX); |
| 1506 | DCHECK_EQ(counter.AsRegister(), RCX); |
| 1507 | DCHECK_EQ(out.AsRegister(), RDI); |
| 1508 | |
| 1509 | // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically, |
| 1510 | // or directly dispatch if we have a constant. |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1511 | SlowPathCode* slow_path = nullptr; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1512 | if (invoke->InputAt(1)->IsIntConstant()) { |
| 1513 | if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) > |
| 1514 | std::numeric_limits<uint16_t>::max()) { |
| 1515 | // Always needs the slow-path. We could directly dispatch to it, but this case should be |
| 1516 | // rare, so for simplicity just put the full slow-path down and branch unconditionally. |
| 1517 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1518 | codegen->AddSlowPath(slow_path); |
| 1519 | __ jmp(slow_path->GetEntryLabel()); |
| 1520 | __ Bind(slow_path->GetExitLabel()); |
| 1521 | return; |
| 1522 | } |
| 1523 | } else { |
| 1524 | __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max())); |
| 1525 | slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke); |
| 1526 | codegen->AddSlowPath(slow_path); |
| 1527 | __ j(kAbove, slow_path->GetEntryLabel()); |
| 1528 | } |
| 1529 | |
| 1530 | // From here down, we know that we are looking for a char that fits in 16 bits. |
| 1531 | // Location of reference to data array within the String object. |
| 1532 | int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1533 | // Location of count within the String object. |
| 1534 | int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1535 | |
| 1536 | // Load string length, i.e., the count field of the string. |
| 1537 | __ movl(string_length, Address(string_obj, count_offset)); |
| 1538 | |
| 1539 | // Do a length check. |
| 1540 | // TODO: Support jecxz. |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1541 | NearLabel not_found_label; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1542 | __ testl(string_length, string_length); |
| 1543 | __ j(kEqual, ¬_found_label); |
| 1544 | |
| 1545 | if (start_at_zero) { |
| 1546 | // Number of chars to scan is the same as the string length. |
| 1547 | __ movl(counter, string_length); |
| 1548 | |
| 1549 | // Move to the start of the string. |
| 1550 | __ addq(string_obj, Immediate(value_offset)); |
| 1551 | } else { |
| 1552 | CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1553 | |
| 1554 | // Do a start_index check. |
| 1555 | __ cmpl(start_index, string_length); |
| 1556 | __ j(kGreaterEqual, ¬_found_label); |
| 1557 | |
| 1558 | // Ensure we have a start index >= 0; |
| 1559 | __ xorl(counter, counter); |
| 1560 | __ cmpl(start_index, Immediate(0)); |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1561 | __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough. |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1562 | |
| 1563 | // Move to the start of the string: string_obj + value_offset + 2 * start_index. |
| 1564 | __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset)); |
| 1565 | |
| 1566 | // Now update ecx, the work counter: it's gonna be string.length - start_index. |
| 1567 | __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit. |
| 1568 | __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0)); |
| 1569 | } |
| 1570 | |
| 1571 | // Everything is set up for repne scasw: |
| 1572 | // * Comparison address in RDI. |
| 1573 | // * Counter in ECX. |
| 1574 | __ repne_scasw(); |
| 1575 | |
| 1576 | // Did we find a match? |
| 1577 | __ j(kNotEqual, ¬_found_label); |
| 1578 | |
| 1579 | // Yes, we matched. Compute the index of the result. |
| 1580 | __ subl(string_length, counter); |
| 1581 | __ leal(out, Address(string_length, -1)); |
| 1582 | |
Mark Mendell | 0c9497d | 2015-08-21 09:30:05 -0400 | [diff] [blame] | 1583 | NearLabel done; |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1584 | __ jmp(&done); |
| 1585 | |
| 1586 | // Failed to match; return -1. |
| 1587 | __ Bind(¬_found_label); |
| 1588 | __ movl(out, Immediate(-1)); |
| 1589 | |
| 1590 | // And join up at the end. |
| 1591 | __ Bind(&done); |
| 1592 | if (slow_path != nullptr) { |
| 1593 | __ Bind(slow_path->GetExitLabel()); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1598 | CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1602 | GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1606 | CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1610 | GenerateStringIndexOf( |
| 1611 | invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false); |
Andreas Gampe | 21030dd | 2015-05-07 14:46:15 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1614 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1615 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1616 | LocationSummary::kCall, |
| 1617 | kIntrinsified); |
| 1618 | InvokeRuntimeCallingConvention calling_convention; |
| 1619 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1620 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1621 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1622 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 1623 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1624 | } |
| 1625 | |
| 1626 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 1627 | X86_64Assembler* assembler = GetAssembler(); |
| 1628 | LocationSummary* locations = invoke->GetLocations(); |
| 1629 | |
| 1630 | CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1631 | __ testl(byte_array, byte_array); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1632 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1633 | codegen_->AddSlowPath(slow_path); |
| 1634 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1635 | |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1636 | __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), |
| 1637 | /* no_rip */ true)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1638 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1639 | __ Bind(slow_path->GetExitLabel()); |
| 1640 | } |
| 1641 | |
| 1642 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 1643 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1644 | LocationSummary::kCall, |
| 1645 | kIntrinsified); |
| 1646 | InvokeRuntimeCallingConvention calling_convention; |
| 1647 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1648 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1649 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 1650 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1651 | } |
| 1652 | |
| 1653 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 1654 | X86_64Assembler* assembler = GetAssembler(); |
| 1655 | |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1656 | __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), |
| 1657 | /* no_rip */ true)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1658 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1659 | } |
| 1660 | |
| 1661 | void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1662 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1663 | LocationSummary::kCall, |
| 1664 | kIntrinsified); |
| 1665 | InvokeRuntimeCallingConvention calling_convention; |
| 1666 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1667 | locations->SetOut(Location::RegisterLocation(RAX)); |
| 1668 | } |
| 1669 | |
| 1670 | void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) { |
| 1671 | X86_64Assembler* assembler = GetAssembler(); |
| 1672 | LocationSummary* locations = invoke->GetLocations(); |
| 1673 | |
| 1674 | CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1675 | __ testl(string_to_copy, string_to_copy); |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 1676 | SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1677 | codegen_->AddSlowPath(slow_path); |
| 1678 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1679 | |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1680 | __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), |
| 1681 | /* no_rip */ true)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1682 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 1683 | __ Bind(slow_path->GetExitLabel()); |
| 1684 | } |
| 1685 | |
Mark Mendell | 8f8926a | 2015-08-17 11:39:06 -0400 | [diff] [blame] | 1686 | void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1687 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1688 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1689 | LocationSummary::kNoCall, |
| 1690 | kIntrinsified); |
| 1691 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1692 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1693 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1694 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1695 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1696 | |
| 1697 | // And we need some temporaries. We will use REP MOVSW, so we need fixed registers. |
| 1698 | locations->AddTemp(Location::RegisterLocation(RSI)); |
| 1699 | locations->AddTemp(Location::RegisterLocation(RDI)); |
| 1700 | locations->AddTemp(Location::RegisterLocation(RCX)); |
| 1701 | } |
| 1702 | |
| 1703 | void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 1704 | X86_64Assembler* assembler = GetAssembler(); |
| 1705 | LocationSummary* locations = invoke->GetLocations(); |
| 1706 | |
| 1707 | size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1708 | // Location of data in char array buffer. |
| 1709 | const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value(); |
| 1710 | // Location of char array data in string. |
| 1711 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1712 | |
| 1713 | // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin); |
| 1714 | CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1715 | Location srcBegin = locations->InAt(1); |
| 1716 | int srcBegin_value = |
| 1717 | srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0; |
| 1718 | CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>(); |
| 1719 | CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>(); |
| 1720 | CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>(); |
| 1721 | |
| 1722 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 1723 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 1724 | DCHECK_EQ(char_size, 2u); |
| 1725 | |
| 1726 | // Compute the address of the destination buffer. |
| 1727 | __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset)); |
| 1728 | |
| 1729 | // Compute the address of the source string. |
| 1730 | if (srcBegin.IsConstant()) { |
| 1731 | // Compute the address of the source string by adding the number of chars from |
| 1732 | // the source beginning to the value offset of a string. |
| 1733 | __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset)); |
| 1734 | } else { |
| 1735 | __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(), |
| 1736 | ScaleFactor::TIMES_2, value_offset)); |
| 1737 | } |
| 1738 | |
| 1739 | // Compute the number of chars (words) to move. |
| 1740 | __ movl(CpuRegister(RCX), srcEnd); |
| 1741 | if (srcBegin.IsConstant()) { |
| 1742 | if (srcBegin_value != 0) { |
| 1743 | __ subl(CpuRegister(RCX), Immediate(srcBegin_value)); |
| 1744 | } |
| 1745 | } else { |
| 1746 | DCHECK(srcBegin.IsRegister()); |
| 1747 | __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>()); |
| 1748 | } |
| 1749 | |
| 1750 | // Do the move. |
| 1751 | __ rep_movsw(); |
| 1752 | } |
| 1753 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1754 | static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 1755 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
| 1756 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity. |
| 1757 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1758 | // to avoid a SIGBUS. |
| 1759 | switch (size) { |
| 1760 | case Primitive::kPrimByte: |
| 1761 | __ movsxb(out, Address(address, 0)); |
| 1762 | break; |
| 1763 | case Primitive::kPrimShort: |
| 1764 | __ movsxw(out, Address(address, 0)); |
| 1765 | break; |
| 1766 | case Primitive::kPrimInt: |
| 1767 | __ movl(out, Address(address, 0)); |
| 1768 | break; |
| 1769 | case Primitive::kPrimLong: |
| 1770 | __ movq(out, Address(address, 0)); |
| 1771 | break; |
| 1772 | default: |
| 1773 | LOG(FATAL) << "Type not recognized for peek: " << size; |
| 1774 | UNREACHABLE(); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1779 | CreateIntToIntLocations(arena_, invoke); |
| 1780 | } |
| 1781 | |
| 1782 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1783 | GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1784 | } |
| 1785 | |
| 1786 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1787 | CreateIntToIntLocations(arena_, invoke); |
| 1788 | } |
| 1789 | |
| 1790 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1791 | GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 1792 | } |
| 1793 | |
| 1794 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1795 | CreateIntToIntLocations(arena_, invoke); |
| 1796 | } |
| 1797 | |
| 1798 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1799 | GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 1800 | } |
| 1801 | |
| 1802 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1803 | CreateIntToIntLocations(arena_, invoke); |
| 1804 | } |
| 1805 | |
| 1806 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1807 | GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 1808 | } |
| 1809 | |
| 1810 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1811 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1812 | LocationSummary::kNoCall, |
| 1813 | kIntrinsified); |
| 1814 | locations->SetInAt(0, Location::RequiresRegister()); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 1815 | locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1))); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) { |
| 1819 | CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>(); |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1820 | Location value = locations->InAt(1); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1821 | // x86 allows unaligned access. We do not have to check the input or use specific instructions |
| 1822 | // to avoid a SIGBUS. |
| 1823 | switch (size) { |
| 1824 | case Primitive::kPrimByte: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1825 | if (value.IsConstant()) { |
| 1826 | __ movb(Address(address, 0), |
| 1827 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1828 | } else { |
| 1829 | __ movb(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1830 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1831 | break; |
| 1832 | case Primitive::kPrimShort: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1833 | if (value.IsConstant()) { |
| 1834 | __ movw(Address(address, 0), |
| 1835 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1836 | } else { |
| 1837 | __ movw(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1838 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1839 | break; |
| 1840 | case Primitive::kPrimInt: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1841 | if (value.IsConstant()) { |
| 1842 | __ movl(Address(address, 0), |
| 1843 | Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant()))); |
| 1844 | } else { |
| 1845 | __ movl(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1846 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1847 | break; |
| 1848 | case Primitive::kPrimLong: |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 1849 | if (value.IsConstant()) { |
| 1850 | int64_t v = value.GetConstant()->AsLongConstant()->GetValue(); |
| 1851 | DCHECK(IsInt<32>(v)); |
| 1852 | int32_t v_32 = v; |
| 1853 | __ movq(Address(address, 0), Immediate(v_32)); |
| 1854 | } else { |
| 1855 | __ movq(Address(address, 0), value.AsRegister<CpuRegister>()); |
| 1856 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1857 | break; |
| 1858 | default: |
| 1859 | LOG(FATAL) << "Type not recognized for poke: " << size; |
| 1860 | UNREACHABLE(); |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1865 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1866 | } |
| 1867 | |
| 1868 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1869 | GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler()); |
| 1870 | } |
| 1871 | |
| 1872 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1873 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1874 | } |
| 1875 | |
| 1876 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1877 | GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler()); |
| 1878 | } |
| 1879 | |
| 1880 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1881 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1882 | } |
| 1883 | |
| 1884 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1885 | GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler()); |
| 1886 | } |
| 1887 | |
| 1888 | void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1889 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1890 | } |
| 1891 | |
| 1892 | void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1893 | GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler()); |
| 1894 | } |
| 1895 | |
| 1896 | void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1897 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1898 | LocationSummary::kNoCall, |
| 1899 | kIntrinsified); |
| 1900 | locations->SetOut(Location::RequiresRegister()); |
| 1901 | } |
| 1902 | |
| 1903 | void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1904 | CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>(); |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1905 | GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), |
| 1906 | /* no_rip */ true)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1909 | static void GenUnsafeGet(HInvoke* invoke, |
| 1910 | Primitive::Type type, |
| 1911 | bool is_volatile ATTRIBUTE_UNUSED, |
| 1912 | CodeGeneratorX86_64* codegen) { |
| 1913 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
| 1914 | LocationSummary* locations = invoke->GetLocations(); |
| 1915 | Location base_loc = locations->InAt(1); |
| 1916 | CpuRegister base = base_loc.AsRegister<CpuRegister>(); |
| 1917 | Location offset_loc = locations->InAt(2); |
| 1918 | CpuRegister offset = offset_loc.AsRegister<CpuRegister>(); |
| 1919 | Location output_loc = locations->Out(); |
| 1920 | CpuRegister output = locations->Out().AsRegister<CpuRegister>(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1921 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1922 | switch (type) { |
| 1923 | case Primitive::kPrimInt: |
| 1924 | case Primitive::kPrimNot: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1925 | __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1926 | if (type == Primitive::kPrimNot) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1927 | codegen->MaybeGenerateReadBarrier(invoke, output_loc, output_loc, base_loc, 0U, offset_loc); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 1928 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1929 | break; |
| 1930 | |
| 1931 | case Primitive::kPrimLong: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1932 | __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0)); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1933 | break; |
| 1934 | |
| 1935 | default: |
| 1936 | LOG(FATAL) << "Unsupported op size " << type; |
| 1937 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1942 | bool can_call = kEmitCompilerReadBarrier && |
| 1943 | (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject || |
| 1944 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1945 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1946 | can_call ? |
| 1947 | LocationSummary::kCallOnSlowPath : |
| 1948 | LocationSummary::kNoCall, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1949 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1950 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1951 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1952 | locations->SetInAt(2, Location::RequiresRegister()); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1953 | locations->SetOut(Location::RequiresRegister()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) { |
| 1957 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1958 | } |
| 1959 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| 1960 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1961 | } |
| 1962 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
| 1963 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1964 | } |
| 1965 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1966 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1967 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1968 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1969 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1970 | } |
| 1971 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1972 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1973 | } |
| 1974 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1975 | |
| 1976 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1977 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1978 | } |
| 1979 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1980 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1981 | } |
| 1982 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1983 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1984 | } |
| 1985 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1986 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1987 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1988 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1989 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1990 | } |
| 1991 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 1992 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1995 | |
| 1996 | static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena, |
| 1997 | Primitive::Type type, |
| 1998 | HInvoke* invoke) { |
| 1999 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2000 | LocationSummary::kNoCall, |
| 2001 | kIntrinsified); |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 2002 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2003 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2004 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2005 | locations->SetInAt(3, Location::RequiresRegister()); |
| 2006 | if (type == Primitive::kPrimNot) { |
| 2007 | // Need temp registers for card-marking. |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2008 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2009 | locations->AddTemp(Location::RequiresRegister()); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) { |
| 2014 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2015 | } |
| 2016 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 2017 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2018 | } |
| 2019 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 2020 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke); |
| 2021 | } |
| 2022 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
| 2023 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2024 | } |
| 2025 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 2026 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2027 | } |
| 2028 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 2029 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke); |
| 2030 | } |
| 2031 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
| 2032 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2033 | } |
| 2034 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 2035 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2036 | } |
| 2037 | void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 2038 | CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke); |
| 2039 | } |
| 2040 | |
| 2041 | // We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86 |
| 2042 | // memory model. |
| 2043 | static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile, |
| 2044 | CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2045 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2046 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2047 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2048 | CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>(); |
| 2049 | |
| 2050 | if (type == Primitive::kPrimLong) { |
| 2051 | __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2052 | } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) { |
| 2053 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2054 | __ movl(temp, value); |
| 2055 | __ PoisonHeapReference(temp); |
| 2056 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2057 | } else { |
| 2058 | __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value); |
| 2059 | } |
| 2060 | |
| 2061 | if (is_volatile) { |
| 2062 | __ mfence(); |
| 2063 | } |
| 2064 | |
| 2065 | if (type == Primitive::kPrimNot) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2066 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2067 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 2068 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 2069 | base, |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 2070 | value, |
| 2071 | value_can_be_null); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2076 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2077 | } |
| 2078 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2079 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2080 | } |
| 2081 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2082 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2083 | } |
| 2084 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2085 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2086 | } |
| 2087 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2088 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2089 | } |
| 2090 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2091 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2092 | } |
| 2093 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2094 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2095 | } |
| 2096 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2097 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2098 | } |
| 2099 | void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 2100 | GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2103 | static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type, |
| 2104 | HInvoke* invoke) { |
| 2105 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2106 | LocationSummary::kNoCall, |
| 2107 | kIntrinsified); |
| 2108 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 2109 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2110 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2111 | // expected value must be in EAX/RAX. |
| 2112 | locations->SetInAt(3, Location::RegisterLocation(RAX)); |
| 2113 | locations->SetInAt(4, Location::RequiresRegister()); |
| 2114 | |
| 2115 | locations->SetOut(Location::RequiresRegister()); |
| 2116 | if (type == Primitive::kPrimNot) { |
| 2117 | // Need temp registers for card-marking. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2118 | locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2119 | locations->AddTemp(Location::RequiresRegister()); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 2124 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke); |
| 2125 | } |
| 2126 | |
| 2127 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 2128 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke); |
| 2129 | } |
| 2130 | |
| 2131 | void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 2132 | CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke); |
| 2133 | } |
| 2134 | |
| 2135 | static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2136 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2137 | LocationSummary* locations = invoke->GetLocations(); |
| 2138 | |
| 2139 | CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>(); |
| 2140 | CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>(); |
| 2141 | CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>(); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2142 | // Ensure `expected` is in RAX (required by the CMPXCHG instruction). |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2143 | DCHECK_EQ(expected.AsRegister(), RAX); |
| 2144 | CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>(); |
| 2145 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2146 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2147 | if (type == Primitive::kPrimNot) { |
| 2148 | // Mark card for object assuming new value is stored. |
| 2149 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 2150 | codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(), |
| 2151 | locations->GetTemp(1).AsRegister<CpuRegister>(), |
| 2152 | base, |
| 2153 | value, |
| 2154 | value_can_be_null); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2155 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2156 | bool base_equals_value = (base.AsRegister() == value.AsRegister()); |
| 2157 | Register value_reg = value.AsRegister(); |
| 2158 | if (kPoisonHeapReferences) { |
| 2159 | if (base_equals_value) { |
| 2160 | // If `base` and `value` are the same register location, move |
| 2161 | // `value_reg` to a temporary register. This way, poisoning |
| 2162 | // `value_reg` won't invalidate `base`. |
| 2163 | value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister(); |
| 2164 | __ movl(CpuRegister(value_reg), base); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2165 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2166 | |
| 2167 | // Check that the register allocator did not assign the location |
| 2168 | // of `expected` (RAX) to `value` nor to `base`, so that heap |
| 2169 | // poisoning (when enabled) works as intended below. |
| 2170 | // - If `value` were equal to `expected`, both references would |
| 2171 | // be poisoned twice, meaning they would not be poisoned at |
| 2172 | // all, as heap poisoning uses address negation. |
| 2173 | // - If `base` were equal to `expected`, poisoning `expected` |
| 2174 | // would invalidate `base`. |
| 2175 | DCHECK_NE(value_reg, expected.AsRegister()); |
| 2176 | DCHECK_NE(base.AsRegister(), expected.AsRegister()); |
| 2177 | |
| 2178 | __ PoisonHeapReference(expected); |
| 2179 | __ PoisonHeapReference(CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2180 | } |
| 2181 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2182 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg)); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2183 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2184 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2185 | // scheduling barriers at this time. |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2186 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2187 | // Convert ZF into the boolean result. |
| 2188 | __ setcc(kZero, out); |
| 2189 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2190 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2191 | // In the case of the `UnsafeCASObject` intrinsic, accessing an |
| 2192 | // object in the heap with LOCK CMPXCHG does not require a read |
| 2193 | // barrier, as we do not keep a reference to this heap location. |
| 2194 | // However, if heap poisoning is enabled, we need to unpoison the |
| 2195 | // values that were poisoned earlier. |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2196 | if (kPoisonHeapReferences) { |
| 2197 | if (base_equals_value) { |
| 2198 | // `value_reg` has been moved to a temporary register, no need |
| 2199 | // to unpoison it. |
| 2200 | } else { |
| 2201 | // Ensure `value` is different from `out`, so that unpoisoning |
| 2202 | // the former does not invalidate the latter. |
| 2203 | DCHECK_NE(value_reg, out.AsRegister()); |
| 2204 | __ UnpoisonHeapReference(CpuRegister(value_reg)); |
| 2205 | } |
| 2206 | // Ensure `expected` is different from `out`, so that unpoisoning |
| 2207 | // the former does not invalidate the latter. |
| 2208 | DCHECK_NE(expected.AsRegister(), out.AsRegister()); |
| 2209 | __ UnpoisonHeapReference(expected); |
| 2210 | } |
| 2211 | } else { |
| 2212 | if (type == Primitive::kPrimInt) { |
| 2213 | __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value); |
| 2214 | } else if (type == Primitive::kPrimLong) { |
| 2215 | __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value); |
| 2216 | } else { |
| 2217 | LOG(FATAL) << "Unexpected CAS type " << type; |
| 2218 | } |
| 2219 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 2220 | // LOCK CMPXCHG has full barrier semantics, and we don't need |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2221 | // scheduling barriers at this time. |
| 2222 | |
| 2223 | // Convert ZF into the boolean result. |
| 2224 | __ setcc(kZero, out); |
| 2225 | __ movzxb(out, out); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2226 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) { |
| 2230 | GenCAS(Primitive::kPrimInt, invoke, codegen_); |
| 2231 | } |
| 2232 | |
| 2233 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) { |
| 2234 | GenCAS(Primitive::kPrimLong, invoke, codegen_); |
| 2235 | } |
| 2236 | |
| 2237 | void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) { |
| 2238 | GenCAS(Primitive::kPrimNot, invoke, codegen_); |
| 2239 | } |
| 2240 | |
| 2241 | void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) { |
| 2242 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2243 | LocationSummary::kNoCall, |
| 2244 | kIntrinsified); |
| 2245 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2246 | locations->SetOut(Location::SameAsFirstInput()); |
| 2247 | locations->AddTemp(Location::RequiresRegister()); |
| 2248 | } |
| 2249 | |
| 2250 | static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask, |
| 2251 | X86_64Assembler* assembler) { |
| 2252 | Immediate imm_shift(shift); |
| 2253 | Immediate imm_mask(mask); |
| 2254 | __ movl(temp, reg); |
| 2255 | __ shrl(reg, imm_shift); |
| 2256 | __ andl(temp, imm_mask); |
| 2257 | __ andl(reg, imm_mask); |
| 2258 | __ shll(temp, imm_shift); |
| 2259 | __ orl(reg, temp); |
| 2260 | } |
| 2261 | |
| 2262 | void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2263 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2264 | LocationSummary* locations = invoke->GetLocations(); |
| 2265 | |
| 2266 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2267 | CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2268 | |
| 2269 | /* |
| 2270 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2271 | * swapping bits to reverse bits in a number x. Using bswap to save instructions |
| 2272 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2273 | * x = bswap x |
| 2274 | * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; |
| 2275 | * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; |
| 2276 | * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F; |
| 2277 | */ |
| 2278 | __ bswapl(reg); |
| 2279 | SwapBits(reg, temp, 1, 0x55555555, assembler); |
| 2280 | SwapBits(reg, temp, 2, 0x33333333, assembler); |
| 2281 | SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler); |
| 2282 | } |
| 2283 | |
| 2284 | void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) { |
| 2285 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2286 | LocationSummary::kNoCall, |
| 2287 | kIntrinsified); |
| 2288 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2289 | locations->SetOut(Location::SameAsFirstInput()); |
| 2290 | locations->AddTemp(Location::RequiresRegister()); |
| 2291 | locations->AddTemp(Location::RequiresRegister()); |
| 2292 | } |
| 2293 | |
| 2294 | static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask, |
| 2295 | int32_t shift, int64_t mask, X86_64Assembler* assembler) { |
| 2296 | Immediate imm_shift(shift); |
| 2297 | __ movq(temp_mask, Immediate(mask)); |
| 2298 | __ movq(temp, reg); |
| 2299 | __ shrq(reg, imm_shift); |
| 2300 | __ andq(temp, temp_mask); |
| 2301 | __ andq(reg, temp_mask); |
| 2302 | __ shlq(temp, imm_shift); |
| 2303 | __ orq(reg, temp); |
| 2304 | } |
| 2305 | |
| 2306 | void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) { |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 2307 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 2308 | LocationSummary* locations = invoke->GetLocations(); |
| 2309 | |
| 2310 | CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2311 | CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>(); |
| 2312 | CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>(); |
| 2313 | |
| 2314 | /* |
| 2315 | * Use one bswap instruction to reverse byte order first and then use 3 rounds of |
| 2316 | * swapping bits to reverse bits in a long number x. Using bswap to save instructions |
| 2317 | * compared to generic luni implementation which has 5 rounds of swapping bits. |
| 2318 | * x = bswap x |
| 2319 | * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555; |
| 2320 | * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333; |
| 2321 | * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F; |
| 2322 | */ |
| 2323 | __ bswapq(reg); |
| 2324 | SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler); |
| 2325 | SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler); |
| 2326 | SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler); |
| 2327 | } |
| 2328 | |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2329 | static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2330 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2331 | LocationSummary::kNoCall, |
| 2332 | kIntrinsified); |
| 2333 | locations->SetInAt(0, Location::Any()); |
| 2334 | locations->SetOut(Location::RequiresRegister()); |
| 2335 | } |
| 2336 | |
| 2337 | static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) { |
| 2338 | LocationSummary* locations = invoke->GetLocations(); |
| 2339 | Location src = locations->InAt(0); |
| 2340 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2341 | |
| 2342 | int zero_value_result = is_long ? 64 : 32; |
| 2343 | if (invoke->InputAt(0)->IsConstant()) { |
| 2344 | // Evaluate this at compile time. |
| 2345 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2346 | if (value == 0) { |
| 2347 | value = zero_value_result; |
| 2348 | } else { |
| 2349 | value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value)); |
| 2350 | } |
| 2351 | if (value == 0) { |
| 2352 | __ xorl(out, out); |
| 2353 | } else { |
| 2354 | __ movl(out, Immediate(value)); |
| 2355 | } |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | // Handle the non-constant cases. |
| 2360 | if (src.IsRegister()) { |
| 2361 | if (is_long) { |
| 2362 | __ bsrq(out, src.AsRegister<CpuRegister>()); |
| 2363 | } else { |
| 2364 | __ bsrl(out, src.AsRegister<CpuRegister>()); |
| 2365 | } |
| 2366 | } else if (is_long) { |
| 2367 | DCHECK(src.IsDoubleStackSlot()); |
| 2368 | __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2369 | } else { |
| 2370 | DCHECK(src.IsStackSlot()); |
| 2371 | __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2372 | } |
| 2373 | |
| 2374 | // 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] | 2375 | NearLabel is_zero, done; |
Mark Mendell | d589767 | 2015-08-12 21:16:41 -0400 | [diff] [blame] | 2376 | __ j(kEqual, &is_zero); |
| 2377 | |
| 2378 | // Correct the result from BSR to get the CLZ result. |
| 2379 | __ xorl(out, Immediate(zero_value_result - 1)); |
| 2380 | __ jmp(&done); |
| 2381 | |
| 2382 | // Fix the zero case with the expected result. |
| 2383 | __ Bind(&is_zero); |
| 2384 | __ movl(out, Immediate(zero_value_result)); |
| 2385 | |
| 2386 | __ Bind(&done); |
| 2387 | } |
| 2388 | |
| 2389 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 2390 | CreateLeadingZeroLocations(arena_, invoke); |
| 2391 | } |
| 2392 | |
| 2393 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 2394 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2395 | GenLeadingZeros(assembler, invoke, /* is_long */ false); |
| 2396 | } |
| 2397 | |
| 2398 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 2399 | CreateLeadingZeroLocations(arena_, invoke); |
| 2400 | } |
| 2401 | |
| 2402 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 2403 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2404 | GenLeadingZeros(assembler, invoke, /* is_long */ true); |
| 2405 | } |
| 2406 | |
Mark Mendell | 2d55479 | 2015-09-15 21:45:18 -0400 | [diff] [blame] | 2407 | static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2408 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2409 | LocationSummary::kNoCall, |
| 2410 | kIntrinsified); |
| 2411 | locations->SetInAt(0, Location::Any()); |
| 2412 | locations->SetOut(Location::RequiresRegister()); |
| 2413 | } |
| 2414 | |
| 2415 | static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) { |
| 2416 | LocationSummary* locations = invoke->GetLocations(); |
| 2417 | Location src = locations->InAt(0); |
| 2418 | CpuRegister out = locations->Out().AsRegister<CpuRegister>(); |
| 2419 | |
| 2420 | int zero_value_result = is_long ? 64 : 32; |
| 2421 | if (invoke->InputAt(0)->IsConstant()) { |
| 2422 | // Evaluate this at compile time. |
| 2423 | int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant()); |
| 2424 | if (value == 0) { |
| 2425 | value = zero_value_result; |
| 2426 | } else { |
| 2427 | value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value)); |
| 2428 | } |
| 2429 | if (value == 0) { |
| 2430 | __ xorl(out, out); |
| 2431 | } else { |
| 2432 | __ movl(out, Immediate(value)); |
| 2433 | } |
| 2434 | return; |
| 2435 | } |
| 2436 | |
| 2437 | // Handle the non-constant cases. |
| 2438 | if (src.IsRegister()) { |
| 2439 | if (is_long) { |
| 2440 | __ bsfq(out, src.AsRegister<CpuRegister>()); |
| 2441 | } else { |
| 2442 | __ bsfl(out, src.AsRegister<CpuRegister>()); |
| 2443 | } |
| 2444 | } else if (is_long) { |
| 2445 | DCHECK(src.IsDoubleStackSlot()); |
| 2446 | __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2447 | } else { |
| 2448 | DCHECK(src.IsStackSlot()); |
| 2449 | __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex())); |
| 2450 | } |
| 2451 | |
| 2452 | // BSF sets ZF if the input was zero, and the output is undefined. |
| 2453 | NearLabel done; |
| 2454 | __ j(kNotEqual, &done); |
| 2455 | |
| 2456 | // Fix the zero case with the expected result. |
| 2457 | __ movl(out, Immediate(zero_value_result)); |
| 2458 | |
| 2459 | __ Bind(&done); |
| 2460 | } |
| 2461 | |
| 2462 | void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 2463 | CreateTrailingZeroLocations(arena_, invoke); |
| 2464 | } |
| 2465 | |
| 2466 | void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 2467 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2468 | GenTrailingZeros(assembler, invoke, /* is_long */ false); |
| 2469 | } |
| 2470 | |
| 2471 | void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 2472 | CreateTrailingZeroLocations(arena_, invoke); |
| 2473 | } |
| 2474 | |
| 2475 | void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 2476 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2477 | GenTrailingZeros(assembler, invoke, /* is_long */ true); |
| 2478 | } |
| 2479 | |
| 2480 | static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 2481 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 2482 | LocationSummary::kNoCall, |
| 2483 | kIntrinsified); |
| 2484 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2485 | // The shift count needs to be in CL or a constant. |
| 2486 | locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1))); |
| 2487 | locations->SetOut(Location::SameAsFirstInput()); |
| 2488 | } |
| 2489 | |
| 2490 | static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) { |
| 2491 | LocationSummary* locations = invoke->GetLocations(); |
| 2492 | CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>(); |
| 2493 | Location second = locations->InAt(1); |
| 2494 | |
| 2495 | if (is_long) { |
| 2496 | if (second.IsRegister()) { |
| 2497 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 2498 | if (is_left) { |
| 2499 | __ rolq(first_reg, second_reg); |
| 2500 | } else { |
| 2501 | __ rorq(first_reg, second_reg); |
| 2502 | } |
| 2503 | } else { |
| 2504 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue); |
| 2505 | if (is_left) { |
| 2506 | __ rolq(first_reg, imm); |
| 2507 | } else { |
| 2508 | __ rorq(first_reg, imm); |
| 2509 | } |
| 2510 | } |
| 2511 | } else { |
| 2512 | if (second.IsRegister()) { |
| 2513 | CpuRegister second_reg = second.AsRegister<CpuRegister>(); |
| 2514 | if (is_left) { |
| 2515 | __ roll(first_reg, second_reg); |
| 2516 | } else { |
| 2517 | __ rorl(first_reg, second_reg); |
| 2518 | } |
| 2519 | } else { |
| 2520 | Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue); |
| 2521 | if (is_left) { |
| 2522 | __ roll(first_reg, imm); |
| 2523 | } else { |
| 2524 | __ rorl(first_reg, imm); |
| 2525 | } |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) { |
| 2531 | CreateRotateLocations(arena_, invoke); |
| 2532 | } |
| 2533 | |
| 2534 | void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) { |
| 2535 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2536 | GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true); |
| 2537 | } |
| 2538 | |
| 2539 | void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) { |
| 2540 | CreateRotateLocations(arena_, invoke); |
| 2541 | } |
| 2542 | |
| 2543 | void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) { |
| 2544 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2545 | GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false); |
| 2546 | } |
| 2547 | |
| 2548 | void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) { |
| 2549 | CreateRotateLocations(arena_, invoke); |
| 2550 | } |
| 2551 | |
| 2552 | void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) { |
| 2553 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2554 | GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true); |
| 2555 | } |
| 2556 | |
| 2557 | void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) { |
| 2558 | CreateRotateLocations(arena_, invoke); |
| 2559 | } |
| 2560 | |
| 2561 | void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) { |
| 2562 | X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler()); |
| 2563 | GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false); |
| 2564 | } |
| 2565 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2566 | // Unimplemented intrinsics. |
| 2567 | |
| 2568 | #define UNIMPLEMENTED_INTRINSIC(Name) \ |
| 2569 | void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 2570 | } \ |
| 2571 | void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 2572 | } |
| 2573 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2574 | UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent) |
| 2575 | |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 2576 | #undef UNIMPLEMENTED_INTRINSIC |
| 2577 | |
| 2578 | #undef __ |
| 2579 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2580 | } // namespace x86_64 |
| 2581 | } // namespace art |