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 "code_generator_mips64.h"
18 
19 #include "art_method.h"
20 #include "code_generator_utils.h"
21 #include "entrypoints/quick/quick_entrypoints.h"
22 #include "entrypoints/quick/quick_entrypoints_enum.h"
23 #include "gc/accounting/card_table.h"
24 #include "intrinsics.h"
25 #include "intrinsics_mips64.h"
26 #include "mirror/array-inl.h"
27 #include "mirror/class-inl.h"
28 #include "offsets.h"
29 #include "thread.h"
30 #include "utils/assembler.h"
31 #include "utils/mips64/assembler_mips64.h"
32 #include "utils/stack_checks.h"
33 
34 namespace art {
35 namespace mips64 {
36 
37 static constexpr int kCurrentMethodStackOffset = 0;
38 static constexpr GpuRegister kMethodRegisterArgument = A0;
39 
Mips64ReturnLocation(Primitive::Type return_type)40 Location Mips64ReturnLocation(Primitive::Type return_type) {
41   switch (return_type) {
42     case Primitive::kPrimBoolean:
43     case Primitive::kPrimByte:
44     case Primitive::kPrimChar:
45     case Primitive::kPrimShort:
46     case Primitive::kPrimInt:
47     case Primitive::kPrimNot:
48     case Primitive::kPrimLong:
49       return Location::RegisterLocation(V0);
50 
51     case Primitive::kPrimFloat:
52     case Primitive::kPrimDouble:
53       return Location::FpuRegisterLocation(F0);
54 
55     case Primitive::kPrimVoid:
56       return Location();
57   }
58   UNREACHABLE();
59 }
60 
GetReturnLocation(Primitive::Type type) const61 Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
62   return Mips64ReturnLocation(type);
63 }
64 
GetMethodLocation() const65 Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
66   return Location::RegisterLocation(kMethodRegisterArgument);
67 }
68 
GetNextLocation(Primitive::Type type)69 Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
70   Location next_location;
71   if (type == Primitive::kPrimVoid) {
72     LOG(FATAL) << "Unexpected parameter type " << type;
73   }
74 
75   if (Primitive::IsFloatingPointType(type) &&
76       (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
77     next_location = Location::FpuRegisterLocation(
78         calling_convention.GetFpuRegisterAt(float_index_++));
79     gp_index_++;
80   } else if (!Primitive::IsFloatingPointType(type) &&
81              (gp_index_ < calling_convention.GetNumberOfRegisters())) {
82     next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
83     float_index_++;
84   } else {
85     size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
86     next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
87                                                  : Location::StackSlot(stack_offset);
88   }
89 
90   // Space on the stack is reserved for all arguments.
91   stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
92 
93   // TODO: review
94 
95   // TODO: shouldn't we use a whole machine word per argument on the stack?
96   // Implicit 4-byte method pointer (and such) will cause misalignment.
97 
98   return next_location;
99 }
100 
GetReturnLocation(Primitive::Type type)101 Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
102   return Mips64ReturnLocation(type);
103 }
104 
105 #define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
106 #define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
107 
108 class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
109  public:
BoundsCheckSlowPathMIPS64(HBoundsCheck * instruction)110   explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
111 
EmitNativeCode(CodeGenerator * codegen)112   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
113     LocationSummary* locations = instruction_->GetLocations();
114     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
115     __ Bind(GetEntryLabel());
116     if (instruction_->CanThrowIntoCatchBlock()) {
117       // Live registers will be restored in the catch block if caught.
118       SaveLiveRegisters(codegen, instruction_->GetLocations());
119     }
120     // We're moving two locations to locations that could overlap, so we need a parallel
121     // move resolver.
122     InvokeRuntimeCallingConvention calling_convention;
123     codegen->EmitParallelMoves(locations->InAt(0),
124                                Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
125                                Primitive::kPrimInt,
126                                locations->InAt(1),
127                                Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
128                                Primitive::kPrimInt);
129     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
130                                   instruction_,
131                                   instruction_->GetDexPc(),
132                                   this);
133     CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
134   }
135 
IsFatal() const136   bool IsFatal() const OVERRIDE { return true; }
137 
GetDescription() const138   const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
139 
140  private:
141   DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
142 };
143 
144 class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
145  public:
DivZeroCheckSlowPathMIPS64(HDivZeroCheck * instruction)146   explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
147 
EmitNativeCode(CodeGenerator * codegen)148   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
149     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
150     __ Bind(GetEntryLabel());
151     if (instruction_->CanThrowIntoCatchBlock()) {
152       // Live registers will be restored in the catch block if caught.
153       SaveLiveRegisters(codegen, instruction_->GetLocations());
154     }
155     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
156                                   instruction_,
157                                   instruction_->GetDexPc(),
158                                   this);
159     CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
160   }
161 
IsFatal() const162   bool IsFatal() const OVERRIDE { return true; }
163 
GetDescription() const164   const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
165 
166  private:
167   DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
168 };
169 
170 class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
171  public:
LoadClassSlowPathMIPS64(HLoadClass * cls,HInstruction * at,uint32_t dex_pc,bool do_clinit)172   LoadClassSlowPathMIPS64(HLoadClass* cls,
173                           HInstruction* at,
174                           uint32_t dex_pc,
175                           bool do_clinit)
176       : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
177     DCHECK(at->IsLoadClass() || at->IsClinitCheck());
178   }
179 
EmitNativeCode(CodeGenerator * codegen)180   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
181     LocationSummary* locations = at_->GetLocations();
182     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
183 
184     __ Bind(GetEntryLabel());
185     SaveLiveRegisters(codegen, locations);
186 
187     InvokeRuntimeCallingConvention calling_convention;
188     __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
189     int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
190                                             : QUICK_ENTRY_POINT(pInitializeType);
191     mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
192     if (do_clinit_) {
193       CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
194     } else {
195       CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
196     }
197 
198     // Move the class to the desired location.
199     Location out = locations->Out();
200     if (out.IsValid()) {
201       DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
202       Primitive::Type type = at_->GetType();
203       mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
204     }
205 
206     RestoreLiveRegisters(codegen, locations);
207     __ Bc(GetExitLabel());
208   }
209 
GetDescription() const210   const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
211 
212  private:
213   // The class this slow path will load.
214   HLoadClass* const cls_;
215 
216   // The instruction where this slow path is happening.
217   // (Might be the load class or an initialization check).
218   HInstruction* const at_;
219 
220   // The dex PC of `at_`.
221   const uint32_t dex_pc_;
222 
223   // Whether to initialize the class.
224   const bool do_clinit_;
225 
226   DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227 };
228 
229 class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230  public:
LoadStringSlowPathMIPS64(HLoadString * instruction)231   explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
232 
EmitNativeCode(CodeGenerator * codegen)233   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234     LocationSummary* locations = instruction_->GetLocations();
235     DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237 
238     __ Bind(GetEntryLabel());
239     SaveLiveRegisters(codegen, locations);
240 
241     InvokeRuntimeCallingConvention calling_convention;
242     const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
243     __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
244     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
245                                   instruction_,
246                                   instruction_->GetDexPc(),
247                                   this);
248     CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
249     Primitive::Type type = instruction_->GetType();
250     mips64_codegen->MoveLocation(locations->Out(),
251                                  calling_convention.GetReturnLocation(type),
252                                  type);
253 
254     RestoreLiveRegisters(codegen, locations);
255     __ Bc(GetExitLabel());
256   }
257 
GetDescription() const258   const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
259 
260  private:
261   DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
262 };
263 
264 class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
265  public:
NullCheckSlowPathMIPS64(HNullCheck * instr)266   explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
267 
EmitNativeCode(CodeGenerator * codegen)268   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
270     __ Bind(GetEntryLabel());
271     if (instruction_->CanThrowIntoCatchBlock()) {
272       // Live registers will be restored in the catch block if caught.
273       SaveLiveRegisters(codegen, instruction_->GetLocations());
274     }
275     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
276                                   instruction_,
277                                   instruction_->GetDexPc(),
278                                   this);
279     CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
280   }
281 
IsFatal() const282   bool IsFatal() const OVERRIDE { return true; }
283 
GetDescription() const284   const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
285 
286  private:
287   DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
288 };
289 
290 class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
291  public:
SuspendCheckSlowPathMIPS64(HSuspendCheck * instruction,HBasicBlock * successor)292   SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
293       : SlowPathCodeMIPS64(instruction), successor_(successor) {}
294 
EmitNativeCode(CodeGenerator * codegen)295   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
297     __ Bind(GetEntryLabel());
298     SaveLiveRegisters(codegen, instruction_->GetLocations());
299     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
300                                   instruction_,
301                                   instruction_->GetDexPc(),
302                                   this);
303     CheckEntrypointTypes<kQuickTestSuspend, void, void>();
304     RestoreLiveRegisters(codegen, instruction_->GetLocations());
305     if (successor_ == nullptr) {
306       __ Bc(GetReturnLabel());
307     } else {
308       __ Bc(mips64_codegen->GetLabelOf(successor_));
309     }
310   }
311 
GetReturnLabel()312   Mips64Label* GetReturnLabel() {
313     DCHECK(successor_ == nullptr);
314     return &return_label_;
315   }
316 
GetDescription() const317   const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
318 
319  private:
320   // If not null, the block to branch to after the suspend check.
321   HBasicBlock* const successor_;
322 
323   // If `successor_` is null, the label to branch to after the suspend check.
324   Mips64Label return_label_;
325 
326   DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
327 };
328 
329 class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
330  public:
TypeCheckSlowPathMIPS64(HInstruction * instruction)331   explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
332 
EmitNativeCode(CodeGenerator * codegen)333   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
334     LocationSummary* locations = instruction_->GetLocations();
335     Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
336     uint32_t dex_pc = instruction_->GetDexPc();
337     DCHECK(instruction_->IsCheckCast()
338            || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
340 
341     __ Bind(GetEntryLabel());
342     SaveLiveRegisters(codegen, locations);
343 
344     // We're moving two locations to locations that could overlap, so we need a parallel
345     // move resolver.
346     InvokeRuntimeCallingConvention calling_convention;
347     codegen->EmitParallelMoves(locations->InAt(1),
348                                Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
349                                Primitive::kPrimNot,
350                                object_class,
351                                Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352                                Primitive::kPrimNot);
353 
354     if (instruction_->IsInstanceOf()) {
355       mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
356                                     instruction_,
357                                     dex_pc,
358                                     this);
359       CheckEntrypointTypes<
360           kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
361       Primitive::Type ret_type = instruction_->GetType();
362       Location ret_loc = calling_convention.GetReturnLocation(ret_type);
363       mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
364     } else {
365       DCHECK(instruction_->IsCheckCast());
366       mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
367       CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
368     }
369 
370     RestoreLiveRegisters(codegen, locations);
371     __ Bc(GetExitLabel());
372   }
373 
GetDescription() const374   const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
375 
376  private:
377   DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
378 };
379 
380 class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
381  public:
DeoptimizationSlowPathMIPS64(HDeoptimize * instruction)382   explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
383     : SlowPathCodeMIPS64(instruction) {}
384 
EmitNativeCode(CodeGenerator * codegen)385   void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386     CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
387     __ Bind(GetEntryLabel());
388     SaveLiveRegisters(codegen, instruction_->GetLocations());
389     mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
390                                   instruction_,
391                                   instruction_->GetDexPc(),
392                                   this);
393     CheckEntrypointTypes<kQuickDeoptimize, void, void>();
394   }
395 
GetDescription() const396   const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
397 
398  private:
399   DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
400 };
401 
CodeGeneratorMIPS64(HGraph * graph,const Mips64InstructionSetFeatures & isa_features,const CompilerOptions & compiler_options,OptimizingCompilerStats * stats)402 CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
403                                          const Mips64InstructionSetFeatures& isa_features,
404                                          const CompilerOptions& compiler_options,
405                                          OptimizingCompilerStats* stats)
406     : CodeGenerator(graph,
407                     kNumberOfGpuRegisters,
408                     kNumberOfFpuRegisters,
409                     /* number_of_register_pairs */ 0,
410                     ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
411                                         arraysize(kCoreCalleeSaves)),
412                     ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
413                                         arraysize(kFpuCalleeSaves)),
414                     compiler_options,
415                     stats),
416       block_labels_(nullptr),
417       location_builder_(graph, this),
418       instruction_visitor_(graph, this),
419       move_resolver_(graph->GetArena(), this),
420       assembler_(graph->GetArena()),
421       isa_features_(isa_features) {
422   // Save RA (containing the return address) to mimic Quick.
423   AddAllocatedRegister(Location::RegisterLocation(RA));
424 }
425 
426 #undef __
427 #define __ down_cast<Mips64Assembler*>(GetAssembler())->
428 #define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
429 
Finalize(CodeAllocator * allocator)430 void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
431   // Ensure that we fix up branches.
432   __ FinalizeCode();
433 
434   // Adjust native pc offsets in stack maps.
435   for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
436     uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
437     uint32_t new_position = __ GetAdjustedPosition(old_position);
438     DCHECK_GE(new_position, old_position);
439     stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
440   }
441 
442   // Adjust pc offsets for the disassembly information.
443   if (disasm_info_ != nullptr) {
444     GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
445     frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
446     frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
447     for (auto& it : *disasm_info_->GetInstructionIntervals()) {
448       it.second.start = __ GetAdjustedPosition(it.second.start);
449       it.second.end = __ GetAdjustedPosition(it.second.end);
450     }
451     for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
452       it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
453       it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
454     }
455   }
456 
457   CodeGenerator::Finalize(allocator);
458 }
459 
GetAssembler() const460 Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
461   return codegen_->GetAssembler();
462 }
463 
EmitMove(size_t index)464 void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
465   MoveOperands* move = moves_[index];
466   codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
467 }
468 
EmitSwap(size_t index)469 void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
470   MoveOperands* move = moves_[index];
471   codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
472 }
473 
RestoreScratch(int reg)474 void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
475   // Pop reg
476   __ Ld(GpuRegister(reg), SP, 0);
477   __ DecreaseFrameSize(kMips64DoublewordSize);
478 }
479 
SpillScratch(int reg)480 void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
481   // Push reg
482   __ IncreaseFrameSize(kMips64DoublewordSize);
483   __ Sd(GpuRegister(reg), SP, 0);
484 }
485 
Exchange(int index1,int index2,bool double_slot)486 void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
487   LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
488   StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
489   // Allocate a scratch register other than TMP, if available.
490   // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
491   // automatically unspilled when the scratch scope object is destroyed).
492   ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
493   // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
494   int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
495   __ LoadFromOffset(load_type,
496                     GpuRegister(ensure_scratch.GetRegister()),
497                     SP,
498                     index1 + stack_offset);
499   __ LoadFromOffset(load_type,
500                     TMP,
501                     SP,
502                     index2 + stack_offset);
503   __ StoreToOffset(store_type,
504                    GpuRegister(ensure_scratch.GetRegister()),
505                    SP,
506                    index2 + stack_offset);
507   __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
508 }
509 
DWARFReg(GpuRegister reg)510 static dwarf::Reg DWARFReg(GpuRegister reg) {
511   return dwarf::Reg::Mips64Core(static_cast<int>(reg));
512 }
513 
DWARFReg(FpuRegister reg)514 static dwarf::Reg DWARFReg(FpuRegister reg) {
515   return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
516 }
517 
GenerateFrameEntry()518 void CodeGeneratorMIPS64::GenerateFrameEntry() {
519   __ Bind(&frame_entry_label_);
520 
521   bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
522 
523   if (do_overflow_check) {
524     __ LoadFromOffset(kLoadWord,
525                       ZERO,
526                       SP,
527                       -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
528     RecordPcInfo(nullptr, 0);
529   }
530 
531   // TODO: anything related to T9/GP/GOT/PIC/.so's?
532 
533   if (HasEmptyFrame()) {
534     return;
535   }
536 
537   // Make sure the frame size isn't unreasonably large. Per the various APIs
538   // it looks like it should always be less than 2GB in size, which allows
539   // us using 32-bit signed offsets from the stack pointer.
540   if (GetFrameSize() > 0x7FFFFFFF)
541     LOG(FATAL) << "Stack frame larger than 2GB";
542 
543   // Spill callee-saved registers.
544   // Note that their cumulative size is small and they can be indexed using
545   // 16-bit offsets.
546 
547   // TODO: increment/decrement SP in one step instead of two or remove this comment.
548 
549   uint32_t ofs = FrameEntrySpillSize();
550   __ IncreaseFrameSize(ofs);
551 
552   for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
553     GpuRegister reg = kCoreCalleeSaves[i];
554     if (allocated_registers_.ContainsCoreRegister(reg)) {
555       ofs -= kMips64DoublewordSize;
556       __ Sd(reg, SP, ofs);
557       __ cfi().RelOffset(DWARFReg(reg), ofs);
558     }
559   }
560 
561   for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
562     FpuRegister reg = kFpuCalleeSaves[i];
563     if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
564       ofs -= kMips64DoublewordSize;
565       __ Sdc1(reg, SP, ofs);
566       __ cfi().RelOffset(DWARFReg(reg), ofs);
567     }
568   }
569 
570   // Allocate the rest of the frame and store the current method pointer
571   // at its end.
572 
573   __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
574 
575   static_assert(IsInt<16>(kCurrentMethodStackOffset),
576                 "kCurrentMethodStackOffset must fit into int16_t");
577   __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
578 }
579 
GenerateFrameExit()580 void CodeGeneratorMIPS64::GenerateFrameExit() {
581   __ cfi().RememberState();
582 
583   // TODO: anything related to T9/GP/GOT/PIC/.so's?
584 
585   if (!HasEmptyFrame()) {
586     // Deallocate the rest of the frame.
587 
588     __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
589 
590     // Restore callee-saved registers.
591     // Note that their cumulative size is small and they can be indexed using
592     // 16-bit offsets.
593 
594     // TODO: increment/decrement SP in one step instead of two or remove this comment.
595 
596     uint32_t ofs = 0;
597 
598     for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
599       FpuRegister reg = kFpuCalleeSaves[i];
600       if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
601         __ Ldc1(reg, SP, ofs);
602         ofs += kMips64DoublewordSize;
603         __ cfi().Restore(DWARFReg(reg));
604       }
605     }
606 
607     for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
608       GpuRegister reg = kCoreCalleeSaves[i];
609       if (allocated_registers_.ContainsCoreRegister(reg)) {
610         __ Ld(reg, SP, ofs);
611         ofs += kMips64DoublewordSize;
612         __ cfi().Restore(DWARFReg(reg));
613       }
614     }
615 
616     DCHECK_EQ(ofs, FrameEntrySpillSize());
617     __ DecreaseFrameSize(ofs);
618   }
619 
620   __ Jr(RA);
621   __ Nop();
622 
623   __ cfi().RestoreState();
624   __ cfi().DefCFAOffset(GetFrameSize());
625 }
626 
Bind(HBasicBlock * block)627 void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
628   __ Bind(GetLabelOf(block));
629 }
630 
MoveLocation(Location destination,Location source,Primitive::Type dst_type)631 void CodeGeneratorMIPS64::MoveLocation(Location destination,
632                                        Location source,
633                                        Primitive::Type dst_type) {
634   if (source.Equals(destination)) {
635     return;
636   }
637 
638   // A valid move can always be inferred from the destination and source
639   // locations. When moving from and to a register, the argument type can be
640   // used to generate 32bit instead of 64bit moves.
641   bool unspecified_type = (dst_type == Primitive::kPrimVoid);
642   DCHECK_EQ(unspecified_type, false);
643 
644   if (destination.IsRegister() || destination.IsFpuRegister()) {
645     if (unspecified_type) {
646       HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
647       if (source.IsStackSlot() ||
648           (src_cst != nullptr && (src_cst->IsIntConstant()
649                                   || src_cst->IsFloatConstant()
650                                   || src_cst->IsNullConstant()))) {
651         // For stack slots and 32bit constants, a 64bit type is appropriate.
652         dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
653       } else {
654         // If the source is a double stack slot or a 64bit constant, a 64bit
655         // type is appropriate. Else the source is a register, and since the
656         // type has not been specified, we chose a 64bit type to force a 64bit
657         // move.
658         dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
659       }
660     }
661     DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
662            (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
663     if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
664       // Move to GPR/FPR from stack
665       LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
666       if (Primitive::IsFloatingPointType(dst_type)) {
667         __ LoadFpuFromOffset(load_type,
668                              destination.AsFpuRegister<FpuRegister>(),
669                              SP,
670                              source.GetStackIndex());
671       } else {
672         // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
673         __ LoadFromOffset(load_type,
674                           destination.AsRegister<GpuRegister>(),
675                           SP,
676                           source.GetStackIndex());
677       }
678     } else if (source.IsConstant()) {
679       // Move to GPR/FPR from constant
680       GpuRegister gpr = AT;
681       if (!Primitive::IsFloatingPointType(dst_type)) {
682         gpr = destination.AsRegister<GpuRegister>();
683       }
684       if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
685         int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
686         if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
687           gpr = ZERO;
688         } else {
689           __ LoadConst32(gpr, value);
690         }
691       } else {
692         int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
693         if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
694           gpr = ZERO;
695         } else {
696           __ LoadConst64(gpr, value);
697         }
698       }
699       if (dst_type == Primitive::kPrimFloat) {
700         __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
701       } else if (dst_type == Primitive::kPrimDouble) {
702         __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
703       }
704     } else if (source.IsRegister()) {
705       if (destination.IsRegister()) {
706         // Move to GPR from GPR
707         __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
708       } else {
709         DCHECK(destination.IsFpuRegister());
710         if (Primitive::Is64BitType(dst_type)) {
711           __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
712         } else {
713           __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
714         }
715       }
716     } else if (source.IsFpuRegister()) {
717       if (destination.IsFpuRegister()) {
718         // Move to FPR from FPR
719         if (dst_type == Primitive::kPrimFloat) {
720           __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
721         } else {
722           DCHECK_EQ(dst_type, Primitive::kPrimDouble);
723           __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
724         }
725       } else {
726         DCHECK(destination.IsRegister());
727         if (Primitive::Is64BitType(dst_type)) {
728           __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
729         } else {
730           __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
731         }
732       }
733     }
734   } else {  // The destination is not a register. It must be a stack slot.
735     DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
736     if (source.IsRegister() || source.IsFpuRegister()) {
737       if (unspecified_type) {
738         if (source.IsRegister()) {
739           dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
740         } else {
741           dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
742         }
743       }
744       DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
745              (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
746       // Move to stack from GPR/FPR
747       StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
748       if (source.IsRegister()) {
749         __ StoreToOffset(store_type,
750                          source.AsRegister<GpuRegister>(),
751                          SP,
752                          destination.GetStackIndex());
753       } else {
754         __ StoreFpuToOffset(store_type,
755                             source.AsFpuRegister<FpuRegister>(),
756                             SP,
757                             destination.GetStackIndex());
758       }
759     } else if (source.IsConstant()) {
760       // Move to stack from constant
761       HConstant* src_cst = source.GetConstant();
762       StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
763       GpuRegister gpr = ZERO;
764       if (destination.IsStackSlot()) {
765         int32_t value = GetInt32ValueOf(src_cst->AsConstant());
766         if (value != 0) {
767           gpr = TMP;
768           __ LoadConst32(gpr, value);
769         }
770       } else {
771         DCHECK(destination.IsDoubleStackSlot());
772         int64_t value = GetInt64ValueOf(src_cst->AsConstant());
773         if (value != 0) {
774           gpr = TMP;
775           __ LoadConst64(gpr, value);
776         }
777       }
778       __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
779     } else {
780       DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
781       DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
782       // Move to stack from stack
783       if (destination.IsStackSlot()) {
784         __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
785         __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
786       } else {
787         __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
788         __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
789       }
790     }
791   }
792 }
793 
SwapLocations(Location loc1,Location loc2,Primitive::Type type)794 void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
795   DCHECK(!loc1.IsConstant());
796   DCHECK(!loc2.IsConstant());
797 
798   if (loc1.Equals(loc2)) {
799     return;
800   }
801 
802   bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
803   bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
804   bool is_fp_reg1 = loc1.IsFpuRegister();
805   bool is_fp_reg2 = loc2.IsFpuRegister();
806 
807   if (loc2.IsRegister() && loc1.IsRegister()) {
808     // Swap 2 GPRs
809     GpuRegister r1 = loc1.AsRegister<GpuRegister>();
810     GpuRegister r2 = loc2.AsRegister<GpuRegister>();
811     __ Move(TMP, r2);
812     __ Move(r2, r1);
813     __ Move(r1, TMP);
814   } else if (is_fp_reg2 && is_fp_reg1) {
815     // Swap 2 FPRs
816     FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
817     FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
818     if (type == Primitive::kPrimFloat) {
819       __ MovS(FTMP, r1);
820       __ MovS(r1, r2);
821       __ MovS(r2, FTMP);
822     } else {
823       DCHECK_EQ(type, Primitive::kPrimDouble);
824       __ MovD(FTMP, r1);
825       __ MovD(r1, r2);
826       __ MovD(r2, FTMP);
827     }
828   } else if (is_slot1 != is_slot2) {
829     // Swap GPR/FPR and stack slot
830     Location reg_loc = is_slot1 ? loc2 : loc1;
831     Location mem_loc = is_slot1 ? loc1 : loc2;
832     LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
833     StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
834     // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
835     __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
836     if (reg_loc.IsFpuRegister()) {
837       __ StoreFpuToOffset(store_type,
838                           reg_loc.AsFpuRegister<FpuRegister>(),
839                           SP,
840                           mem_loc.GetStackIndex());
841       if (mem_loc.IsStackSlot()) {
842         __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
843       } else {
844         DCHECK(mem_loc.IsDoubleStackSlot());
845         __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
846       }
847     } else {
848       __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
849       __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
850     }
851   } else if (is_slot1 && is_slot2) {
852     move_resolver_.Exchange(loc1.GetStackIndex(),
853                             loc2.GetStackIndex(),
854                             loc1.IsDoubleStackSlot());
855   } else {
856     LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
857   }
858 }
859 
MoveConstant(Location location,int32_t value)860 void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
861   DCHECK(location.IsRegister());
862   __ LoadConst32(location.AsRegister<GpuRegister>(), value);
863 }
864 
AddLocationAsTemp(Location location,LocationSummary * locations)865 void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
866   if (location.IsRegister()) {
867     locations->AddTemp(location);
868   } else {
869     UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
870   }
871 }
872 
MarkGCCard(GpuRegister object,GpuRegister value,bool value_can_be_null)873 void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
874                                      GpuRegister value,
875                                      bool value_can_be_null) {
876   Mips64Label done;
877   GpuRegister card = AT;
878   GpuRegister temp = TMP;
879   if (value_can_be_null) {
880     __ Beqzc(value, &done);
881   }
882   __ LoadFromOffset(kLoadDoubleword,
883                     card,
884                     TR,
885                     Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
886   __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
887   __ Daddu(temp, card, temp);
888   __ Sb(card, temp, 0);
889   if (value_can_be_null) {
890     __ Bind(&done);
891   }
892 }
893 
SetupBlockedRegisters() const894 void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
895   // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
896   blocked_core_registers_[ZERO] = true;
897   blocked_core_registers_[K0] = true;
898   blocked_core_registers_[K1] = true;
899   blocked_core_registers_[GP] = true;
900   blocked_core_registers_[SP] = true;
901   blocked_core_registers_[RA] = true;
902 
903   // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
904   // registers (similar to how AT is used by MIPS assemblers).
905   blocked_core_registers_[AT] = true;
906   blocked_core_registers_[TMP] = true;
907   blocked_core_registers_[TMP2] = true;
908   blocked_fpu_registers_[FTMP] = true;
909 
910   // Reserve suspend and thread registers.
911   blocked_core_registers_[S0] = true;
912   blocked_core_registers_[TR] = true;
913 
914   // Reserve T9 for function calls
915   blocked_core_registers_[T9] = true;
916 
917   // TODO: review; anything else?
918 
919   // TODO: remove once all the issues with register saving/restoring are sorted out.
920   for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
921     blocked_core_registers_[kCoreCalleeSaves[i]] = true;
922   }
923 
924   for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
925     blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
926   }
927 }
928 
SaveCoreRegister(size_t stack_index,uint32_t reg_id)929 size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
930   __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
931   return kMips64DoublewordSize;
932 }
933 
RestoreCoreRegister(size_t stack_index,uint32_t reg_id)934 size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
935   __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
936   return kMips64DoublewordSize;
937 }
938 
SaveFloatingPointRegister(size_t stack_index,uint32_t reg_id)939 size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
940   __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
941   return kMips64DoublewordSize;
942 }
943 
RestoreFloatingPointRegister(size_t stack_index,uint32_t reg_id)944 size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945   __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
946   return kMips64DoublewordSize;
947 }
948 
DumpCoreRegister(std::ostream & stream,int reg) const949 void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
950   stream << GpuRegister(reg);
951 }
952 
DumpFloatingPointRegister(std::ostream & stream,int reg) const953 void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
954   stream << FpuRegister(reg);
955 }
956 
InvokeRuntime(QuickEntrypointEnum entrypoint,HInstruction * instruction,uint32_t dex_pc,SlowPathCode * slow_path)957 void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
958                                      HInstruction* instruction,
959                                      uint32_t dex_pc,
960                                      SlowPathCode* slow_path) {
961   InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
962                 instruction,
963                 dex_pc,
964                 slow_path);
965 }
966 
InvokeRuntime(int32_t entry_point_offset,HInstruction * instruction,uint32_t dex_pc,SlowPathCode * slow_path)967 void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
968                                         HInstruction* instruction,
969                                         uint32_t dex_pc,
970                                         SlowPathCode* slow_path) {
971   ValidateInvokeRuntime(instruction, slow_path);
972   // TODO: anything related to T9/GP/GOT/PIC/.so's?
973   __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
974   __ Jalr(T9);
975   __ Nop();
976   RecordPcInfo(instruction, dex_pc, slow_path);
977 }
978 
GenerateClassInitializationCheck(SlowPathCodeMIPS64 * slow_path,GpuRegister class_reg)979 void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
980                                                                       GpuRegister class_reg) {
981   __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
982   __ LoadConst32(AT, mirror::Class::kStatusInitialized);
983   __ Bltc(TMP, AT, slow_path->GetEntryLabel());
984   // TODO: barrier needed?
985   __ Bind(slow_path->GetExitLabel());
986 }
987 
GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED)988 void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
989   __ Sync(0);  // only stype 0 is supported
990 }
991 
GenerateSuspendCheck(HSuspendCheck * instruction,HBasicBlock * successor)992 void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
993                                                           HBasicBlock* successor) {
994   SuspendCheckSlowPathMIPS64* slow_path =
995     new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
996   codegen_->AddSlowPath(slow_path);
997 
998   __ LoadFromOffset(kLoadUnsignedHalfword,
999                     TMP,
1000                     TR,
1001                     Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
1002   if (successor == nullptr) {
1003     __ Bnezc(TMP, slow_path->GetEntryLabel());
1004     __ Bind(slow_path->GetReturnLabel());
1005   } else {
1006     __ Beqzc(TMP, codegen_->GetLabelOf(successor));
1007     __ Bc(slow_path->GetEntryLabel());
1008     // slow_path will return to GetLabelOf(successor).
1009   }
1010 }
1011 
InstructionCodeGeneratorMIPS64(HGraph * graph,CodeGeneratorMIPS64 * codegen)1012 InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1013                                                                CodeGeneratorMIPS64* codegen)
1014       : InstructionCodeGenerator(graph, codegen),
1015         assembler_(codegen->GetAssembler()),
1016         codegen_(codegen) {}
1017 
HandleBinaryOp(HBinaryOperation * instruction)1018 void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1019   DCHECK_EQ(instruction->InputCount(), 2U);
1020   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1021   Primitive::Type type = instruction->GetResultType();
1022   switch (type) {
1023     case Primitive::kPrimInt:
1024     case Primitive::kPrimLong: {
1025       locations->SetInAt(0, Location::RequiresRegister());
1026       HInstruction* right = instruction->InputAt(1);
1027       bool can_use_imm = false;
1028       if (right->IsConstant()) {
1029         int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1030         if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1031           can_use_imm = IsUint<16>(imm);
1032         } else if (instruction->IsAdd()) {
1033           can_use_imm = IsInt<16>(imm);
1034         } else {
1035           DCHECK(instruction->IsSub());
1036           can_use_imm = IsInt<16>(-imm);
1037         }
1038       }
1039       if (can_use_imm)
1040         locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1041       else
1042         locations->SetInAt(1, Location::RequiresRegister());
1043       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1044       }
1045       break;
1046 
1047     case Primitive::kPrimFloat:
1048     case Primitive::kPrimDouble:
1049       locations->SetInAt(0, Location::RequiresFpuRegister());
1050       locations->SetInAt(1, Location::RequiresFpuRegister());
1051       locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1052       break;
1053 
1054     default:
1055       LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1056   }
1057 }
1058 
HandleBinaryOp(HBinaryOperation * instruction)1059 void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1060   Primitive::Type type = instruction->GetType();
1061   LocationSummary* locations = instruction->GetLocations();
1062 
1063   switch (type) {
1064     case Primitive::kPrimInt:
1065     case Primitive::kPrimLong: {
1066       GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1067       GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1068       Location rhs_location = locations->InAt(1);
1069 
1070       GpuRegister rhs_reg = ZERO;
1071       int64_t rhs_imm = 0;
1072       bool use_imm = rhs_location.IsConstant();
1073       if (use_imm) {
1074         rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1075       } else {
1076         rhs_reg = rhs_location.AsRegister<GpuRegister>();
1077       }
1078 
1079       if (instruction->IsAnd()) {
1080         if (use_imm)
1081           __ Andi(dst, lhs, rhs_imm);
1082         else
1083           __ And(dst, lhs, rhs_reg);
1084       } else if (instruction->IsOr()) {
1085         if (use_imm)
1086           __ Ori(dst, lhs, rhs_imm);
1087         else
1088           __ Or(dst, lhs, rhs_reg);
1089       } else if (instruction->IsXor()) {
1090         if (use_imm)
1091           __ Xori(dst, lhs, rhs_imm);
1092         else
1093           __ Xor(dst, lhs, rhs_reg);
1094       } else if (instruction->IsAdd()) {
1095         if (type == Primitive::kPrimInt) {
1096           if (use_imm)
1097             __ Addiu(dst, lhs, rhs_imm);
1098           else
1099             __ Addu(dst, lhs, rhs_reg);
1100         } else {
1101           if (use_imm)
1102             __ Daddiu(dst, lhs, rhs_imm);
1103           else
1104             __ Daddu(dst, lhs, rhs_reg);
1105         }
1106       } else {
1107         DCHECK(instruction->IsSub());
1108         if (type == Primitive::kPrimInt) {
1109           if (use_imm)
1110             __ Addiu(dst, lhs, -rhs_imm);
1111           else
1112             __ Subu(dst, lhs, rhs_reg);
1113         } else {
1114           if (use_imm)
1115             __ Daddiu(dst, lhs, -rhs_imm);
1116           else
1117             __ Dsubu(dst, lhs, rhs_reg);
1118         }
1119       }
1120       break;
1121     }
1122     case Primitive::kPrimFloat:
1123     case Primitive::kPrimDouble: {
1124       FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1125       FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1126       FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1127       if (instruction->IsAdd()) {
1128         if (type == Primitive::kPrimFloat)
1129           __ AddS(dst, lhs, rhs);
1130         else
1131           __ AddD(dst, lhs, rhs);
1132       } else if (instruction->IsSub()) {
1133         if (type == Primitive::kPrimFloat)
1134           __ SubS(dst, lhs, rhs);
1135         else
1136           __ SubD(dst, lhs, rhs);
1137       } else {
1138         LOG(FATAL) << "Unexpected floating-point binary operation";
1139       }
1140       break;
1141     }
1142     default:
1143       LOG(FATAL) << "Unexpected binary operation type " << type;
1144   }
1145 }
1146 
HandleShift(HBinaryOperation * instr)1147 void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
1148   DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
1149 
1150   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1151   Primitive::Type type = instr->GetResultType();
1152   switch (type) {
1153     case Primitive::kPrimInt:
1154     case Primitive::kPrimLong: {
1155       locations->SetInAt(0, Location::RequiresRegister());
1156       locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1157       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1158       break;
1159     }
1160     default:
1161       LOG(FATAL) << "Unexpected shift type " << type;
1162   }
1163 }
1164 
HandleShift(HBinaryOperation * instr)1165 void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
1166   DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
1167   LocationSummary* locations = instr->GetLocations();
1168   Primitive::Type type = instr->GetType();
1169 
1170   switch (type) {
1171     case Primitive::kPrimInt:
1172     case Primitive::kPrimLong: {
1173       GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1174       GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1175       Location rhs_location = locations->InAt(1);
1176 
1177       GpuRegister rhs_reg = ZERO;
1178       int64_t rhs_imm = 0;
1179       bool use_imm = rhs_location.IsConstant();
1180       if (use_imm) {
1181         rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1182       } else {
1183         rhs_reg = rhs_location.AsRegister<GpuRegister>();
1184       }
1185 
1186       if (use_imm) {
1187         uint32_t shift_value = rhs_imm &
1188             (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
1189 
1190         if (shift_value == 0) {
1191           if (dst != lhs) {
1192             __ Move(dst, lhs);
1193           }
1194         } else if (type == Primitive::kPrimInt) {
1195           if (instr->IsShl()) {
1196             __ Sll(dst, lhs, shift_value);
1197           } else if (instr->IsShr()) {
1198             __ Sra(dst, lhs, shift_value);
1199           } else if (instr->IsUShr()) {
1200             __ Srl(dst, lhs, shift_value);
1201           } else {
1202             __ Rotr(dst, lhs, shift_value);
1203           }
1204         } else {
1205           if (shift_value < 32) {
1206             if (instr->IsShl()) {
1207               __ Dsll(dst, lhs, shift_value);
1208             } else if (instr->IsShr()) {
1209               __ Dsra(dst, lhs, shift_value);
1210             } else if (instr->IsUShr()) {
1211               __ Dsrl(dst, lhs, shift_value);
1212             } else {
1213               __ Drotr(dst, lhs, shift_value);
1214             }
1215           } else {
1216             shift_value -= 32;
1217             if (instr->IsShl()) {
1218               __ Dsll32(dst, lhs, shift_value);
1219             } else if (instr->IsShr()) {
1220               __ Dsra32(dst, lhs, shift_value);
1221             } else if (instr->IsUShr()) {
1222               __ Dsrl32(dst, lhs, shift_value);
1223             } else {
1224               __ Drotr32(dst, lhs, shift_value);
1225             }
1226           }
1227         }
1228       } else {
1229         if (type == Primitive::kPrimInt) {
1230           if (instr->IsShl()) {
1231             __ Sllv(dst, lhs, rhs_reg);
1232           } else if (instr->IsShr()) {
1233             __ Srav(dst, lhs, rhs_reg);
1234           } else if (instr->IsUShr()) {
1235             __ Srlv(dst, lhs, rhs_reg);
1236           } else {
1237             __ Rotrv(dst, lhs, rhs_reg);
1238           }
1239         } else {
1240           if (instr->IsShl()) {
1241             __ Dsllv(dst, lhs, rhs_reg);
1242           } else if (instr->IsShr()) {
1243             __ Dsrav(dst, lhs, rhs_reg);
1244           } else if (instr->IsUShr()) {
1245             __ Dsrlv(dst, lhs, rhs_reg);
1246           } else {
1247             __ Drotrv(dst, lhs, rhs_reg);
1248           }
1249         }
1250       }
1251       break;
1252     }
1253     default:
1254       LOG(FATAL) << "Unexpected shift operation type " << type;
1255   }
1256 }
1257 
VisitAdd(HAdd * instruction)1258 void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1259   HandleBinaryOp(instruction);
1260 }
1261 
VisitAdd(HAdd * instruction)1262 void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1263   HandleBinaryOp(instruction);
1264 }
1265 
VisitAnd(HAnd * instruction)1266 void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1267   HandleBinaryOp(instruction);
1268 }
1269 
VisitAnd(HAnd * instruction)1270 void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1271   HandleBinaryOp(instruction);
1272 }
1273 
VisitArrayGet(HArrayGet * instruction)1274 void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1275   LocationSummary* locations =
1276       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1277   locations->SetInAt(0, Location::RequiresRegister());
1278   locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1279   if (Primitive::IsFloatingPointType(instruction->GetType())) {
1280     locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1281   } else {
1282     locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1283   }
1284 }
1285 
VisitArrayGet(HArrayGet * instruction)1286 void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1287   LocationSummary* locations = instruction->GetLocations();
1288   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1289   Location index = locations->InAt(1);
1290   Primitive::Type type = instruction->GetType();
1291 
1292   switch (type) {
1293     case Primitive::kPrimBoolean: {
1294       uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1295       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1296       if (index.IsConstant()) {
1297         size_t offset =
1298             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1299         __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1300       } else {
1301         __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1302         __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1303       }
1304       break;
1305     }
1306 
1307     case Primitive::kPrimByte: {
1308       uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1309       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1310       if (index.IsConstant()) {
1311         size_t offset =
1312             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1313         __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1314       } else {
1315         __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1316         __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1317       }
1318       break;
1319     }
1320 
1321     case Primitive::kPrimShort: {
1322       uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1323       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1324       if (index.IsConstant()) {
1325         size_t offset =
1326             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1327         __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1328       } else {
1329         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1330         __ Daddu(TMP, obj, TMP);
1331         __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1332       }
1333       break;
1334     }
1335 
1336     case Primitive::kPrimChar: {
1337       uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1338       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1339       if (index.IsConstant()) {
1340         size_t offset =
1341             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1342         __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1343       } else {
1344         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1345         __ Daddu(TMP, obj, TMP);
1346         __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1347       }
1348       break;
1349     }
1350 
1351     case Primitive::kPrimInt:
1352     case Primitive::kPrimNot: {
1353       DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1354       uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1355       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1356       LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1357       if (index.IsConstant()) {
1358         size_t offset =
1359             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1360         __ LoadFromOffset(load_type, out, obj, offset);
1361       } else {
1362         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1363         __ Daddu(TMP, obj, TMP);
1364         __ LoadFromOffset(load_type, out, TMP, data_offset);
1365       }
1366       break;
1367     }
1368 
1369     case Primitive::kPrimLong: {
1370       uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1371       GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1372       if (index.IsConstant()) {
1373         size_t offset =
1374             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1375         __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1376       } else {
1377         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1378         __ Daddu(TMP, obj, TMP);
1379         __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1380       }
1381       break;
1382     }
1383 
1384     case Primitive::kPrimFloat: {
1385       uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1386       FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1387       if (index.IsConstant()) {
1388         size_t offset =
1389             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1390         __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1391       } else {
1392         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1393         __ Daddu(TMP, obj, TMP);
1394         __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1395       }
1396       break;
1397     }
1398 
1399     case Primitive::kPrimDouble: {
1400       uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1401       FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1402       if (index.IsConstant()) {
1403         size_t offset =
1404             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1405         __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1406       } else {
1407         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1408         __ Daddu(TMP, obj, TMP);
1409         __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1410       }
1411       break;
1412     }
1413 
1414     case Primitive::kPrimVoid:
1415       LOG(FATAL) << "Unreachable type " << instruction->GetType();
1416       UNREACHABLE();
1417   }
1418   codegen_->MaybeRecordImplicitNullCheck(instruction);
1419 }
1420 
VisitArrayLength(HArrayLength * instruction)1421 void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1422   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1423   locations->SetInAt(0, Location::RequiresRegister());
1424   locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1425 }
1426 
VisitArrayLength(HArrayLength * instruction)1427 void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1428   LocationSummary* locations = instruction->GetLocations();
1429   uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1430   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1431   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1432   __ LoadFromOffset(kLoadWord, out, obj, offset);
1433   codegen_->MaybeRecordImplicitNullCheck(instruction);
1434 }
1435 
VisitArraySet(HArraySet * instruction)1436 void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
1437   bool needs_runtime_call = instruction->NeedsTypeCheck();
1438   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1439       instruction,
1440       needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1441   if (needs_runtime_call) {
1442     InvokeRuntimeCallingConvention calling_convention;
1443     locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1444     locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1445     locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1446   } else {
1447     locations->SetInAt(0, Location::RequiresRegister());
1448     locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1449     if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1450       locations->SetInAt(2, Location::RequiresFpuRegister());
1451     } else {
1452       locations->SetInAt(2, Location::RequiresRegister());
1453     }
1454   }
1455 }
1456 
VisitArraySet(HArraySet * instruction)1457 void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1458   LocationSummary* locations = instruction->GetLocations();
1459   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1460   Location index = locations->InAt(1);
1461   Primitive::Type value_type = instruction->GetComponentType();
1462   bool needs_runtime_call = locations->WillCall();
1463   bool needs_write_barrier =
1464       CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1465 
1466   switch (value_type) {
1467     case Primitive::kPrimBoolean:
1468     case Primitive::kPrimByte: {
1469       uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1470       GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1471       if (index.IsConstant()) {
1472         size_t offset =
1473             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1474         __ StoreToOffset(kStoreByte, value, obj, offset);
1475       } else {
1476         __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1477         __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1478       }
1479       break;
1480     }
1481 
1482     case Primitive::kPrimShort:
1483     case Primitive::kPrimChar: {
1484       uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1485       GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1486       if (index.IsConstant()) {
1487         size_t offset =
1488             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1489         __ StoreToOffset(kStoreHalfword, value, obj, offset);
1490       } else {
1491         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1492         __ Daddu(TMP, obj, TMP);
1493         __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1494       }
1495       break;
1496     }
1497 
1498     case Primitive::kPrimInt:
1499     case Primitive::kPrimNot: {
1500       if (!needs_runtime_call) {
1501         uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1502         GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1503         if (index.IsConstant()) {
1504           size_t offset =
1505               (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1506           __ StoreToOffset(kStoreWord, value, obj, offset);
1507         } else {
1508           DCHECK(index.IsRegister()) << index;
1509           __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1510           __ Daddu(TMP, obj, TMP);
1511           __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1512         }
1513         codegen_->MaybeRecordImplicitNullCheck(instruction);
1514         if (needs_write_barrier) {
1515           DCHECK_EQ(value_type, Primitive::kPrimNot);
1516           codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
1517         }
1518       } else {
1519         DCHECK_EQ(value_type, Primitive::kPrimNot);
1520         codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1521                                 instruction,
1522                                 instruction->GetDexPc(),
1523                                 nullptr);
1524         CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1525       }
1526       break;
1527     }
1528 
1529     case Primitive::kPrimLong: {
1530       uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1531       GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1532       if (index.IsConstant()) {
1533         size_t offset =
1534             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1535         __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1536       } else {
1537         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1538         __ Daddu(TMP, obj, TMP);
1539         __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1540       }
1541       break;
1542     }
1543 
1544     case Primitive::kPrimFloat: {
1545       uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1546       FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1547       DCHECK(locations->InAt(2).IsFpuRegister());
1548       if (index.IsConstant()) {
1549         size_t offset =
1550             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1551         __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1552       } else {
1553         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1554         __ Daddu(TMP, obj, TMP);
1555         __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1556       }
1557       break;
1558     }
1559 
1560     case Primitive::kPrimDouble: {
1561       uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1562       FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1563       DCHECK(locations->InAt(2).IsFpuRegister());
1564       if (index.IsConstant()) {
1565         size_t offset =
1566             (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1567         __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1568       } else {
1569         __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1570         __ Daddu(TMP, obj, TMP);
1571         __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1572       }
1573       break;
1574     }
1575 
1576     case Primitive::kPrimVoid:
1577       LOG(FATAL) << "Unreachable type " << instruction->GetType();
1578       UNREACHABLE();
1579   }
1580 
1581   // Ints and objects are handled in the switch.
1582   if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1583     codegen_->MaybeRecordImplicitNullCheck(instruction);
1584   }
1585 }
1586 
VisitBoundsCheck(HBoundsCheck * instruction)1587 void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1588   LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1589       ? LocationSummary::kCallOnSlowPath
1590       : LocationSummary::kNoCall;
1591   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1592   locations->SetInAt(0, Location::RequiresRegister());
1593   locations->SetInAt(1, Location::RequiresRegister());
1594   if (instruction->HasUses()) {
1595     locations->SetOut(Location::SameAsFirstInput());
1596   }
1597 }
1598 
VisitBoundsCheck(HBoundsCheck * instruction)1599 void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1600   LocationSummary* locations = instruction->GetLocations();
1601   BoundsCheckSlowPathMIPS64* slow_path =
1602       new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
1603   codegen_->AddSlowPath(slow_path);
1604 
1605   GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1606   GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1607 
1608   // length is limited by the maximum positive signed 32-bit integer.
1609   // Unsigned comparison of length and index checks for index < 0
1610   // and for length <= index simultaneously.
1611   __ Bgeuc(index, length, slow_path->GetEntryLabel());
1612 }
1613 
VisitCheckCast(HCheckCast * instruction)1614 void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1615   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1616       instruction,
1617       LocationSummary::kCallOnSlowPath);
1618   locations->SetInAt(0, Location::RequiresRegister());
1619   locations->SetInAt(1, Location::RequiresRegister());
1620   // Note that TypeCheckSlowPathMIPS64 uses this register too.
1621   locations->AddTemp(Location::RequiresRegister());
1622 }
1623 
VisitCheckCast(HCheckCast * instruction)1624 void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1625   LocationSummary* locations = instruction->GetLocations();
1626   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1627   GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1628   GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1629 
1630   SlowPathCodeMIPS64* slow_path =
1631       new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
1632   codegen_->AddSlowPath(slow_path);
1633 
1634   // TODO: avoid this check if we know obj is not null.
1635   __ Beqzc(obj, slow_path->GetExitLabel());
1636   // Compare the class of `obj` with `cls`.
1637   __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1638   __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1639   __ Bind(slow_path->GetExitLabel());
1640 }
1641 
VisitClinitCheck(HClinitCheck * check)1642 void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1643   LocationSummary* locations =
1644       new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1645   locations->SetInAt(0, Location::RequiresRegister());
1646   if (check->HasUses()) {
1647     locations->SetOut(Location::SameAsFirstInput());
1648   }
1649 }
1650 
VisitClinitCheck(HClinitCheck * check)1651 void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1652   // We assume the class is not null.
1653   SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1654       check->GetLoadClass(),
1655       check,
1656       check->GetDexPc(),
1657       true);
1658   codegen_->AddSlowPath(slow_path);
1659   GenerateClassInitializationCheck(slow_path,
1660                                    check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1661 }
1662 
VisitCompare(HCompare * compare)1663 void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1664   Primitive::Type in_type = compare->InputAt(0)->GetType();
1665 
1666   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
1667 
1668   switch (in_type) {
1669     case Primitive::kPrimBoolean:
1670     case Primitive::kPrimByte:
1671     case Primitive::kPrimShort:
1672     case Primitive::kPrimChar:
1673     case Primitive::kPrimInt:
1674     case Primitive::kPrimLong:
1675       locations->SetInAt(0, Location::RequiresRegister());
1676       locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
1677       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1678       break;
1679 
1680     case Primitive::kPrimFloat:
1681     case Primitive::kPrimDouble:
1682       locations->SetInAt(0, Location::RequiresFpuRegister());
1683       locations->SetInAt(1, Location::RequiresFpuRegister());
1684       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1685       break;
1686 
1687     default:
1688       LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1689   }
1690 }
1691 
VisitCompare(HCompare * instruction)1692 void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1693   LocationSummary* locations = instruction->GetLocations();
1694   GpuRegister res = locations->Out().AsRegister<GpuRegister>();
1695   Primitive::Type in_type = instruction->InputAt(0)->GetType();
1696 
1697   //  0 if: left == right
1698   //  1 if: left  > right
1699   // -1 if: left  < right
1700   switch (in_type) {
1701     case Primitive::kPrimBoolean:
1702     case Primitive::kPrimByte:
1703     case Primitive::kPrimShort:
1704     case Primitive::kPrimChar:
1705     case Primitive::kPrimInt:
1706     case Primitive::kPrimLong: {
1707       GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1708       Location rhs_location = locations->InAt(1);
1709       bool use_imm = rhs_location.IsConstant();
1710       GpuRegister rhs = ZERO;
1711       if (use_imm) {
1712         if (in_type == Primitive::kPrimLong) {
1713           int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1714           if (value != 0) {
1715             rhs = AT;
1716             __ LoadConst64(rhs, value);
1717           }
1718         } else {
1719           int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1720           if (value != 0) {
1721             rhs = AT;
1722             __ LoadConst32(rhs, value);
1723           }
1724         }
1725       } else {
1726         rhs = rhs_location.AsRegister<GpuRegister>();
1727       }
1728       __ Slt(TMP, lhs, rhs);
1729       __ Slt(res, rhs, lhs);
1730       __ Subu(res, res, TMP);
1731       break;
1732     }
1733 
1734     case Primitive::kPrimFloat: {
1735       FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1736       FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1737       Mips64Label done;
1738       __ CmpEqS(FTMP, lhs, rhs);
1739       __ LoadConst32(res, 0);
1740       __ Bc1nez(FTMP, &done);
1741       if (instruction->IsGtBias()) {
1742         __ CmpLtS(FTMP, lhs, rhs);
1743         __ LoadConst32(res, -1);
1744         __ Bc1nez(FTMP, &done);
1745         __ LoadConst32(res, 1);
1746       } else {
1747         __ CmpLtS(FTMP, rhs, lhs);
1748         __ LoadConst32(res, 1);
1749         __ Bc1nez(FTMP, &done);
1750         __ LoadConst32(res, -1);
1751       }
1752       __ Bind(&done);
1753       break;
1754     }
1755 
1756     case Primitive::kPrimDouble: {
1757       FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1758       FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1759       Mips64Label done;
1760       __ CmpEqD(FTMP, lhs, rhs);
1761       __ LoadConst32(res, 0);
1762       __ Bc1nez(FTMP, &done);
1763       if (instruction->IsGtBias()) {
1764         __ CmpLtD(FTMP, lhs, rhs);
1765         __ LoadConst32(res, -1);
1766         __ Bc1nez(FTMP, &done);
1767         __ LoadConst32(res, 1);
1768       } else {
1769         __ CmpLtD(FTMP, rhs, lhs);
1770         __ LoadConst32(res, 1);
1771         __ Bc1nez(FTMP, &done);
1772         __ LoadConst32(res, -1);
1773       }
1774       __ Bind(&done);
1775       break;
1776     }
1777 
1778     default:
1779       LOG(FATAL) << "Unimplemented compare type " << in_type;
1780   }
1781 }
1782 
HandleCondition(HCondition * instruction)1783 void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
1784   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1785   switch (instruction->InputAt(0)->GetType()) {
1786     default:
1787     case Primitive::kPrimLong:
1788       locations->SetInAt(0, Location::RequiresRegister());
1789       locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1790       break;
1791 
1792     case Primitive::kPrimFloat:
1793     case Primitive::kPrimDouble:
1794       locations->SetInAt(0, Location::RequiresFpuRegister());
1795       locations->SetInAt(1, Location::RequiresFpuRegister());
1796       break;
1797   }
1798   if (!instruction->IsEmittedAtUseSite()) {
1799     locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1800   }
1801 }
1802 
HandleCondition(HCondition * instruction)1803 void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
1804   if (instruction->IsEmittedAtUseSite()) {
1805     return;
1806   }
1807 
1808   Primitive::Type type = instruction->InputAt(0)->GetType();
1809   LocationSummary* locations = instruction->GetLocations();
1810   GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1811   Mips64Label true_label;
1812 
1813   switch (type) {
1814     default:
1815       // Integer case.
1816       GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1817       return;
1818     case Primitive::kPrimLong:
1819       GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1820       return;
1821 
1822     case Primitive::kPrimFloat:
1823     case Primitive::kPrimDouble:
1824       // TODO: don't use branches.
1825       GenerateFpCompareAndBranch(instruction->GetCondition(),
1826                                  instruction->IsGtBias(),
1827                                  type,
1828                                  locations,
1829                                  &true_label);
1830       break;
1831   }
1832 
1833   // Convert the branches into the result.
1834   Mips64Label done;
1835 
1836   // False case: result = 0.
1837   __ LoadConst32(dst, 0);
1838   __ Bc(&done);
1839 
1840   // True case: result = 1.
1841   __ Bind(&true_label);
1842   __ LoadConst32(dst, 1);
1843   __ Bind(&done);
1844 }
1845 
DivRemOneOrMinusOne(HBinaryOperation * instruction)1846 void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1847   DCHECK(instruction->IsDiv() || instruction->IsRem());
1848   Primitive::Type type = instruction->GetResultType();
1849 
1850   LocationSummary* locations = instruction->GetLocations();
1851   Location second = locations->InAt(1);
1852   DCHECK(second.IsConstant());
1853 
1854   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1855   GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1856   int64_t imm = Int64FromConstant(second.GetConstant());
1857   DCHECK(imm == 1 || imm == -1);
1858 
1859   if (instruction->IsRem()) {
1860     __ Move(out, ZERO);
1861   } else {
1862     if (imm == -1) {
1863       if (type == Primitive::kPrimInt) {
1864         __ Subu(out, ZERO, dividend);
1865       } else {
1866         DCHECK_EQ(type, Primitive::kPrimLong);
1867         __ Dsubu(out, ZERO, dividend);
1868       }
1869     } else if (out != dividend) {
1870       __ Move(out, dividend);
1871     }
1872   }
1873 }
1874 
DivRemByPowerOfTwo(HBinaryOperation * instruction)1875 void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1876   DCHECK(instruction->IsDiv() || instruction->IsRem());
1877   Primitive::Type type = instruction->GetResultType();
1878 
1879   LocationSummary* locations = instruction->GetLocations();
1880   Location second = locations->InAt(1);
1881   DCHECK(second.IsConstant());
1882 
1883   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1884   GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1885   int64_t imm = Int64FromConstant(second.GetConstant());
1886   uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
1887   int ctz_imm = CTZ(abs_imm);
1888 
1889   if (instruction->IsDiv()) {
1890     if (type == Primitive::kPrimInt) {
1891       if (ctz_imm == 1) {
1892         // Fast path for division by +/-2, which is very common.
1893         __ Srl(TMP, dividend, 31);
1894       } else {
1895         __ Sra(TMP, dividend, 31);
1896         __ Srl(TMP, TMP, 32 - ctz_imm);
1897       }
1898       __ Addu(out, dividend, TMP);
1899       __ Sra(out, out, ctz_imm);
1900       if (imm < 0) {
1901         __ Subu(out, ZERO, out);
1902       }
1903     } else {
1904       DCHECK_EQ(type, Primitive::kPrimLong);
1905       if (ctz_imm == 1) {
1906         // Fast path for division by +/-2, which is very common.
1907         __ Dsrl32(TMP, dividend, 31);
1908       } else {
1909         __ Dsra32(TMP, dividend, 31);
1910         if (ctz_imm > 32) {
1911           __ Dsrl(TMP, TMP, 64 - ctz_imm);
1912         } else {
1913           __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1914         }
1915       }
1916       __ Daddu(out, dividend, TMP);
1917       if (ctz_imm < 32) {
1918         __ Dsra(out, out, ctz_imm);
1919       } else {
1920         __ Dsra32(out, out, ctz_imm - 32);
1921       }
1922       if (imm < 0) {
1923         __ Dsubu(out, ZERO, out);
1924       }
1925     }
1926   } else {
1927     if (type == Primitive::kPrimInt) {
1928       if (ctz_imm == 1) {
1929         // Fast path for modulo +/-2, which is very common.
1930         __ Sra(TMP, dividend, 31);
1931         __ Subu(out, dividend, TMP);
1932         __ Andi(out, out, 1);
1933         __ Addu(out, out, TMP);
1934       } else {
1935         __ Sra(TMP, dividend, 31);
1936         __ Srl(TMP, TMP, 32 - ctz_imm);
1937         __ Addu(out, dividend, TMP);
1938         if (IsUint<16>(abs_imm - 1)) {
1939           __ Andi(out, out, abs_imm - 1);
1940         } else {
1941           __ Sll(out, out, 32 - ctz_imm);
1942           __ Srl(out, out, 32 - ctz_imm);
1943         }
1944         __ Subu(out, out, TMP);
1945       }
1946     } else {
1947       DCHECK_EQ(type, Primitive::kPrimLong);
1948       if (ctz_imm == 1) {
1949         // Fast path for modulo +/-2, which is very common.
1950         __ Dsra32(TMP, dividend, 31);
1951         __ Dsubu(out, dividend, TMP);
1952         __ Andi(out, out, 1);
1953         __ Daddu(out, out, TMP);
1954       } else {
1955         __ Dsra32(TMP, dividend, 31);
1956         if (ctz_imm > 32) {
1957           __ Dsrl(TMP, TMP, 64 - ctz_imm);
1958         } else {
1959           __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1960         }
1961         __ Daddu(out, dividend, TMP);
1962         if (IsUint<16>(abs_imm - 1)) {
1963           __ Andi(out, out, abs_imm - 1);
1964         } else {
1965           if (ctz_imm > 32) {
1966             __ Dsll(out, out, 64 - ctz_imm);
1967             __ Dsrl(out, out, 64 - ctz_imm);
1968           } else {
1969             __ Dsll32(out, out, 32 - ctz_imm);
1970             __ Dsrl32(out, out, 32 - ctz_imm);
1971           }
1972         }
1973         __ Dsubu(out, out, TMP);
1974       }
1975     }
1976   }
1977 }
1978 
GenerateDivRemWithAnyConstant(HBinaryOperation * instruction)1979 void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1980   DCHECK(instruction->IsDiv() || instruction->IsRem());
1981 
1982   LocationSummary* locations = instruction->GetLocations();
1983   Location second = locations->InAt(1);
1984   DCHECK(second.IsConstant());
1985 
1986   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1987   GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1988   int64_t imm = Int64FromConstant(second.GetConstant());
1989 
1990   Primitive::Type type = instruction->GetResultType();
1991   DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1992 
1993   int64_t magic;
1994   int shift;
1995   CalculateMagicAndShiftForDivRem(imm,
1996                                   (type == Primitive::kPrimLong),
1997                                   &magic,
1998                                   &shift);
1999 
2000   if (type == Primitive::kPrimInt) {
2001     __ LoadConst32(TMP, magic);
2002     __ MuhR6(TMP, dividend, TMP);
2003 
2004     if (imm > 0 && magic < 0) {
2005       __ Addu(TMP, TMP, dividend);
2006     } else if (imm < 0 && magic > 0) {
2007       __ Subu(TMP, TMP, dividend);
2008     }
2009 
2010     if (shift != 0) {
2011       __ Sra(TMP, TMP, shift);
2012     }
2013 
2014     if (instruction->IsDiv()) {
2015       __ Sra(out, TMP, 31);
2016       __ Subu(out, TMP, out);
2017     } else {
2018       __ Sra(AT, TMP, 31);
2019       __ Subu(AT, TMP, AT);
2020       __ LoadConst32(TMP, imm);
2021       __ MulR6(TMP, AT, TMP);
2022       __ Subu(out, dividend, TMP);
2023     }
2024   } else {
2025     __ LoadConst64(TMP, magic);
2026     __ Dmuh(TMP, dividend, TMP);
2027 
2028     if (imm > 0 && magic < 0) {
2029       __ Daddu(TMP, TMP, dividend);
2030     } else if (imm < 0 && magic > 0) {
2031       __ Dsubu(TMP, TMP, dividend);
2032     }
2033 
2034     if (shift >= 32) {
2035       __ Dsra32(TMP, TMP, shift - 32);
2036     } else if (shift > 0) {
2037       __ Dsra(TMP, TMP, shift);
2038     }
2039 
2040     if (instruction->IsDiv()) {
2041       __ Dsra32(out, TMP, 31);
2042       __ Dsubu(out, TMP, out);
2043     } else {
2044       __ Dsra32(AT, TMP, 31);
2045       __ Dsubu(AT, TMP, AT);
2046       __ LoadConst64(TMP, imm);
2047       __ Dmul(TMP, AT, TMP);
2048       __ Dsubu(out, dividend, TMP);
2049     }
2050   }
2051 }
2052 
GenerateDivRemIntegral(HBinaryOperation * instruction)2053 void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2054   DCHECK(instruction->IsDiv() || instruction->IsRem());
2055   Primitive::Type type = instruction->GetResultType();
2056   DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2057 
2058   LocationSummary* locations = instruction->GetLocations();
2059   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2060   Location second = locations->InAt(1);
2061 
2062   if (second.IsConstant()) {
2063     int64_t imm = Int64FromConstant(second.GetConstant());
2064     if (imm == 0) {
2065       // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2066     } else if (imm == 1 || imm == -1) {
2067       DivRemOneOrMinusOne(instruction);
2068     } else if (IsPowerOfTwo(AbsOrMin(imm))) {
2069       DivRemByPowerOfTwo(instruction);
2070     } else {
2071       DCHECK(imm <= -2 || imm >= 2);
2072       GenerateDivRemWithAnyConstant(instruction);
2073     }
2074   } else {
2075     GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2076     GpuRegister divisor = second.AsRegister<GpuRegister>();
2077     if (instruction->IsDiv()) {
2078       if (type == Primitive::kPrimInt)
2079         __ DivR6(out, dividend, divisor);
2080       else
2081         __ Ddiv(out, dividend, divisor);
2082     } else {
2083       if (type == Primitive::kPrimInt)
2084         __ ModR6(out, dividend, divisor);
2085       else
2086         __ Dmod(out, dividend, divisor);
2087     }
2088   }
2089 }
2090 
VisitDiv(HDiv * div)2091 void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2092   LocationSummary* locations =
2093       new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2094   switch (div->GetResultType()) {
2095     case Primitive::kPrimInt:
2096     case Primitive::kPrimLong:
2097       locations->SetInAt(0, Location::RequiresRegister());
2098       locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2099       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2100       break;
2101 
2102     case Primitive::kPrimFloat:
2103     case Primitive::kPrimDouble:
2104       locations->SetInAt(0, Location::RequiresFpuRegister());
2105       locations->SetInAt(1, Location::RequiresFpuRegister());
2106       locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2107       break;
2108 
2109     default:
2110       LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2111   }
2112 }
2113 
VisitDiv(HDiv * instruction)2114 void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2115   Primitive::Type type = instruction->GetType();
2116   LocationSummary* locations = instruction->GetLocations();
2117 
2118   switch (type) {
2119     case Primitive::kPrimInt:
2120     case Primitive::kPrimLong:
2121       GenerateDivRemIntegral(instruction);
2122       break;
2123     case Primitive::kPrimFloat:
2124     case Primitive::kPrimDouble: {
2125       FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2126       FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2127       FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2128       if (type == Primitive::kPrimFloat)
2129         __ DivS(dst, lhs, rhs);
2130       else
2131         __ DivD(dst, lhs, rhs);
2132       break;
2133     }
2134     default:
2135       LOG(FATAL) << "Unexpected div type " << type;
2136   }
2137 }
2138 
VisitDivZeroCheck(HDivZeroCheck * instruction)2139 void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2140   LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2141       ? LocationSummary::kCallOnSlowPath
2142       : LocationSummary::kNoCall;
2143   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2144   locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2145   if (instruction->HasUses()) {
2146     locations->SetOut(Location::SameAsFirstInput());
2147   }
2148 }
2149 
VisitDivZeroCheck(HDivZeroCheck * instruction)2150 void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2151   SlowPathCodeMIPS64* slow_path =
2152       new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2153   codegen_->AddSlowPath(slow_path);
2154   Location value = instruction->GetLocations()->InAt(0);
2155 
2156   Primitive::Type type = instruction->GetType();
2157 
2158   if (!Primitive::IsIntegralType(type)) {
2159     LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2160     return;
2161   }
2162 
2163   if (value.IsConstant()) {
2164     int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2165     if (divisor == 0) {
2166       __ Bc(slow_path->GetEntryLabel());
2167     } else {
2168       // A division by a non-null constant is valid. We don't need to perform
2169       // any check, so simply fall through.
2170     }
2171   } else {
2172     __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2173   }
2174 }
2175 
VisitDoubleConstant(HDoubleConstant * constant)2176 void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2177   LocationSummary* locations =
2178       new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2179   locations->SetOut(Location::ConstantLocation(constant));
2180 }
2181 
VisitDoubleConstant(HDoubleConstant * cst ATTRIBUTE_UNUSED)2182 void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2183   // Will be generated at use site.
2184 }
2185 
VisitExit(HExit * exit)2186 void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2187   exit->SetLocations(nullptr);
2188 }
2189 
VisitExit(HExit * exit ATTRIBUTE_UNUSED)2190 void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2191 }
2192 
VisitFloatConstant(HFloatConstant * constant)2193 void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2194   LocationSummary* locations =
2195       new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2196   locations->SetOut(Location::ConstantLocation(constant));
2197 }
2198 
VisitFloatConstant(HFloatConstant * constant ATTRIBUTE_UNUSED)2199 void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2200   // Will be generated at use site.
2201 }
2202 
HandleGoto(HInstruction * got,HBasicBlock * successor)2203 void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2204   DCHECK(!successor->IsExitBlock());
2205   HBasicBlock* block = got->GetBlock();
2206   HInstruction* previous = got->GetPrevious();
2207   HLoopInformation* info = block->GetLoopInformation();
2208 
2209   if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2210     codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2211     GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2212     return;
2213   }
2214   if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2215     GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2216   }
2217   if (!codegen_->GoesToNextBlock(block, successor)) {
2218     __ Bc(codegen_->GetLabelOf(successor));
2219   }
2220 }
2221 
VisitGoto(HGoto * got)2222 void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2223   got->SetLocations(nullptr);
2224 }
2225 
VisitGoto(HGoto * got)2226 void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2227   HandleGoto(got, got->GetSuccessor());
2228 }
2229 
VisitTryBoundary(HTryBoundary * try_boundary)2230 void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2231   try_boundary->SetLocations(nullptr);
2232 }
2233 
VisitTryBoundary(HTryBoundary * try_boundary)2234 void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2235   HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2236   if (!successor->IsExitBlock()) {
2237     HandleGoto(try_boundary, successor);
2238   }
2239 }
2240 
GenerateIntLongCompare(IfCondition cond,bool is64bit,LocationSummary * locations)2241 void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2242                                                             bool is64bit,
2243                                                             LocationSummary* locations) {
2244   GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2245   GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2246   Location rhs_location = locations->InAt(1);
2247   GpuRegister rhs_reg = ZERO;
2248   int64_t rhs_imm = 0;
2249   bool use_imm = rhs_location.IsConstant();
2250   if (use_imm) {
2251     if (is64bit) {
2252       rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2253     } else {
2254       rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2255     }
2256   } else {
2257     rhs_reg = rhs_location.AsRegister<GpuRegister>();
2258   }
2259   int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2260 
2261   switch (cond) {
2262     case kCondEQ:
2263     case kCondNE:
2264       if (use_imm && IsUint<16>(rhs_imm)) {
2265         __ Xori(dst, lhs, rhs_imm);
2266       } else {
2267         if (use_imm) {
2268           rhs_reg = TMP;
2269           __ LoadConst64(rhs_reg, rhs_imm);
2270         }
2271         __ Xor(dst, lhs, rhs_reg);
2272       }
2273       if (cond == kCondEQ) {
2274         __ Sltiu(dst, dst, 1);
2275       } else {
2276         __ Sltu(dst, ZERO, dst);
2277       }
2278       break;
2279 
2280     case kCondLT:
2281     case kCondGE:
2282       if (use_imm && IsInt<16>(rhs_imm)) {
2283         __ Slti(dst, lhs, rhs_imm);
2284       } else {
2285         if (use_imm) {
2286           rhs_reg = TMP;
2287           __ LoadConst64(rhs_reg, rhs_imm);
2288         }
2289         __ Slt(dst, lhs, rhs_reg);
2290       }
2291       if (cond == kCondGE) {
2292         // Simulate lhs >= rhs via !(lhs < rhs) since there's
2293         // only the slt instruction but no sge.
2294         __ Xori(dst, dst, 1);
2295       }
2296       break;
2297 
2298     case kCondLE:
2299     case kCondGT:
2300       if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2301         // Simulate lhs <= rhs via lhs < rhs + 1.
2302         __ Slti(dst, lhs, rhs_imm_plus_one);
2303         if (cond == kCondGT) {
2304           // Simulate lhs > rhs via !(lhs <= rhs) since there's
2305           // only the slti instruction but no sgti.
2306           __ Xori(dst, dst, 1);
2307         }
2308       } else {
2309         if (use_imm) {
2310           rhs_reg = TMP;
2311           __ LoadConst64(rhs_reg, rhs_imm);
2312         }
2313         __ Slt(dst, rhs_reg, lhs);
2314         if (cond == kCondLE) {
2315           // Simulate lhs <= rhs via !(rhs < lhs) since there's
2316           // only the slt instruction but no sle.
2317           __ Xori(dst, dst, 1);
2318         }
2319       }
2320       break;
2321 
2322     case kCondB:
2323     case kCondAE:
2324       if (use_imm && IsInt<16>(rhs_imm)) {
2325         // Sltiu sign-extends its 16-bit immediate operand before
2326         // the comparison and thus lets us compare directly with
2327         // unsigned values in the ranges [0, 0x7fff] and
2328         // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2329         __ Sltiu(dst, lhs, rhs_imm);
2330       } else {
2331         if (use_imm) {
2332           rhs_reg = TMP;
2333           __ LoadConst64(rhs_reg, rhs_imm);
2334         }
2335         __ Sltu(dst, lhs, rhs_reg);
2336       }
2337       if (cond == kCondAE) {
2338         // Simulate lhs >= rhs via !(lhs < rhs) since there's
2339         // only the sltu instruction but no sgeu.
2340         __ Xori(dst, dst, 1);
2341       }
2342       break;
2343 
2344     case kCondBE:
2345     case kCondA:
2346       if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2347         // Simulate lhs <= rhs via lhs < rhs + 1.
2348         // Note that this only works if rhs + 1 does not overflow
2349         // to 0, hence the check above.
2350         // Sltiu sign-extends its 16-bit immediate operand before
2351         // the comparison and thus lets us compare directly with
2352         // unsigned values in the ranges [0, 0x7fff] and
2353         // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2354         __ Sltiu(dst, lhs, rhs_imm_plus_one);
2355         if (cond == kCondA) {
2356           // Simulate lhs > rhs via !(lhs <= rhs) since there's
2357           // only the sltiu instruction but no sgtiu.
2358           __ Xori(dst, dst, 1);
2359         }
2360       } else {
2361         if (use_imm) {
2362           rhs_reg = TMP;
2363           __ LoadConst64(rhs_reg, rhs_imm);
2364         }
2365         __ Sltu(dst, rhs_reg, lhs);
2366         if (cond == kCondBE) {
2367           // Simulate lhs <= rhs via !(rhs < lhs) since there's
2368           // only the sltu instruction but no sleu.
2369           __ Xori(dst, dst, 1);
2370         }
2371       }
2372       break;
2373   }
2374 }
2375 
GenerateIntLongCompareAndBranch(IfCondition cond,bool is64bit,LocationSummary * locations,Mips64Label * label)2376 void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2377                                                                      bool is64bit,
2378                                                                      LocationSummary* locations,
2379                                                                      Mips64Label* label) {
2380   GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2381   Location rhs_location = locations->InAt(1);
2382   GpuRegister rhs_reg = ZERO;
2383   int64_t rhs_imm = 0;
2384   bool use_imm = rhs_location.IsConstant();
2385   if (use_imm) {
2386     if (is64bit) {
2387       rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2388     } else {
2389       rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2390     }
2391   } else {
2392     rhs_reg = rhs_location.AsRegister<GpuRegister>();
2393   }
2394 
2395   if (use_imm && rhs_imm == 0) {
2396     switch (cond) {
2397       case kCondEQ:
2398       case kCondBE:  // <= 0 if zero
2399         __ Beqzc(lhs, label);
2400         break;
2401       case kCondNE:
2402       case kCondA:  // > 0 if non-zero
2403         __ Bnezc(lhs, label);
2404         break;
2405       case kCondLT:
2406         __ Bltzc(lhs, label);
2407         break;
2408       case kCondGE:
2409         __ Bgezc(lhs, label);
2410         break;
2411       case kCondLE:
2412         __ Blezc(lhs, label);
2413         break;
2414       case kCondGT:
2415         __ Bgtzc(lhs, label);
2416         break;
2417       case kCondB:  // always false
2418         break;
2419       case kCondAE:  // always true
2420         __ Bc(label);
2421         break;
2422     }
2423   } else {
2424     if (use_imm) {
2425       rhs_reg = TMP;
2426       __ LoadConst64(rhs_reg, rhs_imm);
2427     }
2428     switch (cond) {
2429       case kCondEQ:
2430         __ Beqc(lhs, rhs_reg, label);
2431         break;
2432       case kCondNE:
2433         __ Bnec(lhs, rhs_reg, label);
2434         break;
2435       case kCondLT:
2436         __ Bltc(lhs, rhs_reg, label);
2437         break;
2438       case kCondGE:
2439         __ Bgec(lhs, rhs_reg, label);
2440         break;
2441       case kCondLE:
2442         __ Bgec(rhs_reg, lhs, label);
2443         break;
2444       case kCondGT:
2445         __ Bltc(rhs_reg, lhs, label);
2446         break;
2447       case kCondB:
2448         __ Bltuc(lhs, rhs_reg, label);
2449         break;
2450       case kCondAE:
2451         __ Bgeuc(lhs, rhs_reg, label);
2452         break;
2453       case kCondBE:
2454         __ Bgeuc(rhs_reg, lhs, label);
2455         break;
2456       case kCondA:
2457         __ Bltuc(rhs_reg, lhs, label);
2458         break;
2459     }
2460   }
2461 }
2462 
GenerateFpCompareAndBranch(IfCondition cond,bool gt_bias,Primitive::Type type,LocationSummary * locations,Mips64Label * label)2463 void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2464                                                                 bool gt_bias,
2465                                                                 Primitive::Type type,
2466                                                                 LocationSummary* locations,
2467                                                                 Mips64Label* label) {
2468   FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2469   FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2470   if (type == Primitive::kPrimFloat) {
2471     switch (cond) {
2472       case kCondEQ:
2473         __ CmpEqS(FTMP, lhs, rhs);
2474         __ Bc1nez(FTMP, label);
2475         break;
2476       case kCondNE:
2477         __ CmpEqS(FTMP, lhs, rhs);
2478         __ Bc1eqz(FTMP, label);
2479         break;
2480       case kCondLT:
2481         if (gt_bias) {
2482           __ CmpLtS(FTMP, lhs, rhs);
2483         } else {
2484           __ CmpUltS(FTMP, lhs, rhs);
2485         }
2486         __ Bc1nez(FTMP, label);
2487         break;
2488       case kCondLE:
2489         if (gt_bias) {
2490           __ CmpLeS(FTMP, lhs, rhs);
2491         } else {
2492           __ CmpUleS(FTMP, lhs, rhs);
2493         }
2494         __ Bc1nez(FTMP, label);
2495         break;
2496       case kCondGT:
2497         if (gt_bias) {
2498           __ CmpUltS(FTMP, rhs, lhs);
2499         } else {
2500           __ CmpLtS(FTMP, rhs, lhs);
2501         }
2502         __ Bc1nez(FTMP, label);
2503         break;
2504       case kCondGE:
2505         if (gt_bias) {
2506           __ CmpUleS(FTMP, rhs, lhs);
2507         } else {
2508           __ CmpLeS(FTMP, rhs, lhs);
2509         }
2510         __ Bc1nez(FTMP, label);
2511         break;
2512       default:
2513         LOG(FATAL) << "Unexpected non-floating-point condition";
2514     }
2515   } else {
2516     DCHECK_EQ(type, Primitive::kPrimDouble);
2517     switch (cond) {
2518       case kCondEQ:
2519         __ CmpEqD(FTMP, lhs, rhs);
2520         __ Bc1nez(FTMP, label);
2521         break;
2522       case kCondNE:
2523         __ CmpEqD(FTMP, lhs, rhs);
2524         __ Bc1eqz(FTMP, label);
2525         break;
2526       case kCondLT:
2527         if (gt_bias) {
2528           __ CmpLtD(FTMP, lhs, rhs);
2529         } else {
2530           __ CmpUltD(FTMP, lhs, rhs);
2531         }
2532         __ Bc1nez(FTMP, label);
2533         break;
2534       case kCondLE:
2535         if (gt_bias) {
2536           __ CmpLeD(FTMP, lhs, rhs);
2537         } else {
2538           __ CmpUleD(FTMP, lhs, rhs);
2539         }
2540         __ Bc1nez(FTMP, label);
2541         break;
2542       case kCondGT:
2543         if (gt_bias) {
2544           __ CmpUltD(FTMP, rhs, lhs);
2545         } else {
2546           __ CmpLtD(FTMP, rhs, lhs);
2547         }
2548         __ Bc1nez(FTMP, label);
2549         break;
2550       case kCondGE:
2551         if (gt_bias) {
2552           __ CmpUleD(FTMP, rhs, lhs);
2553         } else {
2554           __ CmpLeD(FTMP, rhs, lhs);
2555         }
2556         __ Bc1nez(FTMP, label);
2557         break;
2558       default:
2559         LOG(FATAL) << "Unexpected non-floating-point condition";
2560     }
2561   }
2562 }
2563 
GenerateTestAndBranch(HInstruction * instruction,size_t condition_input_index,Mips64Label * true_target,Mips64Label * false_target)2564 void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
2565                                                            size_t condition_input_index,
2566                                                            Mips64Label* true_target,
2567                                                            Mips64Label* false_target) {
2568   HInstruction* cond = instruction->InputAt(condition_input_index);
2569 
2570   if (true_target == nullptr && false_target == nullptr) {
2571     // Nothing to do. The code always falls through.
2572     return;
2573   } else if (cond->IsIntConstant()) {
2574     // Constant condition, statically compared against "true" (integer value 1).
2575     if (cond->AsIntConstant()->IsTrue()) {
2576       if (true_target != nullptr) {
2577         __ Bc(true_target);
2578       }
2579     } else {
2580       DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
2581       if (false_target != nullptr) {
2582         __ Bc(false_target);
2583       }
2584     }
2585     return;
2586   }
2587 
2588   // The following code generates these patterns:
2589   //  (1) true_target == nullptr && false_target != nullptr
2590   //        - opposite condition true => branch to false_target
2591   //  (2) true_target != nullptr && false_target == nullptr
2592   //        - condition true => branch to true_target
2593   //  (3) true_target != nullptr && false_target != nullptr
2594   //        - condition true => branch to true_target
2595   //        - branch to false_target
2596   if (IsBooleanValueOrMaterializedCondition(cond)) {
2597     // The condition instruction has been materialized, compare the output to 0.
2598     Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2599     DCHECK(cond_val.IsRegister());
2600     if (true_target == nullptr) {
2601       __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2602     } else {
2603       __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2604     }
2605   } else {
2606     // The condition instruction has not been materialized, use its inputs as
2607     // the comparison and its condition as the branch condition.
2608     HCondition* condition = cond->AsCondition();
2609     Primitive::Type type = condition->InputAt(0)->GetType();
2610     LocationSummary* locations = cond->GetLocations();
2611     IfCondition if_cond = condition->GetCondition();
2612     Mips64Label* branch_target = true_target;
2613 
2614     if (true_target == nullptr) {
2615       if_cond = condition->GetOppositeCondition();
2616       branch_target = false_target;
2617     }
2618 
2619     switch (type) {
2620       default:
2621         GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2622         break;
2623       case Primitive::kPrimLong:
2624         GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2625         break;
2626       case Primitive::kPrimFloat:
2627       case Primitive::kPrimDouble:
2628         GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2629         break;
2630     }
2631   }
2632 
2633   // If neither branch falls through (case 3), the conditional branch to `true_target`
2634   // was already emitted (case 2) and we need to emit a jump to `false_target`.
2635   if (true_target != nullptr && false_target != nullptr) {
2636     __ Bc(false_target);
2637   }
2638 }
2639 
VisitIf(HIf * if_instr)2640 void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2641   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2642   if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2643     locations->SetInAt(0, Location::RequiresRegister());
2644   }
2645 }
2646 
VisitIf(HIf * if_instr)2647 void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
2648   HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2649   HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2650   Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2651       nullptr : codegen_->GetLabelOf(true_successor);
2652   Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2653       nullptr : codegen_->GetLabelOf(false_successor);
2654   GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2655 }
2656 
VisitDeoptimize(HDeoptimize * deoptimize)2657 void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2658   LocationSummary* locations = new (GetGraph()->GetArena())
2659       LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2660   if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2661     locations->SetInAt(0, Location::RequiresRegister());
2662   }
2663 }
2664 
VisitDeoptimize(HDeoptimize * deoptimize)2665 void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2666   SlowPathCodeMIPS64* slow_path =
2667       deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
2668   GenerateTestAndBranch(deoptimize,
2669                         /* condition_input_index */ 0,
2670                         slow_path->GetEntryLabel(),
2671                         /* false_target */ nullptr);
2672 }
2673 
VisitSelect(HSelect * select)2674 void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2675   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2676   if (Primitive::IsFloatingPointType(select->GetType())) {
2677     locations->SetInAt(0, Location::RequiresFpuRegister());
2678     locations->SetInAt(1, Location::RequiresFpuRegister());
2679   } else {
2680     locations->SetInAt(0, Location::RequiresRegister());
2681     locations->SetInAt(1, Location::RequiresRegister());
2682   }
2683   if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2684     locations->SetInAt(2, Location::RequiresRegister());
2685   }
2686   locations->SetOut(Location::SameAsFirstInput());
2687 }
2688 
VisitSelect(HSelect * select)2689 void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2690   LocationSummary* locations = select->GetLocations();
2691   Mips64Label false_target;
2692   GenerateTestAndBranch(select,
2693                         /* condition_input_index */ 2,
2694                         /* true_target */ nullptr,
2695                         &false_target);
2696   codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2697   __ Bind(&false_target);
2698 }
2699 
VisitNativeDebugInfo(HNativeDebugInfo * info)2700 void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2701   new (GetGraph()->GetArena()) LocationSummary(info);
2702 }
2703 
VisitNativeDebugInfo(HNativeDebugInfo *)2704 void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2705   // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2706 }
2707 
GenerateNop()2708 void CodeGeneratorMIPS64::GenerateNop() {
2709   __ Nop();
2710 }
2711 
HandleFieldGet(HInstruction * instruction,const FieldInfo & field_info ATTRIBUTE_UNUSED)2712 void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2713                                             const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2714   LocationSummary* locations =
2715       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2716   locations->SetInAt(0, Location::RequiresRegister());
2717   if (Primitive::IsFloatingPointType(instruction->GetType())) {
2718     locations->SetOut(Location::RequiresFpuRegister());
2719   } else {
2720     locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2721   }
2722 }
2723 
HandleFieldGet(HInstruction * instruction,const FieldInfo & field_info)2724 void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2725                                                     const FieldInfo& field_info) {
2726   Primitive::Type type = field_info.GetFieldType();
2727   LocationSummary* locations = instruction->GetLocations();
2728   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2729   LoadOperandType load_type = kLoadUnsignedByte;
2730   switch (type) {
2731     case Primitive::kPrimBoolean:
2732       load_type = kLoadUnsignedByte;
2733       break;
2734     case Primitive::kPrimByte:
2735       load_type = kLoadSignedByte;
2736       break;
2737     case Primitive::kPrimShort:
2738       load_type = kLoadSignedHalfword;
2739       break;
2740     case Primitive::kPrimChar:
2741       load_type = kLoadUnsignedHalfword;
2742       break;
2743     case Primitive::kPrimInt:
2744     case Primitive::kPrimFloat:
2745       load_type = kLoadWord;
2746       break;
2747     case Primitive::kPrimLong:
2748     case Primitive::kPrimDouble:
2749       load_type = kLoadDoubleword;
2750       break;
2751     case Primitive::kPrimNot:
2752       load_type = kLoadUnsignedWord;
2753       break;
2754     case Primitive::kPrimVoid:
2755       LOG(FATAL) << "Unreachable type " << type;
2756       UNREACHABLE();
2757   }
2758   if (!Primitive::IsFloatingPointType(type)) {
2759     DCHECK(locations->Out().IsRegister());
2760     GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2761     __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2762   } else {
2763     DCHECK(locations->Out().IsFpuRegister());
2764     FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2765     __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2766   }
2767 
2768   codegen_->MaybeRecordImplicitNullCheck(instruction);
2769   // TODO: memory barrier?
2770 }
2771 
HandleFieldSet(HInstruction * instruction,const FieldInfo & field_info ATTRIBUTE_UNUSED)2772 void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2773                                             const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2774   LocationSummary* locations =
2775       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2776   locations->SetInAt(0, Location::RequiresRegister());
2777   if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2778     locations->SetInAt(1, Location::RequiresFpuRegister());
2779   } else {
2780     locations->SetInAt(1, Location::RequiresRegister());
2781   }
2782 }
2783 
HandleFieldSet(HInstruction * instruction,const FieldInfo & field_info,bool value_can_be_null)2784 void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
2785                                                     const FieldInfo& field_info,
2786                                                     bool value_can_be_null) {
2787   Primitive::Type type = field_info.GetFieldType();
2788   LocationSummary* locations = instruction->GetLocations();
2789   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2790   StoreOperandType store_type = kStoreByte;
2791   switch (type) {
2792     case Primitive::kPrimBoolean:
2793     case Primitive::kPrimByte:
2794       store_type = kStoreByte;
2795       break;
2796     case Primitive::kPrimShort:
2797     case Primitive::kPrimChar:
2798       store_type = kStoreHalfword;
2799       break;
2800     case Primitive::kPrimInt:
2801     case Primitive::kPrimFloat:
2802     case Primitive::kPrimNot:
2803       store_type = kStoreWord;
2804       break;
2805     case Primitive::kPrimLong:
2806     case Primitive::kPrimDouble:
2807       store_type = kStoreDoubleword;
2808       break;
2809     case Primitive::kPrimVoid:
2810       LOG(FATAL) << "Unreachable type " << type;
2811       UNREACHABLE();
2812   }
2813   if (!Primitive::IsFloatingPointType(type)) {
2814     DCHECK(locations->InAt(1).IsRegister());
2815     GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2816     __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2817   } else {
2818     DCHECK(locations->InAt(1).IsFpuRegister());
2819     FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2820     __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2821   }
2822 
2823   codegen_->MaybeRecordImplicitNullCheck(instruction);
2824   // TODO: memory barriers?
2825   if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2826     DCHECK(locations->InAt(1).IsRegister());
2827     GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2828     codegen_->MarkGCCard(obj, src, value_can_be_null);
2829   }
2830 }
2831 
VisitInstanceFieldGet(HInstanceFieldGet * instruction)2832 void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2833   HandleFieldGet(instruction, instruction->GetFieldInfo());
2834 }
2835 
VisitInstanceFieldGet(HInstanceFieldGet * instruction)2836 void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2837   HandleFieldGet(instruction, instruction->GetFieldInfo());
2838 }
2839 
VisitInstanceFieldSet(HInstanceFieldSet * instruction)2840 void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2841   HandleFieldSet(instruction, instruction->GetFieldInfo());
2842 }
2843 
VisitInstanceFieldSet(HInstanceFieldSet * instruction)2844 void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2845   HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
2846 }
2847 
VisitInstanceOf(HInstanceOf * instruction)2848 void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2849   LocationSummary::CallKind call_kind =
2850       instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2851   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2852   locations->SetInAt(0, Location::RequiresRegister());
2853   locations->SetInAt(1, Location::RequiresRegister());
2854   // The output does overlap inputs.
2855   // Note that TypeCheckSlowPathMIPS64 uses this register too.
2856   locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2857 }
2858 
VisitInstanceOf(HInstanceOf * instruction)2859 void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2860   LocationSummary* locations = instruction->GetLocations();
2861   GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2862   GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2863   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2864 
2865   Mips64Label done;
2866 
2867   // Return 0 if `obj` is null.
2868   // TODO: Avoid this check if we know `obj` is not null.
2869   __ Move(out, ZERO);
2870   __ Beqzc(obj, &done);
2871 
2872   // Compare the class of `obj` with `cls`.
2873   __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
2874   if (instruction->IsExactCheck()) {
2875     // Classes must be equal for the instanceof to succeed.
2876     __ Xor(out, out, cls);
2877     __ Sltiu(out, out, 1);
2878   } else {
2879     // If the classes are not equal, we go into a slow path.
2880     DCHECK(locations->OnlyCallsOnSlowPath());
2881     SlowPathCodeMIPS64* slow_path =
2882         new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
2883     codegen_->AddSlowPath(slow_path);
2884     __ Bnec(out, cls, slow_path->GetEntryLabel());
2885     __ LoadConst32(out, 1);
2886     __ Bind(slow_path->GetExitLabel());
2887   }
2888 
2889   __ Bind(&done);
2890 }
2891 
VisitIntConstant(HIntConstant * constant)2892 void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2893   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2894   locations->SetOut(Location::ConstantLocation(constant));
2895 }
2896 
VisitIntConstant(HIntConstant * constant ATTRIBUTE_UNUSED)2897 void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2898   // Will be generated at use site.
2899 }
2900 
VisitNullConstant(HNullConstant * constant)2901 void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2902   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2903   locations->SetOut(Location::ConstantLocation(constant));
2904 }
2905 
VisitNullConstant(HNullConstant * constant ATTRIBUTE_UNUSED)2906 void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2907   // Will be generated at use site.
2908 }
2909 
VisitInvokeUnresolved(HInvokeUnresolved * invoke)2910 void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2911   // The trampoline uses the same calling convention as dex calling conventions,
2912   // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2913   // the method_idx.
2914   HandleInvoke(invoke);
2915 }
2916 
VisitInvokeUnresolved(HInvokeUnresolved * invoke)2917 void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2918   codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2919 }
2920 
HandleInvoke(HInvoke * invoke)2921 void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2922   InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2923   CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2924 }
2925 
VisitInvokeInterface(HInvokeInterface * invoke)2926 void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2927   HandleInvoke(invoke);
2928   // The register T0 is required to be used for the hidden argument in
2929   // art_quick_imt_conflict_trampoline, so add the hidden argument.
2930   invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2931 }
2932 
VisitInvokeInterface(HInvokeInterface * invoke)2933 void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2934   // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2935   GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2936   uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2937       invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2938   Location receiver = invoke->GetLocations()->InAt(0);
2939   uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2940   Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
2941 
2942   // Set the hidden argument.
2943   __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2944                  invoke->GetDexMethodIndex());
2945 
2946   // temp = object->GetClass();
2947   if (receiver.IsStackSlot()) {
2948     __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2949     __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2950   } else {
2951     __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2952   }
2953   codegen_->MaybeRecordImplicitNullCheck(invoke);
2954   // temp = temp->GetImtEntryAt(method_offset);
2955   __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2956   // T9 = temp->GetEntryPoint();
2957   __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2958   // T9();
2959   __ Jalr(T9);
2960   __ Nop();
2961   DCHECK(!codegen_->IsLeafMethod());
2962   codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2963 }
2964 
VisitInvokeVirtual(HInvokeVirtual * invoke)2965 void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2966   IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2967   if (intrinsic.TryDispatch(invoke)) {
2968     return;
2969   }
2970 
2971   HandleInvoke(invoke);
2972 }
2973 
VisitInvokeStaticOrDirect(HInvokeStaticOrDirect * invoke)2974 void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2975   // Explicit clinit checks triggered by static invokes must have been pruned by
2976   // art::PrepareForRegisterAllocation.
2977   DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
2978 
2979   IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2980   if (intrinsic.TryDispatch(invoke)) {
2981     return;
2982   }
2983 
2984   HandleInvoke(invoke);
2985 
2986   // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2987   // clobbering somewhere else, reduce further register pressure by avoiding
2988   // allocation of a register for the current method pointer like on x86 baseline.
2989   // TODO: remove this once all the issues with register saving/restoring are
2990   // sorted out.
2991   if (invoke->HasCurrentMethodInput()) {
2992     LocationSummary* locations = invoke->GetLocations();
2993     Location location = locations->InAt(invoke->GetSpecialInputIndex());
2994     if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
2995       locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
2996     }
2997   }
2998 }
2999 
TryGenerateIntrinsicCode(HInvoke * invoke,CodeGeneratorMIPS64 * codegen)3000 static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
3001   if (invoke->GetLocations()->Intrinsified()) {
3002     IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3003     intrinsic.Dispatch(invoke);
3004     return true;
3005   }
3006   return false;
3007 }
3008 
GetSupportedLoadStringKind(HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED)3009 HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3010     HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3011   // TODO: Implement other kinds.
3012   return HLoadString::LoadKind::kDexCacheViaMethod;
3013 }
3014 
GetSupportedInvokeStaticOrDirectDispatch(const HInvokeStaticOrDirect::DispatchInfo & desired_dispatch_info,MethodReference target_method ATTRIBUTE_UNUSED)3015 HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3016       const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3017       MethodReference target_method ATTRIBUTE_UNUSED) {
3018   switch (desired_dispatch_info.method_load_kind) {
3019     case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3020     case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3021       // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3022       return HInvokeStaticOrDirect::DispatchInfo {
3023         HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3024         HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3025         0u,
3026         0u
3027       };
3028     default:
3029       break;
3030   }
3031   switch (desired_dispatch_info.code_ptr_location) {
3032     case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3033     case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3034       // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3035       return HInvokeStaticOrDirect::DispatchInfo {
3036         desired_dispatch_info.method_load_kind,
3037         HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3038         desired_dispatch_info.method_load_data,
3039         0u
3040       };
3041     default:
3042       return desired_dispatch_info;
3043   }
3044 }
3045 
GenerateStaticOrDirectCall(HInvokeStaticOrDirect * invoke,Location temp)3046 void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3047   // All registers are assumed to be correctly set up per the calling convention.
3048 
3049   Location callee_method = temp;  // For all kinds except kRecursive, callee will be in temp.
3050   switch (invoke->GetMethodLoadKind()) {
3051     case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3052       // temp = thread->string_init_entrypoint
3053       __ LoadFromOffset(kLoadDoubleword,
3054                         temp.AsRegister<GpuRegister>(),
3055                         TR,
3056                         invoke->GetStringInitOffset());
3057       break;
3058     case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3059       callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
3060       break;
3061     case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3062       __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3063       break;
3064     case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3065     case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3066       // TODO: Implement these types.
3067       // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3068       LOG(FATAL) << "Unsupported";
3069       UNREACHABLE();
3070     case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3071       Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
3072       GpuRegister reg = temp.AsRegister<GpuRegister>();
3073       GpuRegister method_reg;
3074       if (current_method.IsRegister()) {
3075         method_reg = current_method.AsRegister<GpuRegister>();
3076       } else {
3077         // TODO: use the appropriate DCHECK() here if possible.
3078         // DCHECK(invoke->GetLocations()->Intrinsified());
3079         DCHECK(!current_method.IsValid());
3080         method_reg = reg;
3081         __ Ld(reg, SP, kCurrentMethodStackOffset);
3082       }
3083 
3084       // temp = temp->dex_cache_resolved_methods_;
3085       __ LoadFromOffset(kLoadDoubleword,
3086                         reg,
3087                         method_reg,
3088                         ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
3089       // temp = temp[index_in_cache];
3090       // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3091       uint32_t index_in_cache = invoke->GetDexMethodIndex();
3092       __ LoadFromOffset(kLoadDoubleword,
3093                         reg,
3094                         reg,
3095                         CodeGenerator::GetCachePointerOffset(index_in_cache));
3096       break;
3097     }
3098   }
3099 
3100   switch (invoke->GetCodePtrLocation()) {
3101     case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3102       __ Jialc(&frame_entry_label_, T9);
3103       break;
3104     case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3105       // LR = invoke->GetDirectCodePtr();
3106       __ LoadConst64(T9, invoke->GetDirectCodePtr());
3107       // LR()
3108       __ Jalr(T9);
3109       __ Nop();
3110       break;
3111     case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3112     case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3113       // TODO: Implement these types.
3114       // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3115       LOG(FATAL) << "Unsupported";
3116       UNREACHABLE();
3117     case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3118       // T9 = callee_method->entry_point_from_quick_compiled_code_;
3119       __ LoadFromOffset(kLoadDoubleword,
3120                         T9,
3121                         callee_method.AsRegister<GpuRegister>(),
3122                         ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3123                             kMips64DoublewordSize).Int32Value());
3124       // T9()
3125       __ Jalr(T9);
3126       __ Nop();
3127       break;
3128   }
3129   DCHECK(!IsLeafMethod());
3130 }
3131 
VisitInvokeStaticOrDirect(HInvokeStaticOrDirect * invoke)3132 void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3133   // Explicit clinit checks triggered by static invokes must have been pruned by
3134   // art::PrepareForRegisterAllocation.
3135   DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3136 
3137   if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3138     return;
3139   }
3140 
3141   LocationSummary* locations = invoke->GetLocations();
3142   codegen_->GenerateStaticOrDirectCall(invoke,
3143                                        locations->HasTemps()
3144                                            ? locations->GetTemp(0)
3145                                            : Location::NoLocation());
3146   codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3147 }
3148 
GenerateVirtualCall(HInvokeVirtual * invoke,Location temp_location)3149 void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
3150   // Use the calling convention instead of the location of the receiver, as
3151   // intrinsics may have put the receiver in a different register. In the intrinsics
3152   // slow path, the arguments have been moved to the right place, so here we are
3153   // guaranteed that the receiver is the first register of the calling convention.
3154   InvokeDexCallingConvention calling_convention;
3155   GpuRegister receiver = calling_convention.GetRegisterAt(0);
3156 
3157   GpuRegister temp = temp_location.AsRegister<GpuRegister>();
3158   size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3159       invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3160   uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3161   Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
3162 
3163   // temp = object->GetClass();
3164   __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
3165   MaybeRecordImplicitNullCheck(invoke);
3166   // temp = temp->GetMethodAt(method_offset);
3167   __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3168   // T9 = temp->GetEntryPoint();
3169   __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3170   // T9();
3171   __ Jalr(T9);
3172   __ Nop();
3173 }
3174 
VisitInvokeVirtual(HInvokeVirtual * invoke)3175 void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3176   if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3177     return;
3178   }
3179 
3180   codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
3181   DCHECK(!codegen_->IsLeafMethod());
3182   codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3183 }
3184 
VisitLoadClass(HLoadClass * cls)3185 void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
3186   InvokeRuntimeCallingConvention calling_convention;
3187   CodeGenerator::CreateLoadClassLocationSummary(
3188       cls,
3189       Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3190       calling_convention.GetReturnLocation(cls->GetType()));
3191 }
3192 
VisitLoadClass(HLoadClass * cls)3193 void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3194   LocationSummary* locations = cls->GetLocations();
3195   if (cls->NeedsAccessCheck()) {
3196     codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3197     codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3198                             cls,
3199                             cls->GetDexPc(),
3200                             nullptr);
3201     CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
3202     return;
3203   }
3204 
3205   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3206   GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3207   if (cls->IsReferrersClass()) {
3208     DCHECK(!cls->CanCallRuntime());
3209     DCHECK(!cls->MustGenerateClinitCheck());
3210     __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3211                       ArtMethod::DeclaringClassOffset().Int32Value());
3212   } else {
3213     __ LoadFromOffset(kLoadDoubleword, out, current_method,
3214                       ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
3215     __ LoadFromOffset(
3216         kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
3217     // TODO: We will need a read barrier here.
3218     if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3219       DCHECK(cls->CanCallRuntime());
3220       SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3221           cls,
3222           cls,
3223           cls->GetDexPc(),
3224           cls->MustGenerateClinitCheck());
3225       codegen_->AddSlowPath(slow_path);
3226       if (!cls->IsInDexCache()) {
3227         __ Beqzc(out, slow_path->GetEntryLabel());
3228       }
3229       if (cls->MustGenerateClinitCheck()) {
3230         GenerateClassInitializationCheck(slow_path, out);
3231       } else {
3232         __ Bind(slow_path->GetExitLabel());
3233       }
3234     }
3235   }
3236 }
3237 
GetExceptionTlsOffset()3238 static int32_t GetExceptionTlsOffset() {
3239   return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
3240 }
3241 
VisitLoadException(HLoadException * load)3242 void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3243   LocationSummary* locations =
3244       new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3245   locations->SetOut(Location::RequiresRegister());
3246 }
3247 
VisitLoadException(HLoadException * load)3248 void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3249   GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
3250   __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3251 }
3252 
VisitClearException(HClearException * clear)3253 void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3254   new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3255 }
3256 
VisitClearException(HClearException * clear ATTRIBUTE_UNUSED)3257 void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3258   __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
3259 }
3260 
VisitLoadString(HLoadString * load)3261 void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
3262   LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3263       ? LocationSummary::kCallOnSlowPath
3264       : LocationSummary::kNoCall;
3265   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
3266   locations->SetInAt(0, Location::RequiresRegister());
3267   locations->SetOut(Location::RequiresRegister());
3268 }
3269 
VisitLoadString(HLoadString * load)3270 void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
3271   LocationSummary* locations = load->GetLocations();
3272   GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3273   GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3274   __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3275                     ArtMethod::DeclaringClassOffset().Int32Value());
3276   __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
3277   __ LoadFromOffset(
3278       kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3279   // TODO: We will need a read barrier here.
3280 
3281   if (!load->IsInDexCache()) {
3282     SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3283     codegen_->AddSlowPath(slow_path);
3284     __ Beqzc(out, slow_path->GetEntryLabel());
3285     __ Bind(slow_path->GetExitLabel());
3286   }
3287 }
3288 
VisitLongConstant(HLongConstant * constant)3289 void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3290   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3291   locations->SetOut(Location::ConstantLocation(constant));
3292 }
3293 
VisitLongConstant(HLongConstant * constant ATTRIBUTE_UNUSED)3294 void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3295   // Will be generated at use site.
3296 }
3297 
VisitMonitorOperation(HMonitorOperation * instruction)3298 void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3299   LocationSummary* locations =
3300       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3301   InvokeRuntimeCallingConvention calling_convention;
3302   locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3303 }
3304 
VisitMonitorOperation(HMonitorOperation * instruction)3305 void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3306   codegen_->InvokeRuntime(instruction->IsEnter()
3307                               ? QUICK_ENTRY_POINT(pLockObject)
3308                               : QUICK_ENTRY_POINT(pUnlockObject),
3309                           instruction,
3310                           instruction->GetDexPc(),
3311                           nullptr);
3312   if (instruction->IsEnter()) {
3313     CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3314   } else {
3315     CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3316   }
3317 }
3318 
VisitMul(HMul * mul)3319 void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3320   LocationSummary* locations =
3321       new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3322   switch (mul->GetResultType()) {
3323     case Primitive::kPrimInt:
3324     case Primitive::kPrimLong:
3325       locations->SetInAt(0, Location::RequiresRegister());
3326       locations->SetInAt(1, Location::RequiresRegister());
3327       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3328       break;
3329 
3330     case Primitive::kPrimFloat:
3331     case Primitive::kPrimDouble:
3332       locations->SetInAt(0, Location::RequiresFpuRegister());
3333       locations->SetInAt(1, Location::RequiresFpuRegister());
3334       locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3335       break;
3336 
3337     default:
3338       LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3339   }
3340 }
3341 
VisitMul(HMul * instruction)3342 void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3343   Primitive::Type type = instruction->GetType();
3344   LocationSummary* locations = instruction->GetLocations();
3345 
3346   switch (type) {
3347     case Primitive::kPrimInt:
3348     case Primitive::kPrimLong: {
3349       GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3350       GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3351       GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3352       if (type == Primitive::kPrimInt)
3353         __ MulR6(dst, lhs, rhs);
3354       else
3355         __ Dmul(dst, lhs, rhs);
3356       break;
3357     }
3358     case Primitive::kPrimFloat:
3359     case Primitive::kPrimDouble: {
3360       FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3361       FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3362       FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3363       if (type == Primitive::kPrimFloat)
3364         __ MulS(dst, lhs, rhs);
3365       else
3366         __ MulD(dst, lhs, rhs);
3367       break;
3368     }
3369     default:
3370       LOG(FATAL) << "Unexpected mul type " << type;
3371   }
3372 }
3373 
VisitNeg(HNeg * neg)3374 void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3375   LocationSummary* locations =
3376       new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3377   switch (neg->GetResultType()) {
3378     case Primitive::kPrimInt:
3379     case Primitive::kPrimLong:
3380       locations->SetInAt(0, Location::RequiresRegister());
3381       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3382       break;
3383 
3384     case Primitive::kPrimFloat:
3385     case Primitive::kPrimDouble:
3386       locations->SetInAt(0, Location::RequiresFpuRegister());
3387       locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3388       break;
3389 
3390     default:
3391       LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3392   }
3393 }
3394 
VisitNeg(HNeg * instruction)3395 void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3396   Primitive::Type type = instruction->GetType();
3397   LocationSummary* locations = instruction->GetLocations();
3398 
3399   switch (type) {
3400     case Primitive::kPrimInt:
3401     case Primitive::kPrimLong: {
3402       GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3403       GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3404       if (type == Primitive::kPrimInt)
3405         __ Subu(dst, ZERO, src);
3406       else
3407         __ Dsubu(dst, ZERO, src);
3408       break;
3409     }
3410     case Primitive::kPrimFloat:
3411     case Primitive::kPrimDouble: {
3412       FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3413       FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3414       if (type == Primitive::kPrimFloat)
3415         __ NegS(dst, src);
3416       else
3417         __ NegD(dst, src);
3418       break;
3419     }
3420     default:
3421       LOG(FATAL) << "Unexpected neg type " << type;
3422   }
3423 }
3424 
VisitNewArray(HNewArray * instruction)3425 void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3426   LocationSummary* locations =
3427       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3428   InvokeRuntimeCallingConvention calling_convention;
3429   locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3430   locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3431   locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3432   locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3433 }
3434 
VisitNewArray(HNewArray * instruction)3435 void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3436   LocationSummary* locations = instruction->GetLocations();
3437   // Move an uint16_t value to a register.
3438   __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
3439   codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3440                           instruction,
3441                           instruction->GetDexPc(),
3442                           nullptr);
3443   CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3444 }
3445 
VisitNewInstance(HNewInstance * instruction)3446 void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3447   LocationSummary* locations =
3448       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3449   InvokeRuntimeCallingConvention calling_convention;
3450   if (instruction->IsStringAlloc()) {
3451     locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3452   } else {
3453     locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3454     locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3455   }
3456   locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3457 }
3458 
VisitNewInstance(HNewInstance * instruction)3459 void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
3460   if (instruction->IsStringAlloc()) {
3461     // String is allocated through StringFactory. Call NewEmptyString entry point.
3462     GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
3463     MemberOffset code_offset =
3464         ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
3465     __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3466     __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3467     __ Jalr(T9);
3468     __ Nop();
3469     codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3470   } else {
3471     codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3472                             instruction,
3473                             instruction->GetDexPc(),
3474                             nullptr);
3475     CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3476   }
3477 }
3478 
VisitNot(HNot * instruction)3479 void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3480   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3481   locations->SetInAt(0, Location::RequiresRegister());
3482   locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3483 }
3484 
VisitNot(HNot * instruction)3485 void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3486   Primitive::Type type = instruction->GetType();
3487   LocationSummary* locations = instruction->GetLocations();
3488 
3489   switch (type) {
3490     case Primitive::kPrimInt:
3491     case Primitive::kPrimLong: {
3492       GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3493       GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3494       __ Nor(dst, src, ZERO);
3495       break;
3496     }
3497 
3498     default:
3499       LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3500   }
3501 }
3502 
VisitBooleanNot(HBooleanNot * instruction)3503 void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3504   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3505   locations->SetInAt(0, Location::RequiresRegister());
3506   locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3507 }
3508 
VisitBooleanNot(HBooleanNot * instruction)3509 void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3510   LocationSummary* locations = instruction->GetLocations();
3511   __ Xori(locations->Out().AsRegister<GpuRegister>(),
3512           locations->InAt(0).AsRegister<GpuRegister>(),
3513           1);
3514 }
3515 
VisitNullCheck(HNullCheck * instruction)3516 void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
3517   LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3518       ? LocationSummary::kCallOnSlowPath
3519       : LocationSummary::kNoCall;
3520   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3521   locations->SetInAt(0, Location::RequiresRegister());
3522   if (instruction->HasUses()) {
3523     locations->SetOut(Location::SameAsFirstInput());
3524   }
3525 }
3526 
GenerateImplicitNullCheck(HNullCheck * instruction)3527 void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3528   if (CanMoveNullCheckToUser(instruction)) {
3529     return;
3530   }
3531   Location obj = instruction->GetLocations()->InAt(0);
3532 
3533   __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3534   RecordPcInfo(instruction, instruction->GetDexPc());
3535 }
3536 
GenerateExplicitNullCheck(HNullCheck * instruction)3537 void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3538   SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3539   AddSlowPath(slow_path);
3540 
3541   Location obj = instruction->GetLocations()->InAt(0);
3542 
3543   __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3544 }
3545 
VisitNullCheck(HNullCheck * instruction)3546 void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
3547   codegen_->GenerateNullCheck(instruction);
3548 }
3549 
VisitOr(HOr * instruction)3550 void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3551   HandleBinaryOp(instruction);
3552 }
3553 
VisitOr(HOr * instruction)3554 void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3555   HandleBinaryOp(instruction);
3556 }
3557 
VisitParallelMove(HParallelMove * instruction ATTRIBUTE_UNUSED)3558 void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3559   LOG(FATAL) << "Unreachable";
3560 }
3561 
VisitParallelMove(HParallelMove * instruction)3562 void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3563   codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3564 }
3565 
VisitParameterValue(HParameterValue * instruction)3566 void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3567   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3568   Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3569   if (location.IsStackSlot()) {
3570     location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3571   } else if (location.IsDoubleStackSlot()) {
3572     location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573   }
3574   locations->SetOut(location);
3575 }
3576 
VisitParameterValue(HParameterValue * instruction ATTRIBUTE_UNUSED)3577 void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3578                                                          ATTRIBUTE_UNUSED) {
3579   // Nothing to do, the parameter is already at its location.
3580 }
3581 
VisitCurrentMethod(HCurrentMethod * instruction)3582 void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3583   LocationSummary* locations =
3584       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3585   locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3586 }
3587 
VisitCurrentMethod(HCurrentMethod * instruction ATTRIBUTE_UNUSED)3588 void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3589                                                         ATTRIBUTE_UNUSED) {
3590   // Nothing to do, the method is already at its location.
3591 }
3592 
VisitPhi(HPhi * instruction)3593 void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3594   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3595   for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3596     locations->SetInAt(i, Location::Any());
3597   }
3598   locations->SetOut(Location::Any());
3599 }
3600 
VisitPhi(HPhi * instruction ATTRIBUTE_UNUSED)3601 void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3602   LOG(FATAL) << "Unreachable";
3603 }
3604 
VisitRem(HRem * rem)3605 void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3606   Primitive::Type type = rem->GetResultType();
3607   LocationSummary::CallKind call_kind =
3608       Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3609   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3610 
3611   switch (type) {
3612     case Primitive::kPrimInt:
3613     case Primitive::kPrimLong:
3614       locations->SetInAt(0, Location::RequiresRegister());
3615       locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
3616       locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3617       break;
3618 
3619     case Primitive::kPrimFloat:
3620     case Primitive::kPrimDouble: {
3621       InvokeRuntimeCallingConvention calling_convention;
3622       locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3623       locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3624       locations->SetOut(calling_convention.GetReturnLocation(type));
3625       break;
3626     }
3627 
3628     default:
3629       LOG(FATAL) << "Unexpected rem type " << type;
3630   }
3631 }
3632 
VisitRem(HRem * instruction)3633 void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3634   Primitive::Type type = instruction->GetType();
3635 
3636   switch (type) {
3637     case Primitive::kPrimInt:
3638     case Primitive::kPrimLong:
3639       GenerateDivRemIntegral(instruction);
3640       break;
3641 
3642     case Primitive::kPrimFloat:
3643     case Primitive::kPrimDouble: {
3644       int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3645                                                              : QUICK_ENTRY_POINT(pFmod);
3646       codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
3647       if (type == Primitive::kPrimFloat) {
3648         CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3649       } else {
3650         CheckEntrypointTypes<kQuickFmod, double, double, double>();
3651       }
3652       break;
3653     }
3654     default:
3655       LOG(FATAL) << "Unexpected rem type " << type;
3656   }
3657 }
3658 
VisitMemoryBarrier(HMemoryBarrier * memory_barrier)3659 void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3660   memory_barrier->SetLocations(nullptr);
3661 }
3662 
VisitMemoryBarrier(HMemoryBarrier * memory_barrier)3663 void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3664   GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3665 }
3666 
VisitReturn(HReturn * ret)3667 void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3668   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3669   Primitive::Type return_type = ret->InputAt(0)->GetType();
3670   locations->SetInAt(0, Mips64ReturnLocation(return_type));
3671 }
3672 
VisitReturn(HReturn * ret ATTRIBUTE_UNUSED)3673 void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3674   codegen_->GenerateFrameExit();
3675 }
3676 
VisitReturnVoid(HReturnVoid * ret)3677 void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3678   ret->SetLocations(nullptr);
3679 }
3680 
VisitReturnVoid(HReturnVoid * ret ATTRIBUTE_UNUSED)3681 void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3682   codegen_->GenerateFrameExit();
3683 }
3684 
VisitRor(HRor * ror)3685 void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3686   HandleShift(ror);
3687 }
3688 
VisitRor(HRor * ror)3689 void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3690   HandleShift(ror);
3691 }
3692 
VisitShl(HShl * shl)3693 void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3694   HandleShift(shl);
3695 }
3696 
VisitShl(HShl * shl)3697 void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3698   HandleShift(shl);
3699 }
3700 
VisitShr(HShr * shr)3701 void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3702   HandleShift(shr);
3703 }
3704 
VisitShr(HShr * shr)3705 void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3706   HandleShift(shr);
3707 }
3708 
VisitSub(HSub * instruction)3709 void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3710   HandleBinaryOp(instruction);
3711 }
3712 
VisitSub(HSub * instruction)3713 void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3714   HandleBinaryOp(instruction);
3715 }
3716 
VisitStaticFieldGet(HStaticFieldGet * instruction)3717 void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3718   HandleFieldGet(instruction, instruction->GetFieldInfo());
3719 }
3720 
VisitStaticFieldGet(HStaticFieldGet * instruction)3721 void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3722   HandleFieldGet(instruction, instruction->GetFieldInfo());
3723 }
3724 
VisitStaticFieldSet(HStaticFieldSet * instruction)3725 void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3726   HandleFieldSet(instruction, instruction->GetFieldInfo());
3727 }
3728 
VisitStaticFieldSet(HStaticFieldSet * instruction)3729 void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3730   HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
3731 }
3732 
VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet * instruction)3733 void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3734     HUnresolvedInstanceFieldGet* instruction) {
3735   FieldAccessCallingConventionMIPS64 calling_convention;
3736   codegen_->CreateUnresolvedFieldLocationSummary(
3737       instruction, instruction->GetFieldType(), calling_convention);
3738 }
3739 
VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet * instruction)3740 void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3741     HUnresolvedInstanceFieldGet* instruction) {
3742   FieldAccessCallingConventionMIPS64 calling_convention;
3743   codegen_->GenerateUnresolvedFieldAccess(instruction,
3744                                           instruction->GetFieldType(),
3745                                           instruction->GetFieldIndex(),
3746                                           instruction->GetDexPc(),
3747                                           calling_convention);
3748 }
3749 
VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet * instruction)3750 void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3751     HUnresolvedInstanceFieldSet* instruction) {
3752   FieldAccessCallingConventionMIPS64 calling_convention;
3753   codegen_->CreateUnresolvedFieldLocationSummary(
3754       instruction, instruction->GetFieldType(), calling_convention);
3755 }
3756 
VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet * instruction)3757 void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3758     HUnresolvedInstanceFieldSet* instruction) {
3759   FieldAccessCallingConventionMIPS64 calling_convention;
3760   codegen_->GenerateUnresolvedFieldAccess(instruction,
3761                                           instruction->GetFieldType(),
3762                                           instruction->GetFieldIndex(),
3763                                           instruction->GetDexPc(),
3764                                           calling_convention);
3765 }
3766 
VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet * instruction)3767 void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3768     HUnresolvedStaticFieldGet* instruction) {
3769   FieldAccessCallingConventionMIPS64 calling_convention;
3770   codegen_->CreateUnresolvedFieldLocationSummary(
3771       instruction, instruction->GetFieldType(), calling_convention);
3772 }
3773 
VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet * instruction)3774 void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3775     HUnresolvedStaticFieldGet* instruction) {
3776   FieldAccessCallingConventionMIPS64 calling_convention;
3777   codegen_->GenerateUnresolvedFieldAccess(instruction,
3778                                           instruction->GetFieldType(),
3779                                           instruction->GetFieldIndex(),
3780                                           instruction->GetDexPc(),
3781                                           calling_convention);
3782 }
3783 
VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet * instruction)3784 void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3785     HUnresolvedStaticFieldSet* instruction) {
3786   FieldAccessCallingConventionMIPS64 calling_convention;
3787   codegen_->CreateUnresolvedFieldLocationSummary(
3788       instruction, instruction->GetFieldType(), calling_convention);
3789 }
3790 
VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet * instruction)3791 void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3792     HUnresolvedStaticFieldSet* instruction) {
3793   FieldAccessCallingConventionMIPS64 calling_convention;
3794   codegen_->GenerateUnresolvedFieldAccess(instruction,
3795                                           instruction->GetFieldType(),
3796                                           instruction->GetFieldIndex(),
3797                                           instruction->GetDexPc(),
3798                                           calling_convention);
3799 }
3800 
VisitSuspendCheck(HSuspendCheck * instruction)3801 void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3802   new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3803 }
3804 
VisitSuspendCheck(HSuspendCheck * instruction)3805 void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3806   HBasicBlock* block = instruction->GetBlock();
3807   if (block->GetLoopInformation() != nullptr) {
3808     DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3809     // The back edge will generate the suspend check.
3810     return;
3811   }
3812   if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3813     // The goto will generate the suspend check.
3814     return;
3815   }
3816   GenerateSuspendCheck(instruction, nullptr);
3817 }
3818 
VisitThrow(HThrow * instruction)3819 void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3820   LocationSummary* locations =
3821       new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3822   InvokeRuntimeCallingConvention calling_convention;
3823   locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3824 }
3825 
VisitThrow(HThrow * instruction)3826 void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3827   codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3828                           instruction,
3829                           instruction->GetDexPc(),
3830                           nullptr);
3831   CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3832 }
3833 
VisitTypeConversion(HTypeConversion * conversion)3834 void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3835   Primitive::Type input_type = conversion->GetInputType();
3836   Primitive::Type result_type = conversion->GetResultType();
3837   DCHECK_NE(input_type, result_type);
3838 
3839   if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3840       (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3841     LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3842   }
3843 
3844   LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3845 
3846   if (Primitive::IsFloatingPointType(input_type)) {
3847     locations->SetInAt(0, Location::RequiresFpuRegister());
3848   } else {
3849     locations->SetInAt(0, Location::RequiresRegister());
3850   }
3851 
3852   if (Primitive::IsFloatingPointType(result_type)) {
3853     locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3854   } else {
3855     locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3856   }
3857 }
3858 
VisitTypeConversion(HTypeConversion * conversion)3859 void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3860   LocationSummary* locations = conversion->GetLocations();
3861   Primitive::Type result_type = conversion->GetResultType();
3862   Primitive::Type input_type = conversion->GetInputType();
3863 
3864   DCHECK_NE(input_type, result_type);
3865 
3866   if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3867     GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3868     GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3869 
3870     switch (result_type) {
3871       case Primitive::kPrimChar:
3872         __ Andi(dst, src, 0xFFFF);
3873         break;
3874       case Primitive::kPrimByte:
3875         if (input_type == Primitive::kPrimLong) {
3876           // Type conversion from long to types narrower than int is a result of code
3877           // transformations. To avoid unpredictable results for SEB and SEH, we first
3878           // need to sign-extend the low 32-bit value into bits 32 through 63.
3879           __ Sll(dst, src, 0);
3880           __ Seb(dst, dst);
3881         } else {
3882           __ Seb(dst, src);
3883         }
3884         break;
3885       case Primitive::kPrimShort:
3886         if (input_type == Primitive::kPrimLong) {
3887           // Type conversion from long to types narrower than int is a result of code
3888           // transformations. To avoid unpredictable results for SEB and SEH, we first
3889           // need to sign-extend the low 32-bit value into bits 32 through 63.
3890           __ Sll(dst, src, 0);
3891           __ Seh(dst, dst);
3892         } else {
3893           __ Seh(dst, src);
3894         }
3895         break;
3896       case Primitive::kPrimInt:
3897       case Primitive::kPrimLong:
3898         // Sign-extend 32-bit int into bits 32 through 63 for
3899         // int-to-long and long-to-int conversions
3900         __ Sll(dst, src, 0);
3901         break;
3902 
3903       default:
3904         LOG(FATAL) << "Unexpected type conversion from " << input_type
3905                    << " to " << result_type;
3906     }
3907   } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3908     FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3909     GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3910     if (input_type == Primitive::kPrimLong) {
3911       __ Dmtc1(src, FTMP);
3912       if (result_type == Primitive::kPrimFloat) {
3913         __ Cvtsl(dst, FTMP);
3914       } else {
3915         __ Cvtdl(dst, FTMP);
3916       }
3917     } else {
3918       __ Mtc1(src, FTMP);
3919       if (result_type == Primitive::kPrimFloat) {
3920         __ Cvtsw(dst, FTMP);
3921       } else {
3922         __ Cvtdw(dst, FTMP);
3923       }
3924     }
3925   } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3926     CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3927     GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3928     FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3929     Mips64Label truncate;
3930     Mips64Label done;
3931 
3932     // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3933     // value when the input is either a NaN or is outside of the range of the output type
3934     // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3935     // the same result.
3936     //
3937     // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3938     // value of the output type if the input is outside of the range after the truncation or
3939     // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3940     // results. This matches the desired float/double-to-int/long conversion exactly.
3941     //
3942     // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3943     //
3944     // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3945     // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3946     // even though it must be NAN2008=1 on R6.
3947     //
3948     // The code takes care of the different behaviors by first comparing the input to the
3949     // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3950     // If the input is greater than or equal to the minimum, it procedes to the truncate
3951     // instruction, which will handle such an input the same way irrespective of NAN2008.
3952     // Otherwise the input is compared to itself to determine whether it is a NaN or not
3953     // in order to return either zero or the minimum value.
3954     //
3955     // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3956     // truncate instruction for MIPS64R6.
3957     if (input_type == Primitive::kPrimFloat) {
3958       uint32_t min_val = (result_type == Primitive::kPrimLong)
3959           ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3960           : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3961       __ LoadConst32(TMP, min_val);
3962       __ Mtc1(TMP, FTMP);
3963       __ CmpLeS(FTMP, FTMP, src);
3964     } else {
3965       uint64_t min_val = (result_type == Primitive::kPrimLong)
3966           ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3967           : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3968       __ LoadConst64(TMP, min_val);
3969       __ Dmtc1(TMP, FTMP);
3970       __ CmpLeD(FTMP, FTMP, src);
3971     }
3972 
3973     __ Bc1nez(FTMP, &truncate);
3974 
3975     if (input_type == Primitive::kPrimFloat) {
3976       __ CmpEqS(FTMP, src, src);
3977     } else {
3978       __ CmpEqD(FTMP, src, src);
3979     }
3980     if (result_type == Primitive::kPrimLong) {
3981       __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3982     } else {
3983       __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3984     }
3985     __ Mfc1(TMP, FTMP);
3986     __ And(dst, dst, TMP);
3987 
3988     __ Bc(&done);
3989 
3990     __ Bind(&truncate);
3991 
3992     if (result_type == Primitive::kPrimLong) {
3993       if (input_type == Primitive::kPrimFloat) {
3994         __ TruncLS(FTMP, src);
3995       } else {
3996         __ TruncLD(FTMP, src);
3997       }
3998       __ Dmfc1(dst, FTMP);
3999     } else {
4000       if (input_type == Primitive::kPrimFloat) {
4001         __ TruncWS(FTMP, src);
4002       } else {
4003         __ TruncWD(FTMP, src);
4004       }
4005       __ Mfc1(dst, FTMP);
4006     }
4007 
4008     __ Bind(&done);
4009   } else if (Primitive::IsFloatingPointType(result_type) &&
4010              Primitive::IsFloatingPointType(input_type)) {
4011     FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4012     FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4013     if (result_type == Primitive::kPrimFloat) {
4014       __ Cvtsd(dst, src);
4015     } else {
4016       __ Cvtds(dst, src);
4017     }
4018   } else {
4019     LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4020                 << " to " << result_type;
4021   }
4022 }
4023 
VisitUShr(HUShr * ushr)4024 void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4025   HandleShift(ushr);
4026 }
4027 
VisitUShr(HUShr * ushr)4028 void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4029   HandleShift(ushr);
4030 }
4031 
VisitXor(HXor * instruction)4032 void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4033   HandleBinaryOp(instruction);
4034 }
4035 
VisitXor(HXor * instruction)4036 void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4037   HandleBinaryOp(instruction);
4038 }
4039 
VisitBoundType(HBoundType * instruction ATTRIBUTE_UNUSED)4040 void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4041   // Nothing to do, this should be removed during prepare for register allocator.
4042   LOG(FATAL) << "Unreachable";
4043 }
4044 
VisitBoundType(HBoundType * instruction ATTRIBUTE_UNUSED)4045 void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4046   // Nothing to do, this should be removed during prepare for register allocator.
4047   LOG(FATAL) << "Unreachable";
4048 }
4049 
VisitEqual(HEqual * comp)4050 void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
4051   HandleCondition(comp);
4052 }
4053 
VisitEqual(HEqual * comp)4054 void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
4055   HandleCondition(comp);
4056 }
4057 
VisitNotEqual(HNotEqual * comp)4058 void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
4059   HandleCondition(comp);
4060 }
4061 
VisitNotEqual(HNotEqual * comp)4062 void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
4063   HandleCondition(comp);
4064 }
4065 
VisitLessThan(HLessThan * comp)4066 void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
4067   HandleCondition(comp);
4068 }
4069 
VisitLessThan(HLessThan * comp)4070 void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
4071   HandleCondition(comp);
4072 }
4073 
VisitLessThanOrEqual(HLessThanOrEqual * comp)4074 void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4075   HandleCondition(comp);
4076 }
4077 
VisitLessThanOrEqual(HLessThanOrEqual * comp)4078 void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4079   HandleCondition(comp);
4080 }
4081 
VisitGreaterThan(HGreaterThan * comp)4082 void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
4083   HandleCondition(comp);
4084 }
4085 
VisitGreaterThan(HGreaterThan * comp)4086 void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
4087   HandleCondition(comp);
4088 }
4089 
VisitGreaterThanOrEqual(HGreaterThanOrEqual * comp)4090 void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4091   HandleCondition(comp);
4092 }
4093 
VisitGreaterThanOrEqual(HGreaterThanOrEqual * comp)4094 void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4095   HandleCondition(comp);
4096 }
4097 
VisitBelow(HBelow * comp)4098 void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
4099   HandleCondition(comp);
4100 }
4101 
VisitBelow(HBelow * comp)4102 void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
4103   HandleCondition(comp);
4104 }
4105 
VisitBelowOrEqual(HBelowOrEqual * comp)4106 void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
4107   HandleCondition(comp);
4108 }
4109 
VisitBelowOrEqual(HBelowOrEqual * comp)4110 void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
4111   HandleCondition(comp);
4112 }
4113 
VisitAbove(HAbove * comp)4114 void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
4115   HandleCondition(comp);
4116 }
4117 
VisitAbove(HAbove * comp)4118 void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
4119   HandleCondition(comp);
4120 }
4121 
VisitAboveOrEqual(HAboveOrEqual * comp)4122 void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
4123   HandleCondition(comp);
4124 }
4125 
VisitAboveOrEqual(HAboveOrEqual * comp)4126 void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
4127   HandleCondition(comp);
4128 }
4129 
4130 // Simple implementation of packed switch - generate cascaded compare/jumps.
VisitPackedSwitch(HPackedSwitch * switch_instr)4131 void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4132   LocationSummary* locations =
4133       new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4134   locations->SetInAt(0, Location::RequiresRegister());
4135 }
4136 
VisitPackedSwitch(HPackedSwitch * switch_instr)4137 void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4138   int32_t lower_bound = switch_instr->GetStartValue();
4139   int32_t num_entries = switch_instr->GetNumEntries();
4140   LocationSummary* locations = switch_instr->GetLocations();
4141   GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4142   HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4143 
4144   // Create a set of compare/jumps.
4145   GpuRegister temp_reg = TMP;
4146   if (IsInt<16>(-lower_bound)) {
4147     __ Addiu(temp_reg, value_reg, -lower_bound);
4148   } else {
4149     __ LoadConst32(AT, -lower_bound);
4150     __ Addu(temp_reg, value_reg, AT);
4151   }
4152   // Jump to default if index is negative
4153   // Note: We don't check the case that index is positive while value < lower_bound, because in
4154   // this case, index >= num_entries must be true. So that we can save one branch instruction.
4155   __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4156 
4157   const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4158   // Jump to successors[0] if value == lower_bound.
4159   __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4160   int32_t last_index = 0;
4161   for (; num_entries - last_index > 2; last_index += 2) {
4162     __ Addiu(temp_reg, temp_reg, -2);
4163     // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4164     __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4165     // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4166     __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4167   }
4168   if (num_entries - last_index == 2) {
4169     // The last missing case_value.
4170     __ Addiu(temp_reg, temp_reg, -1);
4171     __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4172   }
4173 
4174   // And the default for any other value.
4175   if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4176     __ Bc(codegen_->GetLabelOf(default_block));
4177   }
4178 }
4179 
VisitClassTableGet(HClassTableGet *)4180 void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4181   UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4182 }
4183 
VisitClassTableGet(HClassTableGet *)4184 void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4185   UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4186 }
4187 
4188 }  // namespace mips64
4189 }  // namespace art
4190