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