Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [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_mips.h" |
| 18 | |
| 19 | #include "arch/mips/instruction_set_features_mips.h" |
| 20 | #include "art_method.h" |
| 21 | #include "code_generator_mips.h" |
| 22 | #include "entrypoints/quick/quick_entrypoints.h" |
| 23 | #include "intrinsics.h" |
| 24 | #include "mirror/array-inl.h" |
| 25 | #include "mirror/string.h" |
| 26 | #include "thread.h" |
| 27 | #include "utils/mips/assembler_mips.h" |
| 28 | #include "utils/mips/constants_mips.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace mips { |
| 33 | |
| 34 | IntrinsicLocationsBuilderMIPS::IntrinsicLocationsBuilderMIPS(CodeGeneratorMIPS* codegen) |
| 35 | : arena_(codegen->GetGraph()->GetArena()) { |
| 36 | } |
| 37 | |
| 38 | MipsAssembler* IntrinsicCodeGeneratorMIPS::GetAssembler() { |
| 39 | return reinterpret_cast<MipsAssembler*>(codegen_->GetAssembler()); |
| 40 | } |
| 41 | |
| 42 | ArenaAllocator* IntrinsicCodeGeneratorMIPS::GetAllocator() { |
| 43 | return codegen_->GetGraph()->GetArena(); |
| 44 | } |
| 45 | |
Alexey Frunze | bb9863a | 2016-01-11 15:51:16 -0800 | [diff] [blame] | 46 | inline bool IntrinsicCodeGeneratorMIPS::IsR2OrNewer() const { |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 47 | return codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2(); |
| 48 | } |
| 49 | |
Alexey Frunze | bb9863a | 2016-01-11 15:51:16 -0800 | [diff] [blame] | 50 | inline bool IntrinsicCodeGeneratorMIPS::IsR6() const { |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 51 | return codegen_->GetInstructionSetFeatures().IsR6(); |
| 52 | } |
| 53 | |
Alexey Frunze | bb9863a | 2016-01-11 15:51:16 -0800 | [diff] [blame] | 54 | inline bool IntrinsicCodeGeneratorMIPS::Is32BitFPU() const { |
| 55 | return codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint(); |
| 56 | } |
| 57 | |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 58 | #define __ codegen->GetAssembler()-> |
| 59 | |
| 60 | static void MoveFromReturnRegister(Location trg, |
| 61 | Primitive::Type type, |
| 62 | CodeGeneratorMIPS* codegen) { |
| 63 | if (!trg.IsValid()) { |
| 64 | DCHECK_EQ(type, Primitive::kPrimVoid); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | DCHECK_NE(type, Primitive::kPrimVoid); |
| 69 | |
| 70 | if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) { |
| 71 | Register trg_reg = trg.AsRegister<Register>(); |
| 72 | if (trg_reg != V0) { |
| 73 | __ Move(V0, trg_reg); |
| 74 | } |
| 75 | } else { |
| 76 | FRegister trg_reg = trg.AsFpuRegister<FRegister>(); |
| 77 | if (trg_reg != F0) { |
| 78 | if (type == Primitive::kPrimFloat) { |
| 79 | __ MovS(F0, trg_reg); |
| 80 | } else { |
| 81 | __ MovD(F0, trg_reg); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS* codegen) { |
| 88 | InvokeDexCallingConventionVisitorMIPS calling_convention_visitor; |
| 89 | IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor); |
| 90 | } |
| 91 | |
| 92 | // Slow-path for fallback (calling the managed code to handle the |
| 93 | // intrinsic) in an intrinsified call. This will copy the arguments |
| 94 | // into the positions for a regular call. |
| 95 | // |
| 96 | // Note: The actual parameters are required to be in the locations |
| 97 | // given by the invoke's location summary. If an intrinsic |
| 98 | // modifies those locations before a slowpath call, they must be |
| 99 | // restored! |
| 100 | class IntrinsicSlowPathMIPS : public SlowPathCodeMIPS { |
| 101 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 102 | explicit IntrinsicSlowPathMIPS(HInvoke* invoke) : SlowPathCodeMIPS(invoke), invoke_(invoke) { } |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 103 | |
| 104 | void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE { |
| 105 | CodeGeneratorMIPS* codegen = down_cast<CodeGeneratorMIPS*>(codegen_in); |
| 106 | |
| 107 | __ Bind(GetEntryLabel()); |
| 108 | |
| 109 | SaveLiveRegisters(codegen, invoke_->GetLocations()); |
| 110 | |
| 111 | MoveArguments(invoke_, codegen); |
| 112 | |
| 113 | if (invoke_->IsInvokeStaticOrDirect()) { |
| 114 | codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), |
| 115 | Location::RegisterLocation(A0)); |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 116 | } else { |
Chris Larsen | 3acee73 | 2015-11-18 13:31:08 -0800 | [diff] [blame] | 117 | codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0)); |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 118 | } |
Chris Larsen | 3acee73 | 2015-11-18 13:31:08 -0800 | [diff] [blame] | 119 | codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this); |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 120 | |
| 121 | // Copy the result back to the expected output. |
| 122 | Location out = invoke_->GetLocations()->Out(); |
| 123 | if (out.IsValid()) { |
| 124 | DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory. |
| 125 | DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg())); |
| 126 | MoveFromReturnRegister(out, invoke_->GetType(), codegen); |
| 127 | } |
| 128 | |
| 129 | RestoreLiveRegisters(codegen, invoke_->GetLocations()); |
| 130 | __ B(GetExitLabel()); |
| 131 | } |
| 132 | |
| 133 | const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS"; } |
| 134 | |
| 135 | private: |
| 136 | // The instruction where this slow path is happening. |
| 137 | HInvoke* const invoke_; |
| 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS); |
| 140 | }; |
| 141 | |
| 142 | #undef __ |
| 143 | |
| 144 | bool IntrinsicLocationsBuilderMIPS::TryDispatch(HInvoke* invoke) { |
| 145 | Dispatch(invoke); |
| 146 | LocationSummary* res = invoke->GetLocations(); |
| 147 | return res != nullptr && res->Intrinsified(); |
| 148 | } |
| 149 | |
| 150 | #define __ assembler-> |
| 151 | |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 152 | static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 153 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 154 | LocationSummary::kNoCall, |
| 155 | kIntrinsified); |
| 156 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 157 | locations->SetOut(Location::RequiresRegister()); |
| 158 | } |
| 159 | |
| 160 | static void MoveFPToInt(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) { |
| 161 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 162 | |
| 163 | if (is64bit) { |
| 164 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 165 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 166 | |
| 167 | __ Mfc1(out_lo, in); |
Alexey Frunze | bb9863a | 2016-01-11 15:51:16 -0800 | [diff] [blame] | 168 | __ MoveFromFpuHigh(out_hi, in); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 169 | } else { |
| 170 | Register out = locations->Out().AsRegister<Register>(); |
| 171 | |
| 172 | __ Mfc1(out, in); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // long java.lang.Double.doubleToRawLongBits(double) |
| 177 | void IntrinsicLocationsBuilderMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
| 178 | CreateFPToIntLocations(arena_, invoke); |
| 179 | } |
| 180 | |
| 181 | void IntrinsicCodeGeneratorMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 182 | MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // int java.lang.Float.floatToRawIntBits(float) |
| 186 | void IntrinsicLocationsBuilderMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
| 187 | CreateFPToIntLocations(arena_, invoke); |
| 188 | } |
| 189 | |
| 190 | void IntrinsicCodeGeneratorMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 191 | MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 195 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 196 | LocationSummary::kNoCall, |
| 197 | kIntrinsified); |
| 198 | locations->SetInAt(0, Location::RequiresRegister()); |
| 199 | locations->SetOut(Location::RequiresFpuRegister()); |
| 200 | } |
| 201 | |
| 202 | static void MoveIntToFP(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) { |
| 203 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 204 | |
| 205 | if (is64bit) { |
| 206 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 207 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 208 | |
| 209 | __ Mtc1(in_lo, out); |
Alexey Frunze | bb9863a | 2016-01-11 15:51:16 -0800 | [diff] [blame] | 210 | __ MoveToFpuHigh(in_hi, out); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 211 | } else { |
| 212 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 213 | |
| 214 | __ Mtc1(in, out); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // double java.lang.Double.longBitsToDouble(long) |
| 219 | void IntrinsicLocationsBuilderMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
| 220 | CreateIntToFPLocations(arena_, invoke); |
| 221 | } |
| 222 | |
| 223 | void IntrinsicCodeGeneratorMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 224 | MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // float java.lang.Float.intBitsToFloat(int) |
| 228 | void IntrinsicLocationsBuilderMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
| 229 | CreateIntToFPLocations(arena_, invoke); |
| 230 | } |
| 231 | |
| 232 | void IntrinsicCodeGeneratorMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) { |
Roland Levillain | bf84a3d | 2015-12-04 14:33:02 +0000 | [diff] [blame] | 233 | MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 236 | static void CreateIntToIntLocations(ArenaAllocator* arena, |
| 237 | HInvoke* invoke, |
| 238 | Location::OutputOverlap overlaps = Location::kNoOutputOverlap) { |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 239 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 240 | LocationSummary::kNoCall, |
| 241 | kIntrinsified); |
| 242 | locations->SetInAt(0, Location::RequiresRegister()); |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 243 | locations->SetOut(Location::RequiresRegister(), overlaps); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 246 | static void GenReverse(LocationSummary* locations, |
| 247 | Primitive::Type type, |
| 248 | bool isR2OrNewer, |
| 249 | bool isR6, |
| 250 | bool reverseBits, |
| 251 | MipsAssembler* assembler) { |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 252 | DCHECK(type == Primitive::kPrimShort || |
| 253 | type == Primitive::kPrimInt || |
| 254 | type == Primitive::kPrimLong); |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 255 | DCHECK(type != Primitive::kPrimShort || !reverseBits); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 256 | |
| 257 | if (type == Primitive::kPrimShort) { |
| 258 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 259 | Register out = locations->Out().AsRegister<Register>(); |
| 260 | |
| 261 | if (isR2OrNewer) { |
| 262 | __ Wsbh(out, in); |
| 263 | __ Seh(out, out); |
| 264 | } else { |
| 265 | __ Sll(TMP, in, 24); |
| 266 | __ Sra(TMP, TMP, 16); |
| 267 | __ Sll(out, in, 16); |
| 268 | __ Srl(out, out, 24); |
| 269 | __ Or(out, out, TMP); |
| 270 | } |
| 271 | } else if (type == Primitive::kPrimInt) { |
| 272 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 273 | Register out = locations->Out().AsRegister<Register>(); |
| 274 | |
| 275 | if (isR2OrNewer) { |
| 276 | __ Rotr(out, in, 16); |
| 277 | __ Wsbh(out, out); |
| 278 | } else { |
| 279 | // MIPS32r1 |
| 280 | // __ Rotr(out, in, 16); |
| 281 | __ Sll(TMP, in, 16); |
| 282 | __ Srl(out, in, 16); |
| 283 | __ Or(out, out, TMP); |
| 284 | // __ Wsbh(out, out); |
| 285 | __ LoadConst32(AT, 0x00FF00FF); |
| 286 | __ And(TMP, out, AT); |
| 287 | __ Sll(TMP, TMP, 8); |
| 288 | __ Srl(out, out, 8); |
| 289 | __ And(out, out, AT); |
| 290 | __ Or(out, out, TMP); |
| 291 | } |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 292 | if (reverseBits) { |
| 293 | if (isR6) { |
| 294 | __ Bitswap(out, out); |
| 295 | } else { |
| 296 | __ LoadConst32(AT, 0x0F0F0F0F); |
| 297 | __ And(TMP, out, AT); |
| 298 | __ Sll(TMP, TMP, 4); |
| 299 | __ Srl(out, out, 4); |
| 300 | __ And(out, out, AT); |
| 301 | __ Or(out, TMP, out); |
| 302 | __ LoadConst32(AT, 0x33333333); |
| 303 | __ And(TMP, out, AT); |
| 304 | __ Sll(TMP, TMP, 2); |
| 305 | __ Srl(out, out, 2); |
| 306 | __ And(out, out, AT); |
| 307 | __ Or(out, TMP, out); |
| 308 | __ LoadConst32(AT, 0x55555555); |
| 309 | __ And(TMP, out, AT); |
| 310 | __ Sll(TMP, TMP, 1); |
| 311 | __ Srl(out, out, 1); |
| 312 | __ And(out, out, AT); |
| 313 | __ Or(out, TMP, out); |
| 314 | } |
| 315 | } |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 316 | } else if (type == Primitive::kPrimLong) { |
| 317 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 318 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 319 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 320 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 321 | |
| 322 | if (isR2OrNewer) { |
| 323 | __ Rotr(AT, in_hi, 16); |
| 324 | __ Rotr(TMP, in_lo, 16); |
| 325 | __ Wsbh(out_lo, AT); |
| 326 | __ Wsbh(out_hi, TMP); |
| 327 | } else { |
| 328 | // When calling CreateIntToIntLocations() we promised that the |
| 329 | // use of the out_lo/out_hi wouldn't overlap with the use of |
| 330 | // in_lo/in_hi. Be very careful not to write to out_lo/out_hi |
| 331 | // until we're completely done reading from in_lo/in_hi. |
| 332 | // __ Rotr(TMP, in_lo, 16); |
| 333 | __ Sll(TMP, in_lo, 16); |
| 334 | __ Srl(AT, in_lo, 16); |
| 335 | __ Or(TMP, TMP, AT); // Hold in TMP until it's safe |
| 336 | // to write to out_hi. |
| 337 | // __ Rotr(out_lo, in_hi, 16); |
| 338 | __ Sll(AT, in_hi, 16); |
| 339 | __ Srl(out_lo, in_hi, 16); // Here we are finally done reading |
| 340 | // from in_lo/in_hi so it's okay to |
| 341 | // write to out_lo/out_hi. |
| 342 | __ Or(out_lo, out_lo, AT); |
| 343 | // __ Wsbh(out_hi, out_hi); |
| 344 | __ LoadConst32(AT, 0x00FF00FF); |
| 345 | __ And(out_hi, TMP, AT); |
| 346 | __ Sll(out_hi, out_hi, 8); |
| 347 | __ Srl(TMP, TMP, 8); |
| 348 | __ And(TMP, TMP, AT); |
| 349 | __ Or(out_hi, out_hi, TMP); |
| 350 | // __ Wsbh(out_lo, out_lo); |
| 351 | __ And(TMP, out_lo, AT); // AT already holds the correct mask value |
| 352 | __ Sll(TMP, TMP, 8); |
| 353 | __ Srl(out_lo, out_lo, 8); |
| 354 | __ And(out_lo, out_lo, AT); |
| 355 | __ Or(out_lo, out_lo, TMP); |
| 356 | } |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 357 | if (reverseBits) { |
| 358 | if (isR6) { |
| 359 | __ Bitswap(out_hi, out_hi); |
| 360 | __ Bitswap(out_lo, out_lo); |
| 361 | } else { |
| 362 | __ LoadConst32(AT, 0x0F0F0F0F); |
| 363 | __ And(TMP, out_hi, AT); |
| 364 | __ Sll(TMP, TMP, 4); |
| 365 | __ Srl(out_hi, out_hi, 4); |
| 366 | __ And(out_hi, out_hi, AT); |
| 367 | __ Or(out_hi, TMP, out_hi); |
| 368 | __ And(TMP, out_lo, AT); |
| 369 | __ Sll(TMP, TMP, 4); |
| 370 | __ Srl(out_lo, out_lo, 4); |
| 371 | __ And(out_lo, out_lo, AT); |
| 372 | __ Or(out_lo, TMP, out_lo); |
| 373 | __ LoadConst32(AT, 0x33333333); |
| 374 | __ And(TMP, out_hi, AT); |
| 375 | __ Sll(TMP, TMP, 2); |
| 376 | __ Srl(out_hi, out_hi, 2); |
| 377 | __ And(out_hi, out_hi, AT); |
| 378 | __ Or(out_hi, TMP, out_hi); |
| 379 | __ And(TMP, out_lo, AT); |
| 380 | __ Sll(TMP, TMP, 2); |
| 381 | __ Srl(out_lo, out_lo, 2); |
| 382 | __ And(out_lo, out_lo, AT); |
| 383 | __ Or(out_lo, TMP, out_lo); |
| 384 | __ LoadConst32(AT, 0x55555555); |
| 385 | __ And(TMP, out_hi, AT); |
| 386 | __ Sll(TMP, TMP, 1); |
| 387 | __ Srl(out_hi, out_hi, 1); |
| 388 | __ And(out_hi, out_hi, AT); |
| 389 | __ Or(out_hi, TMP, out_hi); |
| 390 | __ And(TMP, out_lo, AT); |
| 391 | __ Sll(TMP, TMP, 1); |
| 392 | __ Srl(out_lo, out_lo, 1); |
| 393 | __ And(out_lo, out_lo, AT); |
| 394 | __ Or(out_lo, TMP, out_lo); |
| 395 | } |
| 396 | } |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | // int java.lang.Integer.reverseBytes(int) |
| 401 | void IntrinsicLocationsBuilderMIPS::VisitIntegerReverseBytes(HInvoke* invoke) { |
| 402 | CreateIntToIntLocations(arena_, invoke); |
| 403 | } |
| 404 | |
| 405 | void IntrinsicCodeGeneratorMIPS::VisitIntegerReverseBytes(HInvoke* invoke) { |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 406 | GenReverse(invoke->GetLocations(), |
| 407 | Primitive::kPrimInt, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 408 | IsR2OrNewer(), |
| 409 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 410 | /* reverseBits */ false, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 411 | GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // long java.lang.Long.reverseBytes(long) |
| 415 | void IntrinsicLocationsBuilderMIPS::VisitLongReverseBytes(HInvoke* invoke) { |
| 416 | CreateIntToIntLocations(arena_, invoke); |
| 417 | } |
| 418 | |
| 419 | void IntrinsicCodeGeneratorMIPS::VisitLongReverseBytes(HInvoke* invoke) { |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 420 | GenReverse(invoke->GetLocations(), |
| 421 | Primitive::kPrimLong, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 422 | IsR2OrNewer(), |
| 423 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 424 | /* reverseBits */ false, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 425 | GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // short java.lang.Short.reverseBytes(short) |
| 429 | void IntrinsicLocationsBuilderMIPS::VisitShortReverseBytes(HInvoke* invoke) { |
| 430 | CreateIntToIntLocations(arena_, invoke); |
| 431 | } |
| 432 | |
| 433 | void IntrinsicCodeGeneratorMIPS::VisitShortReverseBytes(HInvoke* invoke) { |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 434 | GenReverse(invoke->GetLocations(), |
| 435 | Primitive::kPrimShort, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 436 | IsR2OrNewer(), |
| 437 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 438 | /* reverseBits */ false, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 439 | GetAssembler()); |
| 440 | } |
| 441 | |
Chris Larsen | e384547 | 2015-11-18 12:27:15 -0800 | [diff] [blame] | 442 | static void GenNumberOfLeadingZeroes(LocationSummary* locations, |
| 443 | bool is64bit, |
| 444 | bool isR6, |
| 445 | MipsAssembler* assembler) { |
| 446 | Register out = locations->Out().AsRegister<Register>(); |
| 447 | if (is64bit) { |
| 448 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 449 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 450 | |
| 451 | if (isR6) { |
| 452 | __ ClzR6(AT, in_hi); |
| 453 | __ ClzR6(TMP, in_lo); |
| 454 | __ Seleqz(TMP, TMP, in_hi); |
| 455 | } else { |
| 456 | __ ClzR2(AT, in_hi); |
| 457 | __ ClzR2(TMP, in_lo); |
| 458 | __ Movn(TMP, ZERO, in_hi); |
| 459 | } |
| 460 | __ Addu(out, AT, TMP); |
| 461 | } else { |
| 462 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 463 | |
| 464 | if (isR6) { |
| 465 | __ ClzR6(out, in); |
| 466 | } else { |
| 467 | __ ClzR2(out, in); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // int java.lang.Integer.numberOfLeadingZeros(int i) |
| 473 | void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
| 474 | CreateIntToIntLocations(arena_, invoke); |
| 475 | } |
| 476 | |
| 477 | void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) { |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 478 | GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ false, IsR6(), GetAssembler()); |
Chris Larsen | e384547 | 2015-11-18 12:27:15 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // int java.lang.Long.numberOfLeadingZeros(long i) |
| 482 | void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
| 483 | CreateIntToIntLocations(arena_, invoke); |
| 484 | } |
| 485 | |
| 486 | void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) { |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 487 | GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ true, IsR6(), GetAssembler()); |
Chris Larsen | e384547 | 2015-11-18 12:27:15 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 490 | static void GenNumberOfTrailingZeroes(LocationSummary* locations, |
| 491 | bool is64bit, |
| 492 | bool isR6, |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 493 | MipsAssembler* assembler) { |
| 494 | Register out = locations->Out().AsRegister<Register>(); |
| 495 | Register in_lo; |
| 496 | Register in; |
| 497 | |
| 498 | if (is64bit) { |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 499 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 500 | |
| 501 | in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 502 | |
| 503 | // If in_lo is zero then count the number of trailing zeroes in in_hi; |
| 504 | // otherwise count the number of trailing zeroes in in_lo. |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 505 | // out = in_lo ? in_lo : in_hi; |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 506 | if (isR6) { |
| 507 | __ Seleqz(out, in_hi, in_lo); |
| 508 | __ Selnez(TMP, in_lo, in_lo); |
| 509 | __ Or(out, out, TMP); |
| 510 | } else { |
| 511 | __ Movz(out, in_hi, in_lo); |
| 512 | __ Movn(out, in_lo, in_lo); |
| 513 | } |
| 514 | |
| 515 | in = out; |
| 516 | } else { |
| 517 | in = locations->InAt(0).AsRegister<Register>(); |
| 518 | // Give in_lo a dummy value to keep the compiler from complaining. |
| 519 | // Since we only get here in the 32-bit case, this value will never |
| 520 | // be used. |
| 521 | in_lo = in; |
| 522 | } |
| 523 | |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 524 | if (isR6) { |
| 525 | // We don't have an instruction to count the number of trailing zeroes. |
| 526 | // Start by flipping the bits end-for-end so we can count the number of |
| 527 | // leading zeroes instead. |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 528 | __ Rotr(out, in, 16); |
| 529 | __ Wsbh(out, out); |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 530 | __ Bitswap(out, out); |
| 531 | __ ClzR6(out, out); |
| 532 | } else { |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 533 | // Convert trailing zeroes to trailing ones, and bits to their left |
| 534 | // to zeroes. |
| 535 | __ Addiu(TMP, in, -1); |
| 536 | __ Xor(out, TMP, in); |
| 537 | __ And(out, out, TMP); |
| 538 | // Count number of leading zeroes. |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 539 | __ ClzR2(out, out); |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 540 | // Subtract number of leading zeroes from 32 to get number of trailing ones. |
| 541 | // Remember that the trailing ones were formerly trailing zeroes. |
| 542 | __ LoadConst32(TMP, 32); |
| 543 | __ Subu(out, TMP, out); |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | if (is64bit) { |
| 547 | // If in_lo is zero, then we counted the number of trailing zeroes in in_hi so we must add the |
| 548 | // number of trailing zeroes in in_lo (32) to get the correct final count |
| 549 | __ LoadConst32(TMP, 32); |
| 550 | if (isR6) { |
| 551 | __ Seleqz(TMP, TMP, in_lo); |
| 552 | } else { |
| 553 | __ Movn(TMP, ZERO, in_lo); |
| 554 | } |
| 555 | __ Addu(out, out, TMP); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // int java.lang.Integer.numberOfTrailingZeros(int i) |
| 560 | void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 561 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 562 | } |
| 563 | |
| 564 | void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 565 | GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ false, IsR6(), GetAssembler()); |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // int java.lang.Long.numberOfTrailingZeros(long i) |
| 569 | void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 570 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 571 | } |
| 572 | |
| 573 | void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
Chris Larsen | bbb2ebe | 2016-02-17 17:44:58 -0800 | [diff] [blame] | 574 | GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ true, IsR6(), GetAssembler()); |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 577 | // int java.lang.Integer.reverse(int) |
| 578 | void IntrinsicLocationsBuilderMIPS::VisitIntegerReverse(HInvoke* invoke) { |
| 579 | CreateIntToIntLocations(arena_, invoke); |
| 580 | } |
| 581 | |
| 582 | void IntrinsicCodeGeneratorMIPS::VisitIntegerReverse(HInvoke* invoke) { |
| 583 | GenReverse(invoke->GetLocations(), |
| 584 | Primitive::kPrimInt, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 585 | IsR2OrNewer(), |
| 586 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 587 | /* reverseBits */ true, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 588 | GetAssembler()); |
| 589 | } |
| 590 | |
| 591 | // long java.lang.Long.reverse(long) |
| 592 | void IntrinsicLocationsBuilderMIPS::VisitLongReverse(HInvoke* invoke) { |
| 593 | CreateIntToIntLocations(arena_, invoke); |
| 594 | } |
| 595 | |
| 596 | void IntrinsicCodeGeneratorMIPS::VisitLongReverse(HInvoke* invoke) { |
| 597 | GenReverse(invoke->GetLocations(), |
| 598 | Primitive::kPrimLong, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 599 | IsR2OrNewer(), |
| 600 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 601 | /* reverseBits */ true, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 602 | GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 605 | static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 606 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 607 | LocationSummary::kNoCall, |
| 608 | kIntrinsified); |
| 609 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 610 | locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); |
| 611 | } |
| 612 | |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 613 | static void GenBitCount(LocationSummary* locations, |
| 614 | Primitive::Type type, |
| 615 | bool isR6, |
| 616 | MipsAssembler* assembler) { |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 617 | Register out = locations->Out().AsRegister<Register>(); |
| 618 | |
| 619 | // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel |
| 620 | // |
| 621 | // A generalization of the best bit counting method to integers of |
| 622 | // bit-widths up to 128 (parameterized by type T) is this: |
| 623 | // |
| 624 | // v = v - ((v >> 1) & (T)~(T)0/3); // temp |
| 625 | // v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp |
| 626 | // v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp |
| 627 | // c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * BITS_PER_BYTE; // count |
| 628 | // |
| 629 | // For comparison, for 32-bit quantities, this algorithm can be executed |
| 630 | // using 20 MIPS instructions (the calls to LoadConst32() generate two |
| 631 | // machine instructions each for the values being used in this algorithm). |
| 632 | // A(n unrolled) loop-based algorithm required 25 instructions. |
| 633 | // |
| 634 | // For 64-bit quantities, this algorithm gets executed twice, (once |
| 635 | // for in_lo, and again for in_hi), but saves a few instructions |
| 636 | // because the mask values only have to be loaded once. Using this |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 637 | // algorithm the count for a 64-bit operand can be performed in 29 |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 638 | // instructions compared to a loop-based algorithm which required 47 |
| 639 | // instructions. |
| 640 | |
| 641 | if (type == Primitive::kPrimInt) { |
| 642 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 643 | |
| 644 | __ Srl(TMP, in, 1); |
| 645 | __ LoadConst32(AT, 0x55555555); |
| 646 | __ And(TMP, TMP, AT); |
| 647 | __ Subu(TMP, in, TMP); |
| 648 | __ LoadConst32(AT, 0x33333333); |
| 649 | __ And(out, TMP, AT); |
| 650 | __ Srl(TMP, TMP, 2); |
| 651 | __ And(TMP, TMP, AT); |
| 652 | __ Addu(TMP, out, TMP); |
| 653 | __ Srl(out, TMP, 4); |
| 654 | __ Addu(out, out, TMP); |
| 655 | __ LoadConst32(AT, 0x0F0F0F0F); |
| 656 | __ And(out, out, AT); |
| 657 | __ LoadConst32(TMP, 0x01010101); |
| 658 | if (isR6) { |
| 659 | __ MulR6(out, out, TMP); |
| 660 | } else { |
| 661 | __ MulR2(out, out, TMP); |
| 662 | } |
| 663 | __ Srl(out, out, 24); |
Roland Levillain | fa3912e | 2016-04-01 18:21:55 +0100 | [diff] [blame] | 664 | } else { |
| 665 | DCHECK_EQ(type, Primitive::kPrimLong); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 666 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 667 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 668 | Register tmp_hi = locations->GetTemp(0).AsRegister<Register>(); |
| 669 | Register out_hi = locations->GetTemp(1).AsRegister<Register>(); |
| 670 | Register tmp_lo = TMP; |
| 671 | Register out_lo = out; |
| 672 | |
| 673 | __ Srl(tmp_lo, in_lo, 1); |
| 674 | __ Srl(tmp_hi, in_hi, 1); |
| 675 | |
| 676 | __ LoadConst32(AT, 0x55555555); |
| 677 | |
| 678 | __ And(tmp_lo, tmp_lo, AT); |
| 679 | __ Subu(tmp_lo, in_lo, tmp_lo); |
| 680 | |
| 681 | __ And(tmp_hi, tmp_hi, AT); |
| 682 | __ Subu(tmp_hi, in_hi, tmp_hi); |
| 683 | |
| 684 | __ LoadConst32(AT, 0x33333333); |
| 685 | |
| 686 | __ And(out_lo, tmp_lo, AT); |
| 687 | __ Srl(tmp_lo, tmp_lo, 2); |
| 688 | __ And(tmp_lo, tmp_lo, AT); |
| 689 | __ Addu(tmp_lo, out_lo, tmp_lo); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 690 | |
| 691 | __ And(out_hi, tmp_hi, AT); |
| 692 | __ Srl(tmp_hi, tmp_hi, 2); |
| 693 | __ And(tmp_hi, tmp_hi, AT); |
| 694 | __ Addu(tmp_hi, out_hi, tmp_hi); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 695 | |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 696 | // Here we deviate from the original algorithm a bit. We've reached |
| 697 | // the stage where the bitfields holding the subtotals are large |
| 698 | // enough to hold the combined subtotals for both the low word, and |
| 699 | // the high word. This means that we can add the subtotals for the |
| 700 | // the high, and low words into a single word, and compute the final |
| 701 | // result for both the high, and low words using fewer instructions. |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 702 | __ LoadConst32(AT, 0x0F0F0F0F); |
| 703 | |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 704 | __ Addu(TMP, tmp_hi, tmp_lo); |
| 705 | |
| 706 | __ Srl(out, TMP, 4); |
| 707 | __ And(out, out, AT); |
| 708 | __ And(TMP, TMP, AT); |
| 709 | __ Addu(out, out, TMP); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 710 | |
| 711 | __ LoadConst32(AT, 0x01010101); |
| 712 | |
| 713 | if (isR6) { |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 714 | __ MulR6(out, out, AT); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 715 | } else { |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 716 | __ MulR2(out, out, AT); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Chris Larsen | 8ca4f97 | 2016-04-14 16:16:29 -0700 | [diff] [blame] | 719 | __ Srl(out, out, 24); |
Chris Larsen | edc1645 | 2016-02-12 17:59:00 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | // int java.lang.Integer.bitCount(int) |
| 724 | void IntrinsicLocationsBuilderMIPS::VisitIntegerBitCount(HInvoke* invoke) { |
| 725 | CreateIntToIntLocations(arena_, invoke); |
| 726 | } |
| 727 | |
| 728 | void IntrinsicCodeGeneratorMIPS::VisitIntegerBitCount(HInvoke* invoke) { |
| 729 | GenBitCount(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler()); |
| 730 | } |
| 731 | |
| 732 | // int java.lang.Long.bitCount(int) |
| 733 | void IntrinsicLocationsBuilderMIPS::VisitLongBitCount(HInvoke* invoke) { |
| 734 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 735 | LocationSummary::kNoCall, |
| 736 | kIntrinsified); |
| 737 | locations->SetInAt(0, Location::RequiresRegister()); |
| 738 | locations->SetOut(Location::RequiresRegister()); |
| 739 | locations->AddTemp(Location::RequiresRegister()); |
| 740 | locations->AddTemp(Location::RequiresRegister()); |
| 741 | } |
| 742 | |
| 743 | void IntrinsicCodeGeneratorMIPS::VisitLongBitCount(HInvoke* invoke) { |
| 744 | GenBitCount(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler()); |
| 745 | } |
| 746 | |
Goran Jakovljevic | b668465 | 2017-01-11 13:42:38 +0100 | [diff] [blame] | 747 | static void MathAbsFP(LocationSummary* locations, |
| 748 | bool is64bit, |
| 749 | bool isR2OrNewer, |
| 750 | bool isR6, |
| 751 | MipsAssembler* assembler) { |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 752 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 753 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 754 | |
Goran Jakovljevic | b668465 | 2017-01-11 13:42:38 +0100 | [diff] [blame] | 755 | // As a "quality of implementation", rather than pure "spec compliance", it is required that |
| 756 | // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN. |
| 757 | // |
| 758 | // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case, |
| 759 | // both regular floating point numbers and NAN values are treated alike, only the sign bit is |
| 760 | // affected by this instruction. |
| 761 | // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any |
| 762 | // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be |
| 763 | // changed when doing abs(NaN). Because of that, we clear sign bit in a different way. |
| 764 | if (isR6) { |
| 765 | if (is64bit) { |
| 766 | __ AbsD(out, in); |
| 767 | } else { |
| 768 | __ AbsS(out, in); |
| 769 | } |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 770 | } else { |
Goran Jakovljevic | b668465 | 2017-01-11 13:42:38 +0100 | [diff] [blame] | 771 | if (is64bit) { |
| 772 | if (in != out) { |
| 773 | __ MovD(out, in); |
| 774 | } |
| 775 | __ MoveFromFpuHigh(TMP, in); |
| 776 | // ins instruction is not available for R1. |
| 777 | if (isR2OrNewer) { |
| 778 | __ Ins(TMP, ZERO, 31, 1); |
| 779 | } else { |
| 780 | __ Sll(TMP, TMP, 1); |
| 781 | __ Srl(TMP, TMP, 1); |
| 782 | } |
| 783 | __ MoveToFpuHigh(TMP, out); |
| 784 | } else { |
| 785 | __ Mfc1(TMP, in); |
| 786 | // ins instruction is not available for R1. |
| 787 | if (isR2OrNewer) { |
| 788 | __ Ins(TMP, ZERO, 31, 1); |
| 789 | } else { |
| 790 | __ Sll(TMP, TMP, 1); |
| 791 | __ Srl(TMP, TMP, 1); |
| 792 | } |
| 793 | __ Mtc1(TMP, out); |
| 794 | } |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | // double java.lang.Math.abs(double) |
| 799 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsDouble(HInvoke* invoke) { |
| 800 | CreateFPToFPLocations(arena_, invoke); |
| 801 | } |
| 802 | |
| 803 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsDouble(HInvoke* invoke) { |
Goran Jakovljevic | b668465 | 2017-01-11 13:42:38 +0100 | [diff] [blame] | 804 | MathAbsFP(invoke->GetLocations(), /* is64bit */ true, IsR2OrNewer(), IsR6(), GetAssembler()); |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // float java.lang.Math.abs(float) |
| 808 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsFloat(HInvoke* invoke) { |
| 809 | CreateFPToFPLocations(arena_, invoke); |
| 810 | } |
| 811 | |
| 812 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsFloat(HInvoke* invoke) { |
Goran Jakovljevic | b668465 | 2017-01-11 13:42:38 +0100 | [diff] [blame] | 813 | MathAbsFP(invoke->GetLocations(), /* is64bit */ false, IsR2OrNewer(), IsR6(), GetAssembler()); |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void GenAbsInteger(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) { |
| 817 | if (is64bit) { |
| 818 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 819 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 820 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 821 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 822 | |
| 823 | // The comments in this section show the analogous operations which would |
| 824 | // be performed if we had 64-bit registers "in", and "out". |
| 825 | // __ Dsra32(AT, in, 31); |
| 826 | __ Sra(AT, in_hi, 31); |
| 827 | // __ Xor(out, in, AT); |
| 828 | __ Xor(TMP, in_lo, AT); |
| 829 | __ Xor(out_hi, in_hi, AT); |
| 830 | // __ Dsubu(out, out, AT); |
| 831 | __ Subu(out_lo, TMP, AT); |
| 832 | __ Sltu(TMP, out_lo, TMP); |
| 833 | __ Addu(out_hi, out_hi, TMP); |
| 834 | } else { |
| 835 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 836 | Register out = locations->Out().AsRegister<Register>(); |
| 837 | |
| 838 | __ Sra(AT, in, 31); |
| 839 | __ Xor(out, in, AT); |
| 840 | __ Subu(out, out, AT); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | // int java.lang.Math.abs(int) |
| 845 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsInt(HInvoke* invoke) { |
| 846 | CreateIntToIntLocations(arena_, invoke); |
| 847 | } |
| 848 | |
| 849 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsInt(HInvoke* invoke) { |
| 850 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
| 851 | } |
| 852 | |
| 853 | // long java.lang.Math.abs(long) |
| 854 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsLong(HInvoke* invoke) { |
| 855 | CreateIntToIntLocations(arena_, invoke); |
| 856 | } |
| 857 | |
| 858 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsLong(HInvoke* invoke) { |
| 859 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
| 860 | } |
| 861 | |
| 862 | static void GenMinMaxFP(LocationSummary* locations, |
| 863 | bool is_min, |
| 864 | Primitive::Type type, |
| 865 | bool is_R6, |
| 866 | MipsAssembler* assembler) { |
| 867 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 868 | FRegister a = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 869 | FRegister b = locations->InAt(1).AsFpuRegister<FRegister>(); |
| 870 | |
| 871 | if (is_R6) { |
| 872 | MipsLabel noNaNs; |
| 873 | MipsLabel done; |
| 874 | FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP; |
| 875 | |
| 876 | // When Java computes min/max it prefers a NaN to a number; the |
| 877 | // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of |
| 878 | // the inputs is a NaN and the other is a valid number, the MIPS |
| 879 | // instruction will return the number; Java wants the NaN value |
| 880 | // returned. This is why there is extra logic preceding the use of |
| 881 | // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a |
| 882 | // NaN, return the NaN, otherwise return the min/max. |
| 883 | if (type == Primitive::kPrimDouble) { |
| 884 | __ CmpUnD(FTMP, a, b); |
| 885 | __ Bc1eqz(FTMP, &noNaNs); |
| 886 | |
| 887 | // One of the inputs is a NaN |
| 888 | __ CmpEqD(ftmp, a, a); |
| 889 | // If a == a then b is the NaN, otherwise a is the NaN. |
| 890 | __ SelD(ftmp, a, b); |
| 891 | |
| 892 | if (ftmp != out) { |
| 893 | __ MovD(out, ftmp); |
| 894 | } |
| 895 | |
| 896 | __ B(&done); |
| 897 | |
| 898 | __ Bind(&noNaNs); |
| 899 | |
| 900 | if (is_min) { |
| 901 | __ MinD(out, a, b); |
| 902 | } else { |
| 903 | __ MaxD(out, a, b); |
| 904 | } |
| 905 | } else { |
| 906 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 907 | __ CmpUnS(FTMP, a, b); |
| 908 | __ Bc1eqz(FTMP, &noNaNs); |
| 909 | |
| 910 | // One of the inputs is a NaN |
| 911 | __ CmpEqS(ftmp, a, a); |
| 912 | // If a == a then b is the NaN, otherwise a is the NaN. |
| 913 | __ SelS(ftmp, a, b); |
| 914 | |
| 915 | if (ftmp != out) { |
| 916 | __ MovS(out, ftmp); |
| 917 | } |
| 918 | |
| 919 | __ B(&done); |
| 920 | |
| 921 | __ Bind(&noNaNs); |
| 922 | |
| 923 | if (is_min) { |
| 924 | __ MinS(out, a, b); |
| 925 | } else { |
| 926 | __ MaxS(out, a, b); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | __ Bind(&done); |
| 931 | } else { |
| 932 | MipsLabel ordered; |
| 933 | MipsLabel compare; |
| 934 | MipsLabel select; |
| 935 | MipsLabel done; |
| 936 | |
| 937 | if (type == Primitive::kPrimDouble) { |
| 938 | __ CunD(a, b); |
| 939 | } else { |
| 940 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 941 | __ CunS(a, b); |
| 942 | } |
| 943 | __ Bc1f(&ordered); |
| 944 | |
| 945 | // a or b (or both) is a NaN. Return one, which is a NaN. |
| 946 | if (type == Primitive::kPrimDouble) { |
| 947 | __ CeqD(b, b); |
| 948 | } else { |
| 949 | __ CeqS(b, b); |
| 950 | } |
| 951 | __ B(&select); |
| 952 | |
| 953 | __ Bind(&ordered); |
| 954 | |
| 955 | // Neither is a NaN. |
| 956 | // a == b? (-0.0 compares equal with +0.0) |
| 957 | // If equal, handle zeroes, else compare further. |
| 958 | if (type == Primitive::kPrimDouble) { |
| 959 | __ CeqD(a, b); |
| 960 | } else { |
| 961 | __ CeqS(a, b); |
| 962 | } |
| 963 | __ Bc1f(&compare); |
| 964 | |
| 965 | // a == b either bit for bit or one is -0.0 and the other is +0.0. |
| 966 | if (type == Primitive::kPrimDouble) { |
| 967 | __ MoveFromFpuHigh(TMP, a); |
| 968 | __ MoveFromFpuHigh(AT, b); |
| 969 | } else { |
| 970 | __ Mfc1(TMP, a); |
| 971 | __ Mfc1(AT, b); |
| 972 | } |
| 973 | |
| 974 | if (is_min) { |
| 975 | // -0.0 prevails over +0.0. |
| 976 | __ Or(TMP, TMP, AT); |
| 977 | } else { |
| 978 | // +0.0 prevails over -0.0. |
| 979 | __ And(TMP, TMP, AT); |
| 980 | } |
| 981 | |
| 982 | if (type == Primitive::kPrimDouble) { |
| 983 | __ Mfc1(AT, a); |
| 984 | __ Mtc1(AT, out); |
| 985 | __ MoveToFpuHigh(TMP, out); |
| 986 | } else { |
| 987 | __ Mtc1(TMP, out); |
| 988 | } |
| 989 | __ B(&done); |
| 990 | |
| 991 | __ Bind(&compare); |
| 992 | |
| 993 | if (type == Primitive::kPrimDouble) { |
| 994 | if (is_min) { |
| 995 | // return (a <= b) ? a : b; |
| 996 | __ ColeD(a, b); |
| 997 | } else { |
| 998 | // return (a >= b) ? a : b; |
| 999 | __ ColeD(b, a); // b <= a |
| 1000 | } |
| 1001 | } else { |
| 1002 | if (is_min) { |
| 1003 | // return (a <= b) ? a : b; |
| 1004 | __ ColeS(a, b); |
| 1005 | } else { |
| 1006 | // return (a >= b) ? a : b; |
| 1007 | __ ColeS(b, a); // b <= a |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | __ Bind(&select); |
| 1012 | |
| 1013 | if (type == Primitive::kPrimDouble) { |
| 1014 | __ MovtD(out, a); |
| 1015 | __ MovfD(out, b); |
| 1016 | } else { |
| 1017 | __ MovtS(out, a); |
| 1018 | __ MovfS(out, b); |
| 1019 | } |
| 1020 | |
| 1021 | __ Bind(&done); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1026 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1027 | LocationSummary::kNoCall, |
| 1028 | kIntrinsified); |
| 1029 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 1030 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 1031 | locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap); |
| 1032 | } |
| 1033 | |
| 1034 | // double java.lang.Math.min(double, double) |
| 1035 | void IntrinsicLocationsBuilderMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) { |
| 1036 | CreateFPFPToFPLocations(arena_, invoke); |
| 1037 | } |
| 1038 | |
| 1039 | void IntrinsicCodeGeneratorMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) { |
| 1040 | GenMinMaxFP(invoke->GetLocations(), |
| 1041 | /* is_min */ true, |
| 1042 | Primitive::kPrimDouble, |
| 1043 | IsR6(), |
| 1044 | GetAssembler()); |
| 1045 | } |
| 1046 | |
| 1047 | // float java.lang.Math.min(float, float) |
| 1048 | void IntrinsicLocationsBuilderMIPS::VisitMathMinFloatFloat(HInvoke* invoke) { |
| 1049 | CreateFPFPToFPLocations(arena_, invoke); |
| 1050 | } |
| 1051 | |
| 1052 | void IntrinsicCodeGeneratorMIPS::VisitMathMinFloatFloat(HInvoke* invoke) { |
| 1053 | GenMinMaxFP(invoke->GetLocations(), |
| 1054 | /* is_min */ true, |
| 1055 | Primitive::kPrimFloat, |
| 1056 | IsR6(), |
| 1057 | GetAssembler()); |
| 1058 | } |
| 1059 | |
| 1060 | // double java.lang.Math.max(double, double) |
| 1061 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
| 1062 | CreateFPFPToFPLocations(arena_, invoke); |
| 1063 | } |
| 1064 | |
| 1065 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
| 1066 | GenMinMaxFP(invoke->GetLocations(), |
| 1067 | /* is_min */ false, |
| 1068 | Primitive::kPrimDouble, |
| 1069 | IsR6(), |
| 1070 | GetAssembler()); |
| 1071 | } |
| 1072 | |
| 1073 | // float java.lang.Math.max(float, float) |
| 1074 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) { |
| 1075 | CreateFPFPToFPLocations(arena_, invoke); |
| 1076 | } |
| 1077 | |
| 1078 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) { |
| 1079 | GenMinMaxFP(invoke->GetLocations(), |
| 1080 | /* is_min */ false, |
| 1081 | Primitive::kPrimFloat, |
| 1082 | IsR6(), |
| 1083 | GetAssembler()); |
| 1084 | } |
| 1085 | |
| 1086 | static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1087 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1088 | LocationSummary::kNoCall, |
| 1089 | kIntrinsified); |
| 1090 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1091 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1092 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 1093 | } |
| 1094 | |
| 1095 | static void GenMinMax(LocationSummary* locations, |
| 1096 | bool is_min, |
| 1097 | Primitive::Type type, |
| 1098 | bool is_R6, |
| 1099 | MipsAssembler* assembler) { |
| 1100 | if (is_R6) { |
| 1101 | // Some architectures, such as ARM and MIPS (prior to r6), have a |
| 1102 | // conditional move instruction which only changes the target |
| 1103 | // (output) register if the condition is true (MIPS prior to r6 had |
| 1104 | // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions |
| 1105 | // always change the target (output) register. If the condition is |
| 1106 | // true the output register gets the contents of the "rs" register; |
| 1107 | // otherwise, the output register is set to zero. One consequence |
| 1108 | // of this is that to implement something like "rd = c==0 ? rs : rt" |
| 1109 | // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions. |
| 1110 | // After executing this pair of instructions one of the output |
| 1111 | // registers from the pair will necessarily contain zero. Then the |
| 1112 | // code ORs the output registers from the SELEQZ/SELNEZ instructions |
| 1113 | // to get the final result. |
| 1114 | // |
| 1115 | // The initial test to see if the output register is same as the |
| 1116 | // first input register is needed to make sure that value in the |
| 1117 | // first input register isn't clobbered before we've finished |
| 1118 | // computing the output value. The logic in the corresponding else |
| 1119 | // clause performs the same task but makes sure the second input |
| 1120 | // register isn't clobbered in the event that it's the same register |
| 1121 | // as the output register; the else clause also handles the case |
| 1122 | // where the output register is distinct from both the first, and the |
| 1123 | // second input registers. |
| 1124 | if (type == Primitive::kPrimLong) { |
| 1125 | Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 1126 | Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 1127 | Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>(); |
| 1128 | Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>(); |
| 1129 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 1130 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 1131 | |
| 1132 | MipsLabel compare_done; |
| 1133 | |
| 1134 | if (a_lo == b_lo) { |
| 1135 | if (out_lo != a_lo) { |
| 1136 | __ Move(out_lo, a_lo); |
| 1137 | __ Move(out_hi, a_hi); |
| 1138 | } |
| 1139 | } else { |
| 1140 | __ Slt(TMP, b_hi, a_hi); |
| 1141 | __ Bne(b_hi, a_hi, &compare_done); |
| 1142 | |
| 1143 | __ Sltu(TMP, b_lo, a_lo); |
| 1144 | |
| 1145 | __ Bind(&compare_done); |
| 1146 | |
| 1147 | if (is_min) { |
| 1148 | __ Seleqz(AT, a_lo, TMP); |
| 1149 | __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo |
| 1150 | // because at this point we're |
| 1151 | // done using a_lo/b_lo. |
| 1152 | } else { |
| 1153 | __ Selnez(AT, a_lo, TMP); |
| 1154 | __ Seleqz(out_lo, b_lo, TMP); // ditto |
| 1155 | } |
| 1156 | __ Or(out_lo, out_lo, AT); |
| 1157 | if (is_min) { |
| 1158 | __ Seleqz(AT, a_hi, TMP); |
| 1159 | __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi |
| 1160 | } else { |
| 1161 | __ Selnez(AT, a_hi, TMP); |
| 1162 | __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi |
| 1163 | } |
| 1164 | __ Or(out_hi, out_hi, AT); |
| 1165 | } |
| 1166 | } else { |
| 1167 | DCHECK_EQ(type, Primitive::kPrimInt); |
| 1168 | Register a = locations->InAt(0).AsRegister<Register>(); |
| 1169 | Register b = locations->InAt(1).AsRegister<Register>(); |
| 1170 | Register out = locations->Out().AsRegister<Register>(); |
| 1171 | |
| 1172 | if (a == b) { |
| 1173 | if (out != a) { |
| 1174 | __ Move(out, a); |
| 1175 | } |
| 1176 | } else { |
| 1177 | __ Slt(AT, b, a); |
| 1178 | if (is_min) { |
| 1179 | __ Seleqz(TMP, a, AT); |
| 1180 | __ Selnez(AT, b, AT); |
| 1181 | } else { |
| 1182 | __ Selnez(TMP, a, AT); |
| 1183 | __ Seleqz(AT, b, AT); |
| 1184 | } |
| 1185 | __ Or(out, TMP, AT); |
| 1186 | } |
| 1187 | } |
| 1188 | } else { |
| 1189 | if (type == Primitive::kPrimLong) { |
| 1190 | Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 1191 | Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 1192 | Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>(); |
| 1193 | Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>(); |
| 1194 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 1195 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 1196 | |
| 1197 | MipsLabel compare_done; |
| 1198 | |
| 1199 | if (a_lo == b_lo) { |
| 1200 | if (out_lo != a_lo) { |
| 1201 | __ Move(out_lo, a_lo); |
| 1202 | __ Move(out_hi, a_hi); |
| 1203 | } |
| 1204 | } else { |
| 1205 | __ Slt(TMP, a_hi, b_hi); |
| 1206 | __ Bne(a_hi, b_hi, &compare_done); |
| 1207 | |
| 1208 | __ Sltu(TMP, a_lo, b_lo); |
| 1209 | |
| 1210 | __ Bind(&compare_done); |
| 1211 | |
| 1212 | if (is_min) { |
| 1213 | if (out_lo != a_lo) { |
| 1214 | __ Movn(out_hi, a_hi, TMP); |
| 1215 | __ Movn(out_lo, a_lo, TMP); |
| 1216 | } |
| 1217 | if (out_lo != b_lo) { |
| 1218 | __ Movz(out_hi, b_hi, TMP); |
| 1219 | __ Movz(out_lo, b_lo, TMP); |
| 1220 | } |
| 1221 | } else { |
| 1222 | if (out_lo != a_lo) { |
| 1223 | __ Movz(out_hi, a_hi, TMP); |
| 1224 | __ Movz(out_lo, a_lo, TMP); |
| 1225 | } |
| 1226 | if (out_lo != b_lo) { |
| 1227 | __ Movn(out_hi, b_hi, TMP); |
| 1228 | __ Movn(out_lo, b_lo, TMP); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | } else { |
| 1233 | DCHECK_EQ(type, Primitive::kPrimInt); |
| 1234 | Register a = locations->InAt(0).AsRegister<Register>(); |
| 1235 | Register b = locations->InAt(1).AsRegister<Register>(); |
| 1236 | Register out = locations->Out().AsRegister<Register>(); |
| 1237 | |
| 1238 | if (a == b) { |
| 1239 | if (out != a) { |
| 1240 | __ Move(out, a); |
| 1241 | } |
| 1242 | } else { |
| 1243 | __ Slt(AT, a, b); |
| 1244 | if (is_min) { |
| 1245 | if (out != a) { |
| 1246 | __ Movn(out, a, AT); |
| 1247 | } |
| 1248 | if (out != b) { |
| 1249 | __ Movz(out, b, AT); |
| 1250 | } |
| 1251 | } else { |
| 1252 | if (out != a) { |
| 1253 | __ Movz(out, a, AT); |
| 1254 | } |
| 1255 | if (out != b) { |
| 1256 | __ Movn(out, b, AT); |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | // int java.lang.Math.min(int, int) |
| 1265 | void IntrinsicLocationsBuilderMIPS::VisitMathMinIntInt(HInvoke* invoke) { |
| 1266 | CreateIntIntToIntLocations(arena_, invoke); |
| 1267 | } |
| 1268 | |
| 1269 | void IntrinsicCodeGeneratorMIPS::VisitMathMinIntInt(HInvoke* invoke) { |
| 1270 | GenMinMax(invoke->GetLocations(), |
| 1271 | /* is_min */ true, |
| 1272 | Primitive::kPrimInt, |
| 1273 | IsR6(), |
| 1274 | GetAssembler()); |
| 1275 | } |
| 1276 | |
| 1277 | // long java.lang.Math.min(long, long) |
| 1278 | void IntrinsicLocationsBuilderMIPS::VisitMathMinLongLong(HInvoke* invoke) { |
| 1279 | CreateIntIntToIntLocations(arena_, invoke); |
| 1280 | } |
| 1281 | |
| 1282 | void IntrinsicCodeGeneratorMIPS::VisitMathMinLongLong(HInvoke* invoke) { |
| 1283 | GenMinMax(invoke->GetLocations(), |
| 1284 | /* is_min */ true, |
| 1285 | Primitive::kPrimLong, |
| 1286 | IsR6(), |
| 1287 | GetAssembler()); |
| 1288 | } |
| 1289 | |
| 1290 | // int java.lang.Math.max(int, int) |
| 1291 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxIntInt(HInvoke* invoke) { |
| 1292 | CreateIntIntToIntLocations(arena_, invoke); |
| 1293 | } |
| 1294 | |
| 1295 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxIntInt(HInvoke* invoke) { |
| 1296 | GenMinMax(invoke->GetLocations(), |
| 1297 | /* is_min */ false, |
| 1298 | Primitive::kPrimInt, |
| 1299 | IsR6(), |
| 1300 | GetAssembler()); |
| 1301 | } |
| 1302 | |
| 1303 | // long java.lang.Math.max(long, long) |
| 1304 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxLongLong(HInvoke* invoke) { |
| 1305 | CreateIntIntToIntLocations(arena_, invoke); |
| 1306 | } |
| 1307 | |
| 1308 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxLongLong(HInvoke* invoke) { |
| 1309 | GenMinMax(invoke->GetLocations(), |
| 1310 | /* is_min */ false, |
| 1311 | Primitive::kPrimLong, |
| 1312 | IsR6(), |
| 1313 | GetAssembler()); |
| 1314 | } |
| 1315 | |
| 1316 | // double java.lang.Math.sqrt(double) |
| 1317 | void IntrinsicLocationsBuilderMIPS::VisitMathSqrt(HInvoke* invoke) { |
| 1318 | CreateFPToFPLocations(arena_, invoke); |
| 1319 | } |
| 1320 | |
| 1321 | void IntrinsicCodeGeneratorMIPS::VisitMathSqrt(HInvoke* invoke) { |
| 1322 | LocationSummary* locations = invoke->GetLocations(); |
| 1323 | MipsAssembler* assembler = GetAssembler(); |
| 1324 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 1325 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 1326 | |
| 1327 | __ SqrtD(out, in); |
| 1328 | } |
| 1329 | |
Chris Larsen | 3acee73 | 2015-11-18 13:31:08 -0800 | [diff] [blame] | 1330 | // byte libcore.io.Memory.peekByte(long address) |
| 1331 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1332 | CreateIntToIntLocations(arena_, invoke); |
| 1333 | } |
| 1334 | |
| 1335 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1336 | MipsAssembler* assembler = GetAssembler(); |
| 1337 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1338 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1339 | |
| 1340 | __ Lb(out, adr, 0); |
| 1341 | } |
| 1342 | |
| 1343 | // short libcore.io.Memory.peekShort(long address) |
| 1344 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1345 | CreateIntToIntLocations(arena_, invoke); |
| 1346 | } |
| 1347 | |
| 1348 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1349 | MipsAssembler* assembler = GetAssembler(); |
| 1350 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1351 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1352 | |
| 1353 | if (IsR6()) { |
| 1354 | __ Lh(out, adr, 0); |
| 1355 | } else if (IsR2OrNewer()) { |
| 1356 | // Unlike for words, there are no lhl/lhr instructions to load |
| 1357 | // unaligned halfwords so the code loads individual bytes, in case |
| 1358 | // the address isn't halfword-aligned, and assembles them into a |
| 1359 | // signed halfword. |
| 1360 | __ Lb(AT, adr, 1); // This byte must be sign-extended. |
| 1361 | __ Lb(out, adr, 0); // This byte can be either sign-extended, or |
| 1362 | // zero-extended because the following |
| 1363 | // instruction overwrites the sign bits. |
| 1364 | __ Ins(out, AT, 8, 24); |
| 1365 | } else { |
| 1366 | __ Lbu(AT, adr, 0); // This byte must be zero-extended. If it's not |
| 1367 | // the "or" instruction below will destroy the upper |
| 1368 | // 24 bits of the final result. |
| 1369 | __ Lb(out, adr, 1); // This byte must be sign-extended. |
| 1370 | __ Sll(out, out, 8); |
| 1371 | __ Or(out, out, AT); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | // int libcore.io.Memory.peekInt(long address) |
| 1376 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1377 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 1378 | } |
| 1379 | |
| 1380 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1381 | MipsAssembler* assembler = GetAssembler(); |
| 1382 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1383 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1384 | |
| 1385 | if (IsR6()) { |
| 1386 | __ Lw(out, adr, 0); |
| 1387 | } else { |
| 1388 | __ Lwr(out, adr, 0); |
| 1389 | __ Lwl(out, adr, 3); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | // long libcore.io.Memory.peekLong(long address) |
| 1394 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1395 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 1396 | } |
| 1397 | |
| 1398 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1399 | MipsAssembler* assembler = GetAssembler(); |
| 1400 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1401 | Register out_lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>(); |
| 1402 | Register out_hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>(); |
| 1403 | |
| 1404 | if (IsR6()) { |
| 1405 | __ Lw(out_lo, adr, 0); |
| 1406 | __ Lw(out_hi, adr, 4); |
| 1407 | } else { |
| 1408 | __ Lwr(out_lo, adr, 0); |
| 1409 | __ Lwl(out_lo, adr, 3); |
| 1410 | __ Lwr(out_hi, adr, 4); |
| 1411 | __ Lwl(out_hi, adr, 7); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1416 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1417 | LocationSummary::kNoCall, |
| 1418 | kIntrinsified); |
| 1419 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1420 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1421 | } |
| 1422 | |
| 1423 | // void libcore.io.Memory.pokeByte(long address, byte value) |
| 1424 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1425 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1426 | } |
| 1427 | |
| 1428 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1429 | MipsAssembler* assembler = GetAssembler(); |
| 1430 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1431 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1432 | |
| 1433 | __ Sb(val, adr, 0); |
| 1434 | } |
| 1435 | |
| 1436 | // void libcore.io.Memory.pokeShort(long address, short value) |
| 1437 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1438 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1439 | } |
| 1440 | |
| 1441 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1442 | MipsAssembler* assembler = GetAssembler(); |
| 1443 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1444 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1445 | |
| 1446 | if (IsR6()) { |
| 1447 | __ Sh(val, adr, 0); |
| 1448 | } else { |
| 1449 | // Unlike for words, there are no shl/shr instructions to store |
| 1450 | // unaligned halfwords so the code stores individual bytes, in case |
| 1451 | // the address isn't halfword-aligned. |
| 1452 | __ Sb(val, adr, 0); |
| 1453 | __ Srl(AT, val, 8); |
| 1454 | __ Sb(AT, adr, 1); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | // void libcore.io.Memory.pokeInt(long address, int value) |
| 1459 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1460 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1461 | } |
| 1462 | |
| 1463 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1464 | MipsAssembler* assembler = GetAssembler(); |
| 1465 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1466 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1467 | |
| 1468 | if (IsR6()) { |
| 1469 | __ Sw(val, adr, 0); |
| 1470 | } else { |
| 1471 | __ Swr(val, adr, 0); |
| 1472 | __ Swl(val, adr, 3); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | // void libcore.io.Memory.pokeLong(long address, long value) |
| 1477 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1478 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1479 | } |
| 1480 | |
| 1481 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1482 | MipsAssembler* assembler = GetAssembler(); |
| 1483 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1484 | Register val_lo = invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>(); |
| 1485 | Register val_hi = invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>(); |
| 1486 | |
| 1487 | if (IsR6()) { |
| 1488 | __ Sw(val_lo, adr, 0); |
| 1489 | __ Sw(val_hi, adr, 4); |
| 1490 | } else { |
| 1491 | __ Swr(val_lo, adr, 0); |
| 1492 | __ Swl(val_lo, adr, 3); |
| 1493 | __ Swr(val_hi, adr, 4); |
| 1494 | __ Swl(val_hi, adr, 7); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 1498 | // Thread java.lang.Thread.currentThread() |
| 1499 | void IntrinsicLocationsBuilderMIPS::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1500 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1501 | LocationSummary::kNoCall, |
| 1502 | kIntrinsified); |
| 1503 | locations->SetOut(Location::RequiresRegister()); |
| 1504 | } |
| 1505 | |
| 1506 | void IntrinsicCodeGeneratorMIPS::VisitThreadCurrentThread(HInvoke* invoke) { |
| 1507 | MipsAssembler* assembler = GetAssembler(); |
| 1508 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1509 | |
| 1510 | __ LoadFromOffset(kLoadWord, |
| 1511 | out, |
| 1512 | TR, |
| 1513 | Thread::PeerOffset<kMipsPointerSize>().Int32Value()); |
| 1514 | } |
| 1515 | |
Chris Larsen | 4fdc6d9 | 2015-12-14 13:26:14 -0800 | [diff] [blame] | 1516 | static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1517 | bool can_call = |
| 1518 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject || |
| 1519 | invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile; |
| 1520 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1521 | can_call ? |
| 1522 | LocationSummary::kCallOnSlowPath : |
| 1523 | LocationSummary::kNoCall, |
| 1524 | kIntrinsified); |
| 1525 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1526 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1527 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1528 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 1529 | } |
| 1530 | |
| 1531 | static void GenUnsafeGet(HInvoke* invoke, |
| 1532 | Primitive::Type type, |
| 1533 | bool is_volatile, |
| 1534 | bool is_R6, |
| 1535 | CodeGeneratorMIPS* codegen) { |
| 1536 | LocationSummary* locations = invoke->GetLocations(); |
| 1537 | DCHECK((type == Primitive::kPrimInt) || |
| 1538 | (type == Primitive::kPrimLong) || |
| 1539 | (type == Primitive::kPrimNot)) << type; |
| 1540 | MipsAssembler* assembler = codegen->GetAssembler(); |
| 1541 | // Object pointer. |
| 1542 | Register base = locations->InAt(1).AsRegister<Register>(); |
| 1543 | // The "offset" argument is passed as a "long". Since this code is for |
| 1544 | // a 32-bit processor, we can only use 32-bit addresses, so we only |
| 1545 | // need the low 32-bits of offset. |
| 1546 | Register offset_lo = invoke->GetLocations()->InAt(2).AsRegisterPairLow<Register>(); |
| 1547 | |
| 1548 | __ Addu(TMP, base, offset_lo); |
| 1549 | if (is_volatile) { |
| 1550 | __ Sync(0); |
| 1551 | } |
| 1552 | if (type == Primitive::kPrimLong) { |
| 1553 | Register trg_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 1554 | Register trg_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 1555 | |
| 1556 | if (is_R6) { |
| 1557 | __ Lw(trg_lo, TMP, 0); |
| 1558 | __ Lw(trg_hi, TMP, 4); |
| 1559 | } else { |
| 1560 | __ Lwr(trg_lo, TMP, 0); |
| 1561 | __ Lwl(trg_lo, TMP, 3); |
| 1562 | __ Lwr(trg_hi, TMP, 4); |
| 1563 | __ Lwl(trg_hi, TMP, 7); |
| 1564 | } |
| 1565 | } else { |
| 1566 | Register trg = locations->Out().AsRegister<Register>(); |
| 1567 | |
| 1568 | if (is_R6) { |
| 1569 | __ Lw(trg, TMP, 0); |
| 1570 | } else { |
| 1571 | __ Lwr(trg, TMP, 0); |
| 1572 | __ Lwl(trg, TMP, 3); |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | // int sun.misc.Unsafe.getInt(Object o, long offset) |
| 1578 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGet(HInvoke* invoke) { |
| 1579 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1580 | } |
| 1581 | |
| 1582 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGet(HInvoke* invoke) { |
| 1583 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, IsR6(), codegen_); |
| 1584 | } |
| 1585 | |
| 1586 | // int sun.misc.Unsafe.getIntVolatile(Object o, long offset) |
| 1587 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| 1588 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1589 | } |
| 1590 | |
| 1591 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) { |
| 1592 | GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, IsR6(), codegen_); |
| 1593 | } |
| 1594 | |
| 1595 | // long sun.misc.Unsafe.getLong(Object o, long offset) |
| 1596 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetLong(HInvoke* invoke) { |
| 1597 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1598 | } |
| 1599 | |
| 1600 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetLong(HInvoke* invoke) { |
| 1601 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, IsR6(), codegen_); |
| 1602 | } |
| 1603 | |
| 1604 | // long sun.misc.Unsafe.getLongVolatile(Object o, long offset) |
| 1605 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1606 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1607 | } |
| 1608 | |
| 1609 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetLongVolatile(HInvoke* invoke) { |
| 1610 | GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, IsR6(), codegen_); |
| 1611 | } |
| 1612 | |
| 1613 | // Object sun.misc.Unsafe.getObject(Object o, long offset) |
| 1614 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1615 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1616 | } |
| 1617 | |
| 1618 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObject(HInvoke* invoke) { |
| 1619 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, IsR6(), codegen_); |
| 1620 | } |
| 1621 | |
| 1622 | // Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset) |
| 1623 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1624 | CreateIntIntIntToIntLocations(arena_, invoke); |
| 1625 | } |
| 1626 | |
| 1627 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) { |
| 1628 | GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, IsR6(), codegen_); |
| 1629 | } |
| 1630 | |
| 1631 | static void CreateIntIntIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1632 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1633 | LocationSummary::kNoCall, |
| 1634 | kIntrinsified); |
| 1635 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1636 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1637 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1638 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1639 | } |
| 1640 | |
| 1641 | static void GenUnsafePut(LocationSummary* locations, |
| 1642 | Primitive::Type type, |
| 1643 | bool is_volatile, |
| 1644 | bool is_ordered, |
| 1645 | bool is_R6, |
| 1646 | CodeGeneratorMIPS* codegen) { |
| 1647 | DCHECK((type == Primitive::kPrimInt) || |
| 1648 | (type == Primitive::kPrimLong) || |
| 1649 | (type == Primitive::kPrimNot)) << type; |
| 1650 | MipsAssembler* assembler = codegen->GetAssembler(); |
| 1651 | // Object pointer. |
| 1652 | Register base = locations->InAt(1).AsRegister<Register>(); |
| 1653 | // The "offset" argument is passed as a "long", i.e., it's 64-bits in |
| 1654 | // size. Since this code is for a 32-bit processor, we can only use |
| 1655 | // 32-bit addresses, so we only need the low 32-bits of offset. |
| 1656 | Register offset_lo = locations->InAt(2).AsRegisterPairLow<Register>(); |
| 1657 | |
| 1658 | __ Addu(TMP, base, offset_lo); |
| 1659 | if (is_volatile || is_ordered) { |
| 1660 | __ Sync(0); |
| 1661 | } |
| 1662 | if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) { |
| 1663 | Register value = locations->InAt(3).AsRegister<Register>(); |
| 1664 | |
| 1665 | if (is_R6) { |
| 1666 | __ Sw(value, TMP, 0); |
| 1667 | } else { |
| 1668 | __ Swr(value, TMP, 0); |
| 1669 | __ Swl(value, TMP, 3); |
| 1670 | } |
| 1671 | } else { |
| 1672 | Register value_lo = locations->InAt(3).AsRegisterPairLow<Register>(); |
| 1673 | Register value_hi = locations->InAt(3).AsRegisterPairHigh<Register>(); |
| 1674 | |
| 1675 | if (is_R6) { |
| 1676 | __ Sw(value_lo, TMP, 0); |
| 1677 | __ Sw(value_hi, TMP, 4); |
| 1678 | } else { |
| 1679 | __ Swr(value_lo, TMP, 0); |
| 1680 | __ Swl(value_lo, TMP, 3); |
| 1681 | __ Swr(value_hi, TMP, 4); |
| 1682 | __ Swl(value_hi, TMP, 7); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (is_volatile) { |
| 1687 | __ Sync(0); |
| 1688 | } |
| 1689 | |
| 1690 | if (type == Primitive::kPrimNot) { |
Goran Jakovljevic | e114da2 | 2016-12-26 14:21:43 +0100 | [diff] [blame] | 1691 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 1692 | codegen->MarkGCCard(base, locations->InAt(3).AsRegister<Register>(), value_can_be_null); |
Chris Larsen | 4fdc6d9 | 2015-12-14 13:26:14 -0800 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | // void sun.misc.Unsafe.putInt(Object o, long offset, int x) |
| 1697 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePut(HInvoke* invoke) { |
| 1698 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1699 | } |
| 1700 | |
| 1701 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePut(HInvoke* invoke) { |
| 1702 | GenUnsafePut(invoke->GetLocations(), |
| 1703 | Primitive::kPrimInt, |
| 1704 | /* is_volatile */ false, |
| 1705 | /* is_ordered */ false, |
| 1706 | IsR6(), |
| 1707 | codegen_); |
| 1708 | } |
| 1709 | |
| 1710 | // void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x) |
| 1711 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1712 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1713 | } |
| 1714 | |
| 1715 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutOrdered(HInvoke* invoke) { |
| 1716 | GenUnsafePut(invoke->GetLocations(), |
| 1717 | Primitive::kPrimInt, |
| 1718 | /* is_volatile */ false, |
| 1719 | /* is_ordered */ true, |
| 1720 | IsR6(), |
| 1721 | codegen_); |
| 1722 | } |
| 1723 | |
| 1724 | // void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x) |
| 1725 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1726 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1727 | } |
| 1728 | |
| 1729 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutVolatile(HInvoke* invoke) { |
| 1730 | GenUnsafePut(invoke->GetLocations(), |
| 1731 | Primitive::kPrimInt, |
| 1732 | /* is_volatile */ true, |
| 1733 | /* is_ordered */ false, |
| 1734 | IsR6(), |
| 1735 | codegen_); |
| 1736 | } |
| 1737 | |
| 1738 | // void sun.misc.Unsafe.putObject(Object o, long offset, Object x) |
| 1739 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObject(HInvoke* invoke) { |
| 1740 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1741 | } |
| 1742 | |
| 1743 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObject(HInvoke* invoke) { |
| 1744 | GenUnsafePut(invoke->GetLocations(), |
| 1745 | Primitive::kPrimNot, |
| 1746 | /* is_volatile */ false, |
| 1747 | /* is_ordered */ false, |
| 1748 | IsR6(), |
| 1749 | codegen_); |
| 1750 | } |
| 1751 | |
| 1752 | // void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x) |
| 1753 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1754 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1755 | } |
| 1756 | |
| 1757 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) { |
| 1758 | GenUnsafePut(invoke->GetLocations(), |
| 1759 | Primitive::kPrimNot, |
| 1760 | /* is_volatile */ false, |
| 1761 | /* is_ordered */ true, |
| 1762 | IsR6(), |
| 1763 | codegen_); |
| 1764 | } |
| 1765 | |
| 1766 | // void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x) |
| 1767 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1768 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1769 | } |
| 1770 | |
| 1771 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) { |
| 1772 | GenUnsafePut(invoke->GetLocations(), |
| 1773 | Primitive::kPrimNot, |
| 1774 | /* is_volatile */ true, |
| 1775 | /* is_ordered */ false, |
| 1776 | IsR6(), |
| 1777 | codegen_); |
| 1778 | } |
| 1779 | |
| 1780 | // void sun.misc.Unsafe.putLong(Object o, long offset, long x) |
| 1781 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLong(HInvoke* invoke) { |
| 1782 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1783 | } |
| 1784 | |
| 1785 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLong(HInvoke* invoke) { |
| 1786 | GenUnsafePut(invoke->GetLocations(), |
| 1787 | Primitive::kPrimLong, |
| 1788 | /* is_volatile */ false, |
| 1789 | /* is_ordered */ false, |
| 1790 | IsR6(), |
| 1791 | codegen_); |
| 1792 | } |
| 1793 | |
| 1794 | // void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x) |
| 1795 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1796 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1797 | } |
| 1798 | |
| 1799 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) { |
| 1800 | GenUnsafePut(invoke->GetLocations(), |
| 1801 | Primitive::kPrimLong, |
| 1802 | /* is_volatile */ false, |
| 1803 | /* is_ordered */ true, |
| 1804 | IsR6(), |
| 1805 | codegen_); |
| 1806 | } |
| 1807 | |
| 1808 | // void sun.misc.Unsafe.putLongVolatile(Object o, long offset, long x) |
| 1809 | void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1810 | CreateIntIntIntIntToVoidLocations(arena_, invoke); |
| 1811 | } |
| 1812 | |
| 1813 | void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLongVolatile(HInvoke* invoke) { |
| 1814 | GenUnsafePut(invoke->GetLocations(), |
| 1815 | Primitive::kPrimLong, |
| 1816 | /* is_volatile */ true, |
| 1817 | /* is_ordered */ false, |
| 1818 | IsR6(), |
| 1819 | codegen_); |
| 1820 | } |
| 1821 | |
Alexey Frunze | 51aff3a | 2016-03-17 17:21:45 -0700 | [diff] [blame] | 1822 | static void CreateIntIntIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1823 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1824 | LocationSummary::kNoCall, |
| 1825 | kIntrinsified); |
| 1826 | locations->SetInAt(0, Location::NoLocation()); // Unused receiver. |
| 1827 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1828 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1829 | locations->SetInAt(3, Location::RequiresRegister()); |
| 1830 | locations->SetInAt(4, Location::RequiresRegister()); |
| 1831 | |
| 1832 | locations->SetOut(Location::RequiresRegister()); |
| 1833 | } |
| 1834 | |
| 1835 | static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorMIPS* codegen) { |
| 1836 | MipsAssembler* assembler = codegen->GetAssembler(); |
| 1837 | bool isR6 = codegen->GetInstructionSetFeatures().IsR6(); |
| 1838 | Register base = locations->InAt(1).AsRegister<Register>(); |
| 1839 | Register offset_lo = locations->InAt(2).AsRegisterPairLow<Register>(); |
| 1840 | Register expected = locations->InAt(3).AsRegister<Register>(); |
| 1841 | Register value = locations->InAt(4).AsRegister<Register>(); |
| 1842 | Register out = locations->Out().AsRegister<Register>(); |
| 1843 | |
| 1844 | DCHECK_NE(base, out); |
| 1845 | DCHECK_NE(offset_lo, out); |
| 1846 | DCHECK_NE(expected, out); |
| 1847 | |
| 1848 | if (type == Primitive::kPrimNot) { |
| 1849 | // Mark card for object assuming new value is stored. |
Goran Jakovljevic | e114da2 | 2016-12-26 14:21:43 +0100 | [diff] [blame] | 1850 | bool value_can_be_null = true; // TODO: Worth finding out this information? |
| 1851 | codegen->MarkGCCard(base, value, value_can_be_null); |
Alexey Frunze | 51aff3a | 2016-03-17 17:21:45 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | // do { |
| 1855 | // tmp_value = [tmp_ptr] - expected; |
| 1856 | // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value)); |
| 1857 | // result = tmp_value != 0; |
| 1858 | |
| 1859 | MipsLabel loop_head, exit_loop; |
| 1860 | __ Addu(TMP, base, offset_lo); |
| 1861 | __ Sync(0); |
| 1862 | __ Bind(&loop_head); |
| 1863 | if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) { |
| 1864 | if (isR6) { |
| 1865 | __ LlR6(out, TMP); |
| 1866 | } else { |
| 1867 | __ LlR2(out, TMP); |
| 1868 | } |
| 1869 | } else { |
| 1870 | LOG(FATAL) << "Unsupported op size " << type; |
| 1871 | UNREACHABLE(); |
| 1872 | } |
| 1873 | __ Subu(out, out, expected); // If we didn't get the 'expected' |
| 1874 | __ Sltiu(out, out, 1); // value, set 'out' to false, and |
| 1875 | __ Beqz(out, &exit_loop); // return. |
| 1876 | __ Move(out, value); // Use 'out' for the 'store conditional' instruction. |
| 1877 | // If we use 'value' directly, we would lose 'value' |
| 1878 | // in the case that the store fails. Whether the |
| 1879 | // store succeeds, or fails, it will load the |
| 1880 | // correct boolean value into the 'out' register. |
| 1881 | // This test isn't really necessary. We only support Primitive::kPrimInt, |
| 1882 | // Primitive::kPrimNot, and we already verified that we're working on one |
| 1883 | // of those two types. It's left here in case the code needs to support |
| 1884 | // other types in the future. |
| 1885 | if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) { |
| 1886 | if (isR6) { |
| 1887 | __ ScR6(out, TMP); |
| 1888 | } else { |
| 1889 | __ ScR2(out, TMP); |
| 1890 | } |
| 1891 | } |
| 1892 | __ Beqz(out, &loop_head); // If we couldn't do the read-modify-write |
| 1893 | // cycle atomically then retry. |
| 1894 | __ Bind(&exit_loop); |
| 1895 | __ Sync(0); |
| 1896 | } |
| 1897 | |
| 1898 | // boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x) |
| 1899 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASInt(HInvoke* invoke) { |
| 1900 | CreateIntIntIntIntIntToIntLocations(arena_, invoke); |
| 1901 | } |
| 1902 | |
| 1903 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASInt(HInvoke* invoke) { |
| 1904 | GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_); |
| 1905 | } |
| 1906 | |
| 1907 | // boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x) |
| 1908 | void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASObject(HInvoke* invoke) { |
| 1909 | CreateIntIntIntIntIntToIntLocations(arena_, invoke); |
| 1910 | } |
| 1911 | |
| 1912 | void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASObject(HInvoke* invoke) { |
| 1913 | GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_); |
| 1914 | } |
| 1915 | |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 1916 | // int java.lang.String.compareTo(String anotherString) |
| 1917 | void IntrinsicLocationsBuilderMIPS::VisitStringCompareTo(HInvoke* invoke) { |
| 1918 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 1919 | LocationSummary::kCallOnMainAndSlowPath, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 1920 | kIntrinsified); |
| 1921 | InvokeRuntimeCallingConvention calling_convention; |
| 1922 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 1923 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 1924 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 1925 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 1926 | } |
| 1927 | |
| 1928 | void IntrinsicCodeGeneratorMIPS::VisitStringCompareTo(HInvoke* invoke) { |
| 1929 | MipsAssembler* assembler = GetAssembler(); |
| 1930 | LocationSummary* locations = invoke->GetLocations(); |
| 1931 | |
| 1932 | // Note that the null check must have been done earlier. |
| 1933 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1934 | |
| 1935 | Register argument = locations->InAt(1).AsRegister<Register>(); |
| 1936 | SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke); |
| 1937 | codegen_->AddSlowPath(slow_path); |
| 1938 | __ Beqz(argument, slow_path->GetEntryLabel()); |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 1939 | codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path); |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 1940 | __ Bind(slow_path->GetExitLabel()); |
| 1941 | } |
| 1942 | |
Chris Larsen | 16ba2b4 | 2015-11-02 10:58:31 -0800 | [diff] [blame] | 1943 | // boolean java.lang.String.equals(Object anObject) |
| 1944 | void IntrinsicLocationsBuilderMIPS::VisitStringEquals(HInvoke* invoke) { |
| 1945 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1946 | LocationSummary::kNoCall, |
| 1947 | kIntrinsified); |
| 1948 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1949 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1950 | locations->SetOut(Location::RequiresRegister()); |
| 1951 | |
| 1952 | // Temporary registers to store lengths of strings and for calculations. |
| 1953 | locations->AddTemp(Location::RequiresRegister()); |
| 1954 | locations->AddTemp(Location::RequiresRegister()); |
| 1955 | locations->AddTemp(Location::RequiresRegister()); |
| 1956 | } |
| 1957 | |
| 1958 | void IntrinsicCodeGeneratorMIPS::VisitStringEquals(HInvoke* invoke) { |
| 1959 | MipsAssembler* assembler = GetAssembler(); |
| 1960 | LocationSummary* locations = invoke->GetLocations(); |
| 1961 | |
| 1962 | Register str = locations->InAt(0).AsRegister<Register>(); |
| 1963 | Register arg = locations->InAt(1).AsRegister<Register>(); |
| 1964 | Register out = locations->Out().AsRegister<Register>(); |
| 1965 | |
| 1966 | Register temp1 = locations->GetTemp(0).AsRegister<Register>(); |
| 1967 | Register temp2 = locations->GetTemp(1).AsRegister<Register>(); |
| 1968 | Register temp3 = locations->GetTemp(2).AsRegister<Register>(); |
| 1969 | |
| 1970 | MipsLabel loop; |
| 1971 | MipsLabel end; |
| 1972 | MipsLabel return_true; |
| 1973 | MipsLabel return_false; |
| 1974 | |
| 1975 | // Get offsets of count, value, and class fields within a string object. |
| 1976 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1977 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1978 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1979 | |
| 1980 | // Note that the null check must have been done earlier. |
| 1981 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1982 | |
| 1983 | // If the register containing the pointer to "this", and the register |
| 1984 | // containing the pointer to "anObject" are the same register then |
| 1985 | // "this", and "anObject" are the same object and we can |
| 1986 | // short-circuit the logic to a true result. |
| 1987 | if (str == arg) { |
| 1988 | __ LoadConst32(out, 1); |
| 1989 | return; |
| 1990 | } |
| 1991 | |
| 1992 | // Check if input is null, return false if it is. |
| 1993 | __ Beqz(arg, &return_false); |
| 1994 | |
| 1995 | // Reference equality check, return true if same reference. |
| 1996 | __ Beq(str, arg, &return_true); |
| 1997 | |
| 1998 | // Instanceof check for the argument by comparing class fields. |
| 1999 | // All string objects must have the same type since String cannot be subclassed. |
| 2000 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 2001 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 2002 | __ Lw(temp1, str, class_offset); |
| 2003 | __ Lw(temp2, arg, class_offset); |
| 2004 | __ Bne(temp1, temp2, &return_false); |
| 2005 | |
| 2006 | // Load lengths of this and argument strings. |
| 2007 | __ Lw(temp1, str, count_offset); |
| 2008 | __ Lw(temp2, arg, count_offset); |
| 2009 | // Check if lengths are equal, return false if they're not. |
| 2010 | __ Bne(temp1, temp2, &return_false); |
| 2011 | // Return true if both strings are empty. |
| 2012 | __ Beqz(temp1, &return_true); |
| 2013 | |
| 2014 | // Don't overwrite input registers |
| 2015 | __ Move(TMP, str); |
| 2016 | __ Move(temp3, arg); |
| 2017 | |
| 2018 | // Assertions that must hold in order to compare strings 2 characters at a time. |
| 2019 | DCHECK_ALIGNED(value_offset, 4); |
| 2020 | static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded"); |
| 2021 | |
| 2022 | // Loop to compare strings 2 characters at a time starting at the beginning of the string. |
| 2023 | // Ok to do this because strings are zero-padded. |
| 2024 | __ Bind(&loop); |
| 2025 | __ Lw(out, TMP, value_offset); |
| 2026 | __ Lw(temp2, temp3, value_offset); |
| 2027 | __ Bne(out, temp2, &return_false); |
| 2028 | __ Addiu(TMP, TMP, 4); |
| 2029 | __ Addiu(temp3, temp3, 4); |
| 2030 | __ Addiu(temp1, temp1, -2); |
| 2031 | __ Bgtz(temp1, &loop); |
| 2032 | |
| 2033 | // Return true and exit the function. |
| 2034 | // If loop does not result in returning false, we return true. |
| 2035 | __ Bind(&return_true); |
| 2036 | __ LoadConst32(out, 1); |
| 2037 | __ B(&end); |
| 2038 | |
| 2039 | // Return false and exit the function. |
| 2040 | __ Bind(&return_false); |
| 2041 | __ LoadConst32(out, 0); |
| 2042 | __ Bind(&end); |
| 2043 | } |
| 2044 | |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2045 | static void GenerateStringIndexOf(HInvoke* invoke, |
| 2046 | bool start_at_zero, |
| 2047 | MipsAssembler* assembler, |
| 2048 | CodeGeneratorMIPS* codegen, |
| 2049 | ArenaAllocator* allocator) { |
| 2050 | LocationSummary* locations = invoke->GetLocations(); |
| 2051 | Register tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<Register>() : TMP; |
| 2052 | |
| 2053 | // Note that the null check must have been done earlier. |
| 2054 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 2055 | |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 2056 | // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically, |
| 2057 | // or directly dispatch for a large constant, or omit slow-path for a small constant or a char. |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2058 | SlowPathCodeMIPS* slow_path = nullptr; |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 2059 | HInstruction* code_point = invoke->InputAt(1); |
| 2060 | if (code_point->IsIntConstant()) { |
Vladimir Marko | da05108 | 2016-05-17 16:10:20 +0100 | [diff] [blame] | 2061 | if (!IsUint<16>(code_point->AsIntConstant()->GetValue())) { |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2062 | // Always needs the slow-path. We could directly dispatch to it, |
| 2063 | // but this case should be rare, so for simplicity just put the |
| 2064 | // full slow-path down and branch unconditionally. |
| 2065 | slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke); |
| 2066 | codegen->AddSlowPath(slow_path); |
| 2067 | __ B(slow_path->GetEntryLabel()); |
| 2068 | __ Bind(slow_path->GetExitLabel()); |
| 2069 | return; |
| 2070 | } |
Vladimir Marko | fb6c90a | 2016-05-06 15:52:12 +0100 | [diff] [blame] | 2071 | } else if (code_point->GetType() != Primitive::kPrimChar) { |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2072 | Register char_reg = locations->InAt(1).AsRegister<Register>(); |
| 2073 | // The "bltu" conditional branch tests to see if the character value |
| 2074 | // fits in a valid 16-bit (MIPS halfword) value. If it doesn't then |
| 2075 | // the character being searched for, if it exists in the string, is |
| 2076 | // encoded using UTF-16 and stored in the string as two (16-bit) |
| 2077 | // halfwords. Currently the assembly code used to implement this |
| 2078 | // intrinsic doesn't support searching for a character stored as |
| 2079 | // two halfwords so we fallback to using the generic implementation |
| 2080 | // of indexOf(). |
| 2081 | __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max()); |
| 2082 | slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke); |
| 2083 | codegen->AddSlowPath(slow_path); |
| 2084 | __ Bltu(tmp_reg, char_reg, slow_path->GetEntryLabel()); |
| 2085 | } |
| 2086 | |
| 2087 | if (start_at_zero) { |
| 2088 | DCHECK_EQ(tmp_reg, A2); |
| 2089 | // Start-index = 0. |
| 2090 | __ Clear(tmp_reg); |
| 2091 | } |
| 2092 | |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 2093 | codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path); |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2094 | if (slow_path != nullptr) { |
| 2095 | __ Bind(slow_path->GetExitLabel()); |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | // int java.lang.String.indexOf(int ch) |
| 2100 | void IntrinsicLocationsBuilderMIPS::VisitStringIndexOf(HInvoke* invoke) { |
| 2101 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 2102 | LocationSummary::kCallOnMainAndSlowPath, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2103 | kIntrinsified); |
| 2104 | // We have a hand-crafted assembly stub that follows the runtime |
| 2105 | // calling convention. So it's best to align the inputs accordingly. |
| 2106 | InvokeRuntimeCallingConvention calling_convention; |
| 2107 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2108 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 2109 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2110 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2111 | |
| 2112 | // Need a temp for slow-path codepoint compare, and need to send start-index=0. |
| 2113 | locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 2114 | } |
| 2115 | |
| 2116 | void IntrinsicCodeGeneratorMIPS::VisitStringIndexOf(HInvoke* invoke) { |
| 2117 | GenerateStringIndexOf(invoke, |
| 2118 | /* start_at_zero */ true, |
| 2119 | GetAssembler(), |
| 2120 | codegen_, |
| 2121 | GetAllocator()); |
| 2122 | } |
| 2123 | |
| 2124 | // int java.lang.String.indexOf(int ch, int fromIndex) |
| 2125 | void IntrinsicLocationsBuilderMIPS::VisitStringIndexOfAfter(HInvoke* invoke) { |
| 2126 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 2127 | LocationSummary::kCallOnMainAndSlowPath, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2128 | kIntrinsified); |
| 2129 | // We have a hand-crafted assembly stub that follows the runtime |
| 2130 | // calling convention. So it's best to align the inputs accordingly. |
| 2131 | InvokeRuntimeCallingConvention calling_convention; |
| 2132 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2133 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 2134 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 2135 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2136 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2137 | |
| 2138 | // Need a temp for slow-path codepoint compare. |
| 2139 | locations->AddTemp(Location::RequiresRegister()); |
| 2140 | } |
| 2141 | |
| 2142 | void IntrinsicCodeGeneratorMIPS::VisitStringIndexOfAfter(HInvoke* invoke) { |
| 2143 | GenerateStringIndexOf(invoke, |
| 2144 | /* start_at_zero */ false, |
| 2145 | GetAssembler(), |
| 2146 | codegen_, |
| 2147 | GetAllocator()); |
| 2148 | } |
| 2149 | |
| 2150 | // java.lang.StringFactory.newStringFromBytes(byte[] data, int high, int offset, int byteCount) |
| 2151 | void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 2152 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 2153 | LocationSummary::kCallOnMainAndSlowPath, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2154 | kIntrinsified); |
| 2155 | InvokeRuntimeCallingConvention calling_convention; |
| 2156 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2157 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 2158 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 2159 | locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3))); |
| 2160 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2161 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2162 | } |
| 2163 | |
| 2164 | void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) { |
| 2165 | MipsAssembler* assembler = GetAssembler(); |
| 2166 | LocationSummary* locations = invoke->GetLocations(); |
| 2167 | |
| 2168 | Register byte_array = locations->InAt(0).AsRegister<Register>(); |
| 2169 | SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke); |
| 2170 | codegen_->AddSlowPath(slow_path); |
| 2171 | __ Beqz(byte_array, slow_path->GetEntryLabel()); |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 2172 | codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path); |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2173 | __ Bind(slow_path->GetExitLabel()); |
| 2174 | } |
| 2175 | |
| 2176 | // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data) |
| 2177 | void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromChars(HInvoke* invoke) { |
| 2178 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 2179 | LocationSummary::kCallOnMainOnly, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2180 | kIntrinsified); |
| 2181 | InvokeRuntimeCallingConvention calling_convention; |
| 2182 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2183 | locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 2184 | locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 2185 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2186 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2187 | } |
| 2188 | |
| 2189 | void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromChars(HInvoke* invoke) { |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2190 | // No need to emit code checking whether `locations->InAt(2)` is a null |
| 2191 | // pointer, as callers of the native method |
| 2192 | // |
| 2193 | // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data) |
| 2194 | // |
| 2195 | // all include a null check on `data` before calling that method. |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 2196 | codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc()); |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | // java.lang.StringFactory.newStringFromString(String toCopy) |
| 2200 | void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromString(HInvoke* invoke) { |
| 2201 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 2202 | LocationSummary::kCallOnMainAndSlowPath, |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2203 | kIntrinsified); |
| 2204 | InvokeRuntimeCallingConvention calling_convention; |
| 2205 | locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2206 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2207 | locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2208 | } |
| 2209 | |
| 2210 | void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromString(HInvoke* invoke) { |
| 2211 | MipsAssembler* assembler = GetAssembler(); |
| 2212 | LocationSummary* locations = invoke->GetLocations(); |
| 2213 | |
| 2214 | Register string_to_copy = locations->InAt(0).AsRegister<Register>(); |
| 2215 | SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke); |
| 2216 | codegen_->AddSlowPath(slow_path); |
| 2217 | __ Beqz(string_to_copy, slow_path->GetEntryLabel()); |
Serban Constantinescu | fca1666 | 2016-07-14 09:21:59 +0100 | [diff] [blame] | 2218 | codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc()); |
Chris Larsen | cf283da | 2016-01-19 16:45:35 -0800 | [diff] [blame] | 2219 | __ Bind(slow_path->GetExitLabel()); |
| 2220 | } |
| 2221 | |
Chris Larsen | 2714fe6 | 2016-02-11 14:23:53 -0800 | [diff] [blame] | 2222 | static void GenIsInfinite(LocationSummary* locations, |
| 2223 | const Primitive::Type type, |
| 2224 | const bool isR6, |
| 2225 | MipsAssembler* assembler) { |
| 2226 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 2227 | Register out = locations->Out().AsRegister<Register>(); |
| 2228 | |
| 2229 | DCHECK(type == Primitive::kPrimFloat || type == Primitive::kPrimDouble); |
| 2230 | |
| 2231 | if (isR6) { |
| 2232 | if (type == Primitive::kPrimDouble) { |
| 2233 | __ ClassD(FTMP, in); |
| 2234 | } else { |
| 2235 | __ ClassS(FTMP, in); |
| 2236 | } |
| 2237 | __ Mfc1(out, FTMP); |
| 2238 | __ Andi(out, out, kPositiveInfinity | kNegativeInfinity); |
| 2239 | __ Sltu(out, ZERO, out); |
| 2240 | } else { |
| 2241 | // If one, or more, of the exponent bits is zero, then the number can't be infinite. |
| 2242 | if (type == Primitive::kPrimDouble) { |
| 2243 | __ MoveFromFpuHigh(TMP, in); |
Anton Kirilov | a3ffea2 | 2016-04-07 17:02:37 +0100 | [diff] [blame] | 2244 | __ LoadConst32(AT, High32Bits(kPositiveInfinityDouble)); |
Chris Larsen | 2714fe6 | 2016-02-11 14:23:53 -0800 | [diff] [blame] | 2245 | } else { |
| 2246 | __ Mfc1(TMP, in); |
Anton Kirilov | a3ffea2 | 2016-04-07 17:02:37 +0100 | [diff] [blame] | 2247 | __ LoadConst32(AT, kPositiveInfinityFloat); |
Chris Larsen | 2714fe6 | 2016-02-11 14:23:53 -0800 | [diff] [blame] | 2248 | } |
| 2249 | __ Xor(TMP, TMP, AT); |
| 2250 | |
| 2251 | __ Sll(TMP, TMP, 1); |
| 2252 | |
| 2253 | if (type == Primitive::kPrimDouble) { |
| 2254 | __ Mfc1(AT, in); |
| 2255 | __ Or(TMP, TMP, AT); |
| 2256 | } |
| 2257 | // If any of the significand bits are one, then the number is not infinite. |
| 2258 | __ Sltiu(out, TMP, 1); |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | // boolean java.lang.Float.isInfinite(float) |
| 2263 | void IntrinsicLocationsBuilderMIPS::VisitFloatIsInfinite(HInvoke* invoke) { |
| 2264 | CreateFPToIntLocations(arena_, invoke); |
| 2265 | } |
| 2266 | |
| 2267 | void IntrinsicCodeGeneratorMIPS::VisitFloatIsInfinite(HInvoke* invoke) { |
| 2268 | GenIsInfinite(invoke->GetLocations(), Primitive::kPrimFloat, IsR6(), GetAssembler()); |
| 2269 | } |
| 2270 | |
| 2271 | // boolean java.lang.Double.isInfinite(double) |
| 2272 | void IntrinsicLocationsBuilderMIPS::VisitDoubleIsInfinite(HInvoke* invoke) { |
| 2273 | CreateFPToIntLocations(arena_, invoke); |
| 2274 | } |
| 2275 | |
| 2276 | void IntrinsicCodeGeneratorMIPS::VisitDoubleIsInfinite(HInvoke* invoke) { |
| 2277 | GenIsInfinite(invoke->GetLocations(), Primitive::kPrimDouble, IsR6(), GetAssembler()); |
| 2278 | } |
| 2279 | |
Chris Larsen | 9775934 | 2016-02-16 17:10:40 -0800 | [diff] [blame] | 2280 | static void GenHighestOneBit(LocationSummary* locations, |
| 2281 | const Primitive::Type type, |
| 2282 | bool isR6, |
| 2283 | MipsAssembler* assembler) { |
| 2284 | DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong); |
| 2285 | |
| 2286 | if (type == Primitive::kPrimLong) { |
| 2287 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 2288 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 2289 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 2290 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 2291 | |
| 2292 | if (isR6) { |
| 2293 | __ ClzR6(TMP, in_hi); |
| 2294 | } else { |
| 2295 | __ ClzR2(TMP, in_hi); |
| 2296 | } |
| 2297 | __ LoadConst32(AT, 0x80000000); |
| 2298 | __ Srlv(out_hi, AT, TMP); |
| 2299 | __ And(out_hi, out_hi, in_hi); |
| 2300 | if (isR6) { |
| 2301 | __ ClzR6(TMP, in_lo); |
| 2302 | } else { |
| 2303 | __ ClzR2(TMP, in_lo); |
| 2304 | } |
| 2305 | __ Srlv(out_lo, AT, TMP); |
| 2306 | __ And(out_lo, out_lo, in_lo); |
| 2307 | if (isR6) { |
| 2308 | __ Seleqz(out_lo, out_lo, out_hi); |
| 2309 | } else { |
| 2310 | __ Movn(out_lo, ZERO, out_hi); |
| 2311 | } |
| 2312 | } else { |
| 2313 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 2314 | Register out = locations->Out().AsRegister<Register>(); |
| 2315 | |
| 2316 | if (isR6) { |
| 2317 | __ ClzR6(TMP, in); |
| 2318 | } else { |
| 2319 | __ ClzR2(TMP, in); |
| 2320 | } |
| 2321 | __ LoadConst32(AT, 0x80000000); |
| 2322 | __ Srlv(AT, AT, TMP); // Srlv shifts in the range of [0;31] bits (lower 5 bits of arg). |
| 2323 | __ And(out, AT, in); // So this is required for 0 (=shift by 32). |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | // int java.lang.Integer.highestOneBit(int) |
| 2328 | void IntrinsicLocationsBuilderMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) { |
| 2329 | CreateIntToIntLocations(arena_, invoke); |
| 2330 | } |
| 2331 | |
| 2332 | void IntrinsicCodeGeneratorMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) { |
| 2333 | GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler()); |
| 2334 | } |
| 2335 | |
| 2336 | // long java.lang.Long.highestOneBit(long) |
| 2337 | void IntrinsicLocationsBuilderMIPS::VisitLongHighestOneBit(HInvoke* invoke) { |
| 2338 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 2339 | } |
| 2340 | |
| 2341 | void IntrinsicCodeGeneratorMIPS::VisitLongHighestOneBit(HInvoke* invoke) { |
| 2342 | GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler()); |
| 2343 | } |
| 2344 | |
| 2345 | static void GenLowestOneBit(LocationSummary* locations, |
| 2346 | const Primitive::Type type, |
| 2347 | bool isR6, |
| 2348 | MipsAssembler* assembler) { |
| 2349 | DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong); |
| 2350 | |
| 2351 | if (type == Primitive::kPrimLong) { |
| 2352 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 2353 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 2354 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 2355 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 2356 | |
| 2357 | __ Subu(TMP, ZERO, in_lo); |
| 2358 | __ And(out_lo, TMP, in_lo); |
| 2359 | __ Subu(TMP, ZERO, in_hi); |
| 2360 | __ And(out_hi, TMP, in_hi); |
| 2361 | if (isR6) { |
| 2362 | __ Seleqz(out_hi, out_hi, out_lo); |
| 2363 | } else { |
| 2364 | __ Movn(out_hi, ZERO, out_lo); |
| 2365 | } |
| 2366 | } else { |
| 2367 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 2368 | Register out = locations->Out().AsRegister<Register>(); |
| 2369 | |
| 2370 | __ Subu(TMP, ZERO, in); |
| 2371 | __ And(out, TMP, in); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | // int java.lang.Integer.lowestOneBit(int) |
| 2376 | void IntrinsicLocationsBuilderMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 2377 | CreateIntToIntLocations(arena_, invoke); |
| 2378 | } |
| 2379 | |
| 2380 | void IntrinsicCodeGeneratorMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) { |
| 2381 | GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler()); |
| 2382 | } |
| 2383 | |
| 2384 | // long java.lang.Long.lowestOneBit(long) |
| 2385 | void IntrinsicLocationsBuilderMIPS::VisitLongLowestOneBit(HInvoke* invoke) { |
| 2386 | CreateIntToIntLocations(arena_, invoke); |
| 2387 | } |
| 2388 | |
| 2389 | void IntrinsicCodeGeneratorMIPS::VisitLongLowestOneBit(HInvoke* invoke) { |
| 2390 | GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler()); |
| 2391 | } |
| 2392 | |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2393 | // int java.lang.Math.round(float) |
| 2394 | void IntrinsicLocationsBuilderMIPS::VisitMathRoundFloat(HInvoke* invoke) { |
| 2395 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2396 | LocationSummary::kNoCall, |
| 2397 | kIntrinsified); |
| 2398 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 2399 | locations->AddTemp(Location::RequiresFpuRegister()); |
| 2400 | locations->SetOut(Location::RequiresRegister()); |
| 2401 | } |
| 2402 | |
| 2403 | void IntrinsicCodeGeneratorMIPS::VisitMathRoundFloat(HInvoke* invoke) { |
| 2404 | LocationSummary* locations = invoke->GetLocations(); |
| 2405 | MipsAssembler* assembler = GetAssembler(); |
| 2406 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 2407 | FRegister half = locations->GetTemp(0).AsFpuRegister<FRegister>(); |
| 2408 | Register out = locations->Out().AsRegister<Register>(); |
| 2409 | |
| 2410 | MipsLabel done; |
| 2411 | MipsLabel finite; |
| 2412 | MipsLabel add; |
| 2413 | |
| 2414 | // if (in.isNaN) { |
| 2415 | // return 0; |
| 2416 | // } |
| 2417 | // |
| 2418 | // out = floor.w.s(in); |
| 2419 | // |
| 2420 | // /* |
| 2421 | // * This "if" statement is only needed for the pre-R6 version of floor.w.s |
| 2422 | // * which outputs Integer.MAX_VALUE for negative numbers with magnitudes |
| 2423 | // * too large to fit in a 32-bit integer. |
| 2424 | // * |
| 2425 | // * Starting with MIPSR6, which always sets FCSR.NAN2008=1, negative |
| 2426 | // * numbers which are too large to be represented in a 32-bit signed |
| 2427 | // * integer will be processed by floor.w.s to output Integer.MIN_VALUE, |
| 2428 | // * and will no longer be processed by this "if" statement. |
| 2429 | // */ |
| 2430 | // if (out == Integer.MAX_VALUE) { |
| 2431 | // TMP = (in < 0.0f) ? 1 : 0; |
| 2432 | // /* |
| 2433 | // * If TMP is 1, then adding it to out will wrap its value from |
| 2434 | // * Integer.MAX_VALUE to Integer.MIN_VALUE. |
| 2435 | // */ |
| 2436 | // return out += TMP; |
| 2437 | // } |
| 2438 | // |
| 2439 | // /* |
| 2440 | // * For negative values not handled by the previous "if" statement the |
| 2441 | // * test here will correctly set the value of TMP. |
| 2442 | // */ |
| 2443 | // TMP = ((in - out) >= 0.5f) ? 1 : 0; |
| 2444 | // return out += TMP; |
| 2445 | |
| 2446 | // Test for NaN. |
| 2447 | if (IsR6()) { |
| 2448 | __ CmpUnS(FTMP, in, in); |
| 2449 | } else { |
| 2450 | __ CunS(in, in); |
| 2451 | } |
| 2452 | |
| 2453 | // Return zero for NaN. |
| 2454 | __ Move(out, ZERO); |
| 2455 | if (IsR6()) { |
| 2456 | __ Bc1nez(FTMP, &done); |
| 2457 | } else { |
| 2458 | __ Bc1t(&done); |
| 2459 | } |
| 2460 | |
| 2461 | // out = floor(in); |
| 2462 | __ FloorWS(FTMP, in); |
| 2463 | __ Mfc1(out, FTMP); |
| 2464 | |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2465 | if (!IsR6()) { |
| 2466 | __ LoadConst32(TMP, -1); |
| 2467 | } |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2468 | |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2469 | // TMP = (out = java.lang.Integer.MAX_VALUE) ? -1 : 0; |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2470 | __ LoadConst32(AT, std::numeric_limits<int32_t>::max()); |
| 2471 | __ Bne(AT, out, &finite); |
| 2472 | |
| 2473 | __ Mtc1(ZERO, FTMP); |
| 2474 | if (IsR6()) { |
| 2475 | __ CmpLtS(FTMP, in, FTMP); |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2476 | __ Mfc1(TMP, FTMP); |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2477 | } else { |
| 2478 | __ ColtS(in, FTMP); |
| 2479 | } |
| 2480 | |
| 2481 | __ B(&add); |
| 2482 | |
| 2483 | __ Bind(&finite); |
| 2484 | |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2485 | // TMP = (0.5f <= (in - out)) ? -1 : 0; |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2486 | __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float". |
| 2487 | __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f)); |
| 2488 | __ SubS(FTMP, in, FTMP); |
| 2489 | __ Mtc1(AT, half); |
| 2490 | if (IsR6()) { |
| 2491 | __ CmpLeS(FTMP, half, FTMP); |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2492 | __ Mfc1(TMP, FTMP); |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2493 | } else { |
| 2494 | __ ColeS(half, FTMP); |
| 2495 | } |
| 2496 | |
| 2497 | __ Bind(&add); |
| 2498 | |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2499 | if (!IsR6()) { |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2500 | __ Movf(TMP, ZERO); |
| 2501 | } |
| 2502 | |
Chris Larsen | 07f712f | 2016-06-10 16:06:02 -0700 | [diff] [blame] | 2503 | // Return out -= TMP. |
| 2504 | __ Subu(out, out, TMP); |
Chris Larsen | f09d532 | 2016-04-22 12:06:34 -0700 | [diff] [blame] | 2505 | |
| 2506 | __ Bind(&done); |
| 2507 | } |
| 2508 | |
Chris Larsen | 692235e | 2016-11-21 16:04:53 -0800 | [diff] [blame] | 2509 | // void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
| 2510 | void IntrinsicLocationsBuilderMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 2511 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 2512 | LocationSummary::kCallOnMainOnly, |
| 2513 | kIntrinsified); |
| 2514 | locations->SetInAt(0, Location::RequiresRegister()); |
| 2515 | locations->SetInAt(1, Location::RequiresRegister()); |
| 2516 | locations->SetInAt(2, Location::RequiresRegister()); |
| 2517 | locations->SetInAt(3, Location::RequiresRegister()); |
| 2518 | locations->SetInAt(4, Location::RequiresRegister()); |
| 2519 | |
| 2520 | // We will call memcpy() to do the actual work. Allocate the temporary |
| 2521 | // registers to use the correct input registers, and output register. |
| 2522 | // memcpy() uses the normal MIPS calling convention. |
| 2523 | InvokeRuntimeCallingConvention calling_convention; |
| 2524 | |
| 2525 | locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); |
| 2526 | locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1))); |
| 2527 | locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2))); |
| 2528 | |
| 2529 | Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt); |
| 2530 | locations->AddTemp(Location::RegisterLocation(outLocation.AsRegister<Register>())); |
| 2531 | } |
| 2532 | |
| 2533 | void IntrinsicCodeGeneratorMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) { |
| 2534 | MipsAssembler* assembler = GetAssembler(); |
| 2535 | LocationSummary* locations = invoke->GetLocations(); |
| 2536 | |
| 2537 | // Check assumption that sizeof(Char) is 2 (used in scaling below). |
| 2538 | const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar); |
| 2539 | DCHECK_EQ(char_size, 2u); |
| 2540 | const size_t char_shift = Primitive::ComponentSizeShift(Primitive::kPrimChar); |
| 2541 | |
| 2542 | Register srcObj = locations->InAt(0).AsRegister<Register>(); |
| 2543 | Register srcBegin = locations->InAt(1).AsRegister<Register>(); |
| 2544 | Register srcEnd = locations->InAt(2).AsRegister<Register>(); |
| 2545 | Register dstObj = locations->InAt(3).AsRegister<Register>(); |
| 2546 | Register dstBegin = locations->InAt(4).AsRegister<Register>(); |
| 2547 | |
| 2548 | Register dstPtr = locations->GetTemp(0).AsRegister<Register>(); |
| 2549 | DCHECK_EQ(dstPtr, A0); |
| 2550 | Register srcPtr = locations->GetTemp(1).AsRegister<Register>(); |
| 2551 | DCHECK_EQ(srcPtr, A1); |
| 2552 | Register numChrs = locations->GetTemp(2).AsRegister<Register>(); |
| 2553 | DCHECK_EQ(numChrs, A2); |
| 2554 | |
| 2555 | Register dstReturn = locations->GetTemp(3).AsRegister<Register>(); |
| 2556 | DCHECK_EQ(dstReturn, V0); |
| 2557 | |
| 2558 | MipsLabel done; |
| 2559 | |
| 2560 | // Location of data in char array buffer. |
| 2561 | const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value(); |
| 2562 | |
| 2563 | // Get offset of value field within a string object. |
| 2564 | const int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 2565 | |
| 2566 | __ Beq(srcEnd, srcBegin, &done); // No characters to move. |
| 2567 | |
| 2568 | // Calculate number of characters to be copied. |
| 2569 | __ Subu(numChrs, srcEnd, srcBegin); |
| 2570 | |
| 2571 | // Calculate destination address. |
| 2572 | __ Addiu(dstPtr, dstObj, data_offset); |
| 2573 | if (IsR6()) { |
| 2574 | __ Lsa(dstPtr, dstBegin, dstPtr, char_shift); |
| 2575 | } else { |
| 2576 | __ Sll(AT, dstBegin, char_shift); |
| 2577 | __ Addu(dstPtr, dstPtr, AT); |
| 2578 | } |
| 2579 | |
| 2580 | // Calculate source address. |
| 2581 | __ Addiu(srcPtr, srcObj, value_offset); |
| 2582 | if (IsR6()) { |
| 2583 | __ Lsa(srcPtr, srcBegin, srcPtr, char_shift); |
| 2584 | } else { |
| 2585 | __ Sll(AT, srcBegin, char_shift); |
| 2586 | __ Addu(srcPtr, srcPtr, AT); |
| 2587 | } |
| 2588 | |
| 2589 | // Calculate number of bytes to copy from number of characters. |
| 2590 | __ Sll(numChrs, numChrs, char_shift); |
| 2591 | |
| 2592 | codegen_->InvokeRuntime(kQuickMemcpy, invoke, invoke->GetDexPc(), nullptr); |
| 2593 | |
| 2594 | __ Bind(&done); |
| 2595 | } |
| 2596 | |
Chris Larsen | 2714fe6 | 2016-02-11 14:23:53 -0800 | [diff] [blame] | 2597 | // Unimplemented intrinsics. |
| 2598 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2599 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCeil) |
| 2600 | UNIMPLEMENTED_INTRINSIC(MIPS, MathFloor) |
| 2601 | UNIMPLEMENTED_INTRINSIC(MIPS, MathRint) |
| 2602 | UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundDouble) |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2603 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASLong) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 2604 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2605 | UNIMPLEMENTED_INTRINSIC(MIPS, ReferenceGetReferent) |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2606 | UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopyChar) |
| 2607 | UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopy) |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 2608 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 2609 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCos) |
| 2610 | UNIMPLEMENTED_INTRINSIC(MIPS, MathSin) |
| 2611 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAcos) |
| 2612 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAsin) |
| 2613 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAtan) |
| 2614 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAtan2) |
| 2615 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCbrt) |
| 2616 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCosh) |
| 2617 | UNIMPLEMENTED_INTRINSIC(MIPS, MathExp) |
| 2618 | UNIMPLEMENTED_INTRINSIC(MIPS, MathExpm1) |
| 2619 | UNIMPLEMENTED_INTRINSIC(MIPS, MathHypot) |
| 2620 | UNIMPLEMENTED_INTRINSIC(MIPS, MathLog) |
| 2621 | UNIMPLEMENTED_INTRINSIC(MIPS, MathLog10) |
| 2622 | UNIMPLEMENTED_INTRINSIC(MIPS, MathNextAfter) |
| 2623 | UNIMPLEMENTED_INTRINSIC(MIPS, MathSinh) |
| 2624 | UNIMPLEMENTED_INTRINSIC(MIPS, MathTan) |
| 2625 | UNIMPLEMENTED_INTRINSIC(MIPS, MathTanh) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 2626 | |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 2627 | UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOf); |
| 2628 | UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOfAfter); |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2629 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferAppend); |
| 2630 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferLength); |
| 2631 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferToString); |
| 2632 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderAppend); |
| 2633 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderLength); |
| 2634 | UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderToString); |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 2635 | |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 2636 | // 1.8. |
| 2637 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddInt) |
| 2638 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddLong) |
| 2639 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetInt) |
| 2640 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetLong) |
| 2641 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetObject) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 2642 | |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 2643 | UNREACHABLE_INTRINSICS(MIPS) |
Chris Larsen | 2714fe6 | 2016-02-11 14:23:53 -0800 | [diff] [blame] | 2644 | |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 2645 | #undef __ |
| 2646 | |
| 2647 | } // namespace mips |
| 2648 | } // namespace art |