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, |
| 493 | bool isR2OrNewer, |
| 494 | MipsAssembler* assembler) { |
| 495 | Register out = locations->Out().AsRegister<Register>(); |
| 496 | Register in_lo; |
| 497 | Register in; |
| 498 | |
| 499 | if (is64bit) { |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 500 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 501 | |
| 502 | in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 503 | |
| 504 | // If in_lo is zero then count the number of trailing zeroes in in_hi; |
| 505 | // otherwise count the number of trailing zeroes in in_lo. |
| 506 | // AT = in_lo ? in_lo : in_hi; |
| 507 | if (isR6) { |
| 508 | __ Seleqz(out, in_hi, in_lo); |
| 509 | __ Selnez(TMP, in_lo, in_lo); |
| 510 | __ Or(out, out, TMP); |
| 511 | } else { |
| 512 | __ Movz(out, in_hi, in_lo); |
| 513 | __ Movn(out, in_lo, in_lo); |
| 514 | } |
| 515 | |
| 516 | in = out; |
| 517 | } else { |
| 518 | in = locations->InAt(0).AsRegister<Register>(); |
| 519 | // Give in_lo a dummy value to keep the compiler from complaining. |
| 520 | // Since we only get here in the 32-bit case, this value will never |
| 521 | // be used. |
| 522 | in_lo = in; |
| 523 | } |
| 524 | |
| 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. |
| 528 | if (isR2OrNewer) { |
| 529 | __ Rotr(out, in, 16); |
| 530 | __ Wsbh(out, out); |
| 531 | } else { |
| 532 | // MIPS32r1 |
| 533 | // __ Rotr(out, in, 16); |
| 534 | __ Sll(TMP, in, 16); |
| 535 | __ Srl(out, in, 16); |
| 536 | __ Or(out, out, TMP); |
| 537 | // __ Wsbh(out, out); |
| 538 | __ LoadConst32(AT, 0x00FF00FF); |
| 539 | __ And(TMP, out, AT); |
| 540 | __ Sll(TMP, TMP, 8); |
| 541 | __ Srl(out, out, 8); |
| 542 | __ And(out, out, AT); |
| 543 | __ Or(out, out, TMP); |
| 544 | } |
| 545 | |
| 546 | if (isR6) { |
| 547 | __ Bitswap(out, out); |
| 548 | __ ClzR6(out, out); |
| 549 | } else { |
| 550 | __ LoadConst32(AT, 0x0F0F0F0F); |
| 551 | __ And(TMP, out, AT); |
| 552 | __ Sll(TMP, TMP, 4); |
| 553 | __ Srl(out, out, 4); |
| 554 | __ And(out, out, AT); |
| 555 | __ Or(out, TMP, out); |
| 556 | __ LoadConst32(AT, 0x33333333); |
| 557 | __ And(TMP, out, AT); |
| 558 | __ Sll(TMP, TMP, 2); |
| 559 | __ Srl(out, out, 2); |
| 560 | __ And(out, out, AT); |
| 561 | __ Or(out, TMP, out); |
| 562 | __ LoadConst32(AT, 0x55555555); |
| 563 | __ And(TMP, out, AT); |
| 564 | __ Sll(TMP, TMP, 1); |
| 565 | __ Srl(out, out, 1); |
| 566 | __ And(out, out, AT); |
| 567 | __ Or(out, TMP, out); |
| 568 | __ ClzR2(out, out); |
| 569 | } |
| 570 | |
| 571 | if (is64bit) { |
| 572 | // If in_lo is zero, then we counted the number of trailing zeroes in in_hi so we must add the |
| 573 | // number of trailing zeroes in in_lo (32) to get the correct final count |
| 574 | __ LoadConst32(TMP, 32); |
| 575 | if (isR6) { |
| 576 | __ Seleqz(TMP, TMP, in_lo); |
| 577 | } else { |
| 578 | __ Movn(TMP, ZERO, in_lo); |
| 579 | } |
| 580 | __ Addu(out, out, TMP); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // int java.lang.Integer.numberOfTrailingZeros(int i) |
| 585 | void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
| 586 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 587 | } |
| 588 | |
| 589 | void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) { |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 590 | GenNumberOfTrailingZeroes(invoke->GetLocations(), |
| 591 | /* is64bit */ false, |
| 592 | IsR6(), |
| 593 | IsR2OrNewer(), |
| 594 | GetAssembler()); |
Chris Larsen | 8682960 | 2015-11-18 12:27:52 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // int java.lang.Long.numberOfTrailingZeros(long i) |
| 598 | void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
| 599 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 600 | } |
| 601 | |
| 602 | void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) { |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 603 | GenNumberOfTrailingZeroes(invoke->GetLocations(), |
| 604 | /* is64bit */ true, |
| 605 | IsR6(), |
| 606 | IsR2OrNewer(), |
| 607 | GetAssembler()); |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 610 | // int java.lang.Integer.reverse(int) |
| 611 | void IntrinsicLocationsBuilderMIPS::VisitIntegerReverse(HInvoke* invoke) { |
| 612 | CreateIntToIntLocations(arena_, invoke); |
| 613 | } |
| 614 | |
| 615 | void IntrinsicCodeGeneratorMIPS::VisitIntegerReverse(HInvoke* invoke) { |
| 616 | GenReverse(invoke->GetLocations(), |
| 617 | Primitive::kPrimInt, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 618 | IsR2OrNewer(), |
| 619 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 620 | /* reverseBits */ true, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 621 | GetAssembler()); |
| 622 | } |
| 623 | |
| 624 | // long java.lang.Long.reverse(long) |
| 625 | void IntrinsicLocationsBuilderMIPS::VisitLongReverse(HInvoke* invoke) { |
| 626 | CreateIntToIntLocations(arena_, invoke); |
| 627 | } |
| 628 | |
| 629 | void IntrinsicCodeGeneratorMIPS::VisitLongReverse(HInvoke* invoke) { |
| 630 | GenReverse(invoke->GetLocations(), |
| 631 | Primitive::kPrimLong, |
Chris Larsen | e16ce5a | 2015-11-18 12:30:20 -0800 | [diff] [blame] | 632 | IsR2OrNewer(), |
| 633 | IsR6(), |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 634 | /* reverseBits */ true, |
Chris Larsen | 70014c8 | 2015-11-18 12:26:08 -0800 | [diff] [blame] | 635 | GetAssembler()); |
Chris Larsen | 3f8bf65 | 2015-10-28 10:08:56 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Chris Larsen | b74353a | 2015-11-20 09:07:09 -0800 | [diff] [blame] | 638 | static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 639 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 640 | LocationSummary::kNoCall, |
| 641 | kIntrinsified); |
| 642 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 643 | locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); |
| 644 | } |
| 645 | |
| 646 | static void MathAbsFP(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) { |
| 647 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 648 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 649 | |
| 650 | if (is64bit) { |
| 651 | __ AbsD(out, in); |
| 652 | } else { |
| 653 | __ AbsS(out, in); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // double java.lang.Math.abs(double) |
| 658 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsDouble(HInvoke* invoke) { |
| 659 | CreateFPToFPLocations(arena_, invoke); |
| 660 | } |
| 661 | |
| 662 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsDouble(HInvoke* invoke) { |
| 663 | MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
| 664 | } |
| 665 | |
| 666 | // float java.lang.Math.abs(float) |
| 667 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsFloat(HInvoke* invoke) { |
| 668 | CreateFPToFPLocations(arena_, invoke); |
| 669 | } |
| 670 | |
| 671 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsFloat(HInvoke* invoke) { |
| 672 | MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
| 673 | } |
| 674 | |
| 675 | static void GenAbsInteger(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) { |
| 676 | if (is64bit) { |
| 677 | Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 678 | Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 679 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 680 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 681 | |
| 682 | // The comments in this section show the analogous operations which would |
| 683 | // be performed if we had 64-bit registers "in", and "out". |
| 684 | // __ Dsra32(AT, in, 31); |
| 685 | __ Sra(AT, in_hi, 31); |
| 686 | // __ Xor(out, in, AT); |
| 687 | __ Xor(TMP, in_lo, AT); |
| 688 | __ Xor(out_hi, in_hi, AT); |
| 689 | // __ Dsubu(out, out, AT); |
| 690 | __ Subu(out_lo, TMP, AT); |
| 691 | __ Sltu(TMP, out_lo, TMP); |
| 692 | __ Addu(out_hi, out_hi, TMP); |
| 693 | } else { |
| 694 | Register in = locations->InAt(0).AsRegister<Register>(); |
| 695 | Register out = locations->Out().AsRegister<Register>(); |
| 696 | |
| 697 | __ Sra(AT, in, 31); |
| 698 | __ Xor(out, in, AT); |
| 699 | __ Subu(out, out, AT); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // int java.lang.Math.abs(int) |
| 704 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsInt(HInvoke* invoke) { |
| 705 | CreateIntToIntLocations(arena_, invoke); |
| 706 | } |
| 707 | |
| 708 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsInt(HInvoke* invoke) { |
| 709 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler()); |
| 710 | } |
| 711 | |
| 712 | // long java.lang.Math.abs(long) |
| 713 | void IntrinsicLocationsBuilderMIPS::VisitMathAbsLong(HInvoke* invoke) { |
| 714 | CreateIntToIntLocations(arena_, invoke); |
| 715 | } |
| 716 | |
| 717 | void IntrinsicCodeGeneratorMIPS::VisitMathAbsLong(HInvoke* invoke) { |
| 718 | GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler()); |
| 719 | } |
| 720 | |
| 721 | static void GenMinMaxFP(LocationSummary* locations, |
| 722 | bool is_min, |
| 723 | Primitive::Type type, |
| 724 | bool is_R6, |
| 725 | MipsAssembler* assembler) { |
| 726 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 727 | FRegister a = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 728 | FRegister b = locations->InAt(1).AsFpuRegister<FRegister>(); |
| 729 | |
| 730 | if (is_R6) { |
| 731 | MipsLabel noNaNs; |
| 732 | MipsLabel done; |
| 733 | FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP; |
| 734 | |
| 735 | // When Java computes min/max it prefers a NaN to a number; the |
| 736 | // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of |
| 737 | // the inputs is a NaN and the other is a valid number, the MIPS |
| 738 | // instruction will return the number; Java wants the NaN value |
| 739 | // returned. This is why there is extra logic preceding the use of |
| 740 | // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a |
| 741 | // NaN, return the NaN, otherwise return the min/max. |
| 742 | if (type == Primitive::kPrimDouble) { |
| 743 | __ CmpUnD(FTMP, a, b); |
| 744 | __ Bc1eqz(FTMP, &noNaNs); |
| 745 | |
| 746 | // One of the inputs is a NaN |
| 747 | __ CmpEqD(ftmp, a, a); |
| 748 | // If a == a then b is the NaN, otherwise a is the NaN. |
| 749 | __ SelD(ftmp, a, b); |
| 750 | |
| 751 | if (ftmp != out) { |
| 752 | __ MovD(out, ftmp); |
| 753 | } |
| 754 | |
| 755 | __ B(&done); |
| 756 | |
| 757 | __ Bind(&noNaNs); |
| 758 | |
| 759 | if (is_min) { |
| 760 | __ MinD(out, a, b); |
| 761 | } else { |
| 762 | __ MaxD(out, a, b); |
| 763 | } |
| 764 | } else { |
| 765 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 766 | __ CmpUnS(FTMP, a, b); |
| 767 | __ Bc1eqz(FTMP, &noNaNs); |
| 768 | |
| 769 | // One of the inputs is a NaN |
| 770 | __ CmpEqS(ftmp, a, a); |
| 771 | // If a == a then b is the NaN, otherwise a is the NaN. |
| 772 | __ SelS(ftmp, a, b); |
| 773 | |
| 774 | if (ftmp != out) { |
| 775 | __ MovS(out, ftmp); |
| 776 | } |
| 777 | |
| 778 | __ B(&done); |
| 779 | |
| 780 | __ Bind(&noNaNs); |
| 781 | |
| 782 | if (is_min) { |
| 783 | __ MinS(out, a, b); |
| 784 | } else { |
| 785 | __ MaxS(out, a, b); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | __ Bind(&done); |
| 790 | } else { |
| 791 | MipsLabel ordered; |
| 792 | MipsLabel compare; |
| 793 | MipsLabel select; |
| 794 | MipsLabel done; |
| 795 | |
| 796 | if (type == Primitive::kPrimDouble) { |
| 797 | __ CunD(a, b); |
| 798 | } else { |
| 799 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 800 | __ CunS(a, b); |
| 801 | } |
| 802 | __ Bc1f(&ordered); |
| 803 | |
| 804 | // a or b (or both) is a NaN. Return one, which is a NaN. |
| 805 | if (type == Primitive::kPrimDouble) { |
| 806 | __ CeqD(b, b); |
| 807 | } else { |
| 808 | __ CeqS(b, b); |
| 809 | } |
| 810 | __ B(&select); |
| 811 | |
| 812 | __ Bind(&ordered); |
| 813 | |
| 814 | // Neither is a NaN. |
| 815 | // a == b? (-0.0 compares equal with +0.0) |
| 816 | // If equal, handle zeroes, else compare further. |
| 817 | if (type == Primitive::kPrimDouble) { |
| 818 | __ CeqD(a, b); |
| 819 | } else { |
| 820 | __ CeqS(a, b); |
| 821 | } |
| 822 | __ Bc1f(&compare); |
| 823 | |
| 824 | // a == b either bit for bit or one is -0.0 and the other is +0.0. |
| 825 | if (type == Primitive::kPrimDouble) { |
| 826 | __ MoveFromFpuHigh(TMP, a); |
| 827 | __ MoveFromFpuHigh(AT, b); |
| 828 | } else { |
| 829 | __ Mfc1(TMP, a); |
| 830 | __ Mfc1(AT, b); |
| 831 | } |
| 832 | |
| 833 | if (is_min) { |
| 834 | // -0.0 prevails over +0.0. |
| 835 | __ Or(TMP, TMP, AT); |
| 836 | } else { |
| 837 | // +0.0 prevails over -0.0. |
| 838 | __ And(TMP, TMP, AT); |
| 839 | } |
| 840 | |
| 841 | if (type == Primitive::kPrimDouble) { |
| 842 | __ Mfc1(AT, a); |
| 843 | __ Mtc1(AT, out); |
| 844 | __ MoveToFpuHigh(TMP, out); |
| 845 | } else { |
| 846 | __ Mtc1(TMP, out); |
| 847 | } |
| 848 | __ B(&done); |
| 849 | |
| 850 | __ Bind(&compare); |
| 851 | |
| 852 | if (type == Primitive::kPrimDouble) { |
| 853 | if (is_min) { |
| 854 | // return (a <= b) ? a : b; |
| 855 | __ ColeD(a, b); |
| 856 | } else { |
| 857 | // return (a >= b) ? a : b; |
| 858 | __ ColeD(b, a); // b <= a |
| 859 | } |
| 860 | } else { |
| 861 | if (is_min) { |
| 862 | // return (a <= b) ? a : b; |
| 863 | __ ColeS(a, b); |
| 864 | } else { |
| 865 | // return (a >= b) ? a : b; |
| 866 | __ ColeS(b, a); // b <= a |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | __ Bind(&select); |
| 871 | |
| 872 | if (type == Primitive::kPrimDouble) { |
| 873 | __ MovtD(out, a); |
| 874 | __ MovfD(out, b); |
| 875 | } else { |
| 876 | __ MovtS(out, a); |
| 877 | __ MovfS(out, b); |
| 878 | } |
| 879 | |
| 880 | __ Bind(&done); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 885 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 886 | LocationSummary::kNoCall, |
| 887 | kIntrinsified); |
| 888 | locations->SetInAt(0, Location::RequiresFpuRegister()); |
| 889 | locations->SetInAt(1, Location::RequiresFpuRegister()); |
| 890 | locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap); |
| 891 | } |
| 892 | |
| 893 | // double java.lang.Math.min(double, double) |
| 894 | void IntrinsicLocationsBuilderMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) { |
| 895 | CreateFPFPToFPLocations(arena_, invoke); |
| 896 | } |
| 897 | |
| 898 | void IntrinsicCodeGeneratorMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) { |
| 899 | GenMinMaxFP(invoke->GetLocations(), |
| 900 | /* is_min */ true, |
| 901 | Primitive::kPrimDouble, |
| 902 | IsR6(), |
| 903 | GetAssembler()); |
| 904 | } |
| 905 | |
| 906 | // float java.lang.Math.min(float, float) |
| 907 | void IntrinsicLocationsBuilderMIPS::VisitMathMinFloatFloat(HInvoke* invoke) { |
| 908 | CreateFPFPToFPLocations(arena_, invoke); |
| 909 | } |
| 910 | |
| 911 | void IntrinsicCodeGeneratorMIPS::VisitMathMinFloatFloat(HInvoke* invoke) { |
| 912 | GenMinMaxFP(invoke->GetLocations(), |
| 913 | /* is_min */ true, |
| 914 | Primitive::kPrimFloat, |
| 915 | IsR6(), |
| 916 | GetAssembler()); |
| 917 | } |
| 918 | |
| 919 | // double java.lang.Math.max(double, double) |
| 920 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
| 921 | CreateFPFPToFPLocations(arena_, invoke); |
| 922 | } |
| 923 | |
| 924 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) { |
| 925 | GenMinMaxFP(invoke->GetLocations(), |
| 926 | /* is_min */ false, |
| 927 | Primitive::kPrimDouble, |
| 928 | IsR6(), |
| 929 | GetAssembler()); |
| 930 | } |
| 931 | |
| 932 | // float java.lang.Math.max(float, float) |
| 933 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) { |
| 934 | CreateFPFPToFPLocations(arena_, invoke); |
| 935 | } |
| 936 | |
| 937 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) { |
| 938 | GenMinMaxFP(invoke->GetLocations(), |
| 939 | /* is_min */ false, |
| 940 | Primitive::kPrimFloat, |
| 941 | IsR6(), |
| 942 | GetAssembler()); |
| 943 | } |
| 944 | |
| 945 | static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 946 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 947 | LocationSummary::kNoCall, |
| 948 | kIntrinsified); |
| 949 | locations->SetInAt(0, Location::RequiresRegister()); |
| 950 | locations->SetInAt(1, Location::RequiresRegister()); |
| 951 | locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); |
| 952 | } |
| 953 | |
| 954 | static void GenMinMax(LocationSummary* locations, |
| 955 | bool is_min, |
| 956 | Primitive::Type type, |
| 957 | bool is_R6, |
| 958 | MipsAssembler* assembler) { |
| 959 | if (is_R6) { |
| 960 | // Some architectures, such as ARM and MIPS (prior to r6), have a |
| 961 | // conditional move instruction which only changes the target |
| 962 | // (output) register if the condition is true (MIPS prior to r6 had |
| 963 | // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions |
| 964 | // always change the target (output) register. If the condition is |
| 965 | // true the output register gets the contents of the "rs" register; |
| 966 | // otherwise, the output register is set to zero. One consequence |
| 967 | // of this is that to implement something like "rd = c==0 ? rs : rt" |
| 968 | // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions. |
| 969 | // After executing this pair of instructions one of the output |
| 970 | // registers from the pair will necessarily contain zero. Then the |
| 971 | // code ORs the output registers from the SELEQZ/SELNEZ instructions |
| 972 | // to get the final result. |
| 973 | // |
| 974 | // The initial test to see if the output register is same as the |
| 975 | // first input register is needed to make sure that value in the |
| 976 | // first input register isn't clobbered before we've finished |
| 977 | // computing the output value. The logic in the corresponding else |
| 978 | // clause performs the same task but makes sure the second input |
| 979 | // register isn't clobbered in the event that it's the same register |
| 980 | // as the output register; the else clause also handles the case |
| 981 | // where the output register is distinct from both the first, and the |
| 982 | // second input registers. |
| 983 | if (type == Primitive::kPrimLong) { |
| 984 | Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 985 | Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 986 | Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>(); |
| 987 | Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>(); |
| 988 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 989 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 990 | |
| 991 | MipsLabel compare_done; |
| 992 | |
| 993 | if (a_lo == b_lo) { |
| 994 | if (out_lo != a_lo) { |
| 995 | __ Move(out_lo, a_lo); |
| 996 | __ Move(out_hi, a_hi); |
| 997 | } |
| 998 | } else { |
| 999 | __ Slt(TMP, b_hi, a_hi); |
| 1000 | __ Bne(b_hi, a_hi, &compare_done); |
| 1001 | |
| 1002 | __ Sltu(TMP, b_lo, a_lo); |
| 1003 | |
| 1004 | __ Bind(&compare_done); |
| 1005 | |
| 1006 | if (is_min) { |
| 1007 | __ Seleqz(AT, a_lo, TMP); |
| 1008 | __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo |
| 1009 | // because at this point we're |
| 1010 | // done using a_lo/b_lo. |
| 1011 | } else { |
| 1012 | __ Selnez(AT, a_lo, TMP); |
| 1013 | __ Seleqz(out_lo, b_lo, TMP); // ditto |
| 1014 | } |
| 1015 | __ Or(out_lo, out_lo, AT); |
| 1016 | if (is_min) { |
| 1017 | __ Seleqz(AT, a_hi, TMP); |
| 1018 | __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi |
| 1019 | } else { |
| 1020 | __ Selnez(AT, a_hi, TMP); |
| 1021 | __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi |
| 1022 | } |
| 1023 | __ Or(out_hi, out_hi, AT); |
| 1024 | } |
| 1025 | } else { |
| 1026 | DCHECK_EQ(type, Primitive::kPrimInt); |
| 1027 | Register a = locations->InAt(0).AsRegister<Register>(); |
| 1028 | Register b = locations->InAt(1).AsRegister<Register>(); |
| 1029 | Register out = locations->Out().AsRegister<Register>(); |
| 1030 | |
| 1031 | if (a == b) { |
| 1032 | if (out != a) { |
| 1033 | __ Move(out, a); |
| 1034 | } |
| 1035 | } else { |
| 1036 | __ Slt(AT, b, a); |
| 1037 | if (is_min) { |
| 1038 | __ Seleqz(TMP, a, AT); |
| 1039 | __ Selnez(AT, b, AT); |
| 1040 | } else { |
| 1041 | __ Selnez(TMP, a, AT); |
| 1042 | __ Seleqz(AT, b, AT); |
| 1043 | } |
| 1044 | __ Or(out, TMP, AT); |
| 1045 | } |
| 1046 | } |
| 1047 | } else { |
| 1048 | if (type == Primitive::kPrimLong) { |
| 1049 | Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>(); |
| 1050 | Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>(); |
| 1051 | Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>(); |
| 1052 | Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>(); |
| 1053 | Register out_lo = locations->Out().AsRegisterPairLow<Register>(); |
| 1054 | Register out_hi = locations->Out().AsRegisterPairHigh<Register>(); |
| 1055 | |
| 1056 | MipsLabel compare_done; |
| 1057 | |
| 1058 | if (a_lo == b_lo) { |
| 1059 | if (out_lo != a_lo) { |
| 1060 | __ Move(out_lo, a_lo); |
| 1061 | __ Move(out_hi, a_hi); |
| 1062 | } |
| 1063 | } else { |
| 1064 | __ Slt(TMP, a_hi, b_hi); |
| 1065 | __ Bne(a_hi, b_hi, &compare_done); |
| 1066 | |
| 1067 | __ Sltu(TMP, a_lo, b_lo); |
| 1068 | |
| 1069 | __ Bind(&compare_done); |
| 1070 | |
| 1071 | if (is_min) { |
| 1072 | if (out_lo != a_lo) { |
| 1073 | __ Movn(out_hi, a_hi, TMP); |
| 1074 | __ Movn(out_lo, a_lo, TMP); |
| 1075 | } |
| 1076 | if (out_lo != b_lo) { |
| 1077 | __ Movz(out_hi, b_hi, TMP); |
| 1078 | __ Movz(out_lo, b_lo, TMP); |
| 1079 | } |
| 1080 | } else { |
| 1081 | if (out_lo != a_lo) { |
| 1082 | __ Movz(out_hi, a_hi, TMP); |
| 1083 | __ Movz(out_lo, a_lo, TMP); |
| 1084 | } |
| 1085 | if (out_lo != b_lo) { |
| 1086 | __ Movn(out_hi, b_hi, TMP); |
| 1087 | __ Movn(out_lo, b_lo, TMP); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } else { |
| 1092 | DCHECK_EQ(type, Primitive::kPrimInt); |
| 1093 | Register a = locations->InAt(0).AsRegister<Register>(); |
| 1094 | Register b = locations->InAt(1).AsRegister<Register>(); |
| 1095 | Register out = locations->Out().AsRegister<Register>(); |
| 1096 | |
| 1097 | if (a == b) { |
| 1098 | if (out != a) { |
| 1099 | __ Move(out, a); |
| 1100 | } |
| 1101 | } else { |
| 1102 | __ Slt(AT, a, b); |
| 1103 | if (is_min) { |
| 1104 | if (out != a) { |
| 1105 | __ Movn(out, a, AT); |
| 1106 | } |
| 1107 | if (out != b) { |
| 1108 | __ Movz(out, b, AT); |
| 1109 | } |
| 1110 | } else { |
| 1111 | if (out != a) { |
| 1112 | __ Movz(out, a, AT); |
| 1113 | } |
| 1114 | if (out != b) { |
| 1115 | __ Movn(out, b, AT); |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | // int java.lang.Math.min(int, int) |
| 1124 | void IntrinsicLocationsBuilderMIPS::VisitMathMinIntInt(HInvoke* invoke) { |
| 1125 | CreateIntIntToIntLocations(arena_, invoke); |
| 1126 | } |
| 1127 | |
| 1128 | void IntrinsicCodeGeneratorMIPS::VisitMathMinIntInt(HInvoke* invoke) { |
| 1129 | GenMinMax(invoke->GetLocations(), |
| 1130 | /* is_min */ true, |
| 1131 | Primitive::kPrimInt, |
| 1132 | IsR6(), |
| 1133 | GetAssembler()); |
| 1134 | } |
| 1135 | |
| 1136 | // long java.lang.Math.min(long, long) |
| 1137 | void IntrinsicLocationsBuilderMIPS::VisitMathMinLongLong(HInvoke* invoke) { |
| 1138 | CreateIntIntToIntLocations(arena_, invoke); |
| 1139 | } |
| 1140 | |
| 1141 | void IntrinsicCodeGeneratorMIPS::VisitMathMinLongLong(HInvoke* invoke) { |
| 1142 | GenMinMax(invoke->GetLocations(), |
| 1143 | /* is_min */ true, |
| 1144 | Primitive::kPrimLong, |
| 1145 | IsR6(), |
| 1146 | GetAssembler()); |
| 1147 | } |
| 1148 | |
| 1149 | // int java.lang.Math.max(int, int) |
| 1150 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxIntInt(HInvoke* invoke) { |
| 1151 | CreateIntIntToIntLocations(arena_, invoke); |
| 1152 | } |
| 1153 | |
| 1154 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxIntInt(HInvoke* invoke) { |
| 1155 | GenMinMax(invoke->GetLocations(), |
| 1156 | /* is_min */ false, |
| 1157 | Primitive::kPrimInt, |
| 1158 | IsR6(), |
| 1159 | GetAssembler()); |
| 1160 | } |
| 1161 | |
| 1162 | // long java.lang.Math.max(long, long) |
| 1163 | void IntrinsicLocationsBuilderMIPS::VisitMathMaxLongLong(HInvoke* invoke) { |
| 1164 | CreateIntIntToIntLocations(arena_, invoke); |
| 1165 | } |
| 1166 | |
| 1167 | void IntrinsicCodeGeneratorMIPS::VisitMathMaxLongLong(HInvoke* invoke) { |
| 1168 | GenMinMax(invoke->GetLocations(), |
| 1169 | /* is_min */ false, |
| 1170 | Primitive::kPrimLong, |
| 1171 | IsR6(), |
| 1172 | GetAssembler()); |
| 1173 | } |
| 1174 | |
| 1175 | // double java.lang.Math.sqrt(double) |
| 1176 | void IntrinsicLocationsBuilderMIPS::VisitMathSqrt(HInvoke* invoke) { |
| 1177 | CreateFPToFPLocations(arena_, invoke); |
| 1178 | } |
| 1179 | |
| 1180 | void IntrinsicCodeGeneratorMIPS::VisitMathSqrt(HInvoke* invoke) { |
| 1181 | LocationSummary* locations = invoke->GetLocations(); |
| 1182 | MipsAssembler* assembler = GetAssembler(); |
| 1183 | FRegister in = locations->InAt(0).AsFpuRegister<FRegister>(); |
| 1184 | FRegister out = locations->Out().AsFpuRegister<FRegister>(); |
| 1185 | |
| 1186 | __ SqrtD(out, in); |
| 1187 | } |
| 1188 | |
Chris Larsen | 3acee73 | 2015-11-18 13:31:08 -0800 | [diff] [blame] | 1189 | // byte libcore.io.Memory.peekByte(long address) |
| 1190 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1191 | CreateIntToIntLocations(arena_, invoke); |
| 1192 | } |
| 1193 | |
| 1194 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekByte(HInvoke* invoke) { |
| 1195 | MipsAssembler* assembler = GetAssembler(); |
| 1196 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1197 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1198 | |
| 1199 | __ Lb(out, adr, 0); |
| 1200 | } |
| 1201 | |
| 1202 | // short libcore.io.Memory.peekShort(long address) |
| 1203 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1204 | CreateIntToIntLocations(arena_, invoke); |
| 1205 | } |
| 1206 | |
| 1207 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) { |
| 1208 | MipsAssembler* assembler = GetAssembler(); |
| 1209 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1210 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1211 | |
| 1212 | if (IsR6()) { |
| 1213 | __ Lh(out, adr, 0); |
| 1214 | } else if (IsR2OrNewer()) { |
| 1215 | // Unlike for words, there are no lhl/lhr instructions to load |
| 1216 | // unaligned halfwords so the code loads individual bytes, in case |
| 1217 | // the address isn't halfword-aligned, and assembles them into a |
| 1218 | // signed halfword. |
| 1219 | __ Lb(AT, adr, 1); // This byte must be sign-extended. |
| 1220 | __ Lb(out, adr, 0); // This byte can be either sign-extended, or |
| 1221 | // zero-extended because the following |
| 1222 | // instruction overwrites the sign bits. |
| 1223 | __ Ins(out, AT, 8, 24); |
| 1224 | } else { |
| 1225 | __ Lbu(AT, adr, 0); // This byte must be zero-extended. If it's not |
| 1226 | // the "or" instruction below will destroy the upper |
| 1227 | // 24 bits of the final result. |
| 1228 | __ Lb(out, adr, 1); // This byte must be sign-extended. |
| 1229 | __ Sll(out, out, 8); |
| 1230 | __ Or(out, out, AT); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | // int libcore.io.Memory.peekInt(long address) |
| 1235 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1236 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 1237 | } |
| 1238 | |
| 1239 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) { |
| 1240 | MipsAssembler* assembler = GetAssembler(); |
| 1241 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1242 | Register out = invoke->GetLocations()->Out().AsRegister<Register>(); |
| 1243 | |
| 1244 | if (IsR6()) { |
| 1245 | __ Lw(out, adr, 0); |
| 1246 | } else { |
| 1247 | __ Lwr(out, adr, 0); |
| 1248 | __ Lwl(out, adr, 3); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | // long libcore.io.Memory.peekLong(long address) |
| 1253 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1254 | CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap); |
| 1255 | } |
| 1256 | |
| 1257 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) { |
| 1258 | MipsAssembler* assembler = GetAssembler(); |
| 1259 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1260 | Register out_lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>(); |
| 1261 | Register out_hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>(); |
| 1262 | |
| 1263 | if (IsR6()) { |
| 1264 | __ Lw(out_lo, adr, 0); |
| 1265 | __ Lw(out_hi, adr, 4); |
| 1266 | } else { |
| 1267 | __ Lwr(out_lo, adr, 0); |
| 1268 | __ Lwl(out_lo, adr, 3); |
| 1269 | __ Lwr(out_hi, adr, 4); |
| 1270 | __ Lwl(out_hi, adr, 7); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) { |
| 1275 | LocationSummary* locations = new (arena) LocationSummary(invoke, |
| 1276 | LocationSummary::kNoCall, |
| 1277 | kIntrinsified); |
| 1278 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1279 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1280 | } |
| 1281 | |
| 1282 | // void libcore.io.Memory.pokeByte(long address, byte value) |
| 1283 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1284 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1285 | } |
| 1286 | |
| 1287 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeByte(HInvoke* invoke) { |
| 1288 | MipsAssembler* assembler = GetAssembler(); |
| 1289 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1290 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1291 | |
| 1292 | __ Sb(val, adr, 0); |
| 1293 | } |
| 1294 | |
| 1295 | // void libcore.io.Memory.pokeShort(long address, short value) |
| 1296 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1297 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1298 | } |
| 1299 | |
| 1300 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) { |
| 1301 | MipsAssembler* assembler = GetAssembler(); |
| 1302 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1303 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1304 | |
| 1305 | if (IsR6()) { |
| 1306 | __ Sh(val, adr, 0); |
| 1307 | } else { |
| 1308 | // Unlike for words, there are no shl/shr instructions to store |
| 1309 | // unaligned halfwords so the code stores individual bytes, in case |
| 1310 | // the address isn't halfword-aligned. |
| 1311 | __ Sb(val, adr, 0); |
| 1312 | __ Srl(AT, val, 8); |
| 1313 | __ Sb(AT, adr, 1); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | // void libcore.io.Memory.pokeInt(long address, int value) |
| 1318 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1319 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1320 | } |
| 1321 | |
| 1322 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) { |
| 1323 | MipsAssembler* assembler = GetAssembler(); |
| 1324 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1325 | Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>(); |
| 1326 | |
| 1327 | if (IsR6()) { |
| 1328 | __ Sw(val, adr, 0); |
| 1329 | } else { |
| 1330 | __ Swr(val, adr, 0); |
| 1331 | __ Swl(val, adr, 3); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // void libcore.io.Memory.pokeLong(long address, long value) |
| 1336 | void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1337 | CreateIntIntToVoidLocations(arena_, invoke); |
| 1338 | } |
| 1339 | |
| 1340 | void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) { |
| 1341 | MipsAssembler* assembler = GetAssembler(); |
| 1342 | Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>(); |
| 1343 | Register val_lo = invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>(); |
| 1344 | Register val_hi = invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>(); |
| 1345 | |
| 1346 | if (IsR6()) { |
| 1347 | __ Sw(val_lo, adr, 0); |
| 1348 | __ Sw(val_hi, adr, 4); |
| 1349 | } else { |
| 1350 | __ Swr(val_lo, adr, 0); |
| 1351 | __ Swl(val_lo, adr, 3); |
| 1352 | __ Swr(val_hi, adr, 4); |
| 1353 | __ Swl(val_hi, adr, 7); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | // char java.lang.String.charAt(int index) |
| 1358 | void IntrinsicLocationsBuilderMIPS::VisitStringCharAt(HInvoke* invoke) { |
| 1359 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1360 | LocationSummary::kCallOnSlowPath, |
| 1361 | kIntrinsified); |
| 1362 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1363 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1364 | locations->SetOut(Location::SameAsFirstInput()); |
| 1365 | } |
| 1366 | |
| 1367 | void IntrinsicCodeGeneratorMIPS::VisitStringCharAt(HInvoke* invoke) { |
| 1368 | LocationSummary* locations = invoke->GetLocations(); |
| 1369 | MipsAssembler* assembler = GetAssembler(); |
| 1370 | |
| 1371 | // Location of reference to data array |
| 1372 | const int32_t value_offset = mirror::String::ValueOffset().Int32Value(); |
| 1373 | // Location of count |
| 1374 | const int32_t count_offset = mirror::String::CountOffset().Int32Value(); |
| 1375 | |
| 1376 | Register obj = locations->InAt(0).AsRegister<Register>(); |
| 1377 | Register idx = locations->InAt(1).AsRegister<Register>(); |
| 1378 | Register out = locations->Out().AsRegister<Register>(); |
| 1379 | |
| 1380 | // TODO: Maybe we can support range check elimination. Overall, |
| 1381 | // though, I think it's not worth the cost. |
| 1382 | // TODO: For simplicity, the index parameter is requested in a |
| 1383 | // register, so different from Quick we will not optimize the |
| 1384 | // code for constants (which would save a register). |
| 1385 | |
| 1386 | SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke); |
| 1387 | codegen_->AddSlowPath(slow_path); |
| 1388 | |
| 1389 | // Load the string size |
| 1390 | __ Lw(TMP, obj, count_offset); |
| 1391 | codegen_->MaybeRecordImplicitNullCheck(invoke); |
| 1392 | // Revert to slow path if idx is too large, or negative |
| 1393 | __ Bgeu(idx, TMP, slow_path->GetEntryLabel()); |
| 1394 | |
| 1395 | // out = obj[2*idx]. |
| 1396 | __ Sll(TMP, idx, 1); // idx * 2 |
| 1397 | __ Addu(TMP, TMP, obj); // Address of char at location idx |
| 1398 | __ Lhu(out, TMP, value_offset); // Load char at location idx |
| 1399 | |
| 1400 | __ Bind(slow_path->GetExitLabel()); |
| 1401 | } |
| 1402 | |
Chris Larsen | 16ba2b4 | 2015-11-02 10:58:31 -0800 | [diff] [blame] | 1403 | // boolean java.lang.String.equals(Object anObject) |
| 1404 | void IntrinsicLocationsBuilderMIPS::VisitStringEquals(HInvoke* invoke) { |
| 1405 | LocationSummary* locations = new (arena_) LocationSummary(invoke, |
| 1406 | LocationSummary::kNoCall, |
| 1407 | kIntrinsified); |
| 1408 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1409 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1410 | locations->SetOut(Location::RequiresRegister()); |
| 1411 | |
| 1412 | // Temporary registers to store lengths of strings and for calculations. |
| 1413 | locations->AddTemp(Location::RequiresRegister()); |
| 1414 | locations->AddTemp(Location::RequiresRegister()); |
| 1415 | locations->AddTemp(Location::RequiresRegister()); |
| 1416 | } |
| 1417 | |
| 1418 | void IntrinsicCodeGeneratorMIPS::VisitStringEquals(HInvoke* invoke) { |
| 1419 | MipsAssembler* assembler = GetAssembler(); |
| 1420 | LocationSummary* locations = invoke->GetLocations(); |
| 1421 | |
| 1422 | Register str = locations->InAt(0).AsRegister<Register>(); |
| 1423 | Register arg = locations->InAt(1).AsRegister<Register>(); |
| 1424 | Register out = locations->Out().AsRegister<Register>(); |
| 1425 | |
| 1426 | Register temp1 = locations->GetTemp(0).AsRegister<Register>(); |
| 1427 | Register temp2 = locations->GetTemp(1).AsRegister<Register>(); |
| 1428 | Register temp3 = locations->GetTemp(2).AsRegister<Register>(); |
| 1429 | |
| 1430 | MipsLabel loop; |
| 1431 | MipsLabel end; |
| 1432 | MipsLabel return_true; |
| 1433 | MipsLabel return_false; |
| 1434 | |
| 1435 | // Get offsets of count, value, and class fields within a string object. |
| 1436 | const uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); |
| 1437 | const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value(); |
| 1438 | const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value(); |
| 1439 | |
| 1440 | // Note that the null check must have been done earlier. |
| 1441 | DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0))); |
| 1442 | |
| 1443 | // If the register containing the pointer to "this", and the register |
| 1444 | // containing the pointer to "anObject" are the same register then |
| 1445 | // "this", and "anObject" are the same object and we can |
| 1446 | // short-circuit the logic to a true result. |
| 1447 | if (str == arg) { |
| 1448 | __ LoadConst32(out, 1); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | // Check if input is null, return false if it is. |
| 1453 | __ Beqz(arg, &return_false); |
| 1454 | |
| 1455 | // Reference equality check, return true if same reference. |
| 1456 | __ Beq(str, arg, &return_true); |
| 1457 | |
| 1458 | // Instanceof check for the argument by comparing class fields. |
| 1459 | // All string objects must have the same type since String cannot be subclassed. |
| 1460 | // Receiver must be a string object, so its class field is equal to all strings' class fields. |
| 1461 | // If the argument is a string object, its class field must be equal to receiver's class field. |
| 1462 | __ Lw(temp1, str, class_offset); |
| 1463 | __ Lw(temp2, arg, class_offset); |
| 1464 | __ Bne(temp1, temp2, &return_false); |
| 1465 | |
| 1466 | // Load lengths of this and argument strings. |
| 1467 | __ Lw(temp1, str, count_offset); |
| 1468 | __ Lw(temp2, arg, count_offset); |
| 1469 | // Check if lengths are equal, return false if they're not. |
| 1470 | __ Bne(temp1, temp2, &return_false); |
| 1471 | // Return true if both strings are empty. |
| 1472 | __ Beqz(temp1, &return_true); |
| 1473 | |
| 1474 | // Don't overwrite input registers |
| 1475 | __ Move(TMP, str); |
| 1476 | __ Move(temp3, arg); |
| 1477 | |
| 1478 | // Assertions that must hold in order to compare strings 2 characters at a time. |
| 1479 | DCHECK_ALIGNED(value_offset, 4); |
| 1480 | static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded"); |
| 1481 | |
| 1482 | // Loop to compare strings 2 characters at a time starting at the beginning of the string. |
| 1483 | // Ok to do this because strings are zero-padded. |
| 1484 | __ Bind(&loop); |
| 1485 | __ Lw(out, TMP, value_offset); |
| 1486 | __ Lw(temp2, temp3, value_offset); |
| 1487 | __ Bne(out, temp2, &return_false); |
| 1488 | __ Addiu(TMP, TMP, 4); |
| 1489 | __ Addiu(temp3, temp3, 4); |
| 1490 | __ Addiu(temp1, temp1, -2); |
| 1491 | __ Bgtz(temp1, &loop); |
| 1492 | |
| 1493 | // Return true and exit the function. |
| 1494 | // If loop does not result in returning false, we return true. |
| 1495 | __ Bind(&return_true); |
| 1496 | __ LoadConst32(out, 1); |
| 1497 | __ B(&end); |
| 1498 | |
| 1499 | // Return false and exit the function. |
| 1500 | __ Bind(&return_false); |
| 1501 | __ LoadConst32(out, 0); |
| 1502 | __ Bind(&end); |
| 1503 | } |
| 1504 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1505 | UNIMPLEMENTED_INTRINSIC(MIPS, IntegerBitCount) |
| 1506 | UNIMPLEMENTED_INTRINSIC(MIPS, LongBitCount) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 1507 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1508 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCeil) |
| 1509 | UNIMPLEMENTED_INTRINSIC(MIPS, MathFloor) |
| 1510 | UNIMPLEMENTED_INTRINSIC(MIPS, MathRint) |
| 1511 | UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundDouble) |
| 1512 | UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundFloat) |
| 1513 | UNIMPLEMENTED_INTRINSIC(MIPS, ThreadCurrentThread) |
| 1514 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGet) |
| 1515 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetVolatile) |
| 1516 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetLong) |
| 1517 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetLongVolatile) |
| 1518 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetObject) |
| 1519 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetObjectVolatile) |
| 1520 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePut) |
| 1521 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutOrdered) |
| 1522 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutVolatile) |
| 1523 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutObject) |
| 1524 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutObjectOrdered) |
| 1525 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutObjectVolatile) |
| 1526 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLong) |
| 1527 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLongOrdered) |
| 1528 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLongVolatile) |
| 1529 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASInt) |
| 1530 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASLong) |
| 1531 | UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASObject) |
| 1532 | UNIMPLEMENTED_INTRINSIC(MIPS, StringCompareTo) |
| 1533 | UNIMPLEMENTED_INTRINSIC(MIPS, StringIndexOf) |
| 1534 | UNIMPLEMENTED_INTRINSIC(MIPS, StringIndexOfAfter) |
| 1535 | UNIMPLEMENTED_INTRINSIC(MIPS, StringNewStringFromBytes) |
| 1536 | UNIMPLEMENTED_INTRINSIC(MIPS, StringNewStringFromChars) |
| 1537 | UNIMPLEMENTED_INTRINSIC(MIPS, StringNewStringFromString) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 1538 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1539 | UNIMPLEMENTED_INTRINSIC(MIPS, ReferenceGetReferent) |
| 1540 | UNIMPLEMENTED_INTRINSIC(MIPS, StringGetCharsNoCheck) |
| 1541 | UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopyChar) |
| 1542 | UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopy) |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 1543 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1544 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCos) |
| 1545 | UNIMPLEMENTED_INTRINSIC(MIPS, MathSin) |
| 1546 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAcos) |
| 1547 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAsin) |
| 1548 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAtan) |
| 1549 | UNIMPLEMENTED_INTRINSIC(MIPS, MathAtan2) |
| 1550 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCbrt) |
| 1551 | UNIMPLEMENTED_INTRINSIC(MIPS, MathCosh) |
| 1552 | UNIMPLEMENTED_INTRINSIC(MIPS, MathExp) |
| 1553 | UNIMPLEMENTED_INTRINSIC(MIPS, MathExpm1) |
| 1554 | UNIMPLEMENTED_INTRINSIC(MIPS, MathHypot) |
| 1555 | UNIMPLEMENTED_INTRINSIC(MIPS, MathLog) |
| 1556 | UNIMPLEMENTED_INTRINSIC(MIPS, MathLog10) |
| 1557 | UNIMPLEMENTED_INTRINSIC(MIPS, MathNextAfter) |
| 1558 | UNIMPLEMENTED_INTRINSIC(MIPS, MathSinh) |
| 1559 | UNIMPLEMENTED_INTRINSIC(MIPS, MathTan) |
| 1560 | UNIMPLEMENTED_INTRINSIC(MIPS, MathTanh) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 1561 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1562 | UNIMPLEMENTED_INTRINSIC(MIPS, FloatIsInfinite) |
| 1563 | UNIMPLEMENTED_INTRINSIC(MIPS, DoubleIsInfinite) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 1564 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1565 | UNIMPLEMENTED_INTRINSIC(MIPS, IntegerHighestOneBit) |
| 1566 | UNIMPLEMENTED_INTRINSIC(MIPS, LongHighestOneBit) |
| 1567 | UNIMPLEMENTED_INTRINSIC(MIPS, IntegerLowestOneBit) |
| 1568 | UNIMPLEMENTED_INTRINSIC(MIPS, LongLowestOneBit) |
Aart Bik | 59c9454 | 2016-01-25 14:20:58 -0800 | [diff] [blame] | 1569 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame^] | 1570 | UNREACHABLE_INTRINSICS(MIPS) |
Chris Larsen | 701566a | 2015-10-27 15:29:13 -0700 | [diff] [blame] | 1571 | |
| 1572 | #undef __ |
| 1573 | |
| 1574 | } // namespace mips |
| 1575 | } // namespace art |