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