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