blob: 1ae34d6c68605a84e44c256e17cffcb6a8dbba8a [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
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
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 // TODO: shouldn't we use a whole machine word per argument on the stack?
95 // Implicit 4-byte method pointer (and such) will cause misalignment.
96
97 return next_location;
98}
99
100Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
101 return Mips64ReturnLocation(type);
102}
103
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100104// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
105#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700107
108class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
109 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000110 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111
112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
115 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000116 if (instruction_->CanThrowIntoCatchBlock()) {
117 // Live registers will be restored in the catch block if caught.
118 SaveLiveRegisters(codegen, instruction_->GetLocations());
119 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700120 // We're moving two locations to locations that could overlap, so we need a parallel
121 // move resolver.
122 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
125 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
128 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100129 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
130 ? kQuickThrowStringBounds
131 : kQuickThrowArrayBounds;
132 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100133 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700134 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
135 }
136
Alexandre Rames8158f282015-08-07 10:26:17 +0100137 bool IsFatal() const OVERRIDE { return true; }
138
Roland Levillain46648892015-06-19 16:07:18 +0100139 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
140
Alexey Frunze4dda3372015-06-01 18:31:49 -0700141 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
143};
144
145class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
146 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700148
149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
150 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
151 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100152 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700153 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
154 }
155
Alexandre Rames8158f282015-08-07 10:26:17 +0100156 bool IsFatal() const OVERRIDE { return true; }
157
Roland Levillain46648892015-06-19 16:07:18 +0100158 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
159
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700161 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
162};
163
164class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
165 public:
166 LoadClassSlowPathMIPS64(HLoadClass* cls,
167 HInstruction* at,
168 uint32_t dex_pc,
169 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000170 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
172 }
173
174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
175 LocationSummary* locations = at_->GetLocations();
176 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
177
178 __ Bind(GetEntryLabel());
179 SaveLiveRegisters(codegen, locations);
180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800182 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100183 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
184 : kQuickInitializeType;
185 mips64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700186 if (do_clinit_) {
187 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
188 } else {
189 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
190 }
191
192 // Move the class to the desired location.
193 Location out = locations->Out();
194 if (out.IsValid()) {
195 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
196 Primitive::Type type = at_->GetType();
197 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
198 }
199
200 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700201 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700202 }
203
Roland Levillain46648892015-06-19 16:07:18 +0100204 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
205
Alexey Frunze4dda3372015-06-01 18:31:49 -0700206 private:
207 // The class this slow path will load.
208 HLoadClass* const cls_;
209
210 // The instruction where this slow path is happening.
211 // (Might be the load class or an initialization check).
212 HInstruction* const at_;
213
214 // The dex PC of `at_`.
215 const uint32_t dex_pc_;
216
217 // Whether to initialize the class.
218 const bool do_clinit_;
219
220 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
221};
222
223class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
224 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000225 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 LocationSummary* locations = instruction_->GetLocations();
229 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
230 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
231
232 __ Bind(GetEntryLabel());
233 SaveLiveRegisters(codegen, locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800236 HLoadString* load = instruction_->AsLoadString();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800237 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
David Srbecky9cd6d372016-02-09 15:24:47 +0000238 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufc734082016-07-19 17:18:07 +0100239 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700240 instruction_,
241 instruction_->GetDexPc(),
242 this);
243 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
244 Primitive::Type type = instruction_->GetType();
245 mips64_codegen->MoveLocation(locations->Out(),
246 calling_convention.GetReturnLocation(type),
247 type);
248
249 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800250
251 // Store the resolved String to the BSS entry.
252 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
253 // .bss entry address in the fast path, so that we can avoid another calculation here.
254 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
255 DCHECK_NE(out, AT);
256 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
257 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
258 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
259 __ Sw(out, AT, /* placeholder */ 0x5678);
260
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700261 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262 }
263
Roland Levillain46648892015-06-19 16:07:18 +0100264 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
265
Alexey Frunze4dda3372015-06-01 18:31:49 -0700266 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
268};
269
270class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
271 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000272 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700273
274 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
275 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
276 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000277 if (instruction_->CanThrowIntoCatchBlock()) {
278 // Live registers will be restored in the catch block if caught.
279 SaveLiveRegisters(codegen, instruction_->GetLocations());
280 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100281 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 instruction_,
283 instruction_->GetDexPc(),
284 this);
285 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
286 }
287
Alexandre Rames8158f282015-08-07 10:26:17 +0100288 bool IsFatal() const OVERRIDE { return true; }
289
Roland Levillain46648892015-06-19 16:07:18 +0100290 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
291
Alexey Frunze4dda3372015-06-01 18:31:49 -0700292 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
294};
295
296class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
297 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100298 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000299 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300
301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
302 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
303 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100304 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700306 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700307 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700309 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 }
311 }
312
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 DCHECK(successor_ == nullptr);
315 return &return_label_;
316 }
317
Roland Levillain46648892015-06-19 16:07:18 +0100318 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
319
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700321 // If not null, the block to branch to after the suspend check.
322 HBasicBlock* const successor_;
323
324 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700325 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326
327 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
328};
329
330class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
331 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000332 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
335 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800336
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338 DCHECK(instruction_->IsCheckCast()
339 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
340 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
341
342 __ Bind(GetEntryLabel());
343 SaveLiveRegisters(codegen, locations);
344
345 // We're moving two locations to locations that could overlap, so we need a parallel
346 // move resolver.
347 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800348 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
350 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800351 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700352 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
353 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100355 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800356 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700357 Primitive::Type ret_type = instruction_->GetType();
358 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
359 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700360 } else {
361 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800362 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
363 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 }
365
366 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700367 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 }
369
Roland Levillain46648892015-06-19 16:07:18 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
371
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
374};
375
376class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
377 public:
Aart Bik42249c32016-01-07 15:33:50 -0800378 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800382 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700383 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100384 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000385 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 }
387
Roland Levillain46648892015-06-19 16:07:18 +0100388 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
389
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
392};
393
394CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
395 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100396 const CompilerOptions& compiler_options,
397 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 : CodeGenerator(graph,
399 kNumberOfGpuRegisters,
400 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000401 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
403 arraysize(kCoreCalleeSaves)),
404 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
405 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100406 compiler_options,
407 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100408 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 location_builder_(graph, this),
410 instruction_visitor_(graph, this),
411 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100412 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800413 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800414 uint32_literals_(std::less<uint32_t>(),
415 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800416 uint64_literals_(std::less<uint64_t>(),
417 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800418 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800419 boot_image_string_patches_(StringReferenceValueComparator(),
420 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
421 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
422 boot_image_type_patches_(TypeReferenceValueComparator(),
423 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
424 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
425 boot_image_address_patches_(std::less<uint32_t>(),
426 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700427 // Save RA (containing the return address) to mimic Quick.
428 AddAllocatedRegister(Location::RegisterLocation(RA));
429}
430
431#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100432// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
433#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700434#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700435
436void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700437 // Ensure that we fix up branches.
438 __ FinalizeCode();
439
440 // Adjust native pc offsets in stack maps.
441 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
442 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
443 uint32_t new_position = __ GetAdjustedPosition(old_position);
444 DCHECK_GE(new_position, old_position);
445 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
446 }
447
448 // Adjust pc offsets for the disassembly information.
449 if (disasm_info_ != nullptr) {
450 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
451 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
452 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
453 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
454 it.second.start = __ GetAdjustedPosition(it.second.start);
455 it.second.end = __ GetAdjustedPosition(it.second.end);
456 }
457 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
458 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
459 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
460 }
461 }
462
Alexey Frunze4dda3372015-06-01 18:31:49 -0700463 CodeGenerator::Finalize(allocator);
464}
465
466Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
467 return codegen_->GetAssembler();
468}
469
470void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100471 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700472 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
473}
474
475void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100476 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
478}
479
480void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
481 // Pop reg
482 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200483 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700484}
485
486void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
487 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200488 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700489 __ Sd(GpuRegister(reg), SP, 0);
490}
491
492void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
493 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
494 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
495 // Allocate a scratch register other than TMP, if available.
496 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
497 // automatically unspilled when the scratch scope object is destroyed).
498 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
499 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200500 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700501 __ LoadFromOffset(load_type,
502 GpuRegister(ensure_scratch.GetRegister()),
503 SP,
504 index1 + stack_offset);
505 __ LoadFromOffset(load_type,
506 TMP,
507 SP,
508 index2 + stack_offset);
509 __ StoreToOffset(store_type,
510 GpuRegister(ensure_scratch.GetRegister()),
511 SP,
512 index2 + stack_offset);
513 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
514}
515
516static dwarf::Reg DWARFReg(GpuRegister reg) {
517 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
518}
519
David Srbeckyba702002016-02-01 18:15:29 +0000520static dwarf::Reg DWARFReg(FpuRegister reg) {
521 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
522}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700523
524void CodeGeneratorMIPS64::GenerateFrameEntry() {
525 __ Bind(&frame_entry_label_);
526
527 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
528
529 if (do_overflow_check) {
530 __ LoadFromOffset(kLoadWord,
531 ZERO,
532 SP,
533 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
534 RecordPcInfo(nullptr, 0);
535 }
536
Alexey Frunze4dda3372015-06-01 18:31:49 -0700537 if (HasEmptyFrame()) {
538 return;
539 }
540
541 // Make sure the frame size isn't unreasonably large. Per the various APIs
542 // it looks like it should always be less than 2GB in size, which allows
543 // us using 32-bit signed offsets from the stack pointer.
544 if (GetFrameSize() > 0x7FFFFFFF)
545 LOG(FATAL) << "Stack frame larger than 2GB";
546
547 // Spill callee-saved registers.
548 // Note that their cumulative size is small and they can be indexed using
549 // 16-bit offsets.
550
551 // TODO: increment/decrement SP in one step instead of two or remove this comment.
552
553 uint32_t ofs = FrameEntrySpillSize();
554 __ IncreaseFrameSize(ofs);
555
556 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
557 GpuRegister reg = kCoreCalleeSaves[i];
558 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200559 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700560 __ Sd(reg, SP, ofs);
561 __ cfi().RelOffset(DWARFReg(reg), ofs);
562 }
563 }
564
565 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
566 FpuRegister reg = kFpuCalleeSaves[i];
567 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200568 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700569 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000570 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 }
572 }
573
574 // Allocate the rest of the frame and store the current method pointer
575 // at its end.
576
577 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
578
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100579 // Save the current method if we need it. Note that we do not
580 // do this in HCurrentMethod, as the instruction might have been removed
581 // in the SSA graph.
582 if (RequiresCurrentMethod()) {
583 static_assert(IsInt<16>(kCurrentMethodStackOffset),
584 "kCurrentMethodStackOffset must fit into int16_t");
585 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
586 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100587
588 if (GetGraph()->HasShouldDeoptimizeFlag()) {
589 // Initialize should_deoptimize flag to 0.
590 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
591 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700592}
593
594void CodeGeneratorMIPS64::GenerateFrameExit() {
595 __ cfi().RememberState();
596
Alexey Frunze4dda3372015-06-01 18:31:49 -0700597 if (!HasEmptyFrame()) {
598 // Deallocate the rest of the frame.
599
600 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
601
602 // Restore callee-saved registers.
603 // Note that their cumulative size is small and they can be indexed using
604 // 16-bit offsets.
605
606 // TODO: increment/decrement SP in one step instead of two or remove this comment.
607
608 uint32_t ofs = 0;
609
610 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
611 FpuRegister reg = kFpuCalleeSaves[i];
612 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
613 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200614 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000615 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700616 }
617 }
618
619 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
620 GpuRegister reg = kCoreCalleeSaves[i];
621 if (allocated_registers_.ContainsCoreRegister(reg)) {
622 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200623 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700624 __ cfi().Restore(DWARFReg(reg));
625 }
626 }
627
628 DCHECK_EQ(ofs, FrameEntrySpillSize());
629 __ DecreaseFrameSize(ofs);
630 }
631
632 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700633 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634
635 __ cfi().RestoreState();
636 __ cfi().DefCFAOffset(GetFrameSize());
637}
638
639void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
640 __ Bind(GetLabelOf(block));
641}
642
643void CodeGeneratorMIPS64::MoveLocation(Location destination,
644 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 if (source.Equals(destination)) {
647 return;
648 }
649
650 // A valid move can always be inferred from the destination and source
651 // locations. When moving from and to a register, the argument type can be
652 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100653 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700654 DCHECK_EQ(unspecified_type, false);
655
656 if (destination.IsRegister() || destination.IsFpuRegister()) {
657 if (unspecified_type) {
658 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
659 if (source.IsStackSlot() ||
660 (src_cst != nullptr && (src_cst->IsIntConstant()
661 || src_cst->IsFloatConstant()
662 || src_cst->IsNullConstant()))) {
663 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100664 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 } else {
666 // If the source is a double stack slot or a 64bit constant, a 64bit
667 // type is appropriate. Else the source is a register, and since the
668 // type has not been specified, we chose a 64bit type to force a 64bit
669 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 }
672 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
674 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700675 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
676 // Move to GPR/FPR from stack
677 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100678 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 __ LoadFpuFromOffset(load_type,
680 destination.AsFpuRegister<FpuRegister>(),
681 SP,
682 source.GetStackIndex());
683 } else {
684 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
685 __ LoadFromOffset(load_type,
686 destination.AsRegister<GpuRegister>(),
687 SP,
688 source.GetStackIndex());
689 }
690 } else if (source.IsConstant()) {
691 // Move to GPR/FPR from constant
692 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 gpr = destination.AsRegister<GpuRegister>();
695 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100696 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700697 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
698 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
699 gpr = ZERO;
700 } else {
701 __ LoadConst32(gpr, value);
702 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700703 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700704 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
705 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
706 gpr = ZERO;
707 } else {
708 __ LoadConst64(gpr, value);
709 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
715 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100716 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 if (destination.IsRegister()) {
718 // Move to GPR from GPR
719 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
720 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 DCHECK(destination.IsFpuRegister());
722 if (Primitive::Is64BitType(dst_type)) {
723 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 } else {
725 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
726 }
727 }
728 } else if (source.IsFpuRegister()) {
729 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100734 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700735 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
736 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100737 } else {
738 DCHECK(destination.IsRegister());
739 if (Primitive::Is64BitType(dst_type)) {
740 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 } else {
742 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
743 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 }
745 }
746 } else { // The destination is not a register. It must be a stack slot.
747 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
748 if (source.IsRegister() || source.IsFpuRegister()) {
749 if (unspecified_type) {
750 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100753 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700754 }
755 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100756 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
757 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700758 // Move to stack from GPR/FPR
759 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
760 if (source.IsRegister()) {
761 __ StoreToOffset(store_type,
762 source.AsRegister<GpuRegister>(),
763 SP,
764 destination.GetStackIndex());
765 } else {
766 __ StoreFpuToOffset(store_type,
767 source.AsFpuRegister<FpuRegister>(),
768 SP,
769 destination.GetStackIndex());
770 }
771 } else if (source.IsConstant()) {
772 // Move to stack from constant
773 HConstant* src_cst = source.GetConstant();
774 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
778 if (value != 0) {
779 gpr = TMP;
780 __ LoadConst32(gpr, value);
781 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700782 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700783 DCHECK(destination.IsDoubleStackSlot());
784 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
785 if (value != 0) {
786 gpr = TMP;
787 __ LoadConst64(gpr, value);
788 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700791 } else {
792 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
793 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
794 // Move to stack from stack
795 if (destination.IsStackSlot()) {
796 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
797 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
798 } else {
799 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
800 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
801 }
802 }
803 }
804}
805
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700806void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700807 DCHECK(!loc1.IsConstant());
808 DCHECK(!loc2.IsConstant());
809
810 if (loc1.Equals(loc2)) {
811 return;
812 }
813
814 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
815 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
816 bool is_fp_reg1 = loc1.IsFpuRegister();
817 bool is_fp_reg2 = loc2.IsFpuRegister();
818
819 if (loc2.IsRegister() && loc1.IsRegister()) {
820 // Swap 2 GPRs
821 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
822 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
823 __ Move(TMP, r2);
824 __ Move(r2, r1);
825 __ Move(r1, TMP);
826 } else if (is_fp_reg2 && is_fp_reg1) {
827 // Swap 2 FPRs
828 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
829 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700830 if (type == Primitive::kPrimFloat) {
831 __ MovS(FTMP, r1);
832 __ MovS(r1, r2);
833 __ MovS(r2, FTMP);
834 } else {
835 DCHECK_EQ(type, Primitive::kPrimDouble);
836 __ MovD(FTMP, r1);
837 __ MovD(r1, r2);
838 __ MovD(r2, FTMP);
839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700840 } else if (is_slot1 != is_slot2) {
841 // Swap GPR/FPR and stack slot
842 Location reg_loc = is_slot1 ? loc2 : loc1;
843 Location mem_loc = is_slot1 ? loc1 : loc2;
844 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
845 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
846 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
847 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
848 if (reg_loc.IsFpuRegister()) {
849 __ StoreFpuToOffset(store_type,
850 reg_loc.AsFpuRegister<FpuRegister>(),
851 SP,
852 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700853 if (mem_loc.IsStackSlot()) {
854 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
855 } else {
856 DCHECK(mem_loc.IsDoubleStackSlot());
857 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
858 }
859 } else {
860 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
861 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
862 }
863 } else if (is_slot1 && is_slot2) {
864 move_resolver_.Exchange(loc1.GetStackIndex(),
865 loc2.GetStackIndex(),
866 loc1.IsDoubleStackSlot());
867 } else {
868 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
869 }
870}
871
Calin Juravle175dc732015-08-25 15:42:32 +0100872void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
873 DCHECK(location.IsRegister());
874 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
875}
876
Calin Juravlee460d1d2015-09-29 04:52:17 +0100877void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
878 if (location.IsRegister()) {
879 locations->AddTemp(location);
880 } else {
881 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
882 }
883}
884
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100885void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
886 GpuRegister value,
887 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700888 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700889 GpuRegister card = AT;
890 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100891 if (value_can_be_null) {
892 __ Beqzc(value, &done);
893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700894 __ LoadFromOffset(kLoadDoubleword,
895 card,
896 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700897 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700898 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
899 __ Daddu(temp, card, temp);
900 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100901 if (value_can_be_null) {
902 __ Bind(&done);
903 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700904}
905
Alexey Frunze19f6c692016-11-30 19:19:55 -0800906template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
907inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
908 const ArenaDeque<PcRelativePatchInfo>& infos,
909 ArenaVector<LinkerPatch>* linker_patches) {
910 for (const PcRelativePatchInfo& info : infos) {
911 const DexFile& dex_file = info.target_dex_file;
912 size_t offset_or_index = info.offset_or_index;
913 DCHECK(info.pc_rel_label.IsBound());
914 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
915 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
916 }
917}
918
919void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
920 DCHECK(linker_patches->empty());
921 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -0800922 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800923 pc_relative_string_patches_.size() +
924 pc_relative_type_patches_.size() +
925 boot_image_string_patches_.size() +
926 boot_image_type_patches_.size() +
927 boot_image_address_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -0800928 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800929 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
930 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800931 if (!GetCompilerOptions().IsBootImage()) {
932 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
933 linker_patches);
934 } else {
935 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
936 linker_patches);
937 }
938 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
939 linker_patches);
940 for (const auto& entry : boot_image_string_patches_) {
941 const StringReference& target_string = entry.first;
942 Literal* literal = entry.second;
943 DCHECK(literal->GetLabel()->IsBound());
944 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
945 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
946 target_string.dex_file,
947 target_string.string_index.index_));
948 }
949 for (const auto& entry : boot_image_type_patches_) {
950 const TypeReference& target_type = entry.first;
951 Literal* literal = entry.second;
952 DCHECK(literal->GetLabel()->IsBound());
953 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
954 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
955 target_type.dex_file,
956 target_type.type_index.index_));
957 }
958 for (const auto& entry : boot_image_address_patches_) {
959 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
960 Literal* literal = entry.second;
961 DCHECK(literal->GetLabel()->IsBound());
962 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
963 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
964 }
965}
966
967CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
968 const DexFile& dex_file, uint32_t string_index) {
969 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
970}
971
972CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
973 const DexFile& dex_file, dex::TypeIndex type_index) {
974 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800975}
976
977CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
978 const DexFile& dex_file, uint32_t element_offset) {
979 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
980}
981
Alexey Frunze19f6c692016-11-30 19:19:55 -0800982CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
983 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
984 patches->emplace_back(dex_file, offset_or_index);
985 return &patches->back();
986}
987
Alexey Frunzef63f5692016-12-13 17:43:11 -0800988Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
989 return map->GetOrCreate(
990 value,
991 [this, value]() { return __ NewLiteral<uint32_t>(value); });
992}
993
Alexey Frunze19f6c692016-11-30 19:19:55 -0800994Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
995 return uint64_literals_.GetOrCreate(
996 value,
997 [this, value]() { return __ NewLiteral<uint64_t>(value); });
998}
999
1000Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1001 MethodToLiteralMap* map) {
1002 return map->GetOrCreate(
1003 target_method,
1004 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1005}
1006
Alexey Frunzef63f5692016-12-13 17:43:11 -08001007Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1008 dex::StringIndex string_index) {
1009 return boot_image_string_patches_.GetOrCreate(
1010 StringReference(&dex_file, string_index),
1011 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1012}
1013
1014Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1015 dex::TypeIndex type_index) {
1016 return boot_image_type_patches_.GetOrCreate(
1017 TypeReference(&dex_file, type_index),
1018 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1019}
1020
1021Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
1022 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1023 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1024 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1025}
1026
Alexey Frunze19f6c692016-11-30 19:19:55 -08001027void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1028 GpuRegister out) {
1029 __ Bind(&info->pc_rel_label);
1030 // Add the high half of a 32-bit offset to PC.
1031 __ Auipc(out, /* placeholder */ 0x1234);
1032 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001033 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001034}
1035
David Brazdil58282f42016-01-14 12:45:10 +00001036void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1038 blocked_core_registers_[ZERO] = true;
1039 blocked_core_registers_[K0] = true;
1040 blocked_core_registers_[K1] = true;
1041 blocked_core_registers_[GP] = true;
1042 blocked_core_registers_[SP] = true;
1043 blocked_core_registers_[RA] = true;
1044
Lazar Trsicd9672662015-09-03 17:33:01 +02001045 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1046 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001047 blocked_core_registers_[AT] = true;
1048 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001049 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001050 blocked_fpu_registers_[FTMP] = true;
1051
1052 // Reserve suspend and thread registers.
1053 blocked_core_registers_[S0] = true;
1054 blocked_core_registers_[TR] = true;
1055
1056 // Reserve T9 for function calls
1057 blocked_core_registers_[T9] = true;
1058
Goran Jakovljevic782be112016-06-21 12:39:04 +02001059 if (GetGraph()->IsDebuggable()) {
1060 // Stubs do not save callee-save floating point registers. If the graph
1061 // is debuggable, we need to deal with these registers differently. For
1062 // now, just block them.
1063 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1064 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1065 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001066 }
1067}
1068
Alexey Frunze4dda3372015-06-01 18:31:49 -07001069size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1070 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001071 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001072}
1073
1074size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1075 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001076 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001077}
1078
1079size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1080 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001081 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001082}
1083
1084size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1085 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001086 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087}
1088
1089void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001090 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091}
1092
1093void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001094 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001095}
1096
Calin Juravle175dc732015-08-25 15:42:32 +01001097void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 HInstruction* instruction,
1099 uint32_t dex_pc,
1100 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001101 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001102 __ LoadFromOffset(kLoadDoubleword,
1103 T9,
1104 TR,
1105 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001106 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001107 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001108 if (EntrypointRequiresStackMap(entrypoint)) {
1109 RecordPcInfo(instruction, dex_pc, slow_path);
1110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111}
1112
1113void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1114 GpuRegister class_reg) {
1115 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1116 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1117 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1118 // TODO: barrier needed?
1119 __ Bind(slow_path->GetExitLabel());
1120}
1121
1122void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1123 __ Sync(0); // only stype 0 is supported
1124}
1125
1126void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1127 HBasicBlock* successor) {
1128 SuspendCheckSlowPathMIPS64* slow_path =
1129 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1130 codegen_->AddSlowPath(slow_path);
1131
1132 __ LoadFromOffset(kLoadUnsignedHalfword,
1133 TMP,
1134 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001135 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001136 if (successor == nullptr) {
1137 __ Bnezc(TMP, slow_path->GetEntryLabel());
1138 __ Bind(slow_path->GetReturnLabel());
1139 } else {
1140 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001141 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142 // slow_path will return to GetLabelOf(successor).
1143 }
1144}
1145
1146InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1147 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001148 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149 assembler_(codegen->GetAssembler()),
1150 codegen_(codegen) {}
1151
1152void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1153 DCHECK_EQ(instruction->InputCount(), 2U);
1154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1155 Primitive::Type type = instruction->GetResultType();
1156 switch (type) {
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimLong: {
1159 locations->SetInAt(0, Location::RequiresRegister());
1160 HInstruction* right = instruction->InputAt(1);
1161 bool can_use_imm = false;
1162 if (right->IsConstant()) {
1163 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1164 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1165 can_use_imm = IsUint<16>(imm);
1166 } else if (instruction->IsAdd()) {
1167 can_use_imm = IsInt<16>(imm);
1168 } else {
1169 DCHECK(instruction->IsSub());
1170 can_use_imm = IsInt<16>(-imm);
1171 }
1172 }
1173 if (can_use_imm)
1174 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1175 else
1176 locations->SetInAt(1, Location::RequiresRegister());
1177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1178 }
1179 break;
1180
1181 case Primitive::kPrimFloat:
1182 case Primitive::kPrimDouble:
1183 locations->SetInAt(0, Location::RequiresFpuRegister());
1184 locations->SetInAt(1, Location::RequiresFpuRegister());
1185 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1186 break;
1187
1188 default:
1189 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1190 }
1191}
1192
1193void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1194 Primitive::Type type = instruction->GetType();
1195 LocationSummary* locations = instruction->GetLocations();
1196
1197 switch (type) {
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimLong: {
1200 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1201 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1202 Location rhs_location = locations->InAt(1);
1203
1204 GpuRegister rhs_reg = ZERO;
1205 int64_t rhs_imm = 0;
1206 bool use_imm = rhs_location.IsConstant();
1207 if (use_imm) {
1208 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1209 } else {
1210 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1211 }
1212
1213 if (instruction->IsAnd()) {
1214 if (use_imm)
1215 __ Andi(dst, lhs, rhs_imm);
1216 else
1217 __ And(dst, lhs, rhs_reg);
1218 } else if (instruction->IsOr()) {
1219 if (use_imm)
1220 __ Ori(dst, lhs, rhs_imm);
1221 else
1222 __ Or(dst, lhs, rhs_reg);
1223 } else if (instruction->IsXor()) {
1224 if (use_imm)
1225 __ Xori(dst, lhs, rhs_imm);
1226 else
1227 __ Xor(dst, lhs, rhs_reg);
1228 } else if (instruction->IsAdd()) {
1229 if (type == Primitive::kPrimInt) {
1230 if (use_imm)
1231 __ Addiu(dst, lhs, rhs_imm);
1232 else
1233 __ Addu(dst, lhs, rhs_reg);
1234 } else {
1235 if (use_imm)
1236 __ Daddiu(dst, lhs, rhs_imm);
1237 else
1238 __ Daddu(dst, lhs, rhs_reg);
1239 }
1240 } else {
1241 DCHECK(instruction->IsSub());
1242 if (type == Primitive::kPrimInt) {
1243 if (use_imm)
1244 __ Addiu(dst, lhs, -rhs_imm);
1245 else
1246 __ Subu(dst, lhs, rhs_reg);
1247 } else {
1248 if (use_imm)
1249 __ Daddiu(dst, lhs, -rhs_imm);
1250 else
1251 __ Dsubu(dst, lhs, rhs_reg);
1252 }
1253 }
1254 break;
1255 }
1256 case Primitive::kPrimFloat:
1257 case Primitive::kPrimDouble: {
1258 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1259 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1260 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1261 if (instruction->IsAdd()) {
1262 if (type == Primitive::kPrimFloat)
1263 __ AddS(dst, lhs, rhs);
1264 else
1265 __ AddD(dst, lhs, rhs);
1266 } else if (instruction->IsSub()) {
1267 if (type == Primitive::kPrimFloat)
1268 __ SubS(dst, lhs, rhs);
1269 else
1270 __ SubD(dst, lhs, rhs);
1271 } else {
1272 LOG(FATAL) << "Unexpected floating-point binary operation";
1273 }
1274 break;
1275 }
1276 default:
1277 LOG(FATAL) << "Unexpected binary operation type " << type;
1278 }
1279}
1280
1281void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001282 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283
1284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1285 Primitive::Type type = instr->GetResultType();
1286 switch (type) {
1287 case Primitive::kPrimInt:
1288 case Primitive::kPrimLong: {
1289 locations->SetInAt(0, Location::RequiresRegister());
1290 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 break;
1293 }
1294 default:
1295 LOG(FATAL) << "Unexpected shift type " << type;
1296 }
1297}
1298
1299void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001300 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001301 LocationSummary* locations = instr->GetLocations();
1302 Primitive::Type type = instr->GetType();
1303
1304 switch (type) {
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimLong: {
1307 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1308 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1309 Location rhs_location = locations->InAt(1);
1310
1311 GpuRegister rhs_reg = ZERO;
1312 int64_t rhs_imm = 0;
1313 bool use_imm = rhs_location.IsConstant();
1314 if (use_imm) {
1315 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1316 } else {
1317 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1318 }
1319
1320 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001321 uint32_t shift_value = rhs_imm &
1322 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001323
Alexey Frunze92d90602015-12-18 18:16:36 -08001324 if (shift_value == 0) {
1325 if (dst != lhs) {
1326 __ Move(dst, lhs);
1327 }
1328 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001329 if (instr->IsShl()) {
1330 __ Sll(dst, lhs, shift_value);
1331 } else if (instr->IsShr()) {
1332 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001333 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001334 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001335 } else {
1336 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001337 }
1338 } else {
1339 if (shift_value < 32) {
1340 if (instr->IsShl()) {
1341 __ Dsll(dst, lhs, shift_value);
1342 } else if (instr->IsShr()) {
1343 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001344 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001345 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001346 } else {
1347 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 }
1349 } else {
1350 shift_value -= 32;
1351 if (instr->IsShl()) {
1352 __ Dsll32(dst, lhs, shift_value);
1353 } else if (instr->IsShr()) {
1354 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001355 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001356 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001357 } else {
1358 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001359 }
1360 }
1361 }
1362 } else {
1363 if (type == Primitive::kPrimInt) {
1364 if (instr->IsShl()) {
1365 __ Sllv(dst, lhs, rhs_reg);
1366 } else if (instr->IsShr()) {
1367 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001368 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001369 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001370 } else {
1371 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001372 }
1373 } else {
1374 if (instr->IsShl()) {
1375 __ Dsllv(dst, lhs, rhs_reg);
1376 } else if (instr->IsShr()) {
1377 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001378 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001379 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001380 } else {
1381 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001382 }
1383 }
1384 }
1385 break;
1386 }
1387 default:
1388 LOG(FATAL) << "Unexpected shift operation type " << type;
1389 }
1390}
1391
1392void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1393 HandleBinaryOp(instruction);
1394}
1395
1396void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1397 HandleBinaryOp(instruction);
1398}
1399
1400void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1401 HandleBinaryOp(instruction);
1402}
1403
1404void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1405 HandleBinaryOp(instruction);
1406}
1407
1408void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1409 LocationSummary* locations =
1410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1411 locations->SetInAt(0, Location::RequiresRegister());
1412 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1413 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1414 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1415 } else {
1416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1417 }
1418}
1419
1420void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1421 LocationSummary* locations = instruction->GetLocations();
1422 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1423 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001424 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001426 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 switch (type) {
1428 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001429 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1430 if (index.IsConstant()) {
1431 size_t offset =
1432 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1433 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1434 } else {
1435 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1436 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1437 }
1438 break;
1439 }
1440
1441 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001442 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1443 if (index.IsConstant()) {
1444 size_t offset =
1445 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1446 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1447 } else {
1448 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1449 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1450 }
1451 break;
1452 }
1453
1454 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001455 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1456 if (index.IsConstant()) {
1457 size_t offset =
1458 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1459 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1460 } else {
1461 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1462 __ Daddu(TMP, obj, TMP);
1463 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001469 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1470 if (index.IsConstant()) {
1471 size_t offset =
1472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1473 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1474 } else {
1475 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1476 __ Daddu(TMP, obj, TMP);
1477 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1478 }
1479 break;
1480 }
1481
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimNot: {
1484 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001485 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1486 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1487 if (index.IsConstant()) {
1488 size_t offset =
1489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1490 __ LoadFromOffset(load_type, out, obj, offset);
1491 } else {
1492 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1493 __ Daddu(TMP, obj, TMP);
1494 __ LoadFromOffset(load_type, out, TMP, data_offset);
1495 }
1496 break;
1497 }
1498
1499 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001500 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1501 if (index.IsConstant()) {
1502 size_t offset =
1503 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1504 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1505 } else {
1506 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1507 __ Daddu(TMP, obj, TMP);
1508 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1509 }
1510 break;
1511 }
1512
1513 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001514 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1515 if (index.IsConstant()) {
1516 size_t offset =
1517 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1518 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1519 } else {
1520 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1521 __ Daddu(TMP, obj, TMP);
1522 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1523 }
1524 break;
1525 }
1526
1527 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001528 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1529 if (index.IsConstant()) {
1530 size_t offset =
1531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1532 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1533 } else {
1534 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1535 __ Daddu(TMP, obj, TMP);
1536 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1537 }
1538 break;
1539 }
1540
1541 case Primitive::kPrimVoid:
1542 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1543 UNREACHABLE();
1544 }
1545 codegen_->MaybeRecordImplicitNullCheck(instruction);
1546}
1547
1548void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1550 locations->SetInAt(0, Location::RequiresRegister());
1551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1552}
1553
1554void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1555 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001556 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001557 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1558 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1559 __ LoadFromOffset(kLoadWord, out, obj, offset);
1560 codegen_->MaybeRecordImplicitNullCheck(instruction);
1561}
1562
1563void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001564 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1566 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001567 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001568 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001569 InvokeRuntimeCallingConvention calling_convention;
1570 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1571 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1572 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1573 } else {
1574 locations->SetInAt(0, Location::RequiresRegister());
1575 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1576 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1577 locations->SetInAt(2, Location::RequiresFpuRegister());
1578 } else {
1579 locations->SetInAt(2, Location::RequiresRegister());
1580 }
1581 }
1582}
1583
1584void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1585 LocationSummary* locations = instruction->GetLocations();
1586 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1587 Location index = locations->InAt(1);
1588 Primitive::Type value_type = instruction->GetComponentType();
1589 bool needs_runtime_call = locations->WillCall();
1590 bool needs_write_barrier =
1591 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1592
1593 switch (value_type) {
1594 case Primitive::kPrimBoolean:
1595 case Primitive::kPrimByte: {
1596 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1597 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1598 if (index.IsConstant()) {
1599 size_t offset =
1600 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1601 __ StoreToOffset(kStoreByte, value, obj, offset);
1602 } else {
1603 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1604 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1605 }
1606 break;
1607 }
1608
1609 case Primitive::kPrimShort:
1610 case Primitive::kPrimChar: {
1611 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1612 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1613 if (index.IsConstant()) {
1614 size_t offset =
1615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1616 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1617 } else {
1618 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1619 __ Daddu(TMP, obj, TMP);
1620 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1621 }
1622 break;
1623 }
1624
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimNot: {
1627 if (!needs_runtime_call) {
1628 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1629 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1630 if (index.IsConstant()) {
1631 size_t offset =
1632 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1633 __ StoreToOffset(kStoreWord, value, obj, offset);
1634 } else {
1635 DCHECK(index.IsRegister()) << index;
1636 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1637 __ Daddu(TMP, obj, TMP);
1638 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1639 }
1640 codegen_->MaybeRecordImplicitNullCheck(instruction);
1641 if (needs_write_barrier) {
1642 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001643 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001644 }
1645 } else {
1646 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001647 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001648 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001649 }
1650 break;
1651 }
1652
1653 case Primitive::kPrimLong: {
1654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1655 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1656 if (index.IsConstant()) {
1657 size_t offset =
1658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1659 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1660 } else {
1661 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1662 __ Daddu(TMP, obj, TMP);
1663 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1664 }
1665 break;
1666 }
1667
1668 case Primitive::kPrimFloat: {
1669 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1670 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1671 DCHECK(locations->InAt(2).IsFpuRegister());
1672 if (index.IsConstant()) {
1673 size_t offset =
1674 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1675 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1676 } else {
1677 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1678 __ Daddu(TMP, obj, TMP);
1679 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1680 }
1681 break;
1682 }
1683
1684 case Primitive::kPrimDouble: {
1685 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1686 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1687 DCHECK(locations->InAt(2).IsFpuRegister());
1688 if (index.IsConstant()) {
1689 size_t offset =
1690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1691 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1692 } else {
1693 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1694 __ Daddu(TMP, obj, TMP);
1695 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1696 }
1697 break;
1698 }
1699
1700 case Primitive::kPrimVoid:
1701 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1702 UNREACHABLE();
1703 }
1704
1705 // Ints and objects are handled in the switch.
1706 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1707 codegen_->MaybeRecordImplicitNullCheck(instruction);
1708 }
1709}
1710
1711void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001712 RegisterSet caller_saves = RegisterSet::Empty();
1713 InvokeRuntimeCallingConvention calling_convention;
1714 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1715 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1716 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001717 locations->SetInAt(0, Location::RequiresRegister());
1718 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001719}
1720
1721void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1722 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001723 BoundsCheckSlowPathMIPS64* slow_path =
1724 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001725 codegen_->AddSlowPath(slow_path);
1726
1727 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1728 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1729
1730 // length is limited by the maximum positive signed 32-bit integer.
1731 // Unsigned comparison of length and index checks for index < 0
1732 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001733 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001734}
1735
1736void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1738 instruction,
1739 LocationSummary::kCallOnSlowPath);
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001742 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743 locations->AddTemp(Location::RequiresRegister());
1744}
1745
1746void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1747 LocationSummary* locations = instruction->GetLocations();
1748 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1749 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1750 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1751
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001752 SlowPathCodeMIPS64* slow_path =
1753 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754 codegen_->AddSlowPath(slow_path);
1755
1756 // TODO: avoid this check if we know obj is not null.
1757 __ Beqzc(obj, slow_path->GetExitLabel());
1758 // Compare the class of `obj` with `cls`.
1759 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1760 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1761 __ Bind(slow_path->GetExitLabel());
1762}
1763
1764void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1765 LocationSummary* locations =
1766 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1767 locations->SetInAt(0, Location::RequiresRegister());
1768 if (check->HasUses()) {
1769 locations->SetOut(Location::SameAsFirstInput());
1770 }
1771}
1772
1773void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1774 // We assume the class is not null.
1775 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1776 check->GetLoadClass(),
1777 check,
1778 check->GetDexPc(),
1779 true);
1780 codegen_->AddSlowPath(slow_path);
1781 GenerateClassInitializationCheck(slow_path,
1782 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1783}
1784
1785void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1786 Primitive::Type in_type = compare->InputAt(0)->GetType();
1787
Alexey Frunze299a9392015-12-08 16:08:02 -08001788 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789
1790 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001791 case Primitive::kPrimBoolean:
1792 case Primitive::kPrimByte:
1793 case Primitive::kPrimShort:
1794 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001795 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 case Primitive::kPrimLong:
1797 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001798 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1800 break;
1801
1802 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001803 case Primitive::kPrimDouble:
1804 locations->SetInAt(0, Location::RequiresFpuRegister());
1805 locations->SetInAt(1, Location::RequiresFpuRegister());
1806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001807 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808
1809 default:
1810 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1811 }
1812}
1813
1814void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1815 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001816 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1818
1819 // 0 if: left == right
1820 // 1 if: left > right
1821 // -1 if: left < right
1822 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001823 case Primitive::kPrimBoolean:
1824 case Primitive::kPrimByte:
1825 case Primitive::kPrimShort:
1826 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001827 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001829 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001830 Location rhs_location = locations->InAt(1);
1831 bool use_imm = rhs_location.IsConstant();
1832 GpuRegister rhs = ZERO;
1833 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001834 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001835 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1836 if (value != 0) {
1837 rhs = AT;
1838 __ LoadConst64(rhs, value);
1839 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001840 } else {
1841 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1842 if (value != 0) {
1843 rhs = AT;
1844 __ LoadConst32(rhs, value);
1845 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001846 }
1847 } else {
1848 rhs = rhs_location.AsRegister<GpuRegister>();
1849 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001850 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001851 __ Slt(res, rhs, lhs);
1852 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001853 break;
1854 }
1855
Alexey Frunze299a9392015-12-08 16:08:02 -08001856 case Primitive::kPrimFloat: {
1857 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1858 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1859 Mips64Label done;
1860 __ CmpEqS(FTMP, lhs, rhs);
1861 __ LoadConst32(res, 0);
1862 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001863 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001864 __ CmpLtS(FTMP, lhs, rhs);
1865 __ LoadConst32(res, -1);
1866 __ Bc1nez(FTMP, &done);
1867 __ LoadConst32(res, 1);
1868 } else {
1869 __ CmpLtS(FTMP, rhs, lhs);
1870 __ LoadConst32(res, 1);
1871 __ Bc1nez(FTMP, &done);
1872 __ LoadConst32(res, -1);
1873 }
1874 __ Bind(&done);
1875 break;
1876 }
1877
Alexey Frunze4dda3372015-06-01 18:31:49 -07001878 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001879 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1880 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1881 Mips64Label done;
1882 __ CmpEqD(FTMP, lhs, rhs);
1883 __ LoadConst32(res, 0);
1884 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001885 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001886 __ CmpLtD(FTMP, lhs, rhs);
1887 __ LoadConst32(res, -1);
1888 __ Bc1nez(FTMP, &done);
1889 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001890 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001891 __ CmpLtD(FTMP, rhs, lhs);
1892 __ LoadConst32(res, 1);
1893 __ Bc1nez(FTMP, &done);
1894 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001895 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001896 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001897 break;
1898 }
1899
1900 default:
1901 LOG(FATAL) << "Unimplemented compare type " << in_type;
1902 }
1903}
1904
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001905void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001907 switch (instruction->InputAt(0)->GetType()) {
1908 default:
1909 case Primitive::kPrimLong:
1910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1912 break;
1913
1914 case Primitive::kPrimFloat:
1915 case Primitive::kPrimDouble:
1916 locations->SetInAt(0, Location::RequiresFpuRegister());
1917 locations->SetInAt(1, Location::RequiresFpuRegister());
1918 break;
1919 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001920 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1922 }
1923}
1924
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001926 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001927 return;
1928 }
1929
Alexey Frunze299a9392015-12-08 16:08:02 -08001930 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001931 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001932 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001933 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001934
Alexey Frunze299a9392015-12-08 16:08:02 -08001935 switch (type) {
1936 default:
1937 // Integer case.
1938 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1939 return;
1940 case Primitive::kPrimLong:
1941 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1942 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001943
Alexey Frunze299a9392015-12-08 16:08:02 -08001944 case Primitive::kPrimFloat:
1945 case Primitive::kPrimDouble:
1946 // TODO: don't use branches.
1947 GenerateFpCompareAndBranch(instruction->GetCondition(),
1948 instruction->IsGtBias(),
1949 type,
1950 locations,
1951 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001952 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001953 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001954
1955 // Convert the branches into the result.
1956 Mips64Label done;
1957
1958 // False case: result = 0.
1959 __ LoadConst32(dst, 0);
1960 __ Bc(&done);
1961
1962 // True case: result = 1.
1963 __ Bind(&true_label);
1964 __ LoadConst32(dst, 1);
1965 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001966}
1967
Alexey Frunzec857c742015-09-23 15:12:39 -07001968void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1969 DCHECK(instruction->IsDiv() || instruction->IsRem());
1970 Primitive::Type type = instruction->GetResultType();
1971
1972 LocationSummary* locations = instruction->GetLocations();
1973 Location second = locations->InAt(1);
1974 DCHECK(second.IsConstant());
1975
1976 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1977 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1978 int64_t imm = Int64FromConstant(second.GetConstant());
1979 DCHECK(imm == 1 || imm == -1);
1980
1981 if (instruction->IsRem()) {
1982 __ Move(out, ZERO);
1983 } else {
1984 if (imm == -1) {
1985 if (type == Primitive::kPrimInt) {
1986 __ Subu(out, ZERO, dividend);
1987 } else {
1988 DCHECK_EQ(type, Primitive::kPrimLong);
1989 __ Dsubu(out, ZERO, dividend);
1990 }
1991 } else if (out != dividend) {
1992 __ Move(out, dividend);
1993 }
1994 }
1995}
1996
1997void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1998 DCHECK(instruction->IsDiv() || instruction->IsRem());
1999 Primitive::Type type = instruction->GetResultType();
2000
2001 LocationSummary* locations = instruction->GetLocations();
2002 Location second = locations->InAt(1);
2003 DCHECK(second.IsConstant());
2004
2005 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2006 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2007 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002008 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002009 int ctz_imm = CTZ(abs_imm);
2010
2011 if (instruction->IsDiv()) {
2012 if (type == Primitive::kPrimInt) {
2013 if (ctz_imm == 1) {
2014 // Fast path for division by +/-2, which is very common.
2015 __ Srl(TMP, dividend, 31);
2016 } else {
2017 __ Sra(TMP, dividend, 31);
2018 __ Srl(TMP, TMP, 32 - ctz_imm);
2019 }
2020 __ Addu(out, dividend, TMP);
2021 __ Sra(out, out, ctz_imm);
2022 if (imm < 0) {
2023 __ Subu(out, ZERO, out);
2024 }
2025 } else {
2026 DCHECK_EQ(type, Primitive::kPrimLong);
2027 if (ctz_imm == 1) {
2028 // Fast path for division by +/-2, which is very common.
2029 __ Dsrl32(TMP, dividend, 31);
2030 } else {
2031 __ Dsra32(TMP, dividend, 31);
2032 if (ctz_imm > 32) {
2033 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2034 } else {
2035 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2036 }
2037 }
2038 __ Daddu(out, dividend, TMP);
2039 if (ctz_imm < 32) {
2040 __ Dsra(out, out, ctz_imm);
2041 } else {
2042 __ Dsra32(out, out, ctz_imm - 32);
2043 }
2044 if (imm < 0) {
2045 __ Dsubu(out, ZERO, out);
2046 }
2047 }
2048 } else {
2049 if (type == Primitive::kPrimInt) {
2050 if (ctz_imm == 1) {
2051 // Fast path for modulo +/-2, which is very common.
2052 __ Sra(TMP, dividend, 31);
2053 __ Subu(out, dividend, TMP);
2054 __ Andi(out, out, 1);
2055 __ Addu(out, out, TMP);
2056 } else {
2057 __ Sra(TMP, dividend, 31);
2058 __ Srl(TMP, TMP, 32 - ctz_imm);
2059 __ Addu(out, dividend, TMP);
2060 if (IsUint<16>(abs_imm - 1)) {
2061 __ Andi(out, out, abs_imm - 1);
2062 } else {
2063 __ Sll(out, out, 32 - ctz_imm);
2064 __ Srl(out, out, 32 - ctz_imm);
2065 }
2066 __ Subu(out, out, TMP);
2067 }
2068 } else {
2069 DCHECK_EQ(type, Primitive::kPrimLong);
2070 if (ctz_imm == 1) {
2071 // Fast path for modulo +/-2, which is very common.
2072 __ Dsra32(TMP, dividend, 31);
2073 __ Dsubu(out, dividend, TMP);
2074 __ Andi(out, out, 1);
2075 __ Daddu(out, out, TMP);
2076 } else {
2077 __ Dsra32(TMP, dividend, 31);
2078 if (ctz_imm > 32) {
2079 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2080 } else {
2081 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2082 }
2083 __ Daddu(out, dividend, TMP);
2084 if (IsUint<16>(abs_imm - 1)) {
2085 __ Andi(out, out, abs_imm - 1);
2086 } else {
2087 if (ctz_imm > 32) {
2088 __ Dsll(out, out, 64 - ctz_imm);
2089 __ Dsrl(out, out, 64 - ctz_imm);
2090 } else {
2091 __ Dsll32(out, out, 32 - ctz_imm);
2092 __ Dsrl32(out, out, 32 - ctz_imm);
2093 }
2094 }
2095 __ Dsubu(out, out, TMP);
2096 }
2097 }
2098 }
2099}
2100
2101void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2102 DCHECK(instruction->IsDiv() || instruction->IsRem());
2103
2104 LocationSummary* locations = instruction->GetLocations();
2105 Location second = locations->InAt(1);
2106 DCHECK(second.IsConstant());
2107
2108 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2109 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2110 int64_t imm = Int64FromConstant(second.GetConstant());
2111
2112 Primitive::Type type = instruction->GetResultType();
2113 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2114
2115 int64_t magic;
2116 int shift;
2117 CalculateMagicAndShiftForDivRem(imm,
2118 (type == Primitive::kPrimLong),
2119 &magic,
2120 &shift);
2121
2122 if (type == Primitive::kPrimInt) {
2123 __ LoadConst32(TMP, magic);
2124 __ MuhR6(TMP, dividend, TMP);
2125
2126 if (imm > 0 && magic < 0) {
2127 __ Addu(TMP, TMP, dividend);
2128 } else if (imm < 0 && magic > 0) {
2129 __ Subu(TMP, TMP, dividend);
2130 }
2131
2132 if (shift != 0) {
2133 __ Sra(TMP, TMP, shift);
2134 }
2135
2136 if (instruction->IsDiv()) {
2137 __ Sra(out, TMP, 31);
2138 __ Subu(out, TMP, out);
2139 } else {
2140 __ Sra(AT, TMP, 31);
2141 __ Subu(AT, TMP, AT);
2142 __ LoadConst32(TMP, imm);
2143 __ MulR6(TMP, AT, TMP);
2144 __ Subu(out, dividend, TMP);
2145 }
2146 } else {
2147 __ LoadConst64(TMP, magic);
2148 __ Dmuh(TMP, dividend, TMP);
2149
2150 if (imm > 0 && magic < 0) {
2151 __ Daddu(TMP, TMP, dividend);
2152 } else if (imm < 0 && magic > 0) {
2153 __ Dsubu(TMP, TMP, dividend);
2154 }
2155
2156 if (shift >= 32) {
2157 __ Dsra32(TMP, TMP, shift - 32);
2158 } else if (shift > 0) {
2159 __ Dsra(TMP, TMP, shift);
2160 }
2161
2162 if (instruction->IsDiv()) {
2163 __ Dsra32(out, TMP, 31);
2164 __ Dsubu(out, TMP, out);
2165 } else {
2166 __ Dsra32(AT, TMP, 31);
2167 __ Dsubu(AT, TMP, AT);
2168 __ LoadConst64(TMP, imm);
2169 __ Dmul(TMP, AT, TMP);
2170 __ Dsubu(out, dividend, TMP);
2171 }
2172 }
2173}
2174
2175void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2176 DCHECK(instruction->IsDiv() || instruction->IsRem());
2177 Primitive::Type type = instruction->GetResultType();
2178 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2179
2180 LocationSummary* locations = instruction->GetLocations();
2181 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2182 Location second = locations->InAt(1);
2183
2184 if (second.IsConstant()) {
2185 int64_t imm = Int64FromConstant(second.GetConstant());
2186 if (imm == 0) {
2187 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2188 } else if (imm == 1 || imm == -1) {
2189 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002190 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002191 DivRemByPowerOfTwo(instruction);
2192 } else {
2193 DCHECK(imm <= -2 || imm >= 2);
2194 GenerateDivRemWithAnyConstant(instruction);
2195 }
2196 } else {
2197 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2198 GpuRegister divisor = second.AsRegister<GpuRegister>();
2199 if (instruction->IsDiv()) {
2200 if (type == Primitive::kPrimInt)
2201 __ DivR6(out, dividend, divisor);
2202 else
2203 __ Ddiv(out, dividend, divisor);
2204 } else {
2205 if (type == Primitive::kPrimInt)
2206 __ ModR6(out, dividend, divisor);
2207 else
2208 __ Dmod(out, dividend, divisor);
2209 }
2210 }
2211}
2212
Alexey Frunze4dda3372015-06-01 18:31:49 -07002213void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2216 switch (div->GetResultType()) {
2217 case Primitive::kPrimInt:
2218 case Primitive::kPrimLong:
2219 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002220 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2222 break;
2223
2224 case Primitive::kPrimFloat:
2225 case Primitive::kPrimDouble:
2226 locations->SetInAt(0, Location::RequiresFpuRegister());
2227 locations->SetInAt(1, Location::RequiresFpuRegister());
2228 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2229 break;
2230
2231 default:
2232 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2233 }
2234}
2235
2236void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2237 Primitive::Type type = instruction->GetType();
2238 LocationSummary* locations = instruction->GetLocations();
2239
2240 switch (type) {
2241 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002242 case Primitive::kPrimLong:
2243 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002245 case Primitive::kPrimFloat:
2246 case Primitive::kPrimDouble: {
2247 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2248 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2249 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2250 if (type == Primitive::kPrimFloat)
2251 __ DivS(dst, lhs, rhs);
2252 else
2253 __ DivD(dst, lhs, rhs);
2254 break;
2255 }
2256 default:
2257 LOG(FATAL) << "Unexpected div type " << type;
2258 }
2259}
2260
2261void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002262 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002263 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264}
2265
2266void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2267 SlowPathCodeMIPS64* slow_path =
2268 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2269 codegen_->AddSlowPath(slow_path);
2270 Location value = instruction->GetLocations()->InAt(0);
2271
2272 Primitive::Type type = instruction->GetType();
2273
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002274 if (!Primitive::IsIntegralType(type)) {
2275 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002276 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002277 }
2278
2279 if (value.IsConstant()) {
2280 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2281 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002282 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002283 } else {
2284 // A division by a non-null constant is valid. We don't need to perform
2285 // any check, so simply fall through.
2286 }
2287 } else {
2288 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2289 }
2290}
2291
2292void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2293 LocationSummary* locations =
2294 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2295 locations->SetOut(Location::ConstantLocation(constant));
2296}
2297
2298void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2299 // Will be generated at use site.
2300}
2301
2302void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2303 exit->SetLocations(nullptr);
2304}
2305
2306void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2307}
2308
2309void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2310 LocationSummary* locations =
2311 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2312 locations->SetOut(Location::ConstantLocation(constant));
2313}
2314
2315void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2316 // Will be generated at use site.
2317}
2318
David Brazdilfc6a86a2015-06-26 10:33:45 +00002319void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002320 DCHECK(!successor->IsExitBlock());
2321 HBasicBlock* block = got->GetBlock();
2322 HInstruction* previous = got->GetPrevious();
2323 HLoopInformation* info = block->GetLoopInformation();
2324
2325 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2326 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2327 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2328 return;
2329 }
2330 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2331 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2332 }
2333 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002334 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002335 }
2336}
2337
David Brazdilfc6a86a2015-06-26 10:33:45 +00002338void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2339 got->SetLocations(nullptr);
2340}
2341
2342void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2343 HandleGoto(got, got->GetSuccessor());
2344}
2345
2346void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2347 try_boundary->SetLocations(nullptr);
2348}
2349
2350void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2351 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2352 if (!successor->IsExitBlock()) {
2353 HandleGoto(try_boundary, successor);
2354 }
2355}
2356
Alexey Frunze299a9392015-12-08 16:08:02 -08002357void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2358 bool is64bit,
2359 LocationSummary* locations) {
2360 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2361 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2362 Location rhs_location = locations->InAt(1);
2363 GpuRegister rhs_reg = ZERO;
2364 int64_t rhs_imm = 0;
2365 bool use_imm = rhs_location.IsConstant();
2366 if (use_imm) {
2367 if (is64bit) {
2368 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2369 } else {
2370 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2371 }
2372 } else {
2373 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2374 }
2375 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2376
2377 switch (cond) {
2378 case kCondEQ:
2379 case kCondNE:
2380 if (use_imm && IsUint<16>(rhs_imm)) {
2381 __ Xori(dst, lhs, rhs_imm);
2382 } else {
2383 if (use_imm) {
2384 rhs_reg = TMP;
2385 __ LoadConst64(rhs_reg, rhs_imm);
2386 }
2387 __ Xor(dst, lhs, rhs_reg);
2388 }
2389 if (cond == kCondEQ) {
2390 __ Sltiu(dst, dst, 1);
2391 } else {
2392 __ Sltu(dst, ZERO, dst);
2393 }
2394 break;
2395
2396 case kCondLT:
2397 case kCondGE:
2398 if (use_imm && IsInt<16>(rhs_imm)) {
2399 __ Slti(dst, lhs, rhs_imm);
2400 } else {
2401 if (use_imm) {
2402 rhs_reg = TMP;
2403 __ LoadConst64(rhs_reg, rhs_imm);
2404 }
2405 __ Slt(dst, lhs, rhs_reg);
2406 }
2407 if (cond == kCondGE) {
2408 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2409 // only the slt instruction but no sge.
2410 __ Xori(dst, dst, 1);
2411 }
2412 break;
2413
2414 case kCondLE:
2415 case kCondGT:
2416 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2417 // Simulate lhs <= rhs via lhs < rhs + 1.
2418 __ Slti(dst, lhs, rhs_imm_plus_one);
2419 if (cond == kCondGT) {
2420 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2421 // only the slti instruction but no sgti.
2422 __ Xori(dst, dst, 1);
2423 }
2424 } else {
2425 if (use_imm) {
2426 rhs_reg = TMP;
2427 __ LoadConst64(rhs_reg, rhs_imm);
2428 }
2429 __ Slt(dst, rhs_reg, lhs);
2430 if (cond == kCondLE) {
2431 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2432 // only the slt instruction but no sle.
2433 __ Xori(dst, dst, 1);
2434 }
2435 }
2436 break;
2437
2438 case kCondB:
2439 case kCondAE:
2440 if (use_imm && IsInt<16>(rhs_imm)) {
2441 // Sltiu sign-extends its 16-bit immediate operand before
2442 // the comparison and thus lets us compare directly with
2443 // unsigned values in the ranges [0, 0x7fff] and
2444 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2445 __ Sltiu(dst, lhs, rhs_imm);
2446 } else {
2447 if (use_imm) {
2448 rhs_reg = TMP;
2449 __ LoadConst64(rhs_reg, rhs_imm);
2450 }
2451 __ Sltu(dst, lhs, rhs_reg);
2452 }
2453 if (cond == kCondAE) {
2454 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2455 // only the sltu instruction but no sgeu.
2456 __ Xori(dst, dst, 1);
2457 }
2458 break;
2459
2460 case kCondBE:
2461 case kCondA:
2462 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2463 // Simulate lhs <= rhs via lhs < rhs + 1.
2464 // Note that this only works if rhs + 1 does not overflow
2465 // to 0, hence the check above.
2466 // Sltiu sign-extends its 16-bit immediate operand before
2467 // the comparison and thus lets us compare directly with
2468 // unsigned values in the ranges [0, 0x7fff] and
2469 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2470 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2471 if (cond == kCondA) {
2472 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2473 // only the sltiu instruction but no sgtiu.
2474 __ Xori(dst, dst, 1);
2475 }
2476 } else {
2477 if (use_imm) {
2478 rhs_reg = TMP;
2479 __ LoadConst64(rhs_reg, rhs_imm);
2480 }
2481 __ Sltu(dst, rhs_reg, lhs);
2482 if (cond == kCondBE) {
2483 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2484 // only the sltu instruction but no sleu.
2485 __ Xori(dst, dst, 1);
2486 }
2487 }
2488 break;
2489 }
2490}
2491
2492void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2493 bool is64bit,
2494 LocationSummary* locations,
2495 Mips64Label* label) {
2496 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2497 Location rhs_location = locations->InAt(1);
2498 GpuRegister rhs_reg = ZERO;
2499 int64_t rhs_imm = 0;
2500 bool use_imm = rhs_location.IsConstant();
2501 if (use_imm) {
2502 if (is64bit) {
2503 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2504 } else {
2505 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2506 }
2507 } else {
2508 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2509 }
2510
2511 if (use_imm && rhs_imm == 0) {
2512 switch (cond) {
2513 case kCondEQ:
2514 case kCondBE: // <= 0 if zero
2515 __ Beqzc(lhs, label);
2516 break;
2517 case kCondNE:
2518 case kCondA: // > 0 if non-zero
2519 __ Bnezc(lhs, label);
2520 break;
2521 case kCondLT:
2522 __ Bltzc(lhs, label);
2523 break;
2524 case kCondGE:
2525 __ Bgezc(lhs, label);
2526 break;
2527 case kCondLE:
2528 __ Blezc(lhs, label);
2529 break;
2530 case kCondGT:
2531 __ Bgtzc(lhs, label);
2532 break;
2533 case kCondB: // always false
2534 break;
2535 case kCondAE: // always true
2536 __ Bc(label);
2537 break;
2538 }
2539 } else {
2540 if (use_imm) {
2541 rhs_reg = TMP;
2542 __ LoadConst64(rhs_reg, rhs_imm);
2543 }
2544 switch (cond) {
2545 case kCondEQ:
2546 __ Beqc(lhs, rhs_reg, label);
2547 break;
2548 case kCondNE:
2549 __ Bnec(lhs, rhs_reg, label);
2550 break;
2551 case kCondLT:
2552 __ Bltc(lhs, rhs_reg, label);
2553 break;
2554 case kCondGE:
2555 __ Bgec(lhs, rhs_reg, label);
2556 break;
2557 case kCondLE:
2558 __ Bgec(rhs_reg, lhs, label);
2559 break;
2560 case kCondGT:
2561 __ Bltc(rhs_reg, lhs, label);
2562 break;
2563 case kCondB:
2564 __ Bltuc(lhs, rhs_reg, label);
2565 break;
2566 case kCondAE:
2567 __ Bgeuc(lhs, rhs_reg, label);
2568 break;
2569 case kCondBE:
2570 __ Bgeuc(rhs_reg, lhs, label);
2571 break;
2572 case kCondA:
2573 __ Bltuc(rhs_reg, lhs, label);
2574 break;
2575 }
2576 }
2577}
2578
2579void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2580 bool gt_bias,
2581 Primitive::Type type,
2582 LocationSummary* locations,
2583 Mips64Label* label) {
2584 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2585 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2586 if (type == Primitive::kPrimFloat) {
2587 switch (cond) {
2588 case kCondEQ:
2589 __ CmpEqS(FTMP, lhs, rhs);
2590 __ Bc1nez(FTMP, label);
2591 break;
2592 case kCondNE:
2593 __ CmpEqS(FTMP, lhs, rhs);
2594 __ Bc1eqz(FTMP, label);
2595 break;
2596 case kCondLT:
2597 if (gt_bias) {
2598 __ CmpLtS(FTMP, lhs, rhs);
2599 } else {
2600 __ CmpUltS(FTMP, lhs, rhs);
2601 }
2602 __ Bc1nez(FTMP, label);
2603 break;
2604 case kCondLE:
2605 if (gt_bias) {
2606 __ CmpLeS(FTMP, lhs, rhs);
2607 } else {
2608 __ CmpUleS(FTMP, lhs, rhs);
2609 }
2610 __ Bc1nez(FTMP, label);
2611 break;
2612 case kCondGT:
2613 if (gt_bias) {
2614 __ CmpUltS(FTMP, rhs, lhs);
2615 } else {
2616 __ CmpLtS(FTMP, rhs, lhs);
2617 }
2618 __ Bc1nez(FTMP, label);
2619 break;
2620 case kCondGE:
2621 if (gt_bias) {
2622 __ CmpUleS(FTMP, rhs, lhs);
2623 } else {
2624 __ CmpLeS(FTMP, rhs, lhs);
2625 }
2626 __ Bc1nez(FTMP, label);
2627 break;
2628 default:
2629 LOG(FATAL) << "Unexpected non-floating-point condition";
2630 }
2631 } else {
2632 DCHECK_EQ(type, Primitive::kPrimDouble);
2633 switch (cond) {
2634 case kCondEQ:
2635 __ CmpEqD(FTMP, lhs, rhs);
2636 __ Bc1nez(FTMP, label);
2637 break;
2638 case kCondNE:
2639 __ CmpEqD(FTMP, lhs, rhs);
2640 __ Bc1eqz(FTMP, label);
2641 break;
2642 case kCondLT:
2643 if (gt_bias) {
2644 __ CmpLtD(FTMP, lhs, rhs);
2645 } else {
2646 __ CmpUltD(FTMP, lhs, rhs);
2647 }
2648 __ Bc1nez(FTMP, label);
2649 break;
2650 case kCondLE:
2651 if (gt_bias) {
2652 __ CmpLeD(FTMP, lhs, rhs);
2653 } else {
2654 __ CmpUleD(FTMP, lhs, rhs);
2655 }
2656 __ Bc1nez(FTMP, label);
2657 break;
2658 case kCondGT:
2659 if (gt_bias) {
2660 __ CmpUltD(FTMP, rhs, lhs);
2661 } else {
2662 __ CmpLtD(FTMP, rhs, lhs);
2663 }
2664 __ Bc1nez(FTMP, label);
2665 break;
2666 case kCondGE:
2667 if (gt_bias) {
2668 __ CmpUleD(FTMP, rhs, lhs);
2669 } else {
2670 __ CmpLeD(FTMP, rhs, lhs);
2671 }
2672 __ Bc1nez(FTMP, label);
2673 break;
2674 default:
2675 LOG(FATAL) << "Unexpected non-floating-point condition";
2676 }
2677 }
2678}
2679
Alexey Frunze4dda3372015-06-01 18:31:49 -07002680void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002681 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002682 Mips64Label* true_target,
2683 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002684 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002685
David Brazdil0debae72015-11-12 18:37:00 +00002686 if (true_target == nullptr && false_target == nullptr) {
2687 // Nothing to do. The code always falls through.
2688 return;
2689 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002690 // Constant condition, statically compared against "true" (integer value 1).
2691 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002692 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002693 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002694 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002696 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002697 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002698 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002699 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002700 }
David Brazdil0debae72015-11-12 18:37:00 +00002701 return;
2702 }
2703
2704 // The following code generates these patterns:
2705 // (1) true_target == nullptr && false_target != nullptr
2706 // - opposite condition true => branch to false_target
2707 // (2) true_target != nullptr && false_target == nullptr
2708 // - condition true => branch to true_target
2709 // (3) true_target != nullptr && false_target != nullptr
2710 // - condition true => branch to true_target
2711 // - branch to false_target
2712 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002713 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002714 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002716 if (true_target == nullptr) {
2717 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2718 } else {
2719 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721 } else {
2722 // The condition instruction has not been materialized, use its inputs as
2723 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002724 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002725 Primitive::Type type = condition->InputAt(0)->GetType();
2726 LocationSummary* locations = cond->GetLocations();
2727 IfCondition if_cond = condition->GetCondition();
2728 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002729
David Brazdil0debae72015-11-12 18:37:00 +00002730 if (true_target == nullptr) {
2731 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002732 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002733 }
2734
Alexey Frunze299a9392015-12-08 16:08:02 -08002735 switch (type) {
2736 default:
2737 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2738 break;
2739 case Primitive::kPrimLong:
2740 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2741 break;
2742 case Primitive::kPrimFloat:
2743 case Primitive::kPrimDouble:
2744 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2745 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 }
2747 }
David Brazdil0debae72015-11-12 18:37:00 +00002748
2749 // If neither branch falls through (case 3), the conditional branch to `true_target`
2750 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2751 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002752 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753 }
2754}
2755
2756void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002758 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759 locations->SetInAt(0, Location::RequiresRegister());
2760 }
2761}
2762
2763void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002764 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2765 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002766 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002767 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002768 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002769 nullptr : codegen_->GetLabelOf(false_successor);
2770 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002771}
2772
2773void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2774 LocationSummary* locations = new (GetGraph()->GetArena())
2775 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002776 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002777 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002778 locations->SetInAt(0, Location::RequiresRegister());
2779 }
2780}
2781
2782void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002783 SlowPathCodeMIPS64* slow_path =
2784 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002785 GenerateTestAndBranch(deoptimize,
2786 /* condition_input_index */ 0,
2787 slow_path->GetEntryLabel(),
2788 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002789}
2790
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002791void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2792 LocationSummary* locations = new (GetGraph()->GetArena())
2793 LocationSummary(flag, LocationSummary::kNoCall);
2794 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07002795}
2796
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002797void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2798 __ LoadFromOffset(kLoadWord,
2799 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
2800 SP,
2801 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07002802}
2803
David Brazdil74eb1b22015-12-14 11:44:01 +00002804void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2806 if (Primitive::IsFloatingPointType(select->GetType())) {
2807 locations->SetInAt(0, Location::RequiresFpuRegister());
2808 locations->SetInAt(1, Location::RequiresFpuRegister());
2809 } else {
2810 locations->SetInAt(0, Location::RequiresRegister());
2811 locations->SetInAt(1, Location::RequiresRegister());
2812 }
2813 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2814 locations->SetInAt(2, Location::RequiresRegister());
2815 }
2816 locations->SetOut(Location::SameAsFirstInput());
2817}
2818
2819void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2820 LocationSummary* locations = select->GetLocations();
2821 Mips64Label false_target;
2822 GenerateTestAndBranch(select,
2823 /* condition_input_index */ 2,
2824 /* true_target */ nullptr,
2825 &false_target);
2826 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2827 __ Bind(&false_target);
2828}
2829
David Srbecky0cf44932015-12-09 14:09:59 +00002830void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2831 new (GetGraph()->GetArena()) LocationSummary(info);
2832}
2833
David Srbeckyd28f4a02016-03-14 17:14:24 +00002834void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2835 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002836}
2837
2838void CodeGeneratorMIPS64::GenerateNop() {
2839 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002840}
2841
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2843 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2844 LocationSummary* locations =
2845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2846 locations->SetInAt(0, Location::RequiresRegister());
2847 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2848 locations->SetOut(Location::RequiresFpuRegister());
2849 } else {
2850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2851 }
2852}
2853
2854void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2855 const FieldInfo& field_info) {
2856 Primitive::Type type = field_info.GetFieldType();
2857 LocationSummary* locations = instruction->GetLocations();
2858 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2859 LoadOperandType load_type = kLoadUnsignedByte;
2860 switch (type) {
2861 case Primitive::kPrimBoolean:
2862 load_type = kLoadUnsignedByte;
2863 break;
2864 case Primitive::kPrimByte:
2865 load_type = kLoadSignedByte;
2866 break;
2867 case Primitive::kPrimShort:
2868 load_type = kLoadSignedHalfword;
2869 break;
2870 case Primitive::kPrimChar:
2871 load_type = kLoadUnsignedHalfword;
2872 break;
2873 case Primitive::kPrimInt:
2874 case Primitive::kPrimFloat:
2875 load_type = kLoadWord;
2876 break;
2877 case Primitive::kPrimLong:
2878 case Primitive::kPrimDouble:
2879 load_type = kLoadDoubleword;
2880 break;
2881 case Primitive::kPrimNot:
2882 load_type = kLoadUnsignedWord;
2883 break;
2884 case Primitive::kPrimVoid:
2885 LOG(FATAL) << "Unreachable type " << type;
2886 UNREACHABLE();
2887 }
2888 if (!Primitive::IsFloatingPointType(type)) {
2889 DCHECK(locations->Out().IsRegister());
2890 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2891 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2892 } else {
2893 DCHECK(locations->Out().IsFpuRegister());
2894 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2895 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2896 }
2897
2898 codegen_->MaybeRecordImplicitNullCheck(instruction);
2899 // TODO: memory barrier?
2900}
2901
2902void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2903 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2904 LocationSummary* locations =
2905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2906 locations->SetInAt(0, Location::RequiresRegister());
2907 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2908 locations->SetInAt(1, Location::RequiresFpuRegister());
2909 } else {
2910 locations->SetInAt(1, Location::RequiresRegister());
2911 }
2912}
2913
2914void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002915 const FieldInfo& field_info,
2916 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002917 Primitive::Type type = field_info.GetFieldType();
2918 LocationSummary* locations = instruction->GetLocations();
2919 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2920 StoreOperandType store_type = kStoreByte;
2921 switch (type) {
2922 case Primitive::kPrimBoolean:
2923 case Primitive::kPrimByte:
2924 store_type = kStoreByte;
2925 break;
2926 case Primitive::kPrimShort:
2927 case Primitive::kPrimChar:
2928 store_type = kStoreHalfword;
2929 break;
2930 case Primitive::kPrimInt:
2931 case Primitive::kPrimFloat:
2932 case Primitive::kPrimNot:
2933 store_type = kStoreWord;
2934 break;
2935 case Primitive::kPrimLong:
2936 case Primitive::kPrimDouble:
2937 store_type = kStoreDoubleword;
2938 break;
2939 case Primitive::kPrimVoid:
2940 LOG(FATAL) << "Unreachable type " << type;
2941 UNREACHABLE();
2942 }
2943 if (!Primitive::IsFloatingPointType(type)) {
2944 DCHECK(locations->InAt(1).IsRegister());
2945 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2946 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2947 } else {
2948 DCHECK(locations->InAt(1).IsFpuRegister());
2949 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2950 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2951 }
2952
2953 codegen_->MaybeRecordImplicitNullCheck(instruction);
2954 // TODO: memory barriers?
2955 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2956 DCHECK(locations->InAt(1).IsRegister());
2957 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002958 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002959 }
2960}
2961
2962void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2963 HandleFieldGet(instruction, instruction->GetFieldInfo());
2964}
2965
2966void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2967 HandleFieldGet(instruction, instruction->GetFieldInfo());
2968}
2969
2970void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2971 HandleFieldSet(instruction, instruction->GetFieldInfo());
2972}
2973
2974void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002975 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002976}
2977
Alexey Frunzef63f5692016-12-13 17:43:11 -08002978void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
2979 HInstruction* instruction ATTRIBUTE_UNUSED,
2980 Location root,
2981 GpuRegister obj,
2982 uint32_t offset) {
2983 // When handling HLoadClass::LoadKind::kDexCachePcRelative, the caller calls
2984 // EmitPcRelativeAddressPlaceholderHigh() and then GenerateGcRootFieldLoad().
2985 // The relative patcher expects the two methods to emit the following patchable
2986 // sequence of instructions in this case:
2987 // auipc reg1, 0x1234 // 0x1234 is a placeholder for offset_high.
2988 // lwu reg2, 0x5678(reg1) // 0x5678 is a placeholder for offset_low.
2989 // TODO: Adjust GenerateGcRootFieldLoad() and its caller when this method is
2990 // extended (e.g. for read barriers) so as not to break the relative patcher.
2991 GpuRegister root_reg = root.AsRegister<GpuRegister>();
2992 if (kEmitCompilerReadBarrier) {
2993 UNIMPLEMENTED(FATAL) << "for read barrier";
2994 } else {
2995 // Plain GC root load with no read barrier.
2996 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
2997 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
2998 // Note that GC roots are not affected by heap poisoning, thus we
2999 // do not have to unpoison `root_reg` here.
3000 }
3001}
3002
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3004 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003005 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3007 locations->SetInAt(0, Location::RequiresRegister());
3008 locations->SetInAt(1, Location::RequiresRegister());
3009 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003010 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3012}
3013
3014void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3015 LocationSummary* locations = instruction->GetLocations();
3016 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3017 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3018 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3019
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003020 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021
3022 // Return 0 if `obj` is null.
3023 // TODO: Avoid this check if we know `obj` is not null.
3024 __ Move(out, ZERO);
3025 __ Beqzc(obj, &done);
3026
3027 // Compare the class of `obj` with `cls`.
3028 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003029 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003030 // Classes must be equal for the instanceof to succeed.
3031 __ Xor(out, out, cls);
3032 __ Sltiu(out, out, 1);
3033 } else {
3034 // If the classes are not equal, we go into a slow path.
3035 DCHECK(locations->OnlyCallsOnSlowPath());
3036 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003037 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038 codegen_->AddSlowPath(slow_path);
3039 __ Bnec(out, cls, slow_path->GetEntryLabel());
3040 __ LoadConst32(out, 1);
3041 __ Bind(slow_path->GetExitLabel());
3042 }
3043
3044 __ Bind(&done);
3045}
3046
3047void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3049 locations->SetOut(Location::ConstantLocation(constant));
3050}
3051
3052void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3053 // Will be generated at use site.
3054}
3055
3056void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3058 locations->SetOut(Location::ConstantLocation(constant));
3059}
3060
3061void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3062 // Will be generated at use site.
3063}
3064
Calin Juravle175dc732015-08-25 15:42:32 +01003065void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3066 // The trampoline uses the same calling convention as dex calling conventions,
3067 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3068 // the method_idx.
3069 HandleInvoke(invoke);
3070}
3071
3072void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3073 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3074}
3075
Alexey Frunze4dda3372015-06-01 18:31:49 -07003076void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3077 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3078 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3079}
3080
3081void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3082 HandleInvoke(invoke);
3083 // The register T0 is required to be used for the hidden argument in
3084 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3085 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3086}
3087
3088void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3089 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3090 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 Location receiver = invoke->GetLocations()->InAt(0);
3092 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003093 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094
3095 // Set the hidden argument.
3096 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3097 invoke->GetDexMethodIndex());
3098
3099 // temp = object->GetClass();
3100 if (receiver.IsStackSlot()) {
3101 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3102 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3103 } else {
3104 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3105 }
3106 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003107 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3108 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3109 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003110 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003111 // temp = temp->GetImtEntryAt(method_offset);
3112 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3113 // T9 = temp->GetEntryPoint();
3114 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3115 // T9();
3116 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003117 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003118 DCHECK(!codegen_->IsLeafMethod());
3119 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3120}
3121
3122void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003123 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3124 if (intrinsic.TryDispatch(invoke)) {
3125 return;
3126 }
3127
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128 HandleInvoke(invoke);
3129}
3130
3131void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003132 // Explicit clinit checks triggered by static invokes must have been pruned by
3133 // art::PrepareForRegisterAllocation.
3134 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003135
Chris Larsen3039e382015-08-26 07:54:08 -07003136 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3137 if (intrinsic.TryDispatch(invoke)) {
3138 return;
3139 }
3140
Alexey Frunze4dda3372015-06-01 18:31:49 -07003141 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142}
3143
Chris Larsen3039e382015-08-26 07:54:08 -07003144static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003145 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003146 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3147 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003148 return true;
3149 }
3150 return false;
3151}
3152
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003153HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003154 HLoadString::LoadKind desired_string_load_kind) {
3155 if (kEmitCompilerReadBarrier) {
3156 UNIMPLEMENTED(FATAL) << "for read barrier";
3157 }
3158 bool fallback_load = false;
3159 switch (desired_string_load_kind) {
3160 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3161 DCHECK(!GetCompilerOptions().GetCompilePic());
3162 break;
3163 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3164 DCHECK(GetCompilerOptions().GetCompilePic());
3165 break;
3166 case HLoadString::LoadKind::kBootImageAddress:
3167 break;
3168 case HLoadString::LoadKind::kBssEntry:
3169 DCHECK(!Runtime::Current()->UseJitCompilation());
3170 break;
3171 case HLoadString::LoadKind::kDexCacheViaMethod:
3172 break;
3173 case HLoadString::LoadKind::kJitTableAddress:
3174 DCHECK(Runtime::Current()->UseJitCompilation());
3175 // TODO: implement.
3176 fallback_load = true;
3177 break;
3178 }
3179 if (fallback_load) {
3180 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3181 }
3182 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003183}
3184
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003185HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3186 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003187 if (kEmitCompilerReadBarrier) {
3188 UNIMPLEMENTED(FATAL) << "for read barrier";
3189 }
3190 bool fallback_load = false;
3191 switch (desired_class_load_kind) {
3192 case HLoadClass::LoadKind::kReferrersClass:
3193 break;
3194 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3195 DCHECK(!GetCompilerOptions().GetCompilePic());
3196 break;
3197 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3198 DCHECK(GetCompilerOptions().GetCompilePic());
3199 break;
3200 case HLoadClass::LoadKind::kBootImageAddress:
3201 break;
3202 case HLoadClass::LoadKind::kJitTableAddress:
3203 DCHECK(Runtime::Current()->UseJitCompilation());
3204 // TODO: implement.
3205 fallback_load = true;
3206 break;
3207 case HLoadClass::LoadKind::kDexCachePcRelative:
3208 DCHECK(!Runtime::Current()->UseJitCompilation());
3209 break;
3210 case HLoadClass::LoadKind::kDexCacheViaMethod:
3211 break;
3212 }
3213 if (fallback_load) {
3214 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3215 }
3216 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003217}
3218
Vladimir Markodc151b22015-10-15 18:02:30 +01003219HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3220 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003221 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003222 // On MIPS64 we support all dispatch types.
3223 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003224}
3225
Alexey Frunze4dda3372015-06-01 18:31:49 -07003226void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3227 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003228 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003229 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3230 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3231
Alexey Frunze19f6c692016-11-30 19:19:55 -08003232 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003233 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003234 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003235 uint32_t offset =
3236 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003237 __ LoadFromOffset(kLoadDoubleword,
3238 temp.AsRegister<GpuRegister>(),
3239 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003240 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003241 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003242 }
Vladimir Marko58155012015-08-19 12:49:41 +00003243 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003244 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003245 break;
3246 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003247 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3248 kLoadDoubleword,
3249 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003250 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003251 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3252 uint32_t offset = invoke->GetDexCacheArrayOffset();
3253 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3254 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset);
3255 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3256 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3257 break;
3258 }
Vladimir Marko58155012015-08-19 12:49:41 +00003259 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003260 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003261 GpuRegister reg = temp.AsRegister<GpuRegister>();
3262 GpuRegister method_reg;
3263 if (current_method.IsRegister()) {
3264 method_reg = current_method.AsRegister<GpuRegister>();
3265 } else {
3266 // TODO: use the appropriate DCHECK() here if possible.
3267 // DCHECK(invoke->GetLocations()->Intrinsified());
3268 DCHECK(!current_method.IsValid());
3269 method_reg = reg;
3270 __ Ld(reg, SP, kCurrentMethodStackOffset);
3271 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003272
Vladimir Marko58155012015-08-19 12:49:41 +00003273 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003274 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003275 reg,
3276 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003277 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003278 // temp = temp[index_in_cache];
3279 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3280 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003281 __ LoadFromOffset(kLoadDoubleword,
3282 reg,
3283 reg,
3284 CodeGenerator::GetCachePointerOffset(index_in_cache));
3285 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003286 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003287 }
3288
Alexey Frunze19f6c692016-11-30 19:19:55 -08003289 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003290 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003291 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003292 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003293 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3294 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3295 __ LoadFromOffset(kLoadDoubleword,
3296 T9,
3297 callee_method.AsRegister<GpuRegister>(),
3298 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003299 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003300 // T9()
3301 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003302 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003303 break;
3304 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003305 DCHECK(!IsLeafMethod());
3306}
3307
3308void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003309 // Explicit clinit checks triggered by static invokes must have been pruned by
3310 // art::PrepareForRegisterAllocation.
3311 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003312
3313 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3314 return;
3315 }
3316
3317 LocationSummary* locations = invoke->GetLocations();
3318 codegen_->GenerateStaticOrDirectCall(invoke,
3319 locations->HasTemps()
3320 ? locations->GetTemp(0)
3321 : Location::NoLocation());
3322 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3323}
3324
Alexey Frunze53afca12015-11-05 16:34:23 -08003325void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003326 // Use the calling convention instead of the location of the receiver, as
3327 // intrinsics may have put the receiver in a different register. In the intrinsics
3328 // slow path, the arguments have been moved to the right place, so here we are
3329 // guaranteed that the receiver is the first register of the calling convention.
3330 InvokeDexCallingConvention calling_convention;
3331 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3332
Alexey Frunze53afca12015-11-05 16:34:23 -08003333 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003334 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3335 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3336 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003337 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003338
3339 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003340 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003341 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003342 // temp = temp->GetMethodAt(method_offset);
3343 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3344 // T9 = temp->GetEntryPoint();
3345 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3346 // T9();
3347 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003348 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003349}
3350
3351void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3352 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3353 return;
3354 }
3355
3356 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003357 DCHECK(!codegen_->IsLeafMethod());
3358 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3359}
3360
3361void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003362 if (cls->NeedsAccessCheck()) {
3363 InvokeRuntimeCallingConvention calling_convention;
3364 CodeGenerator::CreateLoadClassLocationSummary(
3365 cls,
3366 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3367 calling_convention.GetReturnLocation(Primitive::kPrimNot),
3368 /* code_generator_supports_read_barrier */ false);
3369 return;
3370 }
3371
3372 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3373 ? LocationSummary::kCallOnSlowPath
3374 : LocationSummary::kNoCall;
3375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3376 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3377 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3378 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3379 locations->SetInAt(0, Location::RequiresRegister());
3380 }
3381 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003382}
3383
3384void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3385 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003386 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003387 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003388 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003389 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003390 return;
3391 }
3392
Alexey Frunzef63f5692016-12-13 17:43:11 -08003393 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3394 Location out_loc = locations->Out();
3395 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3396 GpuRegister current_method_reg = ZERO;
3397 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3398 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3399 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3400 }
3401
3402 bool generate_null_check = false;
3403 switch (load_kind) {
3404 case HLoadClass::LoadKind::kReferrersClass:
3405 DCHECK(!cls->CanCallRuntime());
3406 DCHECK(!cls->MustGenerateClinitCheck());
3407 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3408 GenerateGcRootFieldLoad(cls,
3409 out_loc,
3410 current_method_reg,
3411 ArtMethod::DeclaringClassOffset().Int32Value());
3412 break;
3413 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3414 DCHECK(!kEmitCompilerReadBarrier);
3415 __ LoadLiteral(out,
3416 kLoadUnsignedWord,
3417 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3418 cls->GetTypeIndex()));
3419 break;
3420 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
3421 DCHECK(!kEmitCompilerReadBarrier);
3422 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3423 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3424 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3425 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3426 break;
3427 }
3428 case HLoadClass::LoadKind::kBootImageAddress: {
3429 DCHECK(!kEmitCompilerReadBarrier);
3430 DCHECK_NE(cls->GetAddress(), 0u);
3431 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
3432 __ LoadLiteral(out,
3433 kLoadUnsignedWord,
3434 codegen_->DeduplicateBootImageAddressLiteral(address));
3435 break;
3436 }
3437 case HLoadClass::LoadKind::kJitTableAddress: {
3438 LOG(FATAL) << "Unimplemented";
3439 break;
3440 }
3441 case HLoadClass::LoadKind::kDexCachePcRelative: {
3442 uint32_t element_offset = cls->GetDexCacheElementOffset();
3443 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3444 codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), element_offset);
3445 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3446 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
3447 GenerateGcRootFieldLoad(cls, out_loc, AT, /* placeholder */ 0x5678);
3448 generate_null_check = !cls->IsInDexCache();
3449 break;
3450 }
3451 case HLoadClass::LoadKind::kDexCacheViaMethod: {
3452 // /* GcRoot<mirror::Class>[] */ out =
3453 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
3454 __ LoadFromOffset(kLoadDoubleword,
3455 out,
3456 current_method_reg,
3457 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
3458 // /* GcRoot<mirror::Class> */ out = out[type_index]
3459 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
3460 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
3461 generate_null_check = !cls->IsInDexCache();
3462 }
3463 }
3464
3465 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3466 DCHECK(cls->CanCallRuntime());
3467 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3468 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3469 codegen_->AddSlowPath(slow_path);
3470 if (generate_null_check) {
3471 __ Beqzc(out, slow_path->GetEntryLabel());
3472 }
3473 if (cls->MustGenerateClinitCheck()) {
3474 GenerateClassInitializationCheck(slow_path, out);
3475 } else {
3476 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477 }
3478 }
3479}
3480
David Brazdilcb1c0552015-08-04 16:22:25 +01003481static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003482 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003483}
3484
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3486 LocationSummary* locations =
3487 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3488 locations->SetOut(Location::RequiresRegister());
3489}
3490
3491void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3492 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003493 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3494}
3495
3496void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3497 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3498}
3499
3500void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3501 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003502}
3503
Alexey Frunze4dda3372015-06-01 18:31:49 -07003504void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003505 HLoadString::LoadKind load_kind = load->GetLoadKind();
3506 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003508 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3509 InvokeRuntimeCallingConvention calling_convention;
3510 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3511 } else {
3512 locations->SetOut(Location::RequiresRegister());
3513 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514}
3515
3516void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003517 HLoadString::LoadKind load_kind = load->GetLoadKind();
3518 LocationSummary* locations = load->GetLocations();
3519 Location out_loc = locations->Out();
3520 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3521
3522 switch (load_kind) {
3523 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3524 __ LoadLiteral(out,
3525 kLoadUnsignedWord,
3526 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3527 load->GetStringIndex()));
3528 return; // No dex cache slow path.
3529 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3530 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3531 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3532 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
3533 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3534 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3535 return; // No dex cache slow path.
3536 }
3537 case HLoadString::LoadKind::kBootImageAddress: {
3538 DCHECK_NE(load->GetAddress(), 0u);
3539 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
3540 __ LoadLiteral(out,
3541 kLoadUnsignedWord,
3542 codegen_->DeduplicateBootImageAddressLiteral(address));
3543 return; // No dex cache slow path.
3544 }
3545 case HLoadString::LoadKind::kBssEntry: {
3546 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3547 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3548 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
3549 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3550 __ Lwu(out, AT, /* placeholder */ 0x5678);
3551 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3552 codegen_->AddSlowPath(slow_path);
3553 __ Beqzc(out, slow_path->GetEntryLabel());
3554 __ Bind(slow_path->GetExitLabel());
3555 return;
3556 }
3557 default:
3558 break;
3559 }
3560
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003561 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003562 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3563 InvokeRuntimeCallingConvention calling_convention;
3564 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3565 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3566 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003567}
3568
Alexey Frunze4dda3372015-06-01 18:31:49 -07003569void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3571 locations->SetOut(Location::ConstantLocation(constant));
3572}
3573
3574void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3575 // Will be generated at use site.
3576}
3577
3578void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3579 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003581 InvokeRuntimeCallingConvention calling_convention;
3582 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3583}
3584
3585void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003586 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003588 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003589 if (instruction->IsEnter()) {
3590 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3591 } else {
3592 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003594}
3595
3596void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3599 switch (mul->GetResultType()) {
3600 case Primitive::kPrimInt:
3601 case Primitive::kPrimLong:
3602 locations->SetInAt(0, Location::RequiresRegister());
3603 locations->SetInAt(1, Location::RequiresRegister());
3604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3605 break;
3606
3607 case Primitive::kPrimFloat:
3608 case Primitive::kPrimDouble:
3609 locations->SetInAt(0, Location::RequiresFpuRegister());
3610 locations->SetInAt(1, Location::RequiresFpuRegister());
3611 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3612 break;
3613
3614 default:
3615 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3616 }
3617}
3618
3619void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3620 Primitive::Type type = instruction->GetType();
3621 LocationSummary* locations = instruction->GetLocations();
3622
3623 switch (type) {
3624 case Primitive::kPrimInt:
3625 case Primitive::kPrimLong: {
3626 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3627 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3628 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3629 if (type == Primitive::kPrimInt)
3630 __ MulR6(dst, lhs, rhs);
3631 else
3632 __ Dmul(dst, lhs, rhs);
3633 break;
3634 }
3635 case Primitive::kPrimFloat:
3636 case Primitive::kPrimDouble: {
3637 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3638 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3639 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3640 if (type == Primitive::kPrimFloat)
3641 __ MulS(dst, lhs, rhs);
3642 else
3643 __ MulD(dst, lhs, rhs);
3644 break;
3645 }
3646 default:
3647 LOG(FATAL) << "Unexpected mul type " << type;
3648 }
3649}
3650
3651void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3654 switch (neg->GetResultType()) {
3655 case Primitive::kPrimInt:
3656 case Primitive::kPrimLong:
3657 locations->SetInAt(0, Location::RequiresRegister());
3658 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3659 break;
3660
3661 case Primitive::kPrimFloat:
3662 case Primitive::kPrimDouble:
3663 locations->SetInAt(0, Location::RequiresFpuRegister());
3664 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3665 break;
3666
3667 default:
3668 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3669 }
3670}
3671
3672void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3673 Primitive::Type type = instruction->GetType();
3674 LocationSummary* locations = instruction->GetLocations();
3675
3676 switch (type) {
3677 case Primitive::kPrimInt:
3678 case Primitive::kPrimLong: {
3679 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3680 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3681 if (type == Primitive::kPrimInt)
3682 __ Subu(dst, ZERO, src);
3683 else
3684 __ Dsubu(dst, ZERO, src);
3685 break;
3686 }
3687 case Primitive::kPrimFloat:
3688 case Primitive::kPrimDouble: {
3689 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3690 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3691 if (type == Primitive::kPrimFloat)
3692 __ NegS(dst, src);
3693 else
3694 __ NegD(dst, src);
3695 break;
3696 }
3697 default:
3698 LOG(FATAL) << "Unexpected neg type " << type;
3699 }
3700}
3701
3702void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3703 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003705 InvokeRuntimeCallingConvention calling_convention;
3706 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3707 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3709 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3710}
3711
3712void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3713 LocationSummary* locations = instruction->GetLocations();
3714 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08003715 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(),
3716 instruction->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003717 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003718 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3719}
3720
3721void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3722 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003724 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003725 if (instruction->IsStringAlloc()) {
3726 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3727 } else {
3728 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3729 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3730 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003731 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3732}
3733
3734void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003735 if (instruction->IsStringAlloc()) {
3736 // String is allocated through StringFactory. Call NewEmptyString entry point.
3737 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003738 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003739 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003740 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3741 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3742 __ Jalr(T9);
3743 __ Nop();
3744 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3745 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003746 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003747 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3748 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003749}
3750
3751void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3753 locations->SetInAt(0, Location::RequiresRegister());
3754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3755}
3756
3757void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3758 Primitive::Type type = instruction->GetType();
3759 LocationSummary* locations = instruction->GetLocations();
3760
3761 switch (type) {
3762 case Primitive::kPrimInt:
3763 case Primitive::kPrimLong: {
3764 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3765 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3766 __ Nor(dst, src, ZERO);
3767 break;
3768 }
3769
3770 default:
3771 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3772 }
3773}
3774
3775void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3777 locations->SetInAt(0, Location::RequiresRegister());
3778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3779}
3780
3781void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3782 LocationSummary* locations = instruction->GetLocations();
3783 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3784 locations->InAt(0).AsRegister<GpuRegister>(),
3785 1);
3786}
3787
3788void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003789 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3790 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003791}
3792
Calin Juravle2ae48182016-03-16 14:05:09 +00003793void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3794 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003795 return;
3796 }
3797 Location obj = instruction->GetLocations()->InAt(0);
3798
3799 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003800 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003801}
3802
Calin Juravle2ae48182016-03-16 14:05:09 +00003803void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003804 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003805 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003806
3807 Location obj = instruction->GetLocations()->InAt(0);
3808
3809 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3810}
3811
3812void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003813 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003814}
3815
3816void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3817 HandleBinaryOp(instruction);
3818}
3819
3820void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3821 HandleBinaryOp(instruction);
3822}
3823
3824void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3825 LOG(FATAL) << "Unreachable";
3826}
3827
3828void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3829 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3830}
3831
3832void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3833 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3834 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3835 if (location.IsStackSlot()) {
3836 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3837 } else if (location.IsDoubleStackSlot()) {
3838 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3839 }
3840 locations->SetOut(location);
3841}
3842
3843void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3844 ATTRIBUTE_UNUSED) {
3845 // Nothing to do, the parameter is already at its location.
3846}
3847
3848void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3849 LocationSummary* locations =
3850 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3851 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3852}
3853
3854void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3855 ATTRIBUTE_UNUSED) {
3856 // Nothing to do, the method is already at its location.
3857}
3858
3859void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003861 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003862 locations->SetInAt(i, Location::Any());
3863 }
3864 locations->SetOut(Location::Any());
3865}
3866
3867void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3868 LOG(FATAL) << "Unreachable";
3869}
3870
3871void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3872 Primitive::Type type = rem->GetResultType();
3873 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003874 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3875 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3877
3878 switch (type) {
3879 case Primitive::kPrimInt:
3880 case Primitive::kPrimLong:
3881 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003882 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3884 break;
3885
3886 case Primitive::kPrimFloat:
3887 case Primitive::kPrimDouble: {
3888 InvokeRuntimeCallingConvention calling_convention;
3889 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3890 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3891 locations->SetOut(calling_convention.GetReturnLocation(type));
3892 break;
3893 }
3894
3895 default:
3896 LOG(FATAL) << "Unexpected rem type " << type;
3897 }
3898}
3899
3900void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3901 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003902
3903 switch (type) {
3904 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003905 case Primitive::kPrimLong:
3906 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003907 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003908
3909 case Primitive::kPrimFloat:
3910 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003911 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3912 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003913 if (type == Primitive::kPrimFloat) {
3914 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3915 } else {
3916 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3917 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003918 break;
3919 }
3920 default:
3921 LOG(FATAL) << "Unexpected rem type " << type;
3922 }
3923}
3924
3925void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3926 memory_barrier->SetLocations(nullptr);
3927}
3928
3929void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3930 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3931}
3932
3933void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3934 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3935 Primitive::Type return_type = ret->InputAt(0)->GetType();
3936 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3937}
3938
3939void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3940 codegen_->GenerateFrameExit();
3941}
3942
3943void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3944 ret->SetLocations(nullptr);
3945}
3946
3947void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3948 codegen_->GenerateFrameExit();
3949}
3950
Alexey Frunze92d90602015-12-18 18:16:36 -08003951void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3952 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003953}
3954
Alexey Frunze92d90602015-12-18 18:16:36 -08003955void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3956 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003957}
3958
Alexey Frunze4dda3372015-06-01 18:31:49 -07003959void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3960 HandleShift(shl);
3961}
3962
3963void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3964 HandleShift(shl);
3965}
3966
3967void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3968 HandleShift(shr);
3969}
3970
3971void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3972 HandleShift(shr);
3973}
3974
Alexey Frunze4dda3372015-06-01 18:31:49 -07003975void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3976 HandleBinaryOp(instruction);
3977}
3978
3979void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3980 HandleBinaryOp(instruction);
3981}
3982
3983void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3984 HandleFieldGet(instruction, instruction->GetFieldInfo());
3985}
3986
3987void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3988 HandleFieldGet(instruction, instruction->GetFieldInfo());
3989}
3990
3991void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3992 HandleFieldSet(instruction, instruction->GetFieldInfo());
3993}
3994
3995void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003996 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003997}
3998
Calin Juravlee460d1d2015-09-29 04:52:17 +01003999void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4000 HUnresolvedInstanceFieldGet* instruction) {
4001 FieldAccessCallingConventionMIPS64 calling_convention;
4002 codegen_->CreateUnresolvedFieldLocationSummary(
4003 instruction, instruction->GetFieldType(), calling_convention);
4004}
4005
4006void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4007 HUnresolvedInstanceFieldGet* instruction) {
4008 FieldAccessCallingConventionMIPS64 calling_convention;
4009 codegen_->GenerateUnresolvedFieldAccess(instruction,
4010 instruction->GetFieldType(),
4011 instruction->GetFieldIndex(),
4012 instruction->GetDexPc(),
4013 calling_convention);
4014}
4015
4016void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4017 HUnresolvedInstanceFieldSet* instruction) {
4018 FieldAccessCallingConventionMIPS64 calling_convention;
4019 codegen_->CreateUnresolvedFieldLocationSummary(
4020 instruction, instruction->GetFieldType(), calling_convention);
4021}
4022
4023void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4024 HUnresolvedInstanceFieldSet* instruction) {
4025 FieldAccessCallingConventionMIPS64 calling_convention;
4026 codegen_->GenerateUnresolvedFieldAccess(instruction,
4027 instruction->GetFieldType(),
4028 instruction->GetFieldIndex(),
4029 instruction->GetDexPc(),
4030 calling_convention);
4031}
4032
4033void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4034 HUnresolvedStaticFieldGet* instruction) {
4035 FieldAccessCallingConventionMIPS64 calling_convention;
4036 codegen_->CreateUnresolvedFieldLocationSummary(
4037 instruction, instruction->GetFieldType(), calling_convention);
4038}
4039
4040void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4041 HUnresolvedStaticFieldGet* instruction) {
4042 FieldAccessCallingConventionMIPS64 calling_convention;
4043 codegen_->GenerateUnresolvedFieldAccess(instruction,
4044 instruction->GetFieldType(),
4045 instruction->GetFieldIndex(),
4046 instruction->GetDexPc(),
4047 calling_convention);
4048}
4049
4050void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4051 HUnresolvedStaticFieldSet* instruction) {
4052 FieldAccessCallingConventionMIPS64 calling_convention;
4053 codegen_->CreateUnresolvedFieldLocationSummary(
4054 instruction, instruction->GetFieldType(), calling_convention);
4055}
4056
4057void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4058 HUnresolvedStaticFieldSet* instruction) {
4059 FieldAccessCallingConventionMIPS64 calling_convention;
4060 codegen_->GenerateUnresolvedFieldAccess(instruction,
4061 instruction->GetFieldType(),
4062 instruction->GetFieldIndex(),
4063 instruction->GetDexPc(),
4064 calling_convention);
4065}
4066
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004070 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004071}
4072
4073void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4074 HBasicBlock* block = instruction->GetBlock();
4075 if (block->GetLoopInformation() != nullptr) {
4076 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4077 // The back edge will generate the suspend check.
4078 return;
4079 }
4080 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4081 // The goto will generate the suspend check.
4082 return;
4083 }
4084 GenerateSuspendCheck(instruction, nullptr);
4085}
4086
Alexey Frunze4dda3372015-06-01 18:31:49 -07004087void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4088 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004090 InvokeRuntimeCallingConvention calling_convention;
4091 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4092}
4093
4094void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004095 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004096 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4097}
4098
4099void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4100 Primitive::Type input_type = conversion->GetInputType();
4101 Primitive::Type result_type = conversion->GetResultType();
4102 DCHECK_NE(input_type, result_type);
4103
4104 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4105 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4106 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4107 }
4108
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004109 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4110
4111 if (Primitive::IsFloatingPointType(input_type)) {
4112 locations->SetInAt(0, Location::RequiresFpuRegister());
4113 } else {
4114 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115 }
4116
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004117 if (Primitive::IsFloatingPointType(result_type)) {
4118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004119 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004121 }
4122}
4123
4124void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4125 LocationSummary* locations = conversion->GetLocations();
4126 Primitive::Type result_type = conversion->GetResultType();
4127 Primitive::Type input_type = conversion->GetInputType();
4128
4129 DCHECK_NE(input_type, result_type);
4130
4131 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4132 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4133 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4134
4135 switch (result_type) {
4136 case Primitive::kPrimChar:
4137 __ Andi(dst, src, 0xFFFF);
4138 break;
4139 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004140 if (input_type == Primitive::kPrimLong) {
4141 // Type conversion from long to types narrower than int is a result of code
4142 // transformations. To avoid unpredictable results for SEB and SEH, we first
4143 // need to sign-extend the low 32-bit value into bits 32 through 63.
4144 __ Sll(dst, src, 0);
4145 __ Seb(dst, dst);
4146 } else {
4147 __ Seb(dst, src);
4148 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004149 break;
4150 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004151 if (input_type == Primitive::kPrimLong) {
4152 // Type conversion from long to types narrower than int is a result of code
4153 // transformations. To avoid unpredictable results for SEB and SEH, we first
4154 // need to sign-extend the low 32-bit value into bits 32 through 63.
4155 __ Sll(dst, src, 0);
4156 __ Seh(dst, dst);
4157 } else {
4158 __ Seh(dst, src);
4159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004160 break;
4161 case Primitive::kPrimInt:
4162 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004163 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4164 // conversions, except when the input and output registers are the same and we are not
4165 // converting longs to shorter types. In these cases, do nothing.
4166 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4167 __ Sll(dst, src, 0);
4168 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004169 break;
4170
4171 default:
4172 LOG(FATAL) << "Unexpected type conversion from " << input_type
4173 << " to " << result_type;
4174 }
4175 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004176 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4177 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4178 if (input_type == Primitive::kPrimLong) {
4179 __ Dmtc1(src, FTMP);
4180 if (result_type == Primitive::kPrimFloat) {
4181 __ Cvtsl(dst, FTMP);
4182 } else {
4183 __ Cvtdl(dst, FTMP);
4184 }
4185 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186 __ Mtc1(src, FTMP);
4187 if (result_type == Primitive::kPrimFloat) {
4188 __ Cvtsw(dst, FTMP);
4189 } else {
4190 __ Cvtdw(dst, FTMP);
4191 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004192 }
4193 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4194 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004195 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4196 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4197 Mips64Label truncate;
4198 Mips64Label done;
4199
4200 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4201 // value when the input is either a NaN or is outside of the range of the output type
4202 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4203 // the same result.
4204 //
4205 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4206 // value of the output type if the input is outside of the range after the truncation or
4207 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4208 // results. This matches the desired float/double-to-int/long conversion exactly.
4209 //
4210 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4211 //
4212 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4213 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4214 // even though it must be NAN2008=1 on R6.
4215 //
4216 // The code takes care of the different behaviors by first comparing the input to the
4217 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4218 // If the input is greater than or equal to the minimum, it procedes to the truncate
4219 // instruction, which will handle such an input the same way irrespective of NAN2008.
4220 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4221 // in order to return either zero or the minimum value.
4222 //
4223 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4224 // truncate instruction for MIPS64R6.
4225 if (input_type == Primitive::kPrimFloat) {
4226 uint32_t min_val = (result_type == Primitive::kPrimLong)
4227 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4228 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4229 __ LoadConst32(TMP, min_val);
4230 __ Mtc1(TMP, FTMP);
4231 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004232 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004233 uint64_t min_val = (result_type == Primitive::kPrimLong)
4234 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4235 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4236 __ LoadConst64(TMP, min_val);
4237 __ Dmtc1(TMP, FTMP);
4238 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004239 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004240
4241 __ Bc1nez(FTMP, &truncate);
4242
4243 if (input_type == Primitive::kPrimFloat) {
4244 __ CmpEqS(FTMP, src, src);
4245 } else {
4246 __ CmpEqD(FTMP, src, src);
4247 }
4248 if (result_type == Primitive::kPrimLong) {
4249 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4250 } else {
4251 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4252 }
4253 __ Mfc1(TMP, FTMP);
4254 __ And(dst, dst, TMP);
4255
4256 __ Bc(&done);
4257
4258 __ Bind(&truncate);
4259
4260 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004261 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004262 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004263 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004264 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004265 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004266 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004267 } else {
4268 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004269 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004270 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004271 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004272 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004273 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004274 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004275
4276 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004277 } else if (Primitive::IsFloatingPointType(result_type) &&
4278 Primitive::IsFloatingPointType(input_type)) {
4279 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4280 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4281 if (result_type == Primitive::kPrimFloat) {
4282 __ Cvtsd(dst, src);
4283 } else {
4284 __ Cvtds(dst, src);
4285 }
4286 } else {
4287 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4288 << " to " << result_type;
4289 }
4290}
4291
4292void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4293 HandleShift(ushr);
4294}
4295
4296void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4297 HandleShift(ushr);
4298}
4299
4300void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4301 HandleBinaryOp(instruction);
4302}
4303
4304void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4305 HandleBinaryOp(instruction);
4306}
4307
4308void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4309 // Nothing to do, this should be removed during prepare for register allocator.
4310 LOG(FATAL) << "Unreachable";
4311}
4312
4313void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4314 // Nothing to do, this should be removed during prepare for register allocator.
4315 LOG(FATAL) << "Unreachable";
4316}
4317
4318void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004319 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004320}
4321
4322void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004323 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004324}
4325
4326void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004327 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004328}
4329
4330void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004331 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004332}
4333
4334void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004335 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004336}
4337
4338void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004339 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004340}
4341
4342void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004343 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004344}
4345
4346void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004347 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348}
4349
4350void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004351 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004352}
4353
4354void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004355 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004356}
4357
4358void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004359 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004360}
4361
4362void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004363 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004364}
4365
Aart Bike9f37602015-10-09 11:15:55 -07004366void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004367 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004368}
4369
4370void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004371 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004372}
4373
4374void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004375 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004376}
4377
4378void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004379 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004380}
4381
4382void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004383 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004384}
4385
4386void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004387 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004388}
4389
4390void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004391 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004392}
4393
4394void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004395 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004396}
4397
Mark Mendellfe57faa2015-09-18 09:26:15 -04004398// Simple implementation of packed switch - generate cascaded compare/jumps.
4399void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4400 LocationSummary* locations =
4401 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4402 locations->SetInAt(0, Location::RequiresRegister());
4403}
4404
4405void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4406 int32_t lower_bound = switch_instr->GetStartValue();
4407 int32_t num_entries = switch_instr->GetNumEntries();
4408 LocationSummary* locations = switch_instr->GetLocations();
4409 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4410 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4411
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004412 // Create a set of compare/jumps.
4413 GpuRegister temp_reg = TMP;
4414 if (IsInt<16>(-lower_bound)) {
4415 __ Addiu(temp_reg, value_reg, -lower_bound);
4416 } else {
4417 __ LoadConst32(AT, -lower_bound);
4418 __ Addu(temp_reg, value_reg, AT);
4419 }
4420 // Jump to default if index is negative
4421 // Note: We don't check the case that index is positive while value < lower_bound, because in
4422 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4423 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4424
Mark Mendellfe57faa2015-09-18 09:26:15 -04004425 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004426 // Jump to successors[0] if value == lower_bound.
4427 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4428 int32_t last_index = 0;
4429 for (; num_entries - last_index > 2; last_index += 2) {
4430 __ Addiu(temp_reg, temp_reg, -2);
4431 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4432 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4433 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4434 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4435 }
4436 if (num_entries - last_index == 2) {
4437 // The last missing case_value.
4438 __ Addiu(temp_reg, temp_reg, -1);
4439 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004440 }
4441
4442 // And the default for any other value.
4443 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004444 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004445 }
4446}
4447
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004448void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4449 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4450}
4451
4452void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4453 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4454}
4455
Alexey Frunze4dda3372015-06-01 18:31:49 -07004456} // namespace mips64
4457} // namespace art